tree-inline.c 181 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989
  1. /* Tree inlining.
  2. Copyright (C) 2001-2015 Free Software Foundation, Inc.
  3. Contributed by Alexandre Oliva <aoliva@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "diagnostic-core.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 "stor-layout.h"
  33. #include "calls.h"
  34. #include "tree-inline.h"
  35. #include "flags.h"
  36. #include "params.h"
  37. #include "insn-config.h"
  38. #include "hashtab.h"
  39. #include "langhooks.h"
  40. #include "predict.h"
  41. #include "hard-reg-set.h"
  42. #include "function.h"
  43. #include "dominance.h"
  44. #include "cfg.h"
  45. #include "cfganal.h"
  46. #include "basic-block.h"
  47. #include "tree-iterator.h"
  48. #include "intl.h"
  49. #include "tree-ssa-alias.h"
  50. #include "internal-fn.h"
  51. #include "gimple-fold.h"
  52. #include "tree-eh.h"
  53. #include "gimple-expr.h"
  54. #include "is-a.h"
  55. #include "gimple.h"
  56. #include "gimplify.h"
  57. #include "gimple-iterator.h"
  58. #include "gimplify-me.h"
  59. #include "gimple-walk.h"
  60. #include "gimple-ssa.h"
  61. #include "tree-cfg.h"
  62. #include "tree-phinodes.h"
  63. #include "ssa-iterators.h"
  64. #include "stringpool.h"
  65. #include "tree-ssanames.h"
  66. #include "tree-into-ssa.h"
  67. #include "rtl.h"
  68. #include "statistics.h"
  69. #include "real.h"
  70. #include "fixed-value.h"
  71. #include "expmed.h"
  72. #include "dojump.h"
  73. #include "explow.h"
  74. #include "emit-rtl.h"
  75. #include "varasm.h"
  76. #include "stmt.h"
  77. #include "expr.h"
  78. #include "tree-dfa.h"
  79. #include "tree-ssa.h"
  80. #include "tree-pretty-print.h"
  81. #include "except.h"
  82. #include "debug.h"
  83. #include "hash-map.h"
  84. #include "plugin-api.h"
  85. #include "ipa-ref.h"
  86. #include "cgraph.h"
  87. #include "alloc-pool.h"
  88. #include "symbol-summary.h"
  89. #include "ipa-prop.h"
  90. #include "value-prof.h"
  91. #include "tree-pass.h"
  92. #include "target.h"
  93. #include "cfgloop.h"
  94. #include "builtins.h"
  95. #include "tree-chkp.h"
  96. #include "rtl.h" /* FIXME: For asm_str_count. */
  97. /* I'm not real happy about this, but we need to handle gimple and
  98. non-gimple trees. */
  99. /* Inlining, Cloning, Versioning, Parallelization
  100. Inlining: a function body is duplicated, but the PARM_DECLs are
  101. remapped into VAR_DECLs, and non-void RETURN_EXPRs become
  102. MODIFY_EXPRs that store to a dedicated returned-value variable.
  103. The duplicated eh_region info of the copy will later be appended
  104. to the info for the caller; the eh_region info in copied throwing
  105. statements and RESX statements are adjusted accordingly.
  106. Cloning: (only in C++) We have one body for a con/de/structor, and
  107. multiple function decls, each with a unique parameter list.
  108. Duplicate the body, using the given splay tree; some parameters
  109. will become constants (like 0 or 1).
  110. Versioning: a function body is duplicated and the result is a new
  111. function rather than into blocks of an existing function as with
  112. inlining. Some parameters will become constants.
  113. Parallelization: a region of a function is duplicated resulting in
  114. a new function. Variables may be replaced with complex expressions
  115. to enable shared variable semantics.
  116. All of these will simultaneously lookup any callgraph edges. If
  117. we're going to inline the duplicated function body, and the given
  118. function has some cloned callgraph nodes (one for each place this
  119. function will be inlined) those callgraph edges will be duplicated.
  120. If we're cloning the body, those callgraph edges will be
  121. updated to point into the new body. (Note that the original
  122. callgraph node and edge list will not be altered.)
  123. See the CALL_EXPR handling case in copy_tree_body_r (). */
  124. /* To Do:
  125. o In order to make inlining-on-trees work, we pessimized
  126. function-local static constants. In particular, they are now
  127. always output, even when not addressed. Fix this by treating
  128. function-local static constants just like global static
  129. constants; the back-end already knows not to output them if they
  130. are not needed.
  131. o Provide heuristics to clamp inlining of recursive template
  132. calls? */
  133. /* Weights that estimate_num_insns uses to estimate the size of the
  134. produced code. */
  135. eni_weights eni_size_weights;
  136. /* Weights that estimate_num_insns uses to estimate the time necessary
  137. to execute the produced code. */
  138. eni_weights eni_time_weights;
  139. /* Prototypes. */
  140. static tree declare_return_variable (copy_body_data *, tree, tree, tree,
  141. basic_block);
  142. static void remap_block (tree *, copy_body_data *);
  143. static void copy_bind_expr (tree *, int *, copy_body_data *);
  144. static void declare_inline_vars (tree, tree);
  145. static void remap_save_expr (tree *, hash_map<tree, tree> *, int *);
  146. static void prepend_lexical_block (tree current_block, tree new_block);
  147. static tree copy_decl_to_var (tree, copy_body_data *);
  148. static tree copy_result_decl_to_var (tree, copy_body_data *);
  149. static tree copy_decl_maybe_to_var (tree, copy_body_data *);
  150. static gimple_seq remap_gimple_stmt (gimple, copy_body_data *);
  151. static bool delete_unreachable_blocks_update_callgraph (copy_body_data *id);
  152. static void insert_init_stmt (copy_body_data *, basic_block, gimple);
  153. /* Insert a tree->tree mapping for ID. Despite the name suggests
  154. that the trees should be variables, it is used for more than that. */
  155. void
  156. insert_decl_map (copy_body_data *id, tree key, tree value)
  157. {
  158. id->decl_map->put (key, value);
  159. /* Always insert an identity map as well. If we see this same new
  160. node again, we won't want to duplicate it a second time. */
  161. if (key != value)
  162. id->decl_map->put (value, value);
  163. }
  164. /* Insert a tree->tree mapping for ID. This is only used for
  165. variables. */
  166. static void
  167. insert_debug_decl_map (copy_body_data *id, tree key, tree value)
  168. {
  169. if (!gimple_in_ssa_p (id->src_cfun))
  170. return;
  171. if (!opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
  172. return;
  173. if (!target_for_debug_bind (key))
  174. return;
  175. gcc_assert (TREE_CODE (key) == PARM_DECL);
  176. gcc_assert (TREE_CODE (value) == VAR_DECL);
  177. if (!id->debug_map)
  178. id->debug_map = new hash_map<tree, tree>;
  179. id->debug_map->put (key, value);
  180. }
  181. /* If nonzero, we're remapping the contents of inlined debug
  182. statements. If negative, an error has occurred, such as a
  183. reference to a variable that isn't available in the inlined
  184. context. */
  185. static int processing_debug_stmt = 0;
  186. /* Construct new SSA name for old NAME. ID is the inline context. */
  187. static tree
  188. remap_ssa_name (tree name, copy_body_data *id)
  189. {
  190. tree new_tree, var;
  191. tree *n;
  192. gcc_assert (TREE_CODE (name) == SSA_NAME);
  193. n = id->decl_map->get (name);
  194. if (n)
  195. return unshare_expr (*n);
  196. if (processing_debug_stmt)
  197. {
  198. if (SSA_NAME_IS_DEFAULT_DEF (name)
  199. && TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
  200. && id->entry_bb == NULL
  201. && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
  202. {
  203. tree vexpr = make_node (DEBUG_EXPR_DECL);
  204. gimple def_temp;
  205. gimple_stmt_iterator gsi;
  206. tree val = SSA_NAME_VAR (name);
  207. n = id->decl_map->get (val);
  208. if (n != NULL)
  209. val = *n;
  210. if (TREE_CODE (val) != PARM_DECL)
  211. {
  212. processing_debug_stmt = -1;
  213. return name;
  214. }
  215. def_temp = gimple_build_debug_source_bind (vexpr, val, NULL);
  216. DECL_ARTIFICIAL (vexpr) = 1;
  217. TREE_TYPE (vexpr) = TREE_TYPE (name);
  218. DECL_MODE (vexpr) = DECL_MODE (SSA_NAME_VAR (name));
  219. gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
  220. gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
  221. return vexpr;
  222. }
  223. processing_debug_stmt = -1;
  224. return name;
  225. }
  226. /* Remap anonymous SSA names or SSA names of anonymous decls. */
  227. var = SSA_NAME_VAR (name);
  228. if (!var
  229. || (!SSA_NAME_IS_DEFAULT_DEF (name)
  230. && TREE_CODE (var) == VAR_DECL
  231. && !VAR_DECL_IS_VIRTUAL_OPERAND (var)
  232. && DECL_ARTIFICIAL (var)
  233. && DECL_IGNORED_P (var)
  234. && !DECL_NAME (var)))
  235. {
  236. struct ptr_info_def *pi;
  237. new_tree = make_ssa_name (remap_type (TREE_TYPE (name), id));
  238. if (!var && SSA_NAME_IDENTIFIER (name))
  239. SET_SSA_NAME_VAR_OR_IDENTIFIER (new_tree, SSA_NAME_IDENTIFIER (name));
  240. insert_decl_map (id, name, new_tree);
  241. SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
  242. = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
  243. /* At least IPA points-to info can be directly transferred. */
  244. if (id->src_cfun->gimple_df
  245. && id->src_cfun->gimple_df->ipa_pta
  246. && (pi = SSA_NAME_PTR_INFO (name))
  247. && !pi->pt.anything)
  248. {
  249. struct ptr_info_def *new_pi = get_ptr_info (new_tree);
  250. new_pi->pt = pi->pt;
  251. }
  252. return new_tree;
  253. }
  254. /* Do not set DEF_STMT yet as statement is not copied yet. We do that
  255. in copy_bb. */
  256. new_tree = remap_decl (var, id);
  257. /* We might've substituted constant or another SSA_NAME for
  258. the variable.
  259. Replace the SSA name representing RESULT_DECL by variable during
  260. inlining: this saves us from need to introduce PHI node in a case
  261. return value is just partly initialized. */
  262. if ((TREE_CODE (new_tree) == VAR_DECL || TREE_CODE (new_tree) == PARM_DECL)
  263. && (!SSA_NAME_VAR (name)
  264. || TREE_CODE (SSA_NAME_VAR (name)) != RESULT_DECL
  265. || !id->transform_return_to_modify))
  266. {
  267. struct ptr_info_def *pi;
  268. new_tree = make_ssa_name (new_tree);
  269. insert_decl_map (id, name, new_tree);
  270. SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_tree)
  271. = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name);
  272. /* At least IPA points-to info can be directly transferred. */
  273. if (id->src_cfun->gimple_df
  274. && id->src_cfun->gimple_df->ipa_pta
  275. && (pi = SSA_NAME_PTR_INFO (name))
  276. && !pi->pt.anything)
  277. {
  278. struct ptr_info_def *new_pi = get_ptr_info (new_tree);
  279. new_pi->pt = pi->pt;
  280. }
  281. if (SSA_NAME_IS_DEFAULT_DEF (name))
  282. {
  283. /* By inlining function having uninitialized variable, we might
  284. extend the lifetime (variable might get reused). This cause
  285. ICE in the case we end up extending lifetime of SSA name across
  286. abnormal edge, but also increase register pressure.
  287. We simply initialize all uninitialized vars by 0 except
  288. for case we are inlining to very first BB. We can avoid
  289. this for all BBs that are not inside strongly connected
  290. regions of the CFG, but this is expensive to test. */
  291. if (id->entry_bb
  292. && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name)
  293. && (!SSA_NAME_VAR (name)
  294. || TREE_CODE (SSA_NAME_VAR (name)) != PARM_DECL)
  295. && (id->entry_bb != EDGE_SUCC (ENTRY_BLOCK_PTR_FOR_FN (cfun),
  296. 0)->dest
  297. || EDGE_COUNT (id->entry_bb->preds) != 1))
  298. {
  299. gimple_stmt_iterator gsi = gsi_last_bb (id->entry_bb);
  300. gimple init_stmt;
  301. tree zero = build_zero_cst (TREE_TYPE (new_tree));
  302. init_stmt = gimple_build_assign (new_tree, zero);
  303. gsi_insert_after (&gsi, init_stmt, GSI_NEW_STMT);
  304. SSA_NAME_IS_DEFAULT_DEF (new_tree) = 0;
  305. }
  306. else
  307. {
  308. SSA_NAME_DEF_STMT (new_tree) = gimple_build_nop ();
  309. set_ssa_default_def (cfun, SSA_NAME_VAR (new_tree), new_tree);
  310. }
  311. }
  312. }
  313. else
  314. insert_decl_map (id, name, new_tree);
  315. return new_tree;
  316. }
  317. /* Remap DECL during the copying of the BLOCK tree for the function. */
  318. tree
  319. remap_decl (tree decl, copy_body_data *id)
  320. {
  321. tree *n;
  322. /* We only remap local variables in the current function. */
  323. /* See if we have remapped this declaration. */
  324. n = id->decl_map->get (decl);
  325. if (!n && processing_debug_stmt)
  326. {
  327. processing_debug_stmt = -1;
  328. return decl;
  329. }
  330. /* If we didn't already have an equivalent for this declaration,
  331. create one now. */
  332. if (!n)
  333. {
  334. /* Make a copy of the variable or label. */
  335. tree t = id->copy_decl (decl, id);
  336. /* Remember it, so that if we encounter this local entity again
  337. we can reuse this copy. Do this early because remap_type may
  338. need this decl for TYPE_STUB_DECL. */
  339. insert_decl_map (id, decl, t);
  340. if (!DECL_P (t))
  341. return t;
  342. /* Remap types, if necessary. */
  343. TREE_TYPE (t) = remap_type (TREE_TYPE (t), id);
  344. if (TREE_CODE (t) == TYPE_DECL)
  345. DECL_ORIGINAL_TYPE (t) = remap_type (DECL_ORIGINAL_TYPE (t), id);
  346. /* Remap sizes as necessary. */
  347. walk_tree (&DECL_SIZE (t), copy_tree_body_r, id, NULL);
  348. walk_tree (&DECL_SIZE_UNIT (t), copy_tree_body_r, id, NULL);
  349. /* If fields, do likewise for offset and qualifier. */
  350. if (TREE_CODE (t) == FIELD_DECL)
  351. {
  352. walk_tree (&DECL_FIELD_OFFSET (t), copy_tree_body_r, id, NULL);
  353. if (TREE_CODE (DECL_CONTEXT (t)) == QUAL_UNION_TYPE)
  354. walk_tree (&DECL_QUALIFIER (t), copy_tree_body_r, id, NULL);
  355. }
  356. return t;
  357. }
  358. if (id->do_not_unshare)
  359. return *n;
  360. else
  361. return unshare_expr (*n);
  362. }
  363. static tree
  364. remap_type_1 (tree type, copy_body_data *id)
  365. {
  366. tree new_tree, t;
  367. /* We do need a copy. build and register it now. If this is a pointer or
  368. reference type, remap the designated type and make a new pointer or
  369. reference type. */
  370. if (TREE_CODE (type) == POINTER_TYPE)
  371. {
  372. new_tree = build_pointer_type_for_mode (remap_type (TREE_TYPE (type), id),
  373. TYPE_MODE (type),
  374. TYPE_REF_CAN_ALIAS_ALL (type));
  375. if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
  376. new_tree = build_type_attribute_qual_variant (new_tree,
  377. TYPE_ATTRIBUTES (type),
  378. TYPE_QUALS (type));
  379. insert_decl_map (id, type, new_tree);
  380. return new_tree;
  381. }
  382. else if (TREE_CODE (type) == REFERENCE_TYPE)
  383. {
  384. new_tree = build_reference_type_for_mode (remap_type (TREE_TYPE (type), id),
  385. TYPE_MODE (type),
  386. TYPE_REF_CAN_ALIAS_ALL (type));
  387. if (TYPE_ATTRIBUTES (type) || TYPE_QUALS (type))
  388. new_tree = build_type_attribute_qual_variant (new_tree,
  389. TYPE_ATTRIBUTES (type),
  390. TYPE_QUALS (type));
  391. insert_decl_map (id, type, new_tree);
  392. return new_tree;
  393. }
  394. else
  395. new_tree = copy_node (type);
  396. insert_decl_map (id, type, new_tree);
  397. /* This is a new type, not a copy of an old type. Need to reassociate
  398. variants. We can handle everything except the main variant lazily. */
  399. t = TYPE_MAIN_VARIANT (type);
  400. if (type != t)
  401. {
  402. t = remap_type (t, id);
  403. TYPE_MAIN_VARIANT (new_tree) = t;
  404. TYPE_NEXT_VARIANT (new_tree) = TYPE_NEXT_VARIANT (t);
  405. TYPE_NEXT_VARIANT (t) = new_tree;
  406. }
  407. else
  408. {
  409. TYPE_MAIN_VARIANT (new_tree) = new_tree;
  410. TYPE_NEXT_VARIANT (new_tree) = NULL;
  411. }
  412. if (TYPE_STUB_DECL (type))
  413. TYPE_STUB_DECL (new_tree) = remap_decl (TYPE_STUB_DECL (type), id);
  414. /* Lazily create pointer and reference types. */
  415. TYPE_POINTER_TO (new_tree) = NULL;
  416. TYPE_REFERENCE_TO (new_tree) = NULL;
  417. /* Copy all types that may contain references to local variables; be sure to
  418. preserve sharing in between type and its main variant when possible. */
  419. switch (TREE_CODE (new_tree))
  420. {
  421. case INTEGER_TYPE:
  422. case REAL_TYPE:
  423. case FIXED_POINT_TYPE:
  424. case ENUMERAL_TYPE:
  425. case BOOLEAN_TYPE:
  426. if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
  427. {
  428. gcc_checking_assert (TYPE_MIN_VALUE (type) == TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (type)));
  429. gcc_checking_assert (TYPE_MAX_VALUE (type) == TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (type)));
  430. TYPE_MIN_VALUE (new_tree) = TYPE_MIN_VALUE (TYPE_MAIN_VARIANT (new_tree));
  431. TYPE_MAX_VALUE (new_tree) = TYPE_MAX_VALUE (TYPE_MAIN_VARIANT (new_tree));
  432. }
  433. else
  434. {
  435. t = TYPE_MIN_VALUE (new_tree);
  436. if (t && TREE_CODE (t) != INTEGER_CST)
  437. walk_tree (&TYPE_MIN_VALUE (new_tree), copy_tree_body_r, id, NULL);
  438. t = TYPE_MAX_VALUE (new_tree);
  439. if (t && TREE_CODE (t) != INTEGER_CST)
  440. walk_tree (&TYPE_MAX_VALUE (new_tree), copy_tree_body_r, id, NULL);
  441. }
  442. return new_tree;
  443. case FUNCTION_TYPE:
  444. if (TYPE_MAIN_VARIANT (new_tree) != new_tree
  445. && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
  446. TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
  447. else
  448. TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
  449. if (TYPE_MAIN_VARIANT (new_tree) != new_tree
  450. && TYPE_ARG_TYPES (type) == TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (type)))
  451. TYPE_ARG_TYPES (new_tree) = TYPE_ARG_TYPES (TYPE_MAIN_VARIANT (new_tree));
  452. else
  453. walk_tree (&TYPE_ARG_TYPES (new_tree), copy_tree_body_r, id, NULL);
  454. return new_tree;
  455. case ARRAY_TYPE:
  456. if (TYPE_MAIN_VARIANT (new_tree) != new_tree
  457. && TREE_TYPE (type) == TREE_TYPE (TYPE_MAIN_VARIANT (type)))
  458. TREE_TYPE (new_tree) = TREE_TYPE (TYPE_MAIN_VARIANT (new_tree));
  459. else
  460. TREE_TYPE (new_tree) = remap_type (TREE_TYPE (new_tree), id);
  461. if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
  462. {
  463. gcc_checking_assert (TYPE_DOMAIN (type) == TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)));
  464. TYPE_DOMAIN (new_tree) = TYPE_DOMAIN (TYPE_MAIN_VARIANT (new_tree));
  465. }
  466. else
  467. TYPE_DOMAIN (new_tree) = remap_type (TYPE_DOMAIN (new_tree), id);
  468. break;
  469. case RECORD_TYPE:
  470. case UNION_TYPE:
  471. case QUAL_UNION_TYPE:
  472. if (TYPE_MAIN_VARIANT (type) != type
  473. && TYPE_FIELDS (type) == TYPE_FIELDS (TYPE_MAIN_VARIANT (type)))
  474. TYPE_FIELDS (new_tree) = TYPE_FIELDS (TYPE_MAIN_VARIANT (new_tree));
  475. else
  476. {
  477. tree f, nf = NULL;
  478. for (f = TYPE_FIELDS (new_tree); f ; f = DECL_CHAIN (f))
  479. {
  480. t = remap_decl (f, id);
  481. DECL_CONTEXT (t) = new_tree;
  482. DECL_CHAIN (t) = nf;
  483. nf = t;
  484. }
  485. TYPE_FIELDS (new_tree) = nreverse (nf);
  486. }
  487. break;
  488. case OFFSET_TYPE:
  489. default:
  490. /* Shouldn't have been thought variable sized. */
  491. gcc_unreachable ();
  492. }
  493. /* All variants of type share the same size, so use the already remaped data. */
  494. if (TYPE_MAIN_VARIANT (new_tree) != new_tree)
  495. {
  496. gcc_checking_assert (TYPE_SIZE (type) == TYPE_SIZE (TYPE_MAIN_VARIANT (type)));
  497. gcc_checking_assert (TYPE_SIZE_UNIT (type) == TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (type)));
  498. TYPE_SIZE (new_tree) = TYPE_SIZE (TYPE_MAIN_VARIANT (new_tree));
  499. TYPE_SIZE_UNIT (new_tree) = TYPE_SIZE_UNIT (TYPE_MAIN_VARIANT (new_tree));
  500. }
  501. else
  502. {
  503. walk_tree (&TYPE_SIZE (new_tree), copy_tree_body_r, id, NULL);
  504. walk_tree (&TYPE_SIZE_UNIT (new_tree), copy_tree_body_r, id, NULL);
  505. }
  506. return new_tree;
  507. }
  508. tree
  509. remap_type (tree type, copy_body_data *id)
  510. {
  511. tree *node;
  512. tree tmp;
  513. if (type == NULL)
  514. return type;
  515. /* See if we have remapped this type. */
  516. node = id->decl_map->get (type);
  517. if (node)
  518. return *node;
  519. /* The type only needs remapping if it's variably modified. */
  520. if (! variably_modified_type_p (type, id->src_fn))
  521. {
  522. insert_decl_map (id, type, type);
  523. return type;
  524. }
  525. id->remapping_type_depth++;
  526. tmp = remap_type_1 (type, id);
  527. id->remapping_type_depth--;
  528. return tmp;
  529. }
  530. /* Decide if DECL can be put into BLOCK_NONLOCAL_VARs. */
  531. static bool
  532. can_be_nonlocal (tree decl, copy_body_data *id)
  533. {
  534. /* We can not duplicate function decls. */
  535. if (TREE_CODE (decl) == FUNCTION_DECL)
  536. return true;
  537. /* Local static vars must be non-local or we get multiple declaration
  538. problems. */
  539. if (TREE_CODE (decl) == VAR_DECL
  540. && !auto_var_in_fn_p (decl, id->src_fn))
  541. return true;
  542. return false;
  543. }
  544. static tree
  545. remap_decls (tree decls, vec<tree, va_gc> **nonlocalized_list,
  546. copy_body_data *id)
  547. {
  548. tree old_var;
  549. tree new_decls = NULL_TREE;
  550. /* Remap its variables. */
  551. for (old_var = decls; old_var; old_var = DECL_CHAIN (old_var))
  552. {
  553. tree new_var;
  554. if (can_be_nonlocal (old_var, id))
  555. {
  556. /* We need to add this variable to the local decls as otherwise
  557. nothing else will do so. */
  558. if (TREE_CODE (old_var) == VAR_DECL
  559. && ! DECL_EXTERNAL (old_var))
  560. add_local_decl (cfun, old_var);
  561. if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
  562. && !DECL_IGNORED_P (old_var)
  563. && nonlocalized_list)
  564. vec_safe_push (*nonlocalized_list, old_var);
  565. continue;
  566. }
  567. /* Remap the variable. */
  568. new_var = remap_decl (old_var, id);
  569. /* If we didn't remap this variable, we can't mess with its
  570. TREE_CHAIN. If we remapped this variable to the return slot, it's
  571. already declared somewhere else, so don't declare it here. */
  572. if (new_var == id->retvar)
  573. ;
  574. else if (!new_var)
  575. {
  576. if ((!optimize || debug_info_level > DINFO_LEVEL_TERSE)
  577. && !DECL_IGNORED_P (old_var)
  578. && nonlocalized_list)
  579. vec_safe_push (*nonlocalized_list, old_var);
  580. }
  581. else
  582. {
  583. gcc_assert (DECL_P (new_var));
  584. DECL_CHAIN (new_var) = new_decls;
  585. new_decls = new_var;
  586. /* Also copy value-expressions. */
  587. if (TREE_CODE (new_var) == VAR_DECL
  588. && DECL_HAS_VALUE_EXPR_P (new_var))
  589. {
  590. tree tem = DECL_VALUE_EXPR (new_var);
  591. bool old_regimplify = id->regimplify;
  592. id->remapping_type_depth++;
  593. walk_tree (&tem, copy_tree_body_r, id, NULL);
  594. id->remapping_type_depth--;
  595. id->regimplify = old_regimplify;
  596. SET_DECL_VALUE_EXPR (new_var, tem);
  597. }
  598. }
  599. }
  600. return nreverse (new_decls);
  601. }
  602. /* Copy the BLOCK to contain remapped versions of the variables
  603. therein. And hook the new block into the block-tree. */
  604. static void
  605. remap_block (tree *block, copy_body_data *id)
  606. {
  607. tree old_block;
  608. tree new_block;
  609. /* Make the new block. */
  610. old_block = *block;
  611. new_block = make_node (BLOCK);
  612. TREE_USED (new_block) = TREE_USED (old_block);
  613. BLOCK_ABSTRACT_ORIGIN (new_block) = old_block;
  614. BLOCK_SOURCE_LOCATION (new_block) = BLOCK_SOURCE_LOCATION (old_block);
  615. BLOCK_NONLOCALIZED_VARS (new_block)
  616. = vec_safe_copy (BLOCK_NONLOCALIZED_VARS (old_block));
  617. *block = new_block;
  618. /* Remap its variables. */
  619. BLOCK_VARS (new_block) = remap_decls (BLOCK_VARS (old_block),
  620. &BLOCK_NONLOCALIZED_VARS (new_block),
  621. id);
  622. if (id->transform_lang_insert_block)
  623. id->transform_lang_insert_block (new_block);
  624. /* Remember the remapped block. */
  625. insert_decl_map (id, old_block, new_block);
  626. }
  627. /* Copy the whole block tree and root it in id->block. */
  628. static tree
  629. remap_blocks (tree block, copy_body_data *id)
  630. {
  631. tree t;
  632. tree new_tree = block;
  633. if (!block)
  634. return NULL;
  635. remap_block (&new_tree, id);
  636. gcc_assert (new_tree != block);
  637. for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
  638. prepend_lexical_block (new_tree, remap_blocks (t, id));
  639. /* Blocks are in arbitrary order, but make things slightly prettier and do
  640. not swap order when producing a copy. */
  641. BLOCK_SUBBLOCKS (new_tree) = blocks_nreverse (BLOCK_SUBBLOCKS (new_tree));
  642. return new_tree;
  643. }
  644. /* Remap the block tree rooted at BLOCK to nothing. */
  645. static void
  646. remap_blocks_to_null (tree block, copy_body_data *id)
  647. {
  648. tree t;
  649. insert_decl_map (id, block, NULL_TREE);
  650. for (t = BLOCK_SUBBLOCKS (block); t ; t = BLOCK_CHAIN (t))
  651. remap_blocks_to_null (t, id);
  652. }
  653. static void
  654. copy_statement_list (tree *tp)
  655. {
  656. tree_stmt_iterator oi, ni;
  657. tree new_tree;
  658. new_tree = alloc_stmt_list ();
  659. ni = tsi_start (new_tree);
  660. oi = tsi_start (*tp);
  661. TREE_TYPE (new_tree) = TREE_TYPE (*tp);
  662. *tp = new_tree;
  663. for (; !tsi_end_p (oi); tsi_next (&oi))
  664. {
  665. tree stmt = tsi_stmt (oi);
  666. if (TREE_CODE (stmt) == STATEMENT_LIST)
  667. /* This copy is not redundant; tsi_link_after will smash this
  668. STATEMENT_LIST into the end of the one we're building, and we
  669. don't want to do that with the original. */
  670. copy_statement_list (&stmt);
  671. tsi_link_after (&ni, stmt, TSI_CONTINUE_LINKING);
  672. }
  673. }
  674. static void
  675. copy_bind_expr (tree *tp, int *walk_subtrees, copy_body_data *id)
  676. {
  677. tree block = BIND_EXPR_BLOCK (*tp);
  678. /* Copy (and replace) the statement. */
  679. copy_tree_r (tp, walk_subtrees, NULL);
  680. if (block)
  681. {
  682. remap_block (&block, id);
  683. BIND_EXPR_BLOCK (*tp) = block;
  684. }
  685. if (BIND_EXPR_VARS (*tp))
  686. /* This will remap a lot of the same decls again, but this should be
  687. harmless. */
  688. BIND_EXPR_VARS (*tp) = remap_decls (BIND_EXPR_VARS (*tp), NULL, id);
  689. }
  690. /* Create a new gimple_seq by remapping all the statements in BODY
  691. using the inlining information in ID. */
  692. static gimple_seq
  693. remap_gimple_seq (gimple_seq body, copy_body_data *id)
  694. {
  695. gimple_stmt_iterator si;
  696. gimple_seq new_body = NULL;
  697. for (si = gsi_start (body); !gsi_end_p (si); gsi_next (&si))
  698. {
  699. gimple_seq new_stmts = remap_gimple_stmt (gsi_stmt (si), id);
  700. gimple_seq_add_seq (&new_body, new_stmts);
  701. }
  702. return new_body;
  703. }
  704. /* Copy a GIMPLE_BIND statement STMT, remapping all the symbols in its
  705. block using the mapping information in ID. */
  706. static gimple
  707. copy_gimple_bind (gbind *stmt, copy_body_data *id)
  708. {
  709. gimple new_bind;
  710. tree new_block, new_vars;
  711. gimple_seq body, new_body;
  712. /* Copy the statement. Note that we purposely don't use copy_stmt
  713. here because we need to remap statements as we copy. */
  714. body = gimple_bind_body (stmt);
  715. new_body = remap_gimple_seq (body, id);
  716. new_block = gimple_bind_block (stmt);
  717. if (new_block)
  718. remap_block (&new_block, id);
  719. /* This will remap a lot of the same decls again, but this should be
  720. harmless. */
  721. new_vars = gimple_bind_vars (stmt);
  722. if (new_vars)
  723. new_vars = remap_decls (new_vars, NULL, id);
  724. new_bind = gimple_build_bind (new_vars, new_body, new_block);
  725. return new_bind;
  726. }
  727. /* Return true if DECL is a parameter or a SSA_NAME for a parameter. */
  728. static bool
  729. is_parm (tree decl)
  730. {
  731. if (TREE_CODE (decl) == SSA_NAME)
  732. {
  733. decl = SSA_NAME_VAR (decl);
  734. if (!decl)
  735. return false;
  736. }
  737. return (TREE_CODE (decl) == PARM_DECL);
  738. }
  739. /* Remap the dependence CLIQUE from the source to the destination function
  740. as specified in ID. */
  741. static unsigned short
  742. remap_dependence_clique (copy_body_data *id, unsigned short clique)
  743. {
  744. if (clique == 0)
  745. return 0;
  746. if (!id->dependence_map)
  747. id->dependence_map
  748. = new hash_map<unsigned short, unsigned short, dependence_hasher>;
  749. bool existed;
  750. unsigned short &newc = id->dependence_map->get_or_insert (clique, &existed);
  751. if (!existed)
  752. newc = ++cfun->last_clique;
  753. return newc;
  754. }
  755. /* Remap the GIMPLE operand pointed to by *TP. DATA is really a
  756. 'struct walk_stmt_info *'. DATA->INFO is a 'copy_body_data *'.
  757. WALK_SUBTREES is used to indicate walk_gimple_op whether to keep
  758. recursing into the children nodes of *TP. */
  759. static tree
  760. remap_gimple_op_r (tree *tp, int *walk_subtrees, void *data)
  761. {
  762. struct walk_stmt_info *wi_p = (struct walk_stmt_info *) data;
  763. copy_body_data *id = (copy_body_data *) wi_p->info;
  764. tree fn = id->src_fn;
  765. if (TREE_CODE (*tp) == SSA_NAME)
  766. {
  767. *tp = remap_ssa_name (*tp, id);
  768. *walk_subtrees = 0;
  769. return NULL;
  770. }
  771. else if (auto_var_in_fn_p (*tp, fn))
  772. {
  773. /* Local variables and labels need to be replaced by equivalent
  774. variables. We don't want to copy static variables; there's
  775. only one of those, no matter how many times we inline the
  776. containing function. Similarly for globals from an outer
  777. function. */
  778. tree new_decl;
  779. /* Remap the declaration. */
  780. new_decl = remap_decl (*tp, id);
  781. gcc_assert (new_decl);
  782. /* Replace this variable with the copy. */
  783. STRIP_TYPE_NOPS (new_decl);
  784. /* ??? The C++ frontend uses void * pointer zero to initialize
  785. any other type. This confuses the middle-end type verification.
  786. As cloned bodies do not go through gimplification again the fixup
  787. there doesn't trigger. */
  788. if (TREE_CODE (new_decl) == INTEGER_CST
  789. && !useless_type_conversion_p (TREE_TYPE (*tp), TREE_TYPE (new_decl)))
  790. new_decl = fold_convert (TREE_TYPE (*tp), new_decl);
  791. *tp = new_decl;
  792. *walk_subtrees = 0;
  793. }
  794. else if (TREE_CODE (*tp) == STATEMENT_LIST)
  795. gcc_unreachable ();
  796. else if (TREE_CODE (*tp) == SAVE_EXPR)
  797. gcc_unreachable ();
  798. else if (TREE_CODE (*tp) == LABEL_DECL
  799. && (!DECL_CONTEXT (*tp)
  800. || decl_function_context (*tp) == id->src_fn))
  801. /* These may need to be remapped for EH handling. */
  802. *tp = remap_decl (*tp, id);
  803. else if (TREE_CODE (*tp) == FIELD_DECL)
  804. {
  805. /* If the enclosing record type is variably_modified_type_p, the field
  806. has already been remapped. Otherwise, it need not be. */
  807. tree *n = id->decl_map->get (*tp);
  808. if (n)
  809. *tp = *n;
  810. *walk_subtrees = 0;
  811. }
  812. else if (TYPE_P (*tp))
  813. /* Types may need remapping as well. */
  814. *tp = remap_type (*tp, id);
  815. else if (CONSTANT_CLASS_P (*tp))
  816. {
  817. /* If this is a constant, we have to copy the node iff the type
  818. will be remapped. copy_tree_r will not copy a constant. */
  819. tree new_type = remap_type (TREE_TYPE (*tp), id);
  820. if (new_type == TREE_TYPE (*tp))
  821. *walk_subtrees = 0;
  822. else if (TREE_CODE (*tp) == INTEGER_CST)
  823. *tp = wide_int_to_tree (new_type, *tp);
  824. else
  825. {
  826. *tp = copy_node (*tp);
  827. TREE_TYPE (*tp) = new_type;
  828. }
  829. }
  830. else
  831. {
  832. /* Otherwise, just copy the node. Note that copy_tree_r already
  833. knows not to copy VAR_DECLs, etc., so this is safe. */
  834. if (TREE_CODE (*tp) == MEM_REF)
  835. {
  836. /* We need to re-canonicalize MEM_REFs from inline substitutions
  837. that can happen when a pointer argument is an ADDR_EXPR.
  838. Recurse here manually to allow that. */
  839. tree ptr = TREE_OPERAND (*tp, 0);
  840. tree type = remap_type (TREE_TYPE (*tp), id);
  841. tree old = *tp;
  842. walk_tree (&ptr, remap_gimple_op_r, data, NULL);
  843. *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
  844. TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
  845. TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
  846. TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
  847. if (MR_DEPENDENCE_CLIQUE (old) != 0)
  848. {
  849. MR_DEPENDENCE_CLIQUE (*tp)
  850. = remap_dependence_clique (id, MR_DEPENDENCE_CLIQUE (old));
  851. MR_DEPENDENCE_BASE (*tp) = MR_DEPENDENCE_BASE (old);
  852. }
  853. /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
  854. remapped a parameter as the property might be valid only
  855. for the parameter itself. */
  856. if (TREE_THIS_NOTRAP (old)
  857. && (!is_parm (TREE_OPERAND (old, 0))
  858. || (!id->transform_parameter && is_parm (ptr))))
  859. TREE_THIS_NOTRAP (*tp) = 1;
  860. *walk_subtrees = 0;
  861. return NULL;
  862. }
  863. /* Here is the "usual case". Copy this tree node, and then
  864. tweak some special cases. */
  865. copy_tree_r (tp, walk_subtrees, NULL);
  866. if (TREE_CODE (*tp) != OMP_CLAUSE)
  867. TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
  868. if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
  869. {
  870. /* The copied TARGET_EXPR has never been expanded, even if the
  871. original node was expanded already. */
  872. TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
  873. TREE_OPERAND (*tp, 3) = NULL_TREE;
  874. }
  875. else if (TREE_CODE (*tp) == ADDR_EXPR)
  876. {
  877. /* Variable substitution need not be simple. In particular,
  878. the MEM_REF substitution above. Make sure that
  879. TREE_CONSTANT and friends are up-to-date. */
  880. int invariant = is_gimple_min_invariant (*tp);
  881. walk_tree (&TREE_OPERAND (*tp, 0), remap_gimple_op_r, data, NULL);
  882. recompute_tree_invariant_for_addr_expr (*tp);
  883. /* If this used to be invariant, but is not any longer,
  884. then regimplification is probably needed. */
  885. if (invariant && !is_gimple_min_invariant (*tp))
  886. id->regimplify = true;
  887. *walk_subtrees = 0;
  888. }
  889. }
  890. /* Update the TREE_BLOCK for the cloned expr. */
  891. if (EXPR_P (*tp))
  892. {
  893. tree new_block = id->remapping_type_depth == 0 ? id->block : NULL;
  894. tree old_block = TREE_BLOCK (*tp);
  895. if (old_block)
  896. {
  897. tree *n;
  898. n = id->decl_map->get (TREE_BLOCK (*tp));
  899. if (n)
  900. new_block = *n;
  901. }
  902. TREE_SET_BLOCK (*tp, new_block);
  903. }
  904. /* Keep iterating. */
  905. return NULL_TREE;
  906. }
  907. /* Called from copy_body_id via walk_tree. DATA is really a
  908. `copy_body_data *'. */
  909. tree
  910. copy_tree_body_r (tree *tp, int *walk_subtrees, void *data)
  911. {
  912. copy_body_data *id = (copy_body_data *) data;
  913. tree fn = id->src_fn;
  914. tree new_block;
  915. /* Begin by recognizing trees that we'll completely rewrite for the
  916. inlining context. Our output for these trees is completely
  917. different from out input (e.g. RETURN_EXPR is deleted, and morphs
  918. into an edge). Further down, we'll handle trees that get
  919. duplicated and/or tweaked. */
  920. /* When requested, RETURN_EXPRs should be transformed to just the
  921. contained MODIFY_EXPR. The branch semantics of the return will
  922. be handled elsewhere by manipulating the CFG rather than a statement. */
  923. if (TREE_CODE (*tp) == RETURN_EXPR && id->transform_return_to_modify)
  924. {
  925. tree assignment = TREE_OPERAND (*tp, 0);
  926. /* If we're returning something, just turn that into an
  927. assignment into the equivalent of the original RESULT_DECL.
  928. If the "assignment" is just the result decl, the result
  929. decl has already been set (e.g. a recent "foo (&result_decl,
  930. ...)"); just toss the entire RETURN_EXPR. */
  931. if (assignment && TREE_CODE (assignment) == MODIFY_EXPR)
  932. {
  933. /* Replace the RETURN_EXPR with (a copy of) the
  934. MODIFY_EXPR hanging underneath. */
  935. *tp = copy_node (assignment);
  936. }
  937. else /* Else the RETURN_EXPR returns no value. */
  938. {
  939. *tp = NULL;
  940. return (tree) (void *)1;
  941. }
  942. }
  943. else if (TREE_CODE (*tp) == SSA_NAME)
  944. {
  945. *tp = remap_ssa_name (*tp, id);
  946. *walk_subtrees = 0;
  947. return NULL;
  948. }
  949. /* Local variables and labels need to be replaced by equivalent
  950. variables. We don't want to copy static variables; there's only
  951. one of those, no matter how many times we inline the containing
  952. function. Similarly for globals from an outer function. */
  953. else if (auto_var_in_fn_p (*tp, fn))
  954. {
  955. tree new_decl;
  956. /* Remap the declaration. */
  957. new_decl = remap_decl (*tp, id);
  958. gcc_assert (new_decl);
  959. /* Replace this variable with the copy. */
  960. STRIP_TYPE_NOPS (new_decl);
  961. *tp = new_decl;
  962. *walk_subtrees = 0;
  963. }
  964. else if (TREE_CODE (*tp) == STATEMENT_LIST)
  965. copy_statement_list (tp);
  966. else if (TREE_CODE (*tp) == SAVE_EXPR
  967. || TREE_CODE (*tp) == TARGET_EXPR)
  968. remap_save_expr (tp, id->decl_map, walk_subtrees);
  969. else if (TREE_CODE (*tp) == LABEL_DECL
  970. && (! DECL_CONTEXT (*tp)
  971. || decl_function_context (*tp) == id->src_fn))
  972. /* These may need to be remapped for EH handling. */
  973. *tp = remap_decl (*tp, id);
  974. else if (TREE_CODE (*tp) == BIND_EXPR)
  975. copy_bind_expr (tp, walk_subtrees, id);
  976. /* Types may need remapping as well. */
  977. else if (TYPE_P (*tp))
  978. *tp = remap_type (*tp, id);
  979. /* If this is a constant, we have to copy the node iff the type will be
  980. remapped. copy_tree_r will not copy a constant. */
  981. else if (CONSTANT_CLASS_P (*tp))
  982. {
  983. tree new_type = remap_type (TREE_TYPE (*tp), id);
  984. if (new_type == TREE_TYPE (*tp))
  985. *walk_subtrees = 0;
  986. else if (TREE_CODE (*tp) == INTEGER_CST)
  987. *tp = wide_int_to_tree (new_type, *tp);
  988. else
  989. {
  990. *tp = copy_node (*tp);
  991. TREE_TYPE (*tp) = new_type;
  992. }
  993. }
  994. /* Otherwise, just copy the node. Note that copy_tree_r already
  995. knows not to copy VAR_DECLs, etc., so this is safe. */
  996. else
  997. {
  998. /* Here we handle trees that are not completely rewritten.
  999. First we detect some inlining-induced bogosities for
  1000. discarding. */
  1001. if (TREE_CODE (*tp) == MODIFY_EXPR
  1002. && TREE_OPERAND (*tp, 0) == TREE_OPERAND (*tp, 1)
  1003. && (auto_var_in_fn_p (TREE_OPERAND (*tp, 0), fn)))
  1004. {
  1005. /* Some assignments VAR = VAR; don't generate any rtl code
  1006. and thus don't count as variable modification. Avoid
  1007. keeping bogosities like 0 = 0. */
  1008. tree decl = TREE_OPERAND (*tp, 0), value;
  1009. tree *n;
  1010. n = id->decl_map->get (decl);
  1011. if (n)
  1012. {
  1013. value = *n;
  1014. STRIP_TYPE_NOPS (value);
  1015. if (TREE_CONSTANT (value) || TREE_READONLY (value))
  1016. {
  1017. *tp = build_empty_stmt (EXPR_LOCATION (*tp));
  1018. return copy_tree_body_r (tp, walk_subtrees, data);
  1019. }
  1020. }
  1021. }
  1022. else if (TREE_CODE (*tp) == INDIRECT_REF)
  1023. {
  1024. /* Get rid of *& from inline substitutions that can happen when a
  1025. pointer argument is an ADDR_EXPR. */
  1026. tree decl = TREE_OPERAND (*tp, 0);
  1027. tree *n = id->decl_map->get (decl);
  1028. if (n)
  1029. {
  1030. /* If we happen to get an ADDR_EXPR in n->value, strip
  1031. it manually here as we'll eventually get ADDR_EXPRs
  1032. which lie about their types pointed to. In this case
  1033. build_fold_indirect_ref wouldn't strip the INDIRECT_REF,
  1034. but we absolutely rely on that. As fold_indirect_ref
  1035. does other useful transformations, try that first, though. */
  1036. tree type = TREE_TYPE (*tp);
  1037. tree ptr = id->do_not_unshare ? *n : unshare_expr (*n);
  1038. tree old = *tp;
  1039. *tp = gimple_fold_indirect_ref (ptr);
  1040. if (! *tp)
  1041. {
  1042. if (TREE_CODE (ptr) == ADDR_EXPR)
  1043. {
  1044. *tp
  1045. = fold_indirect_ref_1 (EXPR_LOCATION (ptr), type, ptr);
  1046. /* ??? We should either assert here or build
  1047. a VIEW_CONVERT_EXPR instead of blindly leaking
  1048. incompatible types to our IL. */
  1049. if (! *tp)
  1050. *tp = TREE_OPERAND (ptr, 0);
  1051. }
  1052. else
  1053. {
  1054. *tp = build1 (INDIRECT_REF, type, ptr);
  1055. TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
  1056. TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
  1057. TREE_READONLY (*tp) = TREE_READONLY (old);
  1058. /* We cannot propagate the TREE_THIS_NOTRAP flag if we
  1059. have remapped a parameter as the property might be
  1060. valid only for the parameter itself. */
  1061. if (TREE_THIS_NOTRAP (old)
  1062. && (!is_parm (TREE_OPERAND (old, 0))
  1063. || (!id->transform_parameter && is_parm (ptr))))
  1064. TREE_THIS_NOTRAP (*tp) = 1;
  1065. }
  1066. }
  1067. *walk_subtrees = 0;
  1068. return NULL;
  1069. }
  1070. }
  1071. else if (TREE_CODE (*tp) == MEM_REF)
  1072. {
  1073. /* We need to re-canonicalize MEM_REFs from inline substitutions
  1074. that can happen when a pointer argument is an ADDR_EXPR.
  1075. Recurse here manually to allow that. */
  1076. tree ptr = TREE_OPERAND (*tp, 0);
  1077. tree type = remap_type (TREE_TYPE (*tp), id);
  1078. tree old = *tp;
  1079. walk_tree (&ptr, copy_tree_body_r, data, NULL);
  1080. *tp = fold_build2 (MEM_REF, type, ptr, TREE_OPERAND (*tp, 1));
  1081. TREE_THIS_VOLATILE (*tp) = TREE_THIS_VOLATILE (old);
  1082. TREE_SIDE_EFFECTS (*tp) = TREE_SIDE_EFFECTS (old);
  1083. TREE_NO_WARNING (*tp) = TREE_NO_WARNING (old);
  1084. if (MR_DEPENDENCE_CLIQUE (old) != 0)
  1085. {
  1086. MR_DEPENDENCE_CLIQUE (*tp)
  1087. = remap_dependence_clique (id, MR_DEPENDENCE_CLIQUE (old));
  1088. MR_DEPENDENCE_BASE (*tp) = MR_DEPENDENCE_BASE (old);
  1089. }
  1090. /* We cannot propagate the TREE_THIS_NOTRAP flag if we have
  1091. remapped a parameter as the property might be valid only
  1092. for the parameter itself. */
  1093. if (TREE_THIS_NOTRAP (old)
  1094. && (!is_parm (TREE_OPERAND (old, 0))
  1095. || (!id->transform_parameter && is_parm (ptr))))
  1096. TREE_THIS_NOTRAP (*tp) = 1;
  1097. *walk_subtrees = 0;
  1098. return NULL;
  1099. }
  1100. /* Here is the "usual case". Copy this tree node, and then
  1101. tweak some special cases. */
  1102. copy_tree_r (tp, walk_subtrees, NULL);
  1103. /* If EXPR has block defined, map it to newly constructed block.
  1104. When inlining we want EXPRs without block appear in the block
  1105. of function call if we are not remapping a type. */
  1106. if (EXPR_P (*tp))
  1107. {
  1108. new_block = id->remapping_type_depth == 0 ? id->block : NULL;
  1109. if (TREE_BLOCK (*tp))
  1110. {
  1111. tree *n;
  1112. n = id->decl_map->get (TREE_BLOCK (*tp));
  1113. if (n)
  1114. new_block = *n;
  1115. }
  1116. TREE_SET_BLOCK (*tp, new_block);
  1117. }
  1118. if (TREE_CODE (*tp) != OMP_CLAUSE)
  1119. TREE_TYPE (*tp) = remap_type (TREE_TYPE (*tp), id);
  1120. /* The copied TARGET_EXPR has never been expanded, even if the
  1121. original node was expanded already. */
  1122. if (TREE_CODE (*tp) == TARGET_EXPR && TREE_OPERAND (*tp, 3))
  1123. {
  1124. TREE_OPERAND (*tp, 1) = TREE_OPERAND (*tp, 3);
  1125. TREE_OPERAND (*tp, 3) = NULL_TREE;
  1126. }
  1127. /* Variable substitution need not be simple. In particular, the
  1128. INDIRECT_REF substitution above. Make sure that TREE_CONSTANT
  1129. and friends are up-to-date. */
  1130. else if (TREE_CODE (*tp) == ADDR_EXPR)
  1131. {
  1132. int invariant = is_gimple_min_invariant (*tp);
  1133. walk_tree (&TREE_OPERAND (*tp, 0), copy_tree_body_r, id, NULL);
  1134. /* Handle the case where we substituted an INDIRECT_REF
  1135. into the operand of the ADDR_EXPR. */
  1136. if (TREE_CODE (TREE_OPERAND (*tp, 0)) == INDIRECT_REF)
  1137. *tp = TREE_OPERAND (TREE_OPERAND (*tp, 0), 0);
  1138. else
  1139. recompute_tree_invariant_for_addr_expr (*tp);
  1140. /* If this used to be invariant, but is not any longer,
  1141. then regimplification is probably needed. */
  1142. if (invariant && !is_gimple_min_invariant (*tp))
  1143. id->regimplify = true;
  1144. *walk_subtrees = 0;
  1145. }
  1146. }
  1147. /* Keep iterating. */
  1148. return NULL_TREE;
  1149. }
  1150. /* Helper for remap_gimple_stmt. Given an EH region number for the
  1151. source function, map that to the duplicate EH region number in
  1152. the destination function. */
  1153. static int
  1154. remap_eh_region_nr (int old_nr, copy_body_data *id)
  1155. {
  1156. eh_region old_r, new_r;
  1157. old_r = get_eh_region_from_number_fn (id->src_cfun, old_nr);
  1158. new_r = static_cast<eh_region> (*id->eh_map->get (old_r));
  1159. return new_r->index;
  1160. }
  1161. /* Similar, but operate on INTEGER_CSTs. */
  1162. static tree
  1163. remap_eh_region_tree_nr (tree old_t_nr, copy_body_data *id)
  1164. {
  1165. int old_nr, new_nr;
  1166. old_nr = tree_to_shwi (old_t_nr);
  1167. new_nr = remap_eh_region_nr (old_nr, id);
  1168. return build_int_cst (integer_type_node, new_nr);
  1169. }
  1170. /* Helper for copy_bb. Remap statement STMT using the inlining
  1171. information in ID. Return the new statement copy. */
  1172. static gimple_seq
  1173. remap_gimple_stmt (gimple stmt, copy_body_data *id)
  1174. {
  1175. gimple copy = NULL;
  1176. struct walk_stmt_info wi;
  1177. bool skip_first = false;
  1178. gimple_seq stmts = NULL;
  1179. if (is_gimple_debug (stmt)
  1180. && !opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
  1181. return stmts;
  1182. /* Begin by recognizing trees that we'll completely rewrite for the
  1183. inlining context. Our output for these trees is completely
  1184. different from out input (e.g. RETURN_EXPR is deleted, and morphs
  1185. into an edge). Further down, we'll handle trees that get
  1186. duplicated and/or tweaked. */
  1187. /* When requested, GIMPLE_RETURNs should be transformed to just the
  1188. contained GIMPLE_ASSIGN. The branch semantics of the return will
  1189. be handled elsewhere by manipulating the CFG rather than the
  1190. statement. */
  1191. if (gimple_code (stmt) == GIMPLE_RETURN && id->transform_return_to_modify)
  1192. {
  1193. tree retval = gimple_return_retval (as_a <greturn *> (stmt));
  1194. tree retbnd = gimple_return_retbnd (stmt);
  1195. tree bndslot = id->retbnd;
  1196. if (retbnd && bndslot)
  1197. {
  1198. gimple bndcopy = gimple_build_assign (bndslot, retbnd);
  1199. memset (&wi, 0, sizeof (wi));
  1200. wi.info = id;
  1201. walk_gimple_op (bndcopy, remap_gimple_op_r, &wi);
  1202. gimple_seq_add_stmt (&stmts, bndcopy);
  1203. }
  1204. /* If we're returning something, just turn that into an
  1205. assignment into the equivalent of the original RESULT_DECL.
  1206. If RETVAL is just the result decl, the result decl has
  1207. already been set (e.g. a recent "foo (&result_decl, ...)");
  1208. just toss the entire GIMPLE_RETURN. */
  1209. if (retval
  1210. && (TREE_CODE (retval) != RESULT_DECL
  1211. && (TREE_CODE (retval) != SSA_NAME
  1212. || ! SSA_NAME_VAR (retval)
  1213. || TREE_CODE (SSA_NAME_VAR (retval)) != RESULT_DECL)))
  1214. {
  1215. copy = gimple_build_assign (id->do_not_unshare
  1216. ? id->retvar : unshare_expr (id->retvar),
  1217. retval);
  1218. /* id->retvar is already substituted. Skip it on later remapping. */
  1219. skip_first = true;
  1220. /* We need to copy bounds if return structure with pointers into
  1221. instrumented function. */
  1222. if (chkp_function_instrumented_p (id->dst_fn)
  1223. && !bndslot
  1224. && !BOUNDED_P (id->retvar)
  1225. && chkp_type_has_pointer (TREE_TYPE (id->retvar)))
  1226. id->assign_stmts.safe_push (copy);
  1227. }
  1228. else
  1229. return stmts;
  1230. }
  1231. else if (gimple_has_substatements (stmt))
  1232. {
  1233. gimple_seq s1, s2;
  1234. /* When cloning bodies from the C++ front end, we will be handed bodies
  1235. in High GIMPLE form. Handle here all the High GIMPLE statements that
  1236. have embedded statements. */
  1237. switch (gimple_code (stmt))
  1238. {
  1239. case GIMPLE_BIND:
  1240. copy = copy_gimple_bind (as_a <gbind *> (stmt), id);
  1241. break;
  1242. case GIMPLE_CATCH:
  1243. {
  1244. gcatch *catch_stmt = as_a <gcatch *> (stmt);
  1245. s1 = remap_gimple_seq (gimple_catch_handler (catch_stmt), id);
  1246. copy = gimple_build_catch (gimple_catch_types (catch_stmt), s1);
  1247. }
  1248. break;
  1249. case GIMPLE_EH_FILTER:
  1250. s1 = remap_gimple_seq (gimple_eh_filter_failure (stmt), id);
  1251. copy = gimple_build_eh_filter (gimple_eh_filter_types (stmt), s1);
  1252. break;
  1253. case GIMPLE_TRY:
  1254. s1 = remap_gimple_seq (gimple_try_eval (stmt), id);
  1255. s2 = remap_gimple_seq (gimple_try_cleanup (stmt), id);
  1256. copy = gimple_build_try (s1, s2, gimple_try_kind (stmt));
  1257. break;
  1258. case GIMPLE_WITH_CLEANUP_EXPR:
  1259. s1 = remap_gimple_seq (gimple_wce_cleanup (stmt), id);
  1260. copy = gimple_build_wce (s1);
  1261. break;
  1262. case GIMPLE_OMP_PARALLEL:
  1263. {
  1264. gomp_parallel *omp_par_stmt = as_a <gomp_parallel *> (stmt);
  1265. s1 = remap_gimple_seq (gimple_omp_body (omp_par_stmt), id);
  1266. copy = gimple_build_omp_parallel
  1267. (s1,
  1268. gimple_omp_parallel_clauses (omp_par_stmt),
  1269. gimple_omp_parallel_child_fn (omp_par_stmt),
  1270. gimple_omp_parallel_data_arg (omp_par_stmt));
  1271. }
  1272. break;
  1273. case GIMPLE_OMP_TASK:
  1274. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1275. copy = gimple_build_omp_task
  1276. (s1,
  1277. gimple_omp_task_clauses (stmt),
  1278. gimple_omp_task_child_fn (stmt),
  1279. gimple_omp_task_data_arg (stmt),
  1280. gimple_omp_task_copy_fn (stmt),
  1281. gimple_omp_task_arg_size (stmt),
  1282. gimple_omp_task_arg_align (stmt));
  1283. break;
  1284. case GIMPLE_OMP_FOR:
  1285. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1286. s2 = remap_gimple_seq (gimple_omp_for_pre_body (stmt), id);
  1287. copy = gimple_build_omp_for (s1, gimple_omp_for_kind (stmt),
  1288. gimple_omp_for_clauses (stmt),
  1289. gimple_omp_for_collapse (stmt), s2);
  1290. {
  1291. size_t i;
  1292. for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
  1293. {
  1294. gimple_omp_for_set_index (copy, i,
  1295. gimple_omp_for_index (stmt, i));
  1296. gimple_omp_for_set_initial (copy, i,
  1297. gimple_omp_for_initial (stmt, i));
  1298. gimple_omp_for_set_final (copy, i,
  1299. gimple_omp_for_final (stmt, i));
  1300. gimple_omp_for_set_incr (copy, i,
  1301. gimple_omp_for_incr (stmt, i));
  1302. gimple_omp_for_set_cond (copy, i,
  1303. gimple_omp_for_cond (stmt, i));
  1304. }
  1305. }
  1306. break;
  1307. case GIMPLE_OMP_MASTER:
  1308. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1309. copy = gimple_build_omp_master (s1);
  1310. break;
  1311. case GIMPLE_OMP_TASKGROUP:
  1312. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1313. copy = gimple_build_omp_taskgroup (s1);
  1314. break;
  1315. case GIMPLE_OMP_ORDERED:
  1316. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1317. copy = gimple_build_omp_ordered (s1);
  1318. break;
  1319. case GIMPLE_OMP_SECTION:
  1320. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1321. copy = gimple_build_omp_section (s1);
  1322. break;
  1323. case GIMPLE_OMP_SECTIONS:
  1324. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1325. copy = gimple_build_omp_sections
  1326. (s1, gimple_omp_sections_clauses (stmt));
  1327. break;
  1328. case GIMPLE_OMP_SINGLE:
  1329. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1330. copy = gimple_build_omp_single
  1331. (s1, gimple_omp_single_clauses (stmt));
  1332. break;
  1333. case GIMPLE_OMP_TARGET:
  1334. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1335. copy = gimple_build_omp_target
  1336. (s1, gimple_omp_target_kind (stmt),
  1337. gimple_omp_target_clauses (stmt));
  1338. break;
  1339. case GIMPLE_OMP_TEAMS:
  1340. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1341. copy = gimple_build_omp_teams
  1342. (s1, gimple_omp_teams_clauses (stmt));
  1343. break;
  1344. case GIMPLE_OMP_CRITICAL:
  1345. s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
  1346. copy = gimple_build_omp_critical (s1,
  1347. gimple_omp_critical_name (
  1348. as_a <gomp_critical *> (stmt)));
  1349. break;
  1350. case GIMPLE_TRANSACTION:
  1351. {
  1352. gtransaction *old_trans_stmt = as_a <gtransaction *> (stmt);
  1353. gtransaction *new_trans_stmt;
  1354. s1 = remap_gimple_seq (gimple_transaction_body (old_trans_stmt),
  1355. id);
  1356. copy = new_trans_stmt
  1357. = gimple_build_transaction (
  1358. s1,
  1359. gimple_transaction_label (old_trans_stmt));
  1360. gimple_transaction_set_subcode (
  1361. new_trans_stmt,
  1362. gimple_transaction_subcode (old_trans_stmt));
  1363. }
  1364. break;
  1365. default:
  1366. gcc_unreachable ();
  1367. }
  1368. }
  1369. else
  1370. {
  1371. if (gimple_assign_copy_p (stmt)
  1372. && gimple_assign_lhs (stmt) == gimple_assign_rhs1 (stmt)
  1373. && auto_var_in_fn_p (gimple_assign_lhs (stmt), id->src_fn))
  1374. {
  1375. /* Here we handle statements that are not completely rewritten.
  1376. First we detect some inlining-induced bogosities for
  1377. discarding. */
  1378. /* Some assignments VAR = VAR; don't generate any rtl code
  1379. and thus don't count as variable modification. Avoid
  1380. keeping bogosities like 0 = 0. */
  1381. tree decl = gimple_assign_lhs (stmt), value;
  1382. tree *n;
  1383. n = id->decl_map->get (decl);
  1384. if (n)
  1385. {
  1386. value = *n;
  1387. STRIP_TYPE_NOPS (value);
  1388. if (TREE_CONSTANT (value) || TREE_READONLY (value))
  1389. return NULL;
  1390. }
  1391. }
  1392. /* For *ptr_N ={v} {CLOBBER}, if ptr_N is SSA_NAME defined
  1393. in a block that we aren't copying during tree_function_versioning,
  1394. just drop the clobber stmt. */
  1395. if (id->blocks_to_copy && gimple_clobber_p (stmt))
  1396. {
  1397. tree lhs = gimple_assign_lhs (stmt);
  1398. if (TREE_CODE (lhs) == MEM_REF
  1399. && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
  1400. {
  1401. gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (lhs, 0));
  1402. if (gimple_bb (def_stmt)
  1403. && !bitmap_bit_p (id->blocks_to_copy,
  1404. gimple_bb (def_stmt)->index))
  1405. return NULL;
  1406. }
  1407. }
  1408. if (gimple_debug_bind_p (stmt))
  1409. {
  1410. gdebug *copy
  1411. = gimple_build_debug_bind (gimple_debug_bind_get_var (stmt),
  1412. gimple_debug_bind_get_value (stmt),
  1413. stmt);
  1414. id->debug_stmts.safe_push (copy);
  1415. gimple_seq_add_stmt (&stmts, copy);
  1416. return stmts;
  1417. }
  1418. if (gimple_debug_source_bind_p (stmt))
  1419. {
  1420. gdebug *copy = gimple_build_debug_source_bind
  1421. (gimple_debug_source_bind_get_var (stmt),
  1422. gimple_debug_source_bind_get_value (stmt),
  1423. stmt);
  1424. id->debug_stmts.safe_push (copy);
  1425. gimple_seq_add_stmt (&stmts, copy);
  1426. return stmts;
  1427. }
  1428. /* Create a new deep copy of the statement. */
  1429. copy = gimple_copy (stmt);
  1430. /* Clear flags that need revisiting. */
  1431. if (gcall *call_stmt = dyn_cast <gcall *> (copy))
  1432. {
  1433. if (gimple_call_tail_p (call_stmt))
  1434. gimple_call_set_tail (call_stmt, false);
  1435. if (gimple_call_from_thunk_p (call_stmt))
  1436. gimple_call_set_from_thunk (call_stmt, false);
  1437. }
  1438. /* Remap the region numbers for __builtin_eh_{pointer,filter},
  1439. RESX and EH_DISPATCH. */
  1440. if (id->eh_map)
  1441. switch (gimple_code (copy))
  1442. {
  1443. case GIMPLE_CALL:
  1444. {
  1445. tree r, fndecl = gimple_call_fndecl (copy);
  1446. if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
  1447. switch (DECL_FUNCTION_CODE (fndecl))
  1448. {
  1449. case BUILT_IN_EH_COPY_VALUES:
  1450. r = gimple_call_arg (copy, 1);
  1451. r = remap_eh_region_tree_nr (r, id);
  1452. gimple_call_set_arg (copy, 1, r);
  1453. /* FALLTHRU */
  1454. case BUILT_IN_EH_POINTER:
  1455. case BUILT_IN_EH_FILTER:
  1456. r = gimple_call_arg (copy, 0);
  1457. r = remap_eh_region_tree_nr (r, id);
  1458. gimple_call_set_arg (copy, 0, r);
  1459. break;
  1460. default:
  1461. break;
  1462. }
  1463. /* Reset alias info if we didn't apply measures to
  1464. keep it valid over inlining by setting DECL_PT_UID. */
  1465. if (!id->src_cfun->gimple_df
  1466. || !id->src_cfun->gimple_df->ipa_pta)
  1467. gimple_call_reset_alias_info (as_a <gcall *> (copy));
  1468. }
  1469. break;
  1470. case GIMPLE_RESX:
  1471. {
  1472. gresx *resx_stmt = as_a <gresx *> (copy);
  1473. int r = gimple_resx_region (resx_stmt);
  1474. r = remap_eh_region_nr (r, id);
  1475. gimple_resx_set_region (resx_stmt, r);
  1476. }
  1477. break;
  1478. case GIMPLE_EH_DISPATCH:
  1479. {
  1480. geh_dispatch *eh_dispatch = as_a <geh_dispatch *> (copy);
  1481. int r = gimple_eh_dispatch_region (eh_dispatch);
  1482. r = remap_eh_region_nr (r, id);
  1483. gimple_eh_dispatch_set_region (eh_dispatch, r);
  1484. }
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. }
  1490. /* If STMT has a block defined, map it to the newly constructed
  1491. block. */
  1492. if (gimple_block (copy))
  1493. {
  1494. tree *n;
  1495. n = id->decl_map->get (gimple_block (copy));
  1496. gcc_assert (n);
  1497. gimple_set_block (copy, *n);
  1498. }
  1499. if (gimple_debug_bind_p (copy) || gimple_debug_source_bind_p (copy))
  1500. {
  1501. gimple_seq_add_stmt (&stmts, copy);
  1502. return stmts;
  1503. }
  1504. /* Remap all the operands in COPY. */
  1505. memset (&wi, 0, sizeof (wi));
  1506. wi.info = id;
  1507. if (skip_first)
  1508. walk_tree (gimple_op_ptr (copy, 1), remap_gimple_op_r, &wi, NULL);
  1509. else
  1510. walk_gimple_op (copy, remap_gimple_op_r, &wi);
  1511. /* Clear the copied virtual operands. We are not remapping them here
  1512. but are going to recreate them from scratch. */
  1513. if (gimple_has_mem_ops (copy))
  1514. {
  1515. gimple_set_vdef (copy, NULL_TREE);
  1516. gimple_set_vuse (copy, NULL_TREE);
  1517. }
  1518. gimple_seq_add_stmt (&stmts, copy);
  1519. return stmts;
  1520. }
  1521. /* Copy basic block, scale profile accordingly. Edges will be taken care of
  1522. later */
  1523. static basic_block
  1524. copy_bb (copy_body_data *id, basic_block bb, int frequency_scale,
  1525. gcov_type count_scale)
  1526. {
  1527. gimple_stmt_iterator gsi, copy_gsi, seq_gsi;
  1528. basic_block copy_basic_block;
  1529. tree decl;
  1530. gcov_type freq;
  1531. basic_block prev;
  1532. /* Search for previous copied basic block. */
  1533. prev = bb->prev_bb;
  1534. while (!prev->aux)
  1535. prev = prev->prev_bb;
  1536. /* create_basic_block() will append every new block to
  1537. basic_block_info automatically. */
  1538. copy_basic_block = create_basic_block (NULL, (void *) 0,
  1539. (basic_block) prev->aux);
  1540. copy_basic_block->count = apply_scale (bb->count, count_scale);
  1541. /* We are going to rebuild frequencies from scratch. These values
  1542. have just small importance to drive canonicalize_loop_headers. */
  1543. freq = apply_scale ((gcov_type)bb->frequency, frequency_scale);
  1544. /* We recompute frequencies after inlining, so this is quite safe. */
  1545. if (freq > BB_FREQ_MAX)
  1546. freq = BB_FREQ_MAX;
  1547. copy_basic_block->frequency = freq;
  1548. copy_gsi = gsi_start_bb (copy_basic_block);
  1549. for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
  1550. {
  1551. gimple_seq stmts;
  1552. gimple stmt = gsi_stmt (gsi);
  1553. gimple orig_stmt = stmt;
  1554. gimple_stmt_iterator stmts_gsi;
  1555. bool stmt_added = false;
  1556. id->regimplify = false;
  1557. stmts = remap_gimple_stmt (stmt, id);
  1558. if (gimple_seq_empty_p (stmts))
  1559. continue;
  1560. seq_gsi = copy_gsi;
  1561. for (stmts_gsi = gsi_start (stmts);
  1562. !gsi_end_p (stmts_gsi); )
  1563. {
  1564. stmt = gsi_stmt (stmts_gsi);
  1565. /* Advance iterator now before stmt is moved to seq_gsi. */
  1566. gsi_next (&stmts_gsi);
  1567. if (gimple_nop_p (stmt))
  1568. continue;
  1569. gimple_duplicate_stmt_histograms (cfun, stmt, id->src_cfun,
  1570. orig_stmt);
  1571. /* With return slot optimization we can end up with
  1572. non-gimple (foo *)&this->m, fix that here. */
  1573. if (is_gimple_assign (stmt)
  1574. && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
  1575. && !is_gimple_val (gimple_assign_rhs1 (stmt)))
  1576. {
  1577. tree new_rhs;
  1578. new_rhs = force_gimple_operand_gsi (&seq_gsi,
  1579. gimple_assign_rhs1 (stmt),
  1580. true, NULL, false,
  1581. GSI_CONTINUE_LINKING);
  1582. gimple_assign_set_rhs1 (stmt, new_rhs);
  1583. id->regimplify = false;
  1584. }
  1585. gsi_insert_after (&seq_gsi, stmt, GSI_NEW_STMT);
  1586. if (id->regimplify)
  1587. gimple_regimplify_operands (stmt, &seq_gsi);
  1588. stmt_added = true;
  1589. }
  1590. if (!stmt_added)
  1591. continue;
  1592. /* If copy_basic_block has been empty at the start of this iteration,
  1593. call gsi_start_bb again to get at the newly added statements. */
  1594. if (gsi_end_p (copy_gsi))
  1595. copy_gsi = gsi_start_bb (copy_basic_block);
  1596. else
  1597. gsi_next (&copy_gsi);
  1598. /* Process the new statement. The call to gimple_regimplify_operands
  1599. possibly turned the statement into multiple statements, we
  1600. need to process all of them. */
  1601. do
  1602. {
  1603. tree fn;
  1604. gcall *call_stmt;
  1605. stmt = gsi_stmt (copy_gsi);
  1606. call_stmt = dyn_cast <gcall *> (stmt);
  1607. if (call_stmt
  1608. && gimple_call_va_arg_pack_p (call_stmt)
  1609. && id->call_stmt)
  1610. {
  1611. /* __builtin_va_arg_pack () should be replaced by
  1612. all arguments corresponding to ... in the caller. */
  1613. tree p;
  1614. gcall *new_call;
  1615. vec<tree> argarray;
  1616. size_t nargs = gimple_call_num_args (id->call_stmt);
  1617. size_t n, i, nargs_to_copy;
  1618. bool remove_bounds = false;
  1619. for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
  1620. nargs--;
  1621. /* Bounds should be removed from arg pack in case
  1622. we handle not instrumented call in instrumented
  1623. function. */
  1624. nargs_to_copy = nargs;
  1625. if (gimple_call_with_bounds_p (id->call_stmt)
  1626. && !gimple_call_with_bounds_p (stmt))
  1627. {
  1628. for (i = gimple_call_num_args (id->call_stmt) - nargs;
  1629. i < gimple_call_num_args (id->call_stmt);
  1630. i++)
  1631. if (POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
  1632. nargs_to_copy--;
  1633. remove_bounds = true;
  1634. }
  1635. /* Create the new array of arguments. */
  1636. n = nargs_to_copy + gimple_call_num_args (call_stmt);
  1637. argarray.create (n);
  1638. argarray.safe_grow_cleared (n);
  1639. /* Copy all the arguments before '...' */
  1640. memcpy (argarray.address (),
  1641. gimple_call_arg_ptr (call_stmt, 0),
  1642. gimple_call_num_args (call_stmt) * sizeof (tree));
  1643. if (remove_bounds)
  1644. {
  1645. /* Append the rest of arguments removing bounds. */
  1646. unsigned cur = gimple_call_num_args (call_stmt);
  1647. i = gimple_call_num_args (id->call_stmt) - nargs;
  1648. for (i = gimple_call_num_args (id->call_stmt) - nargs;
  1649. i < gimple_call_num_args (id->call_stmt);
  1650. i++)
  1651. if (!POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
  1652. argarray[cur++] = gimple_call_arg (id->call_stmt, i);
  1653. gcc_assert (cur == n);
  1654. }
  1655. else
  1656. {
  1657. /* Append the arguments passed in '...' */
  1658. memcpy (argarray.address () + gimple_call_num_args (call_stmt),
  1659. gimple_call_arg_ptr (id->call_stmt, 0)
  1660. + (gimple_call_num_args (id->call_stmt) - nargs),
  1661. nargs * sizeof (tree));
  1662. }
  1663. new_call = gimple_build_call_vec (gimple_call_fn (call_stmt),
  1664. argarray);
  1665. argarray.release ();
  1666. /* Copy all GIMPLE_CALL flags, location and block, except
  1667. GF_CALL_VA_ARG_PACK. */
  1668. gimple_call_copy_flags (new_call, call_stmt);
  1669. gimple_call_set_va_arg_pack (new_call, false);
  1670. gimple_set_location (new_call, gimple_location (stmt));
  1671. gimple_set_block (new_call, gimple_block (stmt));
  1672. gimple_call_set_lhs (new_call, gimple_call_lhs (call_stmt));
  1673. gsi_replace (&copy_gsi, new_call, false);
  1674. stmt = new_call;
  1675. }
  1676. else if (call_stmt
  1677. && id->call_stmt
  1678. && (decl = gimple_call_fndecl (stmt))
  1679. && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
  1680. && DECL_FUNCTION_CODE (decl) == BUILT_IN_VA_ARG_PACK_LEN)
  1681. {
  1682. /* __builtin_va_arg_pack_len () should be replaced by
  1683. the number of anonymous arguments. */
  1684. size_t nargs = gimple_call_num_args (id->call_stmt), i;
  1685. tree count, p;
  1686. gimple new_stmt;
  1687. for (p = DECL_ARGUMENTS (id->src_fn); p; p = DECL_CHAIN (p))
  1688. nargs--;
  1689. /* For instrumented calls we should ignore bounds. */
  1690. for (i = gimple_call_num_args (id->call_stmt) - nargs;
  1691. i < gimple_call_num_args (id->call_stmt);
  1692. i++)
  1693. if (POINTER_BOUNDS_P (gimple_call_arg (id->call_stmt, i)))
  1694. nargs--;
  1695. count = build_int_cst (integer_type_node, nargs);
  1696. new_stmt = gimple_build_assign (gimple_call_lhs (stmt), count);
  1697. gsi_replace (&copy_gsi, new_stmt, false);
  1698. stmt = new_stmt;
  1699. }
  1700. else if (call_stmt
  1701. && id->call_stmt
  1702. && gimple_call_internal_p (stmt)
  1703. && gimple_call_internal_fn (stmt) == IFN_TSAN_FUNC_EXIT)
  1704. {
  1705. /* Drop TSAN_FUNC_EXIT () internal calls during inlining. */
  1706. gsi_remove (&copy_gsi, false);
  1707. continue;
  1708. }
  1709. /* Statements produced by inlining can be unfolded, especially
  1710. when we constant propagated some operands. We can't fold
  1711. them right now for two reasons:
  1712. 1) folding require SSA_NAME_DEF_STMTs to be correct
  1713. 2) we can't change function calls to builtins.
  1714. So we just mark statement for later folding. We mark
  1715. all new statements, instead just statements that has changed
  1716. by some nontrivial substitution so even statements made
  1717. foldable indirectly are updated. If this turns out to be
  1718. expensive, copy_body can be told to watch for nontrivial
  1719. changes. */
  1720. if (id->statements_to_fold)
  1721. id->statements_to_fold->add (stmt);
  1722. /* We're duplicating a CALL_EXPR. Find any corresponding
  1723. callgraph edges and update or duplicate them. */
  1724. if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
  1725. {
  1726. struct cgraph_edge *edge;
  1727. switch (id->transform_call_graph_edges)
  1728. {
  1729. case CB_CGE_DUPLICATE:
  1730. edge = id->src_node->get_edge (orig_stmt);
  1731. if (edge)
  1732. {
  1733. int edge_freq = edge->frequency;
  1734. int new_freq;
  1735. struct cgraph_edge *old_edge = edge;
  1736. edge = edge->clone (id->dst_node, call_stmt,
  1737. gimple_uid (stmt),
  1738. REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
  1739. true);
  1740. /* We could also just rescale the frequency, but
  1741. doing so would introduce roundoff errors and make
  1742. verifier unhappy. */
  1743. new_freq = compute_call_stmt_bb_frequency (id->dst_node->decl,
  1744. copy_basic_block);
  1745. /* Speculative calls consist of two edges - direct and indirect.
  1746. Duplicate the whole thing and distribute frequencies accordingly. */
  1747. if (edge->speculative)
  1748. {
  1749. struct cgraph_edge *direct, *indirect;
  1750. struct ipa_ref *ref;
  1751. gcc_assert (!edge->indirect_unknown_callee);
  1752. old_edge->speculative_call_info (direct, indirect, ref);
  1753. indirect = indirect->clone (id->dst_node, call_stmt,
  1754. gimple_uid (stmt),
  1755. REG_BR_PROB_BASE, CGRAPH_FREQ_BASE,
  1756. true);
  1757. if (old_edge->frequency + indirect->frequency)
  1758. {
  1759. edge->frequency = MIN (RDIV ((gcov_type)new_freq * old_edge->frequency,
  1760. (old_edge->frequency + indirect->frequency)),
  1761. CGRAPH_FREQ_MAX);
  1762. indirect->frequency = MIN (RDIV ((gcov_type)new_freq * indirect->frequency,
  1763. (old_edge->frequency + indirect->frequency)),
  1764. CGRAPH_FREQ_MAX);
  1765. }
  1766. id->dst_node->clone_reference (ref, stmt);
  1767. }
  1768. else
  1769. {
  1770. edge->frequency = new_freq;
  1771. if (dump_file
  1772. && profile_status_for_fn (cfun) != PROFILE_ABSENT
  1773. && (edge_freq > edge->frequency + 10
  1774. || edge_freq < edge->frequency - 10))
  1775. {
  1776. fprintf (dump_file, "Edge frequency estimated by "
  1777. "cgraph %i diverge from inliner's estimate %i\n",
  1778. edge_freq,
  1779. edge->frequency);
  1780. fprintf (dump_file,
  1781. "Orig bb: %i, orig bb freq %i, new bb freq %i\n",
  1782. bb->index,
  1783. bb->frequency,
  1784. copy_basic_block->frequency);
  1785. }
  1786. }
  1787. }
  1788. break;
  1789. case CB_CGE_MOVE_CLONES:
  1790. id->dst_node->set_call_stmt_including_clones (orig_stmt,
  1791. call_stmt);
  1792. edge = id->dst_node->get_edge (stmt);
  1793. break;
  1794. case CB_CGE_MOVE:
  1795. edge = id->dst_node->get_edge (orig_stmt);
  1796. if (edge)
  1797. edge->set_call_stmt (call_stmt);
  1798. break;
  1799. default:
  1800. gcc_unreachable ();
  1801. }
  1802. /* Constant propagation on argument done during inlining
  1803. may create new direct call. Produce an edge for it. */
  1804. if ((!edge
  1805. || (edge->indirect_inlining_edge
  1806. && id->transform_call_graph_edges == CB_CGE_MOVE_CLONES))
  1807. && id->dst_node->definition
  1808. && (fn = gimple_call_fndecl (stmt)) != NULL)
  1809. {
  1810. struct cgraph_node *dest = cgraph_node::get (fn);
  1811. /* We have missing edge in the callgraph. This can happen
  1812. when previous inlining turned an indirect call into a
  1813. direct call by constant propagating arguments or we are
  1814. producing dead clone (for further cloning). In all
  1815. other cases we hit a bug (incorrect node sharing is the
  1816. most common reason for missing edges). */
  1817. gcc_assert (!dest->definition
  1818. || dest->address_taken
  1819. || !id->src_node->definition
  1820. || !id->dst_node->definition);
  1821. if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES)
  1822. id->dst_node->create_edge_including_clones
  1823. (dest, orig_stmt, call_stmt, bb->count,
  1824. compute_call_stmt_bb_frequency (id->dst_node->decl,
  1825. copy_basic_block),
  1826. CIF_ORIGINALLY_INDIRECT_CALL);
  1827. else
  1828. id->dst_node->create_edge (dest, call_stmt,
  1829. bb->count,
  1830. compute_call_stmt_bb_frequency
  1831. (id->dst_node->decl,
  1832. copy_basic_block))->inline_failed
  1833. = CIF_ORIGINALLY_INDIRECT_CALL;
  1834. if (dump_file)
  1835. {
  1836. fprintf (dump_file, "Created new direct edge to %s\n",
  1837. dest->name ());
  1838. }
  1839. }
  1840. notice_special_calls (as_a <gcall *> (stmt));
  1841. }
  1842. maybe_duplicate_eh_stmt_fn (cfun, stmt, id->src_cfun, orig_stmt,
  1843. id->eh_map, id->eh_lp_nr);
  1844. if (gimple_in_ssa_p (cfun) && !is_gimple_debug (stmt))
  1845. {
  1846. ssa_op_iter i;
  1847. tree def;
  1848. FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_DEF)
  1849. if (TREE_CODE (def) == SSA_NAME)
  1850. SSA_NAME_DEF_STMT (def) = stmt;
  1851. }
  1852. gsi_next (&copy_gsi);
  1853. }
  1854. while (!gsi_end_p (copy_gsi));
  1855. copy_gsi = gsi_last_bb (copy_basic_block);
  1856. }
  1857. return copy_basic_block;
  1858. }
  1859. /* Inserting Single Entry Multiple Exit region in SSA form into code in SSA
  1860. form is quite easy, since dominator relationship for old basic blocks does
  1861. not change.
  1862. There is however exception where inlining might change dominator relation
  1863. across EH edges from basic block within inlined functions destinating
  1864. to landing pads in function we inline into.
  1865. The function fills in PHI_RESULTs of such PHI nodes if they refer
  1866. to gimple regs. Otherwise, the function mark PHI_RESULT of such
  1867. PHI nodes for renaming. For non-gimple regs, renaming is safe: the
  1868. EH edges are abnormal and SSA_NAME_OCCURS_IN_ABNORMAL_PHI must be
  1869. set, and this means that there will be no overlapping live ranges
  1870. for the underlying symbol.
  1871. This might change in future if we allow redirecting of EH edges and
  1872. we might want to change way build CFG pre-inlining to include
  1873. all the possible edges then. */
  1874. static void
  1875. update_ssa_across_abnormal_edges (basic_block bb, basic_block ret_bb,
  1876. bool can_throw, bool nonlocal_goto)
  1877. {
  1878. edge e;
  1879. edge_iterator ei;
  1880. FOR_EACH_EDGE (e, ei, bb->succs)
  1881. if (!e->dest->aux
  1882. || ((basic_block)e->dest->aux)->index == ENTRY_BLOCK)
  1883. {
  1884. gphi *phi;
  1885. gphi_iterator si;
  1886. if (!nonlocal_goto)
  1887. gcc_assert (e->flags & EDGE_EH);
  1888. if (!can_throw)
  1889. gcc_assert (!(e->flags & EDGE_EH));
  1890. for (si = gsi_start_phis (e->dest); !gsi_end_p (si); gsi_next (&si))
  1891. {
  1892. edge re;
  1893. phi = si.phi ();
  1894. /* For abnormal goto/call edges the receiver can be the
  1895. ENTRY_BLOCK. Do not assert this cannot happen. */
  1896. gcc_assert ((e->flags & EDGE_EH)
  1897. || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)));
  1898. re = find_edge (ret_bb, e->dest);
  1899. gcc_checking_assert (re);
  1900. gcc_assert ((re->flags & (EDGE_EH | EDGE_ABNORMAL))
  1901. == (e->flags & (EDGE_EH | EDGE_ABNORMAL)));
  1902. SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, e),
  1903. USE_FROM_PTR (PHI_ARG_DEF_PTR_FROM_EDGE (phi, re)));
  1904. }
  1905. }
  1906. }
  1907. /* Copy edges from BB into its copy constructed earlier, scale profile
  1908. accordingly. Edges will be taken care of later. Assume aux
  1909. pointers to point to the copies of each BB. Return true if any
  1910. debug stmts are left after a statement that must end the basic block. */
  1911. static bool
  1912. copy_edges_for_bb (basic_block bb, gcov_type count_scale, basic_block ret_bb,
  1913. basic_block abnormal_goto_dest)
  1914. {
  1915. basic_block new_bb = (basic_block) bb->aux;
  1916. edge_iterator ei;
  1917. edge old_edge;
  1918. gimple_stmt_iterator si;
  1919. int flags;
  1920. bool need_debug_cleanup = false;
  1921. /* Use the indices from the original blocks to create edges for the
  1922. new ones. */
  1923. FOR_EACH_EDGE (old_edge, ei, bb->succs)
  1924. if (!(old_edge->flags & EDGE_EH))
  1925. {
  1926. edge new_edge;
  1927. flags = old_edge->flags;
  1928. /* Return edges do get a FALLTHRU flag when the get inlined. */
  1929. if (old_edge->dest->index == EXIT_BLOCK
  1930. && !(old_edge->flags & (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE|EDGE_FAKE))
  1931. && old_edge->dest->aux != EXIT_BLOCK_PTR_FOR_FN (cfun))
  1932. flags |= EDGE_FALLTHRU;
  1933. new_edge = make_edge (new_bb, (basic_block) old_edge->dest->aux, flags);
  1934. new_edge->count = apply_scale (old_edge->count, count_scale);
  1935. new_edge->probability = old_edge->probability;
  1936. }
  1937. if (bb->index == ENTRY_BLOCK || bb->index == EXIT_BLOCK)
  1938. return false;
  1939. for (si = gsi_start_bb (new_bb); !gsi_end_p (si);)
  1940. {
  1941. gimple copy_stmt;
  1942. bool can_throw, nonlocal_goto;
  1943. copy_stmt = gsi_stmt (si);
  1944. if (!is_gimple_debug (copy_stmt))
  1945. update_stmt (copy_stmt);
  1946. /* Do this before the possible split_block. */
  1947. gsi_next (&si);
  1948. /* If this tree could throw an exception, there are two
  1949. cases where we need to add abnormal edge(s): the
  1950. tree wasn't in a region and there is a "current
  1951. region" in the caller; or the original tree had
  1952. EH edges. In both cases split the block after the tree,
  1953. and add abnormal edge(s) as needed; we need both
  1954. those from the callee and the caller.
  1955. We check whether the copy can throw, because the const
  1956. propagation can change an INDIRECT_REF which throws
  1957. into a COMPONENT_REF which doesn't. If the copy
  1958. can throw, the original could also throw. */
  1959. can_throw = stmt_can_throw_internal (copy_stmt);
  1960. nonlocal_goto
  1961. = (stmt_can_make_abnormal_goto (copy_stmt)
  1962. && !computed_goto_p (copy_stmt));
  1963. if (can_throw || nonlocal_goto)
  1964. {
  1965. if (!gsi_end_p (si))
  1966. {
  1967. while (!gsi_end_p (si) && is_gimple_debug (gsi_stmt (si)))
  1968. gsi_next (&si);
  1969. if (gsi_end_p (si))
  1970. need_debug_cleanup = true;
  1971. }
  1972. if (!gsi_end_p (si))
  1973. /* Note that bb's predecessor edges aren't necessarily
  1974. right at this point; split_block doesn't care. */
  1975. {
  1976. edge e = split_block (new_bb, copy_stmt);
  1977. new_bb = e->dest;
  1978. new_bb->aux = e->src->aux;
  1979. si = gsi_start_bb (new_bb);
  1980. }
  1981. }
  1982. if (gimple_code (copy_stmt) == GIMPLE_EH_DISPATCH)
  1983. make_eh_dispatch_edges (as_a <geh_dispatch *> (copy_stmt));
  1984. else if (can_throw)
  1985. make_eh_edges (copy_stmt);
  1986. /* If the call we inline cannot make abnormal goto do not add
  1987. additional abnormal edges but only retain those already present
  1988. in the original function body. */
  1989. if (abnormal_goto_dest == NULL)
  1990. nonlocal_goto = false;
  1991. if (nonlocal_goto)
  1992. {
  1993. basic_block copy_stmt_bb = gimple_bb (copy_stmt);
  1994. if (get_abnormal_succ_dispatcher (copy_stmt_bb))
  1995. nonlocal_goto = false;
  1996. /* ABNORMAL_DISPATCHER (1) is for longjmp/setjmp or nonlocal gotos
  1997. in OpenMP regions which aren't allowed to be left abnormally.
  1998. So, no need to add abnormal edge in that case. */
  1999. else if (is_gimple_call (copy_stmt)
  2000. && gimple_call_internal_p (copy_stmt)
  2001. && (gimple_call_internal_fn (copy_stmt)
  2002. == IFN_ABNORMAL_DISPATCHER)
  2003. && gimple_call_arg (copy_stmt, 0) == boolean_true_node)
  2004. nonlocal_goto = false;
  2005. else
  2006. make_edge (copy_stmt_bb, abnormal_goto_dest, EDGE_ABNORMAL);
  2007. }
  2008. if ((can_throw || nonlocal_goto)
  2009. && gimple_in_ssa_p (cfun))
  2010. update_ssa_across_abnormal_edges (gimple_bb (copy_stmt), ret_bb,
  2011. can_throw, nonlocal_goto);
  2012. }
  2013. return need_debug_cleanup;
  2014. }
  2015. /* Copy the PHIs. All blocks and edges are copied, some blocks
  2016. was possibly split and new outgoing EH edges inserted.
  2017. BB points to the block of original function and AUX pointers links
  2018. the original and newly copied blocks. */
  2019. static void
  2020. copy_phis_for_bb (basic_block bb, copy_body_data *id)
  2021. {
  2022. basic_block const new_bb = (basic_block) bb->aux;
  2023. edge_iterator ei;
  2024. gphi *phi;
  2025. gphi_iterator si;
  2026. edge new_edge;
  2027. bool inserted = false;
  2028. for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
  2029. {
  2030. tree res, new_res;
  2031. gphi *new_phi;
  2032. phi = si.phi ();
  2033. res = PHI_RESULT (phi);
  2034. new_res = res;
  2035. if (!virtual_operand_p (res))
  2036. {
  2037. walk_tree (&new_res, copy_tree_body_r, id, NULL);
  2038. new_phi = create_phi_node (new_res, new_bb);
  2039. FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
  2040. {
  2041. edge old_edge = find_edge ((basic_block) new_edge->src->aux, bb);
  2042. tree arg;
  2043. tree new_arg;
  2044. edge_iterator ei2;
  2045. location_t locus;
  2046. /* When doing partial cloning, we allow PHIs on the entry block
  2047. as long as all the arguments are the same. Find any input
  2048. edge to see argument to copy. */
  2049. if (!old_edge)
  2050. FOR_EACH_EDGE (old_edge, ei2, bb->preds)
  2051. if (!old_edge->src->aux)
  2052. break;
  2053. arg = PHI_ARG_DEF_FROM_EDGE (phi, old_edge);
  2054. new_arg = arg;
  2055. walk_tree (&new_arg, copy_tree_body_r, id, NULL);
  2056. gcc_assert (new_arg);
  2057. /* With return slot optimization we can end up with
  2058. non-gimple (foo *)&this->m, fix that here. */
  2059. if (TREE_CODE (new_arg) != SSA_NAME
  2060. && TREE_CODE (new_arg) != FUNCTION_DECL
  2061. && !is_gimple_val (new_arg))
  2062. {
  2063. gimple_seq stmts = NULL;
  2064. new_arg = force_gimple_operand (new_arg, &stmts, true, NULL);
  2065. gsi_insert_seq_on_edge (new_edge, stmts);
  2066. inserted = true;
  2067. }
  2068. locus = gimple_phi_arg_location_from_edge (phi, old_edge);
  2069. if (LOCATION_BLOCK (locus))
  2070. {
  2071. tree *n;
  2072. n = id->decl_map->get (LOCATION_BLOCK (locus));
  2073. gcc_assert (n);
  2074. if (*n)
  2075. locus = COMBINE_LOCATION_DATA (line_table, locus, *n);
  2076. else
  2077. locus = LOCATION_LOCUS (locus);
  2078. }
  2079. else
  2080. locus = LOCATION_LOCUS (locus);
  2081. add_phi_arg (new_phi, new_arg, new_edge, locus);
  2082. }
  2083. }
  2084. }
  2085. /* Commit the delayed edge insertions. */
  2086. if (inserted)
  2087. FOR_EACH_EDGE (new_edge, ei, new_bb->preds)
  2088. gsi_commit_one_edge_insert (new_edge, NULL);
  2089. }
  2090. /* Wrapper for remap_decl so it can be used as a callback. */
  2091. static tree
  2092. remap_decl_1 (tree decl, void *data)
  2093. {
  2094. return remap_decl (decl, (copy_body_data *) data);
  2095. }
  2096. /* Build struct function and associated datastructures for the new clone
  2097. NEW_FNDECL to be build. CALLEE_FNDECL is the original. Function changes
  2098. the cfun to the function of new_fndecl (and current_function_decl too). */
  2099. static void
  2100. initialize_cfun (tree new_fndecl, tree callee_fndecl, gcov_type count)
  2101. {
  2102. struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
  2103. gcov_type count_scale;
  2104. if (!DECL_ARGUMENTS (new_fndecl))
  2105. DECL_ARGUMENTS (new_fndecl) = DECL_ARGUMENTS (callee_fndecl);
  2106. if (!DECL_RESULT (new_fndecl))
  2107. DECL_RESULT (new_fndecl) = DECL_RESULT (callee_fndecl);
  2108. if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
  2109. count_scale
  2110. = GCOV_COMPUTE_SCALE (count,
  2111. ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
  2112. else
  2113. count_scale = REG_BR_PROB_BASE;
  2114. /* Register specific tree functions. */
  2115. gimple_register_cfg_hooks ();
  2116. /* Get clean struct function. */
  2117. push_struct_function (new_fndecl);
  2118. /* We will rebuild these, so just sanity check that they are empty. */
  2119. gcc_assert (VALUE_HISTOGRAMS (cfun) == NULL);
  2120. gcc_assert (cfun->local_decls == NULL);
  2121. gcc_assert (cfun->cfg == NULL);
  2122. gcc_assert (cfun->decl == new_fndecl);
  2123. /* Copy items we preserve during cloning. */
  2124. cfun->static_chain_decl = src_cfun->static_chain_decl;
  2125. cfun->nonlocal_goto_save_area = src_cfun->nonlocal_goto_save_area;
  2126. cfun->function_end_locus = src_cfun->function_end_locus;
  2127. cfun->curr_properties = src_cfun->curr_properties;
  2128. cfun->last_verified = src_cfun->last_verified;
  2129. cfun->va_list_gpr_size = src_cfun->va_list_gpr_size;
  2130. cfun->va_list_fpr_size = src_cfun->va_list_fpr_size;
  2131. cfun->has_nonlocal_label = src_cfun->has_nonlocal_label;
  2132. cfun->stdarg = src_cfun->stdarg;
  2133. cfun->after_inlining = src_cfun->after_inlining;
  2134. cfun->can_throw_non_call_exceptions
  2135. = src_cfun->can_throw_non_call_exceptions;
  2136. cfun->can_delete_dead_exceptions = src_cfun->can_delete_dead_exceptions;
  2137. cfun->returns_struct = src_cfun->returns_struct;
  2138. cfun->returns_pcc_struct = src_cfun->returns_pcc_struct;
  2139. init_empty_tree_cfg ();
  2140. profile_status_for_fn (cfun) = profile_status_for_fn (src_cfun);
  2141. ENTRY_BLOCK_PTR_FOR_FN (cfun)->count =
  2142. (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
  2143. REG_BR_PROB_BASE);
  2144. ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency
  2145. = ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
  2146. EXIT_BLOCK_PTR_FOR_FN (cfun)->count =
  2147. (EXIT_BLOCK_PTR_FOR_FN (src_cfun)->count * count_scale /
  2148. REG_BR_PROB_BASE);
  2149. EXIT_BLOCK_PTR_FOR_FN (cfun)->frequency =
  2150. EXIT_BLOCK_PTR_FOR_FN (src_cfun)->frequency;
  2151. if (src_cfun->eh)
  2152. init_eh_for_function ();
  2153. if (src_cfun->gimple_df)
  2154. {
  2155. init_tree_ssa (cfun);
  2156. cfun->gimple_df->in_ssa_p = true;
  2157. init_ssa_operands (cfun);
  2158. }
  2159. }
  2160. /* Helper function for copy_cfg_body. Move debug stmts from the end
  2161. of NEW_BB to the beginning of successor basic blocks when needed. If the
  2162. successor has multiple predecessors, reset them, otherwise keep
  2163. their value. */
  2164. static void
  2165. maybe_move_debug_stmts_to_successors (copy_body_data *id, basic_block new_bb)
  2166. {
  2167. edge e;
  2168. edge_iterator ei;
  2169. gimple_stmt_iterator si = gsi_last_nondebug_bb (new_bb);
  2170. if (gsi_end_p (si)
  2171. || gsi_one_before_end_p (si)
  2172. || !(stmt_can_throw_internal (gsi_stmt (si))
  2173. || stmt_can_make_abnormal_goto (gsi_stmt (si))))
  2174. return;
  2175. FOR_EACH_EDGE (e, ei, new_bb->succs)
  2176. {
  2177. gimple_stmt_iterator ssi = gsi_last_bb (new_bb);
  2178. gimple_stmt_iterator dsi = gsi_after_labels (e->dest);
  2179. while (is_gimple_debug (gsi_stmt (ssi)))
  2180. {
  2181. gimple stmt = gsi_stmt (ssi);
  2182. gdebug *new_stmt;
  2183. tree var;
  2184. tree value;
  2185. /* For the last edge move the debug stmts instead of copying
  2186. them. */
  2187. if (ei_one_before_end_p (ei))
  2188. {
  2189. si = ssi;
  2190. gsi_prev (&ssi);
  2191. if (!single_pred_p (e->dest) && gimple_debug_bind_p (stmt))
  2192. gimple_debug_bind_reset_value (stmt);
  2193. gsi_remove (&si, false);
  2194. gsi_insert_before (&dsi, stmt, GSI_SAME_STMT);
  2195. continue;
  2196. }
  2197. if (gimple_debug_bind_p (stmt))
  2198. {
  2199. var = gimple_debug_bind_get_var (stmt);
  2200. if (single_pred_p (e->dest))
  2201. {
  2202. value = gimple_debug_bind_get_value (stmt);
  2203. value = unshare_expr (value);
  2204. }
  2205. else
  2206. value = NULL_TREE;
  2207. new_stmt = gimple_build_debug_bind (var, value, stmt);
  2208. }
  2209. else if (gimple_debug_source_bind_p (stmt))
  2210. {
  2211. var = gimple_debug_source_bind_get_var (stmt);
  2212. value = gimple_debug_source_bind_get_value (stmt);
  2213. new_stmt = gimple_build_debug_source_bind (var, value, stmt);
  2214. }
  2215. else
  2216. gcc_unreachable ();
  2217. gsi_insert_before (&dsi, new_stmt, GSI_SAME_STMT);
  2218. id->debug_stmts.safe_push (new_stmt);
  2219. gsi_prev (&ssi);
  2220. }
  2221. }
  2222. }
  2223. /* Make a copy of the sub-loops of SRC_PARENT and place them
  2224. as siblings of DEST_PARENT. */
  2225. static void
  2226. copy_loops (copy_body_data *id,
  2227. struct loop *dest_parent, struct loop *src_parent)
  2228. {
  2229. struct loop *src_loop = src_parent->inner;
  2230. while (src_loop)
  2231. {
  2232. if (!id->blocks_to_copy
  2233. || bitmap_bit_p (id->blocks_to_copy, src_loop->header->index))
  2234. {
  2235. struct loop *dest_loop = alloc_loop ();
  2236. /* Assign the new loop its header and latch and associate
  2237. those with the new loop. */
  2238. dest_loop->header = (basic_block)src_loop->header->aux;
  2239. dest_loop->header->loop_father = dest_loop;
  2240. if (src_loop->latch != NULL)
  2241. {
  2242. dest_loop->latch = (basic_block)src_loop->latch->aux;
  2243. dest_loop->latch->loop_father = dest_loop;
  2244. }
  2245. /* Copy loop meta-data. */
  2246. copy_loop_info (src_loop, dest_loop);
  2247. /* Finally place it into the loop array and the loop tree. */
  2248. place_new_loop (cfun, dest_loop);
  2249. flow_loop_tree_node_add (dest_parent, dest_loop);
  2250. dest_loop->safelen = src_loop->safelen;
  2251. dest_loop->dont_vectorize = src_loop->dont_vectorize;
  2252. if (src_loop->force_vectorize)
  2253. {
  2254. dest_loop->force_vectorize = true;
  2255. cfun->has_force_vectorize_loops = true;
  2256. }
  2257. if (src_loop->simduid)
  2258. {
  2259. dest_loop->simduid = remap_decl (src_loop->simduid, id);
  2260. cfun->has_simduid_loops = true;
  2261. }
  2262. /* Recurse. */
  2263. copy_loops (id, dest_loop, src_loop);
  2264. }
  2265. src_loop = src_loop->next;
  2266. }
  2267. }
  2268. /* Call cgraph_redirect_edge_call_stmt_to_callee on all calls in BB */
  2269. void
  2270. redirect_all_calls (copy_body_data * id, basic_block bb)
  2271. {
  2272. gimple_stmt_iterator si;
  2273. gimple last = last_stmt (bb);
  2274. for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
  2275. {
  2276. gimple stmt = gsi_stmt (si);
  2277. if (is_gimple_call (stmt))
  2278. {
  2279. struct cgraph_edge *edge = id->dst_node->get_edge (stmt);
  2280. if (edge)
  2281. {
  2282. edge->redirect_call_stmt_to_callee ();
  2283. if (stmt == last && id->call_stmt && maybe_clean_eh_stmt (stmt))
  2284. gimple_purge_dead_eh_edges (bb);
  2285. }
  2286. }
  2287. }
  2288. }
  2289. /* Convert estimated frequencies into counts for NODE, scaling COUNT
  2290. with each bb's frequency. Used when NODE has a 0-weight entry
  2291. but we are about to inline it into a non-zero count call bb.
  2292. See the comments for handle_missing_profiles() in predict.c for
  2293. when this can happen for COMDATs. */
  2294. void
  2295. freqs_to_counts (struct cgraph_node *node, gcov_type count)
  2296. {
  2297. basic_block bb;
  2298. edge_iterator ei;
  2299. edge e;
  2300. struct function *fn = DECL_STRUCT_FUNCTION (node->decl);
  2301. FOR_ALL_BB_FN(bb, fn)
  2302. {
  2303. bb->count = apply_scale (count,
  2304. GCOV_COMPUTE_SCALE (bb->frequency, BB_FREQ_MAX));
  2305. FOR_EACH_EDGE (e, ei, bb->succs)
  2306. e->count = apply_probability (e->src->count, e->probability);
  2307. }
  2308. }
  2309. /* Make a copy of the body of FN so that it can be inserted inline in
  2310. another function. Walks FN via CFG, returns new fndecl. */
  2311. static tree
  2312. copy_cfg_body (copy_body_data * id, gcov_type count, int frequency_scale,
  2313. basic_block entry_block_map, basic_block exit_block_map,
  2314. basic_block new_entry)
  2315. {
  2316. tree callee_fndecl = id->src_fn;
  2317. /* Original cfun for the callee, doesn't change. */
  2318. struct function *src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
  2319. struct function *cfun_to_copy;
  2320. basic_block bb;
  2321. tree new_fndecl = NULL;
  2322. bool need_debug_cleanup = false;
  2323. gcov_type count_scale;
  2324. int last;
  2325. int incoming_frequency = 0;
  2326. gcov_type incoming_count = 0;
  2327. /* This can happen for COMDAT routines that end up with 0 counts
  2328. despite being called (see the comments for handle_missing_profiles()
  2329. in predict.c as to why). Apply counts to the blocks in the callee
  2330. before inlining, using the guessed edge frequencies, so that we don't
  2331. end up with a 0-count inline body which can confuse downstream
  2332. optimizations such as function splitting. */
  2333. if (!ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count && count)
  2334. {
  2335. /* Apply the larger of the call bb count and the total incoming
  2336. call edge count to the callee. */
  2337. gcov_type in_count = 0;
  2338. struct cgraph_edge *in_edge;
  2339. for (in_edge = id->src_node->callers; in_edge;
  2340. in_edge = in_edge->next_caller)
  2341. in_count += in_edge->count;
  2342. freqs_to_counts (id->src_node, count > in_count ? count : in_count);
  2343. }
  2344. if (ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count)
  2345. count_scale
  2346. = GCOV_COMPUTE_SCALE (count,
  2347. ENTRY_BLOCK_PTR_FOR_FN (src_cfun)->count);
  2348. else
  2349. count_scale = REG_BR_PROB_BASE;
  2350. /* Register specific tree functions. */
  2351. gimple_register_cfg_hooks ();
  2352. /* If we are inlining just region of the function, make sure to connect
  2353. new entry to ENTRY_BLOCK_PTR_FOR_FN (cfun). Since new entry can be
  2354. part of loop, we must compute frequency and probability of
  2355. ENTRY_BLOCK_PTR_FOR_FN (cfun) based on the frequencies and
  2356. probabilities of edges incoming from nonduplicated region. */
  2357. if (new_entry)
  2358. {
  2359. edge e;
  2360. edge_iterator ei;
  2361. FOR_EACH_EDGE (e, ei, new_entry->preds)
  2362. if (!e->src->aux)
  2363. {
  2364. incoming_frequency += EDGE_FREQUENCY (e);
  2365. incoming_count += e->count;
  2366. }
  2367. incoming_count = apply_scale (incoming_count, count_scale);
  2368. incoming_frequency
  2369. = apply_scale ((gcov_type)incoming_frequency, frequency_scale);
  2370. ENTRY_BLOCK_PTR_FOR_FN (cfun)->count = incoming_count;
  2371. ENTRY_BLOCK_PTR_FOR_FN (cfun)->frequency = incoming_frequency;
  2372. }
  2373. /* Must have a CFG here at this point. */
  2374. gcc_assert (ENTRY_BLOCK_PTR_FOR_FN
  2375. (DECL_STRUCT_FUNCTION (callee_fndecl)));
  2376. cfun_to_copy = id->src_cfun = DECL_STRUCT_FUNCTION (callee_fndecl);
  2377. ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = entry_block_map;
  2378. EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy)->aux = exit_block_map;
  2379. entry_block_map->aux = ENTRY_BLOCK_PTR_FOR_FN (cfun_to_copy);
  2380. exit_block_map->aux = EXIT_BLOCK_PTR_FOR_FN (cfun_to_copy);
  2381. /* Duplicate any exception-handling regions. */
  2382. if (cfun->eh)
  2383. id->eh_map = duplicate_eh_regions (cfun_to_copy, NULL, id->eh_lp_nr,
  2384. remap_decl_1, id);
  2385. /* Use aux pointers to map the original blocks to copy. */
  2386. FOR_EACH_BB_FN (bb, cfun_to_copy)
  2387. if (!id->blocks_to_copy || bitmap_bit_p (id->blocks_to_copy, bb->index))
  2388. {
  2389. basic_block new_bb = copy_bb (id, bb, frequency_scale, count_scale);
  2390. bb->aux = new_bb;
  2391. new_bb->aux = bb;
  2392. new_bb->loop_father = entry_block_map->loop_father;
  2393. }
  2394. last = last_basic_block_for_fn (cfun);
  2395. /* Now that we've duplicated the blocks, duplicate their edges. */
  2396. basic_block abnormal_goto_dest = NULL;
  2397. if (id->call_stmt
  2398. && stmt_can_make_abnormal_goto (id->call_stmt))
  2399. {
  2400. gimple_stmt_iterator gsi = gsi_for_stmt (id->call_stmt);
  2401. bb = gimple_bb (id->call_stmt);
  2402. gsi_next (&gsi);
  2403. if (gsi_end_p (gsi))
  2404. abnormal_goto_dest = get_abnormal_succ_dispatcher (bb);
  2405. }
  2406. FOR_ALL_BB_FN (bb, cfun_to_copy)
  2407. if (!id->blocks_to_copy
  2408. || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
  2409. need_debug_cleanup |= copy_edges_for_bb (bb, count_scale, exit_block_map,
  2410. abnormal_goto_dest);
  2411. if (new_entry)
  2412. {
  2413. edge e = make_edge (entry_block_map, (basic_block)new_entry->aux, EDGE_FALLTHRU);
  2414. e->probability = REG_BR_PROB_BASE;
  2415. e->count = incoming_count;
  2416. }
  2417. /* Duplicate the loop tree, if available and wanted. */
  2418. if (loops_for_fn (src_cfun) != NULL
  2419. && current_loops != NULL)
  2420. {
  2421. copy_loops (id, entry_block_map->loop_father,
  2422. get_loop (src_cfun, 0));
  2423. /* Defer to cfgcleanup to update loop-father fields of basic-blocks. */
  2424. loops_state_set (LOOPS_NEED_FIXUP);
  2425. }
  2426. /* If the loop tree in the source function needed fixup, mark the
  2427. destination loop tree for fixup, too. */
  2428. if (loops_for_fn (src_cfun)->state & LOOPS_NEED_FIXUP)
  2429. loops_state_set (LOOPS_NEED_FIXUP);
  2430. if (gimple_in_ssa_p (cfun))
  2431. FOR_ALL_BB_FN (bb, cfun_to_copy)
  2432. if (!id->blocks_to_copy
  2433. || (bb->index > 0 && bitmap_bit_p (id->blocks_to_copy, bb->index)))
  2434. copy_phis_for_bb (bb, id);
  2435. FOR_ALL_BB_FN (bb, cfun_to_copy)
  2436. if (bb->aux)
  2437. {
  2438. if (need_debug_cleanup
  2439. && bb->index != ENTRY_BLOCK
  2440. && bb->index != EXIT_BLOCK)
  2441. maybe_move_debug_stmts_to_successors (id, (basic_block) bb->aux);
  2442. /* Update call edge destinations. This can not be done before loop
  2443. info is updated, because we may split basic blocks. */
  2444. if (id->transform_call_graph_edges == CB_CGE_DUPLICATE
  2445. && bb->index != ENTRY_BLOCK
  2446. && bb->index != EXIT_BLOCK)
  2447. redirect_all_calls (id, (basic_block)bb->aux);
  2448. ((basic_block)bb->aux)->aux = NULL;
  2449. bb->aux = NULL;
  2450. }
  2451. /* Zero out AUX fields of newly created block during EH edge
  2452. insertion. */
  2453. for (; last < last_basic_block_for_fn (cfun); last++)
  2454. {
  2455. if (need_debug_cleanup)
  2456. maybe_move_debug_stmts_to_successors (id,
  2457. BASIC_BLOCK_FOR_FN (cfun, last));
  2458. BASIC_BLOCK_FOR_FN (cfun, last)->aux = NULL;
  2459. /* Update call edge destinations. This can not be done before loop
  2460. info is updated, because we may split basic blocks. */
  2461. if (id->transform_call_graph_edges == CB_CGE_DUPLICATE)
  2462. redirect_all_calls (id, BASIC_BLOCK_FOR_FN (cfun, last));
  2463. }
  2464. entry_block_map->aux = NULL;
  2465. exit_block_map->aux = NULL;
  2466. if (id->eh_map)
  2467. {
  2468. delete id->eh_map;
  2469. id->eh_map = NULL;
  2470. }
  2471. if (id->dependence_map)
  2472. {
  2473. delete id->dependence_map;
  2474. id->dependence_map = NULL;
  2475. }
  2476. return new_fndecl;
  2477. }
  2478. /* Copy the debug STMT using ID. We deal with these statements in a
  2479. special way: if any variable in their VALUE expression wasn't
  2480. remapped yet, we won't remap it, because that would get decl uids
  2481. out of sync, causing codegen differences between -g and -g0. If
  2482. this arises, we drop the VALUE expression altogether. */
  2483. static void
  2484. copy_debug_stmt (gdebug *stmt, copy_body_data *id)
  2485. {
  2486. tree t, *n;
  2487. struct walk_stmt_info wi;
  2488. if (gimple_block (stmt))
  2489. {
  2490. n = id->decl_map->get (gimple_block (stmt));
  2491. gimple_set_block (stmt, n ? *n : id->block);
  2492. }
  2493. /* Remap all the operands in COPY. */
  2494. memset (&wi, 0, sizeof (wi));
  2495. wi.info = id;
  2496. processing_debug_stmt = 1;
  2497. if (gimple_debug_source_bind_p (stmt))
  2498. t = gimple_debug_source_bind_get_var (stmt);
  2499. else
  2500. t = gimple_debug_bind_get_var (stmt);
  2501. if (TREE_CODE (t) == PARM_DECL && id->debug_map
  2502. && (n = id->debug_map->get (t)))
  2503. {
  2504. gcc_assert (TREE_CODE (*n) == VAR_DECL);
  2505. t = *n;
  2506. }
  2507. else if (TREE_CODE (t) == VAR_DECL
  2508. && !is_global_var (t)
  2509. && !id->decl_map->get (t))
  2510. /* T is a non-localized variable. */;
  2511. else
  2512. walk_tree (&t, remap_gimple_op_r, &wi, NULL);
  2513. if (gimple_debug_bind_p (stmt))
  2514. {
  2515. gimple_debug_bind_set_var (stmt, t);
  2516. if (gimple_debug_bind_has_value_p (stmt))
  2517. walk_tree (gimple_debug_bind_get_value_ptr (stmt),
  2518. remap_gimple_op_r, &wi, NULL);
  2519. /* Punt if any decl couldn't be remapped. */
  2520. if (processing_debug_stmt < 0)
  2521. gimple_debug_bind_reset_value (stmt);
  2522. }
  2523. else if (gimple_debug_source_bind_p (stmt))
  2524. {
  2525. gimple_debug_source_bind_set_var (stmt, t);
  2526. walk_tree (gimple_debug_source_bind_get_value_ptr (stmt),
  2527. remap_gimple_op_r, &wi, NULL);
  2528. /* When inlining and source bind refers to one of the optimized
  2529. away parameters, change the source bind into normal debug bind
  2530. referring to the corresponding DEBUG_EXPR_DECL that should have
  2531. been bound before the call stmt. */
  2532. t = gimple_debug_source_bind_get_value (stmt);
  2533. if (t != NULL_TREE
  2534. && TREE_CODE (t) == PARM_DECL
  2535. && id->call_stmt)
  2536. {
  2537. vec<tree, va_gc> **debug_args = decl_debug_args_lookup (id->src_fn);
  2538. unsigned int i;
  2539. if (debug_args != NULL)
  2540. {
  2541. for (i = 0; i < vec_safe_length (*debug_args); i += 2)
  2542. if ((**debug_args)[i] == DECL_ORIGIN (t)
  2543. && TREE_CODE ((**debug_args)[i + 1]) == DEBUG_EXPR_DECL)
  2544. {
  2545. t = (**debug_args)[i + 1];
  2546. stmt->subcode = GIMPLE_DEBUG_BIND;
  2547. gimple_debug_bind_set_value (stmt, t);
  2548. break;
  2549. }
  2550. }
  2551. }
  2552. }
  2553. processing_debug_stmt = 0;
  2554. update_stmt (stmt);
  2555. }
  2556. /* Process deferred debug stmts. In order to give values better odds
  2557. of being successfully remapped, we delay the processing of debug
  2558. stmts until all other stmts that might require remapping are
  2559. processed. */
  2560. static void
  2561. copy_debug_stmts (copy_body_data *id)
  2562. {
  2563. size_t i;
  2564. gdebug *stmt;
  2565. if (!id->debug_stmts.exists ())
  2566. return;
  2567. FOR_EACH_VEC_ELT (id->debug_stmts, i, stmt)
  2568. copy_debug_stmt (stmt, id);
  2569. id->debug_stmts.release ();
  2570. }
  2571. /* Make a copy of the body of SRC_FN so that it can be inserted inline in
  2572. another function. */
  2573. static tree
  2574. copy_tree_body (copy_body_data *id)
  2575. {
  2576. tree fndecl = id->src_fn;
  2577. tree body = DECL_SAVED_TREE (fndecl);
  2578. walk_tree (&body, copy_tree_body_r, id, NULL);
  2579. return body;
  2580. }
  2581. /* Make a copy of the body of FN so that it can be inserted inline in
  2582. another function. */
  2583. static tree
  2584. copy_body (copy_body_data *id, gcov_type count, int frequency_scale,
  2585. basic_block entry_block_map, basic_block exit_block_map,
  2586. basic_block new_entry)
  2587. {
  2588. tree fndecl = id->src_fn;
  2589. tree body;
  2590. /* If this body has a CFG, walk CFG and copy. */
  2591. gcc_assert (ENTRY_BLOCK_PTR_FOR_FN (DECL_STRUCT_FUNCTION (fndecl)));
  2592. body = copy_cfg_body (id, count, frequency_scale, entry_block_map, exit_block_map,
  2593. new_entry);
  2594. copy_debug_stmts (id);
  2595. return body;
  2596. }
  2597. /* Return true if VALUE is an ADDR_EXPR of an automatic variable
  2598. defined in function FN, or of a data member thereof. */
  2599. static bool
  2600. self_inlining_addr_expr (tree value, tree fn)
  2601. {
  2602. tree var;
  2603. if (TREE_CODE (value) != ADDR_EXPR)
  2604. return false;
  2605. var = get_base_address (TREE_OPERAND (value, 0));
  2606. return var && auto_var_in_fn_p (var, fn);
  2607. }
  2608. /* Append to BB a debug annotation that binds VAR to VALUE, inheriting
  2609. lexical block and line number information from base_stmt, if given,
  2610. or from the last stmt of the block otherwise. */
  2611. static gimple
  2612. insert_init_debug_bind (copy_body_data *id,
  2613. basic_block bb, tree var, tree value,
  2614. gimple base_stmt)
  2615. {
  2616. gimple note;
  2617. gimple_stmt_iterator gsi;
  2618. tree tracked_var;
  2619. if (!gimple_in_ssa_p (id->src_cfun))
  2620. return NULL;
  2621. if (!opt_for_fn (id->dst_fn, flag_var_tracking_assignments))
  2622. return NULL;
  2623. tracked_var = target_for_debug_bind (var);
  2624. if (!tracked_var)
  2625. return NULL;
  2626. if (bb)
  2627. {
  2628. gsi = gsi_last_bb (bb);
  2629. if (!base_stmt && !gsi_end_p (gsi))
  2630. base_stmt = gsi_stmt (gsi);
  2631. }
  2632. note = gimple_build_debug_bind (tracked_var, unshare_expr (value), base_stmt);
  2633. if (bb)
  2634. {
  2635. if (!gsi_end_p (gsi))
  2636. gsi_insert_after (&gsi, note, GSI_SAME_STMT);
  2637. else
  2638. gsi_insert_before (&gsi, note, GSI_SAME_STMT);
  2639. }
  2640. return note;
  2641. }
  2642. static void
  2643. insert_init_stmt (copy_body_data *id, basic_block bb, gimple init_stmt)
  2644. {
  2645. /* If VAR represents a zero-sized variable, it's possible that the
  2646. assignment statement may result in no gimple statements. */
  2647. if (init_stmt)
  2648. {
  2649. gimple_stmt_iterator si = gsi_last_bb (bb);
  2650. /* We can end up with init statements that store to a non-register
  2651. from a rhs with a conversion. Handle that here by forcing the
  2652. rhs into a temporary. gimple_regimplify_operands is not
  2653. prepared to do this for us. */
  2654. if (!is_gimple_debug (init_stmt)
  2655. && !is_gimple_reg (gimple_assign_lhs (init_stmt))
  2656. && is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (init_stmt)))
  2657. && gimple_assign_rhs_class (init_stmt) == GIMPLE_UNARY_RHS)
  2658. {
  2659. tree rhs = build1 (gimple_assign_rhs_code (init_stmt),
  2660. gimple_expr_type (init_stmt),
  2661. gimple_assign_rhs1 (init_stmt));
  2662. rhs = force_gimple_operand_gsi (&si, rhs, true, NULL_TREE, false,
  2663. GSI_NEW_STMT);
  2664. gimple_assign_set_rhs_code (init_stmt, TREE_CODE (rhs));
  2665. gimple_assign_set_rhs1 (init_stmt, rhs);
  2666. }
  2667. gsi_insert_after (&si, init_stmt, GSI_NEW_STMT);
  2668. gimple_regimplify_operands (init_stmt, &si);
  2669. if (!is_gimple_debug (init_stmt))
  2670. {
  2671. tree def = gimple_assign_lhs (init_stmt);
  2672. insert_init_debug_bind (id, bb, def, def, init_stmt);
  2673. }
  2674. }
  2675. }
  2676. /* Initialize parameter P with VALUE. If needed, produce init statement
  2677. at the end of BB. When BB is NULL, we return init statement to be
  2678. output later. */
  2679. static gimple
  2680. setup_one_parameter (copy_body_data *id, tree p, tree value, tree fn,
  2681. basic_block bb, tree *vars)
  2682. {
  2683. gimple init_stmt = NULL;
  2684. tree var;
  2685. tree rhs = value;
  2686. tree def = (gimple_in_ssa_p (cfun)
  2687. ? ssa_default_def (id->src_cfun, p) : NULL);
  2688. if (value
  2689. && value != error_mark_node
  2690. && !useless_type_conversion_p (TREE_TYPE (p), TREE_TYPE (value)))
  2691. {
  2692. /* If we can match up types by promotion/demotion do so. */
  2693. if (fold_convertible_p (TREE_TYPE (p), value))
  2694. rhs = fold_convert (TREE_TYPE (p), value);
  2695. else
  2696. {
  2697. /* ??? For valid programs we should not end up here.
  2698. Still if we end up with truly mismatched types here, fall back
  2699. to using a VIEW_CONVERT_EXPR or a literal zero to not leak invalid
  2700. GIMPLE to the following passes. */
  2701. if (!is_gimple_reg_type (TREE_TYPE (value))
  2702. || TYPE_SIZE (TREE_TYPE (p)) == TYPE_SIZE (TREE_TYPE (value)))
  2703. rhs = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (p), value);
  2704. else
  2705. rhs = build_zero_cst (TREE_TYPE (p));
  2706. }
  2707. }
  2708. /* Make an equivalent VAR_DECL. Note that we must NOT remap the type
  2709. here since the type of this decl must be visible to the calling
  2710. function. */
  2711. var = copy_decl_to_var (p, id);
  2712. /* Declare this new variable. */
  2713. DECL_CHAIN (var) = *vars;
  2714. *vars = var;
  2715. /* Make gimplifier happy about this variable. */
  2716. DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
  2717. /* If the parameter is never assigned to, has no SSA_NAMEs created,
  2718. we would not need to create a new variable here at all, if it
  2719. weren't for debug info. Still, we can just use the argument
  2720. value. */
  2721. if (TREE_READONLY (p)
  2722. && !TREE_ADDRESSABLE (p)
  2723. && value && !TREE_SIDE_EFFECTS (value)
  2724. && !def)
  2725. {
  2726. /* We may produce non-gimple trees by adding NOPs or introduce
  2727. invalid sharing when operand is not really constant.
  2728. It is not big deal to prohibit constant propagation here as
  2729. we will constant propagate in DOM1 pass anyway. */
  2730. if (is_gimple_min_invariant (value)
  2731. && useless_type_conversion_p (TREE_TYPE (p),
  2732. TREE_TYPE (value))
  2733. /* We have to be very careful about ADDR_EXPR. Make sure
  2734. the base variable isn't a local variable of the inlined
  2735. function, e.g., when doing recursive inlining, direct or
  2736. mutually-recursive or whatever, which is why we don't
  2737. just test whether fn == current_function_decl. */
  2738. && ! self_inlining_addr_expr (value, fn))
  2739. {
  2740. insert_decl_map (id, p, value);
  2741. insert_debug_decl_map (id, p, var);
  2742. return insert_init_debug_bind (id, bb, var, value, NULL);
  2743. }
  2744. }
  2745. /* Register the VAR_DECL as the equivalent for the PARM_DECL;
  2746. that way, when the PARM_DECL is encountered, it will be
  2747. automatically replaced by the VAR_DECL. */
  2748. insert_decl_map (id, p, var);
  2749. /* Even if P was TREE_READONLY, the new VAR should not be.
  2750. In the original code, we would have constructed a
  2751. temporary, and then the function body would have never
  2752. changed the value of P. However, now, we will be
  2753. constructing VAR directly. The constructor body may
  2754. change its value multiple times as it is being
  2755. constructed. Therefore, it must not be TREE_READONLY;
  2756. the back-end assumes that TREE_READONLY variable is
  2757. assigned to only once. */
  2758. if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (p)))
  2759. TREE_READONLY (var) = 0;
  2760. /* If there is no setup required and we are in SSA, take the easy route
  2761. replacing all SSA names representing the function parameter by the
  2762. SSA name passed to function.
  2763. We need to construct map for the variable anyway as it might be used
  2764. in different SSA names when parameter is set in function.
  2765. Do replacement at -O0 for const arguments replaced by constant.
  2766. This is important for builtin_constant_p and other construct requiring
  2767. constant argument to be visible in inlined function body. */
  2768. if (gimple_in_ssa_p (cfun) && rhs && def && is_gimple_reg (p)
  2769. && (optimize
  2770. || (TREE_READONLY (p)
  2771. && is_gimple_min_invariant (rhs)))
  2772. && (TREE_CODE (rhs) == SSA_NAME
  2773. || is_gimple_min_invariant (rhs))
  2774. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def))
  2775. {
  2776. insert_decl_map (id, def, rhs);
  2777. return insert_init_debug_bind (id, bb, var, rhs, NULL);
  2778. }
  2779. /* If the value of argument is never used, don't care about initializing
  2780. it. */
  2781. if (optimize && gimple_in_ssa_p (cfun) && !def && is_gimple_reg (p))
  2782. {
  2783. gcc_assert (!value || !TREE_SIDE_EFFECTS (value));
  2784. return insert_init_debug_bind (id, bb, var, rhs, NULL);
  2785. }
  2786. /* Initialize this VAR_DECL from the equivalent argument. Convert
  2787. the argument to the proper type in case it was promoted. */
  2788. if (value)
  2789. {
  2790. if (rhs == error_mark_node)
  2791. {
  2792. insert_decl_map (id, p, var);
  2793. return insert_init_debug_bind (id, bb, var, rhs, NULL);
  2794. }
  2795. STRIP_USELESS_TYPE_CONVERSION (rhs);
  2796. /* If we are in SSA form properly remap the default definition
  2797. or assign to a dummy SSA name if the parameter is unused and
  2798. we are not optimizing. */
  2799. if (gimple_in_ssa_p (cfun) && is_gimple_reg (p))
  2800. {
  2801. if (def)
  2802. {
  2803. def = remap_ssa_name (def, id);
  2804. init_stmt = gimple_build_assign (def, rhs);
  2805. SSA_NAME_IS_DEFAULT_DEF (def) = 0;
  2806. set_ssa_default_def (cfun, var, NULL);
  2807. }
  2808. else if (!optimize)
  2809. {
  2810. def = make_ssa_name (var);
  2811. init_stmt = gimple_build_assign (def, rhs);
  2812. }
  2813. }
  2814. else
  2815. init_stmt = gimple_build_assign (var, rhs);
  2816. if (bb && init_stmt)
  2817. insert_init_stmt (id, bb, init_stmt);
  2818. }
  2819. return init_stmt;
  2820. }
  2821. /* Generate code to initialize the parameters of the function at the
  2822. top of the stack in ID from the GIMPLE_CALL STMT. */
  2823. static void
  2824. initialize_inlined_parameters (copy_body_data *id, gimple stmt,
  2825. tree fn, basic_block bb)
  2826. {
  2827. tree parms;
  2828. size_t i;
  2829. tree p;
  2830. tree vars = NULL_TREE;
  2831. tree static_chain = gimple_call_chain (stmt);
  2832. /* Figure out what the parameters are. */
  2833. parms = DECL_ARGUMENTS (fn);
  2834. /* Loop through the parameter declarations, replacing each with an
  2835. equivalent VAR_DECL, appropriately initialized. */
  2836. for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
  2837. {
  2838. tree val;
  2839. val = i < gimple_call_num_args (stmt) ? gimple_call_arg (stmt, i) : NULL;
  2840. setup_one_parameter (id, p, val, fn, bb, &vars);
  2841. }
  2842. /* After remapping parameters remap their types. This has to be done
  2843. in a second loop over all parameters to appropriately remap
  2844. variable sized arrays when the size is specified in a
  2845. parameter following the array. */
  2846. for (p = parms, i = 0; p; p = DECL_CHAIN (p), i++)
  2847. {
  2848. tree *varp = id->decl_map->get (p);
  2849. if (varp
  2850. && TREE_CODE (*varp) == VAR_DECL)
  2851. {
  2852. tree def = (gimple_in_ssa_p (cfun) && is_gimple_reg (p)
  2853. ? ssa_default_def (id->src_cfun, p) : NULL);
  2854. tree var = *varp;
  2855. TREE_TYPE (var) = remap_type (TREE_TYPE (var), id);
  2856. /* Also remap the default definition if it was remapped
  2857. to the default definition of the parameter replacement
  2858. by the parameter setup. */
  2859. if (def)
  2860. {
  2861. tree *defp = id->decl_map->get (def);
  2862. if (defp
  2863. && TREE_CODE (*defp) == SSA_NAME
  2864. && SSA_NAME_VAR (*defp) == var)
  2865. TREE_TYPE (*defp) = TREE_TYPE (var);
  2866. }
  2867. }
  2868. }
  2869. /* Initialize the static chain. */
  2870. p = DECL_STRUCT_FUNCTION (fn)->static_chain_decl;
  2871. gcc_assert (fn != current_function_decl);
  2872. if (p)
  2873. {
  2874. /* No static chain? Seems like a bug in tree-nested.c. */
  2875. gcc_assert (static_chain);
  2876. setup_one_parameter (id, p, static_chain, fn, bb, &vars);
  2877. }
  2878. declare_inline_vars (id->block, vars);
  2879. }
  2880. /* Declare a return variable to replace the RESULT_DECL for the
  2881. function we are calling. An appropriate DECL_STMT is returned.
  2882. The USE_STMT is filled to contain a use of the declaration to
  2883. indicate the return value of the function.
  2884. RETURN_SLOT, if non-null is place where to store the result. It
  2885. is set only for CALL_EXPR_RETURN_SLOT_OPT. MODIFY_DEST, if non-null,
  2886. was the LHS of the MODIFY_EXPR to which this call is the RHS.
  2887. RETURN_BOUNDS holds a destination for returned bounds.
  2888. The return value is a (possibly null) value that holds the result
  2889. as seen by the caller. */
  2890. static tree
  2891. declare_return_variable (copy_body_data *id, tree return_slot, tree modify_dest,
  2892. tree return_bounds, basic_block entry_bb)
  2893. {
  2894. tree callee = id->src_fn;
  2895. tree result = DECL_RESULT (callee);
  2896. tree callee_type = TREE_TYPE (result);
  2897. tree caller_type;
  2898. tree var, use;
  2899. /* Handle type-mismatches in the function declaration return type
  2900. vs. the call expression. */
  2901. if (modify_dest)
  2902. caller_type = TREE_TYPE (modify_dest);
  2903. else
  2904. caller_type = TREE_TYPE (TREE_TYPE (callee));
  2905. /* We don't need to do anything for functions that don't return anything. */
  2906. if (VOID_TYPE_P (callee_type))
  2907. return NULL_TREE;
  2908. /* If there was a return slot, then the return value is the
  2909. dereferenced address of that object. */
  2910. if (return_slot)
  2911. {
  2912. /* The front end shouldn't have used both return_slot and
  2913. a modify expression. */
  2914. gcc_assert (!modify_dest);
  2915. if (DECL_BY_REFERENCE (result))
  2916. {
  2917. tree return_slot_addr = build_fold_addr_expr (return_slot);
  2918. STRIP_USELESS_TYPE_CONVERSION (return_slot_addr);
  2919. /* We are going to construct *&return_slot and we can't do that
  2920. for variables believed to be not addressable.
  2921. FIXME: This check possibly can match, because values returned
  2922. via return slot optimization are not believed to have address
  2923. taken by alias analysis. */
  2924. gcc_assert (TREE_CODE (return_slot) != SSA_NAME);
  2925. var = return_slot_addr;
  2926. }
  2927. else
  2928. {
  2929. var = return_slot;
  2930. gcc_assert (TREE_CODE (var) != SSA_NAME);
  2931. if (TREE_ADDRESSABLE (result))
  2932. mark_addressable (var);
  2933. }
  2934. if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
  2935. || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
  2936. && !DECL_GIMPLE_REG_P (result)
  2937. && DECL_P (var))
  2938. DECL_GIMPLE_REG_P (var) = 0;
  2939. use = NULL;
  2940. goto done;
  2941. }
  2942. /* All types requiring non-trivial constructors should have been handled. */
  2943. gcc_assert (!TREE_ADDRESSABLE (callee_type));
  2944. /* Attempt to avoid creating a new temporary variable. */
  2945. if (modify_dest
  2946. && TREE_CODE (modify_dest) != SSA_NAME)
  2947. {
  2948. bool use_it = false;
  2949. /* We can't use MODIFY_DEST if there's type promotion involved. */
  2950. if (!useless_type_conversion_p (callee_type, caller_type))
  2951. use_it = false;
  2952. /* ??? If we're assigning to a variable sized type, then we must
  2953. reuse the destination variable, because we've no good way to
  2954. create variable sized temporaries at this point. */
  2955. else if (TREE_CODE (TYPE_SIZE_UNIT (caller_type)) != INTEGER_CST)
  2956. use_it = true;
  2957. /* If the callee cannot possibly modify MODIFY_DEST, then we can
  2958. reuse it as the result of the call directly. Don't do this if
  2959. it would promote MODIFY_DEST to addressable. */
  2960. else if (TREE_ADDRESSABLE (result))
  2961. use_it = false;
  2962. else
  2963. {
  2964. tree base_m = get_base_address (modify_dest);
  2965. /* If the base isn't a decl, then it's a pointer, and we don't
  2966. know where that's going to go. */
  2967. if (!DECL_P (base_m))
  2968. use_it = false;
  2969. else if (is_global_var (base_m))
  2970. use_it = false;
  2971. else if ((TREE_CODE (TREE_TYPE (result)) == COMPLEX_TYPE
  2972. || TREE_CODE (TREE_TYPE (result)) == VECTOR_TYPE)
  2973. && !DECL_GIMPLE_REG_P (result)
  2974. && DECL_GIMPLE_REG_P (base_m))
  2975. use_it = false;
  2976. else if (!TREE_ADDRESSABLE (base_m))
  2977. use_it = true;
  2978. }
  2979. if (use_it)
  2980. {
  2981. var = modify_dest;
  2982. use = NULL;
  2983. goto done;
  2984. }
  2985. }
  2986. gcc_assert (TREE_CODE (TYPE_SIZE_UNIT (callee_type)) == INTEGER_CST);
  2987. var = copy_result_decl_to_var (result, id);
  2988. DECL_SEEN_IN_BIND_EXPR_P (var) = 1;
  2989. /* Do not have the rest of GCC warn about this variable as it should
  2990. not be visible to the user. */
  2991. TREE_NO_WARNING (var) = 1;
  2992. declare_inline_vars (id->block, var);
  2993. /* Build the use expr. If the return type of the function was
  2994. promoted, convert it back to the expected type. */
  2995. use = var;
  2996. if (!useless_type_conversion_p (caller_type, TREE_TYPE (var)))
  2997. {
  2998. /* If we can match up types by promotion/demotion do so. */
  2999. if (fold_convertible_p (caller_type, var))
  3000. use = fold_convert (caller_type, var);
  3001. else
  3002. {
  3003. /* ??? For valid programs we should not end up here.
  3004. Still if we end up with truly mismatched types here, fall back
  3005. to using a MEM_REF to not leak invalid GIMPLE to the following
  3006. passes. */
  3007. /* Prevent var from being written into SSA form. */
  3008. if (TREE_CODE (TREE_TYPE (var)) == VECTOR_TYPE
  3009. || TREE_CODE (TREE_TYPE (var)) == COMPLEX_TYPE)
  3010. DECL_GIMPLE_REG_P (var) = false;
  3011. else if (is_gimple_reg_type (TREE_TYPE (var)))
  3012. TREE_ADDRESSABLE (var) = true;
  3013. use = fold_build2 (MEM_REF, caller_type,
  3014. build_fold_addr_expr (var),
  3015. build_int_cst (ptr_type_node, 0));
  3016. }
  3017. }
  3018. STRIP_USELESS_TYPE_CONVERSION (use);
  3019. if (DECL_BY_REFERENCE (result))
  3020. {
  3021. TREE_ADDRESSABLE (var) = 1;
  3022. var = build_fold_addr_expr (var);
  3023. }
  3024. done:
  3025. /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
  3026. way, when the RESULT_DECL is encountered, it will be
  3027. automatically replaced by the VAR_DECL.
  3028. When returning by reference, ensure that RESULT_DECL remaps to
  3029. gimple_val. */
  3030. if (DECL_BY_REFERENCE (result)
  3031. && !is_gimple_val (var))
  3032. {
  3033. tree temp = create_tmp_var (TREE_TYPE (result), "retvalptr");
  3034. insert_decl_map (id, result, temp);
  3035. /* When RESULT_DECL is in SSA form, we need to remap and initialize
  3036. it's default_def SSA_NAME. */
  3037. if (gimple_in_ssa_p (id->src_cfun)
  3038. && is_gimple_reg (result))
  3039. {
  3040. temp = make_ssa_name (temp);
  3041. insert_decl_map (id, ssa_default_def (id->src_cfun, result), temp);
  3042. }
  3043. insert_init_stmt (id, entry_bb, gimple_build_assign (temp, var));
  3044. }
  3045. else
  3046. insert_decl_map (id, result, var);
  3047. /* Remember this so we can ignore it in remap_decls. */
  3048. id->retvar = var;
  3049. /* If returned bounds are used, then make var for them. */
  3050. if (return_bounds)
  3051. {
  3052. tree bndtemp = create_tmp_var (pointer_bounds_type_node, "retbnd");
  3053. DECL_SEEN_IN_BIND_EXPR_P (bndtemp) = 1;
  3054. TREE_NO_WARNING (bndtemp) = 1;
  3055. declare_inline_vars (id->block, bndtemp);
  3056. id->retbnd = bndtemp;
  3057. insert_init_stmt (id, entry_bb,
  3058. gimple_build_assign (bndtemp, chkp_get_zero_bounds_var ()));
  3059. }
  3060. return use;
  3061. }
  3062. /* Callback through walk_tree. Determine if a DECL_INITIAL makes reference
  3063. to a local label. */
  3064. static tree
  3065. has_label_address_in_static_1 (tree *nodep, int *walk_subtrees, void *fnp)
  3066. {
  3067. tree node = *nodep;
  3068. tree fn = (tree) fnp;
  3069. if (TREE_CODE (node) == LABEL_DECL && DECL_CONTEXT (node) == fn)
  3070. return node;
  3071. if (TYPE_P (node))
  3072. *walk_subtrees = 0;
  3073. return NULL_TREE;
  3074. }
  3075. /* Determine if the function can be copied. If so return NULL. If
  3076. not return a string describng the reason for failure. */
  3077. const char *
  3078. copy_forbidden (struct function *fun, tree fndecl)
  3079. {
  3080. const char *reason = fun->cannot_be_copied_reason;
  3081. tree decl;
  3082. unsigned ix;
  3083. /* Only examine the function once. */
  3084. if (fun->cannot_be_copied_set)
  3085. return reason;
  3086. /* We cannot copy a function that receives a non-local goto
  3087. because we cannot remap the destination label used in the
  3088. function that is performing the non-local goto. */
  3089. /* ??? Actually, this should be possible, if we work at it.
  3090. No doubt there's just a handful of places that simply
  3091. assume it doesn't happen and don't substitute properly. */
  3092. if (fun->has_nonlocal_label)
  3093. {
  3094. reason = G_("function %q+F can never be copied "
  3095. "because it receives a non-local goto");
  3096. goto fail;
  3097. }
  3098. FOR_EACH_LOCAL_DECL (fun, ix, decl)
  3099. if (TREE_CODE (decl) == VAR_DECL
  3100. && TREE_STATIC (decl)
  3101. && !DECL_EXTERNAL (decl)
  3102. && DECL_INITIAL (decl)
  3103. && walk_tree_without_duplicates (&DECL_INITIAL (decl),
  3104. has_label_address_in_static_1,
  3105. fndecl))
  3106. {
  3107. reason = G_("function %q+F can never be copied because it saves "
  3108. "address of local label in a static variable");
  3109. goto fail;
  3110. }
  3111. fail:
  3112. fun->cannot_be_copied_reason = reason;
  3113. fun->cannot_be_copied_set = true;
  3114. return reason;
  3115. }
  3116. static const char *inline_forbidden_reason;
  3117. /* A callback for walk_gimple_seq to handle statements. Returns non-null
  3118. iff a function can not be inlined. Also sets the reason why. */
  3119. static tree
  3120. inline_forbidden_p_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
  3121. struct walk_stmt_info *wip)
  3122. {
  3123. tree fn = (tree) wip->info;
  3124. tree t;
  3125. gimple stmt = gsi_stmt (*gsi);
  3126. switch (gimple_code (stmt))
  3127. {
  3128. case GIMPLE_CALL:
  3129. /* Refuse to inline alloca call unless user explicitly forced so as
  3130. this may change program's memory overhead drastically when the
  3131. function using alloca is called in loop. In GCC present in
  3132. SPEC2000 inlining into schedule_block cause it to require 2GB of
  3133. RAM instead of 256MB. Don't do so for alloca calls emitted for
  3134. VLA objects as those can't cause unbounded growth (they're always
  3135. wrapped inside stack_save/stack_restore regions. */
  3136. if (gimple_alloca_call_p (stmt)
  3137. && !gimple_call_alloca_for_var_p (as_a <gcall *> (stmt))
  3138. && !lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)))
  3139. {
  3140. inline_forbidden_reason
  3141. = G_("function %q+F can never be inlined because it uses "
  3142. "alloca (override using the always_inline attribute)");
  3143. *handled_ops_p = true;
  3144. return fn;
  3145. }
  3146. t = gimple_call_fndecl (stmt);
  3147. if (t == NULL_TREE)
  3148. break;
  3149. /* We cannot inline functions that call setjmp. */
  3150. if (setjmp_call_p (t))
  3151. {
  3152. inline_forbidden_reason
  3153. = G_("function %q+F can never be inlined because it uses setjmp");
  3154. *handled_ops_p = true;
  3155. return t;
  3156. }
  3157. if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL)
  3158. switch (DECL_FUNCTION_CODE (t))
  3159. {
  3160. /* We cannot inline functions that take a variable number of
  3161. arguments. */
  3162. case BUILT_IN_VA_START:
  3163. case BUILT_IN_NEXT_ARG:
  3164. case BUILT_IN_VA_END:
  3165. inline_forbidden_reason
  3166. = G_("function %q+F can never be inlined because it "
  3167. "uses variable argument lists");
  3168. *handled_ops_p = true;
  3169. return t;
  3170. case BUILT_IN_LONGJMP:
  3171. /* We can't inline functions that call __builtin_longjmp at
  3172. all. The non-local goto machinery really requires the
  3173. destination be in a different function. If we allow the
  3174. function calling __builtin_longjmp to be inlined into the
  3175. function calling __builtin_setjmp, Things will Go Awry. */
  3176. inline_forbidden_reason
  3177. = G_("function %q+F can never be inlined because "
  3178. "it uses setjmp-longjmp exception handling");
  3179. *handled_ops_p = true;
  3180. return t;
  3181. case BUILT_IN_NONLOCAL_GOTO:
  3182. /* Similarly. */
  3183. inline_forbidden_reason
  3184. = G_("function %q+F can never be inlined because "
  3185. "it uses non-local goto");
  3186. *handled_ops_p = true;
  3187. return t;
  3188. case BUILT_IN_RETURN:
  3189. case BUILT_IN_APPLY_ARGS:
  3190. /* If a __builtin_apply_args caller would be inlined,
  3191. it would be saving arguments of the function it has
  3192. been inlined into. Similarly __builtin_return would
  3193. return from the function the inline has been inlined into. */
  3194. inline_forbidden_reason
  3195. = G_("function %q+F can never be inlined because "
  3196. "it uses __builtin_return or __builtin_apply_args");
  3197. *handled_ops_p = true;
  3198. return t;
  3199. default:
  3200. break;
  3201. }
  3202. break;
  3203. case GIMPLE_GOTO:
  3204. t = gimple_goto_dest (stmt);
  3205. /* We will not inline a function which uses computed goto. The
  3206. addresses of its local labels, which may be tucked into
  3207. global storage, are of course not constant across
  3208. instantiations, which causes unexpected behavior. */
  3209. if (TREE_CODE (t) != LABEL_DECL)
  3210. {
  3211. inline_forbidden_reason
  3212. = G_("function %q+F can never be inlined "
  3213. "because it contains a computed goto");
  3214. *handled_ops_p = true;
  3215. return t;
  3216. }
  3217. break;
  3218. default:
  3219. break;
  3220. }
  3221. *handled_ops_p = false;
  3222. return NULL_TREE;
  3223. }
  3224. /* Return true if FNDECL is a function that cannot be inlined into
  3225. another one. */
  3226. static bool
  3227. inline_forbidden_p (tree fndecl)
  3228. {
  3229. struct function *fun = DECL_STRUCT_FUNCTION (fndecl);
  3230. struct walk_stmt_info wi;
  3231. basic_block bb;
  3232. bool forbidden_p = false;
  3233. /* First check for shared reasons not to copy the code. */
  3234. inline_forbidden_reason = copy_forbidden (fun, fndecl);
  3235. if (inline_forbidden_reason != NULL)
  3236. return true;
  3237. /* Next, walk the statements of the function looking for
  3238. constraucts we can't handle, or are non-optimal for inlining. */
  3239. hash_set<tree> visited_nodes;
  3240. memset (&wi, 0, sizeof (wi));
  3241. wi.info = (void *) fndecl;
  3242. wi.pset = &visited_nodes;
  3243. FOR_EACH_BB_FN (bb, fun)
  3244. {
  3245. gimple ret;
  3246. gimple_seq seq = bb_seq (bb);
  3247. ret = walk_gimple_seq (seq, inline_forbidden_p_stmt, NULL, &wi);
  3248. forbidden_p = (ret != NULL);
  3249. if (forbidden_p)
  3250. break;
  3251. }
  3252. return forbidden_p;
  3253. }
  3254. /* Return false if the function FNDECL cannot be inlined on account of its
  3255. attributes, true otherwise. */
  3256. static bool
  3257. function_attribute_inlinable_p (const_tree fndecl)
  3258. {
  3259. if (targetm.attribute_table)
  3260. {
  3261. const_tree a;
  3262. for (a = DECL_ATTRIBUTES (fndecl); a; a = TREE_CHAIN (a))
  3263. {
  3264. const_tree name = TREE_PURPOSE (a);
  3265. int i;
  3266. for (i = 0; targetm.attribute_table[i].name != NULL; i++)
  3267. if (is_attribute_p (targetm.attribute_table[i].name, name))
  3268. return targetm.function_attribute_inlinable_p (fndecl);
  3269. }
  3270. }
  3271. return true;
  3272. }
  3273. /* Returns nonzero if FN is a function that does not have any
  3274. fundamental inline blocking properties. */
  3275. bool
  3276. tree_inlinable_function_p (tree fn)
  3277. {
  3278. bool inlinable = true;
  3279. bool do_warning;
  3280. tree always_inline;
  3281. /* If we've already decided this function shouldn't be inlined,
  3282. there's no need to check again. */
  3283. if (DECL_UNINLINABLE (fn))
  3284. return false;
  3285. /* We only warn for functions declared `inline' by the user. */
  3286. do_warning = (warn_inline
  3287. && DECL_DECLARED_INLINE_P (fn)
  3288. && !DECL_NO_INLINE_WARNING_P (fn)
  3289. && !DECL_IN_SYSTEM_HEADER (fn));
  3290. always_inline = lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn));
  3291. if (flag_no_inline
  3292. && always_inline == NULL)
  3293. {
  3294. if (do_warning)
  3295. warning (OPT_Winline, "function %q+F can never be inlined because it "
  3296. "is suppressed using -fno-inline", fn);
  3297. inlinable = false;
  3298. }
  3299. else if (!function_attribute_inlinable_p (fn))
  3300. {
  3301. if (do_warning)
  3302. warning (OPT_Winline, "function %q+F can never be inlined because it "
  3303. "uses attributes conflicting with inlining", fn);
  3304. inlinable = false;
  3305. }
  3306. else if (inline_forbidden_p (fn))
  3307. {
  3308. /* See if we should warn about uninlinable functions. Previously,
  3309. some of these warnings would be issued while trying to expand
  3310. the function inline, but that would cause multiple warnings
  3311. about functions that would for example call alloca. But since
  3312. this a property of the function, just one warning is enough.
  3313. As a bonus we can now give more details about the reason why a
  3314. function is not inlinable. */
  3315. if (always_inline)
  3316. error (inline_forbidden_reason, fn);
  3317. else if (do_warning)
  3318. warning (OPT_Winline, inline_forbidden_reason, fn);
  3319. inlinable = false;
  3320. }
  3321. /* Squirrel away the result so that we don't have to check again. */
  3322. DECL_UNINLINABLE (fn) = !inlinable;
  3323. return inlinable;
  3324. }
  3325. /* Estimate the cost of a memory move of type TYPE. Use machine dependent
  3326. word size and take possible memcpy call into account and return
  3327. cost based on whether optimizing for size or speed according to SPEED_P. */
  3328. int
  3329. estimate_move_cost (tree type, bool ARG_UNUSED (speed_p))
  3330. {
  3331. HOST_WIDE_INT size;
  3332. gcc_assert (!VOID_TYPE_P (type));
  3333. if (TREE_CODE (type) == VECTOR_TYPE)
  3334. {
  3335. machine_mode inner = TYPE_MODE (TREE_TYPE (type));
  3336. machine_mode simd
  3337. = targetm.vectorize.preferred_simd_mode (inner);
  3338. int simd_mode_size = GET_MODE_SIZE (simd);
  3339. return ((GET_MODE_SIZE (TYPE_MODE (type)) + simd_mode_size - 1)
  3340. / simd_mode_size);
  3341. }
  3342. size = int_size_in_bytes (type);
  3343. if (size < 0 || size > MOVE_MAX_PIECES * MOVE_RATIO (speed_p))
  3344. /* Cost of a memcpy call, 3 arguments and the call. */
  3345. return 4;
  3346. else
  3347. return ((size + MOVE_MAX_PIECES - 1) / MOVE_MAX_PIECES);
  3348. }
  3349. /* Returns cost of operation CODE, according to WEIGHTS */
  3350. static int
  3351. estimate_operator_cost (enum tree_code code, eni_weights *weights,
  3352. tree op1 ATTRIBUTE_UNUSED, tree op2)
  3353. {
  3354. switch (code)
  3355. {
  3356. /* These are "free" conversions, or their presumed cost
  3357. is folded into other operations. */
  3358. case RANGE_EXPR:
  3359. CASE_CONVERT:
  3360. case COMPLEX_EXPR:
  3361. case PAREN_EXPR:
  3362. case VIEW_CONVERT_EXPR:
  3363. return 0;
  3364. /* Assign cost of 1 to usual operations.
  3365. ??? We may consider mapping RTL costs to this. */
  3366. case COND_EXPR:
  3367. case VEC_COND_EXPR:
  3368. case VEC_PERM_EXPR:
  3369. case PLUS_EXPR:
  3370. case POINTER_PLUS_EXPR:
  3371. case MINUS_EXPR:
  3372. case MULT_EXPR:
  3373. case MULT_HIGHPART_EXPR:
  3374. case FMA_EXPR:
  3375. case ADDR_SPACE_CONVERT_EXPR:
  3376. case FIXED_CONVERT_EXPR:
  3377. case FIX_TRUNC_EXPR:
  3378. case NEGATE_EXPR:
  3379. case FLOAT_EXPR:
  3380. case MIN_EXPR:
  3381. case MAX_EXPR:
  3382. case ABS_EXPR:
  3383. case LSHIFT_EXPR:
  3384. case RSHIFT_EXPR:
  3385. case LROTATE_EXPR:
  3386. case RROTATE_EXPR:
  3387. case BIT_IOR_EXPR:
  3388. case BIT_XOR_EXPR:
  3389. case BIT_AND_EXPR:
  3390. case BIT_NOT_EXPR:
  3391. case TRUTH_ANDIF_EXPR:
  3392. case TRUTH_ORIF_EXPR:
  3393. case TRUTH_AND_EXPR:
  3394. case TRUTH_OR_EXPR:
  3395. case TRUTH_XOR_EXPR:
  3396. case TRUTH_NOT_EXPR:
  3397. case LT_EXPR:
  3398. case LE_EXPR:
  3399. case GT_EXPR:
  3400. case GE_EXPR:
  3401. case EQ_EXPR:
  3402. case NE_EXPR:
  3403. case ORDERED_EXPR:
  3404. case UNORDERED_EXPR:
  3405. case UNLT_EXPR:
  3406. case UNLE_EXPR:
  3407. case UNGT_EXPR:
  3408. case UNGE_EXPR:
  3409. case UNEQ_EXPR:
  3410. case LTGT_EXPR:
  3411. case CONJ_EXPR:
  3412. case PREDECREMENT_EXPR:
  3413. case PREINCREMENT_EXPR:
  3414. case POSTDECREMENT_EXPR:
  3415. case POSTINCREMENT_EXPR:
  3416. case REALIGN_LOAD_EXPR:
  3417. case REDUC_MAX_EXPR:
  3418. case REDUC_MIN_EXPR:
  3419. case REDUC_PLUS_EXPR:
  3420. case WIDEN_SUM_EXPR:
  3421. case WIDEN_MULT_EXPR:
  3422. case DOT_PROD_EXPR:
  3423. case SAD_EXPR:
  3424. case WIDEN_MULT_PLUS_EXPR:
  3425. case WIDEN_MULT_MINUS_EXPR:
  3426. case WIDEN_LSHIFT_EXPR:
  3427. case VEC_WIDEN_MULT_HI_EXPR:
  3428. case VEC_WIDEN_MULT_LO_EXPR:
  3429. case VEC_WIDEN_MULT_EVEN_EXPR:
  3430. case VEC_WIDEN_MULT_ODD_EXPR:
  3431. case VEC_UNPACK_HI_EXPR:
  3432. case VEC_UNPACK_LO_EXPR:
  3433. case VEC_UNPACK_FLOAT_HI_EXPR:
  3434. case VEC_UNPACK_FLOAT_LO_EXPR:
  3435. case VEC_PACK_TRUNC_EXPR:
  3436. case VEC_PACK_SAT_EXPR:
  3437. case VEC_PACK_FIX_TRUNC_EXPR:
  3438. case VEC_WIDEN_LSHIFT_HI_EXPR:
  3439. case VEC_WIDEN_LSHIFT_LO_EXPR:
  3440. return 1;
  3441. /* Few special cases of expensive operations. This is useful
  3442. to avoid inlining on functions having too many of these. */
  3443. case TRUNC_DIV_EXPR:
  3444. case CEIL_DIV_EXPR:
  3445. case FLOOR_DIV_EXPR:
  3446. case ROUND_DIV_EXPR:
  3447. case EXACT_DIV_EXPR:
  3448. case TRUNC_MOD_EXPR:
  3449. case CEIL_MOD_EXPR:
  3450. case FLOOR_MOD_EXPR:
  3451. case ROUND_MOD_EXPR:
  3452. case RDIV_EXPR:
  3453. if (TREE_CODE (op2) != INTEGER_CST)
  3454. return weights->div_mod_cost;
  3455. return 1;
  3456. default:
  3457. /* We expect a copy assignment with no operator. */
  3458. gcc_assert (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS);
  3459. return 0;
  3460. }
  3461. }
  3462. /* Estimate number of instructions that will be created by expanding
  3463. the statements in the statement sequence STMTS.
  3464. WEIGHTS contains weights attributed to various constructs. */
  3465. static
  3466. int estimate_num_insns_seq (gimple_seq stmts, eni_weights *weights)
  3467. {
  3468. int cost;
  3469. gimple_stmt_iterator gsi;
  3470. cost = 0;
  3471. for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
  3472. cost += estimate_num_insns (gsi_stmt (gsi), weights);
  3473. return cost;
  3474. }
  3475. /* Estimate number of instructions that will be created by expanding STMT.
  3476. WEIGHTS contains weights attributed to various constructs. */
  3477. int
  3478. estimate_num_insns (gimple stmt, eni_weights *weights)
  3479. {
  3480. unsigned cost, i;
  3481. enum gimple_code code = gimple_code (stmt);
  3482. tree lhs;
  3483. tree rhs;
  3484. switch (code)
  3485. {
  3486. case GIMPLE_ASSIGN:
  3487. /* Try to estimate the cost of assignments. We have three cases to
  3488. deal with:
  3489. 1) Simple assignments to registers;
  3490. 2) Stores to things that must live in memory. This includes
  3491. "normal" stores to scalars, but also assignments of large
  3492. structures, or constructors of big arrays;
  3493. Let us look at the first two cases, assuming we have "a = b + C":
  3494. <GIMPLE_ASSIGN <var_decl "a">
  3495. <plus_expr <var_decl "b"> <constant C>>
  3496. If "a" is a GIMPLE register, the assignment to it is free on almost
  3497. any target, because "a" usually ends up in a real register. Hence
  3498. the only cost of this expression comes from the PLUS_EXPR, and we
  3499. can ignore the GIMPLE_ASSIGN.
  3500. If "a" is not a GIMPLE register, the assignment to "a" will most
  3501. likely be a real store, so the cost of the GIMPLE_ASSIGN is the cost
  3502. of moving something into "a", which we compute using the function
  3503. estimate_move_cost. */
  3504. if (gimple_clobber_p (stmt))
  3505. return 0; /* ={v} {CLOBBER} stmt expands to nothing. */
  3506. lhs = gimple_assign_lhs (stmt);
  3507. rhs = gimple_assign_rhs1 (stmt);
  3508. cost = 0;
  3509. /* Account for the cost of moving to / from memory. */
  3510. if (gimple_store_p (stmt))
  3511. cost += estimate_move_cost (TREE_TYPE (lhs), weights->time_based);
  3512. if (gimple_assign_load_p (stmt))
  3513. cost += estimate_move_cost (TREE_TYPE (rhs), weights->time_based);
  3514. cost += estimate_operator_cost (gimple_assign_rhs_code (stmt), weights,
  3515. gimple_assign_rhs1 (stmt),
  3516. get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
  3517. == GIMPLE_BINARY_RHS
  3518. ? gimple_assign_rhs2 (stmt) : NULL);
  3519. break;
  3520. case GIMPLE_COND:
  3521. cost = 1 + estimate_operator_cost (gimple_cond_code (stmt), weights,
  3522. gimple_op (stmt, 0),
  3523. gimple_op (stmt, 1));
  3524. break;
  3525. case GIMPLE_SWITCH:
  3526. {
  3527. gswitch *switch_stmt = as_a <gswitch *> (stmt);
  3528. /* Take into account cost of the switch + guess 2 conditional jumps for
  3529. each case label.
  3530. TODO: once the switch expansion logic is sufficiently separated, we can
  3531. do better job on estimating cost of the switch. */
  3532. if (weights->time_based)
  3533. cost = floor_log2 (gimple_switch_num_labels (switch_stmt)) * 2;
  3534. else
  3535. cost = gimple_switch_num_labels (switch_stmt) * 2;
  3536. }
  3537. break;
  3538. case GIMPLE_CALL:
  3539. {
  3540. tree decl;
  3541. if (gimple_call_internal_p (stmt))
  3542. return 0;
  3543. else if ((decl = gimple_call_fndecl (stmt))
  3544. && DECL_BUILT_IN (decl))
  3545. {
  3546. /* Do not special case builtins where we see the body.
  3547. This just confuse inliner. */
  3548. struct cgraph_node *node;
  3549. if (!(node = cgraph_node::get (decl))
  3550. || node->definition)
  3551. ;
  3552. /* For buitins that are likely expanded to nothing or
  3553. inlined do not account operand costs. */
  3554. else if (is_simple_builtin (decl))
  3555. return 0;
  3556. else if (is_inexpensive_builtin (decl))
  3557. return weights->target_builtin_call_cost;
  3558. else if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
  3559. {
  3560. /* We canonicalize x * x to pow (x, 2.0) with -ffast-math, so
  3561. specialize the cheap expansion we do here.
  3562. ??? This asks for a more general solution. */
  3563. switch (DECL_FUNCTION_CODE (decl))
  3564. {
  3565. case BUILT_IN_POW:
  3566. case BUILT_IN_POWF:
  3567. case BUILT_IN_POWL:
  3568. if (TREE_CODE (gimple_call_arg (stmt, 1)) == REAL_CST
  3569. && REAL_VALUES_EQUAL
  3570. (TREE_REAL_CST (gimple_call_arg (stmt, 1)), dconst2))
  3571. return estimate_operator_cost
  3572. (MULT_EXPR, weights, gimple_call_arg (stmt, 0),
  3573. gimple_call_arg (stmt, 0));
  3574. break;
  3575. default:
  3576. break;
  3577. }
  3578. }
  3579. }
  3580. cost = decl ? weights->call_cost : weights->indirect_call_cost;
  3581. if (gimple_call_lhs (stmt))
  3582. cost += estimate_move_cost (TREE_TYPE (gimple_call_lhs (stmt)),
  3583. weights->time_based);
  3584. for (i = 0; i < gimple_call_num_args (stmt); i++)
  3585. {
  3586. tree arg = gimple_call_arg (stmt, i);
  3587. cost += estimate_move_cost (TREE_TYPE (arg),
  3588. weights->time_based);
  3589. }
  3590. break;
  3591. }
  3592. case GIMPLE_RETURN:
  3593. return weights->return_cost;
  3594. case GIMPLE_GOTO:
  3595. case GIMPLE_LABEL:
  3596. case GIMPLE_NOP:
  3597. case GIMPLE_PHI:
  3598. case GIMPLE_PREDICT:
  3599. case GIMPLE_DEBUG:
  3600. return 0;
  3601. case GIMPLE_ASM:
  3602. {
  3603. int count = asm_str_count (gimple_asm_string (as_a <gasm *> (stmt)));
  3604. /* 1000 means infinity. This avoids overflows later
  3605. with very long asm statements. */
  3606. if (count > 1000)
  3607. count = 1000;
  3608. return count;
  3609. }
  3610. case GIMPLE_RESX:
  3611. /* This is either going to be an external function call with one
  3612. argument, or two register copy statements plus a goto. */
  3613. return 2;
  3614. case GIMPLE_EH_DISPATCH:
  3615. /* ??? This is going to turn into a switch statement. Ideally
  3616. we'd have a look at the eh region and estimate the number of
  3617. edges involved. */
  3618. return 10;
  3619. case GIMPLE_BIND:
  3620. return estimate_num_insns_seq (
  3621. gimple_bind_body (as_a <gbind *> (stmt)),
  3622. weights);
  3623. case GIMPLE_EH_FILTER:
  3624. return estimate_num_insns_seq (gimple_eh_filter_failure (stmt), weights);
  3625. case GIMPLE_CATCH:
  3626. return estimate_num_insns_seq (gimple_catch_handler (
  3627. as_a <gcatch *> (stmt)),
  3628. weights);
  3629. case GIMPLE_TRY:
  3630. return (estimate_num_insns_seq (gimple_try_eval (stmt), weights)
  3631. + estimate_num_insns_seq (gimple_try_cleanup (stmt), weights));
  3632. /* OMP directives are generally very expensive. */
  3633. case GIMPLE_OMP_RETURN:
  3634. case GIMPLE_OMP_SECTIONS_SWITCH:
  3635. case GIMPLE_OMP_ATOMIC_STORE:
  3636. case GIMPLE_OMP_CONTINUE:
  3637. /* ...except these, which are cheap. */
  3638. return 0;
  3639. case GIMPLE_OMP_ATOMIC_LOAD:
  3640. return weights->omp_cost;
  3641. case GIMPLE_OMP_FOR:
  3642. return (weights->omp_cost
  3643. + estimate_num_insns_seq (gimple_omp_body (stmt), weights)
  3644. + estimate_num_insns_seq (gimple_omp_for_pre_body (stmt), weights));
  3645. case GIMPLE_OMP_PARALLEL:
  3646. case GIMPLE_OMP_TASK:
  3647. case GIMPLE_OMP_CRITICAL:
  3648. case GIMPLE_OMP_MASTER:
  3649. case GIMPLE_OMP_TASKGROUP:
  3650. case GIMPLE_OMP_ORDERED:
  3651. case GIMPLE_OMP_SECTION:
  3652. case GIMPLE_OMP_SECTIONS:
  3653. case GIMPLE_OMP_SINGLE:
  3654. case GIMPLE_OMP_TARGET:
  3655. case GIMPLE_OMP_TEAMS:
  3656. return (weights->omp_cost
  3657. + estimate_num_insns_seq (gimple_omp_body (stmt), weights));
  3658. case GIMPLE_TRANSACTION:
  3659. return (weights->tm_cost
  3660. + estimate_num_insns_seq (gimple_transaction_body (
  3661. as_a <gtransaction *> (stmt)),
  3662. weights));
  3663. default:
  3664. gcc_unreachable ();
  3665. }
  3666. return cost;
  3667. }
  3668. /* Estimate number of instructions that will be created by expanding
  3669. function FNDECL. WEIGHTS contains weights attributed to various
  3670. constructs. */
  3671. int
  3672. estimate_num_insns_fn (tree fndecl, eni_weights *weights)
  3673. {
  3674. struct function *my_function = DECL_STRUCT_FUNCTION (fndecl);
  3675. gimple_stmt_iterator bsi;
  3676. basic_block bb;
  3677. int n = 0;
  3678. gcc_assert (my_function && my_function->cfg);
  3679. FOR_EACH_BB_FN (bb, my_function)
  3680. {
  3681. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  3682. n += estimate_num_insns (gsi_stmt (bsi), weights);
  3683. }
  3684. return n;
  3685. }
  3686. /* Initializes weights used by estimate_num_insns. */
  3687. void
  3688. init_inline_once (void)
  3689. {
  3690. eni_size_weights.call_cost = 1;
  3691. eni_size_weights.indirect_call_cost = 3;
  3692. eni_size_weights.target_builtin_call_cost = 1;
  3693. eni_size_weights.div_mod_cost = 1;
  3694. eni_size_weights.omp_cost = 40;
  3695. eni_size_weights.tm_cost = 10;
  3696. eni_size_weights.time_based = false;
  3697. eni_size_weights.return_cost = 1;
  3698. /* Estimating time for call is difficult, since we have no idea what the
  3699. called function does. In the current uses of eni_time_weights,
  3700. underestimating the cost does less harm than overestimating it, so
  3701. we choose a rather small value here. */
  3702. eni_time_weights.call_cost = 10;
  3703. eni_time_weights.indirect_call_cost = 15;
  3704. eni_time_weights.target_builtin_call_cost = 1;
  3705. eni_time_weights.div_mod_cost = 10;
  3706. eni_time_weights.omp_cost = 40;
  3707. eni_time_weights.tm_cost = 40;
  3708. eni_time_weights.time_based = true;
  3709. eni_time_weights.return_cost = 2;
  3710. }
  3711. /* Estimate the number of instructions in a gimple_seq. */
  3712. int
  3713. count_insns_seq (gimple_seq seq, eni_weights *weights)
  3714. {
  3715. gimple_stmt_iterator gsi;
  3716. int n = 0;
  3717. for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
  3718. n += estimate_num_insns (gsi_stmt (gsi), weights);
  3719. return n;
  3720. }
  3721. /* Install new lexical TREE_BLOCK underneath 'current_block'. */
  3722. static void
  3723. prepend_lexical_block (tree current_block, tree new_block)
  3724. {
  3725. BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (current_block);
  3726. BLOCK_SUBBLOCKS (current_block) = new_block;
  3727. BLOCK_SUPERCONTEXT (new_block) = current_block;
  3728. }
  3729. /* Add local variables from CALLEE to CALLER. */
  3730. static inline void
  3731. add_local_variables (struct function *callee, struct function *caller,
  3732. copy_body_data *id)
  3733. {
  3734. tree var;
  3735. unsigned ix;
  3736. FOR_EACH_LOCAL_DECL (callee, ix, var)
  3737. if (!can_be_nonlocal (var, id))
  3738. {
  3739. tree new_var = remap_decl (var, id);
  3740. /* Remap debug-expressions. */
  3741. if (TREE_CODE (new_var) == VAR_DECL
  3742. && DECL_HAS_DEBUG_EXPR_P (var)
  3743. && new_var != var)
  3744. {
  3745. tree tem = DECL_DEBUG_EXPR (var);
  3746. bool old_regimplify = id->regimplify;
  3747. id->remapping_type_depth++;
  3748. walk_tree (&tem, copy_tree_body_r, id, NULL);
  3749. id->remapping_type_depth--;
  3750. id->regimplify = old_regimplify;
  3751. SET_DECL_DEBUG_EXPR (new_var, tem);
  3752. DECL_HAS_DEBUG_EXPR_P (new_var) = 1;
  3753. }
  3754. add_local_decl (caller, new_var);
  3755. }
  3756. }
  3757. /* If STMT is a GIMPLE_CALL, replace it with its inline expansion. */
  3758. static bool
  3759. expand_call_inline (basic_block bb, gimple stmt, copy_body_data *id)
  3760. {
  3761. tree use_retvar;
  3762. tree fn;
  3763. hash_map<tree, tree> *dst;
  3764. hash_map<tree, tree> *st = NULL;
  3765. tree return_slot;
  3766. tree modify_dest;
  3767. tree return_bounds = NULL;
  3768. location_t saved_location;
  3769. struct cgraph_edge *cg_edge;
  3770. cgraph_inline_failed_t reason;
  3771. basic_block return_block;
  3772. edge e;
  3773. gimple_stmt_iterator gsi, stmt_gsi;
  3774. bool successfully_inlined = FALSE;
  3775. bool purge_dead_abnormal_edges;
  3776. gcall *call_stmt;
  3777. unsigned int i;
  3778. /* Set input_location here so we get the right instantiation context
  3779. if we call instantiate_decl from inlinable_function_p. */
  3780. /* FIXME: instantiate_decl isn't called by inlinable_function_p. */
  3781. saved_location = input_location;
  3782. input_location = gimple_location (stmt);
  3783. /* From here on, we're only interested in CALL_EXPRs. */
  3784. call_stmt = dyn_cast <gcall *> (stmt);
  3785. if (!call_stmt)
  3786. goto egress;
  3787. cg_edge = id->dst_node->get_edge (stmt);
  3788. gcc_checking_assert (cg_edge);
  3789. /* First, see if we can figure out what function is being called.
  3790. If we cannot, then there is no hope of inlining the function. */
  3791. if (cg_edge->indirect_unknown_callee)
  3792. goto egress;
  3793. fn = cg_edge->callee->decl;
  3794. gcc_checking_assert (fn);
  3795. /* If FN is a declaration of a function in a nested scope that was
  3796. globally declared inline, we don't set its DECL_INITIAL.
  3797. However, we can't blindly follow DECL_ABSTRACT_ORIGIN because the
  3798. C++ front-end uses it for cdtors to refer to their internal
  3799. declarations, that are not real functions. Fortunately those
  3800. don't have trees to be saved, so we can tell by checking their
  3801. gimple_body. */
  3802. if (!DECL_INITIAL (fn)
  3803. && DECL_ABSTRACT_ORIGIN (fn)
  3804. && gimple_has_body_p (DECL_ABSTRACT_ORIGIN (fn)))
  3805. fn = DECL_ABSTRACT_ORIGIN (fn);
  3806. /* Don't try to inline functions that are not well-suited to inlining. */
  3807. if (cg_edge->inline_failed)
  3808. {
  3809. reason = cg_edge->inline_failed;
  3810. /* If this call was originally indirect, we do not want to emit any
  3811. inlining related warnings or sorry messages because there are no
  3812. guarantees regarding those. */
  3813. if (cg_edge->indirect_inlining_edge)
  3814. goto egress;
  3815. if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn))
  3816. /* For extern inline functions that get redefined we always
  3817. silently ignored always_inline flag. Better behaviour would
  3818. be to be able to keep both bodies and use extern inline body
  3819. for inlining, but we can't do that because frontends overwrite
  3820. the body. */
  3821. && !cg_edge->callee->local.redefined_extern_inline
  3822. /* During early inline pass, report only when optimization is
  3823. not turned on. */
  3824. && (symtab->global_info_ready
  3825. || !optimize
  3826. || cgraph_inline_failed_type (reason) == CIF_FINAL_ERROR)
  3827. /* PR 20090218-1_0.c. Body can be provided by another module. */
  3828. && (reason != CIF_BODY_NOT_AVAILABLE || !flag_generate_lto))
  3829. {
  3830. error ("inlining failed in call to always_inline %q+F: %s", fn,
  3831. cgraph_inline_failed_string (reason));
  3832. error ("called from here");
  3833. }
  3834. else if (warn_inline
  3835. && DECL_DECLARED_INLINE_P (fn)
  3836. && !DECL_NO_INLINE_WARNING_P (fn)
  3837. && !DECL_IN_SYSTEM_HEADER (fn)
  3838. && reason != CIF_UNSPECIFIED
  3839. && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fn))
  3840. /* Do not warn about not inlined recursive calls. */
  3841. && !cg_edge->recursive_p ()
  3842. /* Avoid warnings during early inline pass. */
  3843. && symtab->global_info_ready)
  3844. {
  3845. warning (OPT_Winline, "inlining failed in call to %q+F: %s",
  3846. fn, _(cgraph_inline_failed_string (reason)));
  3847. warning (OPT_Winline, "called from here");
  3848. }
  3849. goto egress;
  3850. }
  3851. fn = cg_edge->callee->decl;
  3852. cg_edge->callee->get_untransformed_body ();
  3853. #ifdef ENABLE_CHECKING
  3854. if (cg_edge->callee->decl != id->dst_node->decl)
  3855. cg_edge->callee->verify ();
  3856. #endif
  3857. /* We will be inlining this callee. */
  3858. id->eh_lp_nr = lookup_stmt_eh_lp (stmt);
  3859. id->assign_stmts.create (0);
  3860. /* Update the callers EH personality. */
  3861. if (DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl))
  3862. DECL_FUNCTION_PERSONALITY (cg_edge->caller->decl)
  3863. = DECL_FUNCTION_PERSONALITY (cg_edge->callee->decl);
  3864. /* Split the block holding the GIMPLE_CALL. */
  3865. e = split_block (bb, stmt);
  3866. bb = e->src;
  3867. return_block = e->dest;
  3868. remove_edge (e);
  3869. /* split_block splits after the statement; work around this by
  3870. moving the call into the second block manually. Not pretty,
  3871. but seems easier than doing the CFG manipulation by hand
  3872. when the GIMPLE_CALL is in the last statement of BB. */
  3873. stmt_gsi = gsi_last_bb (bb);
  3874. gsi_remove (&stmt_gsi, false);
  3875. /* If the GIMPLE_CALL was in the last statement of BB, it may have
  3876. been the source of abnormal edges. In this case, schedule
  3877. the removal of dead abnormal edges. */
  3878. gsi = gsi_start_bb (return_block);
  3879. if (gsi_end_p (gsi))
  3880. {
  3881. gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
  3882. purge_dead_abnormal_edges = true;
  3883. }
  3884. else
  3885. {
  3886. gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
  3887. purge_dead_abnormal_edges = false;
  3888. }
  3889. stmt_gsi = gsi_start_bb (return_block);
  3890. /* Build a block containing code to initialize the arguments, the
  3891. actual inline expansion of the body, and a label for the return
  3892. statements within the function to jump to. The type of the
  3893. statement expression is the return type of the function call.
  3894. ??? If the call does not have an associated block then we will
  3895. remap all callee blocks to NULL, effectively dropping most of
  3896. its debug information. This should only happen for calls to
  3897. artificial decls inserted by the compiler itself. We need to
  3898. either link the inlined blocks into the caller block tree or
  3899. not refer to them in any way to not break GC for locations. */
  3900. if (gimple_block (stmt))
  3901. {
  3902. id->block = make_node (BLOCK);
  3903. BLOCK_ABSTRACT_ORIGIN (id->block) = fn;
  3904. BLOCK_SOURCE_LOCATION (id->block) = LOCATION_LOCUS (input_location);
  3905. prepend_lexical_block (gimple_block (stmt), id->block);
  3906. }
  3907. /* Local declarations will be replaced by their equivalents in this
  3908. map. */
  3909. st = id->decl_map;
  3910. id->decl_map = new hash_map<tree, tree>;
  3911. dst = id->debug_map;
  3912. id->debug_map = NULL;
  3913. /* Record the function we are about to inline. */
  3914. id->src_fn = fn;
  3915. id->src_node = cg_edge->callee;
  3916. id->src_cfun = DECL_STRUCT_FUNCTION (fn);
  3917. id->call_stmt = stmt;
  3918. gcc_assert (!id->src_cfun->after_inlining);
  3919. id->entry_bb = bb;
  3920. if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)))
  3921. {
  3922. gimple_stmt_iterator si = gsi_last_bb (bb);
  3923. gsi_insert_after (&si, gimple_build_predict (PRED_COLD_FUNCTION,
  3924. NOT_TAKEN),
  3925. GSI_NEW_STMT);
  3926. }
  3927. initialize_inlined_parameters (id, stmt, fn, bb);
  3928. if (DECL_INITIAL (fn))
  3929. {
  3930. if (gimple_block (stmt))
  3931. {
  3932. tree *var;
  3933. prepend_lexical_block (id->block,
  3934. remap_blocks (DECL_INITIAL (fn), id));
  3935. gcc_checking_assert (BLOCK_SUBBLOCKS (id->block)
  3936. && (BLOCK_CHAIN (BLOCK_SUBBLOCKS (id->block))
  3937. == NULL_TREE));
  3938. /* Move vars for PARM_DECLs from DECL_INITIAL block to id->block,
  3939. otherwise for DWARF DW_TAG_formal_parameter will not be children of
  3940. DW_TAG_inlined_subroutine, but of a DW_TAG_lexical_block
  3941. under it. The parameters can be then evaluated in the debugger,
  3942. but don't show in backtraces. */
  3943. for (var = &BLOCK_VARS (BLOCK_SUBBLOCKS (id->block)); *var; )
  3944. if (TREE_CODE (DECL_ORIGIN (*var)) == PARM_DECL)
  3945. {
  3946. tree v = *var;
  3947. *var = TREE_CHAIN (v);
  3948. TREE_CHAIN (v) = BLOCK_VARS (id->block);
  3949. BLOCK_VARS (id->block) = v;
  3950. }
  3951. else
  3952. var = &TREE_CHAIN (*var);
  3953. }
  3954. else
  3955. remap_blocks_to_null (DECL_INITIAL (fn), id);
  3956. }
  3957. /* Return statements in the function body will be replaced by jumps
  3958. to the RET_LABEL. */
  3959. gcc_assert (DECL_INITIAL (fn));
  3960. gcc_assert (TREE_CODE (DECL_INITIAL (fn)) == BLOCK);
  3961. /* Find the LHS to which the result of this call is assigned. */
  3962. return_slot = NULL;
  3963. if (gimple_call_lhs (stmt))
  3964. {
  3965. modify_dest = gimple_call_lhs (stmt);
  3966. /* Remember where to copy returned bounds. */
  3967. if (gimple_call_with_bounds_p (stmt)
  3968. && TREE_CODE (modify_dest) == SSA_NAME)
  3969. {
  3970. gcall *retbnd = chkp_retbnd_call_by_val (modify_dest);
  3971. if (retbnd)
  3972. {
  3973. return_bounds = gimple_call_lhs (retbnd);
  3974. /* If returned bounds are not used then just
  3975. remove unused call. */
  3976. if (!return_bounds)
  3977. {
  3978. gimple_stmt_iterator iter = gsi_for_stmt (retbnd);
  3979. gsi_remove (&iter, true);
  3980. }
  3981. }
  3982. }
  3983. /* The function which we are inlining might not return a value,
  3984. in which case we should issue a warning that the function
  3985. does not return a value. In that case the optimizers will
  3986. see that the variable to which the value is assigned was not
  3987. initialized. We do not want to issue a warning about that
  3988. uninitialized variable. */
  3989. if (DECL_P (modify_dest))
  3990. TREE_NO_WARNING (modify_dest) = 1;
  3991. if (gimple_call_return_slot_opt_p (call_stmt))
  3992. {
  3993. return_slot = modify_dest;
  3994. modify_dest = NULL;
  3995. }
  3996. }
  3997. else
  3998. modify_dest = NULL;
  3999. /* If we are inlining a call to the C++ operator new, we don't want
  4000. to use type based alias analysis on the return value. Otherwise
  4001. we may get confused if the compiler sees that the inlined new
  4002. function returns a pointer which was just deleted. See bug
  4003. 33407. */
  4004. if (DECL_IS_OPERATOR_NEW (fn))
  4005. {
  4006. return_slot = NULL;
  4007. modify_dest = NULL;
  4008. }
  4009. /* Declare the return variable for the function. */
  4010. use_retvar = declare_return_variable (id, return_slot, modify_dest,
  4011. return_bounds, bb);
  4012. /* Add local vars in this inlined callee to caller. */
  4013. add_local_variables (id->src_cfun, cfun, id);
  4014. if (dump_file && (dump_flags & TDF_DETAILS))
  4015. {
  4016. fprintf (dump_file, "Inlining ");
  4017. print_generic_expr (dump_file, id->src_fn, 0);
  4018. fprintf (dump_file, " to ");
  4019. print_generic_expr (dump_file, id->dst_fn, 0);
  4020. fprintf (dump_file, " with frequency %i\n", cg_edge->frequency);
  4021. }
  4022. /* This is it. Duplicate the callee body. Assume callee is
  4023. pre-gimplified. Note that we must not alter the caller
  4024. function in any way before this point, as this CALL_EXPR may be
  4025. a self-referential call; if we're calling ourselves, we need to
  4026. duplicate our body before altering anything. */
  4027. copy_body (id, cg_edge->callee->count,
  4028. GCOV_COMPUTE_SCALE (cg_edge->frequency, CGRAPH_FREQ_BASE),
  4029. bb, return_block, NULL);
  4030. /* Reset the escaped solution. */
  4031. if (cfun->gimple_df)
  4032. pt_solution_reset (&cfun->gimple_df->escaped);
  4033. /* Clean up. */
  4034. if (id->debug_map)
  4035. {
  4036. delete id->debug_map;
  4037. id->debug_map = dst;
  4038. }
  4039. delete id->decl_map;
  4040. id->decl_map = st;
  4041. /* Unlink the calls virtual operands before replacing it. */
  4042. unlink_stmt_vdef (stmt);
  4043. if (gimple_vdef (stmt)
  4044. && TREE_CODE (gimple_vdef (stmt)) == SSA_NAME)
  4045. release_ssa_name (gimple_vdef (stmt));
  4046. /* If the inlined function returns a result that we care about,
  4047. substitute the GIMPLE_CALL with an assignment of the return
  4048. variable to the LHS of the call. That is, if STMT was
  4049. 'a = foo (...)', substitute the call with 'a = USE_RETVAR'. */
  4050. if (use_retvar && gimple_call_lhs (stmt))
  4051. {
  4052. gimple old_stmt = stmt;
  4053. stmt = gimple_build_assign (gimple_call_lhs (stmt), use_retvar);
  4054. gsi_replace (&stmt_gsi, stmt, false);
  4055. maybe_clean_or_replace_eh_stmt (old_stmt, stmt);
  4056. /* Copy bounds if we copy structure with bounds. */
  4057. if (chkp_function_instrumented_p (id->dst_fn)
  4058. && !BOUNDED_P (use_retvar)
  4059. && chkp_type_has_pointer (TREE_TYPE (use_retvar)))
  4060. id->assign_stmts.safe_push (stmt);
  4061. }
  4062. else
  4063. {
  4064. /* Handle the case of inlining a function with no return
  4065. statement, which causes the return value to become undefined. */
  4066. if (gimple_call_lhs (stmt)
  4067. && TREE_CODE (gimple_call_lhs (stmt)) == SSA_NAME)
  4068. {
  4069. tree name = gimple_call_lhs (stmt);
  4070. tree var = SSA_NAME_VAR (name);
  4071. tree def = ssa_default_def (cfun, var);
  4072. if (def)
  4073. {
  4074. /* If the variable is used undefined, make this name
  4075. undefined via a move. */
  4076. stmt = gimple_build_assign (gimple_call_lhs (stmt), def);
  4077. gsi_replace (&stmt_gsi, stmt, true);
  4078. }
  4079. else
  4080. {
  4081. /* Otherwise make this variable undefined. */
  4082. gsi_remove (&stmt_gsi, true);
  4083. set_ssa_default_def (cfun, var, name);
  4084. SSA_NAME_DEF_STMT (name) = gimple_build_nop ();
  4085. }
  4086. }
  4087. else
  4088. gsi_remove (&stmt_gsi, true);
  4089. }
  4090. /* Put returned bounds into the correct place if required. */
  4091. if (return_bounds)
  4092. {
  4093. gimple old_stmt = SSA_NAME_DEF_STMT (return_bounds);
  4094. gimple new_stmt = gimple_build_assign (return_bounds, id->retbnd);
  4095. gimple_stmt_iterator bnd_gsi = gsi_for_stmt (old_stmt);
  4096. unlink_stmt_vdef (old_stmt);
  4097. gsi_replace (&bnd_gsi, new_stmt, false);
  4098. maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt);
  4099. cgraph_update_edges_for_call_stmt (old_stmt,
  4100. gimple_call_fndecl (old_stmt),
  4101. new_stmt);
  4102. }
  4103. if (purge_dead_abnormal_edges)
  4104. {
  4105. gimple_purge_dead_eh_edges (return_block);
  4106. gimple_purge_dead_abnormal_call_edges (return_block);
  4107. }
  4108. /* If the value of the new expression is ignored, that's OK. We
  4109. don't warn about this for CALL_EXPRs, so we shouldn't warn about
  4110. the equivalent inlined version either. */
  4111. if (is_gimple_assign (stmt))
  4112. {
  4113. gcc_assert (gimple_assign_single_p (stmt)
  4114. || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)));
  4115. TREE_USED (gimple_assign_rhs1 (stmt)) = 1;
  4116. }
  4117. /* Copy bounds for all generated assigns that need it. */
  4118. for (i = 0; i < id->assign_stmts.length (); i++)
  4119. chkp_copy_bounds_for_assign (id->assign_stmts[i], cg_edge);
  4120. id->assign_stmts.release ();
  4121. /* Output the inlining info for this abstract function, since it has been
  4122. inlined. If we don't do this now, we can lose the information about the
  4123. variables in the function when the blocks get blown away as soon as we
  4124. remove the cgraph node. */
  4125. if (gimple_block (stmt))
  4126. (*debug_hooks->outlining_inline_function) (cg_edge->callee->decl);
  4127. /* Update callgraph if needed. */
  4128. cg_edge->callee->remove ();
  4129. id->block = NULL_TREE;
  4130. successfully_inlined = TRUE;
  4131. egress:
  4132. input_location = saved_location;
  4133. return successfully_inlined;
  4134. }
  4135. /* Expand call statements reachable from STMT_P.
  4136. We can only have CALL_EXPRs as the "toplevel" tree code or nested
  4137. in a MODIFY_EXPR. */
  4138. static bool
  4139. gimple_expand_calls_inline (basic_block bb, copy_body_data *id)
  4140. {
  4141. gimple_stmt_iterator gsi;
  4142. bool inlined = false;
  4143. for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
  4144. {
  4145. gimple stmt = gsi_stmt (gsi);
  4146. gsi_prev (&gsi);
  4147. if (is_gimple_call (stmt)
  4148. && !gimple_call_internal_p (stmt))
  4149. inlined |= expand_call_inline (bb, stmt, id);
  4150. }
  4151. return inlined;
  4152. }
  4153. /* Walk all basic blocks created after FIRST and try to fold every statement
  4154. in the STATEMENTS pointer set. */
  4155. static void
  4156. fold_marked_statements (int first, hash_set<gimple> *statements)
  4157. {
  4158. for (; first < n_basic_blocks_for_fn (cfun); first++)
  4159. if (BASIC_BLOCK_FOR_FN (cfun, first))
  4160. {
  4161. gimple_stmt_iterator gsi;
  4162. for (gsi = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
  4163. !gsi_end_p (gsi);
  4164. gsi_next (&gsi))
  4165. if (statements->contains (gsi_stmt (gsi)))
  4166. {
  4167. gimple old_stmt = gsi_stmt (gsi);
  4168. tree old_decl = is_gimple_call (old_stmt) ? gimple_call_fndecl (old_stmt) : 0;
  4169. if (old_decl && DECL_BUILT_IN (old_decl))
  4170. {
  4171. /* Folding builtins can create multiple instructions,
  4172. we need to look at all of them. */
  4173. gimple_stmt_iterator i2 = gsi;
  4174. gsi_prev (&i2);
  4175. if (fold_stmt (&gsi))
  4176. {
  4177. gimple new_stmt;
  4178. /* If a builtin at the end of a bb folded into nothing,
  4179. the following loop won't work. */
  4180. if (gsi_end_p (gsi))
  4181. {
  4182. cgraph_update_edges_for_call_stmt (old_stmt,
  4183. old_decl, NULL);
  4184. break;
  4185. }
  4186. if (gsi_end_p (i2))
  4187. i2 = gsi_start_bb (BASIC_BLOCK_FOR_FN (cfun, first));
  4188. else
  4189. gsi_next (&i2);
  4190. while (1)
  4191. {
  4192. new_stmt = gsi_stmt (i2);
  4193. update_stmt (new_stmt);
  4194. cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
  4195. new_stmt);
  4196. if (new_stmt == gsi_stmt (gsi))
  4197. {
  4198. /* It is okay to check only for the very last
  4199. of these statements. If it is a throwing
  4200. statement nothing will change. If it isn't
  4201. this can remove EH edges. If that weren't
  4202. correct then because some intermediate stmts
  4203. throw, but not the last one. That would mean
  4204. we'd have to split the block, which we can't
  4205. here and we'd loose anyway. And as builtins
  4206. probably never throw, this all
  4207. is mood anyway. */
  4208. if (maybe_clean_or_replace_eh_stmt (old_stmt,
  4209. new_stmt))
  4210. gimple_purge_dead_eh_edges (
  4211. BASIC_BLOCK_FOR_FN (cfun, first));
  4212. break;
  4213. }
  4214. gsi_next (&i2);
  4215. }
  4216. }
  4217. }
  4218. else if (fold_stmt (&gsi))
  4219. {
  4220. /* Re-read the statement from GSI as fold_stmt() may
  4221. have changed it. */
  4222. gimple new_stmt = gsi_stmt (gsi);
  4223. update_stmt (new_stmt);
  4224. if (is_gimple_call (old_stmt)
  4225. || is_gimple_call (new_stmt))
  4226. cgraph_update_edges_for_call_stmt (old_stmt, old_decl,
  4227. new_stmt);
  4228. if (maybe_clean_or_replace_eh_stmt (old_stmt, new_stmt))
  4229. gimple_purge_dead_eh_edges (BASIC_BLOCK_FOR_FN (cfun,
  4230. first));
  4231. }
  4232. }
  4233. }
  4234. }
  4235. /* Expand calls to inline functions in the body of FN. */
  4236. unsigned int
  4237. optimize_inline_calls (tree fn)
  4238. {
  4239. copy_body_data id;
  4240. basic_block bb;
  4241. int last = n_basic_blocks_for_fn (cfun);
  4242. bool inlined_p = false;
  4243. /* Clear out ID. */
  4244. memset (&id, 0, sizeof (id));
  4245. id.src_node = id.dst_node = cgraph_node::get (fn);
  4246. gcc_assert (id.dst_node->definition);
  4247. id.dst_fn = fn;
  4248. /* Or any functions that aren't finished yet. */
  4249. if (current_function_decl)
  4250. id.dst_fn = current_function_decl;
  4251. id.copy_decl = copy_decl_maybe_to_var;
  4252. id.transform_call_graph_edges = CB_CGE_DUPLICATE;
  4253. id.transform_new_cfg = false;
  4254. id.transform_return_to_modify = true;
  4255. id.transform_parameter = true;
  4256. id.transform_lang_insert_block = NULL;
  4257. id.statements_to_fold = new hash_set<gimple>;
  4258. push_gimplify_context ();
  4259. /* We make no attempts to keep dominance info up-to-date. */
  4260. free_dominance_info (CDI_DOMINATORS);
  4261. free_dominance_info (CDI_POST_DOMINATORS);
  4262. /* Register specific gimple functions. */
  4263. gimple_register_cfg_hooks ();
  4264. /* Reach the trees by walking over the CFG, and note the
  4265. enclosing basic-blocks in the call edges. */
  4266. /* We walk the blocks going forward, because inlined function bodies
  4267. will split id->current_basic_block, and the new blocks will
  4268. follow it; we'll trudge through them, processing their CALL_EXPRs
  4269. along the way. */
  4270. FOR_EACH_BB_FN (bb, cfun)
  4271. inlined_p |= gimple_expand_calls_inline (bb, &id);
  4272. pop_gimplify_context (NULL);
  4273. #ifdef ENABLE_CHECKING
  4274. {
  4275. struct cgraph_edge *e;
  4276. id.dst_node->verify ();
  4277. /* Double check that we inlined everything we are supposed to inline. */
  4278. for (e = id.dst_node->callees; e; e = e->next_callee)
  4279. gcc_assert (e->inline_failed);
  4280. }
  4281. #endif
  4282. /* Fold queued statements. */
  4283. fold_marked_statements (last, id.statements_to_fold);
  4284. delete id.statements_to_fold;
  4285. gcc_assert (!id.debug_stmts.exists ());
  4286. /* If we didn't inline into the function there is nothing to do. */
  4287. if (!inlined_p)
  4288. return 0;
  4289. /* Renumber the lexical scoping (non-code) blocks consecutively. */
  4290. number_blocks (fn);
  4291. delete_unreachable_blocks_update_callgraph (&id);
  4292. #ifdef ENABLE_CHECKING
  4293. id.dst_node->verify ();
  4294. #endif
  4295. /* It would be nice to check SSA/CFG/statement consistency here, but it is
  4296. not possible yet - the IPA passes might make various functions to not
  4297. throw and they don't care to proactively update local EH info. This is
  4298. done later in fixup_cfg pass that also execute the verification. */
  4299. return (TODO_update_ssa
  4300. | TODO_cleanup_cfg
  4301. | (gimple_in_ssa_p (cfun) ? TODO_remove_unused_locals : 0)
  4302. | (gimple_in_ssa_p (cfun) ? TODO_update_address_taken : 0)
  4303. | (profile_status_for_fn (cfun) != PROFILE_ABSENT
  4304. ? TODO_rebuild_frequencies : 0));
  4305. }
  4306. /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
  4307. tree
  4308. copy_tree_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
  4309. {
  4310. enum tree_code code = TREE_CODE (*tp);
  4311. enum tree_code_class cl = TREE_CODE_CLASS (code);
  4312. /* We make copies of most nodes. */
  4313. if (IS_EXPR_CODE_CLASS (cl)
  4314. || code == TREE_LIST
  4315. || code == TREE_VEC
  4316. || code == TYPE_DECL
  4317. || code == OMP_CLAUSE)
  4318. {
  4319. /* Because the chain gets clobbered when we make a copy, we save it
  4320. here. */
  4321. tree chain = NULL_TREE, new_tree;
  4322. if (CODE_CONTAINS_STRUCT (code, TS_COMMON))
  4323. chain = TREE_CHAIN (*tp);
  4324. /* Copy the node. */
  4325. new_tree = copy_node (*tp);
  4326. *tp = new_tree;
  4327. /* Now, restore the chain, if appropriate. That will cause
  4328. walk_tree to walk into the chain as well. */
  4329. if (code == PARM_DECL
  4330. || code == TREE_LIST
  4331. || code == OMP_CLAUSE)
  4332. TREE_CHAIN (*tp) = chain;
  4333. /* For now, we don't update BLOCKs when we make copies. So, we
  4334. have to nullify all BIND_EXPRs. */
  4335. if (TREE_CODE (*tp) == BIND_EXPR)
  4336. BIND_EXPR_BLOCK (*tp) = NULL_TREE;
  4337. }
  4338. else if (code == CONSTRUCTOR)
  4339. {
  4340. /* CONSTRUCTOR nodes need special handling because
  4341. we need to duplicate the vector of elements. */
  4342. tree new_tree;
  4343. new_tree = copy_node (*tp);
  4344. CONSTRUCTOR_ELTS (new_tree) = vec_safe_copy (CONSTRUCTOR_ELTS (*tp));
  4345. *tp = new_tree;
  4346. }
  4347. else if (code == STATEMENT_LIST)
  4348. /* We used to just abort on STATEMENT_LIST, but we can run into them
  4349. with statement-expressions (c++/40975). */
  4350. copy_statement_list (tp);
  4351. else if (TREE_CODE_CLASS (code) == tcc_type)
  4352. *walk_subtrees = 0;
  4353. else if (TREE_CODE_CLASS (code) == tcc_declaration)
  4354. *walk_subtrees = 0;
  4355. else if (TREE_CODE_CLASS (code) == tcc_constant)
  4356. *walk_subtrees = 0;
  4357. return NULL_TREE;
  4358. }
  4359. /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
  4360. information indicating to what new SAVE_EXPR this one should be mapped,
  4361. use that one. Otherwise, create a new node and enter it in ST. FN is
  4362. the function into which the copy will be placed. */
  4363. static void
  4364. remap_save_expr (tree *tp, hash_map<tree, tree> *st, int *walk_subtrees)
  4365. {
  4366. tree *n;
  4367. tree t;
  4368. /* See if we already encountered this SAVE_EXPR. */
  4369. n = st->get (*tp);
  4370. /* If we didn't already remap this SAVE_EXPR, do so now. */
  4371. if (!n)
  4372. {
  4373. t = copy_node (*tp);
  4374. /* Remember this SAVE_EXPR. */
  4375. st->put (*tp, t);
  4376. /* Make sure we don't remap an already-remapped SAVE_EXPR. */
  4377. st->put (t, t);
  4378. }
  4379. else
  4380. {
  4381. /* We've already walked into this SAVE_EXPR; don't do it again. */
  4382. *walk_subtrees = 0;
  4383. t = *n;
  4384. }
  4385. /* Replace this SAVE_EXPR with the copy. */
  4386. *tp = t;
  4387. }
  4388. /* Called via walk_gimple_seq. If *GSIP points to a GIMPLE_LABEL for a local
  4389. label, copies the declaration and enters it in the splay_tree in DATA (which
  4390. is really a 'copy_body_data *'. */
  4391. static tree
  4392. mark_local_labels_stmt (gimple_stmt_iterator *gsip,
  4393. bool *handled_ops_p ATTRIBUTE_UNUSED,
  4394. struct walk_stmt_info *wi)
  4395. {
  4396. copy_body_data *id = (copy_body_data *) wi->info;
  4397. glabel *stmt = dyn_cast <glabel *> (gsi_stmt (*gsip));
  4398. if (stmt)
  4399. {
  4400. tree decl = gimple_label_label (stmt);
  4401. /* Copy the decl and remember the copy. */
  4402. insert_decl_map (id, decl, id->copy_decl (decl, id));
  4403. }
  4404. return NULL_TREE;
  4405. }
  4406. /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
  4407. Using the splay_tree pointed to by ST (which is really a `splay_tree'),
  4408. remaps all local declarations to appropriate replacements in gimple
  4409. operands. */
  4410. static tree
  4411. replace_locals_op (tree *tp, int *walk_subtrees, void *data)
  4412. {
  4413. struct walk_stmt_info *wi = (struct walk_stmt_info*) data;
  4414. copy_body_data *id = (copy_body_data *) wi->info;
  4415. hash_map<tree, tree> *st = id->decl_map;
  4416. tree *n;
  4417. tree expr = *tp;
  4418. /* Only a local declaration (variable or label). */
  4419. if ((TREE_CODE (expr) == VAR_DECL
  4420. && !TREE_STATIC (expr))
  4421. || TREE_CODE (expr) == LABEL_DECL)
  4422. {
  4423. /* Lookup the declaration. */
  4424. n = st->get (expr);
  4425. /* If it's there, remap it. */
  4426. if (n)
  4427. *tp = *n;
  4428. *walk_subtrees = 0;
  4429. }
  4430. else if (TREE_CODE (expr) == STATEMENT_LIST
  4431. || TREE_CODE (expr) == BIND_EXPR
  4432. || TREE_CODE (expr) == SAVE_EXPR)
  4433. gcc_unreachable ();
  4434. else if (TREE_CODE (expr) == TARGET_EXPR)
  4435. {
  4436. /* Don't mess with a TARGET_EXPR that hasn't been expanded.
  4437. It's OK for this to happen if it was part of a subtree that
  4438. isn't immediately expanded, such as operand 2 of another
  4439. TARGET_EXPR. */
  4440. if (!TREE_OPERAND (expr, 1))
  4441. {
  4442. TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
  4443. TREE_OPERAND (expr, 3) = NULL_TREE;
  4444. }
  4445. }
  4446. /* Keep iterating. */
  4447. return NULL_TREE;
  4448. }
  4449. /* Called via walk_gimple_seq by copy_gimple_seq_and_replace_local.
  4450. Using the splay_tree pointed to by ST (which is really a `splay_tree'),
  4451. remaps all local declarations to appropriate replacements in gimple
  4452. statements. */
  4453. static tree
  4454. replace_locals_stmt (gimple_stmt_iterator *gsip,
  4455. bool *handled_ops_p ATTRIBUTE_UNUSED,
  4456. struct walk_stmt_info *wi)
  4457. {
  4458. copy_body_data *id = (copy_body_data *) wi->info;
  4459. gimple gs = gsi_stmt (*gsip);
  4460. if (gbind *stmt = dyn_cast <gbind *> (gs))
  4461. {
  4462. tree block = gimple_bind_block (stmt);
  4463. if (block)
  4464. {
  4465. remap_block (&block, id);
  4466. gimple_bind_set_block (stmt, block);
  4467. }
  4468. /* This will remap a lot of the same decls again, but this should be
  4469. harmless. */
  4470. if (gimple_bind_vars (stmt))
  4471. gimple_bind_set_vars (stmt, remap_decls (gimple_bind_vars (stmt),
  4472. NULL, id));
  4473. }
  4474. /* Keep iterating. */
  4475. return NULL_TREE;
  4476. }
  4477. /* Copies everything in SEQ and replaces variables and labels local to
  4478. current_function_decl. */
  4479. gimple_seq
  4480. copy_gimple_seq_and_replace_locals (gimple_seq seq)
  4481. {
  4482. copy_body_data id;
  4483. struct walk_stmt_info wi;
  4484. gimple_seq copy;
  4485. /* There's nothing to do for NULL_TREE. */
  4486. if (seq == NULL)
  4487. return seq;
  4488. /* Set up ID. */
  4489. memset (&id, 0, sizeof (id));
  4490. id.src_fn = current_function_decl;
  4491. id.dst_fn = current_function_decl;
  4492. id.decl_map = new hash_map<tree, tree>;
  4493. id.debug_map = NULL;
  4494. id.copy_decl = copy_decl_no_change;
  4495. id.transform_call_graph_edges = CB_CGE_DUPLICATE;
  4496. id.transform_new_cfg = false;
  4497. id.transform_return_to_modify = false;
  4498. id.transform_parameter = false;
  4499. id.transform_lang_insert_block = NULL;
  4500. /* Walk the tree once to find local labels. */
  4501. memset (&wi, 0, sizeof (wi));
  4502. hash_set<tree> visited;
  4503. wi.info = &id;
  4504. wi.pset = &visited;
  4505. walk_gimple_seq (seq, mark_local_labels_stmt, NULL, &wi);
  4506. copy = gimple_seq_copy (seq);
  4507. /* Walk the copy, remapping decls. */
  4508. memset (&wi, 0, sizeof (wi));
  4509. wi.info = &id;
  4510. walk_gimple_seq (copy, replace_locals_stmt, replace_locals_op, &wi);
  4511. /* Clean up. */
  4512. delete id.decl_map;
  4513. if (id.debug_map)
  4514. delete id.debug_map;
  4515. if (id.dependence_map)
  4516. {
  4517. delete id.dependence_map;
  4518. id.dependence_map = NULL;
  4519. }
  4520. return copy;
  4521. }
  4522. /* Allow someone to determine if SEARCH is a child of TOP from gdb. */
  4523. static tree
  4524. debug_find_tree_1 (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED, void *data)
  4525. {
  4526. if (*tp == data)
  4527. return (tree) data;
  4528. else
  4529. return NULL;
  4530. }
  4531. DEBUG_FUNCTION bool
  4532. debug_find_tree (tree top, tree search)
  4533. {
  4534. return walk_tree_without_duplicates (&top, debug_find_tree_1, search) != 0;
  4535. }
  4536. /* Declare the variables created by the inliner. Add all the variables in
  4537. VARS to BIND_EXPR. */
  4538. static void
  4539. declare_inline_vars (tree block, tree vars)
  4540. {
  4541. tree t;
  4542. for (t = vars; t; t = DECL_CHAIN (t))
  4543. {
  4544. DECL_SEEN_IN_BIND_EXPR_P (t) = 1;
  4545. gcc_assert (!TREE_STATIC (t) && !TREE_ASM_WRITTEN (t));
  4546. add_local_decl (cfun, t);
  4547. }
  4548. if (block)
  4549. BLOCK_VARS (block) = chainon (BLOCK_VARS (block), vars);
  4550. }
  4551. /* Copy NODE (which must be a DECL). The DECL originally was in the FROM_FN,
  4552. but now it will be in the TO_FN. PARM_TO_VAR means enable PARM_DECL to
  4553. VAR_DECL translation. */
  4554. static tree
  4555. copy_decl_for_dup_finish (copy_body_data *id, tree decl, tree copy)
  4556. {
  4557. /* Don't generate debug information for the copy if we wouldn't have
  4558. generated it for the copy either. */
  4559. DECL_ARTIFICIAL (copy) = DECL_ARTIFICIAL (decl);
  4560. DECL_IGNORED_P (copy) = DECL_IGNORED_P (decl);
  4561. /* Set the DECL_ABSTRACT_ORIGIN so the debugging routines know what
  4562. declaration inspired this copy. */
  4563. DECL_ABSTRACT_ORIGIN (copy) = DECL_ORIGIN (decl);
  4564. /* The new variable/label has no RTL, yet. */
  4565. if (CODE_CONTAINS_STRUCT (TREE_CODE (copy), TS_DECL_WRTL)
  4566. && !TREE_STATIC (copy) && !DECL_EXTERNAL (copy))
  4567. SET_DECL_RTL (copy, 0);
  4568. /* These args would always appear unused, if not for this. */
  4569. TREE_USED (copy) = 1;
  4570. /* Set the context for the new declaration. */
  4571. if (!DECL_CONTEXT (decl))
  4572. /* Globals stay global. */
  4573. ;
  4574. else if (DECL_CONTEXT (decl) != id->src_fn)
  4575. /* Things that weren't in the scope of the function we're inlining
  4576. from aren't in the scope we're inlining to, either. */
  4577. ;
  4578. else if (TREE_STATIC (decl))
  4579. /* Function-scoped static variables should stay in the original
  4580. function. */
  4581. ;
  4582. else
  4583. /* Ordinary automatic local variables are now in the scope of the
  4584. new function. */
  4585. DECL_CONTEXT (copy) = id->dst_fn;
  4586. return copy;
  4587. }
  4588. static tree
  4589. copy_decl_to_var (tree decl, copy_body_data *id)
  4590. {
  4591. tree copy, type;
  4592. gcc_assert (TREE_CODE (decl) == PARM_DECL
  4593. || TREE_CODE (decl) == RESULT_DECL);
  4594. type = TREE_TYPE (decl);
  4595. copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
  4596. VAR_DECL, DECL_NAME (decl), type);
  4597. if (DECL_PT_UID_SET_P (decl))
  4598. SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
  4599. TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
  4600. TREE_READONLY (copy) = TREE_READONLY (decl);
  4601. TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
  4602. DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
  4603. return copy_decl_for_dup_finish (id, decl, copy);
  4604. }
  4605. /* Like copy_decl_to_var, but create a return slot object instead of a
  4606. pointer variable for return by invisible reference. */
  4607. static tree
  4608. copy_result_decl_to_var (tree decl, copy_body_data *id)
  4609. {
  4610. tree copy, type;
  4611. gcc_assert (TREE_CODE (decl) == PARM_DECL
  4612. || TREE_CODE (decl) == RESULT_DECL);
  4613. type = TREE_TYPE (decl);
  4614. if (DECL_BY_REFERENCE (decl))
  4615. type = TREE_TYPE (type);
  4616. copy = build_decl (DECL_SOURCE_LOCATION (id->dst_fn),
  4617. VAR_DECL, DECL_NAME (decl), type);
  4618. if (DECL_PT_UID_SET_P (decl))
  4619. SET_DECL_PT_UID (copy, DECL_PT_UID (decl));
  4620. TREE_READONLY (copy) = TREE_READONLY (decl);
  4621. TREE_THIS_VOLATILE (copy) = TREE_THIS_VOLATILE (decl);
  4622. if (!DECL_BY_REFERENCE (decl))
  4623. {
  4624. TREE_ADDRESSABLE (copy) = TREE_ADDRESSABLE (decl);
  4625. DECL_GIMPLE_REG_P (copy) = DECL_GIMPLE_REG_P (decl);
  4626. }
  4627. return copy_decl_for_dup_finish (id, decl, copy);
  4628. }
  4629. tree
  4630. copy_decl_no_change (tree decl, copy_body_data *id)
  4631. {
  4632. tree copy;
  4633. copy = copy_node (decl);
  4634. /* The COPY is not abstract; it will be generated in DST_FN. */
  4635. DECL_ABSTRACT_P (copy) = false;
  4636. lang_hooks.dup_lang_specific_decl (copy);
  4637. /* TREE_ADDRESSABLE isn't used to indicate that a label's address has
  4638. been taken; it's for internal bookkeeping in expand_goto_internal. */
  4639. if (TREE_CODE (copy) == LABEL_DECL)
  4640. {
  4641. TREE_ADDRESSABLE (copy) = 0;
  4642. LABEL_DECL_UID (copy) = -1;
  4643. }
  4644. return copy_decl_for_dup_finish (id, decl, copy);
  4645. }
  4646. static tree
  4647. copy_decl_maybe_to_var (tree decl, copy_body_data *id)
  4648. {
  4649. if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL)
  4650. return copy_decl_to_var (decl, id);
  4651. else
  4652. return copy_decl_no_change (decl, id);
  4653. }
  4654. /* Return a copy of the function's argument tree. */
  4655. static tree
  4656. copy_arguments_for_versioning (tree orig_parm, copy_body_data * id,
  4657. bitmap args_to_skip, tree *vars)
  4658. {
  4659. tree arg, *parg;
  4660. tree new_parm = NULL;
  4661. int i = 0;
  4662. parg = &new_parm;
  4663. for (arg = orig_parm; arg; arg = DECL_CHAIN (arg), i++)
  4664. if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
  4665. {
  4666. tree new_tree = remap_decl (arg, id);
  4667. if (TREE_CODE (new_tree) != PARM_DECL)
  4668. new_tree = id->copy_decl (arg, id);
  4669. lang_hooks.dup_lang_specific_decl (new_tree);
  4670. *parg = new_tree;
  4671. parg = &DECL_CHAIN (new_tree);
  4672. }
  4673. else if (!id->decl_map->get (arg))
  4674. {
  4675. /* Make an equivalent VAR_DECL. If the argument was used
  4676. as temporary variable later in function, the uses will be
  4677. replaced by local variable. */
  4678. tree var = copy_decl_to_var (arg, id);
  4679. insert_decl_map (id, arg, var);
  4680. /* Declare this new variable. */
  4681. DECL_CHAIN (var) = *vars;
  4682. *vars = var;
  4683. }
  4684. return new_parm;
  4685. }
  4686. /* Return a copy of the function's static chain. */
  4687. static tree
  4688. copy_static_chain (tree static_chain, copy_body_data * id)
  4689. {
  4690. tree *chain_copy, *pvar;
  4691. chain_copy = &static_chain;
  4692. for (pvar = chain_copy; *pvar; pvar = &DECL_CHAIN (*pvar))
  4693. {
  4694. tree new_tree = remap_decl (*pvar, id);
  4695. lang_hooks.dup_lang_specific_decl (new_tree);
  4696. DECL_CHAIN (new_tree) = DECL_CHAIN (*pvar);
  4697. *pvar = new_tree;
  4698. }
  4699. return static_chain;
  4700. }
  4701. /* Return true if the function is allowed to be versioned.
  4702. This is a guard for the versioning functionality. */
  4703. bool
  4704. tree_versionable_function_p (tree fndecl)
  4705. {
  4706. return (!lookup_attribute ("noclone", DECL_ATTRIBUTES (fndecl))
  4707. && copy_forbidden (DECL_STRUCT_FUNCTION (fndecl), fndecl) == NULL);
  4708. }
  4709. /* Delete all unreachable basic blocks and update callgraph.
  4710. Doing so is somewhat nontrivial because we need to update all clones and
  4711. remove inline function that become unreachable. */
  4712. static bool
  4713. delete_unreachable_blocks_update_callgraph (copy_body_data *id)
  4714. {
  4715. bool changed = false;
  4716. basic_block b, next_bb;
  4717. find_unreachable_blocks ();
  4718. /* Delete all unreachable basic blocks. */
  4719. for (b = ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb; b
  4720. != EXIT_BLOCK_PTR_FOR_FN (cfun); b = next_bb)
  4721. {
  4722. next_bb = b->next_bb;
  4723. if (!(b->flags & BB_REACHABLE))
  4724. {
  4725. gimple_stmt_iterator bsi;
  4726. for (bsi = gsi_start_bb (b); !gsi_end_p (bsi); gsi_next (&bsi))
  4727. {
  4728. struct cgraph_edge *e;
  4729. struct cgraph_node *node;
  4730. id->dst_node->remove_stmt_references (gsi_stmt (bsi));
  4731. if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
  4732. &&(e = id->dst_node->get_edge (gsi_stmt (bsi))) != NULL)
  4733. {
  4734. if (!e->inline_failed)
  4735. e->callee->remove_symbol_and_inline_clones (id->dst_node);
  4736. else
  4737. e->remove ();
  4738. }
  4739. if (id->transform_call_graph_edges == CB_CGE_MOVE_CLONES
  4740. && id->dst_node->clones)
  4741. for (node = id->dst_node->clones; node != id->dst_node;)
  4742. {
  4743. node->remove_stmt_references (gsi_stmt (bsi));
  4744. if (gimple_code (gsi_stmt (bsi)) == GIMPLE_CALL
  4745. && (e = node->get_edge (gsi_stmt (bsi))) != NULL)
  4746. {
  4747. if (!e->inline_failed)
  4748. e->callee->remove_symbol_and_inline_clones (id->dst_node);
  4749. else
  4750. e->remove ();
  4751. }
  4752. if (node->clones)
  4753. node = node->clones;
  4754. else if (node->next_sibling_clone)
  4755. node = node->next_sibling_clone;
  4756. else
  4757. {
  4758. while (node != id->dst_node && !node->next_sibling_clone)
  4759. node = node->clone_of;
  4760. if (node != id->dst_node)
  4761. node = node->next_sibling_clone;
  4762. }
  4763. }
  4764. }
  4765. delete_basic_block (b);
  4766. changed = true;
  4767. }
  4768. }
  4769. return changed;
  4770. }
  4771. /* Update clone info after duplication. */
  4772. static void
  4773. update_clone_info (copy_body_data * id)
  4774. {
  4775. struct cgraph_node *node;
  4776. if (!id->dst_node->clones)
  4777. return;
  4778. for (node = id->dst_node->clones; node != id->dst_node;)
  4779. {
  4780. /* First update replace maps to match the new body. */
  4781. if (node->clone.tree_map)
  4782. {
  4783. unsigned int i;
  4784. for (i = 0; i < vec_safe_length (node->clone.tree_map); i++)
  4785. {
  4786. struct ipa_replace_map *replace_info;
  4787. replace_info = (*node->clone.tree_map)[i];
  4788. walk_tree (&replace_info->old_tree, copy_tree_body_r, id, NULL);
  4789. walk_tree (&replace_info->new_tree, copy_tree_body_r, id, NULL);
  4790. }
  4791. }
  4792. if (node->clones)
  4793. node = node->clones;
  4794. else if (node->next_sibling_clone)
  4795. node = node->next_sibling_clone;
  4796. else
  4797. {
  4798. while (node != id->dst_node && !node->next_sibling_clone)
  4799. node = node->clone_of;
  4800. if (node != id->dst_node)
  4801. node = node->next_sibling_clone;
  4802. }
  4803. }
  4804. }
  4805. /* Create a copy of a function's tree.
  4806. OLD_DECL and NEW_DECL are FUNCTION_DECL tree nodes
  4807. of the original function and the new copied function
  4808. respectively. In case we want to replace a DECL
  4809. tree with another tree while duplicating the function's
  4810. body, TREE_MAP represents the mapping between these
  4811. trees. If UPDATE_CLONES is set, the call_stmt fields
  4812. of edges of clones of the function will be updated.
  4813. If non-NULL ARGS_TO_SKIP determine function parameters to remove
  4814. from new version.
  4815. If SKIP_RETURN is true, the new version will return void.
  4816. If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
  4817. If non_NULL NEW_ENTRY determine new entry BB of the clone.
  4818. */
  4819. void
  4820. tree_function_versioning (tree old_decl, tree new_decl,
  4821. vec<ipa_replace_map *, va_gc> *tree_map,
  4822. bool update_clones, bitmap args_to_skip,
  4823. bool skip_return, bitmap blocks_to_copy,
  4824. basic_block new_entry)
  4825. {
  4826. struct cgraph_node *old_version_node;
  4827. struct cgraph_node *new_version_node;
  4828. copy_body_data id;
  4829. tree p;
  4830. unsigned i;
  4831. struct ipa_replace_map *replace_info;
  4832. basic_block old_entry_block, bb;
  4833. auto_vec<gimple, 10> init_stmts;
  4834. tree vars = NULL_TREE;
  4835. gcc_assert (TREE_CODE (old_decl) == FUNCTION_DECL
  4836. && TREE_CODE (new_decl) == FUNCTION_DECL);
  4837. DECL_POSSIBLY_INLINED (old_decl) = 1;
  4838. old_version_node = cgraph_node::get (old_decl);
  4839. gcc_checking_assert (old_version_node);
  4840. new_version_node = cgraph_node::get (new_decl);
  4841. gcc_checking_assert (new_version_node);
  4842. /* Copy over debug args. */
  4843. if (DECL_HAS_DEBUG_ARGS_P (old_decl))
  4844. {
  4845. vec<tree, va_gc> **new_debug_args, **old_debug_args;
  4846. gcc_checking_assert (decl_debug_args_lookup (new_decl) == NULL);
  4847. DECL_HAS_DEBUG_ARGS_P (new_decl) = 0;
  4848. old_debug_args = decl_debug_args_lookup (old_decl);
  4849. if (old_debug_args)
  4850. {
  4851. new_debug_args = decl_debug_args_insert (new_decl);
  4852. *new_debug_args = vec_safe_copy (*old_debug_args);
  4853. }
  4854. }
  4855. /* Output the inlining info for this abstract function, since it has been
  4856. inlined. If we don't do this now, we can lose the information about the
  4857. variables in the function when the blocks get blown away as soon as we
  4858. remove the cgraph node. */
  4859. (*debug_hooks->outlining_inline_function) (old_decl);
  4860. DECL_ARTIFICIAL (new_decl) = 1;
  4861. DECL_ABSTRACT_ORIGIN (new_decl) = DECL_ORIGIN (old_decl);
  4862. if (DECL_ORIGIN (old_decl) == old_decl)
  4863. old_version_node->used_as_abstract_origin = true;
  4864. DECL_FUNCTION_PERSONALITY (new_decl) = DECL_FUNCTION_PERSONALITY (old_decl);
  4865. /* Prepare the data structures for the tree copy. */
  4866. memset (&id, 0, sizeof (id));
  4867. /* Generate a new name for the new version. */
  4868. id.statements_to_fold = new hash_set<gimple>;
  4869. id.decl_map = new hash_map<tree, tree>;
  4870. id.debug_map = NULL;
  4871. id.src_fn = old_decl;
  4872. id.dst_fn = new_decl;
  4873. id.src_node = old_version_node;
  4874. id.dst_node = new_version_node;
  4875. id.src_cfun = DECL_STRUCT_FUNCTION (old_decl);
  4876. id.blocks_to_copy = blocks_to_copy;
  4877. id.copy_decl = copy_decl_no_change;
  4878. id.transform_call_graph_edges
  4879. = update_clones ? CB_CGE_MOVE_CLONES : CB_CGE_MOVE;
  4880. id.transform_new_cfg = true;
  4881. id.transform_return_to_modify = false;
  4882. id.transform_parameter = false;
  4883. id.transform_lang_insert_block = NULL;
  4884. old_entry_block = ENTRY_BLOCK_PTR_FOR_FN
  4885. (DECL_STRUCT_FUNCTION (old_decl));
  4886. DECL_RESULT (new_decl) = DECL_RESULT (old_decl);
  4887. DECL_ARGUMENTS (new_decl) = DECL_ARGUMENTS (old_decl);
  4888. initialize_cfun (new_decl, old_decl,
  4889. old_entry_block->count);
  4890. if (DECL_STRUCT_FUNCTION (new_decl)->gimple_df)
  4891. DECL_STRUCT_FUNCTION (new_decl)->gimple_df->ipa_pta
  4892. = id.src_cfun->gimple_df->ipa_pta;
  4893. /* Copy the function's static chain. */
  4894. p = DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl;
  4895. if (p)
  4896. DECL_STRUCT_FUNCTION (new_decl)->static_chain_decl =
  4897. copy_static_chain (DECL_STRUCT_FUNCTION (old_decl)->static_chain_decl,
  4898. &id);
  4899. /* If there's a tree_map, prepare for substitution. */
  4900. if (tree_map)
  4901. for (i = 0; i < tree_map->length (); i++)
  4902. {
  4903. gimple init;
  4904. replace_info = (*tree_map)[i];
  4905. if (replace_info->replace_p)
  4906. {
  4907. if (!replace_info->old_tree)
  4908. {
  4909. int i = replace_info->parm_num;
  4910. tree parm;
  4911. tree req_type;
  4912. for (parm = DECL_ARGUMENTS (old_decl); i; parm = DECL_CHAIN (parm))
  4913. i --;
  4914. replace_info->old_tree = parm;
  4915. req_type = TREE_TYPE (parm);
  4916. if (!useless_type_conversion_p (req_type, TREE_TYPE (replace_info->new_tree)))
  4917. {
  4918. if (fold_convertible_p (req_type, replace_info->new_tree))
  4919. replace_info->new_tree = fold_build1 (NOP_EXPR, req_type, replace_info->new_tree);
  4920. else if (TYPE_SIZE (req_type) == TYPE_SIZE (TREE_TYPE (replace_info->new_tree)))
  4921. replace_info->new_tree = fold_build1 (VIEW_CONVERT_EXPR, req_type, replace_info->new_tree);
  4922. else
  4923. {
  4924. if (dump_file)
  4925. {
  4926. fprintf (dump_file, " const ");
  4927. print_generic_expr (dump_file, replace_info->new_tree, 0);
  4928. fprintf (dump_file, " can't be converted to param ");
  4929. print_generic_expr (dump_file, parm, 0);
  4930. fprintf (dump_file, "\n");
  4931. }
  4932. replace_info->old_tree = NULL;
  4933. }
  4934. }
  4935. }
  4936. else
  4937. gcc_assert (TREE_CODE (replace_info->old_tree) == PARM_DECL);
  4938. if (replace_info->old_tree)
  4939. {
  4940. init = setup_one_parameter (&id, replace_info->old_tree,
  4941. replace_info->new_tree, id.src_fn,
  4942. NULL,
  4943. &vars);
  4944. if (init)
  4945. init_stmts.safe_push (init);
  4946. }
  4947. }
  4948. }
  4949. /* Copy the function's arguments. */
  4950. if (DECL_ARGUMENTS (old_decl) != NULL_TREE)
  4951. DECL_ARGUMENTS (new_decl) =
  4952. copy_arguments_for_versioning (DECL_ARGUMENTS (old_decl), &id,
  4953. args_to_skip, &vars);
  4954. DECL_INITIAL (new_decl) = remap_blocks (DECL_INITIAL (id.src_fn), &id);
  4955. BLOCK_SUPERCONTEXT (DECL_INITIAL (new_decl)) = new_decl;
  4956. declare_inline_vars (DECL_INITIAL (new_decl), vars);
  4957. if (!vec_safe_is_empty (DECL_STRUCT_FUNCTION (old_decl)->local_decls))
  4958. /* Add local vars. */
  4959. add_local_variables (DECL_STRUCT_FUNCTION (old_decl), cfun, &id);
  4960. if (DECL_RESULT (old_decl) == NULL_TREE)
  4961. ;
  4962. else if (skip_return && !VOID_TYPE_P (TREE_TYPE (DECL_RESULT (old_decl))))
  4963. {
  4964. DECL_RESULT (new_decl)
  4965. = build_decl (DECL_SOURCE_LOCATION (DECL_RESULT (old_decl)),
  4966. RESULT_DECL, NULL_TREE, void_type_node);
  4967. DECL_CONTEXT (DECL_RESULT (new_decl)) = new_decl;
  4968. cfun->returns_struct = 0;
  4969. cfun->returns_pcc_struct = 0;
  4970. }
  4971. else
  4972. {
  4973. tree old_name;
  4974. DECL_RESULT (new_decl) = remap_decl (DECL_RESULT (old_decl), &id);
  4975. lang_hooks.dup_lang_specific_decl (DECL_RESULT (new_decl));
  4976. if (gimple_in_ssa_p (id.src_cfun)
  4977. && DECL_BY_REFERENCE (DECL_RESULT (old_decl))
  4978. && (old_name = ssa_default_def (id.src_cfun, DECL_RESULT (old_decl))))
  4979. {
  4980. tree new_name = make_ssa_name (DECL_RESULT (new_decl));
  4981. insert_decl_map (&id, old_name, new_name);
  4982. SSA_NAME_DEF_STMT (new_name) = gimple_build_nop ();
  4983. set_ssa_default_def (cfun, DECL_RESULT (new_decl), new_name);
  4984. }
  4985. }
  4986. /* Set up the destination functions loop tree. */
  4987. if (loops_for_fn (DECL_STRUCT_FUNCTION (old_decl)) != NULL)
  4988. {
  4989. cfun->curr_properties &= ~PROP_loops;
  4990. loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
  4991. cfun->curr_properties |= PROP_loops;
  4992. }
  4993. /* Copy the Function's body. */
  4994. copy_body (&id, old_entry_block->count, REG_BR_PROB_BASE,
  4995. ENTRY_BLOCK_PTR_FOR_FN (cfun), EXIT_BLOCK_PTR_FOR_FN (cfun),
  4996. new_entry);
  4997. /* Renumber the lexical scoping (non-code) blocks consecutively. */
  4998. number_blocks (new_decl);
  4999. /* We want to create the BB unconditionally, so that the addition of
  5000. debug stmts doesn't affect BB count, which may in the end cause
  5001. codegen differences. */
  5002. bb = split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
  5003. while (init_stmts.length ())
  5004. insert_init_stmt (&id, bb, init_stmts.pop ());
  5005. update_clone_info (&id);
  5006. /* Remap the nonlocal_goto_save_area, if any. */
  5007. if (cfun->nonlocal_goto_save_area)
  5008. {
  5009. struct walk_stmt_info wi;
  5010. memset (&wi, 0, sizeof (wi));
  5011. wi.info = &id;
  5012. walk_tree (&cfun->nonlocal_goto_save_area, remap_gimple_op_r, &wi, NULL);
  5013. }
  5014. /* Clean up. */
  5015. delete id.decl_map;
  5016. if (id.debug_map)
  5017. delete id.debug_map;
  5018. free_dominance_info (CDI_DOMINATORS);
  5019. free_dominance_info (CDI_POST_DOMINATORS);
  5020. fold_marked_statements (0, id.statements_to_fold);
  5021. delete id.statements_to_fold;
  5022. fold_cond_expr_cond ();
  5023. delete_unreachable_blocks_update_callgraph (&id);
  5024. if (id.dst_node->definition)
  5025. cgraph_edge::rebuild_references ();
  5026. if (loops_state_satisfies_p (LOOPS_NEED_FIXUP))
  5027. {
  5028. calculate_dominance_info (CDI_DOMINATORS);
  5029. fix_loop_structure (NULL);
  5030. }
  5031. update_ssa (TODO_update_ssa);
  5032. /* After partial cloning we need to rescale frequencies, so they are
  5033. within proper range in the cloned function. */
  5034. if (new_entry)
  5035. {
  5036. struct cgraph_edge *e;
  5037. rebuild_frequencies ();
  5038. new_version_node->count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count;
  5039. for (e = new_version_node->callees; e; e = e->next_callee)
  5040. {
  5041. basic_block bb = gimple_bb (e->call_stmt);
  5042. e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
  5043. bb);
  5044. e->count = bb->count;
  5045. }
  5046. for (e = new_version_node->indirect_calls; e; e = e->next_callee)
  5047. {
  5048. basic_block bb = gimple_bb (e->call_stmt);
  5049. e->frequency = compute_call_stmt_bb_frequency (current_function_decl,
  5050. bb);
  5051. e->count = bb->count;
  5052. }
  5053. }
  5054. free_dominance_info (CDI_DOMINATORS);
  5055. free_dominance_info (CDI_POST_DOMINATORS);
  5056. gcc_assert (!id.debug_stmts.exists ());
  5057. pop_cfun ();
  5058. return;
  5059. }
  5060. /* EXP is CALL_EXPR present in a GENERIC expression tree. Try to integrate
  5061. the callee and return the inlined body on success. */
  5062. tree
  5063. maybe_inline_call_in_expr (tree exp)
  5064. {
  5065. tree fn = get_callee_fndecl (exp);
  5066. /* We can only try to inline "const" functions. */
  5067. if (fn && TREE_READONLY (fn) && DECL_SAVED_TREE (fn))
  5068. {
  5069. call_expr_arg_iterator iter;
  5070. copy_body_data id;
  5071. tree param, arg, t;
  5072. hash_map<tree, tree> decl_map;
  5073. /* Remap the parameters. */
  5074. for (param = DECL_ARGUMENTS (fn), arg = first_call_expr_arg (exp, &iter);
  5075. param;
  5076. param = DECL_CHAIN (param), arg = next_call_expr_arg (&iter))
  5077. decl_map.put (param, arg);
  5078. memset (&id, 0, sizeof (id));
  5079. id.src_fn = fn;
  5080. id.dst_fn = current_function_decl;
  5081. id.src_cfun = DECL_STRUCT_FUNCTION (fn);
  5082. id.decl_map = &decl_map;
  5083. id.copy_decl = copy_decl_no_change;
  5084. id.transform_call_graph_edges = CB_CGE_DUPLICATE;
  5085. id.transform_new_cfg = false;
  5086. id.transform_return_to_modify = true;
  5087. id.transform_parameter = true;
  5088. id.transform_lang_insert_block = NULL;
  5089. /* Make sure not to unshare trees behind the front-end's back
  5090. since front-end specific mechanisms may rely on sharing. */
  5091. id.regimplify = false;
  5092. id.do_not_unshare = true;
  5093. /* We're not inside any EH region. */
  5094. id.eh_lp_nr = 0;
  5095. t = copy_tree_body (&id);
  5096. /* We can only return something suitable for use in a GENERIC
  5097. expression tree. */
  5098. if (TREE_CODE (t) == MODIFY_EXPR)
  5099. return TREE_OPERAND (t, 1);
  5100. }
  5101. return NULL_TREE;
  5102. }
  5103. /* Duplicate a type, fields and all. */
  5104. tree
  5105. build_duplicate_type (tree type)
  5106. {
  5107. struct copy_body_data id;
  5108. memset (&id, 0, sizeof (id));
  5109. id.src_fn = current_function_decl;
  5110. id.dst_fn = current_function_decl;
  5111. id.src_cfun = cfun;
  5112. id.decl_map = new hash_map<tree, tree>;
  5113. id.debug_map = NULL;
  5114. id.copy_decl = copy_decl_no_change;
  5115. type = remap_type_1 (type, &id);
  5116. delete id.decl_map;
  5117. if (id.debug_map)
  5118. delete id.debug_map;
  5119. TYPE_CANONICAL (type) = type;
  5120. return type;
  5121. }
  5122. /* Unshare the entire DECL_SAVED_TREE of FN and return the remapped
  5123. parameters and RESULT_DECL in PARMS and RESULT. Used by C++ constexpr
  5124. evaluation. */
  5125. tree
  5126. copy_fn (tree fn, tree& parms, tree& result)
  5127. {
  5128. copy_body_data id;
  5129. tree param;
  5130. hash_map<tree, tree> decl_map;
  5131. tree *p = &parms;
  5132. *p = NULL_TREE;
  5133. memset (&id, 0, sizeof (id));
  5134. id.src_fn = fn;
  5135. id.dst_fn = current_function_decl;
  5136. id.src_cfun = DECL_STRUCT_FUNCTION (fn);
  5137. id.decl_map = &decl_map;
  5138. id.copy_decl = copy_decl_no_change;
  5139. id.transform_call_graph_edges = CB_CGE_DUPLICATE;
  5140. id.transform_new_cfg = false;
  5141. id.transform_return_to_modify = false;
  5142. id.transform_parameter = true;
  5143. id.transform_lang_insert_block = NULL;
  5144. /* Make sure not to unshare trees behind the front-end's back
  5145. since front-end specific mechanisms may rely on sharing. */
  5146. id.regimplify = false;
  5147. id.do_not_unshare = true;
  5148. /* We're not inside any EH region. */
  5149. id.eh_lp_nr = 0;
  5150. /* Remap the parameters and result and return them to the caller. */
  5151. for (param = DECL_ARGUMENTS (fn);
  5152. param;
  5153. param = DECL_CHAIN (param))
  5154. {
  5155. *p = remap_decl (param, &id);
  5156. p = &DECL_CHAIN (*p);
  5157. }
  5158. if (DECL_RESULT (fn))
  5159. result = remap_decl (DECL_RESULT (fn), &id);
  5160. else
  5161. result = NULL_TREE;
  5162. return copy_tree_body (&id);
  5163. }