cfgexpand.c 182 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328
  1. /* A pass for lowering trees to RTL.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "rtl.h"
  20. #include "hard-reg-set.h"
  21. #include "hash-set.h"
  22. #include "machmode.h"
  23. #include "vec.h"
  24. #include "double-int.h"
  25. #include "input.h"
  26. #include "alias.h"
  27. #include "symtab.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "stringpool.h"
  33. #include "varasm.h"
  34. #include "stor-layout.h"
  35. #include "stmt.h"
  36. #include "print-tree.h"
  37. #include "tm_p.h"
  38. #include "predict.h"
  39. #include "hashtab.h"
  40. #include "function.h"
  41. #include "dominance.h"
  42. #include "cfg.h"
  43. #include "cfgrtl.h"
  44. #include "cfganal.h"
  45. #include "cfgbuild.h"
  46. #include "cfgcleanup.h"
  47. #include "basic-block.h"
  48. #include "insn-codes.h"
  49. #include "optabs.h"
  50. #include "flags.h"
  51. #include "statistics.h"
  52. #include "real.h"
  53. #include "fixed-value.h"
  54. #include "insn-config.h"
  55. #include "expmed.h"
  56. #include "dojump.h"
  57. #include "explow.h"
  58. #include "calls.h"
  59. #include "emit-rtl.h"
  60. #include "expr.h"
  61. #include "langhooks.h"
  62. #include "bitmap.h"
  63. #include "tree-ssa-alias.h"
  64. #include "internal-fn.h"
  65. #include "tree-eh.h"
  66. #include "gimple-expr.h"
  67. #include "is-a.h"
  68. #include "gimple.h"
  69. #include "gimple-iterator.h"
  70. #include "gimple-walk.h"
  71. #include "gimple-ssa.h"
  72. #include "hash-map.h"
  73. #include "plugin-api.h"
  74. #include "ipa-ref.h"
  75. #include "cgraph.h"
  76. #include "tree-cfg.h"
  77. #include "tree-phinodes.h"
  78. #include "ssa-iterators.h"
  79. #include "tree-ssanames.h"
  80. #include "tree-dfa.h"
  81. #include "tree-ssa.h"
  82. #include "tree-pass.h"
  83. #include "except.h"
  84. #include "diagnostic.h"
  85. #include "gimple-pretty-print.h"
  86. #include "toplev.h"
  87. #include "debug.h"
  88. #include "params.h"
  89. #include "tree-inline.h"
  90. #include "value-prof.h"
  91. #include "target.h"
  92. #include "tree-ssa-live.h"
  93. #include "tree-outof-ssa.h"
  94. #include "sbitmap.h"
  95. #include "cfgloop.h"
  96. #include "regs.h" /* For reg_renumber. */
  97. #include "insn-attr.h" /* For INSN_SCHEDULING. */
  98. #include "asan.h"
  99. #include "tree-ssa-address.h"
  100. #include "recog.h"
  101. #include "output.h"
  102. #include "builtins.h"
  103. #include "tree-chkp.h"
  104. #include "rtl-chkp.h"
  105. /* Some systems use __main in a way incompatible with its use in gcc, in these
  106. cases use the macros NAME__MAIN to give a quoted symbol and SYMBOL__MAIN to
  107. give the same symbol without quotes for an alternative entry point. You
  108. must define both, or neither. */
  109. #ifndef NAME__MAIN
  110. #define NAME__MAIN "__main"
  111. #endif
  112. /* This variable holds information helping the rewriting of SSA trees
  113. into RTL. */
  114. struct ssaexpand SA;
  115. /* This variable holds the currently expanded gimple statement for purposes
  116. of comminucating the profile info to the builtin expanders. */
  117. gimple currently_expanding_gimple_stmt;
  118. static rtx expand_debug_expr (tree);
  119. /* Return an expression tree corresponding to the RHS of GIMPLE
  120. statement STMT. */
  121. tree
  122. gimple_assign_rhs_to_tree (gimple stmt)
  123. {
  124. tree t;
  125. enum gimple_rhs_class grhs_class;
  126. grhs_class = get_gimple_rhs_class (gimple_expr_code (stmt));
  127. if (grhs_class == GIMPLE_TERNARY_RHS)
  128. t = build3 (gimple_assign_rhs_code (stmt),
  129. TREE_TYPE (gimple_assign_lhs (stmt)),
  130. gimple_assign_rhs1 (stmt),
  131. gimple_assign_rhs2 (stmt),
  132. gimple_assign_rhs3 (stmt));
  133. else if (grhs_class == GIMPLE_BINARY_RHS)
  134. t = build2 (gimple_assign_rhs_code (stmt),
  135. TREE_TYPE (gimple_assign_lhs (stmt)),
  136. gimple_assign_rhs1 (stmt),
  137. gimple_assign_rhs2 (stmt));
  138. else if (grhs_class == GIMPLE_UNARY_RHS)
  139. t = build1 (gimple_assign_rhs_code (stmt),
  140. TREE_TYPE (gimple_assign_lhs (stmt)),
  141. gimple_assign_rhs1 (stmt));
  142. else if (grhs_class == GIMPLE_SINGLE_RHS)
  143. {
  144. t = gimple_assign_rhs1 (stmt);
  145. /* Avoid modifying this tree in place below. */
  146. if ((gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t)
  147. && gimple_location (stmt) != EXPR_LOCATION (t))
  148. || (gimple_block (stmt)
  149. && currently_expanding_to_rtl
  150. && EXPR_P (t)))
  151. t = copy_node (t);
  152. }
  153. else
  154. gcc_unreachable ();
  155. if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (t))
  156. SET_EXPR_LOCATION (t, gimple_location (stmt));
  157. return t;
  158. }
  159. #ifndef STACK_ALIGNMENT_NEEDED
  160. #define STACK_ALIGNMENT_NEEDED 1
  161. #endif
  162. #define SSAVAR(x) (TREE_CODE (x) == SSA_NAME ? SSA_NAME_VAR (x) : x)
  163. /* Associate declaration T with storage space X. If T is no
  164. SSA name this is exactly SET_DECL_RTL, otherwise make the
  165. partition of T associated with X. */
  166. static inline void
  167. set_rtl (tree t, rtx x)
  168. {
  169. if (TREE_CODE (t) == SSA_NAME)
  170. {
  171. SA.partition_to_pseudo[var_to_partition (SA.map, t)] = x;
  172. if (x && !MEM_P (x))
  173. set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (t), x);
  174. /* For the benefit of debug information at -O0 (where vartracking
  175. doesn't run) record the place also in the base DECL if it's
  176. a normal variable (not a parameter). */
  177. if (x && x != pc_rtx && TREE_CODE (SSA_NAME_VAR (t)) == VAR_DECL)
  178. {
  179. tree var = SSA_NAME_VAR (t);
  180. /* If we don't yet have something recorded, just record it now. */
  181. if (!DECL_RTL_SET_P (var))
  182. SET_DECL_RTL (var, x);
  183. /* If we have it set already to "multiple places" don't
  184. change this. */
  185. else if (DECL_RTL (var) == pc_rtx)
  186. ;
  187. /* If we have something recorded and it's not the same place
  188. as we want to record now, we have multiple partitions for the
  189. same base variable, with different places. We can't just
  190. randomly chose one, hence we have to say that we don't know.
  191. This only happens with optimization, and there var-tracking
  192. will figure out the right thing. */
  193. else if (DECL_RTL (var) != x)
  194. SET_DECL_RTL (var, pc_rtx);
  195. }
  196. }
  197. else
  198. SET_DECL_RTL (t, x);
  199. }
  200. /* This structure holds data relevant to one variable that will be
  201. placed in a stack slot. */
  202. struct stack_var
  203. {
  204. /* The Variable. */
  205. tree decl;
  206. /* Initially, the size of the variable. Later, the size of the partition,
  207. if this variable becomes it's partition's representative. */
  208. HOST_WIDE_INT size;
  209. /* The *byte* alignment required for this variable. Or as, with the
  210. size, the alignment for this partition. */
  211. unsigned int alignb;
  212. /* The partition representative. */
  213. size_t representative;
  214. /* The next stack variable in the partition, or EOC. */
  215. size_t next;
  216. /* The numbers of conflicting stack variables. */
  217. bitmap conflicts;
  218. };
  219. #define EOC ((size_t)-1)
  220. /* We have an array of such objects while deciding allocation. */
  221. static struct stack_var *stack_vars;
  222. static size_t stack_vars_alloc;
  223. static size_t stack_vars_num;
  224. static hash_map<tree, size_t> *decl_to_stack_part;
  225. /* Conflict bitmaps go on this obstack. This allows us to destroy
  226. all of them in one big sweep. */
  227. static bitmap_obstack stack_var_bitmap_obstack;
  228. /* An array of indices such that stack_vars[stack_vars_sorted[i]].size
  229. is non-decreasing. */
  230. static size_t *stack_vars_sorted;
  231. /* The phase of the stack frame. This is the known misalignment of
  232. virtual_stack_vars_rtx from PREFERRED_STACK_BOUNDARY. That is,
  233. (frame_offset+frame_phase) % PREFERRED_STACK_BOUNDARY == 0. */
  234. static int frame_phase;
  235. /* Used during expand_used_vars to remember if we saw any decls for
  236. which we'd like to enable stack smashing protection. */
  237. static bool has_protected_decls;
  238. /* Used during expand_used_vars. Remember if we say a character buffer
  239. smaller than our cutoff threshold. Used for -Wstack-protector. */
  240. static bool has_short_buffer;
  241. /* Compute the byte alignment to use for DECL. Ignore alignment
  242. we can't do with expected alignment of the stack boundary. */
  243. static unsigned int
  244. align_local_variable (tree decl)
  245. {
  246. unsigned int align = LOCAL_DECL_ALIGNMENT (decl);
  247. DECL_ALIGN (decl) = align;
  248. return align / BITS_PER_UNIT;
  249. }
  250. /* Align given offset BASE with ALIGN. Truncate up if ALIGN_UP is true,
  251. down otherwise. Return truncated BASE value. */
  252. static inline unsigned HOST_WIDE_INT
  253. align_base (HOST_WIDE_INT base, unsigned HOST_WIDE_INT align, bool align_up)
  254. {
  255. return align_up ? (base + align - 1) & -align : base & -align;
  256. }
  257. /* Allocate SIZE bytes at byte alignment ALIGN from the stack frame.
  258. Return the frame offset. */
  259. static HOST_WIDE_INT
  260. alloc_stack_frame_space (HOST_WIDE_INT size, unsigned HOST_WIDE_INT align)
  261. {
  262. HOST_WIDE_INT offset, new_frame_offset;
  263. if (FRAME_GROWS_DOWNWARD)
  264. {
  265. new_frame_offset
  266. = align_base (frame_offset - frame_phase - size,
  267. align, false) + frame_phase;
  268. offset = new_frame_offset;
  269. }
  270. else
  271. {
  272. new_frame_offset
  273. = align_base (frame_offset - frame_phase, align, true) + frame_phase;
  274. offset = new_frame_offset;
  275. new_frame_offset += size;
  276. }
  277. frame_offset = new_frame_offset;
  278. if (frame_offset_overflow (frame_offset, cfun->decl))
  279. frame_offset = offset = 0;
  280. return offset;
  281. }
  282. /* Accumulate DECL into STACK_VARS. */
  283. static void
  284. add_stack_var (tree decl)
  285. {
  286. struct stack_var *v;
  287. if (stack_vars_num >= stack_vars_alloc)
  288. {
  289. if (stack_vars_alloc)
  290. stack_vars_alloc = stack_vars_alloc * 3 / 2;
  291. else
  292. stack_vars_alloc = 32;
  293. stack_vars
  294. = XRESIZEVEC (struct stack_var, stack_vars, stack_vars_alloc);
  295. }
  296. if (!decl_to_stack_part)
  297. decl_to_stack_part = new hash_map<tree, size_t>;
  298. v = &stack_vars[stack_vars_num];
  299. decl_to_stack_part->put (decl, stack_vars_num);
  300. v->decl = decl;
  301. v->size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (decl)));
  302. /* Ensure that all variables have size, so that &a != &b for any two
  303. variables that are simultaneously live. */
  304. if (v->size == 0)
  305. v->size = 1;
  306. v->alignb = align_local_variable (SSAVAR (decl));
  307. /* An alignment of zero can mightily confuse us later. */
  308. gcc_assert (v->alignb != 0);
  309. /* All variables are initially in their own partition. */
  310. v->representative = stack_vars_num;
  311. v->next = EOC;
  312. /* All variables initially conflict with no other. */
  313. v->conflicts = NULL;
  314. /* Ensure that this decl doesn't get put onto the list twice. */
  315. set_rtl (decl, pc_rtx);
  316. stack_vars_num++;
  317. }
  318. /* Make the decls associated with luid's X and Y conflict. */
  319. static void
  320. add_stack_var_conflict (size_t x, size_t y)
  321. {
  322. struct stack_var *a = &stack_vars[x];
  323. struct stack_var *b = &stack_vars[y];
  324. if (!a->conflicts)
  325. a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
  326. if (!b->conflicts)
  327. b->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
  328. bitmap_set_bit (a->conflicts, y);
  329. bitmap_set_bit (b->conflicts, x);
  330. }
  331. /* Check whether the decls associated with luid's X and Y conflict. */
  332. static bool
  333. stack_var_conflict_p (size_t x, size_t y)
  334. {
  335. struct stack_var *a = &stack_vars[x];
  336. struct stack_var *b = &stack_vars[y];
  337. if (x == y)
  338. return false;
  339. /* Partitions containing an SSA name result from gimple registers
  340. with things like unsupported modes. They are top-level and
  341. hence conflict with everything else. */
  342. if (TREE_CODE (a->decl) == SSA_NAME || TREE_CODE (b->decl) == SSA_NAME)
  343. return true;
  344. if (!a->conflicts || !b->conflicts)
  345. return false;
  346. return bitmap_bit_p (a->conflicts, y);
  347. }
  348. /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
  349. enter its partition number into bitmap DATA. */
  350. static bool
  351. visit_op (gimple, tree op, tree, void *data)
  352. {
  353. bitmap active = (bitmap)data;
  354. op = get_base_address (op);
  355. if (op
  356. && DECL_P (op)
  357. && DECL_RTL_IF_SET (op) == pc_rtx)
  358. {
  359. size_t *v = decl_to_stack_part->get (op);
  360. if (v)
  361. bitmap_set_bit (active, *v);
  362. }
  363. return false;
  364. }
  365. /* Callback for walk_stmt_ops. If OP is a decl touched by add_stack_var
  366. record conflicts between it and all currently active other partitions
  367. from bitmap DATA. */
  368. static bool
  369. visit_conflict (gimple, tree op, tree, void *data)
  370. {
  371. bitmap active = (bitmap)data;
  372. op = get_base_address (op);
  373. if (op
  374. && DECL_P (op)
  375. && DECL_RTL_IF_SET (op) == pc_rtx)
  376. {
  377. size_t *v = decl_to_stack_part->get (op);
  378. if (v && bitmap_set_bit (active, *v))
  379. {
  380. size_t num = *v;
  381. bitmap_iterator bi;
  382. unsigned i;
  383. gcc_assert (num < stack_vars_num);
  384. EXECUTE_IF_SET_IN_BITMAP (active, 0, i, bi)
  385. add_stack_var_conflict (num, i);
  386. }
  387. }
  388. return false;
  389. }
  390. /* Helper routine for add_scope_conflicts, calculating the active partitions
  391. at the end of BB, leaving the result in WORK. We're called to generate
  392. conflicts when FOR_CONFLICT is true, otherwise we're just tracking
  393. liveness. */
  394. static void
  395. add_scope_conflicts_1 (basic_block bb, bitmap work, bool for_conflict)
  396. {
  397. edge e;
  398. edge_iterator ei;
  399. gimple_stmt_iterator gsi;
  400. walk_stmt_load_store_addr_fn visit;
  401. bitmap_clear (work);
  402. FOR_EACH_EDGE (e, ei, bb->preds)
  403. bitmap_ior_into (work, (bitmap)e->src->aux);
  404. visit = visit_op;
  405. for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  406. {
  407. gimple stmt = gsi_stmt (gsi);
  408. walk_stmt_load_store_addr_ops (stmt, work, NULL, NULL, visit);
  409. }
  410. for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  411. {
  412. gimple stmt = gsi_stmt (gsi);
  413. if (gimple_clobber_p (stmt))
  414. {
  415. tree lhs = gimple_assign_lhs (stmt);
  416. size_t *v;
  417. /* Nested function lowering might introduce LHSs
  418. that are COMPONENT_REFs. */
  419. if (TREE_CODE (lhs) != VAR_DECL)
  420. continue;
  421. if (DECL_RTL_IF_SET (lhs) == pc_rtx
  422. && (v = decl_to_stack_part->get (lhs)))
  423. bitmap_clear_bit (work, *v);
  424. }
  425. else if (!is_gimple_debug (stmt))
  426. {
  427. if (for_conflict
  428. && visit == visit_op)
  429. {
  430. /* If this is the first real instruction in this BB we need
  431. to add conflicts for everything live at this point now.
  432. Unlike classical liveness for named objects we can't
  433. rely on seeing a def/use of the names we're interested in.
  434. There might merely be indirect loads/stores. We'd not add any
  435. conflicts for such partitions. */
  436. bitmap_iterator bi;
  437. unsigned i;
  438. EXECUTE_IF_SET_IN_BITMAP (work, 0, i, bi)
  439. {
  440. struct stack_var *a = &stack_vars[i];
  441. if (!a->conflicts)
  442. a->conflicts = BITMAP_ALLOC (&stack_var_bitmap_obstack);
  443. bitmap_ior_into (a->conflicts, work);
  444. }
  445. visit = visit_conflict;
  446. }
  447. walk_stmt_load_store_addr_ops (stmt, work, visit, visit, visit);
  448. }
  449. }
  450. }
  451. /* Generate stack partition conflicts between all partitions that are
  452. simultaneously live. */
  453. static void
  454. add_scope_conflicts (void)
  455. {
  456. basic_block bb;
  457. bool changed;
  458. bitmap work = BITMAP_ALLOC (NULL);
  459. int *rpo;
  460. int n_bbs;
  461. /* We approximate the live range of a stack variable by taking the first
  462. mention of its name as starting point(s), and by the end-of-scope
  463. death clobber added by gimplify as ending point(s) of the range.
  464. This overapproximates in the case we for instance moved an address-taken
  465. operation upward, without also moving a dereference to it upwards.
  466. But it's conservatively correct as a variable never can hold values
  467. before its name is mentioned at least once.
  468. We then do a mostly classical bitmap liveness algorithm. */
  469. FOR_ALL_BB_FN (bb, cfun)
  470. bb->aux = BITMAP_ALLOC (&stack_var_bitmap_obstack);
  471. rpo = XNEWVEC (int, last_basic_block_for_fn (cfun));
  472. n_bbs = pre_and_rev_post_order_compute (NULL, rpo, false);
  473. changed = true;
  474. while (changed)
  475. {
  476. int i;
  477. changed = false;
  478. for (i = 0; i < n_bbs; i++)
  479. {
  480. bitmap active;
  481. bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
  482. active = (bitmap)bb->aux;
  483. add_scope_conflicts_1 (bb, work, false);
  484. if (bitmap_ior_into (active, work))
  485. changed = true;
  486. }
  487. }
  488. FOR_EACH_BB_FN (bb, cfun)
  489. add_scope_conflicts_1 (bb, work, true);
  490. free (rpo);
  491. BITMAP_FREE (work);
  492. FOR_ALL_BB_FN (bb, cfun)
  493. BITMAP_FREE (bb->aux);
  494. }
  495. /* A subroutine of partition_stack_vars. A comparison function for qsort,
  496. sorting an array of indices by the properties of the object. */
  497. static int
  498. stack_var_cmp (const void *a, const void *b)
  499. {
  500. size_t ia = *(const size_t *)a;
  501. size_t ib = *(const size_t *)b;
  502. unsigned int aligna = stack_vars[ia].alignb;
  503. unsigned int alignb = stack_vars[ib].alignb;
  504. HOST_WIDE_INT sizea = stack_vars[ia].size;
  505. HOST_WIDE_INT sizeb = stack_vars[ib].size;
  506. tree decla = stack_vars[ia].decl;
  507. tree declb = stack_vars[ib].decl;
  508. bool largea, largeb;
  509. unsigned int uida, uidb;
  510. /* Primary compare on "large" alignment. Large comes first. */
  511. largea = (aligna * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
  512. largeb = (alignb * BITS_PER_UNIT > MAX_SUPPORTED_STACK_ALIGNMENT);
  513. if (largea != largeb)
  514. return (int)largeb - (int)largea;
  515. /* Secondary compare on size, decreasing */
  516. if (sizea > sizeb)
  517. return -1;
  518. if (sizea < sizeb)
  519. return 1;
  520. /* Tertiary compare on true alignment, decreasing. */
  521. if (aligna < alignb)
  522. return -1;
  523. if (aligna > alignb)
  524. return 1;
  525. /* Final compare on ID for sort stability, increasing.
  526. Two SSA names are compared by their version, SSA names come before
  527. non-SSA names, and two normal decls are compared by their DECL_UID. */
  528. if (TREE_CODE (decla) == SSA_NAME)
  529. {
  530. if (TREE_CODE (declb) == SSA_NAME)
  531. uida = SSA_NAME_VERSION (decla), uidb = SSA_NAME_VERSION (declb);
  532. else
  533. return -1;
  534. }
  535. else if (TREE_CODE (declb) == SSA_NAME)
  536. return 1;
  537. else
  538. uida = DECL_UID (decla), uidb = DECL_UID (declb);
  539. if (uida < uidb)
  540. return 1;
  541. if (uida > uidb)
  542. return -1;
  543. return 0;
  544. }
  545. struct part_traits : default_hashmap_traits
  546. {
  547. template<typename T>
  548. static bool
  549. is_deleted (T &e)
  550. { return e.m_value == reinterpret_cast<void *> (1); }
  551. template<typename T> static bool is_empty (T &e) { return e.m_value == NULL; }
  552. template<typename T>
  553. static void
  554. mark_deleted (T &e)
  555. { e.m_value = reinterpret_cast<T> (1); }
  556. template<typename T>
  557. static void
  558. mark_empty (T &e)
  559. { e.m_value = NULL; }
  560. };
  561. typedef hash_map<size_t, bitmap, part_traits> part_hashmap;
  562. /* If the points-to solution *PI points to variables that are in a partition
  563. together with other variables add all partition members to the pointed-to
  564. variables bitmap. */
  565. static void
  566. add_partitioned_vars_to_ptset (struct pt_solution *pt,
  567. part_hashmap *decls_to_partitions,
  568. hash_set<bitmap> *visited, bitmap temp)
  569. {
  570. bitmap_iterator bi;
  571. unsigned i;
  572. bitmap *part;
  573. if (pt->anything
  574. || pt->vars == NULL
  575. /* The pointed-to vars bitmap is shared, it is enough to
  576. visit it once. */
  577. || visited->add (pt->vars))
  578. return;
  579. bitmap_clear (temp);
  580. /* By using a temporary bitmap to store all members of the partitions
  581. we have to add we make sure to visit each of the partitions only
  582. once. */
  583. EXECUTE_IF_SET_IN_BITMAP (pt->vars, 0, i, bi)
  584. if ((!temp
  585. || !bitmap_bit_p (temp, i))
  586. && (part = decls_to_partitions->get (i)))
  587. bitmap_ior_into (temp, *part);
  588. if (!bitmap_empty_p (temp))
  589. bitmap_ior_into (pt->vars, temp);
  590. }
  591. /* Update points-to sets based on partition info, so we can use them on RTL.
  592. The bitmaps representing stack partitions will be saved until expand,
  593. where partitioned decls used as bases in memory expressions will be
  594. rewritten. */
  595. static void
  596. update_alias_info_with_stack_vars (void)
  597. {
  598. part_hashmap *decls_to_partitions = NULL;
  599. size_t i, j;
  600. tree var = NULL_TREE;
  601. for (i = 0; i < stack_vars_num; i++)
  602. {
  603. bitmap part = NULL;
  604. tree name;
  605. struct ptr_info_def *pi;
  606. /* Not interested in partitions with single variable. */
  607. if (stack_vars[i].representative != i
  608. || stack_vars[i].next == EOC)
  609. continue;
  610. if (!decls_to_partitions)
  611. {
  612. decls_to_partitions = new part_hashmap;
  613. cfun->gimple_df->decls_to_pointers = new hash_map<tree, tree>;
  614. }
  615. /* Create an SSA_NAME that points to the partition for use
  616. as base during alias-oracle queries on RTL for bases that
  617. have been partitioned. */
  618. if (var == NULL_TREE)
  619. var = create_tmp_var (ptr_type_node);
  620. name = make_ssa_name (var);
  621. /* Create bitmaps representing partitions. They will be used for
  622. points-to sets later, so use GGC alloc. */
  623. part = BITMAP_GGC_ALLOC ();
  624. for (j = i; j != EOC; j = stack_vars[j].next)
  625. {
  626. tree decl = stack_vars[j].decl;
  627. unsigned int uid = DECL_PT_UID (decl);
  628. bitmap_set_bit (part, uid);
  629. decls_to_partitions->put (uid, part);
  630. cfun->gimple_df->decls_to_pointers->put (decl, name);
  631. if (TREE_ADDRESSABLE (decl))
  632. TREE_ADDRESSABLE (name) = 1;
  633. }
  634. /* Make the SSA name point to all partition members. */
  635. pi = get_ptr_info (name);
  636. pt_solution_set (&pi->pt, part, false);
  637. }
  638. /* Make all points-to sets that contain one member of a partition
  639. contain all members of the partition. */
  640. if (decls_to_partitions)
  641. {
  642. unsigned i;
  643. hash_set<bitmap> visited;
  644. bitmap temp = BITMAP_ALLOC (&stack_var_bitmap_obstack);
  645. for (i = 1; i < num_ssa_names; i++)
  646. {
  647. tree name = ssa_name (i);
  648. struct ptr_info_def *pi;
  649. if (name
  650. && POINTER_TYPE_P (TREE_TYPE (name))
  651. && ((pi = SSA_NAME_PTR_INFO (name)) != NULL))
  652. add_partitioned_vars_to_ptset (&pi->pt, decls_to_partitions,
  653. &visited, temp);
  654. }
  655. add_partitioned_vars_to_ptset (&cfun->gimple_df->escaped,
  656. decls_to_partitions, &visited, temp);
  657. delete decls_to_partitions;
  658. BITMAP_FREE (temp);
  659. }
  660. }
  661. /* A subroutine of partition_stack_vars. The UNION portion of a UNION/FIND
  662. partitioning algorithm. Partitions A and B are known to be non-conflicting.
  663. Merge them into a single partition A. */
  664. static void
  665. union_stack_vars (size_t a, size_t b)
  666. {
  667. struct stack_var *vb = &stack_vars[b];
  668. bitmap_iterator bi;
  669. unsigned u;
  670. gcc_assert (stack_vars[b].next == EOC);
  671. /* Add B to A's partition. */
  672. stack_vars[b].next = stack_vars[a].next;
  673. stack_vars[b].representative = a;
  674. stack_vars[a].next = b;
  675. /* Update the required alignment of partition A to account for B. */
  676. if (stack_vars[a].alignb < stack_vars[b].alignb)
  677. stack_vars[a].alignb = stack_vars[b].alignb;
  678. /* Update the interference graph and merge the conflicts. */
  679. if (vb->conflicts)
  680. {
  681. EXECUTE_IF_SET_IN_BITMAP (vb->conflicts, 0, u, bi)
  682. add_stack_var_conflict (a, stack_vars[u].representative);
  683. BITMAP_FREE (vb->conflicts);
  684. }
  685. }
  686. /* A subroutine of expand_used_vars. Binpack the variables into
  687. partitions constrained by the interference graph. The overall
  688. algorithm used is as follows:
  689. Sort the objects by size in descending order.
  690. For each object A {
  691. S = size(A)
  692. O = 0
  693. loop {
  694. Look for the largest non-conflicting object B with size <= S.
  695. UNION (A, B)
  696. }
  697. }
  698. */
  699. static void
  700. partition_stack_vars (void)
  701. {
  702. size_t si, sj, n = stack_vars_num;
  703. stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
  704. for (si = 0; si < n; ++si)
  705. stack_vars_sorted[si] = si;
  706. if (n == 1)
  707. return;
  708. qsort (stack_vars_sorted, n, sizeof (size_t), stack_var_cmp);
  709. for (si = 0; si < n; ++si)
  710. {
  711. size_t i = stack_vars_sorted[si];
  712. unsigned int ialign = stack_vars[i].alignb;
  713. HOST_WIDE_INT isize = stack_vars[i].size;
  714. /* Ignore objects that aren't partition representatives. If we
  715. see a var that is not a partition representative, it must
  716. have been merged earlier. */
  717. if (stack_vars[i].representative != i)
  718. continue;
  719. for (sj = si + 1; sj < n; ++sj)
  720. {
  721. size_t j = stack_vars_sorted[sj];
  722. unsigned int jalign = stack_vars[j].alignb;
  723. HOST_WIDE_INT jsize = stack_vars[j].size;
  724. /* Ignore objects that aren't partition representatives. */
  725. if (stack_vars[j].representative != j)
  726. continue;
  727. /* Do not mix objects of "small" (supported) alignment
  728. and "large" (unsupported) alignment. */
  729. if ((ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
  730. != (jalign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT))
  731. break;
  732. /* For Address Sanitizer do not mix objects with different
  733. sizes, as the shorter vars wouldn't be adequately protected.
  734. Don't do that for "large" (unsupported) alignment objects,
  735. those aren't protected anyway. */
  736. if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && isize != jsize
  737. && ialign * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
  738. break;
  739. /* Ignore conflicting objects. */
  740. if (stack_var_conflict_p (i, j))
  741. continue;
  742. /* UNION the objects, placing J at OFFSET. */
  743. union_stack_vars (i, j);
  744. }
  745. }
  746. update_alias_info_with_stack_vars ();
  747. }
  748. /* A debugging aid for expand_used_vars. Dump the generated partitions. */
  749. static void
  750. dump_stack_var_partition (void)
  751. {
  752. size_t si, i, j, n = stack_vars_num;
  753. for (si = 0; si < n; ++si)
  754. {
  755. i = stack_vars_sorted[si];
  756. /* Skip variables that aren't partition representatives, for now. */
  757. if (stack_vars[i].representative != i)
  758. continue;
  759. fprintf (dump_file, "Partition %lu: size " HOST_WIDE_INT_PRINT_DEC
  760. " align %u\n", (unsigned long) i, stack_vars[i].size,
  761. stack_vars[i].alignb);
  762. for (j = i; j != EOC; j = stack_vars[j].next)
  763. {
  764. fputc ('\t', dump_file);
  765. print_generic_expr (dump_file, stack_vars[j].decl, dump_flags);
  766. }
  767. fputc ('\n', dump_file);
  768. }
  769. }
  770. /* Assign rtl to DECL at BASE + OFFSET. */
  771. static void
  772. expand_one_stack_var_at (tree decl, rtx base, unsigned base_align,
  773. HOST_WIDE_INT offset)
  774. {
  775. unsigned align;
  776. rtx x;
  777. /* If this fails, we've overflowed the stack frame. Error nicely? */
  778. gcc_assert (offset == trunc_int_for_mode (offset, Pmode));
  779. x = plus_constant (Pmode, base, offset);
  780. x = gen_rtx_MEM (DECL_MODE (SSAVAR (decl)), x);
  781. if (TREE_CODE (decl) != SSA_NAME)
  782. {
  783. /* Set alignment we actually gave this decl if it isn't an SSA name.
  784. If it is we generate stack slots only accidentally so it isn't as
  785. important, we'll simply use the alignment that is already set. */
  786. if (base == virtual_stack_vars_rtx)
  787. offset -= frame_phase;
  788. align = offset & -offset;
  789. align *= BITS_PER_UNIT;
  790. if (align == 0 || align > base_align)
  791. align = base_align;
  792. /* One would think that we could assert that we're not decreasing
  793. alignment here, but (at least) the i386 port does exactly this
  794. via the MINIMUM_ALIGNMENT hook. */
  795. DECL_ALIGN (decl) = align;
  796. DECL_USER_ALIGN (decl) = 0;
  797. }
  798. set_mem_attributes (x, SSAVAR (decl), true);
  799. set_rtl (decl, x);
  800. }
  801. struct stack_vars_data
  802. {
  803. /* Vector of offset pairs, always end of some padding followed
  804. by start of the padding that needs Address Sanitizer protection.
  805. The vector is in reversed, highest offset pairs come first. */
  806. vec<HOST_WIDE_INT> asan_vec;
  807. /* Vector of partition representative decls in between the paddings. */
  808. vec<tree> asan_decl_vec;
  809. /* Base pseudo register for Address Sanitizer protected automatic vars. */
  810. rtx asan_base;
  811. /* Alignment needed for the Address Sanitizer protected automatic vars. */
  812. unsigned int asan_alignb;
  813. };
  814. /* A subroutine of expand_used_vars. Give each partition representative
  815. a unique location within the stack frame. Update each partition member
  816. with that location. */
  817. static void
  818. expand_stack_vars (bool (*pred) (size_t), struct stack_vars_data *data)
  819. {
  820. size_t si, i, j, n = stack_vars_num;
  821. HOST_WIDE_INT large_size = 0, large_alloc = 0;
  822. rtx large_base = NULL;
  823. unsigned large_align = 0;
  824. tree decl;
  825. /* Determine if there are any variables requiring "large" alignment.
  826. Since these are dynamically allocated, we only process these if
  827. no predicate involved. */
  828. large_align = stack_vars[stack_vars_sorted[0]].alignb * BITS_PER_UNIT;
  829. if (pred == NULL && large_align > MAX_SUPPORTED_STACK_ALIGNMENT)
  830. {
  831. /* Find the total size of these variables. */
  832. for (si = 0; si < n; ++si)
  833. {
  834. unsigned alignb;
  835. i = stack_vars_sorted[si];
  836. alignb = stack_vars[i].alignb;
  837. /* All "large" alignment decls come before all "small" alignment
  838. decls, but "large" alignment decls are not sorted based on
  839. their alignment. Increase large_align to track the largest
  840. required alignment. */
  841. if ((alignb * BITS_PER_UNIT) > large_align)
  842. large_align = alignb * BITS_PER_UNIT;
  843. /* Stop when we get to the first decl with "small" alignment. */
  844. if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
  845. break;
  846. /* Skip variables that aren't partition representatives. */
  847. if (stack_vars[i].representative != i)
  848. continue;
  849. /* Skip variables that have already had rtl assigned. See also
  850. add_stack_var where we perpetrate this pc_rtx hack. */
  851. decl = stack_vars[i].decl;
  852. if ((TREE_CODE (decl) == SSA_NAME
  853. ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
  854. : DECL_RTL (decl)) != pc_rtx)
  855. continue;
  856. large_size += alignb - 1;
  857. large_size &= -(HOST_WIDE_INT)alignb;
  858. large_size += stack_vars[i].size;
  859. }
  860. /* If there were any, allocate space. */
  861. if (large_size > 0)
  862. large_base = allocate_dynamic_stack_space (GEN_INT (large_size), 0,
  863. large_align, true);
  864. }
  865. for (si = 0; si < n; ++si)
  866. {
  867. rtx base;
  868. unsigned base_align, alignb;
  869. HOST_WIDE_INT offset;
  870. i = stack_vars_sorted[si];
  871. /* Skip variables that aren't partition representatives, for now. */
  872. if (stack_vars[i].representative != i)
  873. continue;
  874. /* Skip variables that have already had rtl assigned. See also
  875. add_stack_var where we perpetrate this pc_rtx hack. */
  876. decl = stack_vars[i].decl;
  877. if ((TREE_CODE (decl) == SSA_NAME
  878. ? SA.partition_to_pseudo[var_to_partition (SA.map, decl)]
  879. : DECL_RTL (decl)) != pc_rtx)
  880. continue;
  881. /* Check the predicate to see whether this variable should be
  882. allocated in this pass. */
  883. if (pred && !pred (i))
  884. continue;
  885. alignb = stack_vars[i].alignb;
  886. if (alignb * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT)
  887. {
  888. base = virtual_stack_vars_rtx;
  889. if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK && pred)
  890. {
  891. HOST_WIDE_INT prev_offset
  892. = align_base (frame_offset,
  893. MAX (alignb, ASAN_RED_ZONE_SIZE),
  894. FRAME_GROWS_DOWNWARD);
  895. tree repr_decl = NULL_TREE;
  896. offset
  897. = alloc_stack_frame_space (stack_vars[i].size
  898. + ASAN_RED_ZONE_SIZE,
  899. MAX (alignb, ASAN_RED_ZONE_SIZE));
  900. data->asan_vec.safe_push (prev_offset);
  901. data->asan_vec.safe_push (offset + stack_vars[i].size);
  902. /* Find best representative of the partition.
  903. Prefer those with DECL_NAME, even better
  904. satisfying asan_protect_stack_decl predicate. */
  905. for (j = i; j != EOC; j = stack_vars[j].next)
  906. if (asan_protect_stack_decl (stack_vars[j].decl)
  907. && DECL_NAME (stack_vars[j].decl))
  908. {
  909. repr_decl = stack_vars[j].decl;
  910. break;
  911. }
  912. else if (repr_decl == NULL_TREE
  913. && DECL_P (stack_vars[j].decl)
  914. && DECL_NAME (stack_vars[j].decl))
  915. repr_decl = stack_vars[j].decl;
  916. if (repr_decl == NULL_TREE)
  917. repr_decl = stack_vars[i].decl;
  918. data->asan_decl_vec.safe_push (repr_decl);
  919. data->asan_alignb = MAX (data->asan_alignb, alignb);
  920. if (data->asan_base == NULL)
  921. data->asan_base = gen_reg_rtx (Pmode);
  922. base = data->asan_base;
  923. if (!STRICT_ALIGNMENT)
  924. base_align = crtl->max_used_stack_slot_alignment;
  925. else
  926. base_align = MAX (crtl->max_used_stack_slot_alignment,
  927. GET_MODE_ALIGNMENT (SImode)
  928. << ASAN_SHADOW_SHIFT);
  929. }
  930. else
  931. {
  932. offset = alloc_stack_frame_space (stack_vars[i].size, alignb);
  933. base_align = crtl->max_used_stack_slot_alignment;
  934. }
  935. }
  936. else
  937. {
  938. /* Large alignment is only processed in the last pass. */
  939. if (pred)
  940. continue;
  941. gcc_assert (large_base != NULL);
  942. large_alloc += alignb - 1;
  943. large_alloc &= -(HOST_WIDE_INT)alignb;
  944. offset = large_alloc;
  945. large_alloc += stack_vars[i].size;
  946. base = large_base;
  947. base_align = large_align;
  948. }
  949. /* Create rtl for each variable based on their location within the
  950. partition. */
  951. for (j = i; j != EOC; j = stack_vars[j].next)
  952. {
  953. expand_one_stack_var_at (stack_vars[j].decl,
  954. base, base_align,
  955. offset);
  956. }
  957. }
  958. gcc_assert (large_alloc == large_size);
  959. }
  960. /* Take into account all sizes of partitions and reset DECL_RTLs. */
  961. static HOST_WIDE_INT
  962. account_stack_vars (void)
  963. {
  964. size_t si, j, i, n = stack_vars_num;
  965. HOST_WIDE_INT size = 0;
  966. for (si = 0; si < n; ++si)
  967. {
  968. i = stack_vars_sorted[si];
  969. /* Skip variables that aren't partition representatives, for now. */
  970. if (stack_vars[i].representative != i)
  971. continue;
  972. size += stack_vars[i].size;
  973. for (j = i; j != EOC; j = stack_vars[j].next)
  974. set_rtl (stack_vars[j].decl, NULL);
  975. }
  976. return size;
  977. }
  978. /* A subroutine of expand_one_var. Called to immediately assign rtl
  979. to a variable to be allocated in the stack frame. */
  980. static void
  981. expand_one_stack_var (tree var)
  982. {
  983. HOST_WIDE_INT size, offset;
  984. unsigned byte_align;
  985. size = tree_to_uhwi (DECL_SIZE_UNIT (SSAVAR (var)));
  986. byte_align = align_local_variable (SSAVAR (var));
  987. /* We handle highly aligned variables in expand_stack_vars. */
  988. gcc_assert (byte_align * BITS_PER_UNIT <= MAX_SUPPORTED_STACK_ALIGNMENT);
  989. offset = alloc_stack_frame_space (size, byte_align);
  990. expand_one_stack_var_at (var, virtual_stack_vars_rtx,
  991. crtl->max_used_stack_slot_alignment, offset);
  992. }
  993. /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
  994. that will reside in a hard register. */
  995. static void
  996. expand_one_hard_reg_var (tree var)
  997. {
  998. rest_of_decl_compilation (var, 0, 0);
  999. }
  1000. /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL
  1001. that will reside in a pseudo register. */
  1002. static void
  1003. expand_one_register_var (tree var)
  1004. {
  1005. tree decl = SSAVAR (var);
  1006. tree type = TREE_TYPE (decl);
  1007. machine_mode reg_mode = promote_decl_mode (decl, NULL);
  1008. rtx x = gen_reg_rtx (reg_mode);
  1009. set_rtl (var, x);
  1010. /* Note if the object is a user variable. */
  1011. if (!DECL_ARTIFICIAL (decl))
  1012. mark_user_reg (x);
  1013. if (POINTER_TYPE_P (type))
  1014. mark_reg_pointer (x, get_pointer_alignment (var));
  1015. }
  1016. /* A subroutine of expand_one_var. Called to assign rtl to a VAR_DECL that
  1017. has some associated error, e.g. its type is error-mark. We just need
  1018. to pick something that won't crash the rest of the compiler. */
  1019. static void
  1020. expand_one_error_var (tree var)
  1021. {
  1022. machine_mode mode = DECL_MODE (var);
  1023. rtx x;
  1024. if (mode == BLKmode)
  1025. x = gen_rtx_MEM (BLKmode, const0_rtx);
  1026. else if (mode == VOIDmode)
  1027. x = const0_rtx;
  1028. else
  1029. x = gen_reg_rtx (mode);
  1030. SET_DECL_RTL (var, x);
  1031. }
  1032. /* A subroutine of expand_one_var. VAR is a variable that will be
  1033. allocated to the local stack frame. Return true if we wish to
  1034. add VAR to STACK_VARS so that it will be coalesced with other
  1035. variables. Return false to allocate VAR immediately.
  1036. This function is used to reduce the number of variables considered
  1037. for coalescing, which reduces the size of the quadratic problem. */
  1038. static bool
  1039. defer_stack_allocation (tree var, bool toplevel)
  1040. {
  1041. /* Whether the variable is small enough for immediate allocation not to be
  1042. a problem with regard to the frame size. */
  1043. bool smallish
  1044. = ((HOST_WIDE_INT) tree_to_uhwi (DECL_SIZE_UNIT (var))
  1045. < PARAM_VALUE (PARAM_MIN_SIZE_FOR_STACK_SHARING));
  1046. /* If stack protection is enabled, *all* stack variables must be deferred,
  1047. so that we can re-order the strings to the top of the frame.
  1048. Similarly for Address Sanitizer. */
  1049. if (flag_stack_protect || ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK))
  1050. return true;
  1051. /* We handle "large" alignment via dynamic allocation. We want to handle
  1052. this extra complication in only one place, so defer them. */
  1053. if (DECL_ALIGN (var) > MAX_SUPPORTED_STACK_ALIGNMENT)
  1054. return true;
  1055. /* When optimization is enabled, DECL_IGNORED_P variables originally scoped
  1056. might be detached from their block and appear at toplevel when we reach
  1057. here. We want to coalesce them with variables from other blocks when
  1058. the immediate contribution to the frame size would be noticeable. */
  1059. if (toplevel && optimize > 0 && DECL_IGNORED_P (var) && !smallish)
  1060. return true;
  1061. /* Variables declared in the outermost scope automatically conflict
  1062. with every other variable. The only reason to want to defer them
  1063. at all is that, after sorting, we can more efficiently pack
  1064. small variables in the stack frame. Continue to defer at -O2. */
  1065. if (toplevel && optimize < 2)
  1066. return false;
  1067. /* Without optimization, *most* variables are allocated from the
  1068. stack, which makes the quadratic problem large exactly when we
  1069. want compilation to proceed as quickly as possible. On the
  1070. other hand, we don't want the function's stack frame size to
  1071. get completely out of hand. So we avoid adding scalars and
  1072. "small" aggregates to the list at all. */
  1073. if (optimize == 0 && smallish)
  1074. return false;
  1075. return true;
  1076. }
  1077. /* A subroutine of expand_used_vars. Expand one variable according to
  1078. its flavor. Variables to be placed on the stack are not actually
  1079. expanded yet, merely recorded.
  1080. When REALLY_EXPAND is false, only add stack values to be allocated.
  1081. Return stack usage this variable is supposed to take.
  1082. */
  1083. static HOST_WIDE_INT
  1084. expand_one_var (tree var, bool toplevel, bool really_expand)
  1085. {
  1086. unsigned int align = BITS_PER_UNIT;
  1087. tree origvar = var;
  1088. var = SSAVAR (var);
  1089. if (TREE_TYPE (var) != error_mark_node && TREE_CODE (var) == VAR_DECL)
  1090. {
  1091. /* Because we don't know if VAR will be in register or on stack,
  1092. we conservatively assume it will be on stack even if VAR is
  1093. eventually put into register after RA pass. For non-automatic
  1094. variables, which won't be on stack, we collect alignment of
  1095. type and ignore user specified alignment. Similarly for
  1096. SSA_NAMEs for which use_register_for_decl returns true. */
  1097. if (TREE_STATIC (var)
  1098. || DECL_EXTERNAL (var)
  1099. || (TREE_CODE (origvar) == SSA_NAME && use_register_for_decl (var)))
  1100. align = MINIMUM_ALIGNMENT (TREE_TYPE (var),
  1101. TYPE_MODE (TREE_TYPE (var)),
  1102. TYPE_ALIGN (TREE_TYPE (var)));
  1103. else if (DECL_HAS_VALUE_EXPR_P (var)
  1104. || (DECL_RTL_SET_P (var) && MEM_P (DECL_RTL (var))))
  1105. /* Don't consider debug only variables with DECL_HAS_VALUE_EXPR_P set
  1106. or variables which were assigned a stack slot already by
  1107. expand_one_stack_var_at - in the latter case DECL_ALIGN has been
  1108. changed from the offset chosen to it. */
  1109. align = crtl->stack_alignment_estimated;
  1110. else
  1111. align = MINIMUM_ALIGNMENT (var, DECL_MODE (var), DECL_ALIGN (var));
  1112. /* If the variable alignment is very large we'll dynamicaly allocate
  1113. it, which means that in-frame portion is just a pointer. */
  1114. if (align > MAX_SUPPORTED_STACK_ALIGNMENT)
  1115. align = POINTER_SIZE;
  1116. }
  1117. if (SUPPORTS_STACK_ALIGNMENT
  1118. && crtl->stack_alignment_estimated < align)
  1119. {
  1120. /* stack_alignment_estimated shouldn't change after stack
  1121. realign decision made */
  1122. gcc_assert (!crtl->stack_realign_processed);
  1123. crtl->stack_alignment_estimated = align;
  1124. }
  1125. /* stack_alignment_needed > PREFERRED_STACK_BOUNDARY is permitted.
  1126. So here we only make sure stack_alignment_needed >= align. */
  1127. if (crtl->stack_alignment_needed < align)
  1128. crtl->stack_alignment_needed = align;
  1129. if (crtl->max_used_stack_slot_alignment < align)
  1130. crtl->max_used_stack_slot_alignment = align;
  1131. if (TREE_CODE (origvar) == SSA_NAME)
  1132. {
  1133. gcc_assert (TREE_CODE (var) != VAR_DECL
  1134. || (!DECL_EXTERNAL (var)
  1135. && !DECL_HAS_VALUE_EXPR_P (var)
  1136. && !TREE_STATIC (var)
  1137. && TREE_TYPE (var) != error_mark_node
  1138. && !DECL_HARD_REGISTER (var)
  1139. && really_expand));
  1140. }
  1141. if (TREE_CODE (var) != VAR_DECL && TREE_CODE (origvar) != SSA_NAME)
  1142. ;
  1143. else if (DECL_EXTERNAL (var))
  1144. ;
  1145. else if (DECL_HAS_VALUE_EXPR_P (var))
  1146. ;
  1147. else if (TREE_STATIC (var))
  1148. ;
  1149. else if (TREE_CODE (origvar) != SSA_NAME && DECL_RTL_SET_P (var))
  1150. ;
  1151. else if (TREE_TYPE (var) == error_mark_node)
  1152. {
  1153. if (really_expand)
  1154. expand_one_error_var (var);
  1155. }
  1156. else if (TREE_CODE (var) == VAR_DECL && DECL_HARD_REGISTER (var))
  1157. {
  1158. if (really_expand)
  1159. {
  1160. expand_one_hard_reg_var (var);
  1161. if (!DECL_HARD_REGISTER (var))
  1162. /* Invalid register specification. */
  1163. expand_one_error_var (var);
  1164. }
  1165. }
  1166. else if (use_register_for_decl (var))
  1167. {
  1168. if (really_expand)
  1169. expand_one_register_var (origvar);
  1170. }
  1171. else if (! valid_constant_size_p (DECL_SIZE_UNIT (var)))
  1172. {
  1173. /* Reject variables which cover more than half of the address-space. */
  1174. if (really_expand)
  1175. {
  1176. error ("size of variable %q+D is too large", var);
  1177. expand_one_error_var (var);
  1178. }
  1179. }
  1180. else if (defer_stack_allocation (var, toplevel))
  1181. add_stack_var (origvar);
  1182. else
  1183. {
  1184. if (really_expand)
  1185. expand_one_stack_var (origvar);
  1186. return tree_to_uhwi (DECL_SIZE_UNIT (var));
  1187. }
  1188. return 0;
  1189. }
  1190. /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
  1191. expanding variables. Those variables that can be put into registers
  1192. are allocated pseudos; those that can't are put on the stack.
  1193. TOPLEVEL is true if this is the outermost BLOCK. */
  1194. static void
  1195. expand_used_vars_for_block (tree block, bool toplevel)
  1196. {
  1197. tree t;
  1198. /* Expand all variables at this level. */
  1199. for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
  1200. if (TREE_USED (t)
  1201. && ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
  1202. || !DECL_NONSHAREABLE (t)))
  1203. expand_one_var (t, toplevel, true);
  1204. /* Expand all variables at containing levels. */
  1205. for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
  1206. expand_used_vars_for_block (t, false);
  1207. }
  1208. /* A subroutine of expand_used_vars. Walk down through the BLOCK tree
  1209. and clear TREE_USED on all local variables. */
  1210. static void
  1211. clear_tree_used (tree block)
  1212. {
  1213. tree t;
  1214. for (t = BLOCK_VARS (block); t ; t = DECL_CHAIN (t))
  1215. /* if (!TREE_STATIC (t) && !DECL_EXTERNAL (t)) */
  1216. if ((TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != RESULT_DECL)
  1217. || !DECL_NONSHAREABLE (t))
  1218. TREE_USED (t) = 0;
  1219. for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
  1220. clear_tree_used (t);
  1221. }
  1222. enum {
  1223. SPCT_FLAG_DEFAULT = 1,
  1224. SPCT_FLAG_ALL = 2,
  1225. SPCT_FLAG_STRONG = 3,
  1226. SPCT_FLAG_EXPLICIT = 4
  1227. };
  1228. /* Examine TYPE and determine a bit mask of the following features. */
  1229. #define SPCT_HAS_LARGE_CHAR_ARRAY 1
  1230. #define SPCT_HAS_SMALL_CHAR_ARRAY 2
  1231. #define SPCT_HAS_ARRAY 4
  1232. #define SPCT_HAS_AGGREGATE 8
  1233. static unsigned int
  1234. stack_protect_classify_type (tree type)
  1235. {
  1236. unsigned int ret = 0;
  1237. tree t;
  1238. switch (TREE_CODE (type))
  1239. {
  1240. case ARRAY_TYPE:
  1241. t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
  1242. if (t == char_type_node
  1243. || t == signed_char_type_node
  1244. || t == unsigned_char_type_node)
  1245. {
  1246. unsigned HOST_WIDE_INT max = PARAM_VALUE (PARAM_SSP_BUFFER_SIZE);
  1247. unsigned HOST_WIDE_INT len;
  1248. if (!TYPE_SIZE_UNIT (type)
  1249. || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
  1250. len = max;
  1251. else
  1252. len = tree_to_uhwi (TYPE_SIZE_UNIT (type));
  1253. if (len < max)
  1254. ret = SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_ARRAY;
  1255. else
  1256. ret = SPCT_HAS_LARGE_CHAR_ARRAY | SPCT_HAS_ARRAY;
  1257. }
  1258. else
  1259. ret = SPCT_HAS_ARRAY;
  1260. break;
  1261. case UNION_TYPE:
  1262. case QUAL_UNION_TYPE:
  1263. case RECORD_TYPE:
  1264. ret = SPCT_HAS_AGGREGATE;
  1265. for (t = TYPE_FIELDS (type); t ; t = TREE_CHAIN (t))
  1266. if (TREE_CODE (t) == FIELD_DECL)
  1267. ret |= stack_protect_classify_type (TREE_TYPE (t));
  1268. break;
  1269. default:
  1270. break;
  1271. }
  1272. return ret;
  1273. }
  1274. /* Return nonzero if DECL should be segregated into the "vulnerable" upper
  1275. part of the local stack frame. Remember if we ever return nonzero for
  1276. any variable in this function. The return value is the phase number in
  1277. which the variable should be allocated. */
  1278. static int
  1279. stack_protect_decl_phase (tree decl)
  1280. {
  1281. unsigned int bits = stack_protect_classify_type (TREE_TYPE (decl));
  1282. int ret = 0;
  1283. if (bits & SPCT_HAS_SMALL_CHAR_ARRAY)
  1284. has_short_buffer = true;
  1285. if (flag_stack_protect == SPCT_FLAG_ALL
  1286. || flag_stack_protect == SPCT_FLAG_STRONG
  1287. || (flag_stack_protect == SPCT_FLAG_EXPLICIT
  1288. && lookup_attribute ("stack_protect",
  1289. DECL_ATTRIBUTES (current_function_decl))))
  1290. {
  1291. if ((bits & (SPCT_HAS_SMALL_CHAR_ARRAY | SPCT_HAS_LARGE_CHAR_ARRAY))
  1292. && !(bits & SPCT_HAS_AGGREGATE))
  1293. ret = 1;
  1294. else if (bits & SPCT_HAS_ARRAY)
  1295. ret = 2;
  1296. }
  1297. else
  1298. ret = (bits & SPCT_HAS_LARGE_CHAR_ARRAY) != 0;
  1299. if (ret)
  1300. has_protected_decls = true;
  1301. return ret;
  1302. }
  1303. /* Two helper routines that check for phase 1 and phase 2. These are used
  1304. as callbacks for expand_stack_vars. */
  1305. static bool
  1306. stack_protect_decl_phase_1 (size_t i)
  1307. {
  1308. return stack_protect_decl_phase (stack_vars[i].decl) == 1;
  1309. }
  1310. static bool
  1311. stack_protect_decl_phase_2 (size_t i)
  1312. {
  1313. return stack_protect_decl_phase (stack_vars[i].decl) == 2;
  1314. }
  1315. /* And helper function that checks for asan phase (with stack protector
  1316. it is phase 3). This is used as callback for expand_stack_vars.
  1317. Returns true if any of the vars in the partition need to be protected. */
  1318. static bool
  1319. asan_decl_phase_3 (size_t i)
  1320. {
  1321. while (i != EOC)
  1322. {
  1323. if (asan_protect_stack_decl (stack_vars[i].decl))
  1324. return true;
  1325. i = stack_vars[i].next;
  1326. }
  1327. return false;
  1328. }
  1329. /* Ensure that variables in different stack protection phases conflict
  1330. so that they are not merged and share the same stack slot. */
  1331. static void
  1332. add_stack_protection_conflicts (void)
  1333. {
  1334. size_t i, j, n = stack_vars_num;
  1335. unsigned char *phase;
  1336. phase = XNEWVEC (unsigned char, n);
  1337. for (i = 0; i < n; ++i)
  1338. phase[i] = stack_protect_decl_phase (stack_vars[i].decl);
  1339. for (i = 0; i < n; ++i)
  1340. {
  1341. unsigned char ph_i = phase[i];
  1342. for (j = i + 1; j < n; ++j)
  1343. if (ph_i != phase[j])
  1344. add_stack_var_conflict (i, j);
  1345. }
  1346. XDELETEVEC (phase);
  1347. }
  1348. /* Create a decl for the guard at the top of the stack frame. */
  1349. static void
  1350. create_stack_guard (void)
  1351. {
  1352. tree guard = build_decl (DECL_SOURCE_LOCATION (current_function_decl),
  1353. VAR_DECL, NULL, ptr_type_node);
  1354. TREE_THIS_VOLATILE (guard) = 1;
  1355. TREE_USED (guard) = 1;
  1356. expand_one_stack_var (guard);
  1357. crtl->stack_protect_guard = guard;
  1358. }
  1359. /* Prepare for expanding variables. */
  1360. static void
  1361. init_vars_expansion (void)
  1362. {
  1363. /* Conflict bitmaps, and a few related temporary bitmaps, go here. */
  1364. bitmap_obstack_initialize (&stack_var_bitmap_obstack);
  1365. /* A map from decl to stack partition. */
  1366. decl_to_stack_part = new hash_map<tree, size_t>;
  1367. /* Initialize local stack smashing state. */
  1368. has_protected_decls = false;
  1369. has_short_buffer = false;
  1370. }
  1371. /* Free up stack variable graph data. */
  1372. static void
  1373. fini_vars_expansion (void)
  1374. {
  1375. bitmap_obstack_release (&stack_var_bitmap_obstack);
  1376. if (stack_vars)
  1377. XDELETEVEC (stack_vars);
  1378. if (stack_vars_sorted)
  1379. XDELETEVEC (stack_vars_sorted);
  1380. stack_vars = NULL;
  1381. stack_vars_sorted = NULL;
  1382. stack_vars_alloc = stack_vars_num = 0;
  1383. delete decl_to_stack_part;
  1384. decl_to_stack_part = NULL;
  1385. }
  1386. /* Make a fair guess for the size of the stack frame of the function
  1387. in NODE. This doesn't have to be exact, the result is only used in
  1388. the inline heuristics. So we don't want to run the full stack var
  1389. packing algorithm (which is quadratic in the number of stack vars).
  1390. Instead, we calculate the total size of all stack vars. This turns
  1391. out to be a pretty fair estimate -- packing of stack vars doesn't
  1392. happen very often. */
  1393. HOST_WIDE_INT
  1394. estimated_stack_frame_size (struct cgraph_node *node)
  1395. {
  1396. HOST_WIDE_INT size = 0;
  1397. size_t i;
  1398. tree var;
  1399. struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
  1400. push_cfun (fn);
  1401. init_vars_expansion ();
  1402. FOR_EACH_LOCAL_DECL (fn, i, var)
  1403. if (auto_var_in_fn_p (var, fn->decl))
  1404. size += expand_one_var (var, true, false);
  1405. if (stack_vars_num > 0)
  1406. {
  1407. /* Fake sorting the stack vars for account_stack_vars (). */
  1408. stack_vars_sorted = XNEWVEC (size_t, stack_vars_num);
  1409. for (i = 0; i < stack_vars_num; ++i)
  1410. stack_vars_sorted[i] = i;
  1411. size += account_stack_vars ();
  1412. }
  1413. fini_vars_expansion ();
  1414. pop_cfun ();
  1415. return size;
  1416. }
  1417. /* Helper routine to check if a record or union contains an array field. */
  1418. static int
  1419. record_or_union_type_has_array_p (const_tree tree_type)
  1420. {
  1421. tree fields = TYPE_FIELDS (tree_type);
  1422. tree f;
  1423. for (f = fields; f; f = DECL_CHAIN (f))
  1424. if (TREE_CODE (f) == FIELD_DECL)
  1425. {
  1426. tree field_type = TREE_TYPE (f);
  1427. if (RECORD_OR_UNION_TYPE_P (field_type)
  1428. && record_or_union_type_has_array_p (field_type))
  1429. return 1;
  1430. if (TREE_CODE (field_type) == ARRAY_TYPE)
  1431. return 1;
  1432. }
  1433. return 0;
  1434. }
  1435. /* Check if the current function has local referenced variables that
  1436. have their addresses taken, contain an array, or are arrays. */
  1437. static bool
  1438. stack_protect_decl_p ()
  1439. {
  1440. unsigned i;
  1441. tree var;
  1442. FOR_EACH_LOCAL_DECL (cfun, i, var)
  1443. if (!is_global_var (var))
  1444. {
  1445. tree var_type = TREE_TYPE (var);
  1446. if (TREE_CODE (var) == VAR_DECL
  1447. && (TREE_CODE (var_type) == ARRAY_TYPE
  1448. || TREE_ADDRESSABLE (var)
  1449. || (RECORD_OR_UNION_TYPE_P (var_type)
  1450. && record_or_union_type_has_array_p (var_type))))
  1451. return true;
  1452. }
  1453. return false;
  1454. }
  1455. /* Check if the current function has calls that use a return slot. */
  1456. static bool
  1457. stack_protect_return_slot_p ()
  1458. {
  1459. basic_block bb;
  1460. FOR_ALL_BB_FN (bb, cfun)
  1461. for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
  1462. !gsi_end_p (gsi); gsi_next (&gsi))
  1463. {
  1464. gimple stmt = gsi_stmt (gsi);
  1465. /* This assumes that calls to internal-only functions never
  1466. use a return slot. */
  1467. if (is_gimple_call (stmt)
  1468. && !gimple_call_internal_p (stmt)
  1469. && aggregate_value_p (TREE_TYPE (gimple_call_fntype (stmt)),
  1470. gimple_call_fndecl (stmt)))
  1471. return true;
  1472. }
  1473. return false;
  1474. }
  1475. /* Expand all variables used in the function. */
  1476. static rtx_insn *
  1477. expand_used_vars (void)
  1478. {
  1479. tree var, outer_block = DECL_INITIAL (current_function_decl);
  1480. vec<tree> maybe_local_decls = vNULL;
  1481. rtx_insn *var_end_seq = NULL;
  1482. unsigned i;
  1483. unsigned len;
  1484. bool gen_stack_protect_signal = false;
  1485. /* Compute the phase of the stack frame for this function. */
  1486. {
  1487. int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
  1488. int off = STARTING_FRAME_OFFSET % align;
  1489. frame_phase = off ? align - off : 0;
  1490. }
  1491. /* Set TREE_USED on all variables in the local_decls. */
  1492. FOR_EACH_LOCAL_DECL (cfun, i, var)
  1493. TREE_USED (var) = 1;
  1494. /* Clear TREE_USED on all variables associated with a block scope. */
  1495. clear_tree_used (DECL_INITIAL (current_function_decl));
  1496. init_vars_expansion ();
  1497. if (targetm.use_pseudo_pic_reg ())
  1498. pic_offset_table_rtx = gen_reg_rtx (Pmode);
  1499. hash_map<tree, tree> ssa_name_decls;
  1500. for (i = 0; i < SA.map->num_partitions; i++)
  1501. {
  1502. tree var = partition_to_var (SA.map, i);
  1503. gcc_assert (!virtual_operand_p (var));
  1504. /* Assign decls to each SSA name partition, share decls for partitions
  1505. we could have coalesced (those with the same type). */
  1506. if (SSA_NAME_VAR (var) == NULL_TREE)
  1507. {
  1508. tree *slot = &ssa_name_decls.get_or_insert (TREE_TYPE (var));
  1509. if (!*slot)
  1510. *slot = create_tmp_reg (TREE_TYPE (var));
  1511. replace_ssa_name_symbol (var, *slot);
  1512. }
  1513. /* Always allocate space for partitions based on VAR_DECLs. But for
  1514. those based on PARM_DECLs or RESULT_DECLs and which matter for the
  1515. debug info, there is no need to do so if optimization is disabled
  1516. because all the SSA_NAMEs based on these DECLs have been coalesced
  1517. into a single partition, which is thus assigned the canonical RTL
  1518. location of the DECLs. If in_lto_p, we can't rely on optimize,
  1519. a function could be compiled with -O1 -flto first and only the
  1520. link performed at -O0. */
  1521. if (TREE_CODE (SSA_NAME_VAR (var)) == VAR_DECL)
  1522. expand_one_var (var, true, true);
  1523. else if (DECL_IGNORED_P (SSA_NAME_VAR (var)) || optimize || in_lto_p)
  1524. {
  1525. /* This is a PARM_DECL or RESULT_DECL. For those partitions that
  1526. contain the default def (representing the parm or result itself)
  1527. we don't do anything here. But those which don't contain the
  1528. default def (representing a temporary based on the parm/result)
  1529. we need to allocate space just like for normal VAR_DECLs. */
  1530. if (!bitmap_bit_p (SA.partition_has_default_def, i))
  1531. {
  1532. expand_one_var (var, true, true);
  1533. gcc_assert (SA.partition_to_pseudo[i]);
  1534. }
  1535. }
  1536. }
  1537. if (flag_stack_protect == SPCT_FLAG_STRONG)
  1538. gen_stack_protect_signal
  1539. = stack_protect_decl_p () || stack_protect_return_slot_p ();
  1540. /* At this point all variables on the local_decls with TREE_USED
  1541. set are not associated with any block scope. Lay them out. */
  1542. len = vec_safe_length (cfun->local_decls);
  1543. FOR_EACH_LOCAL_DECL (cfun, i, var)
  1544. {
  1545. bool expand_now = false;
  1546. /* Expanded above already. */
  1547. if (is_gimple_reg (var))
  1548. {
  1549. TREE_USED (var) = 0;
  1550. goto next;
  1551. }
  1552. /* We didn't set a block for static or extern because it's hard
  1553. to tell the difference between a global variable (re)declared
  1554. in a local scope, and one that's really declared there to
  1555. begin with. And it doesn't really matter much, since we're
  1556. not giving them stack space. Expand them now. */
  1557. else if (TREE_STATIC (var) || DECL_EXTERNAL (var))
  1558. expand_now = true;
  1559. /* Expand variables not associated with any block now. Those created by
  1560. the optimizers could be live anywhere in the function. Those that
  1561. could possibly have been scoped originally and detached from their
  1562. block will have their allocation deferred so we coalesce them with
  1563. others when optimization is enabled. */
  1564. else if (TREE_USED (var))
  1565. expand_now = true;
  1566. /* Finally, mark all variables on the list as used. We'll use
  1567. this in a moment when we expand those associated with scopes. */
  1568. TREE_USED (var) = 1;
  1569. if (expand_now)
  1570. expand_one_var (var, true, true);
  1571. next:
  1572. if (DECL_ARTIFICIAL (var) && !DECL_IGNORED_P (var))
  1573. {
  1574. rtx rtl = DECL_RTL_IF_SET (var);
  1575. /* Keep artificial non-ignored vars in cfun->local_decls
  1576. chain until instantiate_decls. */
  1577. if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
  1578. add_local_decl (cfun, var);
  1579. else if (rtl == NULL_RTX)
  1580. /* If rtl isn't set yet, which can happen e.g. with
  1581. -fstack-protector, retry before returning from this
  1582. function. */
  1583. maybe_local_decls.safe_push (var);
  1584. }
  1585. }
  1586. /* We duplicated some of the decls in CFUN->LOCAL_DECLS.
  1587. +-----------------+-----------------+
  1588. | ...processed... | ...duplicates...|
  1589. +-----------------+-----------------+
  1590. ^
  1591. +-- LEN points here.
  1592. We just want the duplicates, as those are the artificial
  1593. non-ignored vars that we want to keep until instantiate_decls.
  1594. Move them down and truncate the array. */
  1595. if (!vec_safe_is_empty (cfun->local_decls))
  1596. cfun->local_decls->block_remove (0, len);
  1597. /* At this point, all variables within the block tree with TREE_USED
  1598. set are actually used by the optimized function. Lay them out. */
  1599. expand_used_vars_for_block (outer_block, true);
  1600. if (stack_vars_num > 0)
  1601. {
  1602. add_scope_conflicts ();
  1603. /* If stack protection is enabled, we don't share space between
  1604. vulnerable data and non-vulnerable data. */
  1605. if (flag_stack_protect != 0
  1606. && (flag_stack_protect != SPCT_FLAG_EXPLICIT
  1607. || (flag_stack_protect == SPCT_FLAG_EXPLICIT
  1608. && lookup_attribute ("stack_protect",
  1609. DECL_ATTRIBUTES (current_function_decl)))))
  1610. add_stack_protection_conflicts ();
  1611. /* Now that we have collected all stack variables, and have computed a
  1612. minimal interference graph, attempt to save some stack space. */
  1613. partition_stack_vars ();
  1614. if (dump_file)
  1615. dump_stack_var_partition ();
  1616. }
  1617. switch (flag_stack_protect)
  1618. {
  1619. case SPCT_FLAG_ALL:
  1620. create_stack_guard ();
  1621. break;
  1622. case SPCT_FLAG_STRONG:
  1623. if (gen_stack_protect_signal
  1624. || cfun->calls_alloca || has_protected_decls
  1625. || lookup_attribute ("stack_protect",
  1626. DECL_ATTRIBUTES (current_function_decl)))
  1627. create_stack_guard ();
  1628. break;
  1629. case SPCT_FLAG_DEFAULT:
  1630. if (cfun->calls_alloca || has_protected_decls
  1631. || lookup_attribute ("stack_protect",
  1632. DECL_ATTRIBUTES (current_function_decl)))
  1633. create_stack_guard ();
  1634. break;
  1635. case SPCT_FLAG_EXPLICIT:
  1636. if (lookup_attribute ("stack_protect",
  1637. DECL_ATTRIBUTES (current_function_decl)))
  1638. create_stack_guard ();
  1639. break;
  1640. default:
  1641. ;
  1642. }
  1643. /* Assign rtl to each variable based on these partitions. */
  1644. if (stack_vars_num > 0)
  1645. {
  1646. struct stack_vars_data data;
  1647. data.asan_vec = vNULL;
  1648. data.asan_decl_vec = vNULL;
  1649. data.asan_base = NULL_RTX;
  1650. data.asan_alignb = 0;
  1651. /* Reorder decls to be protected by iterating over the variables
  1652. array multiple times, and allocating out of each phase in turn. */
  1653. /* ??? We could probably integrate this into the qsort we did
  1654. earlier, such that we naturally see these variables first,
  1655. and thus naturally allocate things in the right order. */
  1656. if (has_protected_decls)
  1657. {
  1658. /* Phase 1 contains only character arrays. */
  1659. expand_stack_vars (stack_protect_decl_phase_1, &data);
  1660. /* Phase 2 contains other kinds of arrays. */
  1661. if (flag_stack_protect == SPCT_FLAG_ALL
  1662. || flag_stack_protect == SPCT_FLAG_STRONG
  1663. || (flag_stack_protect == SPCT_FLAG_EXPLICIT
  1664. && lookup_attribute ("stack_protect",
  1665. DECL_ATTRIBUTES (current_function_decl))))
  1666. expand_stack_vars (stack_protect_decl_phase_2, &data);
  1667. }
  1668. if ((flag_sanitize & SANITIZE_ADDRESS) && ASAN_STACK)
  1669. /* Phase 3, any partitions that need asan protection
  1670. in addition to phase 1 and 2. */
  1671. expand_stack_vars (asan_decl_phase_3, &data);
  1672. if (!data.asan_vec.is_empty ())
  1673. {
  1674. HOST_WIDE_INT prev_offset = frame_offset;
  1675. HOST_WIDE_INT offset, sz, redzonesz;
  1676. redzonesz = ASAN_RED_ZONE_SIZE;
  1677. sz = data.asan_vec[0] - prev_offset;
  1678. if (data.asan_alignb > ASAN_RED_ZONE_SIZE
  1679. && data.asan_alignb <= 4096
  1680. && sz + ASAN_RED_ZONE_SIZE >= (int) data.asan_alignb)
  1681. redzonesz = ((sz + ASAN_RED_ZONE_SIZE + data.asan_alignb - 1)
  1682. & ~(data.asan_alignb - HOST_WIDE_INT_1)) - sz;
  1683. offset
  1684. = alloc_stack_frame_space (redzonesz, ASAN_RED_ZONE_SIZE);
  1685. data.asan_vec.safe_push (prev_offset);
  1686. data.asan_vec.safe_push (offset);
  1687. /* Leave space for alignment if STRICT_ALIGNMENT. */
  1688. if (STRICT_ALIGNMENT)
  1689. alloc_stack_frame_space ((GET_MODE_ALIGNMENT (SImode)
  1690. << ASAN_SHADOW_SHIFT)
  1691. / BITS_PER_UNIT, 1);
  1692. var_end_seq
  1693. = asan_emit_stack_protection (virtual_stack_vars_rtx,
  1694. data.asan_base,
  1695. data.asan_alignb,
  1696. data.asan_vec.address (),
  1697. data.asan_decl_vec.address (),
  1698. data.asan_vec.length ());
  1699. }
  1700. expand_stack_vars (NULL, &data);
  1701. data.asan_vec.release ();
  1702. data.asan_decl_vec.release ();
  1703. }
  1704. fini_vars_expansion ();
  1705. /* If there were any artificial non-ignored vars without rtl
  1706. found earlier, see if deferred stack allocation hasn't assigned
  1707. rtl to them. */
  1708. FOR_EACH_VEC_ELT_REVERSE (maybe_local_decls, i, var)
  1709. {
  1710. rtx rtl = DECL_RTL_IF_SET (var);
  1711. /* Keep artificial non-ignored vars in cfun->local_decls
  1712. chain until instantiate_decls. */
  1713. if (rtl && (MEM_P (rtl) || GET_CODE (rtl) == CONCAT))
  1714. add_local_decl (cfun, var);
  1715. }
  1716. maybe_local_decls.release ();
  1717. /* If the target requires that FRAME_OFFSET be aligned, do it. */
  1718. if (STACK_ALIGNMENT_NEEDED)
  1719. {
  1720. HOST_WIDE_INT align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
  1721. if (!FRAME_GROWS_DOWNWARD)
  1722. frame_offset += align - 1;
  1723. frame_offset &= -align;
  1724. }
  1725. return var_end_seq;
  1726. }
  1727. /* If we need to produce a detailed dump, print the tree representation
  1728. for STMT to the dump file. SINCE is the last RTX after which the RTL
  1729. generated for STMT should have been appended. */
  1730. static void
  1731. maybe_dump_rtl_for_gimple_stmt (gimple stmt, rtx_insn *since)
  1732. {
  1733. if (dump_file && (dump_flags & TDF_DETAILS))
  1734. {
  1735. fprintf (dump_file, "\n;; ");
  1736. print_gimple_stmt (dump_file, stmt, 0,
  1737. TDF_SLIM | (dump_flags & TDF_LINENO));
  1738. fprintf (dump_file, "\n");
  1739. print_rtl (dump_file, since ? NEXT_INSN (since) : since);
  1740. }
  1741. }
  1742. /* Maps the blocks that do not contain tree labels to rtx labels. */
  1743. static hash_map<basic_block, rtx_code_label *> *lab_rtx_for_bb;
  1744. /* Returns the label_rtx expression for a label starting basic block BB. */
  1745. static rtx
  1746. label_rtx_for_bb (basic_block bb ATTRIBUTE_UNUSED)
  1747. {
  1748. gimple_stmt_iterator gsi;
  1749. tree lab;
  1750. if (bb->flags & BB_RTL)
  1751. return block_label (bb);
  1752. rtx_code_label **elt = lab_rtx_for_bb->get (bb);
  1753. if (elt)
  1754. return *elt;
  1755. /* Find the tree label if it is present. */
  1756. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1757. {
  1758. glabel *lab_stmt;
  1759. lab_stmt = dyn_cast <glabel *> (gsi_stmt (gsi));
  1760. if (!lab_stmt)
  1761. break;
  1762. lab = gimple_label_label (lab_stmt);
  1763. if (DECL_NONLOCAL (lab))
  1764. break;
  1765. return label_rtx (lab);
  1766. }
  1767. rtx_code_label *l = gen_label_rtx ();
  1768. lab_rtx_for_bb->put (bb, l);
  1769. return l;
  1770. }
  1771. /* A subroutine of expand_gimple_cond. Given E, a fallthrough edge
  1772. of a basic block where we just expanded the conditional at the end,
  1773. possibly clean up the CFG and instruction sequence. LAST is the
  1774. last instruction before the just emitted jump sequence. */
  1775. static void
  1776. maybe_cleanup_end_of_block (edge e, rtx_insn *last)
  1777. {
  1778. /* Special case: when jumpif decides that the condition is
  1779. trivial it emits an unconditional jump (and the necessary
  1780. barrier). But we still have two edges, the fallthru one is
  1781. wrong. purge_dead_edges would clean this up later. Unfortunately
  1782. we have to insert insns (and split edges) before
  1783. find_many_sub_basic_blocks and hence before purge_dead_edges.
  1784. But splitting edges might create new blocks which depend on the
  1785. fact that if there are two edges there's no barrier. So the
  1786. barrier would get lost and verify_flow_info would ICE. Instead
  1787. of auditing all edge splitters to care for the barrier (which
  1788. normally isn't there in a cleaned CFG), fix it here. */
  1789. if (BARRIER_P (get_last_insn ()))
  1790. {
  1791. rtx_insn *insn;
  1792. remove_edge (e);
  1793. /* Now, we have a single successor block, if we have insns to
  1794. insert on the remaining edge we potentially will insert
  1795. it at the end of this block (if the dest block isn't feasible)
  1796. in order to avoid splitting the edge. This insertion will take
  1797. place in front of the last jump. But we might have emitted
  1798. multiple jumps (conditional and one unconditional) to the
  1799. same destination. Inserting in front of the last one then
  1800. is a problem. See PR 40021. We fix this by deleting all
  1801. jumps except the last unconditional one. */
  1802. insn = PREV_INSN (get_last_insn ());
  1803. /* Make sure we have an unconditional jump. Otherwise we're
  1804. confused. */
  1805. gcc_assert (JUMP_P (insn) && !any_condjump_p (insn));
  1806. for (insn = PREV_INSN (insn); insn != last;)
  1807. {
  1808. insn = PREV_INSN (insn);
  1809. if (JUMP_P (NEXT_INSN (insn)))
  1810. {
  1811. if (!any_condjump_p (NEXT_INSN (insn)))
  1812. {
  1813. gcc_assert (BARRIER_P (NEXT_INSN (NEXT_INSN (insn))));
  1814. delete_insn (NEXT_INSN (NEXT_INSN (insn)));
  1815. }
  1816. delete_insn (NEXT_INSN (insn));
  1817. }
  1818. }
  1819. }
  1820. }
  1821. /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_COND.
  1822. Returns a new basic block if we've terminated the current basic
  1823. block and created a new one. */
  1824. static basic_block
  1825. expand_gimple_cond (basic_block bb, gcond *stmt)
  1826. {
  1827. basic_block new_bb, dest;
  1828. edge new_edge;
  1829. edge true_edge;
  1830. edge false_edge;
  1831. rtx_insn *last2, *last;
  1832. enum tree_code code;
  1833. tree op0, op1;
  1834. code = gimple_cond_code (stmt);
  1835. op0 = gimple_cond_lhs (stmt);
  1836. op1 = gimple_cond_rhs (stmt);
  1837. /* We're sometimes presented with such code:
  1838. D.123_1 = x < y;
  1839. if (D.123_1 != 0)
  1840. ...
  1841. This would expand to two comparisons which then later might
  1842. be cleaned up by combine. But some pattern matchers like if-conversion
  1843. work better when there's only one compare, so make up for this
  1844. here as special exception if TER would have made the same change. */
  1845. if (SA.values
  1846. && TREE_CODE (op0) == SSA_NAME
  1847. && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
  1848. && TREE_CODE (op1) == INTEGER_CST
  1849. && ((gimple_cond_code (stmt) == NE_EXPR
  1850. && integer_zerop (op1))
  1851. || (gimple_cond_code (stmt) == EQ_EXPR
  1852. && integer_onep (op1)))
  1853. && bitmap_bit_p (SA.values, SSA_NAME_VERSION (op0)))
  1854. {
  1855. gimple second = SSA_NAME_DEF_STMT (op0);
  1856. if (gimple_code (second) == GIMPLE_ASSIGN)
  1857. {
  1858. enum tree_code code2 = gimple_assign_rhs_code (second);
  1859. if (TREE_CODE_CLASS (code2) == tcc_comparison)
  1860. {
  1861. code = code2;
  1862. op0 = gimple_assign_rhs1 (second);
  1863. op1 = gimple_assign_rhs2 (second);
  1864. }
  1865. /* If jumps are cheap and the target does not support conditional
  1866. compare, turn some more codes into jumpy sequences. */
  1867. else if (BRANCH_COST (optimize_insn_for_speed_p (), false) < 4
  1868. && targetm.gen_ccmp_first == NULL)
  1869. {
  1870. if ((code2 == BIT_AND_EXPR
  1871. && TYPE_PRECISION (TREE_TYPE (op0)) == 1
  1872. && TREE_CODE (gimple_assign_rhs2 (second)) != INTEGER_CST)
  1873. || code2 == TRUTH_AND_EXPR)
  1874. {
  1875. code = TRUTH_ANDIF_EXPR;
  1876. op0 = gimple_assign_rhs1 (second);
  1877. op1 = gimple_assign_rhs2 (second);
  1878. }
  1879. else if (code2 == BIT_IOR_EXPR || code2 == TRUTH_OR_EXPR)
  1880. {
  1881. code = TRUTH_ORIF_EXPR;
  1882. op0 = gimple_assign_rhs1 (second);
  1883. op1 = gimple_assign_rhs2 (second);
  1884. }
  1885. }
  1886. }
  1887. }
  1888. last2 = last = get_last_insn ();
  1889. extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
  1890. set_curr_insn_location (gimple_location (stmt));
  1891. /* These flags have no purpose in RTL land. */
  1892. true_edge->flags &= ~EDGE_TRUE_VALUE;
  1893. false_edge->flags &= ~EDGE_FALSE_VALUE;
  1894. /* We can either have a pure conditional jump with one fallthru edge or
  1895. two-way jump that needs to be decomposed into two basic blocks. */
  1896. if (false_edge->dest == bb->next_bb)
  1897. {
  1898. jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
  1899. true_edge->probability);
  1900. maybe_dump_rtl_for_gimple_stmt (stmt, last);
  1901. if (true_edge->goto_locus != UNKNOWN_LOCATION)
  1902. set_curr_insn_location (true_edge->goto_locus);
  1903. false_edge->flags |= EDGE_FALLTHRU;
  1904. maybe_cleanup_end_of_block (false_edge, last);
  1905. return NULL;
  1906. }
  1907. if (true_edge->dest == bb->next_bb)
  1908. {
  1909. jumpifnot_1 (code, op0, op1, label_rtx_for_bb (false_edge->dest),
  1910. false_edge->probability);
  1911. maybe_dump_rtl_for_gimple_stmt (stmt, last);
  1912. if (false_edge->goto_locus != UNKNOWN_LOCATION)
  1913. set_curr_insn_location (false_edge->goto_locus);
  1914. true_edge->flags |= EDGE_FALLTHRU;
  1915. maybe_cleanup_end_of_block (true_edge, last);
  1916. return NULL;
  1917. }
  1918. jumpif_1 (code, op0, op1, label_rtx_for_bb (true_edge->dest),
  1919. true_edge->probability);
  1920. last = get_last_insn ();
  1921. if (false_edge->goto_locus != UNKNOWN_LOCATION)
  1922. set_curr_insn_location (false_edge->goto_locus);
  1923. emit_jump (label_rtx_for_bb (false_edge->dest));
  1924. BB_END (bb) = last;
  1925. if (BARRIER_P (BB_END (bb)))
  1926. BB_END (bb) = PREV_INSN (BB_END (bb));
  1927. update_bb_for_insn (bb);
  1928. new_bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
  1929. dest = false_edge->dest;
  1930. redirect_edge_succ (false_edge, new_bb);
  1931. false_edge->flags |= EDGE_FALLTHRU;
  1932. new_bb->count = false_edge->count;
  1933. new_bb->frequency = EDGE_FREQUENCY (false_edge);
  1934. add_bb_to_loop (new_bb, bb->loop_father);
  1935. new_edge = make_edge (new_bb, dest, 0);
  1936. new_edge->probability = REG_BR_PROB_BASE;
  1937. new_edge->count = new_bb->count;
  1938. if (BARRIER_P (BB_END (new_bb)))
  1939. BB_END (new_bb) = PREV_INSN (BB_END (new_bb));
  1940. update_bb_for_insn (new_bb);
  1941. maybe_dump_rtl_for_gimple_stmt (stmt, last2);
  1942. if (true_edge->goto_locus != UNKNOWN_LOCATION)
  1943. {
  1944. set_curr_insn_location (true_edge->goto_locus);
  1945. true_edge->goto_locus = curr_insn_location ();
  1946. }
  1947. return new_bb;
  1948. }
  1949. /* Mark all calls that can have a transaction restart. */
  1950. static void
  1951. mark_transaction_restart_calls (gimple stmt)
  1952. {
  1953. struct tm_restart_node dummy;
  1954. tm_restart_node **slot;
  1955. if (!cfun->gimple_df->tm_restart)
  1956. return;
  1957. dummy.stmt = stmt;
  1958. slot = cfun->gimple_df->tm_restart->find_slot (&dummy, NO_INSERT);
  1959. if (slot)
  1960. {
  1961. struct tm_restart_node *n = *slot;
  1962. tree list = n->label_or_list;
  1963. rtx_insn *insn;
  1964. for (insn = next_real_insn (get_last_insn ());
  1965. !CALL_P (insn);
  1966. insn = next_real_insn (insn))
  1967. continue;
  1968. if (TREE_CODE (list) == LABEL_DECL)
  1969. add_reg_note (insn, REG_TM, label_rtx (list));
  1970. else
  1971. for (; list ; list = TREE_CHAIN (list))
  1972. add_reg_note (insn, REG_TM, label_rtx (TREE_VALUE (list)));
  1973. }
  1974. }
  1975. /* A subroutine of expand_gimple_stmt_1, expanding one GIMPLE_CALL
  1976. statement STMT. */
  1977. static void
  1978. expand_call_stmt (gcall *stmt)
  1979. {
  1980. tree exp, decl, lhs;
  1981. bool builtin_p;
  1982. size_t i;
  1983. if (gimple_call_internal_p (stmt))
  1984. {
  1985. expand_internal_call (stmt);
  1986. return;
  1987. }
  1988. exp = build_vl_exp (CALL_EXPR, gimple_call_num_args (stmt) + 3);
  1989. CALL_EXPR_FN (exp) = gimple_call_fn (stmt);
  1990. decl = gimple_call_fndecl (stmt);
  1991. builtin_p = decl && DECL_BUILT_IN (decl);
  1992. /* If this is not a builtin function, the function type through which the
  1993. call is made may be different from the type of the function. */
  1994. if (!builtin_p)
  1995. CALL_EXPR_FN (exp)
  1996. = fold_convert (build_pointer_type (gimple_call_fntype (stmt)),
  1997. CALL_EXPR_FN (exp));
  1998. TREE_TYPE (exp) = gimple_call_return_type (stmt);
  1999. CALL_EXPR_STATIC_CHAIN (exp) = gimple_call_chain (stmt);
  2000. for (i = 0; i < gimple_call_num_args (stmt); i++)
  2001. {
  2002. tree arg = gimple_call_arg (stmt, i);
  2003. gimple def;
  2004. /* TER addresses into arguments of builtin functions so we have a
  2005. chance to infer more correct alignment information. See PR39954. */
  2006. if (builtin_p
  2007. && TREE_CODE (arg) == SSA_NAME
  2008. && (def = get_gimple_for_ssa_name (arg))
  2009. && gimple_assign_rhs_code (def) == ADDR_EXPR)
  2010. arg = gimple_assign_rhs1 (def);
  2011. CALL_EXPR_ARG (exp, i) = arg;
  2012. }
  2013. if (gimple_has_side_effects (stmt))
  2014. TREE_SIDE_EFFECTS (exp) = 1;
  2015. if (gimple_call_nothrow_p (stmt))
  2016. TREE_NOTHROW (exp) = 1;
  2017. CALL_EXPR_TAILCALL (exp) = gimple_call_tail_p (stmt);
  2018. CALL_EXPR_RETURN_SLOT_OPT (exp) = gimple_call_return_slot_opt_p (stmt);
  2019. if (decl
  2020. && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
  2021. && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
  2022. || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
  2023. CALL_ALLOCA_FOR_VAR_P (exp) = gimple_call_alloca_for_var_p (stmt);
  2024. else
  2025. CALL_FROM_THUNK_P (exp) = gimple_call_from_thunk_p (stmt);
  2026. CALL_EXPR_VA_ARG_PACK (exp) = gimple_call_va_arg_pack_p (stmt);
  2027. SET_EXPR_LOCATION (exp, gimple_location (stmt));
  2028. CALL_WITH_BOUNDS_P (exp) = gimple_call_with_bounds_p (stmt);
  2029. /* Ensure RTL is created for debug args. */
  2030. if (decl && DECL_HAS_DEBUG_ARGS_P (decl))
  2031. {
  2032. vec<tree, va_gc> **debug_args = decl_debug_args_lookup (decl);
  2033. unsigned int ix;
  2034. tree dtemp;
  2035. if (debug_args)
  2036. for (ix = 1; (*debug_args)->iterate (ix, &dtemp); ix += 2)
  2037. {
  2038. gcc_assert (TREE_CODE (dtemp) == DEBUG_EXPR_DECL);
  2039. expand_debug_expr (dtemp);
  2040. }
  2041. }
  2042. lhs = gimple_call_lhs (stmt);
  2043. if (lhs)
  2044. expand_assignment (lhs, exp, false);
  2045. else
  2046. expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
  2047. mark_transaction_restart_calls (stmt);
  2048. }
  2049. /* Generate RTL for an asm statement (explicit assembler code).
  2050. STRING is a STRING_CST node containing the assembler code text,
  2051. or an ADDR_EXPR containing a STRING_CST. VOL nonzero means the
  2052. insn is volatile; don't optimize it. */
  2053. static void
  2054. expand_asm_loc (tree string, int vol, location_t locus)
  2055. {
  2056. rtx body;
  2057. if (TREE_CODE (string) == ADDR_EXPR)
  2058. string = TREE_OPERAND (string, 0);
  2059. body = gen_rtx_ASM_INPUT_loc (VOIDmode,
  2060. ggc_strdup (TREE_STRING_POINTER (string)),
  2061. locus);
  2062. MEM_VOLATILE_P (body) = vol;
  2063. emit_insn (body);
  2064. }
  2065. /* Return the number of times character C occurs in string S. */
  2066. static int
  2067. n_occurrences (int c, const char *s)
  2068. {
  2069. int n = 0;
  2070. while (*s)
  2071. n += (*s++ == c);
  2072. return n;
  2073. }
  2074. /* A subroutine of expand_asm_operands. Check that all operands have
  2075. the same number of alternatives. Return true if so. */
  2076. static bool
  2077. check_operand_nalternatives (tree outputs, tree inputs)
  2078. {
  2079. if (outputs || inputs)
  2080. {
  2081. tree tmp = TREE_PURPOSE (outputs ? outputs : inputs);
  2082. int nalternatives
  2083. = n_occurrences (',', TREE_STRING_POINTER (TREE_VALUE (tmp)));
  2084. tree next = inputs;
  2085. if (nalternatives + 1 > MAX_RECOG_ALTERNATIVES)
  2086. {
  2087. error ("too many alternatives in %<asm%>");
  2088. return false;
  2089. }
  2090. tmp = outputs;
  2091. while (tmp)
  2092. {
  2093. const char *constraint
  2094. = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tmp)));
  2095. if (n_occurrences (',', constraint) != nalternatives)
  2096. {
  2097. error ("operand constraints for %<asm%> differ "
  2098. "in number of alternatives");
  2099. return false;
  2100. }
  2101. if (TREE_CHAIN (tmp))
  2102. tmp = TREE_CHAIN (tmp);
  2103. else
  2104. tmp = next, next = 0;
  2105. }
  2106. }
  2107. return true;
  2108. }
  2109. /* Check for overlap between registers marked in CLOBBERED_REGS and
  2110. anything inappropriate in T. Emit error and return the register
  2111. variable definition for error, NULL_TREE for ok. */
  2112. static bool
  2113. tree_conflicts_with_clobbers_p (tree t, HARD_REG_SET *clobbered_regs)
  2114. {
  2115. /* Conflicts between asm-declared register variables and the clobber
  2116. list are not allowed. */
  2117. tree overlap = tree_overlaps_hard_reg_set (t, clobbered_regs);
  2118. if (overlap)
  2119. {
  2120. error ("asm-specifier for variable %qE conflicts with asm clobber list",
  2121. DECL_NAME (overlap));
  2122. /* Reset registerness to stop multiple errors emitted for a single
  2123. variable. */
  2124. DECL_REGISTER (overlap) = 0;
  2125. return true;
  2126. }
  2127. return false;
  2128. }
  2129. /* Generate RTL for an asm statement with arguments.
  2130. STRING is the instruction template.
  2131. OUTPUTS is a list of output arguments (lvalues); INPUTS a list of inputs.
  2132. Each output or input has an expression in the TREE_VALUE and
  2133. a tree list in TREE_PURPOSE which in turn contains a constraint
  2134. name in TREE_VALUE (or NULL_TREE) and a constraint string
  2135. in TREE_PURPOSE.
  2136. CLOBBERS is a list of STRING_CST nodes each naming a hard register
  2137. that is clobbered by this insn.
  2138. LABELS is a list of labels, and if LABELS is non-NULL, FALLTHRU_BB
  2139. should be the fallthru basic block of the asm goto.
  2140. Not all kinds of lvalue that may appear in OUTPUTS can be stored directly.
  2141. Some elements of OUTPUTS may be replaced with trees representing temporary
  2142. values. The caller should copy those temporary values to the originally
  2143. specified lvalues.
  2144. VOL nonzero means the insn is volatile; don't optimize it. */
  2145. static void
  2146. expand_asm_operands (tree string, tree outputs, tree inputs,
  2147. tree clobbers, tree labels, basic_block fallthru_bb,
  2148. int vol, location_t locus)
  2149. {
  2150. rtvec argvec, constraintvec, labelvec;
  2151. rtx body;
  2152. int ninputs = list_length (inputs);
  2153. int noutputs = list_length (outputs);
  2154. int nlabels = list_length (labels);
  2155. int ninout;
  2156. int nclobbers;
  2157. HARD_REG_SET clobbered_regs;
  2158. int clobber_conflict_found = 0;
  2159. tree tail;
  2160. tree t;
  2161. int i;
  2162. /* Vector of RTX's of evaluated output operands. */
  2163. rtx *output_rtx = XALLOCAVEC (rtx, noutputs);
  2164. int *inout_opnum = XALLOCAVEC (int, noutputs);
  2165. rtx *real_output_rtx = XALLOCAVEC (rtx, noutputs);
  2166. machine_mode *inout_mode = XALLOCAVEC (machine_mode, noutputs);
  2167. const char **constraints = XALLOCAVEC (const char *, noutputs + ninputs);
  2168. int old_generating_concat_p = generating_concat_p;
  2169. rtx_code_label *fallthru_label = NULL;
  2170. /* An ASM with no outputs needs to be treated as volatile, for now. */
  2171. if (noutputs == 0)
  2172. vol = 1;
  2173. if (! check_operand_nalternatives (outputs, inputs))
  2174. return;
  2175. string = resolve_asm_operand_names (string, outputs, inputs, labels);
  2176. /* Collect constraints. */
  2177. i = 0;
  2178. for (t = outputs; t ; t = TREE_CHAIN (t), i++)
  2179. constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
  2180. for (t = inputs; t ; t = TREE_CHAIN (t), i++)
  2181. constraints[i] = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (t)));
  2182. /* Sometimes we wish to automatically clobber registers across an asm.
  2183. Case in point is when the i386 backend moved from cc0 to a hard reg --
  2184. maintaining source-level compatibility means automatically clobbering
  2185. the flags register. */
  2186. clobbers = targetm.md_asm_clobbers (outputs, inputs, clobbers);
  2187. /* Count the number of meaningful clobbered registers, ignoring what
  2188. we would ignore later. */
  2189. nclobbers = 0;
  2190. CLEAR_HARD_REG_SET (clobbered_regs);
  2191. for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
  2192. {
  2193. const char *regname;
  2194. int nregs;
  2195. if (TREE_VALUE (tail) == error_mark_node)
  2196. return;
  2197. regname = TREE_STRING_POINTER (TREE_VALUE (tail));
  2198. i = decode_reg_name_and_count (regname, &nregs);
  2199. if (i == -4)
  2200. ++nclobbers;
  2201. else if (i == -2)
  2202. error ("unknown register name %qs in %<asm%>", regname);
  2203. /* Mark clobbered registers. */
  2204. if (i >= 0)
  2205. {
  2206. int reg;
  2207. for (reg = i; reg < i + nregs; reg++)
  2208. {
  2209. ++nclobbers;
  2210. /* Clobbering the PIC register is an error. */
  2211. if (reg == (int) PIC_OFFSET_TABLE_REGNUM)
  2212. {
  2213. error ("PIC register clobbered by %qs in %<asm%>", regname);
  2214. return;
  2215. }
  2216. SET_HARD_REG_BIT (clobbered_regs, reg);
  2217. }
  2218. }
  2219. }
  2220. /* First pass over inputs and outputs checks validity and sets
  2221. mark_addressable if needed. */
  2222. ninout = 0;
  2223. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2224. {
  2225. tree val = TREE_VALUE (tail);
  2226. tree type = TREE_TYPE (val);
  2227. const char *constraint;
  2228. bool is_inout;
  2229. bool allows_reg;
  2230. bool allows_mem;
  2231. /* If there's an erroneous arg, emit no insn. */
  2232. if (type == error_mark_node)
  2233. return;
  2234. /* Try to parse the output constraint. If that fails, there's
  2235. no point in going further. */
  2236. constraint = constraints[i];
  2237. if (!parse_output_constraint (&constraint, i, ninputs, noutputs,
  2238. &allows_mem, &allows_reg, &is_inout))
  2239. return;
  2240. if (! allows_reg
  2241. && (allows_mem
  2242. || is_inout
  2243. || (DECL_P (val)
  2244. && REG_P (DECL_RTL (val))
  2245. && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type))))
  2246. mark_addressable (val);
  2247. if (is_inout)
  2248. ninout++;
  2249. }
  2250. ninputs += ninout;
  2251. if (ninputs + noutputs + nlabels > MAX_RECOG_OPERANDS)
  2252. {
  2253. error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
  2254. return;
  2255. }
  2256. for (i = 0, tail = inputs; tail; i++, tail = TREE_CHAIN (tail))
  2257. {
  2258. bool allows_reg, allows_mem;
  2259. const char *constraint;
  2260. /* If there's an erroneous arg, emit no insn, because the ASM_INPUT
  2261. would get VOIDmode and that could cause a crash in reload. */
  2262. if (TREE_TYPE (TREE_VALUE (tail)) == error_mark_node)
  2263. return;
  2264. constraint = constraints[i + noutputs];
  2265. if (! parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
  2266. constraints, &allows_mem, &allows_reg))
  2267. return;
  2268. if (! allows_reg && allows_mem)
  2269. mark_addressable (TREE_VALUE (tail));
  2270. }
  2271. /* Second pass evaluates arguments. */
  2272. /* Make sure stack is consistent for asm goto. */
  2273. if (nlabels > 0)
  2274. do_pending_stack_adjust ();
  2275. ninout = 0;
  2276. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2277. {
  2278. tree val = TREE_VALUE (tail);
  2279. tree type = TREE_TYPE (val);
  2280. bool is_inout;
  2281. bool allows_reg;
  2282. bool allows_mem;
  2283. rtx op;
  2284. bool ok;
  2285. ok = parse_output_constraint (&constraints[i], i, ninputs,
  2286. noutputs, &allows_mem, &allows_reg,
  2287. &is_inout);
  2288. gcc_assert (ok);
  2289. /* If an output operand is not a decl or indirect ref and our constraint
  2290. allows a register, make a temporary to act as an intermediate.
  2291. Make the asm insn write into that, then our caller will copy it to
  2292. the real output operand. Likewise for promoted variables. */
  2293. generating_concat_p = 0;
  2294. real_output_rtx[i] = NULL_RTX;
  2295. if ((TREE_CODE (val) == INDIRECT_REF
  2296. && allows_mem)
  2297. || (DECL_P (val)
  2298. && (allows_mem || REG_P (DECL_RTL (val)))
  2299. && ! (REG_P (DECL_RTL (val))
  2300. && GET_MODE (DECL_RTL (val)) != TYPE_MODE (type)))
  2301. || ! allows_reg
  2302. || is_inout)
  2303. {
  2304. op = expand_expr (val, NULL_RTX, VOIDmode,
  2305. !allows_reg ? EXPAND_MEMORY : EXPAND_WRITE);
  2306. if (MEM_P (op))
  2307. op = validize_mem (op);
  2308. if (! allows_reg && !MEM_P (op))
  2309. error ("output number %d not directly addressable", i);
  2310. if ((! allows_mem && MEM_P (op))
  2311. || GET_CODE (op) == CONCAT)
  2312. {
  2313. real_output_rtx[i] = op;
  2314. op = gen_reg_rtx (GET_MODE (op));
  2315. if (is_inout)
  2316. emit_move_insn (op, real_output_rtx[i]);
  2317. }
  2318. }
  2319. else
  2320. {
  2321. op = assign_temp (type, 0, 1);
  2322. op = validize_mem (op);
  2323. if (!MEM_P (op) && TREE_CODE (TREE_VALUE (tail)) == SSA_NAME)
  2324. set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (TREE_VALUE (tail)), op);
  2325. TREE_VALUE (tail) = make_tree (type, op);
  2326. }
  2327. output_rtx[i] = op;
  2328. generating_concat_p = old_generating_concat_p;
  2329. if (is_inout)
  2330. {
  2331. inout_mode[ninout] = TYPE_MODE (type);
  2332. inout_opnum[ninout++] = i;
  2333. }
  2334. if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
  2335. clobber_conflict_found = 1;
  2336. }
  2337. /* Make vectors for the expression-rtx, constraint strings,
  2338. and named operands. */
  2339. argvec = rtvec_alloc (ninputs);
  2340. constraintvec = rtvec_alloc (ninputs);
  2341. labelvec = rtvec_alloc (nlabels);
  2342. body = gen_rtx_ASM_OPERANDS ((noutputs == 0 ? VOIDmode
  2343. : GET_MODE (output_rtx[0])),
  2344. ggc_strdup (TREE_STRING_POINTER (string)),
  2345. empty_string, 0, argvec, constraintvec,
  2346. labelvec, locus);
  2347. MEM_VOLATILE_P (body) = vol;
  2348. /* Eval the inputs and put them into ARGVEC.
  2349. Put their constraints into ASM_INPUTs and store in CONSTRAINTS. */
  2350. for (i = 0, tail = inputs; tail; tail = TREE_CHAIN (tail), ++i)
  2351. {
  2352. bool allows_reg, allows_mem;
  2353. const char *constraint;
  2354. tree val, type;
  2355. rtx op;
  2356. bool ok;
  2357. constraint = constraints[i + noutputs];
  2358. ok = parse_input_constraint (&constraint, i, ninputs, noutputs, ninout,
  2359. constraints, &allows_mem, &allows_reg);
  2360. gcc_assert (ok);
  2361. generating_concat_p = 0;
  2362. val = TREE_VALUE (tail);
  2363. type = TREE_TYPE (val);
  2364. /* EXPAND_INITIALIZER will not generate code for valid initializer
  2365. constants, but will still generate code for other types of operand.
  2366. This is the behavior we want for constant constraints. */
  2367. op = expand_expr (val, NULL_RTX, VOIDmode,
  2368. allows_reg ? EXPAND_NORMAL
  2369. : allows_mem ? EXPAND_MEMORY
  2370. : EXPAND_INITIALIZER);
  2371. /* Never pass a CONCAT to an ASM. */
  2372. if (GET_CODE (op) == CONCAT)
  2373. op = force_reg (GET_MODE (op), op);
  2374. else if (MEM_P (op))
  2375. op = validize_mem (op);
  2376. if (asm_operand_ok (op, constraint, NULL) <= 0)
  2377. {
  2378. if (allows_reg && TYPE_MODE (type) != BLKmode)
  2379. op = force_reg (TYPE_MODE (type), op);
  2380. else if (!allows_mem)
  2381. warning (0, "asm operand %d probably doesn%'t match constraints",
  2382. i + noutputs);
  2383. else if (MEM_P (op))
  2384. {
  2385. /* We won't recognize either volatile memory or memory
  2386. with a queued address as available a memory_operand
  2387. at this point. Ignore it: clearly this *is* a memory. */
  2388. }
  2389. else
  2390. gcc_unreachable ();
  2391. }
  2392. generating_concat_p = old_generating_concat_p;
  2393. ASM_OPERANDS_INPUT (body, i) = op;
  2394. ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, i)
  2395. = gen_rtx_ASM_INPUT_loc (TYPE_MODE (type),
  2396. ggc_strdup (constraints[i + noutputs]),
  2397. locus);
  2398. if (tree_conflicts_with_clobbers_p (val, &clobbered_regs))
  2399. clobber_conflict_found = 1;
  2400. }
  2401. /* Protect all the operands from the queue now that they have all been
  2402. evaluated. */
  2403. generating_concat_p = 0;
  2404. /* For in-out operands, copy output rtx to input rtx. */
  2405. for (i = 0; i < ninout; i++)
  2406. {
  2407. int j = inout_opnum[i];
  2408. char buffer[16];
  2409. ASM_OPERANDS_INPUT (body, ninputs - ninout + i)
  2410. = output_rtx[j];
  2411. sprintf (buffer, "%d", j);
  2412. ASM_OPERANDS_INPUT_CONSTRAINT_EXP (body, ninputs - ninout + i)
  2413. = gen_rtx_ASM_INPUT_loc (inout_mode[i], ggc_strdup (buffer), locus);
  2414. }
  2415. /* Copy labels to the vector. */
  2416. for (i = 0, tail = labels; i < nlabels; ++i, tail = TREE_CHAIN (tail))
  2417. {
  2418. rtx r;
  2419. /* If asm goto has any labels in the fallthru basic block, use
  2420. a label that we emit immediately after the asm goto. Expansion
  2421. may insert further instructions into the same basic block after
  2422. asm goto and if we don't do this, insertion of instructions on
  2423. the fallthru edge might misbehave. See PR58670. */
  2424. if (fallthru_bb
  2425. && label_to_block_fn (cfun, TREE_VALUE (tail)) == fallthru_bb)
  2426. {
  2427. if (fallthru_label == NULL_RTX)
  2428. fallthru_label = gen_label_rtx ();
  2429. r = fallthru_label;
  2430. }
  2431. else
  2432. r = label_rtx (TREE_VALUE (tail));
  2433. ASM_OPERANDS_LABEL (body, i) = gen_rtx_LABEL_REF (Pmode, r);
  2434. }
  2435. generating_concat_p = old_generating_concat_p;
  2436. /* Now, for each output, construct an rtx
  2437. (set OUTPUT (asm_operands INSN OUTPUTCONSTRAINT OUTPUTNUMBER
  2438. ARGVEC CONSTRAINTS OPNAMES))
  2439. If there is more than one, put them inside a PARALLEL. */
  2440. if (nlabels > 0 && nclobbers == 0)
  2441. {
  2442. gcc_assert (noutputs == 0);
  2443. emit_jump_insn (body);
  2444. }
  2445. else if (noutputs == 0 && nclobbers == 0)
  2446. {
  2447. /* No output operands: put in a raw ASM_OPERANDS rtx. */
  2448. emit_insn (body);
  2449. }
  2450. else if (noutputs == 1 && nclobbers == 0)
  2451. {
  2452. ASM_OPERANDS_OUTPUT_CONSTRAINT (body) = ggc_strdup (constraints[0]);
  2453. emit_insn (gen_rtx_SET (VOIDmode, output_rtx[0], body));
  2454. }
  2455. else
  2456. {
  2457. rtx obody = body;
  2458. int num = noutputs;
  2459. if (num == 0)
  2460. num = 1;
  2461. body = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num + nclobbers));
  2462. /* For each output operand, store a SET. */
  2463. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2464. {
  2465. XVECEXP (body, 0, i)
  2466. = gen_rtx_SET (VOIDmode,
  2467. output_rtx[i],
  2468. gen_rtx_ASM_OPERANDS
  2469. (GET_MODE (output_rtx[i]),
  2470. ggc_strdup (TREE_STRING_POINTER (string)),
  2471. ggc_strdup (constraints[i]),
  2472. i, argvec, constraintvec, labelvec, locus));
  2473. MEM_VOLATILE_P (SET_SRC (XVECEXP (body, 0, i))) = vol;
  2474. }
  2475. /* If there are no outputs (but there are some clobbers)
  2476. store the bare ASM_OPERANDS into the PARALLEL. */
  2477. if (i == 0)
  2478. XVECEXP (body, 0, i++) = obody;
  2479. /* Store (clobber REG) for each clobbered register specified. */
  2480. for (tail = clobbers; tail; tail = TREE_CHAIN (tail))
  2481. {
  2482. const char *regname = TREE_STRING_POINTER (TREE_VALUE (tail));
  2483. int reg, nregs;
  2484. int j = decode_reg_name_and_count (regname, &nregs);
  2485. rtx clobbered_reg;
  2486. if (j < 0)
  2487. {
  2488. if (j == -3) /* `cc', which is not a register */
  2489. continue;
  2490. if (j == -4) /* `memory', don't cache memory across asm */
  2491. {
  2492. XVECEXP (body, 0, i++)
  2493. = gen_rtx_CLOBBER (VOIDmode,
  2494. gen_rtx_MEM
  2495. (BLKmode,
  2496. gen_rtx_SCRATCH (VOIDmode)));
  2497. continue;
  2498. }
  2499. /* Ignore unknown register, error already signaled. */
  2500. continue;
  2501. }
  2502. for (reg = j; reg < j + nregs; reg++)
  2503. {
  2504. /* Use QImode since that's guaranteed to clobber just
  2505. * one reg. */
  2506. clobbered_reg = gen_rtx_REG (QImode, reg);
  2507. /* Do sanity check for overlap between clobbers and
  2508. respectively input and outputs that hasn't been
  2509. handled. Such overlap should have been detected and
  2510. reported above. */
  2511. if (!clobber_conflict_found)
  2512. {
  2513. int opno;
  2514. /* We test the old body (obody) contents to avoid
  2515. tripping over the under-construction body. */
  2516. for (opno = 0; opno < noutputs; opno++)
  2517. if (reg_overlap_mentioned_p (clobbered_reg,
  2518. output_rtx[opno]))
  2519. internal_error
  2520. ("asm clobber conflict with output operand");
  2521. for (opno = 0; opno < ninputs - ninout; opno++)
  2522. if (reg_overlap_mentioned_p (clobbered_reg,
  2523. ASM_OPERANDS_INPUT (obody,
  2524. opno)))
  2525. internal_error
  2526. ("asm clobber conflict with input operand");
  2527. }
  2528. XVECEXP (body, 0, i++)
  2529. = gen_rtx_CLOBBER (VOIDmode, clobbered_reg);
  2530. }
  2531. }
  2532. if (nlabels > 0)
  2533. emit_jump_insn (body);
  2534. else
  2535. emit_insn (body);
  2536. }
  2537. if (fallthru_label)
  2538. emit_label (fallthru_label);
  2539. /* For any outputs that needed reloading into registers, spill them
  2540. back to where they belong. */
  2541. for (i = 0; i < noutputs; ++i)
  2542. if (real_output_rtx[i])
  2543. emit_move_insn (real_output_rtx[i], output_rtx[i]);
  2544. crtl->has_asm_statement = 1;
  2545. free_temp_slots ();
  2546. }
  2547. static void
  2548. expand_asm_stmt (gasm *stmt)
  2549. {
  2550. int noutputs;
  2551. tree outputs, tail, t;
  2552. tree *o;
  2553. size_t i, n;
  2554. const char *s;
  2555. tree str, out, in, cl, labels;
  2556. location_t locus = gimple_location (stmt);
  2557. basic_block fallthru_bb = NULL;
  2558. /* Meh... convert the gimple asm operands into real tree lists.
  2559. Eventually we should make all routines work on the vectors instead
  2560. of relying on TREE_CHAIN. */
  2561. out = NULL_TREE;
  2562. n = gimple_asm_noutputs (stmt);
  2563. if (n > 0)
  2564. {
  2565. t = out = gimple_asm_output_op (stmt, 0);
  2566. for (i = 1; i < n; i++)
  2567. t = TREE_CHAIN (t) = gimple_asm_output_op (stmt, i);
  2568. }
  2569. in = NULL_TREE;
  2570. n = gimple_asm_ninputs (stmt);
  2571. if (n > 0)
  2572. {
  2573. t = in = gimple_asm_input_op (stmt, 0);
  2574. for (i = 1; i < n; i++)
  2575. t = TREE_CHAIN (t) = gimple_asm_input_op (stmt, i);
  2576. }
  2577. cl = NULL_TREE;
  2578. n = gimple_asm_nclobbers (stmt);
  2579. if (n > 0)
  2580. {
  2581. t = cl = gimple_asm_clobber_op (stmt, 0);
  2582. for (i = 1; i < n; i++)
  2583. t = TREE_CHAIN (t) = gimple_asm_clobber_op (stmt, i);
  2584. }
  2585. labels = NULL_TREE;
  2586. n = gimple_asm_nlabels (stmt);
  2587. if (n > 0)
  2588. {
  2589. edge fallthru = find_fallthru_edge (gimple_bb (stmt)->succs);
  2590. if (fallthru)
  2591. fallthru_bb = fallthru->dest;
  2592. t = labels = gimple_asm_label_op (stmt, 0);
  2593. for (i = 1; i < n; i++)
  2594. t = TREE_CHAIN (t) = gimple_asm_label_op (stmt, i);
  2595. }
  2596. s = gimple_asm_string (stmt);
  2597. str = build_string (strlen (s), s);
  2598. if (gimple_asm_input_p (stmt))
  2599. {
  2600. expand_asm_loc (str, gimple_asm_volatile_p (stmt), locus);
  2601. return;
  2602. }
  2603. outputs = out;
  2604. noutputs = gimple_asm_noutputs (stmt);
  2605. /* o[I] is the place that output number I should be written. */
  2606. o = (tree *) alloca (noutputs * sizeof (tree));
  2607. /* Record the contents of OUTPUTS before it is modified. */
  2608. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2609. o[i] = TREE_VALUE (tail);
  2610. /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
  2611. OUTPUTS some trees for where the values were actually stored. */
  2612. expand_asm_operands (str, outputs, in, cl, labels, fallthru_bb,
  2613. gimple_asm_volatile_p (stmt), locus);
  2614. /* Copy all the intermediate outputs into the specified outputs. */
  2615. for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
  2616. {
  2617. if (o[i] != TREE_VALUE (tail))
  2618. {
  2619. expand_assignment (o[i], TREE_VALUE (tail), false);
  2620. free_temp_slots ();
  2621. /* Restore the original value so that it's correct the next
  2622. time we expand this function. */
  2623. TREE_VALUE (tail) = o[i];
  2624. }
  2625. }
  2626. }
  2627. /* Emit code to jump to the address
  2628. specified by the pointer expression EXP. */
  2629. static void
  2630. expand_computed_goto (tree exp)
  2631. {
  2632. rtx x = expand_normal (exp);
  2633. do_pending_stack_adjust ();
  2634. emit_indirect_jump (x);
  2635. }
  2636. /* Generate RTL code for a `goto' statement with target label LABEL.
  2637. LABEL should be a LABEL_DECL tree node that was or will later be
  2638. defined with `expand_label'. */
  2639. static void
  2640. expand_goto (tree label)
  2641. {
  2642. #ifdef ENABLE_CHECKING
  2643. /* Check for a nonlocal goto to a containing function. Should have
  2644. gotten translated to __builtin_nonlocal_goto. */
  2645. tree context = decl_function_context (label);
  2646. gcc_assert (!context || context == current_function_decl);
  2647. #endif
  2648. emit_jump (label_rtx (label));
  2649. }
  2650. /* Output a return with no value. */
  2651. static void
  2652. expand_null_return_1 (void)
  2653. {
  2654. clear_pending_stack_adjust ();
  2655. do_pending_stack_adjust ();
  2656. emit_jump (return_label);
  2657. }
  2658. /* Generate RTL to return from the current function, with no value.
  2659. (That is, we do not do anything about returning any value.) */
  2660. void
  2661. expand_null_return (void)
  2662. {
  2663. /* If this function was declared to return a value, but we
  2664. didn't, clobber the return registers so that they are not
  2665. propagated live to the rest of the function. */
  2666. clobber_return_register ();
  2667. expand_null_return_1 ();
  2668. }
  2669. /* Generate RTL to return from the current function, with value VAL. */
  2670. static void
  2671. expand_value_return (rtx val)
  2672. {
  2673. /* Copy the value to the return location unless it's already there. */
  2674. tree decl = DECL_RESULT (current_function_decl);
  2675. rtx return_reg = DECL_RTL (decl);
  2676. if (return_reg != val)
  2677. {
  2678. tree funtype = TREE_TYPE (current_function_decl);
  2679. tree type = TREE_TYPE (decl);
  2680. int unsignedp = TYPE_UNSIGNED (type);
  2681. machine_mode old_mode = DECL_MODE (decl);
  2682. machine_mode mode;
  2683. if (DECL_BY_REFERENCE (decl))
  2684. mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 2);
  2685. else
  2686. mode = promote_function_mode (type, old_mode, &unsignedp, funtype, 1);
  2687. if (mode != old_mode)
  2688. val = convert_modes (mode, old_mode, val, unsignedp);
  2689. if (GET_CODE (return_reg) == PARALLEL)
  2690. emit_group_load (return_reg, val, type, int_size_in_bytes (type));
  2691. else
  2692. emit_move_insn (return_reg, val);
  2693. }
  2694. expand_null_return_1 ();
  2695. }
  2696. /* Generate RTL to evaluate the expression RETVAL and return it
  2697. from the current function. */
  2698. static void
  2699. expand_return (tree retval, tree bounds)
  2700. {
  2701. rtx result_rtl;
  2702. rtx val = 0;
  2703. tree retval_rhs;
  2704. rtx bounds_rtl;
  2705. /* If function wants no value, give it none. */
  2706. if (TREE_CODE (TREE_TYPE (TREE_TYPE (current_function_decl))) == VOID_TYPE)
  2707. {
  2708. expand_normal (retval);
  2709. expand_null_return ();
  2710. return;
  2711. }
  2712. if (retval == error_mark_node)
  2713. {
  2714. /* Treat this like a return of no value from a function that
  2715. returns a value. */
  2716. expand_null_return ();
  2717. return;
  2718. }
  2719. else if ((TREE_CODE (retval) == MODIFY_EXPR
  2720. || TREE_CODE (retval) == INIT_EXPR)
  2721. && TREE_CODE (TREE_OPERAND (retval, 0)) == RESULT_DECL)
  2722. retval_rhs = TREE_OPERAND (retval, 1);
  2723. else
  2724. retval_rhs = retval;
  2725. result_rtl = DECL_RTL (DECL_RESULT (current_function_decl));
  2726. /* Put returned bounds to the right place. */
  2727. bounds_rtl = DECL_BOUNDS_RTL (DECL_RESULT (current_function_decl));
  2728. if (bounds_rtl)
  2729. {
  2730. rtx addr, bnd;
  2731. if (bounds)
  2732. {
  2733. bnd = expand_normal (bounds);
  2734. targetm.calls.store_returned_bounds (bounds_rtl, bnd);
  2735. }
  2736. else if (REG_P (bounds_rtl))
  2737. {
  2738. addr = expand_normal (build_fold_addr_expr (retval_rhs));
  2739. addr = gen_rtx_MEM (Pmode, addr);
  2740. bnd = targetm.calls.load_bounds_for_arg (addr, NULL, NULL);
  2741. targetm.calls.store_returned_bounds (bounds_rtl, bnd);
  2742. }
  2743. else
  2744. {
  2745. int n;
  2746. gcc_assert (GET_CODE (bounds_rtl) == PARALLEL);
  2747. addr = expand_normal (build_fold_addr_expr (retval_rhs));
  2748. addr = gen_rtx_MEM (Pmode, addr);
  2749. for (n = 0; n < XVECLEN (bounds_rtl, 0); n++)
  2750. {
  2751. rtx offs = XEXP (XVECEXP (bounds_rtl, 0, n), 1);
  2752. rtx slot = XEXP (XVECEXP (bounds_rtl, 0, n), 0);
  2753. rtx from = adjust_address (addr, Pmode, INTVAL (offs));
  2754. rtx bnd = targetm.calls.load_bounds_for_arg (from, NULL, NULL);
  2755. targetm.calls.store_returned_bounds (slot, bnd);
  2756. }
  2757. }
  2758. }
  2759. else if (chkp_function_instrumented_p (current_function_decl)
  2760. && !BOUNDED_P (retval_rhs)
  2761. && chkp_type_has_pointer (TREE_TYPE (retval_rhs))
  2762. && TREE_CODE (retval_rhs) != RESULT_DECL)
  2763. {
  2764. rtx addr = expand_normal (build_fold_addr_expr (retval_rhs));
  2765. addr = gen_rtx_MEM (Pmode, addr);
  2766. gcc_assert (MEM_P (result_rtl));
  2767. chkp_copy_bounds_for_stack_parm (result_rtl, addr, TREE_TYPE (retval_rhs));
  2768. }
  2769. /* If we are returning the RESULT_DECL, then the value has already
  2770. been stored into it, so we don't have to do anything special. */
  2771. if (TREE_CODE (retval_rhs) == RESULT_DECL)
  2772. expand_value_return (result_rtl);
  2773. /* If the result is an aggregate that is being returned in one (or more)
  2774. registers, load the registers here. */
  2775. else if (retval_rhs != 0
  2776. && TYPE_MODE (TREE_TYPE (retval_rhs)) == BLKmode
  2777. && REG_P (result_rtl))
  2778. {
  2779. val = copy_blkmode_to_reg (GET_MODE (result_rtl), retval_rhs);
  2780. if (val)
  2781. {
  2782. /* Use the mode of the result value on the return register. */
  2783. PUT_MODE (result_rtl, GET_MODE (val));
  2784. expand_value_return (val);
  2785. }
  2786. else
  2787. expand_null_return ();
  2788. }
  2789. else if (retval_rhs != 0
  2790. && !VOID_TYPE_P (TREE_TYPE (retval_rhs))
  2791. && (REG_P (result_rtl)
  2792. || (GET_CODE (result_rtl) == PARALLEL)))
  2793. {
  2794. /* Compute the return value into a temporary (usually a pseudo reg). */
  2795. val
  2796. = assign_temp (TREE_TYPE (DECL_RESULT (current_function_decl)), 0, 1);
  2797. val = expand_expr (retval_rhs, val, GET_MODE (val), EXPAND_NORMAL);
  2798. val = force_not_mem (val);
  2799. expand_value_return (val);
  2800. }
  2801. else
  2802. {
  2803. /* No hard reg used; calculate value into hard return reg. */
  2804. expand_expr (retval, const0_rtx, VOIDmode, EXPAND_NORMAL);
  2805. expand_value_return (result_rtl);
  2806. }
  2807. }
  2808. /* A subroutine of expand_gimple_stmt, expanding one gimple statement
  2809. STMT that doesn't require special handling for outgoing edges. That
  2810. is no tailcalls and no GIMPLE_COND. */
  2811. static void
  2812. expand_gimple_stmt_1 (gimple stmt)
  2813. {
  2814. tree op0;
  2815. set_curr_insn_location (gimple_location (stmt));
  2816. switch (gimple_code (stmt))
  2817. {
  2818. case GIMPLE_GOTO:
  2819. op0 = gimple_goto_dest (stmt);
  2820. if (TREE_CODE (op0) == LABEL_DECL)
  2821. expand_goto (op0);
  2822. else
  2823. expand_computed_goto (op0);
  2824. break;
  2825. case GIMPLE_LABEL:
  2826. expand_label (gimple_label_label (as_a <glabel *> (stmt)));
  2827. break;
  2828. case GIMPLE_NOP:
  2829. case GIMPLE_PREDICT:
  2830. break;
  2831. case GIMPLE_SWITCH:
  2832. expand_case (as_a <gswitch *> (stmt));
  2833. break;
  2834. case GIMPLE_ASM:
  2835. expand_asm_stmt (as_a <gasm *> (stmt));
  2836. break;
  2837. case GIMPLE_CALL:
  2838. expand_call_stmt (as_a <gcall *> (stmt));
  2839. break;
  2840. case GIMPLE_RETURN:
  2841. op0 = gimple_return_retval (as_a <greturn *> (stmt));
  2842. if (op0 && op0 != error_mark_node)
  2843. {
  2844. tree result = DECL_RESULT (current_function_decl);
  2845. /* If we are not returning the current function's RESULT_DECL,
  2846. build an assignment to it. */
  2847. if (op0 != result)
  2848. {
  2849. /* I believe that a function's RESULT_DECL is unique. */
  2850. gcc_assert (TREE_CODE (op0) != RESULT_DECL);
  2851. /* ??? We'd like to use simply expand_assignment here,
  2852. but this fails if the value is of BLKmode but the return
  2853. decl is a register. expand_return has special handling
  2854. for this combination, which eventually should move
  2855. to common code. See comments there. Until then, let's
  2856. build a modify expression :-/ */
  2857. op0 = build2 (MODIFY_EXPR, TREE_TYPE (result),
  2858. result, op0);
  2859. }
  2860. }
  2861. if (!op0)
  2862. expand_null_return ();
  2863. else
  2864. expand_return (op0, gimple_return_retbnd (stmt));
  2865. break;
  2866. case GIMPLE_ASSIGN:
  2867. {
  2868. gassign *assign_stmt = as_a <gassign *> (stmt);
  2869. tree lhs = gimple_assign_lhs (assign_stmt);
  2870. /* Tree expand used to fiddle with |= and &= of two bitfield
  2871. COMPONENT_REFs here. This can't happen with gimple, the LHS
  2872. of binary assigns must be a gimple reg. */
  2873. if (TREE_CODE (lhs) != SSA_NAME
  2874. || get_gimple_rhs_class (gimple_expr_code (stmt))
  2875. == GIMPLE_SINGLE_RHS)
  2876. {
  2877. tree rhs = gimple_assign_rhs1 (assign_stmt);
  2878. gcc_assert (get_gimple_rhs_class (gimple_expr_code (stmt))
  2879. == GIMPLE_SINGLE_RHS);
  2880. if (gimple_has_location (stmt) && CAN_HAVE_LOCATION_P (rhs))
  2881. SET_EXPR_LOCATION (rhs, gimple_location (stmt));
  2882. if (TREE_CLOBBER_P (rhs))
  2883. /* This is a clobber to mark the going out of scope for
  2884. this LHS. */
  2885. ;
  2886. else
  2887. expand_assignment (lhs, rhs,
  2888. gimple_assign_nontemporal_move_p (
  2889. assign_stmt));
  2890. }
  2891. else
  2892. {
  2893. rtx target, temp;
  2894. bool nontemporal = gimple_assign_nontemporal_move_p (assign_stmt);
  2895. struct separate_ops ops;
  2896. bool promoted = false;
  2897. target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
  2898. if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
  2899. promoted = true;
  2900. ops.code = gimple_assign_rhs_code (assign_stmt);
  2901. ops.type = TREE_TYPE (lhs);
  2902. switch (get_gimple_rhs_class (gimple_expr_code (stmt)))
  2903. {
  2904. case GIMPLE_TERNARY_RHS:
  2905. ops.op2 = gimple_assign_rhs3 (assign_stmt);
  2906. /* Fallthru */
  2907. case GIMPLE_BINARY_RHS:
  2908. ops.op1 = gimple_assign_rhs2 (assign_stmt);
  2909. /* Fallthru */
  2910. case GIMPLE_UNARY_RHS:
  2911. ops.op0 = gimple_assign_rhs1 (assign_stmt);
  2912. break;
  2913. default:
  2914. gcc_unreachable ();
  2915. }
  2916. ops.location = gimple_location (stmt);
  2917. /* If we want to use a nontemporal store, force the value to
  2918. register first. If we store into a promoted register,
  2919. don't directly expand to target. */
  2920. temp = nontemporal || promoted ? NULL_RTX : target;
  2921. temp = expand_expr_real_2 (&ops, temp, GET_MODE (target),
  2922. EXPAND_NORMAL);
  2923. if (temp == target)
  2924. ;
  2925. else if (promoted)
  2926. {
  2927. int unsignedp = SUBREG_PROMOTED_SIGN (target);
  2928. /* If TEMP is a VOIDmode constant, use convert_modes to make
  2929. sure that we properly convert it. */
  2930. if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
  2931. {
  2932. temp = convert_modes (GET_MODE (target),
  2933. TYPE_MODE (ops.type),
  2934. temp, unsignedp);
  2935. temp = convert_modes (GET_MODE (SUBREG_REG (target)),
  2936. GET_MODE (target), temp, unsignedp);
  2937. }
  2938. convert_move (SUBREG_REG (target), temp, unsignedp);
  2939. }
  2940. else if (nontemporal && emit_storent_insn (target, temp))
  2941. ;
  2942. else
  2943. {
  2944. temp = force_operand (temp, target);
  2945. if (temp != target)
  2946. emit_move_insn (target, temp);
  2947. }
  2948. }
  2949. }
  2950. break;
  2951. default:
  2952. gcc_unreachable ();
  2953. }
  2954. }
  2955. /* Expand one gimple statement STMT and return the last RTL instruction
  2956. before any of the newly generated ones.
  2957. In addition to generating the necessary RTL instructions this also
  2958. sets REG_EH_REGION notes if necessary and sets the current source
  2959. location for diagnostics. */
  2960. static rtx_insn *
  2961. expand_gimple_stmt (gimple stmt)
  2962. {
  2963. location_t saved_location = input_location;
  2964. rtx_insn *last = get_last_insn ();
  2965. int lp_nr;
  2966. gcc_assert (cfun);
  2967. /* We need to save and restore the current source location so that errors
  2968. discovered during expansion are emitted with the right location. But
  2969. it would be better if the diagnostic routines used the source location
  2970. embedded in the tree nodes rather than globals. */
  2971. if (gimple_has_location (stmt))
  2972. input_location = gimple_location (stmt);
  2973. expand_gimple_stmt_1 (stmt);
  2974. /* Free any temporaries used to evaluate this statement. */
  2975. free_temp_slots ();
  2976. input_location = saved_location;
  2977. /* Mark all insns that may trap. */
  2978. lp_nr = lookup_stmt_eh_lp (stmt);
  2979. if (lp_nr)
  2980. {
  2981. rtx_insn *insn;
  2982. for (insn = next_real_insn (last); insn;
  2983. insn = next_real_insn (insn))
  2984. {
  2985. if (! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
  2986. /* If we want exceptions for non-call insns, any
  2987. may_trap_p instruction may throw. */
  2988. && GET_CODE (PATTERN (insn)) != CLOBBER
  2989. && GET_CODE (PATTERN (insn)) != USE
  2990. && insn_could_throw_p (insn))
  2991. make_reg_eh_region_note (insn, 0, lp_nr);
  2992. }
  2993. }
  2994. return last;
  2995. }
  2996. /* A subroutine of expand_gimple_basic_block. Expand one GIMPLE_CALL
  2997. that has CALL_EXPR_TAILCALL set. Returns non-null if we actually
  2998. generated a tail call (something that might be denied by the ABI
  2999. rules governing the call; see calls.c).
  3000. Sets CAN_FALLTHRU if we generated a *conditional* tail call, and
  3001. can still reach the rest of BB. The case here is __builtin_sqrt,
  3002. where the NaN result goes through the external function (with a
  3003. tailcall) and the normal result happens via a sqrt instruction. */
  3004. static basic_block
  3005. expand_gimple_tailcall (basic_block bb, gcall *stmt, bool *can_fallthru)
  3006. {
  3007. rtx_insn *last2, *last;
  3008. edge e;
  3009. edge_iterator ei;
  3010. int probability;
  3011. gcov_type count;
  3012. last2 = last = expand_gimple_stmt (stmt);
  3013. for (last = NEXT_INSN (last); last; last = NEXT_INSN (last))
  3014. if (CALL_P (last) && SIBLING_CALL_P (last))
  3015. goto found;
  3016. maybe_dump_rtl_for_gimple_stmt (stmt, last2);
  3017. *can_fallthru = true;
  3018. return NULL;
  3019. found:
  3020. /* ??? Wouldn't it be better to just reset any pending stack adjust?
  3021. Any instructions emitted here are about to be deleted. */
  3022. do_pending_stack_adjust ();
  3023. /* Remove any non-eh, non-abnormal edges that don't go to exit. */
  3024. /* ??? I.e. the fallthrough edge. HOWEVER! If there were to be
  3025. EH or abnormal edges, we shouldn't have created a tail call in
  3026. the first place. So it seems to me we should just be removing
  3027. all edges here, or redirecting the existing fallthru edge to
  3028. the exit block. */
  3029. probability = 0;
  3030. count = 0;
  3031. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  3032. {
  3033. if (!(e->flags & (EDGE_ABNORMAL | EDGE_EH)))
  3034. {
  3035. if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
  3036. {
  3037. e->dest->count -= e->count;
  3038. e->dest->frequency -= EDGE_FREQUENCY (e);
  3039. if (e->dest->count < 0)
  3040. e->dest->count = 0;
  3041. if (e->dest->frequency < 0)
  3042. e->dest->frequency = 0;
  3043. }
  3044. count += e->count;
  3045. probability += e->probability;
  3046. remove_edge (e);
  3047. }
  3048. else
  3049. ei_next (&ei);
  3050. }
  3051. /* This is somewhat ugly: the call_expr expander often emits instructions
  3052. after the sibcall (to perform the function return). These confuse the
  3053. find_many_sub_basic_blocks code, so we need to get rid of these. */
  3054. last = NEXT_INSN (last);
  3055. gcc_assert (BARRIER_P (last));
  3056. *can_fallthru = false;
  3057. while (NEXT_INSN (last))
  3058. {
  3059. /* For instance an sqrt builtin expander expands if with
  3060. sibcall in the then and label for `else`. */
  3061. if (LABEL_P (NEXT_INSN (last)))
  3062. {
  3063. *can_fallthru = true;
  3064. break;
  3065. }
  3066. delete_insn (NEXT_INSN (last));
  3067. }
  3068. e = make_edge (bb, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_ABNORMAL
  3069. | EDGE_SIBCALL);
  3070. e->probability += probability;
  3071. e->count += count;
  3072. BB_END (bb) = last;
  3073. update_bb_for_insn (bb);
  3074. if (NEXT_INSN (last))
  3075. {
  3076. bb = create_basic_block (NEXT_INSN (last), get_last_insn (), bb);
  3077. last = BB_END (bb);
  3078. if (BARRIER_P (last))
  3079. BB_END (bb) = PREV_INSN (last);
  3080. }
  3081. maybe_dump_rtl_for_gimple_stmt (stmt, last2);
  3082. return bb;
  3083. }
  3084. /* Return the difference between the floor and the truncated result of
  3085. a signed division by OP1 with remainder MOD. */
  3086. static rtx
  3087. floor_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
  3088. {
  3089. /* (mod != 0 ? (op1 / mod < 0 ? -1 : 0) : 0) */
  3090. return gen_rtx_IF_THEN_ELSE
  3091. (mode, gen_rtx_NE (BImode, mod, const0_rtx),
  3092. gen_rtx_IF_THEN_ELSE
  3093. (mode, gen_rtx_LT (BImode,
  3094. gen_rtx_DIV (mode, op1, mod),
  3095. const0_rtx),
  3096. constm1_rtx, const0_rtx),
  3097. const0_rtx);
  3098. }
  3099. /* Return the difference between the ceil and the truncated result of
  3100. a signed division by OP1 with remainder MOD. */
  3101. static rtx
  3102. ceil_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
  3103. {
  3104. /* (mod != 0 ? (op1 / mod > 0 ? 1 : 0) : 0) */
  3105. return gen_rtx_IF_THEN_ELSE
  3106. (mode, gen_rtx_NE (BImode, mod, const0_rtx),
  3107. gen_rtx_IF_THEN_ELSE
  3108. (mode, gen_rtx_GT (BImode,
  3109. gen_rtx_DIV (mode, op1, mod),
  3110. const0_rtx),
  3111. const1_rtx, const0_rtx),
  3112. const0_rtx);
  3113. }
  3114. /* Return the difference between the ceil and the truncated result of
  3115. an unsigned division by OP1 with remainder MOD. */
  3116. static rtx
  3117. ceil_udiv_adjust (machine_mode mode, rtx mod, rtx op1 ATTRIBUTE_UNUSED)
  3118. {
  3119. /* (mod != 0 ? 1 : 0) */
  3120. return gen_rtx_IF_THEN_ELSE
  3121. (mode, gen_rtx_NE (BImode, mod, const0_rtx),
  3122. const1_rtx, const0_rtx);
  3123. }
  3124. /* Return the difference between the rounded and the truncated result
  3125. of a signed division by OP1 with remainder MOD. Halfway cases are
  3126. rounded away from zero, rather than to the nearest even number. */
  3127. static rtx
  3128. round_sdiv_adjust (machine_mode mode, rtx mod, rtx op1)
  3129. {
  3130. /* (abs (mod) >= abs (op1) - abs (mod)
  3131. ? (op1 / mod > 0 ? 1 : -1)
  3132. : 0) */
  3133. return gen_rtx_IF_THEN_ELSE
  3134. (mode, gen_rtx_GE (BImode, gen_rtx_ABS (mode, mod),
  3135. gen_rtx_MINUS (mode,
  3136. gen_rtx_ABS (mode, op1),
  3137. gen_rtx_ABS (mode, mod))),
  3138. gen_rtx_IF_THEN_ELSE
  3139. (mode, gen_rtx_GT (BImode,
  3140. gen_rtx_DIV (mode, op1, mod),
  3141. const0_rtx),
  3142. const1_rtx, constm1_rtx),
  3143. const0_rtx);
  3144. }
  3145. /* Return the difference between the rounded and the truncated result
  3146. of a unsigned division by OP1 with remainder MOD. Halfway cases
  3147. are rounded away from zero, rather than to the nearest even
  3148. number. */
  3149. static rtx
  3150. round_udiv_adjust (machine_mode mode, rtx mod, rtx op1)
  3151. {
  3152. /* (mod >= op1 - mod ? 1 : 0) */
  3153. return gen_rtx_IF_THEN_ELSE
  3154. (mode, gen_rtx_GE (BImode, mod,
  3155. gen_rtx_MINUS (mode, op1, mod)),
  3156. const1_rtx, const0_rtx);
  3157. }
  3158. /* Convert X to MODE, that must be Pmode or ptr_mode, without emitting
  3159. any rtl. */
  3160. static rtx
  3161. convert_debug_memory_address (machine_mode mode, rtx x,
  3162. addr_space_t as)
  3163. {
  3164. machine_mode xmode = GET_MODE (x);
  3165. #ifndef POINTERS_EXTEND_UNSIGNED
  3166. gcc_assert (mode == Pmode
  3167. || mode == targetm.addr_space.address_mode (as));
  3168. gcc_assert (xmode == mode || xmode == VOIDmode);
  3169. #else
  3170. rtx temp;
  3171. gcc_assert (targetm.addr_space.valid_pointer_mode (mode, as));
  3172. if (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode)
  3173. return x;
  3174. if (GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (xmode))
  3175. x = simplify_gen_subreg (mode, x, xmode,
  3176. subreg_lowpart_offset
  3177. (mode, xmode));
  3178. else if (POINTERS_EXTEND_UNSIGNED > 0)
  3179. x = gen_rtx_ZERO_EXTEND (mode, x);
  3180. else if (!POINTERS_EXTEND_UNSIGNED)
  3181. x = gen_rtx_SIGN_EXTEND (mode, x);
  3182. else
  3183. {
  3184. switch (GET_CODE (x))
  3185. {
  3186. case SUBREG:
  3187. if ((SUBREG_PROMOTED_VAR_P (x)
  3188. || (REG_P (SUBREG_REG (x)) && REG_POINTER (SUBREG_REG (x)))
  3189. || (GET_CODE (SUBREG_REG (x)) == PLUS
  3190. && REG_P (XEXP (SUBREG_REG (x), 0))
  3191. && REG_POINTER (XEXP (SUBREG_REG (x), 0))
  3192. && CONST_INT_P (XEXP (SUBREG_REG (x), 1))))
  3193. && GET_MODE (SUBREG_REG (x)) == mode)
  3194. return SUBREG_REG (x);
  3195. break;
  3196. case LABEL_REF:
  3197. temp = gen_rtx_LABEL_REF (mode, LABEL_REF_LABEL (x));
  3198. LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
  3199. return temp;
  3200. case SYMBOL_REF:
  3201. temp = shallow_copy_rtx (x);
  3202. PUT_MODE (temp, mode);
  3203. return temp;
  3204. case CONST:
  3205. temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
  3206. if (temp)
  3207. temp = gen_rtx_CONST (mode, temp);
  3208. return temp;
  3209. case PLUS:
  3210. case MINUS:
  3211. if (CONST_INT_P (XEXP (x, 1)))
  3212. {
  3213. temp = convert_debug_memory_address (mode, XEXP (x, 0), as);
  3214. if (temp)
  3215. return gen_rtx_fmt_ee (GET_CODE (x), mode, temp, XEXP (x, 1));
  3216. }
  3217. break;
  3218. default:
  3219. break;
  3220. }
  3221. /* Don't know how to express ptr_extend as operation in debug info. */
  3222. return NULL;
  3223. }
  3224. #endif /* POINTERS_EXTEND_UNSIGNED */
  3225. return x;
  3226. }
  3227. /* Map from SSA_NAMEs to corresponding DEBUG_EXPR_DECLs created
  3228. by avoid_deep_ter_for_debug. */
  3229. static hash_map<tree, tree> *deep_ter_debug_map;
  3230. /* Split too deep TER chains for debug stmts using debug temporaries. */
  3231. static void
  3232. avoid_deep_ter_for_debug (gimple stmt, int depth)
  3233. {
  3234. use_operand_p use_p;
  3235. ssa_op_iter iter;
  3236. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
  3237. {
  3238. tree use = USE_FROM_PTR (use_p);
  3239. if (TREE_CODE (use) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (use))
  3240. continue;
  3241. gimple g = get_gimple_for_ssa_name (use);
  3242. if (g == NULL)
  3243. continue;
  3244. if (depth > 6 && !stmt_ends_bb_p (g))
  3245. {
  3246. if (deep_ter_debug_map == NULL)
  3247. deep_ter_debug_map = new hash_map<tree, tree>;
  3248. tree &vexpr = deep_ter_debug_map->get_or_insert (use);
  3249. if (vexpr != NULL)
  3250. continue;
  3251. vexpr = make_node (DEBUG_EXPR_DECL);
  3252. gimple def_temp = gimple_build_debug_bind (vexpr, use, g);
  3253. DECL_ARTIFICIAL (vexpr) = 1;
  3254. TREE_TYPE (vexpr) = TREE_TYPE (use);
  3255. DECL_MODE (vexpr) = TYPE_MODE (TREE_TYPE (use));
  3256. gimple_stmt_iterator gsi = gsi_for_stmt (g);
  3257. gsi_insert_after (&gsi, def_temp, GSI_NEW_STMT);
  3258. avoid_deep_ter_for_debug (def_temp, 0);
  3259. }
  3260. else
  3261. avoid_deep_ter_for_debug (g, depth + 1);
  3262. }
  3263. }
  3264. /* Return an RTX equivalent to the value of the parameter DECL. */
  3265. static rtx
  3266. expand_debug_parm_decl (tree decl)
  3267. {
  3268. rtx incoming = DECL_INCOMING_RTL (decl);
  3269. if (incoming
  3270. && GET_MODE (incoming) != BLKmode
  3271. && ((REG_P (incoming) && HARD_REGISTER_P (incoming))
  3272. || (MEM_P (incoming)
  3273. && REG_P (XEXP (incoming, 0))
  3274. && HARD_REGISTER_P (XEXP (incoming, 0)))))
  3275. {
  3276. rtx rtl = gen_rtx_ENTRY_VALUE (GET_MODE (incoming));
  3277. #ifdef HAVE_window_save
  3278. /* DECL_INCOMING_RTL uses the INCOMING_REGNO of parameter registers.
  3279. If the target machine has an explicit window save instruction, the
  3280. actual entry value is the corresponding OUTGOING_REGNO instead. */
  3281. if (REG_P (incoming)
  3282. && OUTGOING_REGNO (REGNO (incoming)) != REGNO (incoming))
  3283. incoming
  3284. = gen_rtx_REG_offset (incoming, GET_MODE (incoming),
  3285. OUTGOING_REGNO (REGNO (incoming)), 0);
  3286. else if (MEM_P (incoming))
  3287. {
  3288. rtx reg = XEXP (incoming, 0);
  3289. if (OUTGOING_REGNO (REGNO (reg)) != REGNO (reg))
  3290. {
  3291. reg = gen_raw_REG (GET_MODE (reg), OUTGOING_REGNO (REGNO (reg)));
  3292. incoming = replace_equiv_address_nv (incoming, reg);
  3293. }
  3294. else
  3295. incoming = copy_rtx (incoming);
  3296. }
  3297. #endif
  3298. ENTRY_VALUE_EXP (rtl) = incoming;
  3299. return rtl;
  3300. }
  3301. if (incoming
  3302. && GET_MODE (incoming) != BLKmode
  3303. && !TREE_ADDRESSABLE (decl)
  3304. && MEM_P (incoming)
  3305. && (XEXP (incoming, 0) == virtual_incoming_args_rtx
  3306. || (GET_CODE (XEXP (incoming, 0)) == PLUS
  3307. && XEXP (XEXP (incoming, 0), 0) == virtual_incoming_args_rtx
  3308. && CONST_INT_P (XEXP (XEXP (incoming, 0), 1)))))
  3309. return copy_rtx (incoming);
  3310. return NULL_RTX;
  3311. }
  3312. /* Return an RTX equivalent to the value of the tree expression EXP. */
  3313. static rtx
  3314. expand_debug_expr (tree exp)
  3315. {
  3316. rtx op0 = NULL_RTX, op1 = NULL_RTX, op2 = NULL_RTX;
  3317. machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
  3318. machine_mode inner_mode = VOIDmode;
  3319. int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
  3320. addr_space_t as;
  3321. switch (TREE_CODE_CLASS (TREE_CODE (exp)))
  3322. {
  3323. case tcc_expression:
  3324. switch (TREE_CODE (exp))
  3325. {
  3326. case COND_EXPR:
  3327. case DOT_PROD_EXPR:
  3328. case SAD_EXPR:
  3329. case WIDEN_MULT_PLUS_EXPR:
  3330. case WIDEN_MULT_MINUS_EXPR:
  3331. case FMA_EXPR:
  3332. goto ternary;
  3333. case TRUTH_ANDIF_EXPR:
  3334. case TRUTH_ORIF_EXPR:
  3335. case TRUTH_AND_EXPR:
  3336. case TRUTH_OR_EXPR:
  3337. case TRUTH_XOR_EXPR:
  3338. goto binary;
  3339. case TRUTH_NOT_EXPR:
  3340. goto unary;
  3341. default:
  3342. break;
  3343. }
  3344. break;
  3345. ternary:
  3346. op2 = expand_debug_expr (TREE_OPERAND (exp, 2));
  3347. if (!op2)
  3348. return NULL_RTX;
  3349. /* Fall through. */
  3350. binary:
  3351. case tcc_binary:
  3352. op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
  3353. if (!op1)
  3354. return NULL_RTX;
  3355. switch (TREE_CODE (exp))
  3356. {
  3357. case LSHIFT_EXPR:
  3358. case RSHIFT_EXPR:
  3359. case LROTATE_EXPR:
  3360. case RROTATE_EXPR:
  3361. case WIDEN_LSHIFT_EXPR:
  3362. /* Ensure second operand isn't wider than the first one. */
  3363. inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 1)));
  3364. if (SCALAR_INT_MODE_P (inner_mode))
  3365. {
  3366. machine_mode opmode = mode;
  3367. if (VECTOR_MODE_P (mode))
  3368. opmode = GET_MODE_INNER (mode);
  3369. if (SCALAR_INT_MODE_P (opmode)
  3370. && (GET_MODE_PRECISION (opmode)
  3371. < GET_MODE_PRECISION (inner_mode)))
  3372. op1 = simplify_gen_subreg (opmode, op1, inner_mode,
  3373. subreg_lowpart_offset (opmode,
  3374. inner_mode));
  3375. }
  3376. break;
  3377. default:
  3378. break;
  3379. }
  3380. /* Fall through. */
  3381. unary:
  3382. case tcc_unary:
  3383. inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
  3384. op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
  3385. if (!op0)
  3386. return NULL_RTX;
  3387. break;
  3388. case tcc_comparison:
  3389. unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)));
  3390. goto binary;
  3391. case tcc_type:
  3392. case tcc_statement:
  3393. gcc_unreachable ();
  3394. case tcc_constant:
  3395. case tcc_exceptional:
  3396. case tcc_declaration:
  3397. case tcc_reference:
  3398. case tcc_vl_exp:
  3399. break;
  3400. }
  3401. switch (TREE_CODE (exp))
  3402. {
  3403. case STRING_CST:
  3404. if (!lookup_constant_def (exp))
  3405. {
  3406. if (strlen (TREE_STRING_POINTER (exp)) + 1
  3407. != (size_t) TREE_STRING_LENGTH (exp))
  3408. return NULL_RTX;
  3409. op0 = gen_rtx_CONST_STRING (Pmode, TREE_STRING_POINTER (exp));
  3410. op0 = gen_rtx_MEM (BLKmode, op0);
  3411. set_mem_attributes (op0, exp, 0);
  3412. return op0;
  3413. }
  3414. /* Fall through... */
  3415. case INTEGER_CST:
  3416. case REAL_CST:
  3417. case FIXED_CST:
  3418. op0 = expand_expr (exp, NULL_RTX, mode, EXPAND_INITIALIZER);
  3419. return op0;
  3420. case COMPLEX_CST:
  3421. gcc_assert (COMPLEX_MODE_P (mode));
  3422. op0 = expand_debug_expr (TREE_REALPART (exp));
  3423. op1 = expand_debug_expr (TREE_IMAGPART (exp));
  3424. return gen_rtx_CONCAT (mode, op0, op1);
  3425. case DEBUG_EXPR_DECL:
  3426. op0 = DECL_RTL_IF_SET (exp);
  3427. if (op0)
  3428. return op0;
  3429. op0 = gen_rtx_DEBUG_EXPR (mode);
  3430. DEBUG_EXPR_TREE_DECL (op0) = exp;
  3431. SET_DECL_RTL (exp, op0);
  3432. return op0;
  3433. case VAR_DECL:
  3434. case PARM_DECL:
  3435. case FUNCTION_DECL:
  3436. case LABEL_DECL:
  3437. case CONST_DECL:
  3438. case RESULT_DECL:
  3439. op0 = DECL_RTL_IF_SET (exp);
  3440. /* This decl was probably optimized away. */
  3441. if (!op0)
  3442. {
  3443. if (TREE_CODE (exp) != VAR_DECL
  3444. || DECL_EXTERNAL (exp)
  3445. || !TREE_STATIC (exp)
  3446. || !DECL_NAME (exp)
  3447. || DECL_HARD_REGISTER (exp)
  3448. || DECL_IN_CONSTANT_POOL (exp)
  3449. || mode == VOIDmode)
  3450. return NULL;
  3451. op0 = make_decl_rtl_for_debug (exp);
  3452. if (!MEM_P (op0)
  3453. || GET_CODE (XEXP (op0, 0)) != SYMBOL_REF
  3454. || SYMBOL_REF_DECL (XEXP (op0, 0)) != exp)
  3455. return NULL;
  3456. }
  3457. else
  3458. op0 = copy_rtx (op0);
  3459. if (GET_MODE (op0) == BLKmode
  3460. /* If op0 is not BLKmode, but mode is, adjust_mode
  3461. below would ICE. While it is likely a FE bug,
  3462. try to be robust here. See PR43166. */
  3463. || mode == BLKmode
  3464. || (mode == VOIDmode && GET_MODE (op0) != VOIDmode))
  3465. {
  3466. gcc_assert (MEM_P (op0));
  3467. op0 = adjust_address_nv (op0, mode, 0);
  3468. return op0;
  3469. }
  3470. /* Fall through. */
  3471. adjust_mode:
  3472. case PAREN_EXPR:
  3473. CASE_CONVERT:
  3474. {
  3475. inner_mode = GET_MODE (op0);
  3476. if (mode == inner_mode)
  3477. return op0;
  3478. if (inner_mode == VOIDmode)
  3479. {
  3480. if (TREE_CODE (exp) == SSA_NAME)
  3481. inner_mode = TYPE_MODE (TREE_TYPE (exp));
  3482. else
  3483. inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
  3484. if (mode == inner_mode)
  3485. return op0;
  3486. }
  3487. if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
  3488. {
  3489. if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
  3490. op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
  3491. else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
  3492. op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
  3493. else
  3494. op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
  3495. }
  3496. else if (FLOAT_MODE_P (mode))
  3497. {
  3498. gcc_assert (TREE_CODE (exp) != SSA_NAME);
  3499. if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
  3500. op0 = simplify_gen_unary (UNSIGNED_FLOAT, mode, op0, inner_mode);
  3501. else
  3502. op0 = simplify_gen_unary (FLOAT, mode, op0, inner_mode);
  3503. }
  3504. else if (FLOAT_MODE_P (inner_mode))
  3505. {
  3506. if (unsignedp)
  3507. op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
  3508. else
  3509. op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
  3510. }
  3511. else if (CONSTANT_P (op0)
  3512. || GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (inner_mode))
  3513. op0 = simplify_gen_subreg (mode, op0, inner_mode,
  3514. subreg_lowpart_offset (mode,
  3515. inner_mode));
  3516. else if (TREE_CODE_CLASS (TREE_CODE (exp)) == tcc_unary
  3517. ? TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0)))
  3518. : unsignedp)
  3519. op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
  3520. else
  3521. op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
  3522. return op0;
  3523. }
  3524. case MEM_REF:
  3525. if (!is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
  3526. {
  3527. tree newexp = fold_binary (MEM_REF, TREE_TYPE (exp),
  3528. TREE_OPERAND (exp, 0),
  3529. TREE_OPERAND (exp, 1));
  3530. if (newexp)
  3531. return expand_debug_expr (newexp);
  3532. }
  3533. /* FALLTHROUGH */
  3534. case INDIRECT_REF:
  3535. inner_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)));
  3536. op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
  3537. if (!op0)
  3538. return NULL;
  3539. if (TREE_CODE (exp) == MEM_REF)
  3540. {
  3541. if (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
  3542. || (GET_CODE (op0) == PLUS
  3543. && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR))
  3544. /* (mem (debug_implicit_ptr)) might confuse aliasing.
  3545. Instead just use get_inner_reference. */
  3546. goto component_ref;
  3547. op1 = expand_debug_expr (TREE_OPERAND (exp, 1));
  3548. if (!op1 || !CONST_INT_P (op1))
  3549. return NULL;
  3550. op0 = plus_constant (inner_mode, op0, INTVAL (op1));
  3551. }
  3552. as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
  3553. op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
  3554. op0, as);
  3555. if (op0 == NULL_RTX)
  3556. return NULL;
  3557. op0 = gen_rtx_MEM (mode, op0);
  3558. set_mem_attributes (op0, exp, 0);
  3559. if (TREE_CODE (exp) == MEM_REF
  3560. && !is_gimple_mem_ref_addr (TREE_OPERAND (exp, 0)))
  3561. set_mem_expr (op0, NULL_TREE);
  3562. set_mem_addr_space (op0, as);
  3563. return op0;
  3564. case TARGET_MEM_REF:
  3565. if (TREE_CODE (TMR_BASE (exp)) == ADDR_EXPR
  3566. && !DECL_RTL_SET_P (TREE_OPERAND (TMR_BASE (exp), 0)))
  3567. return NULL;
  3568. op0 = expand_debug_expr
  3569. (tree_mem_ref_addr (build_pointer_type (TREE_TYPE (exp)), exp));
  3570. if (!op0)
  3571. return NULL;
  3572. as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
  3573. op0 = convert_debug_memory_address (targetm.addr_space.address_mode (as),
  3574. op0, as);
  3575. if (op0 == NULL_RTX)
  3576. return NULL;
  3577. op0 = gen_rtx_MEM (mode, op0);
  3578. set_mem_attributes (op0, exp, 0);
  3579. set_mem_addr_space (op0, as);
  3580. return op0;
  3581. component_ref:
  3582. case ARRAY_REF:
  3583. case ARRAY_RANGE_REF:
  3584. case COMPONENT_REF:
  3585. case BIT_FIELD_REF:
  3586. case REALPART_EXPR:
  3587. case IMAGPART_EXPR:
  3588. case VIEW_CONVERT_EXPR:
  3589. {
  3590. machine_mode mode1;
  3591. HOST_WIDE_INT bitsize, bitpos;
  3592. tree offset;
  3593. int volatilep = 0;
  3594. tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
  3595. &mode1, &unsignedp, &volatilep, false);
  3596. rtx orig_op0;
  3597. if (bitsize == 0)
  3598. return NULL;
  3599. orig_op0 = op0 = expand_debug_expr (tem);
  3600. if (!op0)
  3601. return NULL;
  3602. if (offset)
  3603. {
  3604. machine_mode addrmode, offmode;
  3605. if (!MEM_P (op0))
  3606. return NULL;
  3607. op0 = XEXP (op0, 0);
  3608. addrmode = GET_MODE (op0);
  3609. if (addrmode == VOIDmode)
  3610. addrmode = Pmode;
  3611. op1 = expand_debug_expr (offset);
  3612. if (!op1)
  3613. return NULL;
  3614. offmode = GET_MODE (op1);
  3615. if (offmode == VOIDmode)
  3616. offmode = TYPE_MODE (TREE_TYPE (offset));
  3617. if (addrmode != offmode)
  3618. op1 = simplify_gen_subreg (addrmode, op1, offmode,
  3619. subreg_lowpart_offset (addrmode,
  3620. offmode));
  3621. /* Don't use offset_address here, we don't need a
  3622. recognizable address, and we don't want to generate
  3623. code. */
  3624. op0 = gen_rtx_MEM (mode, simplify_gen_binary (PLUS, addrmode,
  3625. op0, op1));
  3626. }
  3627. if (MEM_P (op0))
  3628. {
  3629. if (mode1 == VOIDmode)
  3630. /* Bitfield. */
  3631. mode1 = smallest_mode_for_size (bitsize, MODE_INT);
  3632. if (bitpos >= BITS_PER_UNIT)
  3633. {
  3634. op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
  3635. bitpos %= BITS_PER_UNIT;
  3636. }
  3637. else if (bitpos < 0)
  3638. {
  3639. HOST_WIDE_INT units
  3640. = (-bitpos + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
  3641. op0 = adjust_address_nv (op0, mode1, units);
  3642. bitpos += units * BITS_PER_UNIT;
  3643. }
  3644. else if (bitpos == 0 && bitsize == GET_MODE_BITSIZE (mode))
  3645. op0 = adjust_address_nv (op0, mode, 0);
  3646. else if (GET_MODE (op0) != mode1)
  3647. op0 = adjust_address_nv (op0, mode1, 0);
  3648. else
  3649. op0 = copy_rtx (op0);
  3650. if (op0 == orig_op0)
  3651. op0 = shallow_copy_rtx (op0);
  3652. set_mem_attributes (op0, exp, 0);
  3653. }
  3654. if (bitpos == 0 && mode == GET_MODE (op0))
  3655. return op0;
  3656. if (bitpos < 0)
  3657. return NULL;
  3658. if (GET_MODE (op0) == BLKmode)
  3659. return NULL;
  3660. if ((bitpos % BITS_PER_UNIT) == 0
  3661. && bitsize == GET_MODE_BITSIZE (mode1))
  3662. {
  3663. machine_mode opmode = GET_MODE (op0);
  3664. if (opmode == VOIDmode)
  3665. opmode = TYPE_MODE (TREE_TYPE (tem));
  3666. /* This condition may hold if we're expanding the address
  3667. right past the end of an array that turned out not to
  3668. be addressable (i.e., the address was only computed in
  3669. debug stmts). The gen_subreg below would rightfully
  3670. crash, and the address doesn't really exist, so just
  3671. drop it. */
  3672. if (bitpos >= GET_MODE_BITSIZE (opmode))
  3673. return NULL;
  3674. if ((bitpos % GET_MODE_BITSIZE (mode)) == 0)
  3675. return simplify_gen_subreg (mode, op0, opmode,
  3676. bitpos / BITS_PER_UNIT);
  3677. }
  3678. return simplify_gen_ternary (SCALAR_INT_MODE_P (GET_MODE (op0))
  3679. && TYPE_UNSIGNED (TREE_TYPE (exp))
  3680. ? SIGN_EXTRACT
  3681. : ZERO_EXTRACT, mode,
  3682. GET_MODE (op0) != VOIDmode
  3683. ? GET_MODE (op0)
  3684. : TYPE_MODE (TREE_TYPE (tem)),
  3685. op0, GEN_INT (bitsize), GEN_INT (bitpos));
  3686. }
  3687. case ABS_EXPR:
  3688. return simplify_gen_unary (ABS, mode, op0, mode);
  3689. case NEGATE_EXPR:
  3690. return simplify_gen_unary (NEG, mode, op0, mode);
  3691. case BIT_NOT_EXPR:
  3692. return simplify_gen_unary (NOT, mode, op0, mode);
  3693. case FLOAT_EXPR:
  3694. return simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
  3695. 0)))
  3696. ? UNSIGNED_FLOAT : FLOAT, mode, op0,
  3697. inner_mode);
  3698. case FIX_TRUNC_EXPR:
  3699. return simplify_gen_unary (unsignedp ? UNSIGNED_FIX : FIX, mode, op0,
  3700. inner_mode);
  3701. case POINTER_PLUS_EXPR:
  3702. /* For the rare target where pointers are not the same size as
  3703. size_t, we need to check for mis-matched modes and correct
  3704. the addend. */
  3705. if (op0 && op1
  3706. && GET_MODE (op0) != VOIDmode && GET_MODE (op1) != VOIDmode
  3707. && GET_MODE (op0) != GET_MODE (op1))
  3708. {
  3709. if (GET_MODE_BITSIZE (GET_MODE (op0)) < GET_MODE_BITSIZE (GET_MODE (op1))
  3710. /* If OP0 is a partial mode, then we must truncate, even if it has
  3711. the same bitsize as OP1 as GCC's representation of partial modes
  3712. is opaque. */
  3713. || (GET_MODE_CLASS (GET_MODE (op0)) == MODE_PARTIAL_INT
  3714. && GET_MODE_BITSIZE (GET_MODE (op0)) == GET_MODE_BITSIZE (GET_MODE (op1))))
  3715. op1 = simplify_gen_unary (TRUNCATE, GET_MODE (op0), op1,
  3716. GET_MODE (op1));
  3717. else
  3718. /* We always sign-extend, regardless of the signedness of
  3719. the operand, because the operand is always unsigned
  3720. here even if the original C expression is signed. */
  3721. op1 = simplify_gen_unary (SIGN_EXTEND, GET_MODE (op0), op1,
  3722. GET_MODE (op1));
  3723. }
  3724. /* Fall through. */
  3725. case PLUS_EXPR:
  3726. return simplify_gen_binary (PLUS, mode, op0, op1);
  3727. case MINUS_EXPR:
  3728. return simplify_gen_binary (MINUS, mode, op0, op1);
  3729. case MULT_EXPR:
  3730. return simplify_gen_binary (MULT, mode, op0, op1);
  3731. case RDIV_EXPR:
  3732. case TRUNC_DIV_EXPR:
  3733. case EXACT_DIV_EXPR:
  3734. if (unsignedp)
  3735. return simplify_gen_binary (UDIV, mode, op0, op1);
  3736. else
  3737. return simplify_gen_binary (DIV, mode, op0, op1);
  3738. case TRUNC_MOD_EXPR:
  3739. return simplify_gen_binary (unsignedp ? UMOD : MOD, mode, op0, op1);
  3740. case FLOOR_DIV_EXPR:
  3741. if (unsignedp)
  3742. return simplify_gen_binary (UDIV, mode, op0, op1);
  3743. else
  3744. {
  3745. rtx div = simplify_gen_binary (DIV, mode, op0, op1);
  3746. rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
  3747. rtx adj = floor_sdiv_adjust (mode, mod, op1);
  3748. return simplify_gen_binary (PLUS, mode, div, adj);
  3749. }
  3750. case FLOOR_MOD_EXPR:
  3751. if (unsignedp)
  3752. return simplify_gen_binary (UMOD, mode, op0, op1);
  3753. else
  3754. {
  3755. rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
  3756. rtx adj = floor_sdiv_adjust (mode, mod, op1);
  3757. adj = simplify_gen_unary (NEG, mode,
  3758. simplify_gen_binary (MULT, mode, adj, op1),
  3759. mode);
  3760. return simplify_gen_binary (PLUS, mode, mod, adj);
  3761. }
  3762. case CEIL_DIV_EXPR:
  3763. if (unsignedp)
  3764. {
  3765. rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
  3766. rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
  3767. rtx adj = ceil_udiv_adjust (mode, mod, op1);
  3768. return simplify_gen_binary (PLUS, mode, div, adj);
  3769. }
  3770. else
  3771. {
  3772. rtx div = simplify_gen_binary (DIV, mode, op0, op1);
  3773. rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
  3774. rtx adj = ceil_sdiv_adjust (mode, mod, op1);
  3775. return simplify_gen_binary (PLUS, mode, div, adj);
  3776. }
  3777. case CEIL_MOD_EXPR:
  3778. if (unsignedp)
  3779. {
  3780. rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
  3781. rtx adj = ceil_udiv_adjust (mode, mod, op1);
  3782. adj = simplify_gen_unary (NEG, mode,
  3783. simplify_gen_binary (MULT, mode, adj, op1),
  3784. mode);
  3785. return simplify_gen_binary (PLUS, mode, mod, adj);
  3786. }
  3787. else
  3788. {
  3789. rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
  3790. rtx adj = ceil_sdiv_adjust (mode, mod, op1);
  3791. adj = simplify_gen_unary (NEG, mode,
  3792. simplify_gen_binary (MULT, mode, adj, op1),
  3793. mode);
  3794. return simplify_gen_binary (PLUS, mode, mod, adj);
  3795. }
  3796. case ROUND_DIV_EXPR:
  3797. if (unsignedp)
  3798. {
  3799. rtx div = simplify_gen_binary (UDIV, mode, op0, op1);
  3800. rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
  3801. rtx adj = round_udiv_adjust (mode, mod, op1);
  3802. return simplify_gen_binary (PLUS, mode, div, adj);
  3803. }
  3804. else
  3805. {
  3806. rtx div = simplify_gen_binary (DIV, mode, op0, op1);
  3807. rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
  3808. rtx adj = round_sdiv_adjust (mode, mod, op1);
  3809. return simplify_gen_binary (PLUS, mode, div, adj);
  3810. }
  3811. case ROUND_MOD_EXPR:
  3812. if (unsignedp)
  3813. {
  3814. rtx mod = simplify_gen_binary (UMOD, mode, op0, op1);
  3815. rtx adj = round_udiv_adjust (mode, mod, op1);
  3816. adj = simplify_gen_unary (NEG, mode,
  3817. simplify_gen_binary (MULT, mode, adj, op1),
  3818. mode);
  3819. return simplify_gen_binary (PLUS, mode, mod, adj);
  3820. }
  3821. else
  3822. {
  3823. rtx mod = simplify_gen_binary (MOD, mode, op0, op1);
  3824. rtx adj = round_sdiv_adjust (mode, mod, op1);
  3825. adj = simplify_gen_unary (NEG, mode,
  3826. simplify_gen_binary (MULT, mode, adj, op1),
  3827. mode);
  3828. return simplify_gen_binary (PLUS, mode, mod, adj);
  3829. }
  3830. case LSHIFT_EXPR:
  3831. return simplify_gen_binary (ASHIFT, mode, op0, op1);
  3832. case RSHIFT_EXPR:
  3833. if (unsignedp)
  3834. return simplify_gen_binary (LSHIFTRT, mode, op0, op1);
  3835. else
  3836. return simplify_gen_binary (ASHIFTRT, mode, op0, op1);
  3837. case LROTATE_EXPR:
  3838. return simplify_gen_binary (ROTATE, mode, op0, op1);
  3839. case RROTATE_EXPR:
  3840. return simplify_gen_binary (ROTATERT, mode, op0, op1);
  3841. case MIN_EXPR:
  3842. return simplify_gen_binary (unsignedp ? UMIN : SMIN, mode, op0, op1);
  3843. case MAX_EXPR:
  3844. return simplify_gen_binary (unsignedp ? UMAX : SMAX, mode, op0, op1);
  3845. case BIT_AND_EXPR:
  3846. case TRUTH_AND_EXPR:
  3847. return simplify_gen_binary (AND, mode, op0, op1);
  3848. case BIT_IOR_EXPR:
  3849. case TRUTH_OR_EXPR:
  3850. return simplify_gen_binary (IOR, mode, op0, op1);
  3851. case BIT_XOR_EXPR:
  3852. case TRUTH_XOR_EXPR:
  3853. return simplify_gen_binary (XOR, mode, op0, op1);
  3854. case TRUTH_ANDIF_EXPR:
  3855. return gen_rtx_IF_THEN_ELSE (mode, op0, op1, const0_rtx);
  3856. case TRUTH_ORIF_EXPR:
  3857. return gen_rtx_IF_THEN_ELSE (mode, op0, const_true_rtx, op1);
  3858. case TRUTH_NOT_EXPR:
  3859. return simplify_gen_relational (EQ, mode, inner_mode, op0, const0_rtx);
  3860. case LT_EXPR:
  3861. return simplify_gen_relational (unsignedp ? LTU : LT, mode, inner_mode,
  3862. op0, op1);
  3863. case LE_EXPR:
  3864. return simplify_gen_relational (unsignedp ? LEU : LE, mode, inner_mode,
  3865. op0, op1);
  3866. case GT_EXPR:
  3867. return simplify_gen_relational (unsignedp ? GTU : GT, mode, inner_mode,
  3868. op0, op1);
  3869. case GE_EXPR:
  3870. return simplify_gen_relational (unsignedp ? GEU : GE, mode, inner_mode,
  3871. op0, op1);
  3872. case EQ_EXPR:
  3873. return simplify_gen_relational (EQ, mode, inner_mode, op0, op1);
  3874. case NE_EXPR:
  3875. return simplify_gen_relational (NE, mode, inner_mode, op0, op1);
  3876. case UNORDERED_EXPR:
  3877. return simplify_gen_relational (UNORDERED, mode, inner_mode, op0, op1);
  3878. case ORDERED_EXPR:
  3879. return simplify_gen_relational (ORDERED, mode, inner_mode, op0, op1);
  3880. case UNLT_EXPR:
  3881. return simplify_gen_relational (UNLT, mode, inner_mode, op0, op1);
  3882. case UNLE_EXPR:
  3883. return simplify_gen_relational (UNLE, mode, inner_mode, op0, op1);
  3884. case UNGT_EXPR:
  3885. return simplify_gen_relational (UNGT, mode, inner_mode, op0, op1);
  3886. case UNGE_EXPR:
  3887. return simplify_gen_relational (UNGE, mode, inner_mode, op0, op1);
  3888. case UNEQ_EXPR:
  3889. return simplify_gen_relational (UNEQ, mode, inner_mode, op0, op1);
  3890. case LTGT_EXPR:
  3891. return simplify_gen_relational (LTGT, mode, inner_mode, op0, op1);
  3892. case COND_EXPR:
  3893. return gen_rtx_IF_THEN_ELSE (mode, op0, op1, op2);
  3894. case COMPLEX_EXPR:
  3895. gcc_assert (COMPLEX_MODE_P (mode));
  3896. if (GET_MODE (op0) == VOIDmode)
  3897. op0 = gen_rtx_CONST (GET_MODE_INNER (mode), op0);
  3898. if (GET_MODE (op1) == VOIDmode)
  3899. op1 = gen_rtx_CONST (GET_MODE_INNER (mode), op1);
  3900. return gen_rtx_CONCAT (mode, op0, op1);
  3901. case CONJ_EXPR:
  3902. if (GET_CODE (op0) == CONCAT)
  3903. return gen_rtx_CONCAT (mode, XEXP (op0, 0),
  3904. simplify_gen_unary (NEG, GET_MODE_INNER (mode),
  3905. XEXP (op0, 1),
  3906. GET_MODE_INNER (mode)));
  3907. else
  3908. {
  3909. machine_mode imode = GET_MODE_INNER (mode);
  3910. rtx re, im;
  3911. if (MEM_P (op0))
  3912. {
  3913. re = adjust_address_nv (op0, imode, 0);
  3914. im = adjust_address_nv (op0, imode, GET_MODE_SIZE (imode));
  3915. }
  3916. else
  3917. {
  3918. machine_mode ifmode = int_mode_for_mode (mode);
  3919. machine_mode ihmode = int_mode_for_mode (imode);
  3920. rtx halfsize;
  3921. if (ifmode == BLKmode || ihmode == BLKmode)
  3922. return NULL;
  3923. halfsize = GEN_INT (GET_MODE_BITSIZE (ihmode));
  3924. re = op0;
  3925. if (mode != ifmode)
  3926. re = gen_rtx_SUBREG (ifmode, re, 0);
  3927. re = gen_rtx_ZERO_EXTRACT (ihmode, re, halfsize, const0_rtx);
  3928. if (imode != ihmode)
  3929. re = gen_rtx_SUBREG (imode, re, 0);
  3930. im = copy_rtx (op0);
  3931. if (mode != ifmode)
  3932. im = gen_rtx_SUBREG (ifmode, im, 0);
  3933. im = gen_rtx_ZERO_EXTRACT (ihmode, im, halfsize, halfsize);
  3934. if (imode != ihmode)
  3935. im = gen_rtx_SUBREG (imode, im, 0);
  3936. }
  3937. im = gen_rtx_NEG (imode, im);
  3938. return gen_rtx_CONCAT (mode, re, im);
  3939. }
  3940. case ADDR_EXPR:
  3941. op0 = expand_debug_expr (TREE_OPERAND (exp, 0));
  3942. if (!op0 || !MEM_P (op0))
  3943. {
  3944. if ((TREE_CODE (TREE_OPERAND (exp, 0)) == VAR_DECL
  3945. || TREE_CODE (TREE_OPERAND (exp, 0)) == PARM_DECL
  3946. || TREE_CODE (TREE_OPERAND (exp, 0)) == RESULT_DECL)
  3947. && (!TREE_ADDRESSABLE (TREE_OPERAND (exp, 0))
  3948. || target_for_debug_bind (TREE_OPERAND (exp, 0))))
  3949. return gen_rtx_DEBUG_IMPLICIT_PTR (mode, TREE_OPERAND (exp, 0));
  3950. if (handled_component_p (TREE_OPERAND (exp, 0)))
  3951. {
  3952. HOST_WIDE_INT bitoffset, bitsize, maxsize;
  3953. tree decl
  3954. = get_ref_base_and_extent (TREE_OPERAND (exp, 0),
  3955. &bitoffset, &bitsize, &maxsize);
  3956. if ((TREE_CODE (decl) == VAR_DECL
  3957. || TREE_CODE (decl) == PARM_DECL
  3958. || TREE_CODE (decl) == RESULT_DECL)
  3959. && (!TREE_ADDRESSABLE (decl)
  3960. || target_for_debug_bind (decl))
  3961. && (bitoffset % BITS_PER_UNIT) == 0
  3962. && bitsize > 0
  3963. && bitsize == maxsize)
  3964. {
  3965. rtx base = gen_rtx_DEBUG_IMPLICIT_PTR (mode, decl);
  3966. return plus_constant (mode, base, bitoffset / BITS_PER_UNIT);
  3967. }
  3968. }
  3969. if (TREE_CODE (TREE_OPERAND (exp, 0)) == MEM_REF
  3970. && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
  3971. == ADDR_EXPR)
  3972. {
  3973. op0 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
  3974. 0));
  3975. if (op0 != NULL
  3976. && (GET_CODE (op0) == DEBUG_IMPLICIT_PTR
  3977. || (GET_CODE (op0) == PLUS
  3978. && GET_CODE (XEXP (op0, 0)) == DEBUG_IMPLICIT_PTR
  3979. && CONST_INT_P (XEXP (op0, 1)))))
  3980. {
  3981. op1 = expand_debug_expr (TREE_OPERAND (TREE_OPERAND (exp, 0),
  3982. 1));
  3983. if (!op1 || !CONST_INT_P (op1))
  3984. return NULL;
  3985. return plus_constant (mode, op0, INTVAL (op1));
  3986. }
  3987. }
  3988. return NULL;
  3989. }
  3990. as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
  3991. op0 = convert_debug_memory_address (mode, XEXP (op0, 0), as);
  3992. return op0;
  3993. case VECTOR_CST:
  3994. {
  3995. unsigned i;
  3996. op0 = gen_rtx_CONCATN
  3997. (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
  3998. for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
  3999. {
  4000. op1 = expand_debug_expr (VECTOR_CST_ELT (exp, i));
  4001. if (!op1)
  4002. return NULL;
  4003. XVECEXP (op0, 0, i) = op1;
  4004. }
  4005. return op0;
  4006. }
  4007. case CONSTRUCTOR:
  4008. if (TREE_CLOBBER_P (exp))
  4009. return NULL;
  4010. else if (TREE_CODE (TREE_TYPE (exp)) == VECTOR_TYPE)
  4011. {
  4012. unsigned i;
  4013. tree val;
  4014. op0 = gen_rtx_CONCATN
  4015. (mode, rtvec_alloc (TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp))));
  4016. FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), i, val)
  4017. {
  4018. op1 = expand_debug_expr (val);
  4019. if (!op1)
  4020. return NULL;
  4021. XVECEXP (op0, 0, i) = op1;
  4022. }
  4023. if (i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)))
  4024. {
  4025. op1 = expand_debug_expr
  4026. (build_zero_cst (TREE_TYPE (TREE_TYPE (exp))));
  4027. if (!op1)
  4028. return NULL;
  4029. for (; i < TYPE_VECTOR_SUBPARTS (TREE_TYPE (exp)); i++)
  4030. XVECEXP (op0, 0, i) = op1;
  4031. }
  4032. return op0;
  4033. }
  4034. else
  4035. goto flag_unsupported;
  4036. case CALL_EXPR:
  4037. /* ??? Maybe handle some builtins? */
  4038. return NULL;
  4039. case SSA_NAME:
  4040. {
  4041. gimple g = get_gimple_for_ssa_name (exp);
  4042. if (g)
  4043. {
  4044. tree t = NULL_TREE;
  4045. if (deep_ter_debug_map)
  4046. {
  4047. tree *slot = deep_ter_debug_map->get (exp);
  4048. if (slot)
  4049. t = *slot;
  4050. }
  4051. if (t == NULL_TREE)
  4052. t = gimple_assign_rhs_to_tree (g);
  4053. op0 = expand_debug_expr (t);
  4054. if (!op0)
  4055. return NULL;
  4056. }
  4057. else
  4058. {
  4059. int part = var_to_partition (SA.map, exp);
  4060. if (part == NO_PARTITION)
  4061. {
  4062. /* If this is a reference to an incoming value of parameter
  4063. that is never used in the code or where the incoming
  4064. value is never used in the code, use PARM_DECL's
  4065. DECL_RTL if set. */
  4066. if (SSA_NAME_IS_DEFAULT_DEF (exp)
  4067. && TREE_CODE (SSA_NAME_VAR (exp)) == PARM_DECL)
  4068. {
  4069. op0 = expand_debug_parm_decl (SSA_NAME_VAR (exp));
  4070. if (op0)
  4071. goto adjust_mode;
  4072. op0 = expand_debug_expr (SSA_NAME_VAR (exp));
  4073. if (op0)
  4074. goto adjust_mode;
  4075. }
  4076. return NULL;
  4077. }
  4078. gcc_assert (part >= 0 && (unsigned)part < SA.map->num_partitions);
  4079. op0 = copy_rtx (SA.partition_to_pseudo[part]);
  4080. }
  4081. goto adjust_mode;
  4082. }
  4083. case ERROR_MARK:
  4084. return NULL;
  4085. /* Vector stuff. For most of the codes we don't have rtl codes. */
  4086. case REALIGN_LOAD_EXPR:
  4087. case REDUC_MAX_EXPR:
  4088. case REDUC_MIN_EXPR:
  4089. case REDUC_PLUS_EXPR:
  4090. case VEC_COND_EXPR:
  4091. case VEC_PACK_FIX_TRUNC_EXPR:
  4092. case VEC_PACK_SAT_EXPR:
  4093. case VEC_PACK_TRUNC_EXPR:
  4094. case VEC_UNPACK_FLOAT_HI_EXPR:
  4095. case VEC_UNPACK_FLOAT_LO_EXPR:
  4096. case VEC_UNPACK_HI_EXPR:
  4097. case VEC_UNPACK_LO_EXPR:
  4098. case VEC_WIDEN_MULT_HI_EXPR:
  4099. case VEC_WIDEN_MULT_LO_EXPR:
  4100. case VEC_WIDEN_MULT_EVEN_EXPR:
  4101. case VEC_WIDEN_MULT_ODD_EXPR:
  4102. case VEC_WIDEN_LSHIFT_HI_EXPR:
  4103. case VEC_WIDEN_LSHIFT_LO_EXPR:
  4104. case VEC_PERM_EXPR:
  4105. return NULL;
  4106. /* Misc codes. */
  4107. case ADDR_SPACE_CONVERT_EXPR:
  4108. case FIXED_CONVERT_EXPR:
  4109. case OBJ_TYPE_REF:
  4110. case WITH_SIZE_EXPR:
  4111. return NULL;
  4112. case DOT_PROD_EXPR:
  4113. if (SCALAR_INT_MODE_P (GET_MODE (op0))
  4114. && SCALAR_INT_MODE_P (mode))
  4115. {
  4116. op0
  4117. = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
  4118. 0)))
  4119. ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
  4120. inner_mode);
  4121. op1
  4122. = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
  4123. 1)))
  4124. ? ZERO_EXTEND : SIGN_EXTEND, mode, op1,
  4125. inner_mode);
  4126. op0 = simplify_gen_binary (MULT, mode, op0, op1);
  4127. return simplify_gen_binary (PLUS, mode, op0, op2);
  4128. }
  4129. return NULL;
  4130. case WIDEN_MULT_EXPR:
  4131. case WIDEN_MULT_PLUS_EXPR:
  4132. case WIDEN_MULT_MINUS_EXPR:
  4133. if (SCALAR_INT_MODE_P (GET_MODE (op0))
  4134. && SCALAR_INT_MODE_P (mode))
  4135. {
  4136. inner_mode = GET_MODE (op0);
  4137. if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 0))))
  4138. op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
  4139. else
  4140. op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
  4141. if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp, 1))))
  4142. op1 = simplify_gen_unary (ZERO_EXTEND, mode, op1, inner_mode);
  4143. else
  4144. op1 = simplify_gen_unary (SIGN_EXTEND, mode, op1, inner_mode);
  4145. op0 = simplify_gen_binary (MULT, mode, op0, op1);
  4146. if (TREE_CODE (exp) == WIDEN_MULT_EXPR)
  4147. return op0;
  4148. else if (TREE_CODE (exp) == WIDEN_MULT_PLUS_EXPR)
  4149. return simplify_gen_binary (PLUS, mode, op0, op2);
  4150. else
  4151. return simplify_gen_binary (MINUS, mode, op2, op0);
  4152. }
  4153. return NULL;
  4154. case MULT_HIGHPART_EXPR:
  4155. /* ??? Similar to the above. */
  4156. return NULL;
  4157. case WIDEN_SUM_EXPR:
  4158. case WIDEN_LSHIFT_EXPR:
  4159. if (SCALAR_INT_MODE_P (GET_MODE (op0))
  4160. && SCALAR_INT_MODE_P (mode))
  4161. {
  4162. op0
  4163. = simplify_gen_unary (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (exp,
  4164. 0)))
  4165. ? ZERO_EXTEND : SIGN_EXTEND, mode, op0,
  4166. inner_mode);
  4167. return simplify_gen_binary (TREE_CODE (exp) == WIDEN_LSHIFT_EXPR
  4168. ? ASHIFT : PLUS, mode, op0, op1);
  4169. }
  4170. return NULL;
  4171. case FMA_EXPR:
  4172. return simplify_gen_ternary (FMA, mode, inner_mode, op0, op1, op2);
  4173. default:
  4174. flag_unsupported:
  4175. #ifdef ENABLE_CHECKING
  4176. debug_tree (exp);
  4177. gcc_unreachable ();
  4178. #else
  4179. return NULL;
  4180. #endif
  4181. }
  4182. }
  4183. /* Return an RTX equivalent to the source bind value of the tree expression
  4184. EXP. */
  4185. static rtx
  4186. expand_debug_source_expr (tree exp)
  4187. {
  4188. rtx op0 = NULL_RTX;
  4189. machine_mode mode = VOIDmode, inner_mode;
  4190. switch (TREE_CODE (exp))
  4191. {
  4192. case PARM_DECL:
  4193. {
  4194. mode = DECL_MODE (exp);
  4195. op0 = expand_debug_parm_decl (exp);
  4196. if (op0)
  4197. break;
  4198. /* See if this isn't an argument that has been completely
  4199. optimized out. */
  4200. if (!DECL_RTL_SET_P (exp)
  4201. && !DECL_INCOMING_RTL (exp)
  4202. && DECL_ABSTRACT_ORIGIN (current_function_decl))
  4203. {
  4204. tree aexp = DECL_ORIGIN (exp);
  4205. if (DECL_CONTEXT (aexp)
  4206. == DECL_ABSTRACT_ORIGIN (current_function_decl))
  4207. {
  4208. vec<tree, va_gc> **debug_args;
  4209. unsigned int ix;
  4210. tree ddecl;
  4211. debug_args = decl_debug_args_lookup (current_function_decl);
  4212. if (debug_args != NULL)
  4213. {
  4214. for (ix = 0; vec_safe_iterate (*debug_args, ix, &ddecl);
  4215. ix += 2)
  4216. if (ddecl == aexp)
  4217. return gen_rtx_DEBUG_PARAMETER_REF (mode, aexp);
  4218. }
  4219. }
  4220. }
  4221. break;
  4222. }
  4223. default:
  4224. break;
  4225. }
  4226. if (op0 == NULL_RTX)
  4227. return NULL_RTX;
  4228. inner_mode = GET_MODE (op0);
  4229. if (mode == inner_mode)
  4230. return op0;
  4231. if (FLOAT_MODE_P (mode) && FLOAT_MODE_P (inner_mode))
  4232. {
  4233. if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (inner_mode))
  4234. op0 = simplify_gen_subreg (mode, op0, inner_mode, 0);
  4235. else if (GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (inner_mode))
  4236. op0 = simplify_gen_unary (FLOAT_TRUNCATE, mode, op0, inner_mode);
  4237. else
  4238. op0 = simplify_gen_unary (FLOAT_EXTEND, mode, op0, inner_mode);
  4239. }
  4240. else if (FLOAT_MODE_P (mode))
  4241. gcc_unreachable ();
  4242. else if (FLOAT_MODE_P (inner_mode))
  4243. {
  4244. if (TYPE_UNSIGNED (TREE_TYPE (exp)))
  4245. op0 = simplify_gen_unary (UNSIGNED_FIX, mode, op0, inner_mode);
  4246. else
  4247. op0 = simplify_gen_unary (FIX, mode, op0, inner_mode);
  4248. }
  4249. else if (CONSTANT_P (op0)
  4250. || GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (inner_mode))
  4251. op0 = simplify_gen_subreg (mode, op0, inner_mode,
  4252. subreg_lowpart_offset (mode, inner_mode));
  4253. else if (TYPE_UNSIGNED (TREE_TYPE (exp)))
  4254. op0 = simplify_gen_unary (ZERO_EXTEND, mode, op0, inner_mode);
  4255. else
  4256. op0 = simplify_gen_unary (SIGN_EXTEND, mode, op0, inner_mode);
  4257. return op0;
  4258. }
  4259. /* Ensure INSN_VAR_LOCATION_LOC (insn) doesn't have unbound complexity.
  4260. Allow 4 levels of rtl nesting for most rtl codes, and if we see anything
  4261. deeper than that, create DEBUG_EXPRs and emit DEBUG_INSNs before INSN. */
  4262. static void
  4263. avoid_complex_debug_insns (rtx_insn *insn, rtx *exp_p, int depth)
  4264. {
  4265. rtx exp = *exp_p;
  4266. if (exp == NULL_RTX)
  4267. return;
  4268. if ((OBJECT_P (exp) && !MEM_P (exp)) || GET_CODE (exp) == CLOBBER)
  4269. return;
  4270. if (depth == 4)
  4271. {
  4272. /* Create DEBUG_EXPR (and DEBUG_EXPR_DECL). */
  4273. rtx dval = make_debug_expr_from_rtl (exp);
  4274. /* Emit a debug bind insn before INSN. */
  4275. rtx bind = gen_rtx_VAR_LOCATION (GET_MODE (exp),
  4276. DEBUG_EXPR_TREE_DECL (dval), exp,
  4277. VAR_INIT_STATUS_INITIALIZED);
  4278. emit_debug_insn_before (bind, insn);
  4279. *exp_p = dval;
  4280. return;
  4281. }
  4282. const char *format_ptr = GET_RTX_FORMAT (GET_CODE (exp));
  4283. int i, j;
  4284. for (i = 0; i < GET_RTX_LENGTH (GET_CODE (exp)); i++)
  4285. switch (*format_ptr++)
  4286. {
  4287. case 'e':
  4288. avoid_complex_debug_insns (insn, &XEXP (exp, i), depth + 1);
  4289. break;
  4290. case 'E':
  4291. case 'V':
  4292. for (j = 0; j < XVECLEN (exp, i); j++)
  4293. avoid_complex_debug_insns (insn, &XVECEXP (exp, i, j), depth + 1);
  4294. break;
  4295. default:
  4296. break;
  4297. }
  4298. }
  4299. /* Expand the _LOCs in debug insns. We run this after expanding all
  4300. regular insns, so that any variables referenced in the function
  4301. will have their DECL_RTLs set. */
  4302. static void
  4303. expand_debug_locations (void)
  4304. {
  4305. rtx_insn *insn;
  4306. rtx_insn *last = get_last_insn ();
  4307. int save_strict_alias = flag_strict_aliasing;
  4308. /* New alias sets while setting up memory attributes cause
  4309. -fcompare-debug failures, even though it doesn't bring about any
  4310. codegen changes. */
  4311. flag_strict_aliasing = 0;
  4312. for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  4313. if (DEBUG_INSN_P (insn))
  4314. {
  4315. tree value = (tree)INSN_VAR_LOCATION_LOC (insn);
  4316. rtx val;
  4317. rtx_insn *prev_insn, *insn2;
  4318. machine_mode mode;
  4319. if (value == NULL_TREE)
  4320. val = NULL_RTX;
  4321. else
  4322. {
  4323. if (INSN_VAR_LOCATION_STATUS (insn)
  4324. == VAR_INIT_STATUS_UNINITIALIZED)
  4325. val = expand_debug_source_expr (value);
  4326. /* The avoid_deep_ter_for_debug function inserts
  4327. debug bind stmts after SSA_NAME definition, with the
  4328. SSA_NAME as the whole bind location. Disable temporarily
  4329. expansion of that SSA_NAME into the DEBUG_EXPR_DECL
  4330. being defined in this DEBUG_INSN. */
  4331. else if (deep_ter_debug_map && TREE_CODE (value) == SSA_NAME)
  4332. {
  4333. tree *slot = deep_ter_debug_map->get (value);
  4334. if (slot)
  4335. {
  4336. if (*slot == INSN_VAR_LOCATION_DECL (insn))
  4337. *slot = NULL_TREE;
  4338. else
  4339. slot = NULL;
  4340. }
  4341. val = expand_debug_expr (value);
  4342. if (slot)
  4343. *slot = INSN_VAR_LOCATION_DECL (insn);
  4344. }
  4345. else
  4346. val = expand_debug_expr (value);
  4347. gcc_assert (last == get_last_insn ());
  4348. }
  4349. if (!val)
  4350. val = gen_rtx_UNKNOWN_VAR_LOC ();
  4351. else
  4352. {
  4353. mode = GET_MODE (INSN_VAR_LOCATION (insn));
  4354. gcc_assert (mode == GET_MODE (val)
  4355. || (GET_MODE (val) == VOIDmode
  4356. && (CONST_SCALAR_INT_P (val)
  4357. || GET_CODE (val) == CONST_FIXED
  4358. || GET_CODE (val) == LABEL_REF)));
  4359. }
  4360. INSN_VAR_LOCATION_LOC (insn) = val;
  4361. prev_insn = PREV_INSN (insn);
  4362. for (insn2 = insn; insn2 != prev_insn; insn2 = PREV_INSN (insn2))
  4363. avoid_complex_debug_insns (insn2, &INSN_VAR_LOCATION_LOC (insn2), 0);
  4364. }
  4365. flag_strict_aliasing = save_strict_alias;
  4366. }
  4367. /* Performs swapping operands of commutative operations to expand
  4368. the expensive one first. */
  4369. static void
  4370. reorder_operands (basic_block bb)
  4371. {
  4372. unsigned int *lattice; /* Hold cost of each statement. */
  4373. unsigned int i = 0, n = 0;
  4374. gimple_stmt_iterator gsi;
  4375. gimple_seq stmts;
  4376. gimple stmt;
  4377. bool swap;
  4378. tree op0, op1;
  4379. ssa_op_iter iter;
  4380. use_operand_p use_p;
  4381. gimple def0, def1;
  4382. /* Compute cost of each statement using estimate_num_insns. */
  4383. stmts = bb_seq (bb);
  4384. for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
  4385. {
  4386. stmt = gsi_stmt (gsi);
  4387. if (!is_gimple_debug (stmt))
  4388. gimple_set_uid (stmt, n++);
  4389. }
  4390. lattice = XNEWVEC (unsigned int, n);
  4391. for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
  4392. {
  4393. unsigned cost;
  4394. stmt = gsi_stmt (gsi);
  4395. if (is_gimple_debug (stmt))
  4396. continue;
  4397. cost = estimate_num_insns (stmt, &eni_size_weights);
  4398. lattice[i] = cost;
  4399. FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
  4400. {
  4401. tree use = USE_FROM_PTR (use_p);
  4402. gimple def_stmt;
  4403. if (TREE_CODE (use) != SSA_NAME)
  4404. continue;
  4405. def_stmt = get_gimple_for_ssa_name (use);
  4406. if (!def_stmt)
  4407. continue;
  4408. lattice[i] += lattice[gimple_uid (def_stmt)];
  4409. }
  4410. i++;
  4411. if (!is_gimple_assign (stmt)
  4412. || !commutative_tree_code (gimple_assign_rhs_code (stmt)))
  4413. continue;
  4414. op0 = gimple_op (stmt, 1);
  4415. op1 = gimple_op (stmt, 2);
  4416. if (TREE_CODE (op0) != SSA_NAME
  4417. || TREE_CODE (op1) != SSA_NAME)
  4418. continue;
  4419. /* Swap operands if the second one is more expensive. */
  4420. def0 = get_gimple_for_ssa_name (op0);
  4421. def1 = get_gimple_for_ssa_name (op1);
  4422. if (!def1)
  4423. continue;
  4424. swap = false;
  4425. if (!def0 || lattice[gimple_uid (def1)] > lattice[gimple_uid (def0)])
  4426. swap = true;
  4427. if (swap)
  4428. {
  4429. if (dump_file && (dump_flags & TDF_DETAILS))
  4430. {
  4431. fprintf (dump_file, "Swap operands in stmt:\n");
  4432. print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
  4433. fprintf (dump_file, "Cost left opnd=%d, right opnd=%d\n",
  4434. def0 ? lattice[gimple_uid (def0)] : 0,
  4435. lattice[gimple_uid (def1)]);
  4436. }
  4437. swap_ssa_operands (stmt, gimple_assign_rhs1_ptr (stmt),
  4438. gimple_assign_rhs2_ptr (stmt));
  4439. }
  4440. }
  4441. XDELETE (lattice);
  4442. }
  4443. /* Expand basic block BB from GIMPLE trees to RTL. */
  4444. static basic_block
  4445. expand_gimple_basic_block (basic_block bb, bool disable_tail_calls)
  4446. {
  4447. gimple_stmt_iterator gsi;
  4448. gimple_seq stmts;
  4449. gimple stmt = NULL;
  4450. rtx_note *note;
  4451. rtx_insn *last;
  4452. edge e;
  4453. edge_iterator ei;
  4454. if (dump_file)
  4455. fprintf (dump_file, "\n;; Generating RTL for gimple basic block %d\n",
  4456. bb->index);
  4457. /* Note that since we are now transitioning from GIMPLE to RTL, we
  4458. cannot use the gsi_*_bb() routines because they expect the basic
  4459. block to be in GIMPLE, instead of RTL. Therefore, we need to
  4460. access the BB sequence directly. */
  4461. if (optimize)
  4462. reorder_operands (bb);
  4463. stmts = bb_seq (bb);
  4464. bb->il.gimple.seq = NULL;
  4465. bb->il.gimple.phi_nodes = NULL;
  4466. rtl_profile_for_bb (bb);
  4467. init_rtl_bb_info (bb);
  4468. bb->flags |= BB_RTL;
  4469. /* Remove the RETURN_EXPR if we may fall though to the exit
  4470. instead. */
  4471. gsi = gsi_last (stmts);
  4472. if (!gsi_end_p (gsi)
  4473. && gimple_code (gsi_stmt (gsi)) == GIMPLE_RETURN)
  4474. {
  4475. greturn *ret_stmt = as_a <greturn *> (gsi_stmt (gsi));
  4476. gcc_assert (single_succ_p (bb));
  4477. gcc_assert (single_succ (bb) == EXIT_BLOCK_PTR_FOR_FN (cfun));
  4478. if (bb->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
  4479. && !gimple_return_retval (ret_stmt))
  4480. {
  4481. gsi_remove (&gsi, false);
  4482. single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
  4483. }
  4484. }
  4485. gsi = gsi_start (stmts);
  4486. if (!gsi_end_p (gsi))
  4487. {
  4488. stmt = gsi_stmt (gsi);
  4489. if (gimple_code (stmt) != GIMPLE_LABEL)
  4490. stmt = NULL;
  4491. }
  4492. rtx_code_label **elt = lab_rtx_for_bb->get (bb);
  4493. if (stmt || elt)
  4494. {
  4495. last = get_last_insn ();
  4496. if (stmt)
  4497. {
  4498. expand_gimple_stmt (stmt);
  4499. gsi_next (&gsi);
  4500. }
  4501. if (elt)
  4502. emit_label (*elt);
  4503. /* Java emits line number notes in the top of labels.
  4504. ??? Make this go away once line number notes are obsoleted. */
  4505. BB_HEAD (bb) = NEXT_INSN (last);
  4506. if (NOTE_P (BB_HEAD (bb)))
  4507. BB_HEAD (bb) = NEXT_INSN (BB_HEAD (bb));
  4508. note = emit_note_after (NOTE_INSN_BASIC_BLOCK, BB_HEAD (bb));
  4509. maybe_dump_rtl_for_gimple_stmt (stmt, last);
  4510. }
  4511. else
  4512. BB_HEAD (bb) = note = emit_note (NOTE_INSN_BASIC_BLOCK);
  4513. NOTE_BASIC_BLOCK (note) = bb;
  4514. for (; !gsi_end_p (gsi); gsi_next (&gsi))
  4515. {
  4516. basic_block new_bb;
  4517. stmt = gsi_stmt (gsi);
  4518. /* If this statement is a non-debug one, and we generate debug
  4519. insns, then this one might be the last real use of a TERed
  4520. SSA_NAME, but where there are still some debug uses further
  4521. down. Expanding the current SSA name in such further debug
  4522. uses by their RHS might lead to wrong debug info, as coalescing
  4523. might make the operands of such RHS be placed into the same
  4524. pseudo as something else. Like so:
  4525. a_1 = a_0 + 1; // Assume a_1 is TERed and a_0 is dead
  4526. use(a_1);
  4527. a_2 = ...
  4528. #DEBUG ... => a_1
  4529. As a_0 and a_2 don't overlap in lifetime, assume they are coalesced.
  4530. If we now would expand a_1 by it's RHS (a_0 + 1) in the debug use,
  4531. the write to a_2 would actually have clobbered the place which
  4532. formerly held a_0.
  4533. So, instead of that, we recognize the situation, and generate
  4534. debug temporaries at the last real use of TERed SSA names:
  4535. a_1 = a_0 + 1;
  4536. #DEBUG #D1 => a_1
  4537. use(a_1);
  4538. a_2 = ...
  4539. #DEBUG ... => #D1
  4540. */
  4541. if (MAY_HAVE_DEBUG_INSNS
  4542. && SA.values
  4543. && !is_gimple_debug (stmt))
  4544. {
  4545. ssa_op_iter iter;
  4546. tree op;
  4547. gimple def;
  4548. location_t sloc = curr_insn_location ();
  4549. /* Look for SSA names that have their last use here (TERed
  4550. names always have only one real use). */
  4551. FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
  4552. if ((def = get_gimple_for_ssa_name (op)))
  4553. {
  4554. imm_use_iterator imm_iter;
  4555. use_operand_p use_p;
  4556. bool have_debug_uses = false;
  4557. FOR_EACH_IMM_USE_FAST (use_p, imm_iter, op)
  4558. {
  4559. if (gimple_debug_bind_p (USE_STMT (use_p)))
  4560. {
  4561. have_debug_uses = true;
  4562. break;
  4563. }
  4564. }
  4565. if (have_debug_uses)
  4566. {
  4567. /* OP is a TERed SSA name, with DEF its defining
  4568. statement, and where OP is used in further debug
  4569. instructions. Generate a debug temporary, and
  4570. replace all uses of OP in debug insns with that
  4571. temporary. */
  4572. gimple debugstmt;
  4573. tree value = gimple_assign_rhs_to_tree (def);
  4574. tree vexpr = make_node (DEBUG_EXPR_DECL);
  4575. rtx val;
  4576. machine_mode mode;
  4577. set_curr_insn_location (gimple_location (def));
  4578. DECL_ARTIFICIAL (vexpr) = 1;
  4579. TREE_TYPE (vexpr) = TREE_TYPE (value);
  4580. if (DECL_P (value))
  4581. mode = DECL_MODE (value);
  4582. else
  4583. mode = TYPE_MODE (TREE_TYPE (value));
  4584. DECL_MODE (vexpr) = mode;
  4585. val = gen_rtx_VAR_LOCATION
  4586. (mode, vexpr, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
  4587. emit_debug_insn (val);
  4588. FOR_EACH_IMM_USE_STMT (debugstmt, imm_iter, op)
  4589. {
  4590. if (!gimple_debug_bind_p (debugstmt))
  4591. continue;
  4592. FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
  4593. SET_USE (use_p, vexpr);
  4594. update_stmt (debugstmt);
  4595. }
  4596. }
  4597. }
  4598. set_curr_insn_location (sloc);
  4599. }
  4600. currently_expanding_gimple_stmt = stmt;
  4601. /* Expand this statement, then evaluate the resulting RTL and
  4602. fixup the CFG accordingly. */
  4603. if (gimple_code (stmt) == GIMPLE_COND)
  4604. {
  4605. new_bb = expand_gimple_cond (bb, as_a <gcond *> (stmt));
  4606. if (new_bb)
  4607. return new_bb;
  4608. }
  4609. else if (gimple_debug_bind_p (stmt))
  4610. {
  4611. location_t sloc = curr_insn_location ();
  4612. gimple_stmt_iterator nsi = gsi;
  4613. for (;;)
  4614. {
  4615. tree var = gimple_debug_bind_get_var (stmt);
  4616. tree value;
  4617. rtx val;
  4618. machine_mode mode;
  4619. if (TREE_CODE (var) != DEBUG_EXPR_DECL
  4620. && TREE_CODE (var) != LABEL_DECL
  4621. && !target_for_debug_bind (var))
  4622. goto delink_debug_stmt;
  4623. if (gimple_debug_bind_has_value_p (stmt))
  4624. value = gimple_debug_bind_get_value (stmt);
  4625. else
  4626. value = NULL_TREE;
  4627. last = get_last_insn ();
  4628. set_curr_insn_location (gimple_location (stmt));
  4629. if (DECL_P (var))
  4630. mode = DECL_MODE (var);
  4631. else
  4632. mode = TYPE_MODE (TREE_TYPE (var));
  4633. val = gen_rtx_VAR_LOCATION
  4634. (mode, var, (rtx)value, VAR_INIT_STATUS_INITIALIZED);
  4635. emit_debug_insn (val);
  4636. if (dump_file && (dump_flags & TDF_DETAILS))
  4637. {
  4638. /* We can't dump the insn with a TREE where an RTX
  4639. is expected. */
  4640. PAT_VAR_LOCATION_LOC (val) = const0_rtx;
  4641. maybe_dump_rtl_for_gimple_stmt (stmt, last);
  4642. PAT_VAR_LOCATION_LOC (val) = (rtx)value;
  4643. }
  4644. delink_debug_stmt:
  4645. /* In order not to generate too many debug temporaries,
  4646. we delink all uses of debug statements we already expanded.
  4647. Therefore debug statements between definition and real
  4648. use of TERed SSA names will continue to use the SSA name,
  4649. and not be replaced with debug temps. */
  4650. delink_stmt_imm_use (stmt);
  4651. gsi = nsi;
  4652. gsi_next (&nsi);
  4653. if (gsi_end_p (nsi))
  4654. break;
  4655. stmt = gsi_stmt (nsi);
  4656. if (!gimple_debug_bind_p (stmt))
  4657. break;
  4658. }
  4659. set_curr_insn_location (sloc);
  4660. }
  4661. else if (gimple_debug_source_bind_p (stmt))
  4662. {
  4663. location_t sloc = curr_insn_location ();
  4664. tree var = gimple_debug_source_bind_get_var (stmt);
  4665. tree value = gimple_debug_source_bind_get_value (stmt);
  4666. rtx val;
  4667. machine_mode mode;
  4668. last = get_last_insn ();
  4669. set_curr_insn_location (gimple_location (stmt));
  4670. mode = DECL_MODE (var);
  4671. val = gen_rtx_VAR_LOCATION (mode, var, (rtx)value,
  4672. VAR_INIT_STATUS_UNINITIALIZED);
  4673. emit_debug_insn (val);
  4674. if (dump_file && (dump_flags & TDF_DETAILS))
  4675. {
  4676. /* We can't dump the insn with a TREE where an RTX
  4677. is expected. */
  4678. PAT_VAR_LOCATION_LOC (val) = const0_rtx;
  4679. maybe_dump_rtl_for_gimple_stmt (stmt, last);
  4680. PAT_VAR_LOCATION_LOC (val) = (rtx)value;
  4681. }
  4682. set_curr_insn_location (sloc);
  4683. }
  4684. else
  4685. {
  4686. gcall *call_stmt = dyn_cast <gcall *> (stmt);
  4687. if (call_stmt
  4688. && gimple_call_tail_p (call_stmt)
  4689. && disable_tail_calls)
  4690. gimple_call_set_tail (call_stmt, false);
  4691. if (call_stmt && gimple_call_tail_p (call_stmt))
  4692. {
  4693. bool can_fallthru;
  4694. new_bb = expand_gimple_tailcall (bb, call_stmt, &can_fallthru);
  4695. if (new_bb)
  4696. {
  4697. if (can_fallthru)
  4698. bb = new_bb;
  4699. else
  4700. return new_bb;
  4701. }
  4702. }
  4703. else
  4704. {
  4705. def_operand_p def_p;
  4706. def_p = SINGLE_SSA_DEF_OPERAND (stmt, SSA_OP_DEF);
  4707. if (def_p != NULL)
  4708. {
  4709. /* Ignore this stmt if it is in the list of
  4710. replaceable expressions. */
  4711. if (SA.values
  4712. && bitmap_bit_p (SA.values,
  4713. SSA_NAME_VERSION (DEF_FROM_PTR (def_p))))
  4714. continue;
  4715. }
  4716. last = expand_gimple_stmt (stmt);
  4717. maybe_dump_rtl_for_gimple_stmt (stmt, last);
  4718. }
  4719. }
  4720. }
  4721. currently_expanding_gimple_stmt = NULL;
  4722. /* Expand implicit goto and convert goto_locus. */
  4723. FOR_EACH_EDGE (e, ei, bb->succs)
  4724. {
  4725. if (e->goto_locus != UNKNOWN_LOCATION)
  4726. set_curr_insn_location (e->goto_locus);
  4727. if ((e->flags & EDGE_FALLTHRU) && e->dest != bb->next_bb)
  4728. {
  4729. emit_jump (label_rtx_for_bb (e->dest));
  4730. e->flags &= ~EDGE_FALLTHRU;
  4731. }
  4732. }
  4733. /* Expanded RTL can create a jump in the last instruction of block.
  4734. This later might be assumed to be a jump to successor and break edge insertion.
  4735. We need to insert dummy move to prevent this. PR41440. */
  4736. if (single_succ_p (bb)
  4737. && (single_succ_edge (bb)->flags & EDGE_FALLTHRU)
  4738. && (last = get_last_insn ())
  4739. && JUMP_P (last))
  4740. {
  4741. rtx dummy = gen_reg_rtx (SImode);
  4742. emit_insn_after_noloc (gen_move_insn (dummy, dummy), last, NULL);
  4743. }
  4744. do_pending_stack_adjust ();
  4745. /* Find the block tail. The last insn in the block is the insn
  4746. before a barrier and/or table jump insn. */
  4747. last = get_last_insn ();
  4748. if (BARRIER_P (last))
  4749. last = PREV_INSN (last);
  4750. if (JUMP_TABLE_DATA_P (last))
  4751. last = PREV_INSN (PREV_INSN (last));
  4752. BB_END (bb) = last;
  4753. update_bb_for_insn (bb);
  4754. return bb;
  4755. }
  4756. /* Create a basic block for initialization code. */
  4757. static basic_block
  4758. construct_init_block (void)
  4759. {
  4760. basic_block init_block, first_block;
  4761. edge e = NULL;
  4762. int flags;
  4763. /* Multiple entry points not supported yet. */
  4764. gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR_FOR_FN (cfun)->succs) == 1);
  4765. init_rtl_bb_info (ENTRY_BLOCK_PTR_FOR_FN (cfun));
  4766. init_rtl_bb_info (EXIT_BLOCK_PTR_FOR_FN (cfun));
  4767. ENTRY_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
  4768. EXIT_BLOCK_PTR_FOR_FN (cfun)->flags |= BB_RTL;
  4769. e = EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun), 0);
  4770. /* When entry edge points to first basic block, we don't need jump,
  4771. otherwise we have to jump into proper target. */
  4772. if (e && e->dest != ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb)
  4773. {
  4774. tree label = gimple_block_label (e->dest);
  4775. emit_jump (label_rtx (label));
  4776. flags = 0;
  4777. }
  4778. else
  4779. flags = EDGE_FALLTHRU;
  4780. init_block = create_basic_block (NEXT_INSN (get_insns ()),
  4781. get_last_insn (),
  4782. ENTRY_BLOCK_PTR_FOR_FN (cfun));
  4783. init_block->frequency = ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency;
  4784. init_block->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
  4785. add_bb_to_loop (init_block, ENTRY_BLOCK_PTR_FOR_FN (cfun)->loop_father);
  4786. if (e)
  4787. {
  4788. first_block = e->dest;
  4789. redirect_edge_succ (e, init_block);
  4790. e = make_edge (init_block, first_block, flags);
  4791. }
  4792. else
  4793. e = make_edge (init_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
  4794. e->probability = REG_BR_PROB_BASE;
  4795. e->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
  4796. update_bb_for_insn (init_block);
  4797. return init_block;
  4798. }
  4799. /* For each lexical block, set BLOCK_NUMBER to the depth at which it is
  4800. found in the block tree. */
  4801. static void
  4802. set_block_levels (tree block, int level)
  4803. {
  4804. while (block)
  4805. {
  4806. BLOCK_NUMBER (block) = level;
  4807. set_block_levels (BLOCK_SUBBLOCKS (block), level + 1);
  4808. block = BLOCK_CHAIN (block);
  4809. }
  4810. }
  4811. /* Create a block containing landing pads and similar stuff. */
  4812. static void
  4813. construct_exit_block (void)
  4814. {
  4815. rtx_insn *head = get_last_insn ();
  4816. rtx_insn *end;
  4817. basic_block exit_block;
  4818. edge e, e2;
  4819. unsigned ix;
  4820. edge_iterator ei;
  4821. basic_block prev_bb = EXIT_BLOCK_PTR_FOR_FN (cfun)->prev_bb;
  4822. rtx_insn *orig_end = BB_END (prev_bb);
  4823. rtl_profile_for_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
  4824. /* Make sure the locus is set to the end of the function, so that
  4825. epilogue line numbers and warnings are set properly. */
  4826. if (LOCATION_LOCUS (cfun->function_end_locus) != UNKNOWN_LOCATION)
  4827. input_location = cfun->function_end_locus;
  4828. /* Generate rtl for function exit. */
  4829. expand_function_end ();
  4830. end = get_last_insn ();
  4831. if (head == end)
  4832. return;
  4833. /* While emitting the function end we could move end of the last basic
  4834. block. */
  4835. BB_END (prev_bb) = orig_end;
  4836. while (NEXT_INSN (head) && NOTE_P (NEXT_INSN (head)))
  4837. head = NEXT_INSN (head);
  4838. /* But make sure exit_block starts with RETURN_LABEL, otherwise the
  4839. bb frequency counting will be confused. Any instructions before that
  4840. label are emitted for the case where PREV_BB falls through into the
  4841. exit block, so append those instructions to prev_bb in that case. */
  4842. if (NEXT_INSN (head) != return_label)
  4843. {
  4844. while (NEXT_INSN (head) != return_label)
  4845. {
  4846. if (!NOTE_P (NEXT_INSN (head)))
  4847. BB_END (prev_bb) = NEXT_INSN (head);
  4848. head = NEXT_INSN (head);
  4849. }
  4850. }
  4851. exit_block = create_basic_block (NEXT_INSN (head), end, prev_bb);
  4852. exit_block->frequency = EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency;
  4853. exit_block->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
  4854. add_bb_to_loop (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun)->loop_father);
  4855. ix = 0;
  4856. while (ix < EDGE_COUNT (EXIT_BLOCK_PTR_FOR_FN (cfun)->preds))
  4857. {
  4858. e = EDGE_PRED (EXIT_BLOCK_PTR_FOR_FN (cfun), ix);
  4859. if (!(e->flags & EDGE_ABNORMAL))
  4860. redirect_edge_succ (e, exit_block);
  4861. else
  4862. ix++;
  4863. }
  4864. e = make_edge (exit_block, EXIT_BLOCK_PTR_FOR_FN (cfun), EDGE_FALLTHRU);
  4865. e->probability = REG_BR_PROB_BASE;
  4866. e->count = EXIT_BLOCK_PTR_FOR_FN (cfun)->count;
  4867. FOR_EACH_EDGE (e2, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
  4868. if (e2 != e)
  4869. {
  4870. e->count -= e2->count;
  4871. exit_block->count -= e2->count;
  4872. exit_block->frequency -= EDGE_FREQUENCY (e2);
  4873. }
  4874. if (e->count < 0)
  4875. e->count = 0;
  4876. if (exit_block->count < 0)
  4877. exit_block->count = 0;
  4878. if (exit_block->frequency < 0)
  4879. exit_block->frequency = 0;
  4880. update_bb_for_insn (exit_block);
  4881. }
  4882. /* Helper function for discover_nonconstant_array_refs.
  4883. Look for ARRAY_REF nodes with non-constant indexes and mark them
  4884. addressable. */
  4885. static tree
  4886. discover_nonconstant_array_refs_r (tree * tp, int *walk_subtrees,
  4887. void *data ATTRIBUTE_UNUSED)
  4888. {
  4889. tree t = *tp;
  4890. if (IS_TYPE_OR_DECL_P (t))
  4891. *walk_subtrees = 0;
  4892. else if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
  4893. {
  4894. while (((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
  4895. && is_gimple_min_invariant (TREE_OPERAND (t, 1))
  4896. && (!TREE_OPERAND (t, 2)
  4897. || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
  4898. || (TREE_CODE (t) == COMPONENT_REF
  4899. && (!TREE_OPERAND (t,2)
  4900. || is_gimple_min_invariant (TREE_OPERAND (t, 2))))
  4901. || TREE_CODE (t) == BIT_FIELD_REF
  4902. || TREE_CODE (t) == REALPART_EXPR
  4903. || TREE_CODE (t) == IMAGPART_EXPR
  4904. || TREE_CODE (t) == VIEW_CONVERT_EXPR
  4905. || CONVERT_EXPR_P (t))
  4906. t = TREE_OPERAND (t, 0);
  4907. if (TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
  4908. {
  4909. t = get_base_address (t);
  4910. if (t && DECL_P (t)
  4911. && DECL_MODE (t) != BLKmode)
  4912. TREE_ADDRESSABLE (t) = 1;
  4913. }
  4914. *walk_subtrees = 0;
  4915. }
  4916. return NULL_TREE;
  4917. }
  4918. /* RTL expansion is not able to compile array references with variable
  4919. offsets for arrays stored in single register. Discover such
  4920. expressions and mark variables as addressable to avoid this
  4921. scenario. */
  4922. static void
  4923. discover_nonconstant_array_refs (void)
  4924. {
  4925. basic_block bb;
  4926. gimple_stmt_iterator gsi;
  4927. FOR_EACH_BB_FN (bb, cfun)
  4928. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  4929. {
  4930. gimple stmt = gsi_stmt (gsi);
  4931. if (!is_gimple_debug (stmt))
  4932. walk_gimple_op (stmt, discover_nonconstant_array_refs_r, NULL);
  4933. }
  4934. }
  4935. /* This function sets crtl->args.internal_arg_pointer to a virtual
  4936. register if DRAP is needed. Local register allocator will replace
  4937. virtual_incoming_args_rtx with the virtual register. */
  4938. static void
  4939. expand_stack_alignment (void)
  4940. {
  4941. rtx drap_rtx;
  4942. unsigned int preferred_stack_boundary;
  4943. if (! SUPPORTS_STACK_ALIGNMENT)
  4944. return;
  4945. if (cfun->calls_alloca
  4946. || cfun->has_nonlocal_label
  4947. || crtl->has_nonlocal_goto)
  4948. crtl->need_drap = true;
  4949. /* Call update_stack_boundary here again to update incoming stack
  4950. boundary. It may set incoming stack alignment to a different
  4951. value after RTL expansion. TARGET_FUNCTION_OK_FOR_SIBCALL may
  4952. use the minimum incoming stack alignment to check if it is OK
  4953. to perform sibcall optimization since sibcall optimization will
  4954. only align the outgoing stack to incoming stack boundary. */
  4955. if (targetm.calls.update_stack_boundary)
  4956. targetm.calls.update_stack_boundary ();
  4957. /* The incoming stack frame has to be aligned at least at
  4958. parm_stack_boundary. */
  4959. gcc_assert (crtl->parm_stack_boundary <= INCOMING_STACK_BOUNDARY);
  4960. /* Update crtl->stack_alignment_estimated and use it later to align
  4961. stack. We check PREFERRED_STACK_BOUNDARY if there may be non-call
  4962. exceptions since callgraph doesn't collect incoming stack alignment
  4963. in this case. */
  4964. if (cfun->can_throw_non_call_exceptions
  4965. && PREFERRED_STACK_BOUNDARY > crtl->preferred_stack_boundary)
  4966. preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
  4967. else
  4968. preferred_stack_boundary = crtl->preferred_stack_boundary;
  4969. if (preferred_stack_boundary > crtl->stack_alignment_estimated)
  4970. crtl->stack_alignment_estimated = preferred_stack_boundary;
  4971. if (preferred_stack_boundary > crtl->stack_alignment_needed)
  4972. crtl->stack_alignment_needed = preferred_stack_boundary;
  4973. gcc_assert (crtl->stack_alignment_needed
  4974. <= crtl->stack_alignment_estimated);
  4975. crtl->stack_realign_needed
  4976. = INCOMING_STACK_BOUNDARY < crtl->stack_alignment_estimated;
  4977. crtl->stack_realign_tried = crtl->stack_realign_needed;
  4978. crtl->stack_realign_processed = true;
  4979. /* Target has to redefine TARGET_GET_DRAP_RTX to support stack
  4980. alignment. */
  4981. gcc_assert (targetm.calls.get_drap_rtx != NULL);
  4982. drap_rtx = targetm.calls.get_drap_rtx ();
  4983. /* stack_realign_drap and drap_rtx must match. */
  4984. gcc_assert ((stack_realign_drap != 0) == (drap_rtx != NULL));
  4985. /* Do nothing if NULL is returned, which means DRAP is not needed. */
  4986. if (NULL != drap_rtx)
  4987. {
  4988. crtl->args.internal_arg_pointer = drap_rtx;
  4989. /* Call fixup_tail_calls to clean up REG_EQUIV note if DRAP is
  4990. needed. */
  4991. fixup_tail_calls ();
  4992. }
  4993. }
  4994. static void
  4995. expand_main_function (void)
  4996. {
  4997. #if (defined(INVOKE__main) \
  4998. || (!defined(HAS_INIT_SECTION) \
  4999. && !defined(INIT_SECTION_ASM_OP) \
  5000. && !defined(INIT_ARRAY_SECTION_ASM_OP)))
  5001. emit_library_call (init_one_libfunc (NAME__MAIN), LCT_NORMAL, VOIDmode, 0);
  5002. #endif
  5003. }
  5004. /* Expand code to initialize the stack_protect_guard. This is invoked at
  5005. the beginning of a function to be protected. */
  5006. #ifndef HAVE_stack_protect_set
  5007. # define HAVE_stack_protect_set 0
  5008. # define gen_stack_protect_set(x,y) (gcc_unreachable (), NULL_RTX)
  5009. #endif
  5010. static void
  5011. stack_protect_prologue (void)
  5012. {
  5013. tree guard_decl = targetm.stack_protect_guard ();
  5014. rtx x, y;
  5015. x = expand_normal (crtl->stack_protect_guard);
  5016. y = expand_normal (guard_decl);
  5017. /* Allow the target to copy from Y to X without leaking Y into a
  5018. register. */
  5019. if (HAVE_stack_protect_set)
  5020. {
  5021. rtx insn = gen_stack_protect_set (x, y);
  5022. if (insn)
  5023. {
  5024. emit_insn (insn);
  5025. return;
  5026. }
  5027. }
  5028. /* Otherwise do a straight move. */
  5029. emit_move_insn (x, y);
  5030. }
  5031. /* Translate the intermediate representation contained in the CFG
  5032. from GIMPLE trees to RTL.
  5033. We do conversion per basic block and preserve/update the tree CFG.
  5034. This implies we have to do some magic as the CFG can simultaneously
  5035. consist of basic blocks containing RTL and GIMPLE trees. This can
  5036. confuse the CFG hooks, so be careful to not manipulate CFG during
  5037. the expansion. */
  5038. namespace {
  5039. const pass_data pass_data_expand =
  5040. {
  5041. RTL_PASS, /* type */
  5042. "expand", /* name */
  5043. OPTGROUP_NONE, /* optinfo_flags */
  5044. TV_EXPAND, /* tv_id */
  5045. ( PROP_ssa | PROP_gimple_leh | PROP_cfg
  5046. | PROP_gimple_lcx
  5047. | PROP_gimple_lvec ), /* properties_required */
  5048. PROP_rtl, /* properties_provided */
  5049. ( PROP_ssa | PROP_trees ), /* properties_destroyed */
  5050. 0, /* todo_flags_start */
  5051. 0, /* todo_flags_finish */
  5052. };
  5053. class pass_expand : public rtl_opt_pass
  5054. {
  5055. public:
  5056. pass_expand (gcc::context *ctxt)
  5057. : rtl_opt_pass (pass_data_expand, ctxt)
  5058. {}
  5059. /* opt_pass methods: */
  5060. virtual unsigned int execute (function *);
  5061. }; // class pass_expand
  5062. unsigned int
  5063. pass_expand::execute (function *fun)
  5064. {
  5065. basic_block bb, init_block;
  5066. sbitmap blocks;
  5067. edge_iterator ei;
  5068. edge e;
  5069. rtx_insn *var_seq, *var_ret_seq;
  5070. unsigned i;
  5071. timevar_push (TV_OUT_OF_SSA);
  5072. rewrite_out_of_ssa (&SA);
  5073. timevar_pop (TV_OUT_OF_SSA);
  5074. SA.partition_to_pseudo = XCNEWVEC (rtx, SA.map->num_partitions);
  5075. if (MAY_HAVE_DEBUG_STMTS && flag_tree_ter)
  5076. {
  5077. gimple_stmt_iterator gsi;
  5078. FOR_EACH_BB_FN (bb, cfun)
  5079. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  5080. if (gimple_debug_bind_p (gsi_stmt (gsi)))
  5081. avoid_deep_ter_for_debug (gsi_stmt (gsi), 0);
  5082. }
  5083. /* Make sure all values used by the optimization passes have sane
  5084. defaults. */
  5085. reg_renumber = 0;
  5086. /* Some backends want to know that we are expanding to RTL. */
  5087. currently_expanding_to_rtl = 1;
  5088. /* Dominators are not kept up-to-date as we may create new basic-blocks. */
  5089. free_dominance_info (CDI_DOMINATORS);
  5090. rtl_profile_for_bb (ENTRY_BLOCK_PTR_FOR_FN (fun));
  5091. if (chkp_function_instrumented_p (current_function_decl))
  5092. chkp_reset_rtl_bounds ();
  5093. insn_locations_init ();
  5094. if (!DECL_IS_BUILTIN (current_function_decl))
  5095. {
  5096. /* Eventually, all FEs should explicitly set function_start_locus. */
  5097. if (LOCATION_LOCUS (fun->function_start_locus) == UNKNOWN_LOCATION)
  5098. set_curr_insn_location
  5099. (DECL_SOURCE_LOCATION (current_function_decl));
  5100. else
  5101. set_curr_insn_location (fun->function_start_locus);
  5102. }
  5103. else
  5104. set_curr_insn_location (UNKNOWN_LOCATION);
  5105. prologue_location = curr_insn_location ();
  5106. #ifdef INSN_SCHEDULING
  5107. init_sched_attrs ();
  5108. #endif
  5109. /* Make sure first insn is a note even if we don't want linenums.
  5110. This makes sure the first insn will never be deleted.
  5111. Also, final expects a note to appear there. */
  5112. emit_note (NOTE_INSN_DELETED);
  5113. /* Mark arrays indexed with non-constant indices with TREE_ADDRESSABLE. */
  5114. discover_nonconstant_array_refs ();
  5115. targetm.expand_to_rtl_hook ();
  5116. crtl->stack_alignment_needed = STACK_BOUNDARY;
  5117. crtl->max_used_stack_slot_alignment = STACK_BOUNDARY;
  5118. crtl->stack_alignment_estimated = 0;
  5119. crtl->preferred_stack_boundary = STACK_BOUNDARY;
  5120. fun->cfg->max_jumptable_ents = 0;
  5121. /* Resovle the function section. Some targets, like ARM EABI rely on knowledge
  5122. of the function section at exapnsion time to predict distance of calls. */
  5123. resolve_unique_section (current_function_decl, 0, flag_function_sections);
  5124. /* Expand the variables recorded during gimple lowering. */
  5125. timevar_push (TV_VAR_EXPAND);
  5126. start_sequence ();
  5127. var_ret_seq = expand_used_vars ();
  5128. var_seq = get_insns ();
  5129. end_sequence ();
  5130. timevar_pop (TV_VAR_EXPAND);
  5131. /* Honor stack protection warnings. */
  5132. if (warn_stack_protect)
  5133. {
  5134. if (fun->calls_alloca)
  5135. warning (OPT_Wstack_protector,
  5136. "stack protector not protecting local variables: "
  5137. "variable length buffer");
  5138. if (has_short_buffer && !crtl->stack_protect_guard)
  5139. warning (OPT_Wstack_protector,
  5140. "stack protector not protecting function: "
  5141. "all local arrays are less than %d bytes long",
  5142. (int) PARAM_VALUE (PARAM_SSP_BUFFER_SIZE));
  5143. }
  5144. /* Set up parameters and prepare for return, for the function. */
  5145. expand_function_start (current_function_decl);
  5146. /* If we emitted any instructions for setting up the variables,
  5147. emit them before the FUNCTION_START note. */
  5148. if (var_seq)
  5149. {
  5150. emit_insn_before (var_seq, parm_birth_insn);
  5151. /* In expand_function_end we'll insert the alloca save/restore
  5152. before parm_birth_insn. We've just insertted an alloca call.
  5153. Adjust the pointer to match. */
  5154. parm_birth_insn = var_seq;
  5155. }
  5156. /* Now that we also have the parameter RTXs, copy them over to our
  5157. partitions. */
  5158. for (i = 0; i < SA.map->num_partitions; i++)
  5159. {
  5160. tree var = SSA_NAME_VAR (partition_to_var (SA.map, i));
  5161. if (TREE_CODE (var) != VAR_DECL
  5162. && !SA.partition_to_pseudo[i])
  5163. SA.partition_to_pseudo[i] = DECL_RTL_IF_SET (var);
  5164. gcc_assert (SA.partition_to_pseudo[i]);
  5165. /* If this decl was marked as living in multiple places, reset
  5166. this now to NULL. */
  5167. if (DECL_RTL_IF_SET (var) == pc_rtx)
  5168. SET_DECL_RTL (var, NULL);
  5169. /* Some RTL parts really want to look at DECL_RTL(x) when x
  5170. was a decl marked in REG_ATTR or MEM_ATTR. We could use
  5171. SET_DECL_RTL here making this available, but that would mean
  5172. to select one of the potentially many RTLs for one DECL. Instead
  5173. of doing that we simply reset the MEM_EXPR of the RTL in question,
  5174. then nobody can get at it and hence nobody can call DECL_RTL on it. */
  5175. if (!DECL_RTL_SET_P (var))
  5176. {
  5177. if (MEM_P (SA.partition_to_pseudo[i]))
  5178. set_mem_expr (SA.partition_to_pseudo[i], NULL);
  5179. }
  5180. }
  5181. /* If we have a class containing differently aligned pointers
  5182. we need to merge those into the corresponding RTL pointer
  5183. alignment. */
  5184. for (i = 1; i < num_ssa_names; i++)
  5185. {
  5186. tree name = ssa_name (i);
  5187. int part;
  5188. rtx r;
  5189. if (!name
  5190. /* We might have generated new SSA names in
  5191. update_alias_info_with_stack_vars. They will have a NULL
  5192. defining statements, and won't be part of the partitioning,
  5193. so ignore those. */
  5194. || !SSA_NAME_DEF_STMT (name))
  5195. continue;
  5196. part = var_to_partition (SA.map, name);
  5197. if (part == NO_PARTITION)
  5198. continue;
  5199. /* Adjust all partition members to get the underlying decl of
  5200. the representative which we might have created in expand_one_var. */
  5201. if (SSA_NAME_VAR (name) == NULL_TREE)
  5202. {
  5203. tree leader = partition_to_var (SA.map, part);
  5204. gcc_assert (SSA_NAME_VAR (leader) != NULL_TREE);
  5205. replace_ssa_name_symbol (name, SSA_NAME_VAR (leader));
  5206. }
  5207. if (!POINTER_TYPE_P (TREE_TYPE (name)))
  5208. continue;
  5209. r = SA.partition_to_pseudo[part];
  5210. if (REG_P (r))
  5211. mark_reg_pointer (r, get_pointer_alignment (name));
  5212. }
  5213. /* If this function is `main', emit a call to `__main'
  5214. to run global initializers, etc. */
  5215. if (DECL_NAME (current_function_decl)
  5216. && MAIN_NAME_P (DECL_NAME (current_function_decl))
  5217. && DECL_FILE_SCOPE_P (current_function_decl))
  5218. expand_main_function ();
  5219. /* Initialize the stack_protect_guard field. This must happen after the
  5220. call to __main (if any) so that the external decl is initialized. */
  5221. if (crtl->stack_protect_guard)
  5222. stack_protect_prologue ();
  5223. expand_phi_nodes (&SA);
  5224. /* Register rtl specific functions for cfg. */
  5225. rtl_register_cfg_hooks ();
  5226. init_block = construct_init_block ();
  5227. /* Clear EDGE_EXECUTABLE on the entry edge(s). It is cleaned from the
  5228. remaining edges later. */
  5229. FOR_EACH_EDGE (e, ei, ENTRY_BLOCK_PTR_FOR_FN (fun)->succs)
  5230. e->flags &= ~EDGE_EXECUTABLE;
  5231. lab_rtx_for_bb = new hash_map<basic_block, rtx_code_label *>;
  5232. FOR_BB_BETWEEN (bb, init_block->next_bb, EXIT_BLOCK_PTR_FOR_FN (fun),
  5233. next_bb)
  5234. bb = expand_gimple_basic_block (bb, var_ret_seq != NULL_RTX);
  5235. if (MAY_HAVE_DEBUG_INSNS)
  5236. expand_debug_locations ();
  5237. if (deep_ter_debug_map)
  5238. {
  5239. delete deep_ter_debug_map;
  5240. deep_ter_debug_map = NULL;
  5241. }
  5242. /* Free stuff we no longer need after GIMPLE optimizations. */
  5243. free_dominance_info (CDI_DOMINATORS);
  5244. free_dominance_info (CDI_POST_DOMINATORS);
  5245. delete_tree_cfg_annotations ();
  5246. timevar_push (TV_OUT_OF_SSA);
  5247. finish_out_of_ssa (&SA);
  5248. timevar_pop (TV_OUT_OF_SSA);
  5249. timevar_push (TV_POST_EXPAND);
  5250. /* We are no longer in SSA form. */
  5251. fun->gimple_df->in_ssa_p = false;
  5252. loops_state_clear (LOOP_CLOSED_SSA);
  5253. /* Expansion is used by optimization passes too, set maybe_hot_insn_p
  5254. conservatively to true until they are all profile aware. */
  5255. delete lab_rtx_for_bb;
  5256. free_histograms ();
  5257. construct_exit_block ();
  5258. insn_locations_finalize ();
  5259. if (var_ret_seq)
  5260. {
  5261. rtx_insn *after = return_label;
  5262. rtx_insn *next = NEXT_INSN (after);
  5263. if (next && NOTE_INSN_BASIC_BLOCK_P (next))
  5264. after = next;
  5265. emit_insn_after (var_ret_seq, after);
  5266. }
  5267. /* Zap the tree EH table. */
  5268. set_eh_throw_stmt_table (fun, NULL);
  5269. /* We need JUMP_LABEL be set in order to redirect jumps, and hence
  5270. split edges which edge insertions might do. */
  5271. rebuild_jump_labels (get_insns ());
  5272. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun),
  5273. EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
  5274. {
  5275. edge e;
  5276. edge_iterator ei;
  5277. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  5278. {
  5279. if (e->insns.r)
  5280. {
  5281. rebuild_jump_labels_chain (e->insns.r);
  5282. /* Put insns after parm birth, but before
  5283. NOTE_INSNS_FUNCTION_BEG. */
  5284. if (e->src == ENTRY_BLOCK_PTR_FOR_FN (fun)
  5285. && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (fun)))
  5286. {
  5287. rtx_insn *insns = e->insns.r;
  5288. e->insns.r = NULL;
  5289. if (NOTE_P (parm_birth_insn)
  5290. && NOTE_KIND (parm_birth_insn) == NOTE_INSN_FUNCTION_BEG)
  5291. emit_insn_before_noloc (insns, parm_birth_insn, e->dest);
  5292. else
  5293. emit_insn_after_noloc (insns, parm_birth_insn, e->dest);
  5294. }
  5295. else
  5296. commit_one_edge_insertion (e);
  5297. }
  5298. else
  5299. ei_next (&ei);
  5300. }
  5301. }
  5302. /* We're done expanding trees to RTL. */
  5303. currently_expanding_to_rtl = 0;
  5304. FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (fun)->next_bb,
  5305. EXIT_BLOCK_PTR_FOR_FN (fun), next_bb)
  5306. {
  5307. edge e;
  5308. edge_iterator ei;
  5309. for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); )
  5310. {
  5311. /* Clear EDGE_EXECUTABLE. This flag is never used in the backend. */
  5312. e->flags &= ~EDGE_EXECUTABLE;
  5313. /* At the moment not all abnormal edges match the RTL
  5314. representation. It is safe to remove them here as
  5315. find_many_sub_basic_blocks will rediscover them.
  5316. In the future we should get this fixed properly. */
  5317. if ((e->flags & EDGE_ABNORMAL)
  5318. && !(e->flags & EDGE_SIBCALL))
  5319. remove_edge (e);
  5320. else
  5321. ei_next (&ei);
  5322. }
  5323. }
  5324. blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
  5325. bitmap_ones (blocks);
  5326. find_many_sub_basic_blocks (blocks);
  5327. sbitmap_free (blocks);
  5328. purge_all_dead_edges ();
  5329. expand_stack_alignment ();
  5330. /* Fixup REG_EQUIV notes in the prologue if there are tailcalls in this
  5331. function. */
  5332. if (crtl->tail_call_emit)
  5333. fixup_tail_calls ();
  5334. /* After initial rtl generation, call back to finish generating
  5335. exception support code. We need to do this before cleaning up
  5336. the CFG as the code does not expect dead landing pads. */
  5337. if (fun->eh->region_tree != NULL)
  5338. finish_eh_generation ();
  5339. /* Remove unreachable blocks, otherwise we cannot compute dominators
  5340. which are needed for loop state verification. As a side-effect
  5341. this also compacts blocks.
  5342. ??? We cannot remove trivially dead insns here as for example
  5343. the DRAP reg on i?86 is not magically live at this point.
  5344. gcc.c-torture/execute/ipa-sra-2.c execution, -Os -m32 fails otherwise. */
  5345. cleanup_cfg (CLEANUP_NO_INSN_DEL);
  5346. #ifdef ENABLE_CHECKING
  5347. verify_flow_info ();
  5348. #endif
  5349. /* Initialize pseudos allocated for hard registers. */
  5350. emit_initial_value_sets ();
  5351. /* And finally unshare all RTL. */
  5352. unshare_all_rtl ();
  5353. /* There's no need to defer outputting this function any more; we
  5354. know we want to output it. */
  5355. DECL_DEFER_OUTPUT (current_function_decl) = 0;
  5356. /* Now that we're done expanding trees to RTL, we shouldn't have any
  5357. more CONCATs anywhere. */
  5358. generating_concat_p = 0;
  5359. if (dump_file)
  5360. {
  5361. fprintf (dump_file,
  5362. "\n\n;;\n;; Full RTL generated for this function:\n;;\n");
  5363. /* And the pass manager will dump RTL for us. */
  5364. }
  5365. /* If we're emitting a nested function, make sure its parent gets
  5366. emitted as well. Doing otherwise confuses debug info. */
  5367. {
  5368. tree parent;
  5369. for (parent = DECL_CONTEXT (current_function_decl);
  5370. parent != NULL_TREE;
  5371. parent = get_containing_scope (parent))
  5372. if (TREE_CODE (parent) == FUNCTION_DECL)
  5373. TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (parent)) = 1;
  5374. }
  5375. /* We are now committed to emitting code for this function. Do any
  5376. preparation, such as emitting abstract debug info for the inline
  5377. before it gets mangled by optimization. */
  5378. if (cgraph_function_possibly_inlined_p (current_function_decl))
  5379. (*debug_hooks->outlining_inline_function) (current_function_decl);
  5380. TREE_ASM_WRITTEN (current_function_decl) = 1;
  5381. /* After expanding, the return labels are no longer needed. */
  5382. return_label = NULL;
  5383. naked_return_label = NULL;
  5384. /* After expanding, the tm_restart map is no longer needed. */
  5385. if (fun->gimple_df->tm_restart)
  5386. fun->gimple_df->tm_restart = NULL;
  5387. /* Tag the blocks with a depth number so that change_scope can find
  5388. the common parent easily. */
  5389. set_block_levels (DECL_INITIAL (fun->decl), 0);
  5390. default_rtl_profile ();
  5391. timevar_pop (TV_POST_EXPAND);
  5392. return 0;
  5393. }
  5394. } // anon namespace
  5395. rtl_opt_pass *
  5396. make_pass_expand (gcc::context *ctxt)
  5397. {
  5398. return new pass_expand (ctxt);
  5399. }