AttributorAttributes.cpp 432 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700
  1. //===- AttributorAttributes.cpp - Attributes for Attributor deduction -----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // See the Attributor.h file comment and the class descriptions in that file for
  10. // more information.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/IPO/Attributor.h"
  14. #include "llvm/ADT/APInt.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/MapVector.h"
  18. #include "llvm/ADT/SCCIterator.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SetOperations.h"
  21. #include "llvm/ADT/SetVector.h"
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/Statistic.h"
  25. #include "llvm/Analysis/AliasAnalysis.h"
  26. #include "llvm/Analysis/AssumeBundleQueries.h"
  27. #include "llvm/Analysis/AssumptionCache.h"
  28. #include "llvm/Analysis/CaptureTracking.h"
  29. #include "llvm/Analysis/CycleAnalysis.h"
  30. #include "llvm/Analysis/InstructionSimplify.h"
  31. #include "llvm/Analysis/LazyValueInfo.h"
  32. #include "llvm/Analysis/MemoryBuiltins.h"
  33. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  34. #include "llvm/Analysis/ScalarEvolution.h"
  35. #include "llvm/Analysis/TargetTransformInfo.h"
  36. #include "llvm/Analysis/ValueTracking.h"
  37. #include "llvm/IR/Argument.h"
  38. #include "llvm/IR/Assumptions.h"
  39. #include "llvm/IR/BasicBlock.h"
  40. #include "llvm/IR/Constant.h"
  41. #include "llvm/IR/Constants.h"
  42. #include "llvm/IR/DataLayout.h"
  43. #include "llvm/IR/DerivedTypes.h"
  44. #include "llvm/IR/GlobalValue.h"
  45. #include "llvm/IR/IRBuilder.h"
  46. #include "llvm/IR/InlineAsm.h"
  47. #include "llvm/IR/InstrTypes.h"
  48. #include "llvm/IR/Instruction.h"
  49. #include "llvm/IR/Instructions.h"
  50. #include "llvm/IR/IntrinsicInst.h"
  51. #include "llvm/IR/IntrinsicsAMDGPU.h"
  52. #include "llvm/IR/IntrinsicsNVPTX.h"
  53. #include "llvm/IR/NoFolder.h"
  54. #include "llvm/IR/Value.h"
  55. #include "llvm/IR/ValueHandle.h"
  56. #include "llvm/Support/Alignment.h"
  57. #include "llvm/Support/Casting.h"
  58. #include "llvm/Support/CommandLine.h"
  59. #include "llvm/Support/ErrorHandling.h"
  60. #include "llvm/Support/GraphWriter.h"
  61. #include "llvm/Support/MathExtras.h"
  62. #include "llvm/Support/raw_ostream.h"
  63. #include "llvm/Transforms/Utils/Local.h"
  64. #include "llvm/Transforms/Utils/ValueMapper.h"
  65. #include <cassert>
  66. #include <numeric>
  67. #include <optional>
  68. using namespace llvm;
  69. #define DEBUG_TYPE "attributor"
  70. static cl::opt<bool> ManifestInternal(
  71. "attributor-manifest-internal", cl::Hidden,
  72. cl::desc("Manifest Attributor internal string attributes."),
  73. cl::init(false));
  74. static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
  75. cl::Hidden);
  76. template <>
  77. unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
  78. template <> unsigned llvm::PotentialLLVMValuesState::MaxPotentialValues = -1;
  79. static cl::opt<unsigned, true> MaxPotentialValues(
  80. "attributor-max-potential-values", cl::Hidden,
  81. cl::desc("Maximum number of potential values to be "
  82. "tracked for each position."),
  83. cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues),
  84. cl::init(7));
  85. static cl::opt<int> MaxPotentialValuesIterations(
  86. "attributor-max-potential-values-iterations", cl::Hidden,
  87. cl::desc(
  88. "Maximum number of iterations we keep dismantling potential values."),
  89. cl::init(64));
  90. STATISTIC(NumAAs, "Number of abstract attributes created");
  91. // Some helper macros to deal with statistics tracking.
  92. //
  93. // Usage:
  94. // For simple IR attribute tracking overload trackStatistics in the abstract
  95. // attribute and choose the right STATS_DECLTRACK_********* macro,
  96. // e.g.,:
  97. // void trackStatistics() const override {
  98. // STATS_DECLTRACK_ARG_ATTR(returned)
  99. // }
  100. // If there is a single "increment" side one can use the macro
  101. // STATS_DECLTRACK with a custom message. If there are multiple increment
  102. // sides, STATS_DECL and STATS_TRACK can also be used separately.
  103. //
  104. #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
  105. ("Number of " #TYPE " marked '" #NAME "'")
  106. #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
  107. #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
  108. #define STATS_DECL(NAME, TYPE, MSG) \
  109. STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
  110. #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
  111. #define STATS_DECLTRACK(NAME, TYPE, MSG) \
  112. { \
  113. STATS_DECL(NAME, TYPE, MSG) \
  114. STATS_TRACK(NAME, TYPE) \
  115. }
  116. #define STATS_DECLTRACK_ARG_ATTR(NAME) \
  117. STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
  118. #define STATS_DECLTRACK_CSARG_ATTR(NAME) \
  119. STATS_DECLTRACK(NAME, CSArguments, \
  120. BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
  121. #define STATS_DECLTRACK_FN_ATTR(NAME) \
  122. STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
  123. #define STATS_DECLTRACK_CS_ATTR(NAME) \
  124. STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
  125. #define STATS_DECLTRACK_FNRET_ATTR(NAME) \
  126. STATS_DECLTRACK(NAME, FunctionReturn, \
  127. BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
  128. #define STATS_DECLTRACK_CSRET_ATTR(NAME) \
  129. STATS_DECLTRACK(NAME, CSReturn, \
  130. BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
  131. #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
  132. STATS_DECLTRACK(NAME, Floating, \
  133. ("Number of floating values known to be '" #NAME "'"))
  134. // Specialization of the operator<< for abstract attributes subclasses. This
  135. // disambiguates situations where multiple operators are applicable.
  136. namespace llvm {
  137. #define PIPE_OPERATOR(CLASS) \
  138. raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
  139. return OS << static_cast<const AbstractAttribute &>(AA); \
  140. }
  141. PIPE_OPERATOR(AAIsDead)
  142. PIPE_OPERATOR(AANoUnwind)
  143. PIPE_OPERATOR(AANoSync)
  144. PIPE_OPERATOR(AANoRecurse)
  145. PIPE_OPERATOR(AAWillReturn)
  146. PIPE_OPERATOR(AANoReturn)
  147. PIPE_OPERATOR(AAReturnedValues)
  148. PIPE_OPERATOR(AANonNull)
  149. PIPE_OPERATOR(AANoAlias)
  150. PIPE_OPERATOR(AADereferenceable)
  151. PIPE_OPERATOR(AAAlign)
  152. PIPE_OPERATOR(AAInstanceInfo)
  153. PIPE_OPERATOR(AANoCapture)
  154. PIPE_OPERATOR(AAValueSimplify)
  155. PIPE_OPERATOR(AANoFree)
  156. PIPE_OPERATOR(AAHeapToStack)
  157. PIPE_OPERATOR(AAIntraFnReachability)
  158. PIPE_OPERATOR(AAMemoryBehavior)
  159. PIPE_OPERATOR(AAMemoryLocation)
  160. PIPE_OPERATOR(AAValueConstantRange)
  161. PIPE_OPERATOR(AAPrivatizablePtr)
  162. PIPE_OPERATOR(AAUndefinedBehavior)
  163. PIPE_OPERATOR(AAPotentialConstantValues)
  164. PIPE_OPERATOR(AAPotentialValues)
  165. PIPE_OPERATOR(AANoUndef)
  166. PIPE_OPERATOR(AACallEdges)
  167. PIPE_OPERATOR(AAInterFnReachability)
  168. PIPE_OPERATOR(AAPointerInfo)
  169. PIPE_OPERATOR(AAAssumptionInfo)
  170. PIPE_OPERATOR(AAUnderlyingObjects)
  171. #undef PIPE_OPERATOR
  172. template <>
  173. ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
  174. const DerefState &R) {
  175. ChangeStatus CS0 =
  176. clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
  177. ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
  178. return CS0 | CS1;
  179. }
  180. } // namespace llvm
  181. /// Checks if a type could have padding bytes.
  182. static bool isDenselyPacked(Type *Ty, const DataLayout &DL) {
  183. // There is no size information, so be conservative.
  184. if (!Ty->isSized())
  185. return false;
  186. // If the alloc size is not equal to the storage size, then there are padding
  187. // bytes. For x86_fp80 on x86-64, size: 80 alloc size: 128.
  188. if (DL.getTypeSizeInBits(Ty) != DL.getTypeAllocSizeInBits(Ty))
  189. return false;
  190. // FIXME: This isn't the right way to check for padding in vectors with
  191. // non-byte-size elements.
  192. if (VectorType *SeqTy = dyn_cast<VectorType>(Ty))
  193. return isDenselyPacked(SeqTy->getElementType(), DL);
  194. // For array types, check for padding within members.
  195. if (ArrayType *SeqTy = dyn_cast<ArrayType>(Ty))
  196. return isDenselyPacked(SeqTy->getElementType(), DL);
  197. if (!isa<StructType>(Ty))
  198. return true;
  199. // Check for padding within and between elements of a struct.
  200. StructType *StructTy = cast<StructType>(Ty);
  201. const StructLayout *Layout = DL.getStructLayout(StructTy);
  202. uint64_t StartPos = 0;
  203. for (unsigned I = 0, E = StructTy->getNumElements(); I < E; ++I) {
  204. Type *ElTy = StructTy->getElementType(I);
  205. if (!isDenselyPacked(ElTy, DL))
  206. return false;
  207. if (StartPos != Layout->getElementOffsetInBits(I))
  208. return false;
  209. StartPos += DL.getTypeAllocSizeInBits(ElTy);
  210. }
  211. return true;
  212. }
  213. /// Get pointer operand of memory accessing instruction. If \p I is
  214. /// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
  215. /// is set to false and the instruction is volatile, return nullptr.
  216. static const Value *getPointerOperand(const Instruction *I,
  217. bool AllowVolatile) {
  218. if (!AllowVolatile && I->isVolatile())
  219. return nullptr;
  220. if (auto *LI = dyn_cast<LoadInst>(I)) {
  221. return LI->getPointerOperand();
  222. }
  223. if (auto *SI = dyn_cast<StoreInst>(I)) {
  224. return SI->getPointerOperand();
  225. }
  226. if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
  227. return CXI->getPointerOperand();
  228. }
  229. if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
  230. return RMWI->getPointerOperand();
  231. }
  232. return nullptr;
  233. }
  234. /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
  235. /// advanced by \p Offset bytes. To aid later analysis the method tries to build
  236. /// getelement pointer instructions that traverse the natural type of \p Ptr if
  237. /// possible. If that fails, the remaining offset is adjusted byte-wise, hence
  238. /// through a cast to i8*.
  239. ///
  240. /// TODO: This could probably live somewhere more prominantly if it doesn't
  241. /// already exist.
  242. static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
  243. int64_t Offset, IRBuilder<NoFolder> &IRB,
  244. const DataLayout &DL) {
  245. assert(Offset >= 0 && "Negative offset not supported yet!");
  246. LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
  247. << "-bytes as " << *ResTy << "\n");
  248. if (Offset) {
  249. Type *Ty = PtrElemTy;
  250. APInt IntOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset);
  251. SmallVector<APInt> IntIndices = DL.getGEPIndicesForOffset(Ty, IntOffset);
  252. SmallVector<Value *, 4> ValIndices;
  253. std::string GEPName = Ptr->getName().str();
  254. for (const APInt &Index : IntIndices) {
  255. ValIndices.push_back(IRB.getInt(Index));
  256. GEPName += "." + std::to_string(Index.getZExtValue());
  257. }
  258. // Create a GEP for the indices collected above.
  259. Ptr = IRB.CreateGEP(PtrElemTy, Ptr, ValIndices, GEPName);
  260. // If an offset is left we use byte-wise adjustment.
  261. if (IntOffset != 0) {
  262. Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
  263. Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(IntOffset),
  264. GEPName + ".b" + Twine(IntOffset.getZExtValue()));
  265. }
  266. }
  267. // Ensure the result has the requested type.
  268. Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, ResTy,
  269. Ptr->getName() + ".cast");
  270. LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
  271. return Ptr;
  272. }
  273. static const Value *
  274. stripAndAccumulateOffsets(Attributor &A, const AbstractAttribute &QueryingAA,
  275. const Value *Val, const DataLayout &DL, APInt &Offset,
  276. bool GetMinOffset, bool AllowNonInbounds,
  277. bool UseAssumed = false) {
  278. auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool {
  279. const IRPosition &Pos = IRPosition::value(V);
  280. // Only track dependence if we are going to use the assumed info.
  281. const AAValueConstantRange &ValueConstantRangeAA =
  282. A.getAAFor<AAValueConstantRange>(QueryingAA, Pos,
  283. UseAssumed ? DepClassTy::OPTIONAL
  284. : DepClassTy::NONE);
  285. ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed()
  286. : ValueConstantRangeAA.getKnown();
  287. if (Range.isFullSet())
  288. return false;
  289. // We can only use the lower part of the range because the upper part can
  290. // be higher than what the value can really be.
  291. if (GetMinOffset)
  292. ROffset = Range.getSignedMin();
  293. else
  294. ROffset = Range.getSignedMax();
  295. return true;
  296. };
  297. return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds,
  298. /* AllowInvariant */ true,
  299. AttributorAnalysis);
  300. }
  301. static const Value *
  302. getMinimalBaseOfPointer(Attributor &A, const AbstractAttribute &QueryingAA,
  303. const Value *Ptr, int64_t &BytesOffset,
  304. const DataLayout &DL, bool AllowNonInbounds = false) {
  305. APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
  306. const Value *Base =
  307. stripAndAccumulateOffsets(A, QueryingAA, Ptr, DL, OffsetAPInt,
  308. /* GetMinOffset */ true, AllowNonInbounds);
  309. BytesOffset = OffsetAPInt.getSExtValue();
  310. return Base;
  311. }
  312. /// Clamp the information known for all returned values of a function
  313. /// (identified by \p QueryingAA) into \p S.
  314. template <typename AAType, typename StateType = typename AAType::StateType>
  315. static void clampReturnedValueStates(
  316. Attributor &A, const AAType &QueryingAA, StateType &S,
  317. const IRPosition::CallBaseContext *CBContext = nullptr) {
  318. LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
  319. << QueryingAA << " into " << S << "\n");
  320. assert((QueryingAA.getIRPosition().getPositionKind() ==
  321. IRPosition::IRP_RETURNED ||
  322. QueryingAA.getIRPosition().getPositionKind() ==
  323. IRPosition::IRP_CALL_SITE_RETURNED) &&
  324. "Can only clamp returned value states for a function returned or call "
  325. "site returned position!");
  326. // Use an optional state as there might not be any return values and we want
  327. // to join (IntegerState::operator&) the state of all there are.
  328. std::optional<StateType> T;
  329. // Callback for each possibly returned value.
  330. auto CheckReturnValue = [&](Value &RV) -> bool {
  331. const IRPosition &RVPos = IRPosition::value(RV, CBContext);
  332. const AAType &AA =
  333. A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED);
  334. LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
  335. << " @ " << RVPos << "\n");
  336. const StateType &AAS = AA.getState();
  337. if (!T)
  338. T = StateType::getBestState(AAS);
  339. *T &= AAS;
  340. LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
  341. << "\n");
  342. return T->isValidState();
  343. };
  344. if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
  345. S.indicatePessimisticFixpoint();
  346. else if (T)
  347. S ^= *T;
  348. }
  349. namespace {
  350. /// Helper class for generic deduction: return value -> returned position.
  351. template <typename AAType, typename BaseType,
  352. typename StateType = typename BaseType::StateType,
  353. bool PropagateCallBaseContext = false>
  354. struct AAReturnedFromReturnedValues : public BaseType {
  355. AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A)
  356. : BaseType(IRP, A) {}
  357. /// See AbstractAttribute::updateImpl(...).
  358. ChangeStatus updateImpl(Attributor &A) override {
  359. StateType S(StateType::getBestState(this->getState()));
  360. clampReturnedValueStates<AAType, StateType>(
  361. A, *this, S,
  362. PropagateCallBaseContext ? this->getCallBaseContext() : nullptr);
  363. // TODO: If we know we visited all returned values, thus no are assumed
  364. // dead, we can take the known information from the state T.
  365. return clampStateAndIndicateChange<StateType>(this->getState(), S);
  366. }
  367. };
  368. /// Clamp the information known at all call sites for a given argument
  369. /// (identified by \p QueryingAA) into \p S.
  370. template <typename AAType, typename StateType = typename AAType::StateType>
  371. static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
  372. StateType &S) {
  373. LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
  374. << QueryingAA << " into " << S << "\n");
  375. assert(QueryingAA.getIRPosition().getPositionKind() ==
  376. IRPosition::IRP_ARGUMENT &&
  377. "Can only clamp call site argument states for an argument position!");
  378. // Use an optional state as there might not be any return values and we want
  379. // to join (IntegerState::operator&) the state of all there are.
  380. std::optional<StateType> T;
  381. // The argument number which is also the call site argument number.
  382. unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo();
  383. auto CallSiteCheck = [&](AbstractCallSite ACS) {
  384. const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
  385. // Check if a coresponding argument was found or if it is on not associated
  386. // (which can happen for callback calls).
  387. if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
  388. return false;
  389. const AAType &AA =
  390. A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED);
  391. LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
  392. << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
  393. const StateType &AAS = AA.getState();
  394. if (!T)
  395. T = StateType::getBestState(AAS);
  396. *T &= AAS;
  397. LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
  398. << "\n");
  399. return T->isValidState();
  400. };
  401. bool UsedAssumedInformation = false;
  402. if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
  403. UsedAssumedInformation))
  404. S.indicatePessimisticFixpoint();
  405. else if (T)
  406. S ^= *T;
  407. }
  408. /// This function is the bridge between argument position and the call base
  409. /// context.
  410. template <typename AAType, typename BaseType,
  411. typename StateType = typename AAType::StateType>
  412. bool getArgumentStateFromCallBaseContext(Attributor &A,
  413. BaseType &QueryingAttribute,
  414. IRPosition &Pos, StateType &State) {
  415. assert((Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) &&
  416. "Expected an 'argument' position !");
  417. const CallBase *CBContext = Pos.getCallBaseContext();
  418. if (!CBContext)
  419. return false;
  420. int ArgNo = Pos.getCallSiteArgNo();
  421. assert(ArgNo >= 0 && "Invalid Arg No!");
  422. const auto &AA = A.getAAFor<AAType>(
  423. QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo),
  424. DepClassTy::REQUIRED);
  425. const StateType &CBArgumentState =
  426. static_cast<const StateType &>(AA.getState());
  427. LLVM_DEBUG(dbgs() << "[Attributor] Briding Call site context to argument"
  428. << "Position:" << Pos << "CB Arg state:" << CBArgumentState
  429. << "\n");
  430. // NOTE: If we want to do call site grouping it should happen here.
  431. State ^= CBArgumentState;
  432. return true;
  433. }
  434. /// Helper class for generic deduction: call site argument -> argument position.
  435. template <typename AAType, typename BaseType,
  436. typename StateType = typename AAType::StateType,
  437. bool BridgeCallBaseContext = false>
  438. struct AAArgumentFromCallSiteArguments : public BaseType {
  439. AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A)
  440. : BaseType(IRP, A) {}
  441. /// See AbstractAttribute::updateImpl(...).
  442. ChangeStatus updateImpl(Attributor &A) override {
  443. StateType S = StateType::getBestState(this->getState());
  444. if (BridgeCallBaseContext) {
  445. bool Success =
  446. getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>(
  447. A, *this, this->getIRPosition(), S);
  448. if (Success)
  449. return clampStateAndIndicateChange<StateType>(this->getState(), S);
  450. }
  451. clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
  452. // TODO: If we know we visited all incoming values, thus no are assumed
  453. // dead, we can take the known information from the state T.
  454. return clampStateAndIndicateChange<StateType>(this->getState(), S);
  455. }
  456. };
  457. /// Helper class for generic replication: function returned -> cs returned.
  458. template <typename AAType, typename BaseType,
  459. typename StateType = typename BaseType::StateType,
  460. bool IntroduceCallBaseContext = false>
  461. struct AACallSiteReturnedFromReturned : public BaseType {
  462. AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A)
  463. : BaseType(IRP, A) {}
  464. /// See AbstractAttribute::updateImpl(...).
  465. ChangeStatus updateImpl(Attributor &A) override {
  466. assert(this->getIRPosition().getPositionKind() ==
  467. IRPosition::IRP_CALL_SITE_RETURNED &&
  468. "Can only wrap function returned positions for call site returned "
  469. "positions!");
  470. auto &S = this->getState();
  471. const Function *AssociatedFunction =
  472. this->getIRPosition().getAssociatedFunction();
  473. if (!AssociatedFunction)
  474. return S.indicatePessimisticFixpoint();
  475. CallBase &CBContext = cast<CallBase>(this->getAnchorValue());
  476. if (IntroduceCallBaseContext)
  477. LLVM_DEBUG(dbgs() << "[Attributor] Introducing call base context:"
  478. << CBContext << "\n");
  479. IRPosition FnPos = IRPosition::returned(
  480. *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr);
  481. const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED);
  482. return clampStateAndIndicateChange(S, AA.getState());
  483. }
  484. };
  485. /// Helper function to accumulate uses.
  486. template <class AAType, typename StateType = typename AAType::StateType>
  487. static void followUsesInContext(AAType &AA, Attributor &A,
  488. MustBeExecutedContextExplorer &Explorer,
  489. const Instruction *CtxI,
  490. SetVector<const Use *> &Uses,
  491. StateType &State) {
  492. auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
  493. for (unsigned u = 0; u < Uses.size(); ++u) {
  494. const Use *U = Uses[u];
  495. if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
  496. bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
  497. if (Found && AA.followUseInMBEC(A, U, UserI, State))
  498. for (const Use &Us : UserI->uses())
  499. Uses.insert(&Us);
  500. }
  501. }
  502. }
  503. /// Use the must-be-executed-context around \p I to add information into \p S.
  504. /// The AAType class is required to have `followUseInMBEC` method with the
  505. /// following signature and behaviour:
  506. ///
  507. /// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I)
  508. /// U - Underlying use.
  509. /// I - The user of the \p U.
  510. /// Returns true if the value should be tracked transitively.
  511. ///
  512. template <class AAType, typename StateType = typename AAType::StateType>
  513. static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
  514. Instruction &CtxI) {
  515. // Container for (transitive) uses of the associated value.
  516. SetVector<const Use *> Uses;
  517. for (const Use &U : AA.getIRPosition().getAssociatedValue().uses())
  518. Uses.insert(&U);
  519. MustBeExecutedContextExplorer &Explorer =
  520. A.getInfoCache().getMustBeExecutedContextExplorer();
  521. followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S);
  522. if (S.isAtFixpoint())
  523. return;
  524. SmallVector<const BranchInst *, 4> BrInsts;
  525. auto Pred = [&](const Instruction *I) {
  526. if (const BranchInst *Br = dyn_cast<BranchInst>(I))
  527. if (Br->isConditional())
  528. BrInsts.push_back(Br);
  529. return true;
  530. };
  531. // Here, accumulate conditional branch instructions in the context. We
  532. // explore the child paths and collect the known states. The disjunction of
  533. // those states can be merged to its own state. Let ParentState_i be a state
  534. // to indicate the known information for an i-th branch instruction in the
  535. // context. ChildStates are created for its successors respectively.
  536. //
  537. // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
  538. // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
  539. // ...
  540. // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
  541. //
  542. // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
  543. //
  544. // FIXME: Currently, recursive branches are not handled. For example, we
  545. // can't deduce that ptr must be dereferenced in below function.
  546. //
  547. // void f(int a, int c, int *ptr) {
  548. // if(a)
  549. // if (b) {
  550. // *ptr = 0;
  551. // } else {
  552. // *ptr = 1;
  553. // }
  554. // else {
  555. // if (b) {
  556. // *ptr = 0;
  557. // } else {
  558. // *ptr = 1;
  559. // }
  560. // }
  561. // }
  562. Explorer.checkForAllContext(&CtxI, Pred);
  563. for (const BranchInst *Br : BrInsts) {
  564. StateType ParentState;
  565. // The known state of the parent state is a conjunction of children's
  566. // known states so it is initialized with a best state.
  567. ParentState.indicateOptimisticFixpoint();
  568. for (const BasicBlock *BB : Br->successors()) {
  569. StateType ChildState;
  570. size_t BeforeSize = Uses.size();
  571. followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState);
  572. // Erase uses which only appear in the child.
  573. for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
  574. It = Uses.erase(It);
  575. ParentState &= ChildState;
  576. }
  577. // Use only known state.
  578. S += ParentState;
  579. }
  580. }
  581. } // namespace
  582. /// ------------------------ PointerInfo ---------------------------------------
  583. namespace llvm {
  584. namespace AA {
  585. namespace PointerInfo {
  586. struct State;
  587. } // namespace PointerInfo
  588. } // namespace AA
  589. /// Helper for AA::PointerInfo::Access DenseMap/Set usage.
  590. template <>
  591. struct DenseMapInfo<AAPointerInfo::Access> : DenseMapInfo<Instruction *> {
  592. using Access = AAPointerInfo::Access;
  593. static inline Access getEmptyKey();
  594. static inline Access getTombstoneKey();
  595. static unsigned getHashValue(const Access &A);
  596. static bool isEqual(const Access &LHS, const Access &RHS);
  597. };
  598. /// Helper that allows RangeTy as a key in a DenseMap.
  599. template <> struct DenseMapInfo<AA::RangeTy> {
  600. static inline AA::RangeTy getEmptyKey() {
  601. auto EmptyKey = DenseMapInfo<int64_t>::getEmptyKey();
  602. return AA::RangeTy{EmptyKey, EmptyKey};
  603. }
  604. static inline AA::RangeTy getTombstoneKey() {
  605. auto TombstoneKey = DenseMapInfo<int64_t>::getTombstoneKey();
  606. return AA::RangeTy{TombstoneKey, TombstoneKey};
  607. }
  608. static unsigned getHashValue(const AA::RangeTy &Range) {
  609. return detail::combineHashValue(
  610. DenseMapInfo<int64_t>::getHashValue(Range.Offset),
  611. DenseMapInfo<int64_t>::getHashValue(Range.Size));
  612. }
  613. static bool isEqual(const AA::RangeTy &A, const AA::RangeTy B) {
  614. return A == B;
  615. }
  616. };
  617. /// Helper for AA::PointerInfo::Access DenseMap/Set usage ignoring everythign
  618. /// but the instruction
  619. struct AccessAsInstructionInfo : DenseMapInfo<Instruction *> {
  620. using Base = DenseMapInfo<Instruction *>;
  621. using Access = AAPointerInfo::Access;
  622. static inline Access getEmptyKey();
  623. static inline Access getTombstoneKey();
  624. static unsigned getHashValue(const Access &A);
  625. static bool isEqual(const Access &LHS, const Access &RHS);
  626. };
  627. } // namespace llvm
  628. /// A type to track pointer/struct usage and accesses for AAPointerInfo.
  629. struct AA::PointerInfo::State : public AbstractState {
  630. /// Return the best possible representable state.
  631. static State getBestState(const State &SIS) { return State(); }
  632. /// Return the worst possible representable state.
  633. static State getWorstState(const State &SIS) {
  634. State R;
  635. R.indicatePessimisticFixpoint();
  636. return R;
  637. }
  638. State() = default;
  639. State(State &&SIS) = default;
  640. const State &getAssumed() const { return *this; }
  641. /// See AbstractState::isValidState().
  642. bool isValidState() const override { return BS.isValidState(); }
  643. /// See AbstractState::isAtFixpoint().
  644. bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
  645. /// See AbstractState::indicateOptimisticFixpoint().
  646. ChangeStatus indicateOptimisticFixpoint() override {
  647. BS.indicateOptimisticFixpoint();
  648. return ChangeStatus::UNCHANGED;
  649. }
  650. /// See AbstractState::indicatePessimisticFixpoint().
  651. ChangeStatus indicatePessimisticFixpoint() override {
  652. BS.indicatePessimisticFixpoint();
  653. return ChangeStatus::CHANGED;
  654. }
  655. State &operator=(const State &R) {
  656. if (this == &R)
  657. return *this;
  658. BS = R.BS;
  659. AccessList = R.AccessList;
  660. OffsetBins = R.OffsetBins;
  661. RemoteIMap = R.RemoteIMap;
  662. return *this;
  663. }
  664. State &operator=(State &&R) {
  665. if (this == &R)
  666. return *this;
  667. std::swap(BS, R.BS);
  668. std::swap(AccessList, R.AccessList);
  669. std::swap(OffsetBins, R.OffsetBins);
  670. std::swap(RemoteIMap, R.RemoteIMap);
  671. return *this;
  672. }
  673. /// Add a new Access to the state at offset \p Offset and with size \p Size.
  674. /// The access is associated with \p I, writes \p Content (if anything), and
  675. /// is of kind \p Kind. If an Access already exists for the same \p I and same
  676. /// \p RemoteI, the two are combined, potentially losing information about
  677. /// offset and size. The resulting access must now be moved from its original
  678. /// OffsetBin to the bin for its new offset.
  679. ///
  680. /// \Returns CHANGED, if the state changed, UNCHANGED otherwise.
  681. ChangeStatus addAccess(Attributor &A, const AAPointerInfo::RangeList &Ranges,
  682. Instruction &I, std::optional<Value *> Content,
  683. AAPointerInfo::AccessKind Kind, Type *Ty,
  684. Instruction *RemoteI = nullptr);
  685. using OffsetBinsTy = DenseMap<RangeTy, SmallSet<unsigned, 4>>;
  686. using const_bin_iterator = OffsetBinsTy::const_iterator;
  687. const_bin_iterator begin() const { return OffsetBins.begin(); }
  688. const_bin_iterator end() const { return OffsetBins.end(); }
  689. const AAPointerInfo::Access &getAccess(unsigned Index) const {
  690. return AccessList[Index];
  691. }
  692. protected:
  693. // Every memory instruction results in an Access object. We maintain a list of
  694. // all Access objects that we own, along with the following maps:
  695. //
  696. // - OffsetBins: RangeTy -> { Access }
  697. // - RemoteIMap: RemoteI x LocalI -> Access
  698. //
  699. // A RemoteI is any instruction that accesses memory. RemoteI is different
  700. // from LocalI if and only if LocalI is a call; then RemoteI is some
  701. // instruction in the callgraph starting from LocalI. Multiple paths in the
  702. // callgraph from LocalI to RemoteI may produce multiple accesses, but these
  703. // are all combined into a single Access object. This may result in loss of
  704. // information in RangeTy in the Access object.
  705. SmallVector<AAPointerInfo::Access> AccessList;
  706. OffsetBinsTy OffsetBins;
  707. DenseMap<const Instruction *, SmallVector<unsigned>> RemoteIMap;
  708. /// See AAPointerInfo::forallInterferingAccesses.
  709. bool forallInterferingAccesses(
  710. AA::RangeTy Range,
  711. function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const {
  712. if (!isValidState())
  713. return false;
  714. for (const auto &It : OffsetBins) {
  715. AA::RangeTy ItRange = It.getFirst();
  716. if (!Range.mayOverlap(ItRange))
  717. continue;
  718. bool IsExact = Range == ItRange && !Range.offsetOrSizeAreUnknown();
  719. for (auto Index : It.getSecond()) {
  720. auto &Access = AccessList[Index];
  721. if (!CB(Access, IsExact))
  722. return false;
  723. }
  724. }
  725. return true;
  726. }
  727. /// See AAPointerInfo::forallInterferingAccesses.
  728. bool forallInterferingAccesses(
  729. Instruction &I,
  730. function_ref<bool(const AAPointerInfo::Access &, bool)> CB,
  731. AA::RangeTy &Range) const {
  732. if (!isValidState())
  733. return false;
  734. auto LocalList = RemoteIMap.find(&I);
  735. if (LocalList == RemoteIMap.end()) {
  736. return true;
  737. }
  738. for (unsigned Index : LocalList->getSecond()) {
  739. for (auto &R : AccessList[Index]) {
  740. Range &= R;
  741. if (Range.offsetOrSizeAreUnknown())
  742. break;
  743. }
  744. }
  745. return forallInterferingAccesses(Range, CB);
  746. }
  747. private:
  748. /// State to track fixpoint and validity.
  749. BooleanState BS;
  750. };
  751. ChangeStatus AA::PointerInfo::State::addAccess(
  752. Attributor &A, const AAPointerInfo::RangeList &Ranges, Instruction &I,
  753. std::optional<Value *> Content, AAPointerInfo::AccessKind Kind, Type *Ty,
  754. Instruction *RemoteI) {
  755. RemoteI = RemoteI ? RemoteI : &I;
  756. // Check if we have an access for this instruction, if not, simply add it.
  757. auto &LocalList = RemoteIMap[RemoteI];
  758. bool AccExists = false;
  759. unsigned AccIndex = AccessList.size();
  760. for (auto Index : LocalList) {
  761. auto &A = AccessList[Index];
  762. if (A.getLocalInst() == &I) {
  763. AccExists = true;
  764. AccIndex = Index;
  765. break;
  766. }
  767. }
  768. auto AddToBins = [&](const AAPointerInfo::RangeList &ToAdd) {
  769. LLVM_DEBUG(
  770. if (ToAdd.size())
  771. dbgs() << "[AAPointerInfo] Inserting access in new offset bins\n";
  772. );
  773. for (auto Key : ToAdd) {
  774. LLVM_DEBUG(dbgs() << " key " << Key << "\n");
  775. OffsetBins[Key].insert(AccIndex);
  776. }
  777. };
  778. if (!AccExists) {
  779. AccessList.emplace_back(&I, RemoteI, Ranges, Content, Kind, Ty);
  780. assert((AccessList.size() == AccIndex + 1) &&
  781. "New Access should have been at AccIndex");
  782. LocalList.push_back(AccIndex);
  783. AddToBins(AccessList[AccIndex].getRanges());
  784. return ChangeStatus::CHANGED;
  785. }
  786. // Combine the new Access with the existing Access, and then update the
  787. // mapping in the offset bins.
  788. AAPointerInfo::Access Acc(&I, RemoteI, Ranges, Content, Kind, Ty);
  789. auto &Current = AccessList[AccIndex];
  790. auto Before = Current;
  791. Current &= Acc;
  792. if (Current == Before)
  793. return ChangeStatus::UNCHANGED;
  794. auto &ExistingRanges = Before.getRanges();
  795. auto &NewRanges = Current.getRanges();
  796. // Ranges that are in the old access but not the new access need to be removed
  797. // from the offset bins.
  798. AAPointerInfo::RangeList ToRemove;
  799. AAPointerInfo::RangeList::set_difference(ExistingRanges, NewRanges, ToRemove);
  800. LLVM_DEBUG(
  801. if (ToRemove.size())
  802. dbgs() << "[AAPointerInfo] Removing access from old offset bins\n";
  803. );
  804. for (auto Key : ToRemove) {
  805. LLVM_DEBUG(dbgs() << " key " << Key << "\n");
  806. assert(OffsetBins.count(Key) && "Existing Access must be in some bin.");
  807. auto &Bin = OffsetBins[Key];
  808. assert(Bin.count(AccIndex) &&
  809. "Expected bin to actually contain the Access.");
  810. Bin.erase(AccIndex);
  811. }
  812. // Ranges that are in the new access but not the old access need to be added
  813. // to the offset bins.
  814. AAPointerInfo::RangeList ToAdd;
  815. AAPointerInfo::RangeList::set_difference(NewRanges, ExistingRanges, ToAdd);
  816. AddToBins(ToAdd);
  817. return ChangeStatus::CHANGED;
  818. }
  819. namespace {
  820. /// A helper containing a list of offsets computed for a Use. Ideally this
  821. /// list should be strictly ascending, but we ensure that only when we
  822. /// actually translate the list of offsets to a RangeList.
  823. struct OffsetInfo {
  824. using VecTy = SmallVector<int64_t>;
  825. using const_iterator = VecTy::const_iterator;
  826. VecTy Offsets;
  827. const_iterator begin() const { return Offsets.begin(); }
  828. const_iterator end() const { return Offsets.end(); }
  829. bool operator==(const OffsetInfo &RHS) const {
  830. return Offsets == RHS.Offsets;
  831. }
  832. bool operator!=(const OffsetInfo &RHS) const { return !(*this == RHS); }
  833. void insert(int64_t Offset) { Offsets.push_back(Offset); }
  834. bool isUnassigned() const { return Offsets.size() == 0; }
  835. bool isUnknown() const {
  836. if (isUnassigned())
  837. return false;
  838. if (Offsets.size() == 1)
  839. return Offsets.front() == AA::RangeTy::Unknown;
  840. return false;
  841. }
  842. void setUnknown() {
  843. Offsets.clear();
  844. Offsets.push_back(AA::RangeTy::Unknown);
  845. }
  846. void addToAll(int64_t Inc) {
  847. for (auto &Offset : Offsets) {
  848. Offset += Inc;
  849. }
  850. }
  851. /// Copy offsets from \p R into the current list.
  852. ///
  853. /// Ideally all lists should be strictly ascending, but we defer that to the
  854. /// actual use of the list. So we just blindly append here.
  855. void merge(const OffsetInfo &R) { Offsets.append(R.Offsets); }
  856. };
  857. #ifndef NDEBUG
  858. static raw_ostream &operator<<(raw_ostream &OS, const OffsetInfo &OI) {
  859. ListSeparator LS;
  860. OS << "[";
  861. for (auto Offset : OI) {
  862. OS << LS << Offset;
  863. }
  864. OS << "]";
  865. return OS;
  866. }
  867. #endif // NDEBUG
  868. struct AAPointerInfoImpl
  869. : public StateWrapper<AA::PointerInfo::State, AAPointerInfo> {
  870. using BaseTy = StateWrapper<AA::PointerInfo::State, AAPointerInfo>;
  871. AAPointerInfoImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {}
  872. /// See AbstractAttribute::getAsStr().
  873. const std::string getAsStr() const override {
  874. return std::string("PointerInfo ") +
  875. (isValidState() ? (std::string("#") +
  876. std::to_string(OffsetBins.size()) + " bins")
  877. : "<invalid>");
  878. }
  879. /// See AbstractAttribute::manifest(...).
  880. ChangeStatus manifest(Attributor &A) override {
  881. return AAPointerInfo::manifest(A);
  882. }
  883. bool forallInterferingAccesses(
  884. AA::RangeTy Range,
  885. function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
  886. const override {
  887. return State::forallInterferingAccesses(Range, CB);
  888. }
  889. bool forallInterferingAccesses(
  890. Attributor &A, const AbstractAttribute &QueryingAA, Instruction &I,
  891. function_ref<bool(const Access &, bool)> UserCB, bool &HasBeenWrittenTo,
  892. AA::RangeTy &Range) const override {
  893. HasBeenWrittenTo = false;
  894. SmallPtrSet<const Access *, 8> DominatingWrites;
  895. SmallVector<std::pair<const Access *, bool>, 8> InterferingAccesses;
  896. Function &Scope = *I.getFunction();
  897. const auto &NoSyncAA = A.getAAFor<AANoSync>(
  898. QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL);
  899. const auto *ExecDomainAA = A.lookupAAFor<AAExecutionDomain>(
  900. IRPosition::function(Scope), &QueryingAA, DepClassTy::NONE);
  901. bool AllInSameNoSyncFn = NoSyncAA.isAssumedNoSync();
  902. bool InstIsExecutedByInitialThreadOnly =
  903. ExecDomainAA && ExecDomainAA->isExecutedByInitialThreadOnly(I);
  904. bool InstIsExecutedInAlignedRegion =
  905. ExecDomainAA && ExecDomainAA->isExecutedInAlignedRegion(A, I);
  906. if (InstIsExecutedInAlignedRegion || InstIsExecutedByInitialThreadOnly)
  907. A.recordDependence(*ExecDomainAA, QueryingAA, DepClassTy::OPTIONAL);
  908. InformationCache &InfoCache = A.getInfoCache();
  909. bool IsThreadLocalObj =
  910. AA::isAssumedThreadLocalObject(A, getAssociatedValue(), *this);
  911. // Helper to determine if we need to consider threading, which we cannot
  912. // right now. However, if the function is (assumed) nosync or the thread
  913. // executing all instructions is the main thread only we can ignore
  914. // threading. Also, thread-local objects do not require threading reasoning.
  915. // Finally, we can ignore threading if either access is executed in an
  916. // aligned region.
  917. auto CanIgnoreThreadingForInst = [&](const Instruction &I) -> bool {
  918. if (IsThreadLocalObj || AllInSameNoSyncFn)
  919. return true;
  920. const auto *FnExecDomainAA =
  921. I.getFunction() == &Scope
  922. ? ExecDomainAA
  923. : A.lookupAAFor<AAExecutionDomain>(
  924. IRPosition::function(*I.getFunction()), &QueryingAA,
  925. DepClassTy::NONE);
  926. if (!FnExecDomainAA)
  927. return false;
  928. if (InstIsExecutedInAlignedRegion ||
  929. FnExecDomainAA->isExecutedInAlignedRegion(A, I)) {
  930. A.recordDependence(*FnExecDomainAA, QueryingAA, DepClassTy::OPTIONAL);
  931. return true;
  932. }
  933. if (InstIsExecutedByInitialThreadOnly &&
  934. FnExecDomainAA->isExecutedByInitialThreadOnly(I)) {
  935. A.recordDependence(*FnExecDomainAA, QueryingAA, DepClassTy::OPTIONAL);
  936. return true;
  937. }
  938. return false;
  939. };
  940. // Helper to determine if the access is executed by the same thread as the
  941. // given instruction, for now it is sufficient to avoid any potential
  942. // threading effects as we cannot deal with them anyway.
  943. auto CanIgnoreThreading = [&](const Access &Acc) -> bool {
  944. return CanIgnoreThreadingForInst(*Acc.getRemoteInst()) ||
  945. (Acc.getRemoteInst() != Acc.getLocalInst() &&
  946. CanIgnoreThreadingForInst(*Acc.getLocalInst()));
  947. };
  948. // TODO: Use inter-procedural reachability and dominance.
  949. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  950. QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL);
  951. const bool FindInterferingWrites = I.mayReadFromMemory();
  952. const bool FindInterferingReads = I.mayWriteToMemory();
  953. const bool UseDominanceReasoning =
  954. FindInterferingWrites && NoRecurseAA.isKnownNoRecurse();
  955. const DominatorTree *DT =
  956. InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(Scope);
  957. // Helper to check if a value has "kernel lifetime", that is it will not
  958. // outlive a GPU kernel. This is true for shared, constant, and local
  959. // globals on AMD and NVIDIA GPUs.
  960. auto HasKernelLifetime = [&](Value *V, Module &M) {
  961. Triple T(M.getTargetTriple());
  962. if (!(T.isAMDGPU() || T.isNVPTX()))
  963. return false;
  964. switch (AA::GPUAddressSpace(V->getType()->getPointerAddressSpace())) {
  965. case AA::GPUAddressSpace::Shared:
  966. case AA::GPUAddressSpace::Constant:
  967. case AA::GPUAddressSpace::Local:
  968. return true;
  969. default:
  970. return false;
  971. };
  972. };
  973. // The IsLiveInCalleeCB will be used by the AA::isPotentiallyReachable query
  974. // to determine if we should look at reachability from the callee. For
  975. // certain pointers we know the lifetime and we do not have to step into the
  976. // callee to determine reachability as the pointer would be dead in the
  977. // callee. See the conditional initialization below.
  978. std::function<bool(const Function &)> IsLiveInCalleeCB;
  979. if (auto *AI = dyn_cast<AllocaInst>(&getAssociatedValue())) {
  980. // If the alloca containing function is not recursive the alloca
  981. // must be dead in the callee.
  982. const Function *AIFn = AI->getFunction();
  983. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  984. *this, IRPosition::function(*AIFn), DepClassTy::OPTIONAL);
  985. if (NoRecurseAA.isAssumedNoRecurse()) {
  986. IsLiveInCalleeCB = [AIFn](const Function &Fn) { return AIFn != &Fn; };
  987. }
  988. } else if (auto *GV = dyn_cast<GlobalValue>(&getAssociatedValue())) {
  989. // If the global has kernel lifetime we can stop if we reach a kernel
  990. // as it is "dead" in the (unknown) callees.
  991. if (HasKernelLifetime(GV, *GV->getParent()))
  992. IsLiveInCalleeCB = [](const Function &Fn) {
  993. return !Fn.hasFnAttribute("kernel");
  994. };
  995. }
  996. // Set of accesses/instructions that will overwrite the result and are
  997. // therefore blockers in the reachability traversal.
  998. AA::InstExclusionSetTy ExclusionSet;
  999. auto AccessCB = [&](const Access &Acc, bool Exact) {
  1000. if (Exact && Acc.isMustAccess() && Acc.getRemoteInst() != &I) {
  1001. if (Acc.isWrite() || (isa<LoadInst>(I) && Acc.isWriteOrAssumption()))
  1002. ExclusionSet.insert(Acc.getRemoteInst());
  1003. }
  1004. if ((!FindInterferingWrites || !Acc.isWriteOrAssumption()) &&
  1005. (!FindInterferingReads || !Acc.isRead()))
  1006. return true;
  1007. bool Dominates = FindInterferingWrites && DT && Exact &&
  1008. Acc.isMustAccess() &&
  1009. (Acc.getRemoteInst()->getFunction() == &Scope) &&
  1010. DT->dominates(Acc.getRemoteInst(), &I);
  1011. if (Dominates)
  1012. DominatingWrites.insert(&Acc);
  1013. // Track if all interesting accesses are in the same `nosync` function as
  1014. // the given instruction.
  1015. AllInSameNoSyncFn &= Acc.getRemoteInst()->getFunction() == &Scope;
  1016. InterferingAccesses.push_back({&Acc, Exact});
  1017. return true;
  1018. };
  1019. if (!State::forallInterferingAccesses(I, AccessCB, Range))
  1020. return false;
  1021. HasBeenWrittenTo = !DominatingWrites.empty();
  1022. // Dominating writes form a chain, find the least/lowest member.
  1023. Instruction *LeastDominatingWriteInst = nullptr;
  1024. for (const Access *Acc : DominatingWrites) {
  1025. if (!LeastDominatingWriteInst) {
  1026. LeastDominatingWriteInst = Acc->getRemoteInst();
  1027. } else if (DT->dominates(LeastDominatingWriteInst,
  1028. Acc->getRemoteInst())) {
  1029. LeastDominatingWriteInst = Acc->getRemoteInst();
  1030. }
  1031. }
  1032. // Helper to determine if we can skip a specific write access.
  1033. auto CanSkipAccess = [&](const Access &Acc, bool Exact) {
  1034. if (!CanIgnoreThreading(Acc))
  1035. return false;
  1036. // Check read (RAW) dependences and write (WAR) dependences as necessary.
  1037. // If we successfully excluded all effects we are interested in, the
  1038. // access can be skipped.
  1039. bool ReadChecked = !FindInterferingReads;
  1040. bool WriteChecked = !FindInterferingWrites;
  1041. // If the instruction cannot reach the access, the former does not
  1042. // interfere with what the access reads.
  1043. if (!ReadChecked) {
  1044. if (!AA::isPotentiallyReachable(A, I, *Acc.getRemoteInst(), QueryingAA,
  1045. &ExclusionSet, IsLiveInCalleeCB))
  1046. ReadChecked = true;
  1047. }
  1048. // If the instruction cannot be reach from the access, the latter does not
  1049. // interfere with what the instruction reads.
  1050. if (!WriteChecked) {
  1051. if (!AA::isPotentiallyReachable(A, *Acc.getRemoteInst(), I, QueryingAA,
  1052. &ExclusionSet, IsLiveInCalleeCB))
  1053. WriteChecked = true;
  1054. }
  1055. // If we still might be affected by the write of the access but there are
  1056. // dominating writes in the function of the instruction
  1057. // (HasBeenWrittenTo), we can try to reason that the access is overwritten
  1058. // by them. This would have happend above if they are all in the same
  1059. // function, so we only check the inter-procedural case. Effectively, we
  1060. // want to show that there is no call after the dominting write that might
  1061. // reach the access, and when it returns reach the instruction with the
  1062. // updated value. To this end, we iterate all call sites, check if they
  1063. // might reach the instruction without going through another access
  1064. // (ExclusionSet) and at the same time might reach the access. However,
  1065. // that is all part of AAInterFnReachability.
  1066. if (!WriteChecked && HasBeenWrittenTo &&
  1067. Acc.getRemoteInst()->getFunction() != &Scope) {
  1068. const auto &FnReachabilityAA = A.getAAFor<AAInterFnReachability>(
  1069. QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL);
  1070. // Without going backwards in the call tree, can we reach the access
  1071. // from the least dominating write. Do not allow to pass the instruction
  1072. // itself either.
  1073. bool Inserted = ExclusionSet.insert(&I).second;
  1074. if (!FnReachabilityAA.instructionCanReach(
  1075. A, *LeastDominatingWriteInst,
  1076. *Acc.getRemoteInst()->getFunction(), &ExclusionSet))
  1077. WriteChecked = true;
  1078. if (Inserted)
  1079. ExclusionSet.erase(&I);
  1080. }
  1081. if (ReadChecked && WriteChecked)
  1082. return true;
  1083. if (!DT || !UseDominanceReasoning)
  1084. return false;
  1085. if (!DominatingWrites.count(&Acc))
  1086. return false;
  1087. return LeastDominatingWriteInst != Acc.getRemoteInst();
  1088. };
  1089. // Run the user callback on all accesses we cannot skip and return if
  1090. // that succeeded for all or not.
  1091. for (auto &It : InterferingAccesses) {
  1092. if ((!AllInSameNoSyncFn && !IsThreadLocalObj && !ExecDomainAA) ||
  1093. !CanSkipAccess(*It.first, It.second)) {
  1094. if (!UserCB(*It.first, It.second))
  1095. return false;
  1096. }
  1097. }
  1098. return true;
  1099. }
  1100. ChangeStatus translateAndAddStateFromCallee(Attributor &A,
  1101. const AAPointerInfo &OtherAA,
  1102. CallBase &CB) {
  1103. using namespace AA::PointerInfo;
  1104. if (!OtherAA.getState().isValidState() || !isValidState())
  1105. return indicatePessimisticFixpoint();
  1106. const auto &OtherAAImpl = static_cast<const AAPointerInfoImpl &>(OtherAA);
  1107. bool IsByval = OtherAAImpl.getAssociatedArgument()->hasByValAttr();
  1108. // Combine the accesses bin by bin.
  1109. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1110. const auto &State = OtherAAImpl.getState();
  1111. for (const auto &It : State) {
  1112. for (auto Index : It.getSecond()) {
  1113. const auto &RAcc = State.getAccess(Index);
  1114. if (IsByval && !RAcc.isRead())
  1115. continue;
  1116. bool UsedAssumedInformation = false;
  1117. AccessKind AK = RAcc.getKind();
  1118. auto Content = A.translateArgumentToCallSiteContent(
  1119. RAcc.getContent(), CB, *this, UsedAssumedInformation);
  1120. AK = AccessKind(AK & (IsByval ? AccessKind::AK_R : AccessKind::AK_RW));
  1121. AK = AccessKind(AK | (RAcc.isMayAccess() ? AK_MAY : AK_MUST));
  1122. Changed |= addAccess(A, RAcc.getRanges(), CB, Content, AK,
  1123. RAcc.getType(), RAcc.getRemoteInst());
  1124. }
  1125. }
  1126. return Changed;
  1127. }
  1128. ChangeStatus translateAndAddState(Attributor &A, const AAPointerInfo &OtherAA,
  1129. const OffsetInfo &Offsets, CallBase &CB) {
  1130. using namespace AA::PointerInfo;
  1131. if (!OtherAA.getState().isValidState() || !isValidState())
  1132. return indicatePessimisticFixpoint();
  1133. const auto &OtherAAImpl = static_cast<const AAPointerInfoImpl &>(OtherAA);
  1134. // Combine the accesses bin by bin.
  1135. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1136. const auto &State = OtherAAImpl.getState();
  1137. for (const auto &It : State) {
  1138. for (auto Index : It.getSecond()) {
  1139. const auto &RAcc = State.getAccess(Index);
  1140. for (auto Offset : Offsets) {
  1141. auto NewRanges = Offset == AA::RangeTy::Unknown
  1142. ? AA::RangeTy::getUnknown()
  1143. : RAcc.getRanges();
  1144. if (!NewRanges.isUnknown()) {
  1145. NewRanges.addToAllOffsets(Offset);
  1146. }
  1147. Changed |=
  1148. addAccess(A, NewRanges, CB, RAcc.getContent(), RAcc.getKind(),
  1149. RAcc.getType(), RAcc.getRemoteInst());
  1150. }
  1151. }
  1152. }
  1153. return Changed;
  1154. }
  1155. /// Statistic tracking for all AAPointerInfo implementations.
  1156. /// See AbstractAttribute::trackStatistics().
  1157. void trackPointerInfoStatistics(const IRPosition &IRP) const {}
  1158. /// Dump the state into \p O.
  1159. void dumpState(raw_ostream &O) {
  1160. for (auto &It : OffsetBins) {
  1161. O << "[" << It.first.Offset << "-" << It.first.Offset + It.first.Size
  1162. << "] : " << It.getSecond().size() << "\n";
  1163. for (auto AccIndex : It.getSecond()) {
  1164. auto &Acc = AccessList[AccIndex];
  1165. O << " - " << Acc.getKind() << " - " << *Acc.getLocalInst() << "\n";
  1166. if (Acc.getLocalInst() != Acc.getRemoteInst())
  1167. O << " --> " << *Acc.getRemoteInst()
  1168. << "\n";
  1169. if (!Acc.isWrittenValueYetUndetermined()) {
  1170. if (Acc.getWrittenValue())
  1171. O << " - c: " << *Acc.getWrittenValue() << "\n";
  1172. else
  1173. O << " - c: <unknown>\n";
  1174. }
  1175. }
  1176. }
  1177. }
  1178. };
  1179. struct AAPointerInfoFloating : public AAPointerInfoImpl {
  1180. using AccessKind = AAPointerInfo::AccessKind;
  1181. AAPointerInfoFloating(const IRPosition &IRP, Attributor &A)
  1182. : AAPointerInfoImpl(IRP, A) {}
  1183. /// Deal with an access and signal if it was handled successfully.
  1184. bool handleAccess(Attributor &A, Instruction &I,
  1185. std::optional<Value *> Content, AccessKind Kind,
  1186. SmallVectorImpl<int64_t> &Offsets, ChangeStatus &Changed,
  1187. Type &Ty) {
  1188. using namespace AA::PointerInfo;
  1189. auto Size = AA::RangeTy::Unknown;
  1190. const DataLayout &DL = A.getDataLayout();
  1191. TypeSize AccessSize = DL.getTypeStoreSize(&Ty);
  1192. if (!AccessSize.isScalable())
  1193. Size = AccessSize.getFixedValue();
  1194. // Make a strictly ascending list of offsets as required by addAccess()
  1195. llvm::sort(Offsets);
  1196. auto *Last = std::unique(Offsets.begin(), Offsets.end());
  1197. Offsets.erase(Last, Offsets.end());
  1198. VectorType *VT = dyn_cast<VectorType>(&Ty);
  1199. if (!VT || VT->getElementCount().isScalable() ||
  1200. !Content.value_or(nullptr) || !isa<Constant>(*Content) ||
  1201. (*Content)->getType() != VT ||
  1202. DL.getTypeStoreSize(VT->getElementType()).isScalable()) {
  1203. Changed = Changed | addAccess(A, {Offsets, Size}, I, Content, Kind, &Ty);
  1204. } else {
  1205. // Handle vector stores with constant content element-wise.
  1206. // TODO: We could look for the elements or create instructions
  1207. // representing them.
  1208. // TODO: We need to push the Content into the range abstraction
  1209. // (AA::RangeTy) to allow different content values for different
  1210. // ranges. ranges. Hence, support vectors storing different values.
  1211. Type *ElementType = VT->getElementType();
  1212. int64_t ElementSize = DL.getTypeStoreSize(ElementType).getFixedValue();
  1213. auto *ConstContent = cast<Constant>(*Content);
  1214. Type *Int32Ty = Type::getInt32Ty(ElementType->getContext());
  1215. SmallVector<int64_t> ElementOffsets(Offsets.begin(), Offsets.end());
  1216. for (int i = 0, e = VT->getElementCount().getFixedValue(); i != e; ++i) {
  1217. Value *ElementContent = ConstantExpr::getExtractElement(
  1218. ConstContent, ConstantInt::get(Int32Ty, i));
  1219. // Add the element access.
  1220. Changed = Changed | addAccess(A, {ElementOffsets, ElementSize}, I,
  1221. ElementContent, Kind, ElementType);
  1222. // Advance the offsets for the next element.
  1223. for (auto &ElementOffset : ElementOffsets)
  1224. ElementOffset += ElementSize;
  1225. }
  1226. }
  1227. return true;
  1228. };
  1229. /// See AbstractAttribute::updateImpl(...).
  1230. ChangeStatus updateImpl(Attributor &A) override;
  1231. /// If the indices to \p GEP can be traced to constants, incorporate all
  1232. /// of these into \p UsrOI.
  1233. ///
  1234. /// \return true iff \p UsrOI is updated.
  1235. bool collectConstantsForGEP(Attributor &A, const DataLayout &DL,
  1236. OffsetInfo &UsrOI, const OffsetInfo &PtrOI,
  1237. const GEPOperator *GEP);
  1238. /// See AbstractAttribute::trackStatistics()
  1239. void trackStatistics() const override {
  1240. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1241. }
  1242. };
  1243. bool AAPointerInfoFloating::collectConstantsForGEP(Attributor &A,
  1244. const DataLayout &DL,
  1245. OffsetInfo &UsrOI,
  1246. const OffsetInfo &PtrOI,
  1247. const GEPOperator *GEP) {
  1248. unsigned BitWidth = DL.getIndexTypeSizeInBits(GEP->getType());
  1249. MapVector<Value *, APInt> VariableOffsets;
  1250. APInt ConstantOffset(BitWidth, 0);
  1251. assert(!UsrOI.isUnknown() && !PtrOI.isUnknown() &&
  1252. "Don't look for constant values if the offset has already been "
  1253. "determined to be unknown.");
  1254. if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset)) {
  1255. UsrOI.setUnknown();
  1256. return true;
  1257. }
  1258. LLVM_DEBUG(dbgs() << "[AAPointerInfo] GEP offset is "
  1259. << (VariableOffsets.empty() ? "" : "not") << " constant "
  1260. << *GEP << "\n");
  1261. auto Union = PtrOI;
  1262. Union.addToAll(ConstantOffset.getSExtValue());
  1263. // Each VI in VariableOffsets has a set of potential constant values. Every
  1264. // combination of elements, picked one each from these sets, is separately
  1265. // added to the original set of offsets, thus resulting in more offsets.
  1266. for (const auto &VI : VariableOffsets) {
  1267. auto &PotentialConstantsAA = A.getAAFor<AAPotentialConstantValues>(
  1268. *this, IRPosition::value(*VI.first), DepClassTy::OPTIONAL);
  1269. if (!PotentialConstantsAA.isValidState()) {
  1270. UsrOI.setUnknown();
  1271. return true;
  1272. }
  1273. // UndefValue is treated as a zero, which leaves Union as is.
  1274. if (PotentialConstantsAA.undefIsContained())
  1275. continue;
  1276. // We need at least one constant in every set to compute an actual offset.
  1277. // Otherwise, we end up pessimizing AAPointerInfo by respecting offsets that
  1278. // don't actually exist. In other words, the absence of constant values
  1279. // implies that the operation can be assumed dead for now.
  1280. auto &AssumedSet = PotentialConstantsAA.getAssumedSet();
  1281. if (AssumedSet.empty())
  1282. return false;
  1283. OffsetInfo Product;
  1284. for (const auto &ConstOffset : AssumedSet) {
  1285. auto CopyPerOffset = Union;
  1286. CopyPerOffset.addToAll(ConstOffset.getSExtValue() *
  1287. VI.second.getZExtValue());
  1288. Product.merge(CopyPerOffset);
  1289. }
  1290. Union = Product;
  1291. }
  1292. UsrOI = std::move(Union);
  1293. return true;
  1294. }
  1295. ChangeStatus AAPointerInfoFloating::updateImpl(Attributor &A) {
  1296. using namespace AA::PointerInfo;
  1297. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1298. const DataLayout &DL = A.getDataLayout();
  1299. Value &AssociatedValue = getAssociatedValue();
  1300. DenseMap<Value *, OffsetInfo> OffsetInfoMap;
  1301. OffsetInfoMap[&AssociatedValue].insert(0);
  1302. auto HandlePassthroughUser = [&](Value *Usr, Value *CurPtr, bool &Follow) {
  1303. // One does not simply walk into a map and assign a reference to a possibly
  1304. // new location. That can cause an invalidation before the assignment
  1305. // happens, like so:
  1306. //
  1307. // OffsetInfoMap[Usr] = OffsetInfoMap[CurPtr]; /* bad idea! */
  1308. //
  1309. // The RHS is a reference that may be invalidated by an insertion caused by
  1310. // the LHS. So we ensure that the side-effect of the LHS happens first.
  1311. auto &UsrOI = OffsetInfoMap[Usr];
  1312. auto &PtrOI = OffsetInfoMap[CurPtr];
  1313. assert(!PtrOI.isUnassigned() &&
  1314. "Cannot pass through if the input Ptr was not visited!");
  1315. UsrOI = PtrOI;
  1316. Follow = true;
  1317. return true;
  1318. };
  1319. const auto *F = getAnchorScope();
  1320. const auto *CI =
  1321. F ? A.getInfoCache().getAnalysisResultForFunction<CycleAnalysis>(*F)
  1322. : nullptr;
  1323. const auto *TLI =
  1324. F ? A.getInfoCache().getTargetLibraryInfoForFunction(*F) : nullptr;
  1325. auto UsePred = [&](const Use &U, bool &Follow) -> bool {
  1326. Value *CurPtr = U.get();
  1327. User *Usr = U.getUser();
  1328. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in " << *Usr
  1329. << "\n");
  1330. assert(OffsetInfoMap.count(CurPtr) &&
  1331. "The current pointer offset should have been seeded!");
  1332. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) {
  1333. if (CE->isCast())
  1334. return HandlePassthroughUser(Usr, CurPtr, Follow);
  1335. if (CE->isCompare())
  1336. return true;
  1337. if (!isa<GEPOperator>(CE)) {
  1338. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled constant user " << *CE
  1339. << "\n");
  1340. return false;
  1341. }
  1342. }
  1343. if (auto *GEP = dyn_cast<GEPOperator>(Usr)) {
  1344. // Note the order here, the Usr access might change the map, CurPtr is
  1345. // already in it though.
  1346. auto &UsrOI = OffsetInfoMap[Usr];
  1347. auto &PtrOI = OffsetInfoMap[CurPtr];
  1348. if (UsrOI.isUnknown())
  1349. return true;
  1350. if (PtrOI.isUnknown()) {
  1351. Follow = true;
  1352. UsrOI.setUnknown();
  1353. return true;
  1354. }
  1355. Follow = collectConstantsForGEP(A, DL, UsrOI, PtrOI, GEP);
  1356. return true;
  1357. }
  1358. if (isa<PtrToIntInst>(Usr))
  1359. return false;
  1360. if (isa<CastInst>(Usr) || isa<SelectInst>(Usr) || isa<ReturnInst>(Usr))
  1361. return HandlePassthroughUser(Usr, CurPtr, Follow);
  1362. // For PHIs we need to take care of the recurrence explicitly as the value
  1363. // might change while we iterate through a loop. For now, we give up if
  1364. // the PHI is not invariant.
  1365. if (isa<PHINode>(Usr)) {
  1366. // Note the order here, the Usr access might change the map, CurPtr is
  1367. // already in it though.
  1368. bool IsFirstPHIUser = !OffsetInfoMap.count(Usr);
  1369. auto &UsrOI = OffsetInfoMap[Usr];
  1370. auto &PtrOI = OffsetInfoMap[CurPtr];
  1371. // Check if the PHI operand has already an unknown offset as we can't
  1372. // improve on that anymore.
  1373. if (PtrOI.isUnknown()) {
  1374. LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI operand offset unknown "
  1375. << *CurPtr << " in " << *Usr << "\n");
  1376. Follow = !UsrOI.isUnknown();
  1377. UsrOI.setUnknown();
  1378. return true;
  1379. }
  1380. // Check if the PHI is invariant (so far).
  1381. if (UsrOI == PtrOI) {
  1382. assert(!PtrOI.isUnassigned() &&
  1383. "Cannot assign if the current Ptr was not visited!");
  1384. LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI is invariant (so far)");
  1385. return true;
  1386. }
  1387. // Check if the PHI operand can be traced back to AssociatedValue.
  1388. APInt Offset(
  1389. DL.getIndexSizeInBits(CurPtr->getType()->getPointerAddressSpace()),
  1390. 0);
  1391. Value *CurPtrBase = CurPtr->stripAndAccumulateConstantOffsets(
  1392. DL, Offset, /* AllowNonInbounds */ true);
  1393. auto It = OffsetInfoMap.find(CurPtrBase);
  1394. if (It == OffsetInfoMap.end()) {
  1395. LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI operand is too complex "
  1396. << *CurPtr << " in " << *Usr << "\n");
  1397. UsrOI.setUnknown();
  1398. Follow = true;
  1399. return true;
  1400. }
  1401. auto mayBeInCycleHeader = [](const CycleInfo *CI, const Instruction *I) {
  1402. if (!CI)
  1403. return true;
  1404. auto *BB = I->getParent();
  1405. auto *C = CI->getCycle(BB);
  1406. if (!C)
  1407. return false;
  1408. return BB == C->getHeader();
  1409. };
  1410. // Check if the PHI operand is not dependent on the PHI itself. Every
  1411. // recurrence is a cyclic net of PHIs in the data flow, and has an
  1412. // equivalent Cycle in the control flow. One of those PHIs must be in the
  1413. // header of that control flow Cycle. This is independent of the choice of
  1414. // Cycles reported by CycleInfo. It is sufficient to check the PHIs in
  1415. // every Cycle header; if such a node is marked unknown, this will
  1416. // eventually propagate through the whole net of PHIs in the recurrence.
  1417. if (mayBeInCycleHeader(CI, cast<Instruction>(Usr))) {
  1418. auto BaseOI = It->getSecond();
  1419. BaseOI.addToAll(Offset.getZExtValue());
  1420. if (IsFirstPHIUser || BaseOI == UsrOI) {
  1421. LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI is invariant " << *CurPtr
  1422. << " in " << *Usr << "\n");
  1423. return HandlePassthroughUser(Usr, CurPtr, Follow);
  1424. }
  1425. LLVM_DEBUG(
  1426. dbgs() << "[AAPointerInfo] PHI operand pointer offset mismatch "
  1427. << *CurPtr << " in " << *Usr << "\n");
  1428. UsrOI.setUnknown();
  1429. Follow = true;
  1430. return true;
  1431. }
  1432. UsrOI.merge(PtrOI);
  1433. Follow = true;
  1434. return true;
  1435. }
  1436. if (auto *LoadI = dyn_cast<LoadInst>(Usr)) {
  1437. // If the access is to a pointer that may or may not be the associated
  1438. // value, e.g. due to a PHI, we cannot assume it will be read.
  1439. AccessKind AK = AccessKind::AK_R;
  1440. if (getUnderlyingObject(CurPtr) == &AssociatedValue)
  1441. AK = AccessKind(AK | AccessKind::AK_MUST);
  1442. else
  1443. AK = AccessKind(AK | AccessKind::AK_MAY);
  1444. if (!handleAccess(A, *LoadI, /* Content */ nullptr, AK,
  1445. OffsetInfoMap[CurPtr].Offsets, Changed,
  1446. *LoadI->getType()))
  1447. return false;
  1448. auto IsAssumption = [](Instruction &I) {
  1449. if (auto *II = dyn_cast<IntrinsicInst>(&I))
  1450. return II->isAssumeLikeIntrinsic();
  1451. return false;
  1452. };
  1453. auto IsImpactedInRange = [&](Instruction *FromI, Instruction *ToI) {
  1454. // Check if the assumption and the load are executed together without
  1455. // memory modification.
  1456. do {
  1457. if (FromI->mayWriteToMemory() && !IsAssumption(*FromI))
  1458. return true;
  1459. FromI = FromI->getNextNonDebugInstruction();
  1460. } while (FromI && FromI != ToI);
  1461. return false;
  1462. };
  1463. BasicBlock *BB = LoadI->getParent();
  1464. auto IsValidAssume = [&](IntrinsicInst &IntrI) {
  1465. if (IntrI.getIntrinsicID() != Intrinsic::assume)
  1466. return false;
  1467. BasicBlock *IntrBB = IntrI.getParent();
  1468. if (IntrI.getParent() == BB) {
  1469. if (IsImpactedInRange(LoadI->getNextNonDebugInstruction(), &IntrI))
  1470. return false;
  1471. } else {
  1472. auto PredIt = pred_begin(IntrBB);
  1473. if ((*PredIt) != BB)
  1474. return false;
  1475. if (++PredIt != pred_end(IntrBB))
  1476. return false;
  1477. for (auto *SuccBB : successors(BB)) {
  1478. if (SuccBB == IntrBB)
  1479. continue;
  1480. if (isa<UnreachableInst>(SuccBB->getTerminator()))
  1481. continue;
  1482. return false;
  1483. }
  1484. if (IsImpactedInRange(LoadI->getNextNonDebugInstruction(),
  1485. BB->getTerminator()))
  1486. return false;
  1487. if (IsImpactedInRange(&IntrBB->front(), &IntrI))
  1488. return false;
  1489. }
  1490. return true;
  1491. };
  1492. std::pair<Value *, IntrinsicInst *> Assumption;
  1493. for (const Use &LoadU : LoadI->uses()) {
  1494. if (auto *CmpI = dyn_cast<CmpInst>(LoadU.getUser())) {
  1495. if (!CmpI->isEquality() || !CmpI->isTrueWhenEqual())
  1496. continue;
  1497. for (const Use &CmpU : CmpI->uses()) {
  1498. if (auto *IntrI = dyn_cast<IntrinsicInst>(CmpU.getUser())) {
  1499. if (!IsValidAssume(*IntrI))
  1500. continue;
  1501. int Idx = CmpI->getOperandUse(0) == LoadU;
  1502. Assumption = {CmpI->getOperand(Idx), IntrI};
  1503. break;
  1504. }
  1505. }
  1506. }
  1507. if (Assumption.first)
  1508. break;
  1509. }
  1510. // Check if we found an assumption associated with this load.
  1511. if (!Assumption.first || !Assumption.second)
  1512. return true;
  1513. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Assumption found "
  1514. << *Assumption.second << ": " << *LoadI
  1515. << " == " << *Assumption.first << "\n");
  1516. return handleAccess(
  1517. A, *Assumption.second, Assumption.first, AccessKind::AK_ASSUMPTION,
  1518. OffsetInfoMap[CurPtr].Offsets, Changed, *LoadI->getType());
  1519. }
  1520. auto HandleStoreLike = [&](Instruction &I, Value *ValueOp, Type &ValueTy,
  1521. ArrayRef<Value *> OtherOps, AccessKind AK) {
  1522. for (auto *OtherOp : OtherOps) {
  1523. if (OtherOp == CurPtr) {
  1524. LLVM_DEBUG(
  1525. dbgs()
  1526. << "[AAPointerInfo] Escaping use in store like instruction " << I
  1527. << "\n");
  1528. return false;
  1529. }
  1530. }
  1531. // If the access is to a pointer that may or may not be the associated
  1532. // value, e.g. due to a PHI, we cannot assume it will be written.
  1533. if (getUnderlyingObject(CurPtr) == &AssociatedValue)
  1534. AK = AccessKind(AK | AccessKind::AK_MUST);
  1535. else
  1536. AK = AccessKind(AK | AccessKind::AK_MAY);
  1537. bool UsedAssumedInformation = false;
  1538. std::optional<Value *> Content = nullptr;
  1539. if (ValueOp)
  1540. Content = A.getAssumedSimplified(
  1541. *ValueOp, *this, UsedAssumedInformation, AA::Interprocedural);
  1542. return handleAccess(A, I, Content, AK, OffsetInfoMap[CurPtr].Offsets,
  1543. Changed, ValueTy);
  1544. };
  1545. if (auto *StoreI = dyn_cast<StoreInst>(Usr))
  1546. return HandleStoreLike(*StoreI, StoreI->getValueOperand(),
  1547. *StoreI->getValueOperand()->getType(),
  1548. {StoreI->getValueOperand()}, AccessKind::AK_W);
  1549. if (auto *RMWI = dyn_cast<AtomicRMWInst>(Usr))
  1550. return HandleStoreLike(*RMWI, nullptr, *RMWI->getValOperand()->getType(),
  1551. {RMWI->getValOperand()}, AccessKind::AK_RW);
  1552. if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(Usr))
  1553. return HandleStoreLike(
  1554. *CXI, nullptr, *CXI->getNewValOperand()->getType(),
  1555. {CXI->getCompareOperand(), CXI->getNewValOperand()},
  1556. AccessKind::AK_RW);
  1557. if (auto *CB = dyn_cast<CallBase>(Usr)) {
  1558. if (CB->isLifetimeStartOrEnd())
  1559. return true;
  1560. if (getFreedOperand(CB, TLI) == U)
  1561. return true;
  1562. if (CB->isArgOperand(&U)) {
  1563. unsigned ArgNo = CB->getArgOperandNo(&U);
  1564. const auto &CSArgPI = A.getAAFor<AAPointerInfo>(
  1565. *this, IRPosition::callsite_argument(*CB, ArgNo),
  1566. DepClassTy::REQUIRED);
  1567. Changed = translateAndAddState(A, CSArgPI, OffsetInfoMap[CurPtr], *CB) |
  1568. Changed;
  1569. return isValidState();
  1570. }
  1571. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Call user not handled " << *CB
  1572. << "\n");
  1573. // TODO: Allow some call uses
  1574. return false;
  1575. }
  1576. LLVM_DEBUG(dbgs() << "[AAPointerInfo] User not handled " << *Usr << "\n");
  1577. return false;
  1578. };
  1579. auto EquivalentUseCB = [&](const Use &OldU, const Use &NewU) {
  1580. assert(OffsetInfoMap.count(OldU) && "Old use should be known already!");
  1581. if (OffsetInfoMap.count(NewU)) {
  1582. LLVM_DEBUG({
  1583. if (!(OffsetInfoMap[NewU] == OffsetInfoMap[OldU])) {
  1584. dbgs() << "[AAPointerInfo] Equivalent use callback failed: "
  1585. << OffsetInfoMap[NewU] << " vs " << OffsetInfoMap[OldU]
  1586. << "\n";
  1587. }
  1588. });
  1589. return OffsetInfoMap[NewU] == OffsetInfoMap[OldU];
  1590. }
  1591. OffsetInfoMap[NewU] = OffsetInfoMap[OldU];
  1592. return true;
  1593. };
  1594. if (!A.checkForAllUses(UsePred, *this, AssociatedValue,
  1595. /* CheckBBLivenessOnly */ true, DepClassTy::OPTIONAL,
  1596. /* IgnoreDroppableUses */ true, EquivalentUseCB)) {
  1597. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Check for all uses failed, abort!\n");
  1598. return indicatePessimisticFixpoint();
  1599. }
  1600. LLVM_DEBUG({
  1601. dbgs() << "Accesses by bin after update:\n";
  1602. dumpState(dbgs());
  1603. });
  1604. return Changed;
  1605. }
  1606. struct AAPointerInfoReturned final : AAPointerInfoImpl {
  1607. AAPointerInfoReturned(const IRPosition &IRP, Attributor &A)
  1608. : AAPointerInfoImpl(IRP, A) {}
  1609. /// See AbstractAttribute::updateImpl(...).
  1610. ChangeStatus updateImpl(Attributor &A) override {
  1611. return indicatePessimisticFixpoint();
  1612. }
  1613. /// See AbstractAttribute::trackStatistics()
  1614. void trackStatistics() const override {
  1615. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1616. }
  1617. };
  1618. struct AAPointerInfoArgument final : AAPointerInfoFloating {
  1619. AAPointerInfoArgument(const IRPosition &IRP, Attributor &A)
  1620. : AAPointerInfoFloating(IRP, A) {}
  1621. /// See AbstractAttribute::initialize(...).
  1622. void initialize(Attributor &A) override {
  1623. AAPointerInfoFloating::initialize(A);
  1624. if (getAnchorScope()->isDeclaration())
  1625. indicatePessimisticFixpoint();
  1626. }
  1627. /// See AbstractAttribute::trackStatistics()
  1628. void trackStatistics() const override {
  1629. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1630. }
  1631. };
  1632. struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating {
  1633. AAPointerInfoCallSiteArgument(const IRPosition &IRP, Attributor &A)
  1634. : AAPointerInfoFloating(IRP, A) {}
  1635. /// See AbstractAttribute::updateImpl(...).
  1636. ChangeStatus updateImpl(Attributor &A) override {
  1637. using namespace AA::PointerInfo;
  1638. // We handle memory intrinsics explicitly, at least the first (=
  1639. // destination) and second (=source) arguments as we know how they are
  1640. // accessed.
  1641. if (auto *MI = dyn_cast_or_null<MemIntrinsic>(getCtxI())) {
  1642. ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
  1643. int64_t LengthVal = AA::RangeTy::Unknown;
  1644. if (Length)
  1645. LengthVal = Length->getSExtValue();
  1646. unsigned ArgNo = getIRPosition().getCallSiteArgNo();
  1647. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1648. if (ArgNo > 1) {
  1649. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "
  1650. << *MI << "\n");
  1651. return indicatePessimisticFixpoint();
  1652. } else {
  1653. auto Kind =
  1654. ArgNo == 0 ? AccessKind::AK_MUST_WRITE : AccessKind::AK_MUST_READ;
  1655. Changed =
  1656. Changed | addAccess(A, {0, LengthVal}, *MI, nullptr, Kind, nullptr);
  1657. }
  1658. LLVM_DEBUG({
  1659. dbgs() << "Accesses by bin after update:\n";
  1660. dumpState(dbgs());
  1661. });
  1662. return Changed;
  1663. }
  1664. // TODO: Once we have call site specific value information we can provide
  1665. // call site specific liveness information and then it makes
  1666. // sense to specialize attributes for call sites arguments instead of
  1667. // redirecting requests to the callee argument.
  1668. Argument *Arg = getAssociatedArgument();
  1669. if (Arg) {
  1670. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  1671. auto &ArgAA =
  1672. A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
  1673. if (ArgAA.getState().isValidState())
  1674. return translateAndAddStateFromCallee(A, ArgAA,
  1675. *cast<CallBase>(getCtxI()));
  1676. if (!Arg->getParent()->isDeclaration())
  1677. return indicatePessimisticFixpoint();
  1678. }
  1679. const auto &NoCaptureAA =
  1680. A.getAAFor<AANoCapture>(*this, getIRPosition(), DepClassTy::OPTIONAL);
  1681. if (!NoCaptureAA.isAssumedNoCapture())
  1682. return indicatePessimisticFixpoint();
  1683. bool IsKnown = false;
  1684. if (AA::isAssumedReadNone(A, getIRPosition(), *this, IsKnown))
  1685. return ChangeStatus::UNCHANGED;
  1686. bool ReadOnly = AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown);
  1687. auto Kind =
  1688. ReadOnly ? AccessKind::AK_MAY_READ : AccessKind::AK_MAY_READ_WRITE;
  1689. return addAccess(A, AA::RangeTy::getUnknown(), *getCtxI(), nullptr, Kind,
  1690. nullptr);
  1691. }
  1692. /// See AbstractAttribute::trackStatistics()
  1693. void trackStatistics() const override {
  1694. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1695. }
  1696. };
  1697. struct AAPointerInfoCallSiteReturned final : AAPointerInfoFloating {
  1698. AAPointerInfoCallSiteReturned(const IRPosition &IRP, Attributor &A)
  1699. : AAPointerInfoFloating(IRP, A) {}
  1700. /// See AbstractAttribute::trackStatistics()
  1701. void trackStatistics() const override {
  1702. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1703. }
  1704. };
  1705. } // namespace
  1706. /// -----------------------NoUnwind Function Attribute--------------------------
  1707. namespace {
  1708. struct AANoUnwindImpl : AANoUnwind {
  1709. AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {}
  1710. const std::string getAsStr() const override {
  1711. return getAssumed() ? "nounwind" : "may-unwind";
  1712. }
  1713. /// See AbstractAttribute::updateImpl(...).
  1714. ChangeStatus updateImpl(Attributor &A) override {
  1715. auto Opcodes = {
  1716. (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
  1717. (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
  1718. (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
  1719. auto CheckForNoUnwind = [&](Instruction &I) {
  1720. if (!I.mayThrow())
  1721. return true;
  1722. if (const auto *CB = dyn_cast<CallBase>(&I)) {
  1723. const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
  1724. *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
  1725. return NoUnwindAA.isAssumedNoUnwind();
  1726. }
  1727. return false;
  1728. };
  1729. bool UsedAssumedInformation = false;
  1730. if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes,
  1731. UsedAssumedInformation))
  1732. return indicatePessimisticFixpoint();
  1733. return ChangeStatus::UNCHANGED;
  1734. }
  1735. };
  1736. struct AANoUnwindFunction final : public AANoUnwindImpl {
  1737. AANoUnwindFunction(const IRPosition &IRP, Attributor &A)
  1738. : AANoUnwindImpl(IRP, A) {}
  1739. /// See AbstractAttribute::trackStatistics()
  1740. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
  1741. };
  1742. /// NoUnwind attribute deduction for a call sites.
  1743. struct AANoUnwindCallSite final : AANoUnwindImpl {
  1744. AANoUnwindCallSite(const IRPosition &IRP, Attributor &A)
  1745. : AANoUnwindImpl(IRP, A) {}
  1746. /// See AbstractAttribute::initialize(...).
  1747. void initialize(Attributor &A) override {
  1748. AANoUnwindImpl::initialize(A);
  1749. Function *F = getAssociatedFunction();
  1750. if (!F || F->isDeclaration())
  1751. indicatePessimisticFixpoint();
  1752. }
  1753. /// See AbstractAttribute::updateImpl(...).
  1754. ChangeStatus updateImpl(Attributor &A) override {
  1755. // TODO: Once we have call site specific value information we can provide
  1756. // call site specific liveness information and then it makes
  1757. // sense to specialize attributes for call sites arguments instead of
  1758. // redirecting requests to the callee argument.
  1759. Function *F = getAssociatedFunction();
  1760. const IRPosition &FnPos = IRPosition::function(*F);
  1761. auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED);
  1762. return clampStateAndIndicateChange(getState(), FnAA.getState());
  1763. }
  1764. /// See AbstractAttribute::trackStatistics()
  1765. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
  1766. };
  1767. } // namespace
  1768. /// --------------------- Function Return Values -------------------------------
  1769. namespace {
  1770. /// "Attribute" that collects all potential returned values and the return
  1771. /// instructions that they arise from.
  1772. ///
  1773. /// If there is a unique returned value R, the manifest method will:
  1774. /// - mark R with the "returned" attribute, if R is an argument.
  1775. class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
  1776. /// Mapping of values potentially returned by the associated function to the
  1777. /// return instructions that might return them.
  1778. MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
  1779. /// State flags
  1780. ///
  1781. ///{
  1782. bool IsFixed = false;
  1783. bool IsValidState = true;
  1784. ///}
  1785. public:
  1786. AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A)
  1787. : AAReturnedValues(IRP, A) {}
  1788. /// See AbstractAttribute::initialize(...).
  1789. void initialize(Attributor &A) override {
  1790. // Reset the state.
  1791. IsFixed = false;
  1792. IsValidState = true;
  1793. ReturnedValues.clear();
  1794. Function *F = getAssociatedFunction();
  1795. if (!F || F->isDeclaration()) {
  1796. indicatePessimisticFixpoint();
  1797. return;
  1798. }
  1799. assert(!F->getReturnType()->isVoidTy() &&
  1800. "Did not expect a void return type!");
  1801. // The map from instruction opcodes to those instructions in the function.
  1802. auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
  1803. // Look through all arguments, if one is marked as returned we are done.
  1804. for (Argument &Arg : F->args()) {
  1805. if (Arg.hasReturnedAttr()) {
  1806. auto &ReturnInstSet = ReturnedValues[&Arg];
  1807. if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret))
  1808. for (Instruction *RI : *Insts)
  1809. ReturnInstSet.insert(cast<ReturnInst>(RI));
  1810. indicateOptimisticFixpoint();
  1811. return;
  1812. }
  1813. }
  1814. if (!A.isFunctionIPOAmendable(*F))
  1815. indicatePessimisticFixpoint();
  1816. }
  1817. /// See AbstractAttribute::manifest(...).
  1818. ChangeStatus manifest(Attributor &A) override;
  1819. /// See AbstractAttribute::getState(...).
  1820. AbstractState &getState() override { return *this; }
  1821. /// See AbstractAttribute::getState(...).
  1822. const AbstractState &getState() const override { return *this; }
  1823. /// See AbstractAttribute::updateImpl(Attributor &A).
  1824. ChangeStatus updateImpl(Attributor &A) override;
  1825. llvm::iterator_range<iterator> returned_values() override {
  1826. return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
  1827. }
  1828. llvm::iterator_range<const_iterator> returned_values() const override {
  1829. return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
  1830. }
  1831. /// Return the number of potential return values, -1 if unknown.
  1832. size_t getNumReturnValues() const override {
  1833. return isValidState() ? ReturnedValues.size() : -1;
  1834. }
  1835. /// Return an assumed unique return value if a single candidate is found. If
  1836. /// there cannot be one, return a nullptr. If it is not clear yet, return
  1837. /// std::nullopt.
  1838. std::optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
  1839. /// See AbstractState::checkForAllReturnedValues(...).
  1840. bool checkForAllReturnedValuesAndReturnInsts(
  1841. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
  1842. const override;
  1843. /// Pretty print the attribute similar to the IR representation.
  1844. const std::string getAsStr() const override;
  1845. /// See AbstractState::isAtFixpoint().
  1846. bool isAtFixpoint() const override { return IsFixed; }
  1847. /// See AbstractState::isValidState().
  1848. bool isValidState() const override { return IsValidState; }
  1849. /// See AbstractState::indicateOptimisticFixpoint(...).
  1850. ChangeStatus indicateOptimisticFixpoint() override {
  1851. IsFixed = true;
  1852. return ChangeStatus::UNCHANGED;
  1853. }
  1854. ChangeStatus indicatePessimisticFixpoint() override {
  1855. IsFixed = true;
  1856. IsValidState = false;
  1857. return ChangeStatus::CHANGED;
  1858. }
  1859. };
  1860. ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
  1861. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1862. // Bookkeeping.
  1863. assert(isValidState());
  1864. STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
  1865. "Number of function with known return values");
  1866. // Check if we have an assumed unique return value that we could manifest.
  1867. std::optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
  1868. if (!UniqueRV || !*UniqueRV)
  1869. return Changed;
  1870. // Bookkeeping.
  1871. STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
  1872. "Number of function with unique return");
  1873. // If the assumed unique return value is an argument, annotate it.
  1874. if (auto *UniqueRVArg = dyn_cast<Argument>(*UniqueRV)) {
  1875. if (UniqueRVArg->getType()->canLosslesslyBitCastTo(
  1876. getAssociatedFunction()->getReturnType())) {
  1877. getIRPosition() = IRPosition::argument(*UniqueRVArg);
  1878. Changed = IRAttribute::manifest(A);
  1879. }
  1880. }
  1881. return Changed;
  1882. }
  1883. const std::string AAReturnedValuesImpl::getAsStr() const {
  1884. return (isAtFixpoint() ? "returns(#" : "may-return(#") +
  1885. (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
  1886. }
  1887. std::optional<Value *>
  1888. AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
  1889. // If checkForAllReturnedValues provides a unique value, ignoring potential
  1890. // undef values that can also be present, it is assumed to be the actual
  1891. // return value and forwarded to the caller of this method. If there are
  1892. // multiple, a nullptr is returned indicating there cannot be a unique
  1893. // returned value.
  1894. std::optional<Value *> UniqueRV;
  1895. Type *Ty = getAssociatedFunction()->getReturnType();
  1896. auto Pred = [&](Value &RV) -> bool {
  1897. UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty);
  1898. return UniqueRV != std::optional<Value *>(nullptr);
  1899. };
  1900. if (!A.checkForAllReturnedValues(Pred, *this))
  1901. UniqueRV = nullptr;
  1902. return UniqueRV;
  1903. }
  1904. bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
  1905. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
  1906. const {
  1907. if (!isValidState())
  1908. return false;
  1909. // Check all returned values but ignore call sites as long as we have not
  1910. // encountered an overdefined one during an update.
  1911. for (const auto &It : ReturnedValues) {
  1912. Value *RV = It.first;
  1913. if (!Pred(*RV, It.second))
  1914. return false;
  1915. }
  1916. return true;
  1917. }
  1918. ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
  1919. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1920. SmallVector<AA::ValueAndContext> Values;
  1921. bool UsedAssumedInformation = false;
  1922. auto ReturnInstCB = [&](Instruction &I) {
  1923. ReturnInst &Ret = cast<ReturnInst>(I);
  1924. Values.clear();
  1925. if (!A.getAssumedSimplifiedValues(IRPosition::value(*Ret.getReturnValue()),
  1926. *this, Values, AA::Intraprocedural,
  1927. UsedAssumedInformation))
  1928. Values.push_back({*Ret.getReturnValue(), Ret});
  1929. for (auto &VAC : Values) {
  1930. assert(AA::isValidInScope(*VAC.getValue(), Ret.getFunction()) &&
  1931. "Assumed returned value should be valid in function scope!");
  1932. if (ReturnedValues[VAC.getValue()].insert(&Ret))
  1933. Changed = ChangeStatus::CHANGED;
  1934. }
  1935. return true;
  1936. };
  1937. // Discover returned values from all live returned instructions in the
  1938. // associated function.
  1939. if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret},
  1940. UsedAssumedInformation))
  1941. return indicatePessimisticFixpoint();
  1942. return Changed;
  1943. }
  1944. struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
  1945. AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A)
  1946. : AAReturnedValuesImpl(IRP, A) {}
  1947. /// See AbstractAttribute::trackStatistics()
  1948. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
  1949. };
  1950. /// Returned values information for a call sites.
  1951. struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
  1952. AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A)
  1953. : AAReturnedValuesImpl(IRP, A) {}
  1954. /// See AbstractAttribute::initialize(...).
  1955. void initialize(Attributor &A) override {
  1956. // TODO: Once we have call site specific value information we can provide
  1957. // call site specific liveness information and then it makes
  1958. // sense to specialize attributes for call sites instead of
  1959. // redirecting requests to the callee.
  1960. llvm_unreachable("Abstract attributes for returned values are not "
  1961. "supported for call sites yet!");
  1962. }
  1963. /// See AbstractAttribute::updateImpl(...).
  1964. ChangeStatus updateImpl(Attributor &A) override {
  1965. return indicatePessimisticFixpoint();
  1966. }
  1967. /// See AbstractAttribute::trackStatistics()
  1968. void trackStatistics() const override {}
  1969. };
  1970. } // namespace
  1971. /// ------------------------ NoSync Function Attribute -------------------------
  1972. bool AANoSync::isAlignedBarrier(const CallBase &CB, bool ExecutedAligned) {
  1973. switch (CB.getIntrinsicID()) {
  1974. case Intrinsic::nvvm_barrier0:
  1975. case Intrinsic::nvvm_barrier0_and:
  1976. case Intrinsic::nvvm_barrier0_or:
  1977. case Intrinsic::nvvm_barrier0_popc:
  1978. return true;
  1979. case Intrinsic::amdgcn_s_barrier:
  1980. if (ExecutedAligned)
  1981. return true;
  1982. break;
  1983. default:
  1984. break;
  1985. }
  1986. return hasAssumption(CB, KnownAssumptionString("ompx_aligned_barrier"));
  1987. }
  1988. bool AANoSync::isNonRelaxedAtomic(const Instruction *I) {
  1989. if (!I->isAtomic())
  1990. return false;
  1991. if (auto *FI = dyn_cast<FenceInst>(I))
  1992. // All legal orderings for fence are stronger than monotonic.
  1993. return FI->getSyncScopeID() != SyncScope::SingleThread;
  1994. if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
  1995. // Unordered is not a legal ordering for cmpxchg.
  1996. return (AI->getSuccessOrdering() != AtomicOrdering::Monotonic ||
  1997. AI->getFailureOrdering() != AtomicOrdering::Monotonic);
  1998. }
  1999. AtomicOrdering Ordering;
  2000. switch (I->getOpcode()) {
  2001. case Instruction::AtomicRMW:
  2002. Ordering = cast<AtomicRMWInst>(I)->getOrdering();
  2003. break;
  2004. case Instruction::Store:
  2005. Ordering = cast<StoreInst>(I)->getOrdering();
  2006. break;
  2007. case Instruction::Load:
  2008. Ordering = cast<LoadInst>(I)->getOrdering();
  2009. break;
  2010. default:
  2011. llvm_unreachable(
  2012. "New atomic operations need to be known in the attributor.");
  2013. }
  2014. return (Ordering != AtomicOrdering::Unordered &&
  2015. Ordering != AtomicOrdering::Monotonic);
  2016. }
  2017. /// Return true if this intrinsic is nosync. This is only used for intrinsics
  2018. /// which would be nosync except that they have a volatile flag. All other
  2019. /// intrinsics are simply annotated with the nosync attribute in Intrinsics.td.
  2020. bool AANoSync::isNoSyncIntrinsic(const Instruction *I) {
  2021. if (auto *MI = dyn_cast<MemIntrinsic>(I))
  2022. return !MI->isVolatile();
  2023. return false;
  2024. }
  2025. namespace {
  2026. struct AANoSyncImpl : AANoSync {
  2027. AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {}
  2028. const std::string getAsStr() const override {
  2029. return getAssumed() ? "nosync" : "may-sync";
  2030. }
  2031. /// See AbstractAttribute::updateImpl(...).
  2032. ChangeStatus updateImpl(Attributor &A) override;
  2033. };
  2034. ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
  2035. auto CheckRWInstForNoSync = [&](Instruction &I) {
  2036. return AA::isNoSyncInst(A, I, *this);
  2037. };
  2038. auto CheckForNoSync = [&](Instruction &I) {
  2039. // At this point we handled all read/write effects and they are all
  2040. // nosync, so they can be skipped.
  2041. if (I.mayReadOrWriteMemory())
  2042. return true;
  2043. // non-convergent and readnone imply nosync.
  2044. return !cast<CallBase>(I).isConvergent();
  2045. };
  2046. bool UsedAssumedInformation = false;
  2047. if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this,
  2048. UsedAssumedInformation) ||
  2049. !A.checkForAllCallLikeInstructions(CheckForNoSync, *this,
  2050. UsedAssumedInformation))
  2051. return indicatePessimisticFixpoint();
  2052. return ChangeStatus::UNCHANGED;
  2053. }
  2054. struct AANoSyncFunction final : public AANoSyncImpl {
  2055. AANoSyncFunction(const IRPosition &IRP, Attributor &A)
  2056. : AANoSyncImpl(IRP, A) {}
  2057. /// See AbstractAttribute::trackStatistics()
  2058. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
  2059. };
  2060. /// NoSync attribute deduction for a call sites.
  2061. struct AANoSyncCallSite final : AANoSyncImpl {
  2062. AANoSyncCallSite(const IRPosition &IRP, Attributor &A)
  2063. : AANoSyncImpl(IRP, A) {}
  2064. /// See AbstractAttribute::initialize(...).
  2065. void initialize(Attributor &A) override {
  2066. AANoSyncImpl::initialize(A);
  2067. Function *F = getAssociatedFunction();
  2068. if (!F || F->isDeclaration())
  2069. indicatePessimisticFixpoint();
  2070. }
  2071. /// See AbstractAttribute::updateImpl(...).
  2072. ChangeStatus updateImpl(Attributor &A) override {
  2073. // TODO: Once we have call site specific value information we can provide
  2074. // call site specific liveness information and then it makes
  2075. // sense to specialize attributes for call sites arguments instead of
  2076. // redirecting requests to the callee argument.
  2077. Function *F = getAssociatedFunction();
  2078. const IRPosition &FnPos = IRPosition::function(*F);
  2079. auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED);
  2080. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2081. }
  2082. /// See AbstractAttribute::trackStatistics()
  2083. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
  2084. };
  2085. } // namespace
  2086. /// ------------------------ No-Free Attributes ----------------------------
  2087. namespace {
  2088. struct AANoFreeImpl : public AANoFree {
  2089. AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {}
  2090. /// See AbstractAttribute::updateImpl(...).
  2091. ChangeStatus updateImpl(Attributor &A) override {
  2092. auto CheckForNoFree = [&](Instruction &I) {
  2093. const auto &CB = cast<CallBase>(I);
  2094. if (CB.hasFnAttr(Attribute::NoFree))
  2095. return true;
  2096. const auto &NoFreeAA = A.getAAFor<AANoFree>(
  2097. *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
  2098. return NoFreeAA.isAssumedNoFree();
  2099. };
  2100. bool UsedAssumedInformation = false;
  2101. if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this,
  2102. UsedAssumedInformation))
  2103. return indicatePessimisticFixpoint();
  2104. return ChangeStatus::UNCHANGED;
  2105. }
  2106. /// See AbstractAttribute::getAsStr().
  2107. const std::string getAsStr() const override {
  2108. return getAssumed() ? "nofree" : "may-free";
  2109. }
  2110. };
  2111. struct AANoFreeFunction final : public AANoFreeImpl {
  2112. AANoFreeFunction(const IRPosition &IRP, Attributor &A)
  2113. : AANoFreeImpl(IRP, A) {}
  2114. /// See AbstractAttribute::trackStatistics()
  2115. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
  2116. };
  2117. /// NoFree attribute deduction for a call sites.
  2118. struct AANoFreeCallSite final : AANoFreeImpl {
  2119. AANoFreeCallSite(const IRPosition &IRP, Attributor &A)
  2120. : AANoFreeImpl(IRP, A) {}
  2121. /// See AbstractAttribute::initialize(...).
  2122. void initialize(Attributor &A) override {
  2123. AANoFreeImpl::initialize(A);
  2124. Function *F = getAssociatedFunction();
  2125. if (!F || F->isDeclaration())
  2126. indicatePessimisticFixpoint();
  2127. }
  2128. /// See AbstractAttribute::updateImpl(...).
  2129. ChangeStatus updateImpl(Attributor &A) override {
  2130. // TODO: Once we have call site specific value information we can provide
  2131. // call site specific liveness information and then it makes
  2132. // sense to specialize attributes for call sites arguments instead of
  2133. // redirecting requests to the callee argument.
  2134. Function *F = getAssociatedFunction();
  2135. const IRPosition &FnPos = IRPosition::function(*F);
  2136. auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED);
  2137. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2138. }
  2139. /// See AbstractAttribute::trackStatistics()
  2140. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
  2141. };
  2142. /// NoFree attribute for floating values.
  2143. struct AANoFreeFloating : AANoFreeImpl {
  2144. AANoFreeFloating(const IRPosition &IRP, Attributor &A)
  2145. : AANoFreeImpl(IRP, A) {}
  2146. /// See AbstractAttribute::trackStatistics()
  2147. void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
  2148. /// See Abstract Attribute::updateImpl(...).
  2149. ChangeStatus updateImpl(Attributor &A) override {
  2150. const IRPosition &IRP = getIRPosition();
  2151. const auto &NoFreeAA = A.getAAFor<AANoFree>(
  2152. *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL);
  2153. if (NoFreeAA.isAssumedNoFree())
  2154. return ChangeStatus::UNCHANGED;
  2155. Value &AssociatedValue = getIRPosition().getAssociatedValue();
  2156. auto Pred = [&](const Use &U, bool &Follow) -> bool {
  2157. Instruction *UserI = cast<Instruction>(U.getUser());
  2158. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  2159. if (CB->isBundleOperand(&U))
  2160. return false;
  2161. if (!CB->isArgOperand(&U))
  2162. return true;
  2163. unsigned ArgNo = CB->getArgOperandNo(&U);
  2164. const auto &NoFreeArg = A.getAAFor<AANoFree>(
  2165. *this, IRPosition::callsite_argument(*CB, ArgNo),
  2166. DepClassTy::REQUIRED);
  2167. return NoFreeArg.isAssumedNoFree();
  2168. }
  2169. if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
  2170. isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
  2171. Follow = true;
  2172. return true;
  2173. }
  2174. if (isa<StoreInst>(UserI) || isa<LoadInst>(UserI) ||
  2175. isa<ReturnInst>(UserI))
  2176. return true;
  2177. // Unknown user.
  2178. return false;
  2179. };
  2180. if (!A.checkForAllUses(Pred, *this, AssociatedValue))
  2181. return indicatePessimisticFixpoint();
  2182. return ChangeStatus::UNCHANGED;
  2183. }
  2184. };
  2185. /// NoFree attribute for a call site argument.
  2186. struct AANoFreeArgument final : AANoFreeFloating {
  2187. AANoFreeArgument(const IRPosition &IRP, Attributor &A)
  2188. : AANoFreeFloating(IRP, A) {}
  2189. /// See AbstractAttribute::trackStatistics()
  2190. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
  2191. };
  2192. /// NoFree attribute for call site arguments.
  2193. struct AANoFreeCallSiteArgument final : AANoFreeFloating {
  2194. AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A)
  2195. : AANoFreeFloating(IRP, A) {}
  2196. /// See AbstractAttribute::updateImpl(...).
  2197. ChangeStatus updateImpl(Attributor &A) override {
  2198. // TODO: Once we have call site specific value information we can provide
  2199. // call site specific liveness information and then it makes
  2200. // sense to specialize attributes for call sites arguments instead of
  2201. // redirecting requests to the callee argument.
  2202. Argument *Arg = getAssociatedArgument();
  2203. if (!Arg)
  2204. return indicatePessimisticFixpoint();
  2205. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  2206. auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED);
  2207. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  2208. }
  2209. /// See AbstractAttribute::trackStatistics()
  2210. void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
  2211. };
  2212. /// NoFree attribute for function return value.
  2213. struct AANoFreeReturned final : AANoFreeFloating {
  2214. AANoFreeReturned(const IRPosition &IRP, Attributor &A)
  2215. : AANoFreeFloating(IRP, A) {
  2216. llvm_unreachable("NoFree is not applicable to function returns!");
  2217. }
  2218. /// See AbstractAttribute::initialize(...).
  2219. void initialize(Attributor &A) override {
  2220. llvm_unreachable("NoFree is not applicable to function returns!");
  2221. }
  2222. /// See AbstractAttribute::updateImpl(...).
  2223. ChangeStatus updateImpl(Attributor &A) override {
  2224. llvm_unreachable("NoFree is not applicable to function returns!");
  2225. }
  2226. /// See AbstractAttribute::trackStatistics()
  2227. void trackStatistics() const override {}
  2228. };
  2229. /// NoFree attribute deduction for a call site return value.
  2230. struct AANoFreeCallSiteReturned final : AANoFreeFloating {
  2231. AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A)
  2232. : AANoFreeFloating(IRP, A) {}
  2233. ChangeStatus manifest(Attributor &A) override {
  2234. return ChangeStatus::UNCHANGED;
  2235. }
  2236. /// See AbstractAttribute::trackStatistics()
  2237. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
  2238. };
  2239. } // namespace
  2240. /// ------------------------ NonNull Argument Attribute ------------------------
  2241. namespace {
  2242. static int64_t getKnownNonNullAndDerefBytesForUse(
  2243. Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
  2244. const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
  2245. TrackUse = false;
  2246. const Value *UseV = U->get();
  2247. if (!UseV->getType()->isPointerTy())
  2248. return 0;
  2249. // We need to follow common pointer manipulation uses to the accesses they
  2250. // feed into. We can try to be smart to avoid looking through things we do not
  2251. // like for now, e.g., non-inbounds GEPs.
  2252. if (isa<CastInst>(I)) {
  2253. TrackUse = true;
  2254. return 0;
  2255. }
  2256. if (isa<GetElementPtrInst>(I)) {
  2257. TrackUse = true;
  2258. return 0;
  2259. }
  2260. Type *PtrTy = UseV->getType();
  2261. const Function *F = I->getFunction();
  2262. bool NullPointerIsDefined =
  2263. F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
  2264. const DataLayout &DL = A.getInfoCache().getDL();
  2265. if (const auto *CB = dyn_cast<CallBase>(I)) {
  2266. if (CB->isBundleOperand(U)) {
  2267. if (RetainedKnowledge RK = getKnowledgeFromUse(
  2268. U, {Attribute::NonNull, Attribute::Dereferenceable})) {
  2269. IsNonNull |=
  2270. (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined);
  2271. return RK.ArgValue;
  2272. }
  2273. return 0;
  2274. }
  2275. if (CB->isCallee(U)) {
  2276. IsNonNull |= !NullPointerIsDefined;
  2277. return 0;
  2278. }
  2279. unsigned ArgNo = CB->getArgOperandNo(U);
  2280. IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
  2281. // As long as we only use known information there is no need to track
  2282. // dependences here.
  2283. auto &DerefAA =
  2284. A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE);
  2285. IsNonNull |= DerefAA.isKnownNonNull();
  2286. return DerefAA.getKnownDereferenceableBytes();
  2287. }
  2288. std::optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
  2289. if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
  2290. return 0;
  2291. int64_t Offset;
  2292. const Value *Base =
  2293. getMinimalBaseOfPointer(A, QueryingAA, Loc->Ptr, Offset, DL);
  2294. if (Base && Base == &AssociatedValue) {
  2295. int64_t DerefBytes = Loc->Size.getValue() + Offset;
  2296. IsNonNull |= !NullPointerIsDefined;
  2297. return std::max(int64_t(0), DerefBytes);
  2298. }
  2299. /// Corner case when an offset is 0.
  2300. Base = GetPointerBaseWithConstantOffset(Loc->Ptr, Offset, DL,
  2301. /*AllowNonInbounds*/ true);
  2302. if (Base && Base == &AssociatedValue && Offset == 0) {
  2303. int64_t DerefBytes = Loc->Size.getValue();
  2304. IsNonNull |= !NullPointerIsDefined;
  2305. return std::max(int64_t(0), DerefBytes);
  2306. }
  2307. return 0;
  2308. }
  2309. struct AANonNullImpl : AANonNull {
  2310. AANonNullImpl(const IRPosition &IRP, Attributor &A)
  2311. : AANonNull(IRP, A),
  2312. NullIsDefined(NullPointerIsDefined(
  2313. getAnchorScope(),
  2314. getAssociatedValue().getType()->getPointerAddressSpace())) {}
  2315. /// See AbstractAttribute::initialize(...).
  2316. void initialize(Attributor &A) override {
  2317. Value &V = *getAssociatedValue().stripPointerCasts();
  2318. if (!NullIsDefined &&
  2319. hasAttr({Attribute::NonNull, Attribute::Dereferenceable},
  2320. /* IgnoreSubsumingPositions */ false, &A)) {
  2321. indicateOptimisticFixpoint();
  2322. return;
  2323. }
  2324. if (isa<ConstantPointerNull>(V)) {
  2325. indicatePessimisticFixpoint();
  2326. return;
  2327. }
  2328. AANonNull::initialize(A);
  2329. bool CanBeNull, CanBeFreed;
  2330. if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull,
  2331. CanBeFreed)) {
  2332. if (!CanBeNull) {
  2333. indicateOptimisticFixpoint();
  2334. return;
  2335. }
  2336. }
  2337. if (isa<GlobalValue>(V)) {
  2338. indicatePessimisticFixpoint();
  2339. return;
  2340. }
  2341. if (Instruction *CtxI = getCtxI())
  2342. followUsesInMBEC(*this, A, getState(), *CtxI);
  2343. }
  2344. /// See followUsesInMBEC
  2345. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  2346. AANonNull::StateType &State) {
  2347. bool IsNonNull = false;
  2348. bool TrackUse = false;
  2349. getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
  2350. IsNonNull, TrackUse);
  2351. State.setKnown(IsNonNull);
  2352. return TrackUse;
  2353. }
  2354. /// See AbstractAttribute::getAsStr().
  2355. const std::string getAsStr() const override {
  2356. return getAssumed() ? "nonnull" : "may-null";
  2357. }
  2358. /// Flag to determine if the underlying value can be null and still allow
  2359. /// valid accesses.
  2360. const bool NullIsDefined;
  2361. };
  2362. /// NonNull attribute for a floating value.
  2363. struct AANonNullFloating : public AANonNullImpl {
  2364. AANonNullFloating(const IRPosition &IRP, Attributor &A)
  2365. : AANonNullImpl(IRP, A) {}
  2366. /// See AbstractAttribute::updateImpl(...).
  2367. ChangeStatus updateImpl(Attributor &A) override {
  2368. const DataLayout &DL = A.getDataLayout();
  2369. bool Stripped;
  2370. bool UsedAssumedInformation = false;
  2371. SmallVector<AA::ValueAndContext> Values;
  2372. if (!A.getAssumedSimplifiedValues(getIRPosition(), *this, Values,
  2373. AA::AnyScope, UsedAssumedInformation)) {
  2374. Values.push_back({getAssociatedValue(), getCtxI()});
  2375. Stripped = false;
  2376. } else {
  2377. Stripped = Values.size() != 1 ||
  2378. Values.front().getValue() != &getAssociatedValue();
  2379. }
  2380. DominatorTree *DT = nullptr;
  2381. AssumptionCache *AC = nullptr;
  2382. InformationCache &InfoCache = A.getInfoCache();
  2383. if (const Function *Fn = getAnchorScope()) {
  2384. DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
  2385. AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
  2386. }
  2387. AANonNull::StateType T;
  2388. auto VisitValueCB = [&](Value &V, const Instruction *CtxI) -> bool {
  2389. const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V),
  2390. DepClassTy::REQUIRED);
  2391. if (!Stripped && this == &AA) {
  2392. if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT))
  2393. T.indicatePessimisticFixpoint();
  2394. } else {
  2395. // Use abstract attribute information.
  2396. const AANonNull::StateType &NS = AA.getState();
  2397. T ^= NS;
  2398. }
  2399. return T.isValidState();
  2400. };
  2401. for (const auto &VAC : Values)
  2402. if (!VisitValueCB(*VAC.getValue(), VAC.getCtxI()))
  2403. return indicatePessimisticFixpoint();
  2404. return clampStateAndIndicateChange(getState(), T);
  2405. }
  2406. /// See AbstractAttribute::trackStatistics()
  2407. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
  2408. };
  2409. /// NonNull attribute for function return value.
  2410. struct AANonNullReturned final
  2411. : AAReturnedFromReturnedValues<AANonNull, AANonNull> {
  2412. AANonNullReturned(const IRPosition &IRP, Attributor &A)
  2413. : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {}
  2414. /// See AbstractAttribute::getAsStr().
  2415. const std::string getAsStr() const override {
  2416. return getAssumed() ? "nonnull" : "may-null";
  2417. }
  2418. /// See AbstractAttribute::trackStatistics()
  2419. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
  2420. };
  2421. /// NonNull attribute for function argument.
  2422. struct AANonNullArgument final
  2423. : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
  2424. AANonNullArgument(const IRPosition &IRP, Attributor &A)
  2425. : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {}
  2426. /// See AbstractAttribute::trackStatistics()
  2427. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
  2428. };
  2429. struct AANonNullCallSiteArgument final : AANonNullFloating {
  2430. AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A)
  2431. : AANonNullFloating(IRP, A) {}
  2432. /// See AbstractAttribute::trackStatistics()
  2433. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
  2434. };
  2435. /// NonNull attribute for a call site return position.
  2436. struct AANonNullCallSiteReturned final
  2437. : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
  2438. AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A)
  2439. : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {}
  2440. /// See AbstractAttribute::trackStatistics()
  2441. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
  2442. };
  2443. } // namespace
  2444. /// ------------------------ No-Recurse Attributes ----------------------------
  2445. namespace {
  2446. struct AANoRecurseImpl : public AANoRecurse {
  2447. AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {}
  2448. /// See AbstractAttribute::getAsStr()
  2449. const std::string getAsStr() const override {
  2450. return getAssumed() ? "norecurse" : "may-recurse";
  2451. }
  2452. };
  2453. struct AANoRecurseFunction final : AANoRecurseImpl {
  2454. AANoRecurseFunction(const IRPosition &IRP, Attributor &A)
  2455. : AANoRecurseImpl(IRP, A) {}
  2456. /// See AbstractAttribute::updateImpl(...).
  2457. ChangeStatus updateImpl(Attributor &A) override {
  2458. // If all live call sites are known to be no-recurse, we are as well.
  2459. auto CallSitePred = [&](AbstractCallSite ACS) {
  2460. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  2461. *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
  2462. DepClassTy::NONE);
  2463. return NoRecurseAA.isKnownNoRecurse();
  2464. };
  2465. bool UsedAssumedInformation = false;
  2466. if (A.checkForAllCallSites(CallSitePred, *this, true,
  2467. UsedAssumedInformation)) {
  2468. // If we know all call sites and all are known no-recurse, we are done.
  2469. // If all known call sites, which might not be all that exist, are known
  2470. // to be no-recurse, we are not done but we can continue to assume
  2471. // no-recurse. If one of the call sites we have not visited will become
  2472. // live, another update is triggered.
  2473. if (!UsedAssumedInformation)
  2474. indicateOptimisticFixpoint();
  2475. return ChangeStatus::UNCHANGED;
  2476. }
  2477. const AAInterFnReachability &EdgeReachability =
  2478. A.getAAFor<AAInterFnReachability>(*this, getIRPosition(),
  2479. DepClassTy::REQUIRED);
  2480. if (EdgeReachability.canReach(A, *getAnchorScope()))
  2481. return indicatePessimisticFixpoint();
  2482. return ChangeStatus::UNCHANGED;
  2483. }
  2484. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
  2485. };
  2486. /// NoRecurse attribute deduction for a call sites.
  2487. struct AANoRecurseCallSite final : AANoRecurseImpl {
  2488. AANoRecurseCallSite(const IRPosition &IRP, Attributor &A)
  2489. : AANoRecurseImpl(IRP, A) {}
  2490. /// See AbstractAttribute::initialize(...).
  2491. void initialize(Attributor &A) override {
  2492. AANoRecurseImpl::initialize(A);
  2493. Function *F = getAssociatedFunction();
  2494. if (!F || F->isDeclaration())
  2495. indicatePessimisticFixpoint();
  2496. }
  2497. /// See AbstractAttribute::updateImpl(...).
  2498. ChangeStatus updateImpl(Attributor &A) override {
  2499. // TODO: Once we have call site specific value information we can provide
  2500. // call site specific liveness information and then it makes
  2501. // sense to specialize attributes for call sites arguments instead of
  2502. // redirecting requests to the callee argument.
  2503. Function *F = getAssociatedFunction();
  2504. const IRPosition &FnPos = IRPosition::function(*F);
  2505. auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED);
  2506. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2507. }
  2508. /// See AbstractAttribute::trackStatistics()
  2509. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
  2510. };
  2511. } // namespace
  2512. /// -------------------- Undefined-Behavior Attributes ------------------------
  2513. namespace {
  2514. struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
  2515. AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A)
  2516. : AAUndefinedBehavior(IRP, A) {}
  2517. /// See AbstractAttribute::updateImpl(...).
  2518. // through a pointer (i.e. also branches etc.)
  2519. ChangeStatus updateImpl(Attributor &A) override {
  2520. const size_t UBPrevSize = KnownUBInsts.size();
  2521. const size_t NoUBPrevSize = AssumedNoUBInsts.size();
  2522. auto InspectMemAccessInstForUB = [&](Instruction &I) {
  2523. // Lang ref now states volatile store is not UB, let's skip them.
  2524. if (I.isVolatile() && I.mayWriteToMemory())
  2525. return true;
  2526. // Skip instructions that are already saved.
  2527. if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
  2528. return true;
  2529. // If we reach here, we know we have an instruction
  2530. // that accesses memory through a pointer operand,
  2531. // for which getPointerOperand() should give it to us.
  2532. Value *PtrOp =
  2533. const_cast<Value *>(getPointerOperand(&I, /* AllowVolatile */ true));
  2534. assert(PtrOp &&
  2535. "Expected pointer operand of memory accessing instruction");
  2536. // Either we stopped and the appropriate action was taken,
  2537. // or we got back a simplified value to continue.
  2538. std::optional<Value *> SimplifiedPtrOp =
  2539. stopOnUndefOrAssumed(A, PtrOp, &I);
  2540. if (!SimplifiedPtrOp || !*SimplifiedPtrOp)
  2541. return true;
  2542. const Value *PtrOpVal = *SimplifiedPtrOp;
  2543. // A memory access through a pointer is considered UB
  2544. // only if the pointer has constant null value.
  2545. // TODO: Expand it to not only check constant values.
  2546. if (!isa<ConstantPointerNull>(PtrOpVal)) {
  2547. AssumedNoUBInsts.insert(&I);
  2548. return true;
  2549. }
  2550. const Type *PtrTy = PtrOpVal->getType();
  2551. // Because we only consider instructions inside functions,
  2552. // assume that a parent function exists.
  2553. const Function *F = I.getFunction();
  2554. // A memory access using constant null pointer is only considered UB
  2555. // if null pointer is _not_ defined for the target platform.
  2556. if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
  2557. AssumedNoUBInsts.insert(&I);
  2558. else
  2559. KnownUBInsts.insert(&I);
  2560. return true;
  2561. };
  2562. auto InspectBrInstForUB = [&](Instruction &I) {
  2563. // A conditional branch instruction is considered UB if it has `undef`
  2564. // condition.
  2565. // Skip instructions that are already saved.
  2566. if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
  2567. return true;
  2568. // We know we have a branch instruction.
  2569. auto *BrInst = cast<BranchInst>(&I);
  2570. // Unconditional branches are never considered UB.
  2571. if (BrInst->isUnconditional())
  2572. return true;
  2573. // Either we stopped and the appropriate action was taken,
  2574. // or we got back a simplified value to continue.
  2575. std::optional<Value *> SimplifiedCond =
  2576. stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
  2577. if (!SimplifiedCond || !*SimplifiedCond)
  2578. return true;
  2579. AssumedNoUBInsts.insert(&I);
  2580. return true;
  2581. };
  2582. auto InspectCallSiteForUB = [&](Instruction &I) {
  2583. // Check whether a callsite always cause UB or not
  2584. // Skip instructions that are already saved.
  2585. if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
  2586. return true;
  2587. // Check nonnull and noundef argument attribute violation for each
  2588. // callsite.
  2589. CallBase &CB = cast<CallBase>(I);
  2590. Function *Callee = CB.getCalledFunction();
  2591. if (!Callee)
  2592. return true;
  2593. for (unsigned idx = 0; idx < CB.arg_size(); idx++) {
  2594. // If current argument is known to be simplified to null pointer and the
  2595. // corresponding argument position is known to have nonnull attribute,
  2596. // the argument is poison. Furthermore, if the argument is poison and
  2597. // the position is known to have noundef attriubte, this callsite is
  2598. // considered UB.
  2599. if (idx >= Callee->arg_size())
  2600. break;
  2601. Value *ArgVal = CB.getArgOperand(idx);
  2602. if (!ArgVal)
  2603. continue;
  2604. // Here, we handle three cases.
  2605. // (1) Not having a value means it is dead. (we can replace the value
  2606. // with undef)
  2607. // (2) Simplified to undef. The argument violate noundef attriubte.
  2608. // (3) Simplified to null pointer where known to be nonnull.
  2609. // The argument is a poison value and violate noundef attribute.
  2610. IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
  2611. auto &NoUndefAA =
  2612. A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE);
  2613. if (!NoUndefAA.isKnownNoUndef())
  2614. continue;
  2615. bool UsedAssumedInformation = false;
  2616. std::optional<Value *> SimplifiedVal =
  2617. A.getAssumedSimplified(IRPosition::value(*ArgVal), *this,
  2618. UsedAssumedInformation, AA::Interprocedural);
  2619. if (UsedAssumedInformation)
  2620. continue;
  2621. if (SimplifiedVal && !*SimplifiedVal)
  2622. return true;
  2623. if (!SimplifiedVal || isa<UndefValue>(**SimplifiedVal)) {
  2624. KnownUBInsts.insert(&I);
  2625. continue;
  2626. }
  2627. if (!ArgVal->getType()->isPointerTy() ||
  2628. !isa<ConstantPointerNull>(**SimplifiedVal))
  2629. continue;
  2630. auto &NonNullAA =
  2631. A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE);
  2632. if (NonNullAA.isKnownNonNull())
  2633. KnownUBInsts.insert(&I);
  2634. }
  2635. return true;
  2636. };
  2637. auto InspectReturnInstForUB = [&](Instruction &I) {
  2638. auto &RI = cast<ReturnInst>(I);
  2639. // Either we stopped and the appropriate action was taken,
  2640. // or we got back a simplified return value to continue.
  2641. std::optional<Value *> SimplifiedRetValue =
  2642. stopOnUndefOrAssumed(A, RI.getReturnValue(), &I);
  2643. if (!SimplifiedRetValue || !*SimplifiedRetValue)
  2644. return true;
  2645. // Check if a return instruction always cause UB or not
  2646. // Note: It is guaranteed that the returned position of the anchor
  2647. // scope has noundef attribute when this is called.
  2648. // We also ensure the return position is not "assumed dead"
  2649. // because the returned value was then potentially simplified to
  2650. // `undef` in AAReturnedValues without removing the `noundef`
  2651. // attribute yet.
  2652. // When the returned position has noundef attriubte, UB occurs in the
  2653. // following cases.
  2654. // (1) Returned value is known to be undef.
  2655. // (2) The value is known to be a null pointer and the returned
  2656. // position has nonnull attribute (because the returned value is
  2657. // poison).
  2658. if (isa<ConstantPointerNull>(*SimplifiedRetValue)) {
  2659. auto &NonNullAA = A.getAAFor<AANonNull>(
  2660. *this, IRPosition::returned(*getAnchorScope()), DepClassTy::NONE);
  2661. if (NonNullAA.isKnownNonNull())
  2662. KnownUBInsts.insert(&I);
  2663. }
  2664. return true;
  2665. };
  2666. bool UsedAssumedInformation = false;
  2667. A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
  2668. {Instruction::Load, Instruction::Store,
  2669. Instruction::AtomicCmpXchg,
  2670. Instruction::AtomicRMW},
  2671. UsedAssumedInformation,
  2672. /* CheckBBLivenessOnly */ true);
  2673. A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
  2674. UsedAssumedInformation,
  2675. /* CheckBBLivenessOnly */ true);
  2676. A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this,
  2677. UsedAssumedInformation);
  2678. // If the returned position of the anchor scope has noundef attriubte, check
  2679. // all returned instructions.
  2680. if (!getAnchorScope()->getReturnType()->isVoidTy()) {
  2681. const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
  2682. if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) {
  2683. auto &RetPosNoUndefAA =
  2684. A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
  2685. if (RetPosNoUndefAA.isKnownNoUndef())
  2686. A.checkForAllInstructions(InspectReturnInstForUB, *this,
  2687. {Instruction::Ret}, UsedAssumedInformation,
  2688. /* CheckBBLivenessOnly */ true);
  2689. }
  2690. }
  2691. if (NoUBPrevSize != AssumedNoUBInsts.size() ||
  2692. UBPrevSize != KnownUBInsts.size())
  2693. return ChangeStatus::CHANGED;
  2694. return ChangeStatus::UNCHANGED;
  2695. }
  2696. bool isKnownToCauseUB(Instruction *I) const override {
  2697. return KnownUBInsts.count(I);
  2698. }
  2699. bool isAssumedToCauseUB(Instruction *I) const override {
  2700. // In simple words, if an instruction is not in the assumed to _not_
  2701. // cause UB, then it is assumed UB (that includes those
  2702. // in the KnownUBInsts set). The rest is boilerplate
  2703. // is to ensure that it is one of the instructions we test
  2704. // for UB.
  2705. switch (I->getOpcode()) {
  2706. case Instruction::Load:
  2707. case Instruction::Store:
  2708. case Instruction::AtomicCmpXchg:
  2709. case Instruction::AtomicRMW:
  2710. return !AssumedNoUBInsts.count(I);
  2711. case Instruction::Br: {
  2712. auto *BrInst = cast<BranchInst>(I);
  2713. if (BrInst->isUnconditional())
  2714. return false;
  2715. return !AssumedNoUBInsts.count(I);
  2716. } break;
  2717. default:
  2718. return false;
  2719. }
  2720. return false;
  2721. }
  2722. ChangeStatus manifest(Attributor &A) override {
  2723. if (KnownUBInsts.empty())
  2724. return ChangeStatus::UNCHANGED;
  2725. for (Instruction *I : KnownUBInsts)
  2726. A.changeToUnreachableAfterManifest(I);
  2727. return ChangeStatus::CHANGED;
  2728. }
  2729. /// See AbstractAttribute::getAsStr()
  2730. const std::string getAsStr() const override {
  2731. return getAssumed() ? "undefined-behavior" : "no-ub";
  2732. }
  2733. /// Note: The correctness of this analysis depends on the fact that the
  2734. /// following 2 sets will stop changing after some point.
  2735. /// "Change" here means that their size changes.
  2736. /// The size of each set is monotonically increasing
  2737. /// (we only add items to them) and it is upper bounded by the number of
  2738. /// instructions in the processed function (we can never save more
  2739. /// elements in either set than this number). Hence, at some point,
  2740. /// they will stop increasing.
  2741. /// Consequently, at some point, both sets will have stopped
  2742. /// changing, effectively making the analysis reach a fixpoint.
  2743. /// Note: These 2 sets are disjoint and an instruction can be considered
  2744. /// one of 3 things:
  2745. /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
  2746. /// the KnownUBInsts set.
  2747. /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
  2748. /// has a reason to assume it).
  2749. /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
  2750. /// could not find a reason to assume or prove that it can cause UB,
  2751. /// hence it assumes it doesn't. We have a set for these instructions
  2752. /// so that we don't reprocess them in every update.
  2753. /// Note however that instructions in this set may cause UB.
  2754. protected:
  2755. /// A set of all live instructions _known_ to cause UB.
  2756. SmallPtrSet<Instruction *, 8> KnownUBInsts;
  2757. private:
  2758. /// A set of all the (live) instructions that are assumed to _not_ cause UB.
  2759. SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
  2760. // Should be called on updates in which if we're processing an instruction
  2761. // \p I that depends on a value \p V, one of the following has to happen:
  2762. // - If the value is assumed, then stop.
  2763. // - If the value is known but undef, then consider it UB.
  2764. // - Otherwise, do specific processing with the simplified value.
  2765. // We return std::nullopt in the first 2 cases to signify that an appropriate
  2766. // action was taken and the caller should stop.
  2767. // Otherwise, we return the simplified value that the caller should
  2768. // use for specific processing.
  2769. std::optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V,
  2770. Instruction *I) {
  2771. bool UsedAssumedInformation = false;
  2772. std::optional<Value *> SimplifiedV =
  2773. A.getAssumedSimplified(IRPosition::value(*V), *this,
  2774. UsedAssumedInformation, AA::Interprocedural);
  2775. if (!UsedAssumedInformation) {
  2776. // Don't depend on assumed values.
  2777. if (!SimplifiedV) {
  2778. // If it is known (which we tested above) but it doesn't have a value,
  2779. // then we can assume `undef` and hence the instruction is UB.
  2780. KnownUBInsts.insert(I);
  2781. return std::nullopt;
  2782. }
  2783. if (!*SimplifiedV)
  2784. return nullptr;
  2785. V = *SimplifiedV;
  2786. }
  2787. if (isa<UndefValue>(V)) {
  2788. KnownUBInsts.insert(I);
  2789. return std::nullopt;
  2790. }
  2791. return V;
  2792. }
  2793. };
  2794. struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
  2795. AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
  2796. : AAUndefinedBehaviorImpl(IRP, A) {}
  2797. /// See AbstractAttribute::trackStatistics()
  2798. void trackStatistics() const override {
  2799. STATS_DECL(UndefinedBehaviorInstruction, Instruction,
  2800. "Number of instructions known to have UB");
  2801. BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
  2802. KnownUBInsts.size();
  2803. }
  2804. };
  2805. } // namespace
  2806. /// ------------------------ Will-Return Attributes ----------------------------
  2807. namespace {
  2808. // Helper function that checks whether a function has any cycle which we don't
  2809. // know if it is bounded or not.
  2810. // Loops with maximum trip count are considered bounded, any other cycle not.
  2811. static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
  2812. ScalarEvolution *SE =
  2813. A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
  2814. LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
  2815. // If either SCEV or LoopInfo is not available for the function then we assume
  2816. // any cycle to be unbounded cycle.
  2817. // We use scc_iterator which uses Tarjan algorithm to find all the maximal
  2818. // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
  2819. if (!SE || !LI) {
  2820. for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
  2821. if (SCCI.hasCycle())
  2822. return true;
  2823. return false;
  2824. }
  2825. // If there's irreducible control, the function may contain non-loop cycles.
  2826. if (mayContainIrreducibleControl(F, LI))
  2827. return true;
  2828. // Any loop that does not have a max trip count is considered unbounded cycle.
  2829. for (auto *L : LI->getLoopsInPreorder()) {
  2830. if (!SE->getSmallConstantMaxTripCount(L))
  2831. return true;
  2832. }
  2833. return false;
  2834. }
  2835. struct AAWillReturnImpl : public AAWillReturn {
  2836. AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
  2837. : AAWillReturn(IRP, A) {}
  2838. /// See AbstractAttribute::initialize(...).
  2839. void initialize(Attributor &A) override {
  2840. AAWillReturn::initialize(A);
  2841. if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
  2842. indicateOptimisticFixpoint();
  2843. return;
  2844. }
  2845. }
  2846. /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
  2847. bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
  2848. // Check for `mustprogress` in the scope and the associated function which
  2849. // might be different if this is a call site.
  2850. if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
  2851. (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
  2852. return false;
  2853. bool IsKnown;
  2854. if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
  2855. return IsKnown || !KnownOnly;
  2856. return false;
  2857. }
  2858. /// See AbstractAttribute::updateImpl(...).
  2859. ChangeStatus updateImpl(Attributor &A) override {
  2860. if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
  2861. return ChangeStatus::UNCHANGED;
  2862. auto CheckForWillReturn = [&](Instruction &I) {
  2863. IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
  2864. const auto &WillReturnAA =
  2865. A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
  2866. if (WillReturnAA.isKnownWillReturn())
  2867. return true;
  2868. if (!WillReturnAA.isAssumedWillReturn())
  2869. return false;
  2870. const auto &NoRecurseAA =
  2871. A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
  2872. return NoRecurseAA.isAssumedNoRecurse();
  2873. };
  2874. bool UsedAssumedInformation = false;
  2875. if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this,
  2876. UsedAssumedInformation))
  2877. return indicatePessimisticFixpoint();
  2878. return ChangeStatus::UNCHANGED;
  2879. }
  2880. /// See AbstractAttribute::getAsStr()
  2881. const std::string getAsStr() const override {
  2882. return getAssumed() ? "willreturn" : "may-noreturn";
  2883. }
  2884. };
  2885. struct AAWillReturnFunction final : AAWillReturnImpl {
  2886. AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
  2887. : AAWillReturnImpl(IRP, A) {}
  2888. /// See AbstractAttribute::initialize(...).
  2889. void initialize(Attributor &A) override {
  2890. AAWillReturnImpl::initialize(A);
  2891. Function *F = getAnchorScope();
  2892. if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
  2893. indicatePessimisticFixpoint();
  2894. }
  2895. /// See AbstractAttribute::trackStatistics()
  2896. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
  2897. };
  2898. /// WillReturn attribute deduction for a call sites.
  2899. struct AAWillReturnCallSite final : AAWillReturnImpl {
  2900. AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
  2901. : AAWillReturnImpl(IRP, A) {}
  2902. /// See AbstractAttribute::initialize(...).
  2903. void initialize(Attributor &A) override {
  2904. AAWillReturnImpl::initialize(A);
  2905. Function *F = getAssociatedFunction();
  2906. if (!F || !A.isFunctionIPOAmendable(*F))
  2907. indicatePessimisticFixpoint();
  2908. }
  2909. /// See AbstractAttribute::updateImpl(...).
  2910. ChangeStatus updateImpl(Attributor &A) override {
  2911. if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
  2912. return ChangeStatus::UNCHANGED;
  2913. // TODO: Once we have call site specific value information we can provide
  2914. // call site specific liveness information and then it makes
  2915. // sense to specialize attributes for call sites arguments instead of
  2916. // redirecting requests to the callee argument.
  2917. Function *F = getAssociatedFunction();
  2918. const IRPosition &FnPos = IRPosition::function(*F);
  2919. auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
  2920. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2921. }
  2922. /// See AbstractAttribute::trackStatistics()
  2923. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
  2924. };
  2925. } // namespace
  2926. /// -------------------AAIntraFnReachability Attribute--------------------------
  2927. /// All information associated with a reachability query. This boilerplate code
  2928. /// is used by both AAIntraFnReachability and AAInterFnReachability, with
  2929. /// different \p ToTy values.
  2930. template <typename ToTy> struct ReachabilityQueryInfo {
  2931. enum class Reachable {
  2932. No,
  2933. Yes,
  2934. };
  2935. /// Start here,
  2936. const Instruction *From = nullptr;
  2937. /// reach this place,
  2938. const ToTy *To = nullptr;
  2939. /// without going through any of these instructions,
  2940. const AA::InstExclusionSetTy *ExclusionSet = nullptr;
  2941. /// and remember if it worked:
  2942. Reachable Result = Reachable::No;
  2943. ReachabilityQueryInfo(const Instruction *From, const ToTy *To)
  2944. : From(From), To(To) {}
  2945. /// Constructor replacement to ensure unique and stable sets are used for the
  2946. /// cache.
  2947. ReachabilityQueryInfo(Attributor &A, const Instruction &From, const ToTy &To,
  2948. const AA::InstExclusionSetTy *ES)
  2949. : From(&From), To(&To), ExclusionSet(ES) {
  2950. if (ExclusionSet && !ExclusionSet->empty()) {
  2951. ExclusionSet =
  2952. A.getInfoCache().getOrCreateUniqueBlockExecutionSet(ExclusionSet);
  2953. } else {
  2954. ExclusionSet = nullptr;
  2955. }
  2956. }
  2957. ReachabilityQueryInfo(const ReachabilityQueryInfo &RQI)
  2958. : From(RQI.From), To(RQI.To), ExclusionSet(RQI.ExclusionSet) {
  2959. assert(RQI.Result == Reachable::No &&
  2960. "Didn't expect to copy an explored RQI!");
  2961. }
  2962. };
  2963. namespace llvm {
  2964. template <typename ToTy> struct DenseMapInfo<ReachabilityQueryInfo<ToTy> *> {
  2965. using InstSetDMI = DenseMapInfo<const AA::InstExclusionSetTy *>;
  2966. using PairDMI = DenseMapInfo<std::pair<const Instruction *, const ToTy *>>;
  2967. static ReachabilityQueryInfo<ToTy> EmptyKey;
  2968. static ReachabilityQueryInfo<ToTy> TombstoneKey;
  2969. static inline ReachabilityQueryInfo<ToTy> *getEmptyKey() { return &EmptyKey; }
  2970. static inline ReachabilityQueryInfo<ToTy> *getTombstoneKey() {
  2971. return &TombstoneKey;
  2972. }
  2973. static unsigned getHashValue(const ReachabilityQueryInfo<ToTy> *RQI) {
  2974. unsigned H = PairDMI ::getHashValue({RQI->From, RQI->To});
  2975. H += InstSetDMI::getHashValue(RQI->ExclusionSet);
  2976. return H;
  2977. }
  2978. static bool isEqual(const ReachabilityQueryInfo<ToTy> *LHS,
  2979. const ReachabilityQueryInfo<ToTy> *RHS) {
  2980. if (!PairDMI::isEqual({LHS->From, LHS->To}, {RHS->From, RHS->To}))
  2981. return false;
  2982. return InstSetDMI::isEqual(LHS->ExclusionSet, RHS->ExclusionSet);
  2983. }
  2984. };
  2985. #define DefineKeys(ToTy) \
  2986. template <> \
  2987. ReachabilityQueryInfo<ToTy> \
  2988. DenseMapInfo<ReachabilityQueryInfo<ToTy> *>::EmptyKey = \
  2989. ReachabilityQueryInfo<ToTy>( \
  2990. DenseMapInfo<const Instruction *>::getEmptyKey(), \
  2991. DenseMapInfo<const ToTy *>::getEmptyKey()); \
  2992. template <> \
  2993. ReachabilityQueryInfo<ToTy> \
  2994. DenseMapInfo<ReachabilityQueryInfo<ToTy> *>::TombstoneKey = \
  2995. ReachabilityQueryInfo<ToTy>( \
  2996. DenseMapInfo<const Instruction *>::getTombstoneKey(), \
  2997. DenseMapInfo<const ToTy *>::getTombstoneKey());
  2998. DefineKeys(Instruction) DefineKeys(Function)
  2999. #undef DefineKeys
  3000. } // namespace llvm
  3001. namespace {
  3002. template <typename BaseTy, typename ToTy>
  3003. struct CachedReachabilityAA : public BaseTy {
  3004. using RQITy = ReachabilityQueryInfo<ToTy>;
  3005. CachedReachabilityAA<BaseTy, ToTy>(const IRPosition &IRP, Attributor &A)
  3006. : BaseTy(IRP, A) {}
  3007. /// See AbstractAttribute::isQueryAA.
  3008. bool isQueryAA() const override { return true; }
  3009. /// See AbstractAttribute::updateImpl(...).
  3010. ChangeStatus updateImpl(Attributor &A) override {
  3011. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  3012. InUpdate = true;
  3013. for (RQITy *RQI : QueryVector) {
  3014. if (RQI->Result == RQITy::Reachable::No && isReachableImpl(A, *RQI))
  3015. Changed = ChangeStatus::CHANGED;
  3016. }
  3017. InUpdate = false;
  3018. return Changed;
  3019. }
  3020. virtual bool isReachableImpl(Attributor &A, RQITy &RQI) = 0;
  3021. bool rememberResult(Attributor &A, typename RQITy::Reachable Result,
  3022. RQITy &RQI) {
  3023. if (Result == RQITy::Reachable::No) {
  3024. if (!InUpdate)
  3025. A.registerForUpdate(*this);
  3026. return false;
  3027. }
  3028. assert(RQI.Result == RQITy::Reachable::No && "Already reachable?");
  3029. RQI.Result = Result;
  3030. return true;
  3031. }
  3032. const std::string getAsStr() const override {
  3033. // TODO: Return the number of reachable queries.
  3034. return "#queries(" + std::to_string(QueryVector.size()) + ")";
  3035. }
  3036. RQITy *checkQueryCache(Attributor &A, RQITy &StackRQI,
  3037. typename RQITy::Reachable &Result) {
  3038. if (!this->getState().isValidState()) {
  3039. Result = RQITy::Reachable::Yes;
  3040. return nullptr;
  3041. }
  3042. auto It = QueryCache.find(&StackRQI);
  3043. if (It != QueryCache.end()) {
  3044. Result = (*It)->Result;
  3045. return nullptr;
  3046. }
  3047. RQITy *RQIPtr = new (A.Allocator) RQITy(StackRQI);
  3048. QueryVector.push_back(RQIPtr);
  3049. QueryCache.insert(RQIPtr);
  3050. return RQIPtr;
  3051. }
  3052. private:
  3053. bool InUpdate = false;
  3054. SmallVector<RQITy *> QueryVector;
  3055. DenseSet<RQITy *> QueryCache;
  3056. };
  3057. struct AAIntraFnReachabilityFunction final
  3058. : public CachedReachabilityAA<AAIntraFnReachability, Instruction> {
  3059. AAIntraFnReachabilityFunction(const IRPosition &IRP, Attributor &A)
  3060. : CachedReachabilityAA<AAIntraFnReachability, Instruction>(IRP, A) {}
  3061. bool isAssumedReachable(
  3062. Attributor &A, const Instruction &From, const Instruction &To,
  3063. const AA::InstExclusionSetTy *ExclusionSet) const override {
  3064. auto *NonConstThis = const_cast<AAIntraFnReachabilityFunction *>(this);
  3065. if (&From == &To)
  3066. return true;
  3067. RQITy StackRQI(A, From, To, ExclusionSet);
  3068. typename RQITy::Reachable Result;
  3069. if (RQITy *RQIPtr = NonConstThis->checkQueryCache(A, StackRQI, Result)) {
  3070. return NonConstThis->isReachableImpl(A, *RQIPtr);
  3071. }
  3072. return Result == RQITy::Reachable::Yes;
  3073. }
  3074. bool isReachableImpl(Attributor &A, RQITy &RQI) override {
  3075. const Instruction *Origin = RQI.From;
  3076. auto WillReachInBlock = [=](const Instruction &From, const Instruction &To,
  3077. const AA::InstExclusionSetTy *ExclusionSet) {
  3078. const Instruction *IP = &From;
  3079. while (IP && IP != &To) {
  3080. if (ExclusionSet && IP != Origin && ExclusionSet->count(IP))
  3081. break;
  3082. IP = IP->getNextNode();
  3083. }
  3084. return IP == &To;
  3085. };
  3086. const BasicBlock *FromBB = RQI.From->getParent();
  3087. const BasicBlock *ToBB = RQI.To->getParent();
  3088. assert(FromBB->getParent() == ToBB->getParent() &&
  3089. "Not an intra-procedural query!");
  3090. // Check intra-block reachability, however, other reaching paths are still
  3091. // possible.
  3092. if (FromBB == ToBB &&
  3093. WillReachInBlock(*RQI.From, *RQI.To, RQI.ExclusionSet))
  3094. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  3095. SmallPtrSet<const BasicBlock *, 16> ExclusionBlocks;
  3096. if (RQI.ExclusionSet)
  3097. for (auto *I : *RQI.ExclusionSet)
  3098. ExclusionBlocks.insert(I->getParent());
  3099. // Check if we make it out of the FromBB block at all.
  3100. if (ExclusionBlocks.count(FromBB) &&
  3101. !WillReachInBlock(*RQI.From, *FromBB->getTerminator(),
  3102. RQI.ExclusionSet))
  3103. return rememberResult(A, RQITy::Reachable::No, RQI);
  3104. SmallPtrSet<const BasicBlock *, 16> Visited;
  3105. SmallVector<const BasicBlock *, 16> Worklist;
  3106. Worklist.push_back(FromBB);
  3107. auto &LivenessAA =
  3108. A.getAAFor<AAIsDead>(*this, getIRPosition(), DepClassTy::OPTIONAL);
  3109. while (!Worklist.empty()) {
  3110. const BasicBlock *BB = Worklist.pop_back_val();
  3111. if (!Visited.insert(BB).second)
  3112. continue;
  3113. for (const BasicBlock *SuccBB : successors(BB)) {
  3114. if (LivenessAA.isEdgeDead(BB, SuccBB))
  3115. continue;
  3116. if (SuccBB == ToBB &&
  3117. WillReachInBlock(SuccBB->front(), *RQI.To, RQI.ExclusionSet))
  3118. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  3119. if (ExclusionBlocks.count(SuccBB))
  3120. continue;
  3121. Worklist.push_back(SuccBB);
  3122. }
  3123. }
  3124. return rememberResult(A, RQITy::Reachable::No, RQI);
  3125. }
  3126. /// See AbstractAttribute::trackStatistics()
  3127. void trackStatistics() const override {}
  3128. };
  3129. } // namespace
  3130. /// ------------------------ NoAlias Argument Attribute ------------------------
  3131. namespace {
  3132. struct AANoAliasImpl : AANoAlias {
  3133. AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
  3134. assert(getAssociatedType()->isPointerTy() &&
  3135. "Noalias is a pointer attribute");
  3136. }
  3137. const std::string getAsStr() const override {
  3138. return getAssumed() ? "noalias" : "may-alias";
  3139. }
  3140. };
  3141. /// NoAlias attribute for a floating value.
  3142. struct AANoAliasFloating final : AANoAliasImpl {
  3143. AANoAliasFloating(const IRPosition &IRP, Attributor &A)
  3144. : AANoAliasImpl(IRP, A) {}
  3145. /// See AbstractAttribute::initialize(...).
  3146. void initialize(Attributor &A) override {
  3147. AANoAliasImpl::initialize(A);
  3148. Value *Val = &getAssociatedValue();
  3149. do {
  3150. CastInst *CI = dyn_cast<CastInst>(Val);
  3151. if (!CI)
  3152. break;
  3153. Value *Base = CI->getOperand(0);
  3154. if (!Base->hasOneUse())
  3155. break;
  3156. Val = Base;
  3157. } while (true);
  3158. if (!Val->getType()->isPointerTy()) {
  3159. indicatePessimisticFixpoint();
  3160. return;
  3161. }
  3162. if (isa<AllocaInst>(Val))
  3163. indicateOptimisticFixpoint();
  3164. else if (isa<ConstantPointerNull>(Val) &&
  3165. !NullPointerIsDefined(getAnchorScope(),
  3166. Val->getType()->getPointerAddressSpace()))
  3167. indicateOptimisticFixpoint();
  3168. else if (Val != &getAssociatedValue()) {
  3169. const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
  3170. *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
  3171. if (ValNoAliasAA.isKnownNoAlias())
  3172. indicateOptimisticFixpoint();
  3173. }
  3174. }
  3175. /// See AbstractAttribute::updateImpl(...).
  3176. ChangeStatus updateImpl(Attributor &A) override {
  3177. // TODO: Implement this.
  3178. return indicatePessimisticFixpoint();
  3179. }
  3180. /// See AbstractAttribute::trackStatistics()
  3181. void trackStatistics() const override {
  3182. STATS_DECLTRACK_FLOATING_ATTR(noalias)
  3183. }
  3184. };
  3185. /// NoAlias attribute for an argument.
  3186. struct AANoAliasArgument final
  3187. : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
  3188. using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
  3189. AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
  3190. /// See AbstractAttribute::initialize(...).
  3191. void initialize(Attributor &A) override {
  3192. Base::initialize(A);
  3193. // See callsite argument attribute and callee argument attribute.
  3194. if (hasAttr({Attribute::ByVal}))
  3195. indicateOptimisticFixpoint();
  3196. }
  3197. /// See AbstractAttribute::update(...).
  3198. ChangeStatus updateImpl(Attributor &A) override {
  3199. // We have to make sure no-alias on the argument does not break
  3200. // synchronization when this is a callback argument, see also [1] below.
  3201. // If synchronization cannot be affected, we delegate to the base updateImpl
  3202. // function, otherwise we give up for now.
  3203. // If the function is no-sync, no-alias cannot break synchronization.
  3204. const auto &NoSyncAA =
  3205. A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
  3206. DepClassTy::OPTIONAL);
  3207. if (NoSyncAA.isAssumedNoSync())
  3208. return Base::updateImpl(A);
  3209. // If the argument is read-only, no-alias cannot break synchronization.
  3210. bool IsKnown;
  3211. if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
  3212. return Base::updateImpl(A);
  3213. // If the argument is never passed through callbacks, no-alias cannot break
  3214. // synchronization.
  3215. bool UsedAssumedInformation = false;
  3216. if (A.checkForAllCallSites(
  3217. [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
  3218. true, UsedAssumedInformation))
  3219. return Base::updateImpl(A);
  3220. // TODO: add no-alias but make sure it doesn't break synchronization by
  3221. // introducing fake uses. See:
  3222. // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
  3223. // International Workshop on OpenMP 2018,
  3224. // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
  3225. return indicatePessimisticFixpoint();
  3226. }
  3227. /// See AbstractAttribute::trackStatistics()
  3228. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
  3229. };
  3230. struct AANoAliasCallSiteArgument final : AANoAliasImpl {
  3231. AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
  3232. : AANoAliasImpl(IRP, A) {}
  3233. /// See AbstractAttribute::initialize(...).
  3234. void initialize(Attributor &A) override {
  3235. // See callsite argument attribute and callee argument attribute.
  3236. const auto &CB = cast<CallBase>(getAnchorValue());
  3237. if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
  3238. indicateOptimisticFixpoint();
  3239. Value &Val = getAssociatedValue();
  3240. if (isa<ConstantPointerNull>(Val) &&
  3241. !NullPointerIsDefined(getAnchorScope(),
  3242. Val.getType()->getPointerAddressSpace()))
  3243. indicateOptimisticFixpoint();
  3244. }
  3245. /// Determine if the underlying value may alias with the call site argument
  3246. /// \p OtherArgNo of \p ICS (= the underlying call site).
  3247. bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
  3248. const AAMemoryBehavior &MemBehaviorAA,
  3249. const CallBase &CB, unsigned OtherArgNo) {
  3250. // We do not need to worry about aliasing with the underlying IRP.
  3251. if (this->getCalleeArgNo() == (int)OtherArgNo)
  3252. return false;
  3253. // If it is not a pointer or pointer vector we do not alias.
  3254. const Value *ArgOp = CB.getArgOperand(OtherArgNo);
  3255. if (!ArgOp->getType()->isPtrOrPtrVectorTy())
  3256. return false;
  3257. auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
  3258. *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
  3259. // If the argument is readnone, there is no read-write aliasing.
  3260. if (CBArgMemBehaviorAA.isAssumedReadNone()) {
  3261. A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
  3262. return false;
  3263. }
  3264. // If the argument is readonly and the underlying value is readonly, there
  3265. // is no read-write aliasing.
  3266. bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
  3267. if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
  3268. A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
  3269. A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
  3270. return false;
  3271. }
  3272. // We have to utilize actual alias analysis queries so we need the object.
  3273. if (!AAR)
  3274. AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
  3275. // Try to rule it out at the call site.
  3276. bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
  3277. LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
  3278. "callsite arguments: "
  3279. << getAssociatedValue() << " " << *ArgOp << " => "
  3280. << (IsAliasing ? "" : "no-") << "alias \n");
  3281. return IsAliasing;
  3282. }
  3283. bool
  3284. isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
  3285. const AAMemoryBehavior &MemBehaviorAA,
  3286. const AANoAlias &NoAliasAA) {
  3287. // We can deduce "noalias" if the following conditions hold.
  3288. // (i) Associated value is assumed to be noalias in the definition.
  3289. // (ii) Associated value is assumed to be no-capture in all the uses
  3290. // possibly executed before this callsite.
  3291. // (iii) There is no other pointer argument which could alias with the
  3292. // value.
  3293. bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
  3294. if (!AssociatedValueIsNoAliasAtDef) {
  3295. LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
  3296. << " is not no-alias at the definition\n");
  3297. return false;
  3298. }
  3299. auto IsDereferenceableOrNull = [&](Value *O, const DataLayout &DL) {
  3300. const auto &DerefAA = A.getAAFor<AADereferenceable>(
  3301. *this, IRPosition::value(*O), DepClassTy::OPTIONAL);
  3302. return DerefAA.getAssumedDereferenceableBytes();
  3303. };
  3304. A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
  3305. const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
  3306. const Function *ScopeFn = VIRP.getAnchorScope();
  3307. auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
  3308. // Check whether the value is captured in the scope using AANoCapture.
  3309. // Look at CFG and check only uses possibly executed before this
  3310. // callsite.
  3311. auto UsePred = [&](const Use &U, bool &Follow) -> bool {
  3312. Instruction *UserI = cast<Instruction>(U.getUser());
  3313. // If UserI is the curr instruction and there is a single potential use of
  3314. // the value in UserI we allow the use.
  3315. // TODO: We should inspect the operands and allow those that cannot alias
  3316. // with the value.
  3317. if (UserI == getCtxI() && UserI->getNumOperands() == 1)
  3318. return true;
  3319. if (ScopeFn) {
  3320. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  3321. if (CB->isArgOperand(&U)) {
  3322. unsigned ArgNo = CB->getArgOperandNo(&U);
  3323. const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
  3324. *this, IRPosition::callsite_argument(*CB, ArgNo),
  3325. DepClassTy::OPTIONAL);
  3326. if (NoCaptureAA.isAssumedNoCapture())
  3327. return true;
  3328. }
  3329. }
  3330. if (!AA::isPotentiallyReachable(
  3331. A, *UserI, *getCtxI(), *this, /* ExclusionSet */ nullptr,
  3332. [ScopeFn](const Function &Fn) { return &Fn != ScopeFn; }))
  3333. return true;
  3334. }
  3335. // TODO: We should track the capturing uses in AANoCapture but the problem
  3336. // is CGSCC runs. For those we would need to "allow" AANoCapture for
  3337. // a value in the module slice.
  3338. switch (DetermineUseCaptureKind(U, IsDereferenceableOrNull)) {
  3339. case UseCaptureKind::NO_CAPTURE:
  3340. return true;
  3341. case UseCaptureKind::MAY_CAPTURE:
  3342. LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *UserI
  3343. << "\n");
  3344. return false;
  3345. case UseCaptureKind::PASSTHROUGH:
  3346. Follow = true;
  3347. return true;
  3348. }
  3349. llvm_unreachable("unknown UseCaptureKind");
  3350. };
  3351. if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
  3352. if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
  3353. LLVM_DEBUG(
  3354. dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
  3355. << " cannot be noalias as it is potentially captured\n");
  3356. return false;
  3357. }
  3358. }
  3359. A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
  3360. // Check there is no other pointer argument which could alias with the
  3361. // value passed at this call site.
  3362. // TODO: AbstractCallSite
  3363. const auto &CB = cast<CallBase>(getAnchorValue());
  3364. for (unsigned OtherArgNo = 0; OtherArgNo < CB.arg_size(); OtherArgNo++)
  3365. if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
  3366. return false;
  3367. return true;
  3368. }
  3369. /// See AbstractAttribute::updateImpl(...).
  3370. ChangeStatus updateImpl(Attributor &A) override {
  3371. // If the argument is readnone we are done as there are no accesses via the
  3372. // argument.
  3373. auto &MemBehaviorAA =
  3374. A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
  3375. if (MemBehaviorAA.isAssumedReadNone()) {
  3376. A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
  3377. return ChangeStatus::UNCHANGED;
  3378. }
  3379. const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
  3380. const auto &NoAliasAA =
  3381. A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
  3382. AAResults *AAR = nullptr;
  3383. if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
  3384. NoAliasAA)) {
  3385. LLVM_DEBUG(
  3386. dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
  3387. return ChangeStatus::UNCHANGED;
  3388. }
  3389. return indicatePessimisticFixpoint();
  3390. }
  3391. /// See AbstractAttribute::trackStatistics()
  3392. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
  3393. };
  3394. /// NoAlias attribute for function return value.
  3395. struct AANoAliasReturned final : AANoAliasImpl {
  3396. AANoAliasReturned(const IRPosition &IRP, Attributor &A)
  3397. : AANoAliasImpl(IRP, A) {}
  3398. /// See AbstractAttribute::initialize(...).
  3399. void initialize(Attributor &A) override {
  3400. AANoAliasImpl::initialize(A);
  3401. Function *F = getAssociatedFunction();
  3402. if (!F || F->isDeclaration())
  3403. indicatePessimisticFixpoint();
  3404. }
  3405. /// See AbstractAttribute::updateImpl(...).
  3406. ChangeStatus updateImpl(Attributor &A) override {
  3407. auto CheckReturnValue = [&](Value &RV) -> bool {
  3408. if (Constant *C = dyn_cast<Constant>(&RV))
  3409. if (C->isNullValue() || isa<UndefValue>(C))
  3410. return true;
  3411. /// For now, we can only deduce noalias if we have call sites.
  3412. /// FIXME: add more support.
  3413. if (!isa<CallBase>(&RV))
  3414. return false;
  3415. const IRPosition &RVPos = IRPosition::value(RV);
  3416. const auto &NoAliasAA =
  3417. A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
  3418. if (!NoAliasAA.isAssumedNoAlias())
  3419. return false;
  3420. const auto &NoCaptureAA =
  3421. A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
  3422. return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
  3423. };
  3424. if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
  3425. return indicatePessimisticFixpoint();
  3426. return ChangeStatus::UNCHANGED;
  3427. }
  3428. /// See AbstractAttribute::trackStatistics()
  3429. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
  3430. };
  3431. /// NoAlias attribute deduction for a call site return value.
  3432. struct AANoAliasCallSiteReturned final : AANoAliasImpl {
  3433. AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
  3434. : AANoAliasImpl(IRP, A) {}
  3435. /// See AbstractAttribute::initialize(...).
  3436. void initialize(Attributor &A) override {
  3437. AANoAliasImpl::initialize(A);
  3438. Function *F = getAssociatedFunction();
  3439. if (!F || F->isDeclaration())
  3440. indicatePessimisticFixpoint();
  3441. }
  3442. /// See AbstractAttribute::updateImpl(...).
  3443. ChangeStatus updateImpl(Attributor &A) override {
  3444. // TODO: Once we have call site specific value information we can provide
  3445. // call site specific liveness information and then it makes
  3446. // sense to specialize attributes for call sites arguments instead of
  3447. // redirecting requests to the callee argument.
  3448. Function *F = getAssociatedFunction();
  3449. const IRPosition &FnPos = IRPosition::returned(*F);
  3450. auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
  3451. return clampStateAndIndicateChange(getState(), FnAA.getState());
  3452. }
  3453. /// See AbstractAttribute::trackStatistics()
  3454. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
  3455. };
  3456. } // namespace
  3457. /// -------------------AAIsDead Function Attribute-----------------------
  3458. namespace {
  3459. struct AAIsDeadValueImpl : public AAIsDead {
  3460. AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
  3461. /// See AbstractAttribute::initialize(...).
  3462. void initialize(Attributor &A) override {
  3463. if (auto *Scope = getAnchorScope())
  3464. if (!A.isRunOn(*Scope))
  3465. indicatePessimisticFixpoint();
  3466. }
  3467. /// See AAIsDead::isAssumedDead().
  3468. bool isAssumedDead() const override { return isAssumed(IS_DEAD); }
  3469. /// See AAIsDead::isKnownDead().
  3470. bool isKnownDead() const override { return isKnown(IS_DEAD); }
  3471. /// See AAIsDead::isAssumedDead(BasicBlock *).
  3472. bool isAssumedDead(const BasicBlock *BB) const override { return false; }
  3473. /// See AAIsDead::isKnownDead(BasicBlock *).
  3474. bool isKnownDead(const BasicBlock *BB) const override { return false; }
  3475. /// See AAIsDead::isAssumedDead(Instruction *I).
  3476. bool isAssumedDead(const Instruction *I) const override {
  3477. return I == getCtxI() && isAssumedDead();
  3478. }
  3479. /// See AAIsDead::isKnownDead(Instruction *I).
  3480. bool isKnownDead(const Instruction *I) const override {
  3481. return isAssumedDead(I) && isKnownDead();
  3482. }
  3483. /// See AbstractAttribute::getAsStr().
  3484. const std::string getAsStr() const override {
  3485. return isAssumedDead() ? "assumed-dead" : "assumed-live";
  3486. }
  3487. /// Check if all uses are assumed dead.
  3488. bool areAllUsesAssumedDead(Attributor &A, Value &V) {
  3489. // Callers might not check the type, void has no uses.
  3490. if (V.getType()->isVoidTy() || V.use_empty())
  3491. return true;
  3492. // If we replace a value with a constant there are no uses left afterwards.
  3493. if (!isa<Constant>(V)) {
  3494. if (auto *I = dyn_cast<Instruction>(&V))
  3495. if (!A.isRunOn(*I->getFunction()))
  3496. return false;
  3497. bool UsedAssumedInformation = false;
  3498. std::optional<Constant *> C =
  3499. A.getAssumedConstant(V, *this, UsedAssumedInformation);
  3500. if (!C || *C)
  3501. return true;
  3502. }
  3503. auto UsePred = [&](const Use &U, bool &Follow) { return false; };
  3504. // Explicitly set the dependence class to required because we want a long
  3505. // chain of N dependent instructions to be considered live as soon as one is
  3506. // without going through N update cycles. This is not required for
  3507. // correctness.
  3508. return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false,
  3509. DepClassTy::REQUIRED,
  3510. /* IgnoreDroppableUses */ false);
  3511. }
  3512. /// Determine if \p I is assumed to be side-effect free.
  3513. bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
  3514. if (!I || wouldInstructionBeTriviallyDead(I))
  3515. return true;
  3516. auto *CB = dyn_cast<CallBase>(I);
  3517. if (!CB || isa<IntrinsicInst>(CB))
  3518. return false;
  3519. const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
  3520. const auto &NoUnwindAA =
  3521. A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
  3522. if (!NoUnwindAA.isAssumedNoUnwind())
  3523. return false;
  3524. if (!NoUnwindAA.isKnownNoUnwind())
  3525. A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
  3526. bool IsKnown;
  3527. return AA::isAssumedReadOnly(A, CallIRP, *this, IsKnown);
  3528. }
  3529. };
  3530. struct AAIsDeadFloating : public AAIsDeadValueImpl {
  3531. AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
  3532. : AAIsDeadValueImpl(IRP, A) {}
  3533. /// See AbstractAttribute::initialize(...).
  3534. void initialize(Attributor &A) override {
  3535. AAIsDeadValueImpl::initialize(A);
  3536. if (isa<UndefValue>(getAssociatedValue())) {
  3537. indicatePessimisticFixpoint();
  3538. return;
  3539. }
  3540. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  3541. if (!isAssumedSideEffectFree(A, I)) {
  3542. if (!isa_and_nonnull<StoreInst>(I))
  3543. indicatePessimisticFixpoint();
  3544. else
  3545. removeAssumedBits(HAS_NO_EFFECT);
  3546. }
  3547. }
  3548. bool isDeadStore(Attributor &A, StoreInst &SI,
  3549. SmallSetVector<Instruction *, 8> *AssumeOnlyInst = nullptr) {
  3550. // Lang ref now states volatile store is not UB/dead, let's skip them.
  3551. if (SI.isVolatile())
  3552. return false;
  3553. // If we are collecting assumes to be deleted we are in the manifest stage.
  3554. // It's problematic to collect the potential copies again now so we use the
  3555. // cached ones.
  3556. bool UsedAssumedInformation = false;
  3557. if (!AssumeOnlyInst) {
  3558. PotentialCopies.clear();
  3559. if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this,
  3560. UsedAssumedInformation)) {
  3561. LLVM_DEBUG(
  3562. dbgs()
  3563. << "[AAIsDead] Could not determine potential copies of store!\n");
  3564. return false;
  3565. }
  3566. }
  3567. LLVM_DEBUG(dbgs() << "[AAIsDead] Store has " << PotentialCopies.size()
  3568. << " potential copies.\n");
  3569. InformationCache &InfoCache = A.getInfoCache();
  3570. return llvm::all_of(PotentialCopies, [&](Value *V) {
  3571. if (A.isAssumedDead(IRPosition::value(*V), this, nullptr,
  3572. UsedAssumedInformation))
  3573. return true;
  3574. if (auto *LI = dyn_cast<LoadInst>(V)) {
  3575. if (llvm::all_of(LI->uses(), [&](const Use &U) {
  3576. auto &UserI = cast<Instruction>(*U.getUser());
  3577. if (InfoCache.isOnlyUsedByAssume(UserI)) {
  3578. if (AssumeOnlyInst)
  3579. AssumeOnlyInst->insert(&UserI);
  3580. return true;
  3581. }
  3582. return A.isAssumedDead(U, this, nullptr, UsedAssumedInformation);
  3583. })) {
  3584. return true;
  3585. }
  3586. }
  3587. LLVM_DEBUG(dbgs() << "[AAIsDead] Potential copy " << *V
  3588. << " is assumed live!\n");
  3589. return false;
  3590. });
  3591. }
  3592. /// See AbstractAttribute::getAsStr().
  3593. const std::string getAsStr() const override {
  3594. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  3595. if (isa_and_nonnull<StoreInst>(I))
  3596. if (isValidState())
  3597. return "assumed-dead-store";
  3598. return AAIsDeadValueImpl::getAsStr();
  3599. }
  3600. /// See AbstractAttribute::updateImpl(...).
  3601. ChangeStatus updateImpl(Attributor &A) override {
  3602. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  3603. if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
  3604. if (!isDeadStore(A, *SI))
  3605. return indicatePessimisticFixpoint();
  3606. } else {
  3607. if (!isAssumedSideEffectFree(A, I))
  3608. return indicatePessimisticFixpoint();
  3609. if (!areAllUsesAssumedDead(A, getAssociatedValue()))
  3610. return indicatePessimisticFixpoint();
  3611. }
  3612. return ChangeStatus::UNCHANGED;
  3613. }
  3614. bool isRemovableStore() const override {
  3615. return isAssumed(IS_REMOVABLE) && isa<StoreInst>(&getAssociatedValue());
  3616. }
  3617. /// See AbstractAttribute::manifest(...).
  3618. ChangeStatus manifest(Attributor &A) override {
  3619. Value &V = getAssociatedValue();
  3620. if (auto *I = dyn_cast<Instruction>(&V)) {
  3621. // If we get here we basically know the users are all dead. We check if
  3622. // isAssumedSideEffectFree returns true here again because it might not be
  3623. // the case and only the users are dead but the instruction (=call) is
  3624. // still needed.
  3625. if (auto *SI = dyn_cast<StoreInst>(I)) {
  3626. SmallSetVector<Instruction *, 8> AssumeOnlyInst;
  3627. bool IsDead = isDeadStore(A, *SI, &AssumeOnlyInst);
  3628. (void)IsDead;
  3629. assert(IsDead && "Store was assumed to be dead!");
  3630. A.deleteAfterManifest(*I);
  3631. for (size_t i = 0; i < AssumeOnlyInst.size(); ++i) {
  3632. Instruction *AOI = AssumeOnlyInst[i];
  3633. for (auto *Usr : AOI->users())
  3634. AssumeOnlyInst.insert(cast<Instruction>(Usr));
  3635. A.deleteAfterManifest(*AOI);
  3636. }
  3637. return ChangeStatus::CHANGED;
  3638. }
  3639. if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) {
  3640. A.deleteAfterManifest(*I);
  3641. return ChangeStatus::CHANGED;
  3642. }
  3643. }
  3644. return ChangeStatus::UNCHANGED;
  3645. }
  3646. /// See AbstractAttribute::trackStatistics()
  3647. void trackStatistics() const override {
  3648. STATS_DECLTRACK_FLOATING_ATTR(IsDead)
  3649. }
  3650. private:
  3651. // The potential copies of a dead store, used for deletion during manifest.
  3652. SmallSetVector<Value *, 4> PotentialCopies;
  3653. };
  3654. struct AAIsDeadArgument : public AAIsDeadFloating {
  3655. AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
  3656. : AAIsDeadFloating(IRP, A) {}
  3657. /// See AbstractAttribute::initialize(...).
  3658. void initialize(Attributor &A) override {
  3659. AAIsDeadFloating::initialize(A);
  3660. if (!A.isFunctionIPOAmendable(*getAnchorScope()))
  3661. indicatePessimisticFixpoint();
  3662. }
  3663. /// See AbstractAttribute::manifest(...).
  3664. ChangeStatus manifest(Attributor &A) override {
  3665. Argument &Arg = *getAssociatedArgument();
  3666. if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
  3667. if (A.registerFunctionSignatureRewrite(
  3668. Arg, /* ReplacementTypes */ {},
  3669. Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
  3670. Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
  3671. return ChangeStatus::CHANGED;
  3672. }
  3673. return ChangeStatus::UNCHANGED;
  3674. }
  3675. /// See AbstractAttribute::trackStatistics()
  3676. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
  3677. };
  3678. struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
  3679. AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
  3680. : AAIsDeadValueImpl(IRP, A) {}
  3681. /// See AbstractAttribute::initialize(...).
  3682. void initialize(Attributor &A) override {
  3683. AAIsDeadValueImpl::initialize(A);
  3684. if (isa<UndefValue>(getAssociatedValue()))
  3685. indicatePessimisticFixpoint();
  3686. }
  3687. /// See AbstractAttribute::updateImpl(...).
  3688. ChangeStatus updateImpl(Attributor &A) override {
  3689. // TODO: Once we have call site specific value information we can provide
  3690. // call site specific liveness information and then it makes
  3691. // sense to specialize attributes for call sites arguments instead of
  3692. // redirecting requests to the callee argument.
  3693. Argument *Arg = getAssociatedArgument();
  3694. if (!Arg)
  3695. return indicatePessimisticFixpoint();
  3696. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  3697. auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
  3698. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  3699. }
  3700. /// See AbstractAttribute::manifest(...).
  3701. ChangeStatus manifest(Attributor &A) override {
  3702. CallBase &CB = cast<CallBase>(getAnchorValue());
  3703. Use &U = CB.getArgOperandUse(getCallSiteArgNo());
  3704. assert(!isa<UndefValue>(U.get()) &&
  3705. "Expected undef values to be filtered out!");
  3706. UndefValue &UV = *UndefValue::get(U->getType());
  3707. if (A.changeUseAfterManifest(U, UV))
  3708. return ChangeStatus::CHANGED;
  3709. return ChangeStatus::UNCHANGED;
  3710. }
  3711. /// See AbstractAttribute::trackStatistics()
  3712. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
  3713. };
  3714. struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
  3715. AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
  3716. : AAIsDeadFloating(IRP, A) {}
  3717. /// See AAIsDead::isAssumedDead().
  3718. bool isAssumedDead() const override {
  3719. return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
  3720. }
  3721. /// See AbstractAttribute::initialize(...).
  3722. void initialize(Attributor &A) override {
  3723. AAIsDeadFloating::initialize(A);
  3724. if (isa<UndefValue>(getAssociatedValue())) {
  3725. indicatePessimisticFixpoint();
  3726. return;
  3727. }
  3728. // We track this separately as a secondary state.
  3729. IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
  3730. }
  3731. /// See AbstractAttribute::updateImpl(...).
  3732. ChangeStatus updateImpl(Attributor &A) override {
  3733. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  3734. if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
  3735. IsAssumedSideEffectFree = false;
  3736. Changed = ChangeStatus::CHANGED;
  3737. }
  3738. if (!areAllUsesAssumedDead(A, getAssociatedValue()))
  3739. return indicatePessimisticFixpoint();
  3740. return Changed;
  3741. }
  3742. /// See AbstractAttribute::trackStatistics()
  3743. void trackStatistics() const override {
  3744. if (IsAssumedSideEffectFree)
  3745. STATS_DECLTRACK_CSRET_ATTR(IsDead)
  3746. else
  3747. STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
  3748. }
  3749. /// See AbstractAttribute::getAsStr().
  3750. const std::string getAsStr() const override {
  3751. return isAssumedDead()
  3752. ? "assumed-dead"
  3753. : (getAssumed() ? "assumed-dead-users" : "assumed-live");
  3754. }
  3755. private:
  3756. bool IsAssumedSideEffectFree = true;
  3757. };
  3758. struct AAIsDeadReturned : public AAIsDeadValueImpl {
  3759. AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
  3760. : AAIsDeadValueImpl(IRP, A) {}
  3761. /// See AbstractAttribute::updateImpl(...).
  3762. ChangeStatus updateImpl(Attributor &A) override {
  3763. bool UsedAssumedInformation = false;
  3764. A.checkForAllInstructions([](Instruction &) { return true; }, *this,
  3765. {Instruction::Ret}, UsedAssumedInformation);
  3766. auto PredForCallSite = [&](AbstractCallSite ACS) {
  3767. if (ACS.isCallbackCall() || !ACS.getInstruction())
  3768. return false;
  3769. return areAllUsesAssumedDead(A, *ACS.getInstruction());
  3770. };
  3771. if (!A.checkForAllCallSites(PredForCallSite, *this, true,
  3772. UsedAssumedInformation))
  3773. return indicatePessimisticFixpoint();
  3774. return ChangeStatus::UNCHANGED;
  3775. }
  3776. /// See AbstractAttribute::manifest(...).
  3777. ChangeStatus manifest(Attributor &A) override {
  3778. // TODO: Rewrite the signature to return void?
  3779. bool AnyChange = false;
  3780. UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
  3781. auto RetInstPred = [&](Instruction &I) {
  3782. ReturnInst &RI = cast<ReturnInst>(I);
  3783. if (!isa<UndefValue>(RI.getReturnValue()))
  3784. AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
  3785. return true;
  3786. };
  3787. bool UsedAssumedInformation = false;
  3788. A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret},
  3789. UsedAssumedInformation);
  3790. return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  3791. }
  3792. /// See AbstractAttribute::trackStatistics()
  3793. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
  3794. };
  3795. struct AAIsDeadFunction : public AAIsDead {
  3796. AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
  3797. /// See AbstractAttribute::initialize(...).
  3798. void initialize(Attributor &A) override {
  3799. Function *F = getAnchorScope();
  3800. if (!F || F->isDeclaration() || !A.isRunOn(*F)) {
  3801. indicatePessimisticFixpoint();
  3802. return;
  3803. }
  3804. if (!isAssumedDeadInternalFunction(A)) {
  3805. ToBeExploredFrom.insert(&F->getEntryBlock().front());
  3806. assumeLive(A, F->getEntryBlock());
  3807. }
  3808. }
  3809. bool isAssumedDeadInternalFunction(Attributor &A) {
  3810. if (!getAnchorScope()->hasLocalLinkage())
  3811. return false;
  3812. bool UsedAssumedInformation = false;
  3813. return A.checkForAllCallSites([](AbstractCallSite) { return false; }, *this,
  3814. true, UsedAssumedInformation);
  3815. }
  3816. /// See AbstractAttribute::getAsStr().
  3817. const std::string getAsStr() const override {
  3818. return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
  3819. std::to_string(getAnchorScope()->size()) + "][#TBEP " +
  3820. std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
  3821. std::to_string(KnownDeadEnds.size()) + "]";
  3822. }
  3823. /// See AbstractAttribute::manifest(...).
  3824. ChangeStatus manifest(Attributor &A) override {
  3825. assert(getState().isValidState() &&
  3826. "Attempted to manifest an invalid state!");
  3827. ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
  3828. Function &F = *getAnchorScope();
  3829. if (AssumedLiveBlocks.empty()) {
  3830. A.deleteAfterManifest(F);
  3831. return ChangeStatus::CHANGED;
  3832. }
  3833. // Flag to determine if we can change an invoke to a call assuming the
  3834. // callee is nounwind. This is not possible if the personality of the
  3835. // function allows to catch asynchronous exceptions.
  3836. bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
  3837. KnownDeadEnds.set_union(ToBeExploredFrom);
  3838. for (const Instruction *DeadEndI : KnownDeadEnds) {
  3839. auto *CB = dyn_cast<CallBase>(DeadEndI);
  3840. if (!CB)
  3841. continue;
  3842. const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
  3843. *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
  3844. bool MayReturn = !NoReturnAA.isAssumedNoReturn();
  3845. if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
  3846. continue;
  3847. if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
  3848. A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
  3849. else
  3850. A.changeToUnreachableAfterManifest(
  3851. const_cast<Instruction *>(DeadEndI->getNextNode()));
  3852. HasChanged = ChangeStatus::CHANGED;
  3853. }
  3854. STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
  3855. for (BasicBlock &BB : F)
  3856. if (!AssumedLiveBlocks.count(&BB)) {
  3857. A.deleteAfterManifest(BB);
  3858. ++BUILD_STAT_NAME(AAIsDead, BasicBlock);
  3859. HasChanged = ChangeStatus::CHANGED;
  3860. }
  3861. return HasChanged;
  3862. }
  3863. /// See AbstractAttribute::updateImpl(...).
  3864. ChangeStatus updateImpl(Attributor &A) override;
  3865. bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
  3866. assert(From->getParent() == getAnchorScope() &&
  3867. To->getParent() == getAnchorScope() &&
  3868. "Used AAIsDead of the wrong function");
  3869. return isValidState() && !AssumedLiveEdges.count(std::make_pair(From, To));
  3870. }
  3871. /// See AbstractAttribute::trackStatistics()
  3872. void trackStatistics() const override {}
  3873. /// Returns true if the function is assumed dead.
  3874. bool isAssumedDead() const override { return false; }
  3875. /// See AAIsDead::isKnownDead().
  3876. bool isKnownDead() const override { return false; }
  3877. /// See AAIsDead::isAssumedDead(BasicBlock *).
  3878. bool isAssumedDead(const BasicBlock *BB) const override {
  3879. assert(BB->getParent() == getAnchorScope() &&
  3880. "BB must be in the same anchor scope function.");
  3881. if (!getAssumed())
  3882. return false;
  3883. return !AssumedLiveBlocks.count(BB);
  3884. }
  3885. /// See AAIsDead::isKnownDead(BasicBlock *).
  3886. bool isKnownDead(const BasicBlock *BB) const override {
  3887. return getKnown() && isAssumedDead(BB);
  3888. }
  3889. /// See AAIsDead::isAssumed(Instruction *I).
  3890. bool isAssumedDead(const Instruction *I) const override {
  3891. assert(I->getParent()->getParent() == getAnchorScope() &&
  3892. "Instruction must be in the same anchor scope function.");
  3893. if (!getAssumed())
  3894. return false;
  3895. // If it is not in AssumedLiveBlocks then it for sure dead.
  3896. // Otherwise, it can still be after noreturn call in a live block.
  3897. if (!AssumedLiveBlocks.count(I->getParent()))
  3898. return true;
  3899. // If it is not after a liveness barrier it is live.
  3900. const Instruction *PrevI = I->getPrevNode();
  3901. while (PrevI) {
  3902. if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
  3903. return true;
  3904. PrevI = PrevI->getPrevNode();
  3905. }
  3906. return false;
  3907. }
  3908. /// See AAIsDead::isKnownDead(Instruction *I).
  3909. bool isKnownDead(const Instruction *I) const override {
  3910. return getKnown() && isAssumedDead(I);
  3911. }
  3912. /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
  3913. /// that internal function called from \p BB should now be looked at.
  3914. bool assumeLive(Attributor &A, const BasicBlock &BB) {
  3915. if (!AssumedLiveBlocks.insert(&BB).second)
  3916. return false;
  3917. // We assume that all of BB is (probably) live now and if there are calls to
  3918. // internal functions we will assume that those are now live as well. This
  3919. // is a performance optimization for blocks with calls to a lot of internal
  3920. // functions. It can however cause dead functions to be treated as live.
  3921. for (const Instruction &I : BB)
  3922. if (const auto *CB = dyn_cast<CallBase>(&I))
  3923. if (const Function *F = CB->getCalledFunction())
  3924. if (F->hasLocalLinkage())
  3925. A.markLiveInternalFunction(*F);
  3926. return true;
  3927. }
  3928. /// Collection of instructions that need to be explored again, e.g., we
  3929. /// did assume they do not transfer control to (one of their) successors.
  3930. SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
  3931. /// Collection of instructions that are known to not transfer control.
  3932. SmallSetVector<const Instruction *, 8> KnownDeadEnds;
  3933. /// Collection of all assumed live edges
  3934. DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
  3935. /// Collection of all assumed live BasicBlocks.
  3936. DenseSet<const BasicBlock *> AssumedLiveBlocks;
  3937. };
  3938. static bool
  3939. identifyAliveSuccessors(Attributor &A, const CallBase &CB,
  3940. AbstractAttribute &AA,
  3941. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3942. const IRPosition &IPos = IRPosition::callsite_function(CB);
  3943. const auto &NoReturnAA =
  3944. A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
  3945. if (NoReturnAA.isAssumedNoReturn())
  3946. return !NoReturnAA.isKnownNoReturn();
  3947. if (CB.isTerminator())
  3948. AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
  3949. else
  3950. AliveSuccessors.push_back(CB.getNextNode());
  3951. return false;
  3952. }
  3953. static bool
  3954. identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
  3955. AbstractAttribute &AA,
  3956. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3957. bool UsedAssumedInformation =
  3958. identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
  3959. // First, determine if we can change an invoke to a call assuming the
  3960. // callee is nounwind. This is not possible if the personality of the
  3961. // function allows to catch asynchronous exceptions.
  3962. if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
  3963. AliveSuccessors.push_back(&II.getUnwindDest()->front());
  3964. } else {
  3965. const IRPosition &IPos = IRPosition::callsite_function(II);
  3966. const auto &AANoUnw =
  3967. A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
  3968. if (AANoUnw.isAssumedNoUnwind()) {
  3969. UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
  3970. } else {
  3971. AliveSuccessors.push_back(&II.getUnwindDest()->front());
  3972. }
  3973. }
  3974. return UsedAssumedInformation;
  3975. }
  3976. static bool
  3977. identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
  3978. AbstractAttribute &AA,
  3979. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3980. bool UsedAssumedInformation = false;
  3981. if (BI.getNumSuccessors() == 1) {
  3982. AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
  3983. } else {
  3984. std::optional<Constant *> C =
  3985. A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation);
  3986. if (!C || isa_and_nonnull<UndefValue>(*C)) {
  3987. // No value yet, assume both edges are dead.
  3988. } else if (isa_and_nonnull<ConstantInt>(*C)) {
  3989. const BasicBlock *SuccBB =
  3990. BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue());
  3991. AliveSuccessors.push_back(&SuccBB->front());
  3992. } else {
  3993. AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
  3994. AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
  3995. UsedAssumedInformation = false;
  3996. }
  3997. }
  3998. return UsedAssumedInformation;
  3999. }
  4000. static bool
  4001. identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
  4002. AbstractAttribute &AA,
  4003. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  4004. bool UsedAssumedInformation = false;
  4005. std::optional<Constant *> C =
  4006. A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation);
  4007. if (!C || isa_and_nonnull<UndefValue>(*C)) {
  4008. // No value yet, assume all edges are dead.
  4009. } else if (isa_and_nonnull<ConstantInt>(*C)) {
  4010. for (const auto &CaseIt : SI.cases()) {
  4011. if (CaseIt.getCaseValue() == *C) {
  4012. AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
  4013. return UsedAssumedInformation;
  4014. }
  4015. }
  4016. AliveSuccessors.push_back(&SI.getDefaultDest()->front());
  4017. return UsedAssumedInformation;
  4018. } else {
  4019. for (const BasicBlock *SuccBB : successors(SI.getParent()))
  4020. AliveSuccessors.push_back(&SuccBB->front());
  4021. }
  4022. return UsedAssumedInformation;
  4023. }
  4024. ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
  4025. ChangeStatus Change = ChangeStatus::UNCHANGED;
  4026. if (AssumedLiveBlocks.empty()) {
  4027. if (isAssumedDeadInternalFunction(A))
  4028. return ChangeStatus::UNCHANGED;
  4029. Function *F = getAnchorScope();
  4030. ToBeExploredFrom.insert(&F->getEntryBlock().front());
  4031. assumeLive(A, F->getEntryBlock());
  4032. Change = ChangeStatus::CHANGED;
  4033. }
  4034. LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
  4035. << getAnchorScope()->size() << "] BBs and "
  4036. << ToBeExploredFrom.size() << " exploration points and "
  4037. << KnownDeadEnds.size() << " known dead ends\n");
  4038. // Copy and clear the list of instructions we need to explore from. It is
  4039. // refilled with instructions the next update has to look at.
  4040. SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
  4041. ToBeExploredFrom.end());
  4042. decltype(ToBeExploredFrom) NewToBeExploredFrom;
  4043. SmallVector<const Instruction *, 8> AliveSuccessors;
  4044. while (!Worklist.empty()) {
  4045. const Instruction *I = Worklist.pop_back_val();
  4046. LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
  4047. // Fast forward for uninteresting instructions. We could look for UB here
  4048. // though.
  4049. while (!I->isTerminator() && !isa<CallBase>(I))
  4050. I = I->getNextNode();
  4051. AliveSuccessors.clear();
  4052. bool UsedAssumedInformation = false;
  4053. switch (I->getOpcode()) {
  4054. // TODO: look for (assumed) UB to backwards propagate "deadness".
  4055. default:
  4056. assert(I->isTerminator() &&
  4057. "Expected non-terminators to be handled already!");
  4058. for (const BasicBlock *SuccBB : successors(I->getParent()))
  4059. AliveSuccessors.push_back(&SuccBB->front());
  4060. break;
  4061. case Instruction::Call:
  4062. UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
  4063. *this, AliveSuccessors);
  4064. break;
  4065. case Instruction::Invoke:
  4066. UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
  4067. *this, AliveSuccessors);
  4068. break;
  4069. case Instruction::Br:
  4070. UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
  4071. *this, AliveSuccessors);
  4072. break;
  4073. case Instruction::Switch:
  4074. UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
  4075. *this, AliveSuccessors);
  4076. break;
  4077. }
  4078. if (UsedAssumedInformation) {
  4079. NewToBeExploredFrom.insert(I);
  4080. } else if (AliveSuccessors.empty() ||
  4081. (I->isTerminator() &&
  4082. AliveSuccessors.size() < I->getNumSuccessors())) {
  4083. if (KnownDeadEnds.insert(I))
  4084. Change = ChangeStatus::CHANGED;
  4085. }
  4086. LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
  4087. << AliveSuccessors.size() << " UsedAssumedInformation: "
  4088. << UsedAssumedInformation << "\n");
  4089. for (const Instruction *AliveSuccessor : AliveSuccessors) {
  4090. if (!I->isTerminator()) {
  4091. assert(AliveSuccessors.size() == 1 &&
  4092. "Non-terminator expected to have a single successor!");
  4093. Worklist.push_back(AliveSuccessor);
  4094. } else {
  4095. // record the assumed live edge
  4096. auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent());
  4097. if (AssumedLiveEdges.insert(Edge).second)
  4098. Change = ChangeStatus::CHANGED;
  4099. if (assumeLive(A, *AliveSuccessor->getParent()))
  4100. Worklist.push_back(AliveSuccessor);
  4101. }
  4102. }
  4103. }
  4104. // Check if the content of ToBeExploredFrom changed, ignore the order.
  4105. if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() ||
  4106. llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) {
  4107. return !ToBeExploredFrom.count(I);
  4108. })) {
  4109. Change = ChangeStatus::CHANGED;
  4110. ToBeExploredFrom = std::move(NewToBeExploredFrom);
  4111. }
  4112. // If we know everything is live there is no need to query for liveness.
  4113. // Instead, indicating a pessimistic fixpoint will cause the state to be
  4114. // "invalid" and all queries to be answered conservatively without lookups.
  4115. // To be in this state we have to (1) finished the exploration and (3) not
  4116. // discovered any non-trivial dead end and (2) not ruled unreachable code
  4117. // dead.
  4118. if (ToBeExploredFrom.empty() &&
  4119. getAnchorScope()->size() == AssumedLiveBlocks.size() &&
  4120. llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
  4121. return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
  4122. }))
  4123. return indicatePessimisticFixpoint();
  4124. return Change;
  4125. }
  4126. /// Liveness information for a call sites.
  4127. struct AAIsDeadCallSite final : AAIsDeadFunction {
  4128. AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
  4129. : AAIsDeadFunction(IRP, A) {}
  4130. /// See AbstractAttribute::initialize(...).
  4131. void initialize(Attributor &A) override {
  4132. // TODO: Once we have call site specific value information we can provide
  4133. // call site specific liveness information and then it makes
  4134. // sense to specialize attributes for call sites instead of
  4135. // redirecting requests to the callee.
  4136. llvm_unreachable("Abstract attributes for liveness are not "
  4137. "supported for call sites yet!");
  4138. }
  4139. /// See AbstractAttribute::updateImpl(...).
  4140. ChangeStatus updateImpl(Attributor &A) override {
  4141. return indicatePessimisticFixpoint();
  4142. }
  4143. /// See AbstractAttribute::trackStatistics()
  4144. void trackStatistics() const override {}
  4145. };
  4146. } // namespace
  4147. /// -------------------- Dereferenceable Argument Attribute --------------------
  4148. namespace {
  4149. struct AADereferenceableImpl : AADereferenceable {
  4150. AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
  4151. : AADereferenceable(IRP, A) {}
  4152. using StateType = DerefState;
  4153. /// See AbstractAttribute::initialize(...).
  4154. void initialize(Attributor &A) override {
  4155. Value &V = *getAssociatedValue().stripPointerCasts();
  4156. SmallVector<Attribute, 4> Attrs;
  4157. getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
  4158. Attrs, /* IgnoreSubsumingPositions */ false, &A);
  4159. for (const Attribute &Attr : Attrs)
  4160. takeKnownDerefBytesMaximum(Attr.getValueAsInt());
  4161. const IRPosition &IRP = this->getIRPosition();
  4162. NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
  4163. bool CanBeNull, CanBeFreed;
  4164. takeKnownDerefBytesMaximum(V.getPointerDereferenceableBytes(
  4165. A.getDataLayout(), CanBeNull, CanBeFreed));
  4166. bool IsFnInterface = IRP.isFnInterfaceKind();
  4167. Function *FnScope = IRP.getAnchorScope();
  4168. if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
  4169. indicatePessimisticFixpoint();
  4170. return;
  4171. }
  4172. if (Instruction *CtxI = getCtxI())
  4173. followUsesInMBEC(*this, A, getState(), *CtxI);
  4174. }
  4175. /// See AbstractAttribute::getState()
  4176. /// {
  4177. StateType &getState() override { return *this; }
  4178. const StateType &getState() const override { return *this; }
  4179. /// }
  4180. /// Helper function for collecting accessed bytes in must-be-executed-context
  4181. void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
  4182. DerefState &State) {
  4183. const Value *UseV = U->get();
  4184. if (!UseV->getType()->isPointerTy())
  4185. return;
  4186. std::optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
  4187. if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
  4188. return;
  4189. int64_t Offset;
  4190. const Value *Base = GetPointerBaseWithConstantOffset(
  4191. Loc->Ptr, Offset, A.getDataLayout(), /*AllowNonInbounds*/ true);
  4192. if (Base && Base == &getAssociatedValue())
  4193. State.addAccessedBytes(Offset, Loc->Size.getValue());
  4194. }
  4195. /// See followUsesInMBEC
  4196. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  4197. AADereferenceable::StateType &State) {
  4198. bool IsNonNull = false;
  4199. bool TrackUse = false;
  4200. int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
  4201. A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
  4202. LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes
  4203. << " for instruction " << *I << "\n");
  4204. addAccessedBytesForUse(A, U, I, State);
  4205. State.takeKnownDerefBytesMaximum(DerefBytes);
  4206. return TrackUse;
  4207. }
  4208. /// See AbstractAttribute::manifest(...).
  4209. ChangeStatus manifest(Attributor &A) override {
  4210. ChangeStatus Change = AADereferenceable::manifest(A);
  4211. if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
  4212. removeAttrs({Attribute::DereferenceableOrNull});
  4213. return ChangeStatus::CHANGED;
  4214. }
  4215. return Change;
  4216. }
  4217. void getDeducedAttributes(LLVMContext &Ctx,
  4218. SmallVectorImpl<Attribute> &Attrs) const override {
  4219. // TODO: Add *_globally support
  4220. if (isAssumedNonNull())
  4221. Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
  4222. Ctx, getAssumedDereferenceableBytes()));
  4223. else
  4224. Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
  4225. Ctx, getAssumedDereferenceableBytes()));
  4226. }
  4227. /// See AbstractAttribute::getAsStr().
  4228. const std::string getAsStr() const override {
  4229. if (!getAssumedDereferenceableBytes())
  4230. return "unknown-dereferenceable";
  4231. return std::string("dereferenceable") +
  4232. (isAssumedNonNull() ? "" : "_or_null") +
  4233. (isAssumedGlobal() ? "_globally" : "") + "<" +
  4234. std::to_string(getKnownDereferenceableBytes()) + "-" +
  4235. std::to_string(getAssumedDereferenceableBytes()) + ">";
  4236. }
  4237. };
  4238. /// Dereferenceable attribute for a floating value.
  4239. struct AADereferenceableFloating : AADereferenceableImpl {
  4240. AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
  4241. : AADereferenceableImpl(IRP, A) {}
  4242. /// See AbstractAttribute::updateImpl(...).
  4243. ChangeStatus updateImpl(Attributor &A) override {
  4244. bool Stripped;
  4245. bool UsedAssumedInformation = false;
  4246. SmallVector<AA::ValueAndContext> Values;
  4247. if (!A.getAssumedSimplifiedValues(getIRPosition(), *this, Values,
  4248. AA::AnyScope, UsedAssumedInformation)) {
  4249. Values.push_back({getAssociatedValue(), getCtxI()});
  4250. Stripped = false;
  4251. } else {
  4252. Stripped = Values.size() != 1 ||
  4253. Values.front().getValue() != &getAssociatedValue();
  4254. }
  4255. const DataLayout &DL = A.getDataLayout();
  4256. DerefState T;
  4257. auto VisitValueCB = [&](const Value &V) -> bool {
  4258. unsigned IdxWidth =
  4259. DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
  4260. APInt Offset(IdxWidth, 0);
  4261. const Value *Base = stripAndAccumulateOffsets(
  4262. A, *this, &V, DL, Offset, /* GetMinOffset */ false,
  4263. /* AllowNonInbounds */ true);
  4264. const auto &AA = A.getAAFor<AADereferenceable>(
  4265. *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
  4266. int64_t DerefBytes = 0;
  4267. if (!Stripped && this == &AA) {
  4268. // Use IR information if we did not strip anything.
  4269. // TODO: track globally.
  4270. bool CanBeNull, CanBeFreed;
  4271. DerefBytes =
  4272. Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
  4273. T.GlobalState.indicatePessimisticFixpoint();
  4274. } else {
  4275. const DerefState &DS = AA.getState();
  4276. DerefBytes = DS.DerefBytesState.getAssumed();
  4277. T.GlobalState &= DS.GlobalState;
  4278. }
  4279. // For now we do not try to "increase" dereferenceability due to negative
  4280. // indices as we first have to come up with code to deal with loops and
  4281. // for overflows of the dereferenceable bytes.
  4282. int64_t OffsetSExt = Offset.getSExtValue();
  4283. if (OffsetSExt < 0)
  4284. OffsetSExt = 0;
  4285. T.takeAssumedDerefBytesMinimum(
  4286. std::max(int64_t(0), DerefBytes - OffsetSExt));
  4287. if (this == &AA) {
  4288. if (!Stripped) {
  4289. // If nothing was stripped IR information is all we got.
  4290. T.takeKnownDerefBytesMaximum(
  4291. std::max(int64_t(0), DerefBytes - OffsetSExt));
  4292. T.indicatePessimisticFixpoint();
  4293. } else if (OffsetSExt > 0) {
  4294. // If something was stripped but there is circular reasoning we look
  4295. // for the offset. If it is positive we basically decrease the
  4296. // dereferenceable bytes in a circular loop now, which will simply
  4297. // drive them down to the known value in a very slow way which we
  4298. // can accelerate.
  4299. T.indicatePessimisticFixpoint();
  4300. }
  4301. }
  4302. return T.isValidState();
  4303. };
  4304. for (const auto &VAC : Values)
  4305. if (!VisitValueCB(*VAC.getValue()))
  4306. return indicatePessimisticFixpoint();
  4307. return clampStateAndIndicateChange(getState(), T);
  4308. }
  4309. /// See AbstractAttribute::trackStatistics()
  4310. void trackStatistics() const override {
  4311. STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
  4312. }
  4313. };
  4314. /// Dereferenceable attribute for a return value.
  4315. struct AADereferenceableReturned final
  4316. : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
  4317. AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
  4318. : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
  4319. IRP, A) {}
  4320. /// See AbstractAttribute::trackStatistics()
  4321. void trackStatistics() const override {
  4322. STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
  4323. }
  4324. };
  4325. /// Dereferenceable attribute for an argument
  4326. struct AADereferenceableArgument final
  4327. : AAArgumentFromCallSiteArguments<AADereferenceable,
  4328. AADereferenceableImpl> {
  4329. using Base =
  4330. AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
  4331. AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
  4332. : Base(IRP, A) {}
  4333. /// See AbstractAttribute::trackStatistics()
  4334. void trackStatistics() const override {
  4335. STATS_DECLTRACK_ARG_ATTR(dereferenceable)
  4336. }
  4337. };
  4338. /// Dereferenceable attribute for a call site argument.
  4339. struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
  4340. AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
  4341. : AADereferenceableFloating(IRP, A) {}
  4342. /// See AbstractAttribute::trackStatistics()
  4343. void trackStatistics() const override {
  4344. STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
  4345. }
  4346. };
  4347. /// Dereferenceable attribute deduction for a call site return value.
  4348. struct AADereferenceableCallSiteReturned final
  4349. : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
  4350. using Base =
  4351. AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
  4352. AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
  4353. : Base(IRP, A) {}
  4354. /// See AbstractAttribute::trackStatistics()
  4355. void trackStatistics() const override {
  4356. STATS_DECLTRACK_CS_ATTR(dereferenceable);
  4357. }
  4358. };
  4359. } // namespace
  4360. // ------------------------ Align Argument Attribute ------------------------
  4361. namespace {
  4362. static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
  4363. Value &AssociatedValue, const Use *U,
  4364. const Instruction *I, bool &TrackUse) {
  4365. // We need to follow common pointer manipulation uses to the accesses they
  4366. // feed into.
  4367. if (isa<CastInst>(I)) {
  4368. // Follow all but ptr2int casts.
  4369. TrackUse = !isa<PtrToIntInst>(I);
  4370. return 0;
  4371. }
  4372. if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
  4373. if (GEP->hasAllConstantIndices())
  4374. TrackUse = true;
  4375. return 0;
  4376. }
  4377. MaybeAlign MA;
  4378. if (const auto *CB = dyn_cast<CallBase>(I)) {
  4379. if (CB->isBundleOperand(U) || CB->isCallee(U))
  4380. return 0;
  4381. unsigned ArgNo = CB->getArgOperandNo(U);
  4382. IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
  4383. // As long as we only use known information there is no need to track
  4384. // dependences here.
  4385. auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
  4386. MA = MaybeAlign(AlignAA.getKnownAlign());
  4387. }
  4388. const DataLayout &DL = A.getDataLayout();
  4389. const Value *UseV = U->get();
  4390. if (auto *SI = dyn_cast<StoreInst>(I)) {
  4391. if (SI->getPointerOperand() == UseV)
  4392. MA = SI->getAlign();
  4393. } else if (auto *LI = dyn_cast<LoadInst>(I)) {
  4394. if (LI->getPointerOperand() == UseV)
  4395. MA = LI->getAlign();
  4396. }
  4397. if (!MA || *MA <= QueryingAA.getKnownAlign())
  4398. return 0;
  4399. unsigned Alignment = MA->value();
  4400. int64_t Offset;
  4401. if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
  4402. if (Base == &AssociatedValue) {
  4403. // BasePointerAddr + Offset = Alignment * Q for some integer Q.
  4404. // So we can say that the maximum power of two which is a divisor of
  4405. // gcd(Offset, Alignment) is an alignment.
  4406. uint32_t gcd = std::gcd(uint32_t(abs((int32_t)Offset)), Alignment);
  4407. Alignment = llvm::PowerOf2Floor(gcd);
  4408. }
  4409. }
  4410. return Alignment;
  4411. }
  4412. struct AAAlignImpl : AAAlign {
  4413. AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
  4414. /// See AbstractAttribute::initialize(...).
  4415. void initialize(Attributor &A) override {
  4416. SmallVector<Attribute, 4> Attrs;
  4417. getAttrs({Attribute::Alignment}, Attrs);
  4418. for (const Attribute &Attr : Attrs)
  4419. takeKnownMaximum(Attr.getValueAsInt());
  4420. Value &V = *getAssociatedValue().stripPointerCasts();
  4421. takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
  4422. if (getIRPosition().isFnInterfaceKind() &&
  4423. (!getAnchorScope() ||
  4424. !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
  4425. indicatePessimisticFixpoint();
  4426. return;
  4427. }
  4428. if (Instruction *CtxI = getCtxI())
  4429. followUsesInMBEC(*this, A, getState(), *CtxI);
  4430. }
  4431. /// See AbstractAttribute::manifest(...).
  4432. ChangeStatus manifest(Attributor &A) override {
  4433. ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
  4434. // Check for users that allow alignment annotations.
  4435. Value &AssociatedValue = getAssociatedValue();
  4436. for (const Use &U : AssociatedValue.uses()) {
  4437. if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
  4438. if (SI->getPointerOperand() == &AssociatedValue)
  4439. if (SI->getAlign() < getAssumedAlign()) {
  4440. STATS_DECLTRACK(AAAlign, Store,
  4441. "Number of times alignment added to a store");
  4442. SI->setAlignment(getAssumedAlign());
  4443. LoadStoreChanged = ChangeStatus::CHANGED;
  4444. }
  4445. } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
  4446. if (LI->getPointerOperand() == &AssociatedValue)
  4447. if (LI->getAlign() < getAssumedAlign()) {
  4448. LI->setAlignment(getAssumedAlign());
  4449. STATS_DECLTRACK(AAAlign, Load,
  4450. "Number of times alignment added to a load");
  4451. LoadStoreChanged = ChangeStatus::CHANGED;
  4452. }
  4453. }
  4454. }
  4455. ChangeStatus Changed = AAAlign::manifest(A);
  4456. Align InheritAlign =
  4457. getAssociatedValue().getPointerAlignment(A.getDataLayout());
  4458. if (InheritAlign >= getAssumedAlign())
  4459. return LoadStoreChanged;
  4460. return Changed | LoadStoreChanged;
  4461. }
  4462. // TODO: Provide a helper to determine the implied ABI alignment and check in
  4463. // the existing manifest method and a new one for AAAlignImpl that value
  4464. // to avoid making the alignment explicit if it did not improve.
  4465. /// See AbstractAttribute::getDeducedAttributes
  4466. void getDeducedAttributes(LLVMContext &Ctx,
  4467. SmallVectorImpl<Attribute> &Attrs) const override {
  4468. if (getAssumedAlign() > 1)
  4469. Attrs.emplace_back(
  4470. Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
  4471. }
  4472. /// See followUsesInMBEC
  4473. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  4474. AAAlign::StateType &State) {
  4475. bool TrackUse = false;
  4476. unsigned int KnownAlign =
  4477. getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
  4478. State.takeKnownMaximum(KnownAlign);
  4479. return TrackUse;
  4480. }
  4481. /// See AbstractAttribute::getAsStr().
  4482. const std::string getAsStr() const override {
  4483. return "align<" + std::to_string(getKnownAlign().value()) + "-" +
  4484. std::to_string(getAssumedAlign().value()) + ">";
  4485. }
  4486. };
  4487. /// Align attribute for a floating value.
  4488. struct AAAlignFloating : AAAlignImpl {
  4489. AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
  4490. /// See AbstractAttribute::updateImpl(...).
  4491. ChangeStatus updateImpl(Attributor &A) override {
  4492. const DataLayout &DL = A.getDataLayout();
  4493. bool Stripped;
  4494. bool UsedAssumedInformation = false;
  4495. SmallVector<AA::ValueAndContext> Values;
  4496. if (!A.getAssumedSimplifiedValues(getIRPosition(), *this, Values,
  4497. AA::AnyScope, UsedAssumedInformation)) {
  4498. Values.push_back({getAssociatedValue(), getCtxI()});
  4499. Stripped = false;
  4500. } else {
  4501. Stripped = Values.size() != 1 ||
  4502. Values.front().getValue() != &getAssociatedValue();
  4503. }
  4504. StateType T;
  4505. auto VisitValueCB = [&](Value &V) -> bool {
  4506. if (isa<UndefValue>(V) || isa<ConstantPointerNull>(V))
  4507. return true;
  4508. const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
  4509. DepClassTy::REQUIRED);
  4510. if (!Stripped && this == &AA) {
  4511. int64_t Offset;
  4512. unsigned Alignment = 1;
  4513. if (const Value *Base =
  4514. GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
  4515. // TODO: Use AAAlign for the base too.
  4516. Align PA = Base->getPointerAlignment(DL);
  4517. // BasePointerAddr + Offset = Alignment * Q for some integer Q.
  4518. // So we can say that the maximum power of two which is a divisor of
  4519. // gcd(Offset, Alignment) is an alignment.
  4520. uint32_t gcd =
  4521. std::gcd(uint32_t(abs((int32_t)Offset)), uint32_t(PA.value()));
  4522. Alignment = llvm::PowerOf2Floor(gcd);
  4523. } else {
  4524. Alignment = V.getPointerAlignment(DL).value();
  4525. }
  4526. // Use only IR information if we did not strip anything.
  4527. T.takeKnownMaximum(Alignment);
  4528. T.indicatePessimisticFixpoint();
  4529. } else {
  4530. // Use abstract attribute information.
  4531. const AAAlign::StateType &DS = AA.getState();
  4532. T ^= DS;
  4533. }
  4534. return T.isValidState();
  4535. };
  4536. for (const auto &VAC : Values) {
  4537. if (!VisitValueCB(*VAC.getValue()))
  4538. return indicatePessimisticFixpoint();
  4539. }
  4540. // TODO: If we know we visited all incoming values, thus no are assumed
  4541. // dead, we can take the known information from the state T.
  4542. return clampStateAndIndicateChange(getState(), T);
  4543. }
  4544. /// See AbstractAttribute::trackStatistics()
  4545. void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
  4546. };
  4547. /// Align attribute for function return value.
  4548. struct AAAlignReturned final
  4549. : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
  4550. using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
  4551. AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
  4552. /// See AbstractAttribute::initialize(...).
  4553. void initialize(Attributor &A) override {
  4554. Base::initialize(A);
  4555. Function *F = getAssociatedFunction();
  4556. if (!F || F->isDeclaration())
  4557. indicatePessimisticFixpoint();
  4558. }
  4559. /// See AbstractAttribute::trackStatistics()
  4560. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
  4561. };
  4562. /// Align attribute for function argument.
  4563. struct AAAlignArgument final
  4564. : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
  4565. using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
  4566. AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
  4567. /// See AbstractAttribute::manifest(...).
  4568. ChangeStatus manifest(Attributor &A) override {
  4569. // If the associated argument is involved in a must-tail call we give up
  4570. // because we would need to keep the argument alignments of caller and
  4571. // callee in-sync. Just does not seem worth the trouble right now.
  4572. if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
  4573. return ChangeStatus::UNCHANGED;
  4574. return Base::manifest(A);
  4575. }
  4576. /// See AbstractAttribute::trackStatistics()
  4577. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
  4578. };
  4579. struct AAAlignCallSiteArgument final : AAAlignFloating {
  4580. AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
  4581. : AAAlignFloating(IRP, A) {}
  4582. /// See AbstractAttribute::manifest(...).
  4583. ChangeStatus manifest(Attributor &A) override {
  4584. // If the associated argument is involved in a must-tail call we give up
  4585. // because we would need to keep the argument alignments of caller and
  4586. // callee in-sync. Just does not seem worth the trouble right now.
  4587. if (Argument *Arg = getAssociatedArgument())
  4588. if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
  4589. return ChangeStatus::UNCHANGED;
  4590. ChangeStatus Changed = AAAlignImpl::manifest(A);
  4591. Align InheritAlign =
  4592. getAssociatedValue().getPointerAlignment(A.getDataLayout());
  4593. if (InheritAlign >= getAssumedAlign())
  4594. Changed = ChangeStatus::UNCHANGED;
  4595. return Changed;
  4596. }
  4597. /// See AbstractAttribute::updateImpl(Attributor &A).
  4598. ChangeStatus updateImpl(Attributor &A) override {
  4599. ChangeStatus Changed = AAAlignFloating::updateImpl(A);
  4600. if (Argument *Arg = getAssociatedArgument()) {
  4601. // We only take known information from the argument
  4602. // so we do not need to track a dependence.
  4603. const auto &ArgAlignAA = A.getAAFor<AAAlign>(
  4604. *this, IRPosition::argument(*Arg), DepClassTy::NONE);
  4605. takeKnownMaximum(ArgAlignAA.getKnownAlign().value());
  4606. }
  4607. return Changed;
  4608. }
  4609. /// See AbstractAttribute::trackStatistics()
  4610. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
  4611. };
  4612. /// Align attribute deduction for a call site return value.
  4613. struct AAAlignCallSiteReturned final
  4614. : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
  4615. using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
  4616. AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
  4617. : Base(IRP, A) {}
  4618. /// See AbstractAttribute::initialize(...).
  4619. void initialize(Attributor &A) override {
  4620. Base::initialize(A);
  4621. Function *F = getAssociatedFunction();
  4622. if (!F || F->isDeclaration())
  4623. indicatePessimisticFixpoint();
  4624. }
  4625. /// See AbstractAttribute::trackStatistics()
  4626. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
  4627. };
  4628. } // namespace
  4629. /// ------------------ Function No-Return Attribute ----------------------------
  4630. namespace {
  4631. struct AANoReturnImpl : public AANoReturn {
  4632. AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
  4633. /// See AbstractAttribute::initialize(...).
  4634. void initialize(Attributor &A) override {
  4635. AANoReturn::initialize(A);
  4636. Function *F = getAssociatedFunction();
  4637. if (!F || F->isDeclaration())
  4638. indicatePessimisticFixpoint();
  4639. }
  4640. /// See AbstractAttribute::getAsStr().
  4641. const std::string getAsStr() const override {
  4642. return getAssumed() ? "noreturn" : "may-return";
  4643. }
  4644. /// See AbstractAttribute::updateImpl(Attributor &A).
  4645. ChangeStatus updateImpl(Attributor &A) override {
  4646. auto CheckForNoReturn = [](Instruction &) { return false; };
  4647. bool UsedAssumedInformation = false;
  4648. if (!A.checkForAllInstructions(CheckForNoReturn, *this,
  4649. {(unsigned)Instruction::Ret},
  4650. UsedAssumedInformation))
  4651. return indicatePessimisticFixpoint();
  4652. return ChangeStatus::UNCHANGED;
  4653. }
  4654. };
  4655. struct AANoReturnFunction final : AANoReturnImpl {
  4656. AANoReturnFunction(const IRPosition &IRP, Attributor &A)
  4657. : AANoReturnImpl(IRP, A) {}
  4658. /// See AbstractAttribute::trackStatistics()
  4659. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
  4660. };
  4661. /// NoReturn attribute deduction for a call sites.
  4662. struct AANoReturnCallSite final : AANoReturnImpl {
  4663. AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
  4664. : AANoReturnImpl(IRP, A) {}
  4665. /// See AbstractAttribute::initialize(...).
  4666. void initialize(Attributor &A) override {
  4667. AANoReturnImpl::initialize(A);
  4668. if (Function *F = getAssociatedFunction()) {
  4669. const IRPosition &FnPos = IRPosition::function(*F);
  4670. auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
  4671. if (!FnAA.isAssumedNoReturn())
  4672. indicatePessimisticFixpoint();
  4673. }
  4674. }
  4675. /// See AbstractAttribute::updateImpl(...).
  4676. ChangeStatus updateImpl(Attributor &A) override {
  4677. // TODO: Once we have call site specific value information we can provide
  4678. // call site specific liveness information and then it makes
  4679. // sense to specialize attributes for call sites arguments instead of
  4680. // redirecting requests to the callee argument.
  4681. Function *F = getAssociatedFunction();
  4682. const IRPosition &FnPos = IRPosition::function(*F);
  4683. auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
  4684. return clampStateAndIndicateChange(getState(), FnAA.getState());
  4685. }
  4686. /// See AbstractAttribute::trackStatistics()
  4687. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
  4688. };
  4689. } // namespace
  4690. /// ----------------------- Instance Info ---------------------------------
  4691. namespace {
  4692. /// A class to hold the state of for no-capture attributes.
  4693. struct AAInstanceInfoImpl : public AAInstanceInfo {
  4694. AAInstanceInfoImpl(const IRPosition &IRP, Attributor &A)
  4695. : AAInstanceInfo(IRP, A) {}
  4696. /// See AbstractAttribute::initialize(...).
  4697. void initialize(Attributor &A) override {
  4698. Value &V = getAssociatedValue();
  4699. if (auto *C = dyn_cast<Constant>(&V)) {
  4700. if (C->isThreadDependent())
  4701. indicatePessimisticFixpoint();
  4702. else
  4703. indicateOptimisticFixpoint();
  4704. return;
  4705. }
  4706. if (auto *CB = dyn_cast<CallBase>(&V))
  4707. if (CB->arg_size() == 0 && !CB->mayHaveSideEffects() &&
  4708. !CB->mayReadFromMemory()) {
  4709. indicateOptimisticFixpoint();
  4710. return;
  4711. }
  4712. }
  4713. /// See AbstractAttribute::updateImpl(...).
  4714. ChangeStatus updateImpl(Attributor &A) override {
  4715. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  4716. Value &V = getAssociatedValue();
  4717. const Function *Scope = nullptr;
  4718. if (auto *I = dyn_cast<Instruction>(&V))
  4719. Scope = I->getFunction();
  4720. if (auto *A = dyn_cast<Argument>(&V)) {
  4721. Scope = A->getParent();
  4722. if (!Scope->hasLocalLinkage())
  4723. return Changed;
  4724. }
  4725. if (!Scope)
  4726. return indicateOptimisticFixpoint();
  4727. auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  4728. *this, IRPosition::function(*Scope), DepClassTy::OPTIONAL);
  4729. if (NoRecurseAA.isAssumedNoRecurse())
  4730. return Changed;
  4731. auto UsePred = [&](const Use &U, bool &Follow) {
  4732. const Instruction *UserI = dyn_cast<Instruction>(U.getUser());
  4733. if (!UserI || isa<GetElementPtrInst>(UserI) || isa<CastInst>(UserI) ||
  4734. isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
  4735. Follow = true;
  4736. return true;
  4737. }
  4738. if (isa<LoadInst>(UserI) || isa<CmpInst>(UserI) ||
  4739. (isa<StoreInst>(UserI) &&
  4740. cast<StoreInst>(UserI)->getValueOperand() != U.get()))
  4741. return true;
  4742. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  4743. // This check is not guaranteeing uniqueness but for now that we cannot
  4744. // end up with two versions of \p U thinking it was one.
  4745. if (!CB->getCalledFunction() ||
  4746. !CB->getCalledFunction()->hasLocalLinkage())
  4747. return true;
  4748. if (!CB->isArgOperand(&U))
  4749. return false;
  4750. const auto &ArgInstanceInfoAA = A.getAAFor<AAInstanceInfo>(
  4751. *this, IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U)),
  4752. DepClassTy::OPTIONAL);
  4753. if (!ArgInstanceInfoAA.isAssumedUniqueForAnalysis())
  4754. return false;
  4755. // If this call base might reach the scope again we might forward the
  4756. // argument back here. This is very conservative.
  4757. if (AA::isPotentiallyReachable(
  4758. A, *CB, *Scope, *this, /* ExclusionSet */ nullptr,
  4759. [Scope](const Function &Fn) { return &Fn != Scope; }))
  4760. return false;
  4761. return true;
  4762. }
  4763. return false;
  4764. };
  4765. auto EquivalentUseCB = [&](const Use &OldU, const Use &NewU) {
  4766. if (auto *SI = dyn_cast<StoreInst>(OldU.getUser())) {
  4767. auto *Ptr = SI->getPointerOperand()->stripPointerCasts();
  4768. if ((isa<AllocaInst>(Ptr) || isNoAliasCall(Ptr)) &&
  4769. AA::isDynamicallyUnique(A, *this, *Ptr))
  4770. return true;
  4771. }
  4772. return false;
  4773. };
  4774. if (!A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ true,
  4775. DepClassTy::OPTIONAL,
  4776. /* IgnoreDroppableUses */ true, EquivalentUseCB))
  4777. return indicatePessimisticFixpoint();
  4778. return Changed;
  4779. }
  4780. /// See AbstractState::getAsStr().
  4781. const std::string getAsStr() const override {
  4782. return isAssumedUniqueForAnalysis() ? "<unique [fAa]>" : "<unknown>";
  4783. }
  4784. /// See AbstractAttribute::trackStatistics()
  4785. void trackStatistics() const override {}
  4786. };
  4787. /// InstanceInfo attribute for floating values.
  4788. struct AAInstanceInfoFloating : AAInstanceInfoImpl {
  4789. AAInstanceInfoFloating(const IRPosition &IRP, Attributor &A)
  4790. : AAInstanceInfoImpl(IRP, A) {}
  4791. };
  4792. /// NoCapture attribute for function arguments.
  4793. struct AAInstanceInfoArgument final : AAInstanceInfoFloating {
  4794. AAInstanceInfoArgument(const IRPosition &IRP, Attributor &A)
  4795. : AAInstanceInfoFloating(IRP, A) {}
  4796. };
  4797. /// InstanceInfo attribute for call site arguments.
  4798. struct AAInstanceInfoCallSiteArgument final : AAInstanceInfoImpl {
  4799. AAInstanceInfoCallSiteArgument(const IRPosition &IRP, Attributor &A)
  4800. : AAInstanceInfoImpl(IRP, A) {}
  4801. /// See AbstractAttribute::updateImpl(...).
  4802. ChangeStatus updateImpl(Attributor &A) override {
  4803. // TODO: Once we have call site specific value information we can provide
  4804. // call site specific liveness information and then it makes
  4805. // sense to specialize attributes for call sites arguments instead of
  4806. // redirecting requests to the callee argument.
  4807. Argument *Arg = getAssociatedArgument();
  4808. if (!Arg)
  4809. return indicatePessimisticFixpoint();
  4810. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  4811. auto &ArgAA =
  4812. A.getAAFor<AAInstanceInfo>(*this, ArgPos, DepClassTy::REQUIRED);
  4813. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  4814. }
  4815. };
  4816. /// InstanceInfo attribute for function return value.
  4817. struct AAInstanceInfoReturned final : AAInstanceInfoImpl {
  4818. AAInstanceInfoReturned(const IRPosition &IRP, Attributor &A)
  4819. : AAInstanceInfoImpl(IRP, A) {
  4820. llvm_unreachable("InstanceInfo is not applicable to function returns!");
  4821. }
  4822. /// See AbstractAttribute::initialize(...).
  4823. void initialize(Attributor &A) override {
  4824. llvm_unreachable("InstanceInfo is not applicable to function returns!");
  4825. }
  4826. /// See AbstractAttribute::updateImpl(...).
  4827. ChangeStatus updateImpl(Attributor &A) override {
  4828. llvm_unreachable("InstanceInfo is not applicable to function returns!");
  4829. }
  4830. };
  4831. /// InstanceInfo attribute deduction for a call site return value.
  4832. struct AAInstanceInfoCallSiteReturned final : AAInstanceInfoFloating {
  4833. AAInstanceInfoCallSiteReturned(const IRPosition &IRP, Attributor &A)
  4834. : AAInstanceInfoFloating(IRP, A) {}
  4835. };
  4836. } // namespace
  4837. /// ----------------------- Variable Capturing ---------------------------------
  4838. namespace {
  4839. /// A class to hold the state of for no-capture attributes.
  4840. struct AANoCaptureImpl : public AANoCapture {
  4841. AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
  4842. /// See AbstractAttribute::initialize(...).
  4843. void initialize(Attributor &A) override {
  4844. if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
  4845. indicateOptimisticFixpoint();
  4846. return;
  4847. }
  4848. Function *AnchorScope = getAnchorScope();
  4849. if (isFnInterfaceKind() &&
  4850. (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
  4851. indicatePessimisticFixpoint();
  4852. return;
  4853. }
  4854. // You cannot "capture" null in the default address space.
  4855. if (isa<ConstantPointerNull>(getAssociatedValue()) &&
  4856. getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
  4857. indicateOptimisticFixpoint();
  4858. return;
  4859. }
  4860. const Function *F =
  4861. isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
  4862. // Check what state the associated function can actually capture.
  4863. if (F)
  4864. determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
  4865. else
  4866. indicatePessimisticFixpoint();
  4867. }
  4868. /// See AbstractAttribute::updateImpl(...).
  4869. ChangeStatus updateImpl(Attributor &A) override;
  4870. /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
  4871. void getDeducedAttributes(LLVMContext &Ctx,
  4872. SmallVectorImpl<Attribute> &Attrs) const override {
  4873. if (!isAssumedNoCaptureMaybeReturned())
  4874. return;
  4875. if (isArgumentPosition()) {
  4876. if (isAssumedNoCapture())
  4877. Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
  4878. else if (ManifestInternal)
  4879. Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
  4880. }
  4881. }
  4882. /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
  4883. /// depending on the ability of the function associated with \p IRP to capture
  4884. /// state in memory and through "returning/throwing", respectively.
  4885. static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
  4886. const Function &F,
  4887. BitIntegerState &State) {
  4888. // TODO: Once we have memory behavior attributes we should use them here.
  4889. // If we know we cannot communicate or write to memory, we do not care about
  4890. // ptr2int anymore.
  4891. if (F.onlyReadsMemory() && F.doesNotThrow() &&
  4892. F.getReturnType()->isVoidTy()) {
  4893. State.addKnownBits(NO_CAPTURE);
  4894. return;
  4895. }
  4896. // A function cannot capture state in memory if it only reads memory, it can
  4897. // however return/throw state and the state might be influenced by the
  4898. // pointer value, e.g., loading from a returned pointer might reveal a bit.
  4899. if (F.onlyReadsMemory())
  4900. State.addKnownBits(NOT_CAPTURED_IN_MEM);
  4901. // A function cannot communicate state back if it does not through
  4902. // exceptions and doesn not return values.
  4903. if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
  4904. State.addKnownBits(NOT_CAPTURED_IN_RET);
  4905. // Check existing "returned" attributes.
  4906. int ArgNo = IRP.getCalleeArgNo();
  4907. if (F.doesNotThrow() && ArgNo >= 0) {
  4908. for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
  4909. if (F.hasParamAttribute(u, Attribute::Returned)) {
  4910. if (u == unsigned(ArgNo))
  4911. State.removeAssumedBits(NOT_CAPTURED_IN_RET);
  4912. else if (F.onlyReadsMemory())
  4913. State.addKnownBits(NO_CAPTURE);
  4914. else
  4915. State.addKnownBits(NOT_CAPTURED_IN_RET);
  4916. break;
  4917. }
  4918. }
  4919. }
  4920. /// See AbstractState::getAsStr().
  4921. const std::string getAsStr() const override {
  4922. if (isKnownNoCapture())
  4923. return "known not-captured";
  4924. if (isAssumedNoCapture())
  4925. return "assumed not-captured";
  4926. if (isKnownNoCaptureMaybeReturned())
  4927. return "known not-captured-maybe-returned";
  4928. if (isAssumedNoCaptureMaybeReturned())
  4929. return "assumed not-captured-maybe-returned";
  4930. return "assumed-captured";
  4931. }
  4932. /// Check the use \p U and update \p State accordingly. Return true if we
  4933. /// should continue to update the state.
  4934. bool checkUse(Attributor &A, AANoCapture::StateType &State, const Use &U,
  4935. bool &Follow) {
  4936. Instruction *UInst = cast<Instruction>(U.getUser());
  4937. LLVM_DEBUG(dbgs() << "[AANoCapture] Check use: " << *U.get() << " in "
  4938. << *UInst << "\n");
  4939. // Deal with ptr2int by following uses.
  4940. if (isa<PtrToIntInst>(UInst)) {
  4941. LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
  4942. return isCapturedIn(State, /* Memory */ true, /* Integer */ true,
  4943. /* Return */ true);
  4944. }
  4945. // For stores we already checked if we can follow them, if they make it
  4946. // here we give up.
  4947. if (isa<StoreInst>(UInst))
  4948. return isCapturedIn(State, /* Memory */ true, /* Integer */ false,
  4949. /* Return */ false);
  4950. // Explicitly catch return instructions.
  4951. if (isa<ReturnInst>(UInst)) {
  4952. if (UInst->getFunction() == getAnchorScope())
  4953. return isCapturedIn(State, /* Memory */ false, /* Integer */ false,
  4954. /* Return */ true);
  4955. return isCapturedIn(State, /* Memory */ true, /* Integer */ true,
  4956. /* Return */ true);
  4957. }
  4958. // For now we only use special logic for call sites. However, the tracker
  4959. // itself knows about a lot of other non-capturing cases already.
  4960. auto *CB = dyn_cast<CallBase>(UInst);
  4961. if (!CB || !CB->isArgOperand(&U))
  4962. return isCapturedIn(State, /* Memory */ true, /* Integer */ true,
  4963. /* Return */ true);
  4964. unsigned ArgNo = CB->getArgOperandNo(&U);
  4965. const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
  4966. // If we have a abstract no-capture attribute for the argument we can use
  4967. // it to justify a non-capture attribute here. This allows recursion!
  4968. auto &ArgNoCaptureAA =
  4969. A.getAAFor<AANoCapture>(*this, CSArgPos, DepClassTy::REQUIRED);
  4970. if (ArgNoCaptureAA.isAssumedNoCapture())
  4971. return isCapturedIn(State, /* Memory */ false, /* Integer */ false,
  4972. /* Return */ false);
  4973. if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
  4974. Follow = true;
  4975. return isCapturedIn(State, /* Memory */ false, /* Integer */ false,
  4976. /* Return */ false);
  4977. }
  4978. // Lastly, we could not find a reason no-capture can be assumed so we don't.
  4979. return isCapturedIn(State, /* Memory */ true, /* Integer */ true,
  4980. /* Return */ true);
  4981. }
  4982. /// Update \p State according to \p CapturedInMem, \p CapturedInInt, and
  4983. /// \p CapturedInRet, then return true if we should continue updating the
  4984. /// state.
  4985. static bool isCapturedIn(AANoCapture::StateType &State, bool CapturedInMem,
  4986. bool CapturedInInt, bool CapturedInRet) {
  4987. LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
  4988. << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
  4989. if (CapturedInMem)
  4990. State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
  4991. if (CapturedInInt)
  4992. State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
  4993. if (CapturedInRet)
  4994. State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
  4995. return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
  4996. }
  4997. };
  4998. ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
  4999. const IRPosition &IRP = getIRPosition();
  5000. Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
  5001. : &IRP.getAssociatedValue();
  5002. if (!V)
  5003. return indicatePessimisticFixpoint();
  5004. const Function *F =
  5005. isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
  5006. assert(F && "Expected a function!");
  5007. const IRPosition &FnPos = IRPosition::function(*F);
  5008. AANoCapture::StateType T;
  5009. // Readonly means we cannot capture through memory.
  5010. bool IsKnown;
  5011. if (AA::isAssumedReadOnly(A, FnPos, *this, IsKnown)) {
  5012. T.addKnownBits(NOT_CAPTURED_IN_MEM);
  5013. if (IsKnown)
  5014. addKnownBits(NOT_CAPTURED_IN_MEM);
  5015. }
  5016. // Make sure all returned values are different than the underlying value.
  5017. // TODO: we could do this in a more sophisticated way inside
  5018. // AAReturnedValues, e.g., track all values that escape through returns
  5019. // directly somehow.
  5020. auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
  5021. if (!RVAA.getState().isValidState())
  5022. return false;
  5023. bool SeenConstant = false;
  5024. for (const auto &It : RVAA.returned_values()) {
  5025. if (isa<Constant>(It.first)) {
  5026. if (SeenConstant)
  5027. return false;
  5028. SeenConstant = true;
  5029. } else if (!isa<Argument>(It.first) ||
  5030. It.first == getAssociatedArgument())
  5031. return false;
  5032. }
  5033. return true;
  5034. };
  5035. const auto &NoUnwindAA =
  5036. A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
  5037. if (NoUnwindAA.isAssumedNoUnwind()) {
  5038. bool IsVoidTy = F->getReturnType()->isVoidTy();
  5039. const AAReturnedValues *RVAA =
  5040. IsVoidTy ? nullptr
  5041. : &A.getAAFor<AAReturnedValues>(*this, FnPos,
  5042. DepClassTy::OPTIONAL);
  5043. if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
  5044. T.addKnownBits(NOT_CAPTURED_IN_RET);
  5045. if (T.isKnown(NOT_CAPTURED_IN_MEM))
  5046. return ChangeStatus::UNCHANGED;
  5047. if (NoUnwindAA.isKnownNoUnwind() &&
  5048. (IsVoidTy || RVAA->getState().isAtFixpoint())) {
  5049. addKnownBits(NOT_CAPTURED_IN_RET);
  5050. if (isKnown(NOT_CAPTURED_IN_MEM))
  5051. return indicateOptimisticFixpoint();
  5052. }
  5053. }
  5054. }
  5055. auto IsDereferenceableOrNull = [&](Value *O, const DataLayout &DL) {
  5056. const auto &DerefAA = A.getAAFor<AADereferenceable>(
  5057. *this, IRPosition::value(*O), DepClassTy::OPTIONAL);
  5058. return DerefAA.getAssumedDereferenceableBytes();
  5059. };
  5060. auto UseCheck = [&](const Use &U, bool &Follow) -> bool {
  5061. switch (DetermineUseCaptureKind(U, IsDereferenceableOrNull)) {
  5062. case UseCaptureKind::NO_CAPTURE:
  5063. return true;
  5064. case UseCaptureKind::MAY_CAPTURE:
  5065. return checkUse(A, T, U, Follow);
  5066. case UseCaptureKind::PASSTHROUGH:
  5067. Follow = true;
  5068. return true;
  5069. }
  5070. llvm_unreachable("Unexpected use capture kind!");
  5071. };
  5072. if (!A.checkForAllUses(UseCheck, *this, *V))
  5073. return indicatePessimisticFixpoint();
  5074. AANoCapture::StateType &S = getState();
  5075. auto Assumed = S.getAssumed();
  5076. S.intersectAssumedBits(T.getAssumed());
  5077. if (!isAssumedNoCaptureMaybeReturned())
  5078. return indicatePessimisticFixpoint();
  5079. return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
  5080. : ChangeStatus::CHANGED;
  5081. }
  5082. /// NoCapture attribute for function arguments.
  5083. struct AANoCaptureArgument final : AANoCaptureImpl {
  5084. AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
  5085. : AANoCaptureImpl(IRP, A) {}
  5086. /// See AbstractAttribute::trackStatistics()
  5087. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
  5088. };
  5089. /// NoCapture attribute for call site arguments.
  5090. struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
  5091. AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
  5092. : AANoCaptureImpl(IRP, A) {}
  5093. /// See AbstractAttribute::initialize(...).
  5094. void initialize(Attributor &A) override {
  5095. if (Argument *Arg = getAssociatedArgument())
  5096. if (Arg->hasByValAttr())
  5097. indicateOptimisticFixpoint();
  5098. AANoCaptureImpl::initialize(A);
  5099. }
  5100. /// See AbstractAttribute::updateImpl(...).
  5101. ChangeStatus updateImpl(Attributor &A) override {
  5102. // TODO: Once we have call site specific value information we can provide
  5103. // call site specific liveness information and then it makes
  5104. // sense to specialize attributes for call sites arguments instead of
  5105. // redirecting requests to the callee argument.
  5106. Argument *Arg = getAssociatedArgument();
  5107. if (!Arg)
  5108. return indicatePessimisticFixpoint();
  5109. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  5110. auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
  5111. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  5112. }
  5113. /// See AbstractAttribute::trackStatistics()
  5114. void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
  5115. };
  5116. /// NoCapture attribute for floating values.
  5117. struct AANoCaptureFloating final : AANoCaptureImpl {
  5118. AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
  5119. : AANoCaptureImpl(IRP, A) {}
  5120. /// See AbstractAttribute::trackStatistics()
  5121. void trackStatistics() const override {
  5122. STATS_DECLTRACK_FLOATING_ATTR(nocapture)
  5123. }
  5124. };
  5125. /// NoCapture attribute for function return value.
  5126. struct AANoCaptureReturned final : AANoCaptureImpl {
  5127. AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
  5128. : AANoCaptureImpl(IRP, A) {
  5129. llvm_unreachable("NoCapture is not applicable to function returns!");
  5130. }
  5131. /// See AbstractAttribute::initialize(...).
  5132. void initialize(Attributor &A) override {
  5133. llvm_unreachable("NoCapture is not applicable to function returns!");
  5134. }
  5135. /// See AbstractAttribute::updateImpl(...).
  5136. ChangeStatus updateImpl(Attributor &A) override {
  5137. llvm_unreachable("NoCapture is not applicable to function returns!");
  5138. }
  5139. /// See AbstractAttribute::trackStatistics()
  5140. void trackStatistics() const override {}
  5141. };
  5142. /// NoCapture attribute deduction for a call site return value.
  5143. struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
  5144. AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
  5145. : AANoCaptureImpl(IRP, A) {}
  5146. /// See AbstractAttribute::initialize(...).
  5147. void initialize(Attributor &A) override {
  5148. const Function *F = getAnchorScope();
  5149. // Check what state the associated function can actually capture.
  5150. determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
  5151. }
  5152. /// See AbstractAttribute::trackStatistics()
  5153. void trackStatistics() const override {
  5154. STATS_DECLTRACK_CSRET_ATTR(nocapture)
  5155. }
  5156. };
  5157. } // namespace
  5158. /// ------------------ Value Simplify Attribute ----------------------------
  5159. bool ValueSimplifyStateType::unionAssumed(std::optional<Value *> Other) {
  5160. // FIXME: Add a typecast support.
  5161. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  5162. SimplifiedAssociatedValue, Other, Ty);
  5163. if (SimplifiedAssociatedValue == std::optional<Value *>(nullptr))
  5164. return false;
  5165. LLVM_DEBUG({
  5166. if (SimplifiedAssociatedValue)
  5167. dbgs() << "[ValueSimplify] is assumed to be "
  5168. << **SimplifiedAssociatedValue << "\n";
  5169. else
  5170. dbgs() << "[ValueSimplify] is assumed to be <none>\n";
  5171. });
  5172. return true;
  5173. }
  5174. namespace {
  5175. struct AAValueSimplifyImpl : AAValueSimplify {
  5176. AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
  5177. : AAValueSimplify(IRP, A) {}
  5178. /// See AbstractAttribute::initialize(...).
  5179. void initialize(Attributor &A) override {
  5180. if (getAssociatedValue().getType()->isVoidTy())
  5181. indicatePessimisticFixpoint();
  5182. if (A.hasSimplificationCallback(getIRPosition()))
  5183. indicatePessimisticFixpoint();
  5184. }
  5185. /// See AbstractAttribute::getAsStr().
  5186. const std::string getAsStr() const override {
  5187. LLVM_DEBUG({
  5188. dbgs() << "SAV: " << (bool)SimplifiedAssociatedValue << " ";
  5189. if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)
  5190. dbgs() << "SAV: " << **SimplifiedAssociatedValue << " ";
  5191. });
  5192. return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple")
  5193. : "not-simple";
  5194. }
  5195. /// See AbstractAttribute::trackStatistics()
  5196. void trackStatistics() const override {}
  5197. /// See AAValueSimplify::getAssumedSimplifiedValue()
  5198. std::optional<Value *>
  5199. getAssumedSimplifiedValue(Attributor &A) const override {
  5200. return SimplifiedAssociatedValue;
  5201. }
  5202. /// Ensure the return value is \p V with type \p Ty, if not possible return
  5203. /// nullptr. If \p Check is true we will only verify such an operation would
  5204. /// suceed and return a non-nullptr value if that is the case. No IR is
  5205. /// generated or modified.
  5206. static Value *ensureType(Attributor &A, Value &V, Type &Ty, Instruction *CtxI,
  5207. bool Check) {
  5208. if (auto *TypedV = AA::getWithType(V, Ty))
  5209. return TypedV;
  5210. if (CtxI && V.getType()->canLosslesslyBitCastTo(&Ty))
  5211. return Check ? &V
  5212. : BitCastInst::CreatePointerBitCastOrAddrSpaceCast(&V, &Ty,
  5213. "", CtxI);
  5214. return nullptr;
  5215. }
  5216. /// Reproduce \p I with type \p Ty or return nullptr if that is not posisble.
  5217. /// If \p Check is true we will only verify such an operation would suceed and
  5218. /// return a non-nullptr value if that is the case. No IR is generated or
  5219. /// modified.
  5220. static Value *reproduceInst(Attributor &A,
  5221. const AbstractAttribute &QueryingAA,
  5222. Instruction &I, Type &Ty, Instruction *CtxI,
  5223. bool Check, ValueToValueMapTy &VMap) {
  5224. assert(CtxI && "Cannot reproduce an instruction without context!");
  5225. if (Check && (I.mayReadFromMemory() ||
  5226. !isSafeToSpeculativelyExecute(&I, CtxI, /* DT */ nullptr,
  5227. /* TLI */ nullptr)))
  5228. return nullptr;
  5229. for (Value *Op : I.operands()) {
  5230. Value *NewOp = reproduceValue(A, QueryingAA, *Op, Ty, CtxI, Check, VMap);
  5231. if (!NewOp) {
  5232. assert(Check && "Manifest of new value unexpectedly failed!");
  5233. return nullptr;
  5234. }
  5235. if (!Check)
  5236. VMap[Op] = NewOp;
  5237. }
  5238. if (Check)
  5239. return &I;
  5240. Instruction *CloneI = I.clone();
  5241. // TODO: Try to salvage debug information here.
  5242. CloneI->setDebugLoc(DebugLoc());
  5243. VMap[&I] = CloneI;
  5244. CloneI->insertBefore(CtxI);
  5245. RemapInstruction(CloneI, VMap);
  5246. return CloneI;
  5247. }
  5248. /// Reproduce \p V with type \p Ty or return nullptr if that is not posisble.
  5249. /// If \p Check is true we will only verify such an operation would suceed and
  5250. /// return a non-nullptr value if that is the case. No IR is generated or
  5251. /// modified.
  5252. static Value *reproduceValue(Attributor &A,
  5253. const AbstractAttribute &QueryingAA, Value &V,
  5254. Type &Ty, Instruction *CtxI, bool Check,
  5255. ValueToValueMapTy &VMap) {
  5256. if (const auto &NewV = VMap.lookup(&V))
  5257. return NewV;
  5258. bool UsedAssumedInformation = false;
  5259. std::optional<Value *> SimpleV = A.getAssumedSimplified(
  5260. V, QueryingAA, UsedAssumedInformation, AA::Interprocedural);
  5261. if (!SimpleV.has_value())
  5262. return PoisonValue::get(&Ty);
  5263. Value *EffectiveV = &V;
  5264. if (*SimpleV)
  5265. EffectiveV = *SimpleV;
  5266. if (auto *C = dyn_cast<Constant>(EffectiveV))
  5267. return C;
  5268. if (CtxI && AA::isValidAtPosition(AA::ValueAndContext(*EffectiveV, *CtxI),
  5269. A.getInfoCache()))
  5270. return ensureType(A, *EffectiveV, Ty, CtxI, Check);
  5271. if (auto *I = dyn_cast<Instruction>(EffectiveV))
  5272. if (Value *NewV = reproduceInst(A, QueryingAA, *I, Ty, CtxI, Check, VMap))
  5273. return ensureType(A, *NewV, Ty, CtxI, Check);
  5274. return nullptr;
  5275. }
  5276. /// Return a value we can use as replacement for the associated one, or
  5277. /// nullptr if we don't have one that makes sense.
  5278. Value *manifestReplacementValue(Attributor &A, Instruction *CtxI) const {
  5279. Value *NewV = SimplifiedAssociatedValue
  5280. ? *SimplifiedAssociatedValue
  5281. : UndefValue::get(getAssociatedType());
  5282. if (NewV && NewV != &getAssociatedValue()) {
  5283. ValueToValueMapTy VMap;
  5284. // First verify we can reprduce the value with the required type at the
  5285. // context location before we actually start modifying the IR.
  5286. if (reproduceValue(A, *this, *NewV, *getAssociatedType(), CtxI,
  5287. /* CheckOnly */ true, VMap))
  5288. return reproduceValue(A, *this, *NewV, *getAssociatedType(), CtxI,
  5289. /* CheckOnly */ false, VMap);
  5290. }
  5291. return nullptr;
  5292. }
  5293. /// Helper function for querying AAValueSimplify and updating candidate.
  5294. /// \param IRP The value position we are trying to unify with SimplifiedValue
  5295. bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
  5296. const IRPosition &IRP, bool Simplify = true) {
  5297. bool UsedAssumedInformation = false;
  5298. std::optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue();
  5299. if (Simplify)
  5300. QueryingValueSimplified = A.getAssumedSimplified(
  5301. IRP, QueryingAA, UsedAssumedInformation, AA::Interprocedural);
  5302. return unionAssumed(QueryingValueSimplified);
  5303. }
  5304. /// Returns a candidate is found or not
  5305. template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
  5306. if (!getAssociatedValue().getType()->isIntegerTy())
  5307. return false;
  5308. // This will also pass the call base context.
  5309. const auto &AA =
  5310. A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
  5311. std::optional<Constant *> COpt = AA.getAssumedConstant(A);
  5312. if (!COpt) {
  5313. SimplifiedAssociatedValue = std::nullopt;
  5314. A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
  5315. return true;
  5316. }
  5317. if (auto *C = *COpt) {
  5318. SimplifiedAssociatedValue = C;
  5319. A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
  5320. return true;
  5321. }
  5322. return false;
  5323. }
  5324. bool askSimplifiedValueForOtherAAs(Attributor &A) {
  5325. if (askSimplifiedValueFor<AAValueConstantRange>(A))
  5326. return true;
  5327. if (askSimplifiedValueFor<AAPotentialConstantValues>(A))
  5328. return true;
  5329. return false;
  5330. }
  5331. /// See AbstractAttribute::manifest(...).
  5332. ChangeStatus manifest(Attributor &A) override {
  5333. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  5334. for (auto &U : getAssociatedValue().uses()) {
  5335. // Check if we need to adjust the insertion point to make sure the IR is
  5336. // valid.
  5337. Instruction *IP = dyn_cast<Instruction>(U.getUser());
  5338. if (auto *PHI = dyn_cast_or_null<PHINode>(IP))
  5339. IP = PHI->getIncomingBlock(U)->getTerminator();
  5340. if (auto *NewV = manifestReplacementValue(A, IP)) {
  5341. LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue()
  5342. << " -> " << *NewV << " :: " << *this << "\n");
  5343. if (A.changeUseAfterManifest(U, *NewV))
  5344. Changed = ChangeStatus::CHANGED;
  5345. }
  5346. }
  5347. return Changed | AAValueSimplify::manifest(A);
  5348. }
  5349. /// See AbstractState::indicatePessimisticFixpoint(...).
  5350. ChangeStatus indicatePessimisticFixpoint() override {
  5351. SimplifiedAssociatedValue = &getAssociatedValue();
  5352. return AAValueSimplify::indicatePessimisticFixpoint();
  5353. }
  5354. };
  5355. struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
  5356. AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
  5357. : AAValueSimplifyImpl(IRP, A) {}
  5358. void initialize(Attributor &A) override {
  5359. AAValueSimplifyImpl::initialize(A);
  5360. if (!getAnchorScope() || getAnchorScope()->isDeclaration())
  5361. indicatePessimisticFixpoint();
  5362. if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
  5363. Attribute::StructRet, Attribute::Nest, Attribute::ByVal},
  5364. /* IgnoreSubsumingPositions */ true))
  5365. indicatePessimisticFixpoint();
  5366. }
  5367. /// See AbstractAttribute::updateImpl(...).
  5368. ChangeStatus updateImpl(Attributor &A) override {
  5369. // Byval is only replacable if it is readonly otherwise we would write into
  5370. // the replaced value and not the copy that byval creates implicitly.
  5371. Argument *Arg = getAssociatedArgument();
  5372. if (Arg->hasByValAttr()) {
  5373. // TODO: We probably need to verify synchronization is not an issue, e.g.,
  5374. // there is no race by not copying a constant byval.
  5375. bool IsKnown;
  5376. if (!AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
  5377. return indicatePessimisticFixpoint();
  5378. }
  5379. auto Before = SimplifiedAssociatedValue;
  5380. auto PredForCallSite = [&](AbstractCallSite ACS) {
  5381. const IRPosition &ACSArgPos =
  5382. IRPosition::callsite_argument(ACS, getCallSiteArgNo());
  5383. // Check if a coresponding argument was found or if it is on not
  5384. // associated (which can happen for callback calls).
  5385. if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
  5386. return false;
  5387. // Simplify the argument operand explicitly and check if the result is
  5388. // valid in the current scope. This avoids refering to simplified values
  5389. // in other functions, e.g., we don't want to say a an argument in a
  5390. // static function is actually an argument in a different function.
  5391. bool UsedAssumedInformation = false;
  5392. std::optional<Constant *> SimpleArgOp =
  5393. A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
  5394. if (!SimpleArgOp)
  5395. return true;
  5396. if (!*SimpleArgOp)
  5397. return false;
  5398. if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
  5399. return false;
  5400. return unionAssumed(*SimpleArgOp);
  5401. };
  5402. // Generate a answer specific to a call site context.
  5403. bool Success;
  5404. bool UsedAssumedInformation = false;
  5405. if (hasCallBaseContext() &&
  5406. getCallBaseContext()->getCalledFunction() == Arg->getParent())
  5407. Success = PredForCallSite(
  5408. AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
  5409. else
  5410. Success = A.checkForAllCallSites(PredForCallSite, *this, true,
  5411. UsedAssumedInformation);
  5412. if (!Success)
  5413. if (!askSimplifiedValueForOtherAAs(A))
  5414. return indicatePessimisticFixpoint();
  5415. // If a candidate was found in this update, return CHANGED.
  5416. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  5417. : ChangeStatus ::CHANGED;
  5418. }
  5419. /// See AbstractAttribute::trackStatistics()
  5420. void trackStatistics() const override {
  5421. STATS_DECLTRACK_ARG_ATTR(value_simplify)
  5422. }
  5423. };
  5424. struct AAValueSimplifyReturned : AAValueSimplifyImpl {
  5425. AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
  5426. : AAValueSimplifyImpl(IRP, A) {}
  5427. /// See AAValueSimplify::getAssumedSimplifiedValue()
  5428. std::optional<Value *>
  5429. getAssumedSimplifiedValue(Attributor &A) const override {
  5430. if (!isValidState())
  5431. return nullptr;
  5432. return SimplifiedAssociatedValue;
  5433. }
  5434. /// See AbstractAttribute::updateImpl(...).
  5435. ChangeStatus updateImpl(Attributor &A) override {
  5436. auto Before = SimplifiedAssociatedValue;
  5437. auto ReturnInstCB = [&](Instruction &I) {
  5438. auto &RI = cast<ReturnInst>(I);
  5439. return checkAndUpdate(
  5440. A, *this,
  5441. IRPosition::value(*RI.getReturnValue(), getCallBaseContext()));
  5442. };
  5443. bool UsedAssumedInformation = false;
  5444. if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret},
  5445. UsedAssumedInformation))
  5446. if (!askSimplifiedValueForOtherAAs(A))
  5447. return indicatePessimisticFixpoint();
  5448. // If a candidate was found in this update, return CHANGED.
  5449. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  5450. : ChangeStatus ::CHANGED;
  5451. }
  5452. ChangeStatus manifest(Attributor &A) override {
  5453. // We queried AAValueSimplify for the returned values so they will be
  5454. // replaced if a simplified form was found. Nothing to do here.
  5455. return ChangeStatus::UNCHANGED;
  5456. }
  5457. /// See AbstractAttribute::trackStatistics()
  5458. void trackStatistics() const override {
  5459. STATS_DECLTRACK_FNRET_ATTR(value_simplify)
  5460. }
  5461. };
  5462. struct AAValueSimplifyFloating : AAValueSimplifyImpl {
  5463. AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
  5464. : AAValueSimplifyImpl(IRP, A) {}
  5465. /// See AbstractAttribute::initialize(...).
  5466. void initialize(Attributor &A) override {
  5467. AAValueSimplifyImpl::initialize(A);
  5468. Value &V = getAnchorValue();
  5469. // TODO: add other stuffs
  5470. if (isa<Constant>(V))
  5471. indicatePessimisticFixpoint();
  5472. }
  5473. /// See AbstractAttribute::updateImpl(...).
  5474. ChangeStatus updateImpl(Attributor &A) override {
  5475. auto Before = SimplifiedAssociatedValue;
  5476. if (!askSimplifiedValueForOtherAAs(A))
  5477. return indicatePessimisticFixpoint();
  5478. // If a candidate was found in this update, return CHANGED.
  5479. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  5480. : ChangeStatus ::CHANGED;
  5481. }
  5482. /// See AbstractAttribute::trackStatistics()
  5483. void trackStatistics() const override {
  5484. STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
  5485. }
  5486. };
  5487. struct AAValueSimplifyFunction : AAValueSimplifyImpl {
  5488. AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
  5489. : AAValueSimplifyImpl(IRP, A) {}
  5490. /// See AbstractAttribute::initialize(...).
  5491. void initialize(Attributor &A) override {
  5492. SimplifiedAssociatedValue = nullptr;
  5493. indicateOptimisticFixpoint();
  5494. }
  5495. /// See AbstractAttribute::initialize(...).
  5496. ChangeStatus updateImpl(Attributor &A) override {
  5497. llvm_unreachable(
  5498. "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
  5499. }
  5500. /// See AbstractAttribute::trackStatistics()
  5501. void trackStatistics() const override {
  5502. STATS_DECLTRACK_FN_ATTR(value_simplify)
  5503. }
  5504. };
  5505. struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
  5506. AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
  5507. : AAValueSimplifyFunction(IRP, A) {}
  5508. /// See AbstractAttribute::trackStatistics()
  5509. void trackStatistics() const override {
  5510. STATS_DECLTRACK_CS_ATTR(value_simplify)
  5511. }
  5512. };
  5513. struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {
  5514. AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
  5515. : AAValueSimplifyImpl(IRP, A) {}
  5516. void initialize(Attributor &A) override {
  5517. AAValueSimplifyImpl::initialize(A);
  5518. Function *Fn = getAssociatedFunction();
  5519. if (!Fn) {
  5520. indicatePessimisticFixpoint();
  5521. return;
  5522. }
  5523. for (Argument &Arg : Fn->args()) {
  5524. if (Arg.hasReturnedAttr()) {
  5525. auto IRP = IRPosition::callsite_argument(*cast<CallBase>(getCtxI()),
  5526. Arg.getArgNo());
  5527. if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE_ARGUMENT &&
  5528. checkAndUpdate(A, *this, IRP))
  5529. indicateOptimisticFixpoint();
  5530. else
  5531. indicatePessimisticFixpoint();
  5532. return;
  5533. }
  5534. }
  5535. }
  5536. /// See AbstractAttribute::updateImpl(...).
  5537. ChangeStatus updateImpl(Attributor &A) override {
  5538. auto Before = SimplifiedAssociatedValue;
  5539. auto &RetAA = A.getAAFor<AAReturnedValues>(
  5540. *this, IRPosition::function(*getAssociatedFunction()),
  5541. DepClassTy::REQUIRED);
  5542. auto PredForReturned =
  5543. [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
  5544. bool UsedAssumedInformation = false;
  5545. std::optional<Value *> CSRetVal =
  5546. A.translateArgumentToCallSiteContent(
  5547. &RetVal, *cast<CallBase>(getCtxI()), *this,
  5548. UsedAssumedInformation);
  5549. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  5550. SimplifiedAssociatedValue, CSRetVal, getAssociatedType());
  5551. return SimplifiedAssociatedValue != std::optional<Value *>(nullptr);
  5552. };
  5553. if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned))
  5554. if (!askSimplifiedValueForOtherAAs(A))
  5555. return indicatePessimisticFixpoint();
  5556. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  5557. : ChangeStatus ::CHANGED;
  5558. }
  5559. void trackStatistics() const override {
  5560. STATS_DECLTRACK_CSRET_ATTR(value_simplify)
  5561. }
  5562. };
  5563. struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
  5564. AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
  5565. : AAValueSimplifyFloating(IRP, A) {}
  5566. /// See AbstractAttribute::manifest(...).
  5567. ChangeStatus manifest(Attributor &A) override {
  5568. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  5569. // TODO: We should avoid simplification duplication to begin with.
  5570. auto *FloatAA = A.lookupAAFor<AAValueSimplify>(
  5571. IRPosition::value(getAssociatedValue()), this, DepClassTy::NONE);
  5572. if (FloatAA && FloatAA->getState().isValidState())
  5573. return Changed;
  5574. if (auto *NewV = manifestReplacementValue(A, getCtxI())) {
  5575. Use &U = cast<CallBase>(&getAnchorValue())
  5576. ->getArgOperandUse(getCallSiteArgNo());
  5577. if (A.changeUseAfterManifest(U, *NewV))
  5578. Changed = ChangeStatus::CHANGED;
  5579. }
  5580. return Changed | AAValueSimplify::manifest(A);
  5581. }
  5582. void trackStatistics() const override {
  5583. STATS_DECLTRACK_CSARG_ATTR(value_simplify)
  5584. }
  5585. };
  5586. } // namespace
  5587. /// ----------------------- Heap-To-Stack Conversion ---------------------------
  5588. namespace {
  5589. struct AAHeapToStackFunction final : public AAHeapToStack {
  5590. struct AllocationInfo {
  5591. /// The call that allocates the memory.
  5592. CallBase *const CB;
  5593. /// The library function id for the allocation.
  5594. LibFunc LibraryFunctionId = NotLibFunc;
  5595. /// The status wrt. a rewrite.
  5596. enum {
  5597. STACK_DUE_TO_USE,
  5598. STACK_DUE_TO_FREE,
  5599. INVALID,
  5600. } Status = STACK_DUE_TO_USE;
  5601. /// Flag to indicate if we encountered a use that might free this allocation
  5602. /// but which is not in the deallocation infos.
  5603. bool HasPotentiallyFreeingUnknownUses = false;
  5604. /// Flag to indicate that we should place the new alloca in the function
  5605. /// entry block rather than where the call site (CB) is.
  5606. bool MoveAllocaIntoEntry = true;
  5607. /// The set of free calls that use this allocation.
  5608. SmallSetVector<CallBase *, 1> PotentialFreeCalls{};
  5609. };
  5610. struct DeallocationInfo {
  5611. /// The call that deallocates the memory.
  5612. CallBase *const CB;
  5613. /// The value freed by the call.
  5614. Value *FreedOp;
  5615. /// Flag to indicate if we don't know all objects this deallocation might
  5616. /// free.
  5617. bool MightFreeUnknownObjects = false;
  5618. /// The set of allocation calls that are potentially freed.
  5619. SmallSetVector<CallBase *, 1> PotentialAllocationCalls{};
  5620. };
  5621. AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
  5622. : AAHeapToStack(IRP, A) {}
  5623. ~AAHeapToStackFunction() {
  5624. // Ensure we call the destructor so we release any memory allocated in the
  5625. // sets.
  5626. for (auto &It : AllocationInfos)
  5627. It.second->~AllocationInfo();
  5628. for (auto &It : DeallocationInfos)
  5629. It.second->~DeallocationInfo();
  5630. }
  5631. void initialize(Attributor &A) override {
  5632. AAHeapToStack::initialize(A);
  5633. const Function *F = getAnchorScope();
  5634. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5635. auto AllocationIdentifierCB = [&](Instruction &I) {
  5636. CallBase *CB = dyn_cast<CallBase>(&I);
  5637. if (!CB)
  5638. return true;
  5639. if (Value *FreedOp = getFreedOperand(CB, TLI)) {
  5640. DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB, FreedOp};
  5641. return true;
  5642. }
  5643. // To do heap to stack, we need to know that the allocation itself is
  5644. // removable once uses are rewritten, and that we can initialize the
  5645. // alloca to the same pattern as the original allocation result.
  5646. if (isRemovableAlloc(CB, TLI)) {
  5647. auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext());
  5648. if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) {
  5649. AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB};
  5650. AllocationInfos[CB] = AI;
  5651. if (TLI)
  5652. TLI->getLibFunc(*CB, AI->LibraryFunctionId);
  5653. }
  5654. }
  5655. return true;
  5656. };
  5657. bool UsedAssumedInformation = false;
  5658. bool Success = A.checkForAllCallLikeInstructions(
  5659. AllocationIdentifierCB, *this, UsedAssumedInformation,
  5660. /* CheckBBLivenessOnly */ false,
  5661. /* CheckPotentiallyDead */ true);
  5662. (void)Success;
  5663. assert(Success && "Did not expect the call base visit callback to fail!");
  5664. Attributor::SimplifictionCallbackTy SCB =
  5665. [](const IRPosition &, const AbstractAttribute *,
  5666. bool &) -> std::optional<Value *> { return nullptr; };
  5667. for (const auto &It : AllocationInfos)
  5668. A.registerSimplificationCallback(IRPosition::callsite_returned(*It.first),
  5669. SCB);
  5670. for (const auto &It : DeallocationInfos)
  5671. A.registerSimplificationCallback(IRPosition::callsite_returned(*It.first),
  5672. SCB);
  5673. }
  5674. const std::string getAsStr() const override {
  5675. unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0;
  5676. for (const auto &It : AllocationInfos) {
  5677. if (It.second->Status == AllocationInfo::INVALID)
  5678. ++NumInvalidMallocs;
  5679. else
  5680. ++NumH2SMallocs;
  5681. }
  5682. return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" +
  5683. std::to_string(NumInvalidMallocs);
  5684. }
  5685. /// See AbstractAttribute::trackStatistics().
  5686. void trackStatistics() const override {
  5687. STATS_DECL(
  5688. MallocCalls, Function,
  5689. "Number of malloc/calloc/aligned_alloc calls converted to allocas");
  5690. for (const auto &It : AllocationInfos)
  5691. if (It.second->Status != AllocationInfo::INVALID)
  5692. ++BUILD_STAT_NAME(MallocCalls, Function);
  5693. }
  5694. bool isAssumedHeapToStack(const CallBase &CB) const override {
  5695. if (isValidState())
  5696. if (AllocationInfo *AI =
  5697. AllocationInfos.lookup(const_cast<CallBase *>(&CB)))
  5698. return AI->Status != AllocationInfo::INVALID;
  5699. return false;
  5700. }
  5701. bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override {
  5702. if (!isValidState())
  5703. return false;
  5704. for (const auto &It : AllocationInfos) {
  5705. AllocationInfo &AI = *It.second;
  5706. if (AI.Status == AllocationInfo::INVALID)
  5707. continue;
  5708. if (AI.PotentialFreeCalls.count(&CB))
  5709. return true;
  5710. }
  5711. return false;
  5712. }
  5713. ChangeStatus manifest(Attributor &A) override {
  5714. assert(getState().isValidState() &&
  5715. "Attempted to manifest an invalid state!");
  5716. ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
  5717. Function *F = getAnchorScope();
  5718. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5719. for (auto &It : AllocationInfos) {
  5720. AllocationInfo &AI = *It.second;
  5721. if (AI.Status == AllocationInfo::INVALID)
  5722. continue;
  5723. for (CallBase *FreeCall : AI.PotentialFreeCalls) {
  5724. LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
  5725. A.deleteAfterManifest(*FreeCall);
  5726. HasChanged = ChangeStatus::CHANGED;
  5727. }
  5728. LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CB
  5729. << "\n");
  5730. auto Remark = [&](OptimizationRemark OR) {
  5731. LibFunc IsAllocShared;
  5732. if (TLI->getLibFunc(*AI.CB, IsAllocShared))
  5733. if (IsAllocShared == LibFunc___kmpc_alloc_shared)
  5734. return OR << "Moving globalized variable to the stack.";
  5735. return OR << "Moving memory allocation from the heap to the stack.";
  5736. };
  5737. if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
  5738. A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark);
  5739. else
  5740. A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark);
  5741. const DataLayout &DL = A.getInfoCache().getDL();
  5742. Value *Size;
  5743. std::optional<APInt> SizeAPI = getSize(A, *this, AI);
  5744. if (SizeAPI) {
  5745. Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
  5746. } else {
  5747. LLVMContext &Ctx = AI.CB->getContext();
  5748. ObjectSizeOpts Opts;
  5749. ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, Opts);
  5750. SizeOffsetEvalType SizeOffsetPair = Eval.compute(AI.CB);
  5751. assert(SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() &&
  5752. cast<ConstantInt>(SizeOffsetPair.second)->isZero());
  5753. Size = SizeOffsetPair.first;
  5754. }
  5755. Instruction *IP =
  5756. AI.MoveAllocaIntoEntry ? &F->getEntryBlock().front() : AI.CB;
  5757. Align Alignment(1);
  5758. if (MaybeAlign RetAlign = AI.CB->getRetAlign())
  5759. Alignment = std::max(Alignment, *RetAlign);
  5760. if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
  5761. std::optional<APInt> AlignmentAPI = getAPInt(A, *this, *Align);
  5762. assert(AlignmentAPI && AlignmentAPI->getZExtValue() > 0 &&
  5763. "Expected an alignment during manifest!");
  5764. Alignment =
  5765. std::max(Alignment, assumeAligned(AlignmentAPI->getZExtValue()));
  5766. }
  5767. // TODO: Hoist the alloca towards the function entry.
  5768. unsigned AS = DL.getAllocaAddrSpace();
  5769. Instruction *Alloca =
  5770. new AllocaInst(Type::getInt8Ty(F->getContext()), AS, Size, Alignment,
  5771. AI.CB->getName() + ".h2s", IP);
  5772. if (Alloca->getType() != AI.CB->getType())
  5773. Alloca = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
  5774. Alloca, AI.CB->getType(), "malloc_cast", AI.CB);
  5775. auto *I8Ty = Type::getInt8Ty(F->getContext());
  5776. auto *InitVal = getInitialValueOfAllocation(AI.CB, TLI, I8Ty);
  5777. assert(InitVal &&
  5778. "Must be able to materialize initial memory state of allocation");
  5779. A.changeAfterManifest(IRPosition::inst(*AI.CB), *Alloca);
  5780. if (auto *II = dyn_cast<InvokeInst>(AI.CB)) {
  5781. auto *NBB = II->getNormalDest();
  5782. BranchInst::Create(NBB, AI.CB->getParent());
  5783. A.deleteAfterManifest(*AI.CB);
  5784. } else {
  5785. A.deleteAfterManifest(*AI.CB);
  5786. }
  5787. // Initialize the alloca with the same value as used by the allocation
  5788. // function. We can skip undef as the initial value of an alloc is
  5789. // undef, and the memset would simply end up being DSEd.
  5790. if (!isa<UndefValue>(InitVal)) {
  5791. IRBuilder<> Builder(Alloca->getNextNode());
  5792. // TODO: Use alignment above if align!=1
  5793. Builder.CreateMemSet(Alloca, InitVal, Size, std::nullopt);
  5794. }
  5795. HasChanged = ChangeStatus::CHANGED;
  5796. }
  5797. return HasChanged;
  5798. }
  5799. std::optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA,
  5800. Value &V) {
  5801. bool UsedAssumedInformation = false;
  5802. std::optional<Constant *> SimpleV =
  5803. A.getAssumedConstant(V, AA, UsedAssumedInformation);
  5804. if (!SimpleV)
  5805. return APInt(64, 0);
  5806. if (auto *CI = dyn_cast_or_null<ConstantInt>(*SimpleV))
  5807. return CI->getValue();
  5808. return std::nullopt;
  5809. }
  5810. std::optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
  5811. AllocationInfo &AI) {
  5812. auto Mapper = [&](const Value *V) -> const Value * {
  5813. bool UsedAssumedInformation = false;
  5814. if (std::optional<Constant *> SimpleV =
  5815. A.getAssumedConstant(*V, AA, UsedAssumedInformation))
  5816. if (*SimpleV)
  5817. return *SimpleV;
  5818. return V;
  5819. };
  5820. const Function *F = getAnchorScope();
  5821. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5822. return getAllocSize(AI.CB, TLI, Mapper);
  5823. }
  5824. /// Collection of all malloc-like calls in a function with associated
  5825. /// information.
  5826. MapVector<CallBase *, AllocationInfo *> AllocationInfos;
  5827. /// Collection of all free-like calls in a function with associated
  5828. /// information.
  5829. MapVector<CallBase *, DeallocationInfo *> DeallocationInfos;
  5830. ChangeStatus updateImpl(Attributor &A) override;
  5831. };
  5832. ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
  5833. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  5834. const Function *F = getAnchorScope();
  5835. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5836. const auto &LivenessAA =
  5837. A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE);
  5838. MustBeExecutedContextExplorer &Explorer =
  5839. A.getInfoCache().getMustBeExecutedContextExplorer();
  5840. bool StackIsAccessibleByOtherThreads =
  5841. A.getInfoCache().stackIsAccessibleByOtherThreads();
  5842. LoopInfo *LI =
  5843. A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(*F);
  5844. std::optional<bool> MayContainIrreducibleControl;
  5845. auto IsInLoop = [&](BasicBlock &BB) {
  5846. if (&F->getEntryBlock() == &BB)
  5847. return false;
  5848. if (!MayContainIrreducibleControl.has_value())
  5849. MayContainIrreducibleControl = mayContainIrreducibleControl(*F, LI);
  5850. if (*MayContainIrreducibleControl)
  5851. return true;
  5852. if (!LI)
  5853. return true;
  5854. return LI->getLoopFor(&BB) != nullptr;
  5855. };
  5856. // Flag to ensure we update our deallocation information at most once per
  5857. // updateImpl call and only if we use the free check reasoning.
  5858. bool HasUpdatedFrees = false;
  5859. auto UpdateFrees = [&]() {
  5860. HasUpdatedFrees = true;
  5861. for (auto &It : DeallocationInfos) {
  5862. DeallocationInfo &DI = *It.second;
  5863. // For now we cannot use deallocations that have unknown inputs, skip
  5864. // them.
  5865. if (DI.MightFreeUnknownObjects)
  5866. continue;
  5867. // No need to analyze dead calls, ignore them instead.
  5868. bool UsedAssumedInformation = false;
  5869. if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation,
  5870. /* CheckBBLivenessOnly */ true))
  5871. continue;
  5872. // Use the non-optimistic version to get the freed object.
  5873. Value *Obj = getUnderlyingObject(DI.FreedOp);
  5874. if (!Obj) {
  5875. LLVM_DEBUG(dbgs() << "[H2S] Unknown underlying object for free!\n");
  5876. DI.MightFreeUnknownObjects = true;
  5877. continue;
  5878. }
  5879. // Free of null and undef can be ignored as no-ops (or UB in the latter
  5880. // case).
  5881. if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj))
  5882. continue;
  5883. CallBase *ObjCB = dyn_cast<CallBase>(Obj);
  5884. if (!ObjCB) {
  5885. LLVM_DEBUG(dbgs() << "[H2S] Free of a non-call object: " << *Obj
  5886. << "\n");
  5887. DI.MightFreeUnknownObjects = true;
  5888. continue;
  5889. }
  5890. AllocationInfo *AI = AllocationInfos.lookup(ObjCB);
  5891. if (!AI) {
  5892. LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Obj
  5893. << "\n");
  5894. DI.MightFreeUnknownObjects = true;
  5895. continue;
  5896. }
  5897. DI.PotentialAllocationCalls.insert(ObjCB);
  5898. }
  5899. };
  5900. auto FreeCheck = [&](AllocationInfo &AI) {
  5901. // If the stack is not accessible by other threads, the "must-free" logic
  5902. // doesn't apply as the pointer could be shared and needs to be places in
  5903. // "shareable" memory.
  5904. if (!StackIsAccessibleByOtherThreads) {
  5905. auto &NoSyncAA =
  5906. A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL);
  5907. if (!NoSyncAA.isAssumedNoSync()) {
  5908. LLVM_DEBUG(
  5909. dbgs() << "[H2S] found an escaping use, stack is not accessible by "
  5910. "other threads and function is not nosync:\n");
  5911. return false;
  5912. }
  5913. }
  5914. if (!HasUpdatedFrees)
  5915. UpdateFrees();
  5916. // TODO: Allow multi exit functions that have different free calls.
  5917. if (AI.PotentialFreeCalls.size() != 1) {
  5918. LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "
  5919. << AI.PotentialFreeCalls.size() << "\n");
  5920. return false;
  5921. }
  5922. CallBase *UniqueFree = *AI.PotentialFreeCalls.begin();
  5923. DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree);
  5924. if (!DI) {
  5925. LLVM_DEBUG(
  5926. dbgs() << "[H2S] unique free call was not known as deallocation call "
  5927. << *UniqueFree << "\n");
  5928. return false;
  5929. }
  5930. if (DI->MightFreeUnknownObjects) {
  5931. LLVM_DEBUG(
  5932. dbgs() << "[H2S] unique free call might free unknown allocations\n");
  5933. return false;
  5934. }
  5935. if (DI->PotentialAllocationCalls.empty())
  5936. return true;
  5937. if (DI->PotentialAllocationCalls.size() > 1) {
  5938. LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "
  5939. << DI->PotentialAllocationCalls.size()
  5940. << " different allocations\n");
  5941. return false;
  5942. }
  5943. if (*DI->PotentialAllocationCalls.begin() != AI.CB) {
  5944. LLVM_DEBUG(
  5945. dbgs()
  5946. << "[H2S] unique free call not known to free this allocation but "
  5947. << **DI->PotentialAllocationCalls.begin() << "\n");
  5948. return false;
  5949. }
  5950. Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode();
  5951. if (!Explorer.findInContextOf(UniqueFree, CtxI)) {
  5952. LLVM_DEBUG(
  5953. dbgs()
  5954. << "[H2S] unique free call might not be executed with the allocation "
  5955. << *UniqueFree << "\n");
  5956. return false;
  5957. }
  5958. return true;
  5959. };
  5960. auto UsesCheck = [&](AllocationInfo &AI) {
  5961. bool ValidUsesOnly = true;
  5962. auto Pred = [&](const Use &U, bool &Follow) -> bool {
  5963. Instruction *UserI = cast<Instruction>(U.getUser());
  5964. if (isa<LoadInst>(UserI))
  5965. return true;
  5966. if (auto *SI = dyn_cast<StoreInst>(UserI)) {
  5967. if (SI->getValueOperand() == U.get()) {
  5968. LLVM_DEBUG(dbgs()
  5969. << "[H2S] escaping store to memory: " << *UserI << "\n");
  5970. ValidUsesOnly = false;
  5971. } else {
  5972. // A store into the malloc'ed memory is fine.
  5973. }
  5974. return true;
  5975. }
  5976. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  5977. if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
  5978. return true;
  5979. if (DeallocationInfos.count(CB)) {
  5980. AI.PotentialFreeCalls.insert(CB);
  5981. return true;
  5982. }
  5983. unsigned ArgNo = CB->getArgOperandNo(&U);
  5984. const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
  5985. *this, IRPosition::callsite_argument(*CB, ArgNo),
  5986. DepClassTy::OPTIONAL);
  5987. // If a call site argument use is nofree, we are fine.
  5988. const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
  5989. *this, IRPosition::callsite_argument(*CB, ArgNo),
  5990. DepClassTy::OPTIONAL);
  5991. bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture();
  5992. bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree();
  5993. if (MaybeCaptured ||
  5994. (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared &&
  5995. MaybeFreed)) {
  5996. AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed;
  5997. // Emit a missed remark if this is missed OpenMP globalization.
  5998. auto Remark = [&](OptimizationRemarkMissed ORM) {
  5999. return ORM
  6000. << "Could not move globalized variable to the stack. "
  6001. "Variable is potentially captured in call. Mark "
  6002. "parameter as `__attribute__((noescape))` to override.";
  6003. };
  6004. if (ValidUsesOnly &&
  6005. AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
  6006. A.emitRemark<OptimizationRemarkMissed>(CB, "OMP113", Remark);
  6007. LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
  6008. ValidUsesOnly = false;
  6009. }
  6010. return true;
  6011. }
  6012. if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
  6013. isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
  6014. Follow = true;
  6015. return true;
  6016. }
  6017. // Unknown user for which we can not track uses further (in a way that
  6018. // makes sense).
  6019. LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
  6020. ValidUsesOnly = false;
  6021. return true;
  6022. };
  6023. if (!A.checkForAllUses(Pred, *this, *AI.CB))
  6024. return false;
  6025. return ValidUsesOnly;
  6026. };
  6027. // The actual update starts here. We look at all allocations and depending on
  6028. // their status perform the appropriate check(s).
  6029. for (auto &It : AllocationInfos) {
  6030. AllocationInfo &AI = *It.second;
  6031. if (AI.Status == AllocationInfo::INVALID)
  6032. continue;
  6033. if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
  6034. std::optional<APInt> APAlign = getAPInt(A, *this, *Align);
  6035. if (!APAlign) {
  6036. // Can't generate an alloca which respects the required alignment
  6037. // on the allocation.
  6038. LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB
  6039. << "\n");
  6040. AI.Status = AllocationInfo::INVALID;
  6041. Changed = ChangeStatus::CHANGED;
  6042. continue;
  6043. }
  6044. if (APAlign->ugt(llvm::Value::MaximumAlignment) ||
  6045. !APAlign->isPowerOf2()) {
  6046. LLVM_DEBUG(dbgs() << "[H2S] Invalid allocation alignment: " << APAlign
  6047. << "\n");
  6048. AI.Status = AllocationInfo::INVALID;
  6049. Changed = ChangeStatus::CHANGED;
  6050. continue;
  6051. }
  6052. }
  6053. std::optional<APInt> Size = getSize(A, *this, AI);
  6054. if (MaxHeapToStackSize != -1) {
  6055. if (!Size || Size->ugt(MaxHeapToStackSize)) {
  6056. LLVM_DEBUG({
  6057. if (!Size)
  6058. dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n";
  6059. else
  6060. dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "
  6061. << MaxHeapToStackSize << "\n";
  6062. });
  6063. AI.Status = AllocationInfo::INVALID;
  6064. Changed = ChangeStatus::CHANGED;
  6065. continue;
  6066. }
  6067. }
  6068. switch (AI.Status) {
  6069. case AllocationInfo::STACK_DUE_TO_USE:
  6070. if (UsesCheck(AI))
  6071. break;
  6072. AI.Status = AllocationInfo::STACK_DUE_TO_FREE;
  6073. [[fallthrough]];
  6074. case AllocationInfo::STACK_DUE_TO_FREE:
  6075. if (FreeCheck(AI))
  6076. break;
  6077. AI.Status = AllocationInfo::INVALID;
  6078. Changed = ChangeStatus::CHANGED;
  6079. break;
  6080. case AllocationInfo::INVALID:
  6081. llvm_unreachable("Invalid allocations should never reach this point!");
  6082. };
  6083. // Check if we still think we can move it into the entry block. If the
  6084. // alloca comes from a converted __kmpc_alloc_shared then we can usually
  6085. // ignore the potential compilations associated with loops.
  6086. bool IsGlobalizedLocal =
  6087. AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared;
  6088. if (AI.MoveAllocaIntoEntry &&
  6089. (!Size.has_value() ||
  6090. (!IsGlobalizedLocal && IsInLoop(*AI.CB->getParent()))))
  6091. AI.MoveAllocaIntoEntry = false;
  6092. }
  6093. return Changed;
  6094. }
  6095. } // namespace
  6096. /// ----------------------- Privatizable Pointers ------------------------------
  6097. namespace {
  6098. struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
  6099. AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
  6100. : AAPrivatizablePtr(IRP, A), PrivatizableType(std::nullopt) {}
  6101. ChangeStatus indicatePessimisticFixpoint() override {
  6102. AAPrivatizablePtr::indicatePessimisticFixpoint();
  6103. PrivatizableType = nullptr;
  6104. return ChangeStatus::CHANGED;
  6105. }
  6106. /// Identify the type we can chose for a private copy of the underlying
  6107. /// argument. None means it is not clear yet, nullptr means there is none.
  6108. virtual std::optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
  6109. /// Return a privatizable type that encloses both T0 and T1.
  6110. /// TODO: This is merely a stub for now as we should manage a mapping as well.
  6111. std::optional<Type *> combineTypes(std::optional<Type *> T0,
  6112. std::optional<Type *> T1) {
  6113. if (!T0)
  6114. return T1;
  6115. if (!T1)
  6116. return T0;
  6117. if (T0 == T1)
  6118. return T0;
  6119. return nullptr;
  6120. }
  6121. std::optional<Type *> getPrivatizableType() const override {
  6122. return PrivatizableType;
  6123. }
  6124. const std::string getAsStr() const override {
  6125. return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
  6126. }
  6127. protected:
  6128. std::optional<Type *> PrivatizableType;
  6129. };
  6130. // TODO: Do this for call site arguments (probably also other values) as well.
  6131. struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
  6132. AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
  6133. : AAPrivatizablePtrImpl(IRP, A) {}
  6134. /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
  6135. std::optional<Type *> identifyPrivatizableType(Attributor &A) override {
  6136. // If this is a byval argument and we know all the call sites (so we can
  6137. // rewrite them), there is no need to check them explicitly.
  6138. bool UsedAssumedInformation = false;
  6139. SmallVector<Attribute, 1> Attrs;
  6140. getAttrs({Attribute::ByVal}, Attrs, /* IgnoreSubsumingPositions */ true);
  6141. if (!Attrs.empty() &&
  6142. A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
  6143. true, UsedAssumedInformation))
  6144. return Attrs[0].getValueAsType();
  6145. std::optional<Type *> Ty;
  6146. unsigned ArgNo = getIRPosition().getCallSiteArgNo();
  6147. // Make sure the associated call site argument has the same type at all call
  6148. // sites and it is an allocation we know is safe to privatize, for now that
  6149. // means we only allow alloca instructions.
  6150. // TODO: We can additionally analyze the accesses in the callee to create
  6151. // the type from that information instead. That is a little more
  6152. // involved and will be done in a follow up patch.
  6153. auto CallSiteCheck = [&](AbstractCallSite ACS) {
  6154. IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
  6155. // Check if a coresponding argument was found or if it is one not
  6156. // associated (which can happen for callback calls).
  6157. if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
  6158. return false;
  6159. // Check that all call sites agree on a type.
  6160. auto &PrivCSArgAA =
  6161. A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
  6162. std::optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
  6163. LLVM_DEBUG({
  6164. dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
  6165. if (CSTy && *CSTy)
  6166. (*CSTy)->print(dbgs());
  6167. else if (CSTy)
  6168. dbgs() << "<nullptr>";
  6169. else
  6170. dbgs() << "<none>";
  6171. });
  6172. Ty = combineTypes(Ty, CSTy);
  6173. LLVM_DEBUG({
  6174. dbgs() << " : New Type: ";
  6175. if (Ty && *Ty)
  6176. (*Ty)->print(dbgs());
  6177. else if (Ty)
  6178. dbgs() << "<nullptr>";
  6179. else
  6180. dbgs() << "<none>";
  6181. dbgs() << "\n";
  6182. });
  6183. return !Ty || *Ty;
  6184. };
  6185. if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
  6186. UsedAssumedInformation))
  6187. return nullptr;
  6188. return Ty;
  6189. }
  6190. /// See AbstractAttribute::updateImpl(...).
  6191. ChangeStatus updateImpl(Attributor &A) override {
  6192. PrivatizableType = identifyPrivatizableType(A);
  6193. if (!PrivatizableType)
  6194. return ChangeStatus::UNCHANGED;
  6195. if (!*PrivatizableType)
  6196. return indicatePessimisticFixpoint();
  6197. // The dependence is optional so we don't give up once we give up on the
  6198. // alignment.
  6199. A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
  6200. DepClassTy::OPTIONAL);
  6201. // Avoid arguments with padding for now.
  6202. if (!getIRPosition().hasAttr(Attribute::ByVal) &&
  6203. !isDenselyPacked(*PrivatizableType, A.getInfoCache().getDL())) {
  6204. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
  6205. return indicatePessimisticFixpoint();
  6206. }
  6207. // Collect the types that will replace the privatizable type in the function
  6208. // signature.
  6209. SmallVector<Type *, 16> ReplacementTypes;
  6210. identifyReplacementTypes(*PrivatizableType, ReplacementTypes);
  6211. // Verify callee and caller agree on how the promoted argument would be
  6212. // passed.
  6213. Function &Fn = *getIRPosition().getAnchorScope();
  6214. const auto *TTI =
  6215. A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
  6216. if (!TTI) {
  6217. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Missing TTI for function "
  6218. << Fn.getName() << "\n");
  6219. return indicatePessimisticFixpoint();
  6220. }
  6221. auto CallSiteCheck = [&](AbstractCallSite ACS) {
  6222. CallBase *CB = ACS.getInstruction();
  6223. return TTI->areTypesABICompatible(
  6224. CB->getCaller(), CB->getCalledFunction(), ReplacementTypes);
  6225. };
  6226. bool UsedAssumedInformation = false;
  6227. if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
  6228. UsedAssumedInformation)) {
  6229. LLVM_DEBUG(
  6230. dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
  6231. << Fn.getName() << "\n");
  6232. return indicatePessimisticFixpoint();
  6233. }
  6234. // Register a rewrite of the argument.
  6235. Argument *Arg = getAssociatedArgument();
  6236. if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
  6237. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
  6238. return indicatePessimisticFixpoint();
  6239. }
  6240. unsigned ArgNo = Arg->getArgNo();
  6241. // Helper to check if for the given call site the associated argument is
  6242. // passed to a callback where the privatization would be different.
  6243. auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
  6244. SmallVector<const Use *, 4> CallbackUses;
  6245. AbstractCallSite::getCallbackUses(CB, CallbackUses);
  6246. for (const Use *U : CallbackUses) {
  6247. AbstractCallSite CBACS(U);
  6248. assert(CBACS && CBACS.isCallbackCall());
  6249. for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
  6250. int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
  6251. LLVM_DEBUG({
  6252. dbgs()
  6253. << "[AAPrivatizablePtr] Argument " << *Arg
  6254. << "check if can be privatized in the context of its parent ("
  6255. << Arg->getParent()->getName()
  6256. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  6257. "callback ("
  6258. << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
  6259. << ")\n[AAPrivatizablePtr] " << CBArg << " : "
  6260. << CBACS.getCallArgOperand(CBArg) << " vs "
  6261. << CB.getArgOperand(ArgNo) << "\n"
  6262. << "[AAPrivatizablePtr] " << CBArg << " : "
  6263. << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
  6264. });
  6265. if (CBArgNo != int(ArgNo))
  6266. continue;
  6267. const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
  6268. *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
  6269. if (CBArgPrivAA.isValidState()) {
  6270. auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
  6271. if (!CBArgPrivTy)
  6272. continue;
  6273. if (*CBArgPrivTy == PrivatizableType)
  6274. continue;
  6275. }
  6276. LLVM_DEBUG({
  6277. dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
  6278. << " cannot be privatized in the context of its parent ("
  6279. << Arg->getParent()->getName()
  6280. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  6281. "callback ("
  6282. << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
  6283. << ").\n[AAPrivatizablePtr] for which the argument "
  6284. "privatization is not compatible.\n";
  6285. });
  6286. return false;
  6287. }
  6288. }
  6289. return true;
  6290. };
  6291. // Helper to check if for the given call site the associated argument is
  6292. // passed to a direct call where the privatization would be different.
  6293. auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
  6294. CallBase *DC = cast<CallBase>(ACS.getInstruction());
  6295. int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
  6296. assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() &&
  6297. "Expected a direct call operand for callback call operand");
  6298. LLVM_DEBUG({
  6299. dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
  6300. << " check if be privatized in the context of its parent ("
  6301. << Arg->getParent()->getName()
  6302. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  6303. "direct call of ("
  6304. << DCArgNo << "@" << DC->getCalledFunction()->getName()
  6305. << ").\n";
  6306. });
  6307. Function *DCCallee = DC->getCalledFunction();
  6308. if (unsigned(DCArgNo) < DCCallee->arg_size()) {
  6309. const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
  6310. *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
  6311. DepClassTy::REQUIRED);
  6312. if (DCArgPrivAA.isValidState()) {
  6313. auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
  6314. if (!DCArgPrivTy)
  6315. return true;
  6316. if (*DCArgPrivTy == PrivatizableType)
  6317. return true;
  6318. }
  6319. }
  6320. LLVM_DEBUG({
  6321. dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
  6322. << " cannot be privatized in the context of its parent ("
  6323. << Arg->getParent()->getName()
  6324. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  6325. "direct call of ("
  6326. << ACS.getInstruction()->getCalledFunction()->getName()
  6327. << ").\n[AAPrivatizablePtr] for which the argument "
  6328. "privatization is not compatible.\n";
  6329. });
  6330. return false;
  6331. };
  6332. // Helper to check if the associated argument is used at the given abstract
  6333. // call site in a way that is incompatible with the privatization assumed
  6334. // here.
  6335. auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
  6336. if (ACS.isDirectCall())
  6337. return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
  6338. if (ACS.isCallbackCall())
  6339. return IsCompatiblePrivArgOfDirectCS(ACS);
  6340. return false;
  6341. };
  6342. if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
  6343. UsedAssumedInformation))
  6344. return indicatePessimisticFixpoint();
  6345. return ChangeStatus::UNCHANGED;
  6346. }
  6347. /// Given a type to private \p PrivType, collect the constituates (which are
  6348. /// used) in \p ReplacementTypes.
  6349. static void
  6350. identifyReplacementTypes(Type *PrivType,
  6351. SmallVectorImpl<Type *> &ReplacementTypes) {
  6352. // TODO: For now we expand the privatization type to the fullest which can
  6353. // lead to dead arguments that need to be removed later.
  6354. assert(PrivType && "Expected privatizable type!");
  6355. // Traverse the type, extract constituate types on the outermost level.
  6356. if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
  6357. for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
  6358. ReplacementTypes.push_back(PrivStructType->getElementType(u));
  6359. } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
  6360. ReplacementTypes.append(PrivArrayType->getNumElements(),
  6361. PrivArrayType->getElementType());
  6362. } else {
  6363. ReplacementTypes.push_back(PrivType);
  6364. }
  6365. }
  6366. /// Initialize \p Base according to the type \p PrivType at position \p IP.
  6367. /// The values needed are taken from the arguments of \p F starting at
  6368. /// position \p ArgNo.
  6369. static void createInitialization(Type *PrivType, Value &Base, Function &F,
  6370. unsigned ArgNo, Instruction &IP) {
  6371. assert(PrivType && "Expected privatizable type!");
  6372. IRBuilder<NoFolder> IRB(&IP);
  6373. const DataLayout &DL = F.getParent()->getDataLayout();
  6374. // Traverse the type, build GEPs and stores.
  6375. if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
  6376. const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
  6377. for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
  6378. Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
  6379. Value *Ptr =
  6380. constructPointer(PointeeTy, PrivType, &Base,
  6381. PrivStructLayout->getElementOffset(u), IRB, DL);
  6382. new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
  6383. }
  6384. } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
  6385. Type *PointeeTy = PrivArrayType->getElementType();
  6386. Type *PointeePtrTy = PointeeTy->getPointerTo();
  6387. uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
  6388. for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
  6389. Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
  6390. u * PointeeTySize, IRB, DL);
  6391. new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
  6392. }
  6393. } else {
  6394. new StoreInst(F.getArg(ArgNo), &Base, &IP);
  6395. }
  6396. }
  6397. /// Extract values from \p Base according to the type \p PrivType at the
  6398. /// call position \p ACS. The values are appended to \p ReplacementValues.
  6399. void createReplacementValues(Align Alignment, Type *PrivType,
  6400. AbstractCallSite ACS, Value *Base,
  6401. SmallVectorImpl<Value *> &ReplacementValues) {
  6402. assert(Base && "Expected base value!");
  6403. assert(PrivType && "Expected privatizable type!");
  6404. Instruction *IP = ACS.getInstruction();
  6405. IRBuilder<NoFolder> IRB(IP);
  6406. const DataLayout &DL = IP->getModule()->getDataLayout();
  6407. Type *PrivPtrType = PrivType->getPointerTo();
  6408. if (Base->getType() != PrivPtrType)
  6409. Base = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
  6410. Base, PrivPtrType, "", ACS.getInstruction());
  6411. // Traverse the type, build GEPs and loads.
  6412. if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
  6413. const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
  6414. for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
  6415. Type *PointeeTy = PrivStructType->getElementType(u);
  6416. Value *Ptr =
  6417. constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
  6418. PrivStructLayout->getElementOffset(u), IRB, DL);
  6419. LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
  6420. L->setAlignment(Alignment);
  6421. ReplacementValues.push_back(L);
  6422. }
  6423. } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
  6424. Type *PointeeTy = PrivArrayType->getElementType();
  6425. uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
  6426. Type *PointeePtrTy = PointeeTy->getPointerTo();
  6427. for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
  6428. Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
  6429. u * PointeeTySize, IRB, DL);
  6430. LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
  6431. L->setAlignment(Alignment);
  6432. ReplacementValues.push_back(L);
  6433. }
  6434. } else {
  6435. LoadInst *L = new LoadInst(PrivType, Base, "", IP);
  6436. L->setAlignment(Alignment);
  6437. ReplacementValues.push_back(L);
  6438. }
  6439. }
  6440. /// See AbstractAttribute::manifest(...)
  6441. ChangeStatus manifest(Attributor &A) override {
  6442. if (!PrivatizableType)
  6443. return ChangeStatus::UNCHANGED;
  6444. assert(*PrivatizableType && "Expected privatizable type!");
  6445. // Collect all tail calls in the function as we cannot allow new allocas to
  6446. // escape into tail recursion.
  6447. // TODO: Be smarter about new allocas escaping into tail calls.
  6448. SmallVector<CallInst *, 16> TailCalls;
  6449. bool UsedAssumedInformation = false;
  6450. if (!A.checkForAllInstructions(
  6451. [&](Instruction &I) {
  6452. CallInst &CI = cast<CallInst>(I);
  6453. if (CI.isTailCall())
  6454. TailCalls.push_back(&CI);
  6455. return true;
  6456. },
  6457. *this, {Instruction::Call}, UsedAssumedInformation))
  6458. return ChangeStatus::UNCHANGED;
  6459. Argument *Arg = getAssociatedArgument();
  6460. // Query AAAlign attribute for alignment of associated argument to
  6461. // determine the best alignment of loads.
  6462. const auto &AlignAA =
  6463. A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
  6464. // Callback to repair the associated function. A new alloca is placed at the
  6465. // beginning and initialized with the values passed through arguments. The
  6466. // new alloca replaces the use of the old pointer argument.
  6467. Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
  6468. [=](const Attributor::ArgumentReplacementInfo &ARI,
  6469. Function &ReplacementFn, Function::arg_iterator ArgIt) {
  6470. BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
  6471. Instruction *IP = &*EntryBB.getFirstInsertionPt();
  6472. const DataLayout &DL = IP->getModule()->getDataLayout();
  6473. unsigned AS = DL.getAllocaAddrSpace();
  6474. Instruction *AI = new AllocaInst(*PrivatizableType, AS,
  6475. Arg->getName() + ".priv", IP);
  6476. createInitialization(*PrivatizableType, *AI, ReplacementFn,
  6477. ArgIt->getArgNo(), *IP);
  6478. if (AI->getType() != Arg->getType())
  6479. AI = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
  6480. AI, Arg->getType(), "", IP);
  6481. Arg->replaceAllUsesWith(AI);
  6482. for (CallInst *CI : TailCalls)
  6483. CI->setTailCall(false);
  6484. };
  6485. // Callback to repair a call site of the associated function. The elements
  6486. // of the privatizable type are loaded prior to the call and passed to the
  6487. // new function version.
  6488. Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
  6489. [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
  6490. AbstractCallSite ACS,
  6491. SmallVectorImpl<Value *> &NewArgOperands) {
  6492. // When no alignment is specified for the load instruction,
  6493. // natural alignment is assumed.
  6494. createReplacementValues(
  6495. AlignAA.getAssumedAlign(), *PrivatizableType, ACS,
  6496. ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
  6497. NewArgOperands);
  6498. };
  6499. // Collect the types that will replace the privatizable type in the function
  6500. // signature.
  6501. SmallVector<Type *, 16> ReplacementTypes;
  6502. identifyReplacementTypes(*PrivatizableType, ReplacementTypes);
  6503. // Register a rewrite of the argument.
  6504. if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
  6505. std::move(FnRepairCB),
  6506. std::move(ACSRepairCB)))
  6507. return ChangeStatus::CHANGED;
  6508. return ChangeStatus::UNCHANGED;
  6509. }
  6510. /// See AbstractAttribute::trackStatistics()
  6511. void trackStatistics() const override {
  6512. STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
  6513. }
  6514. };
  6515. struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
  6516. AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
  6517. : AAPrivatizablePtrImpl(IRP, A) {}
  6518. /// See AbstractAttribute::initialize(...).
  6519. void initialize(Attributor &A) override {
  6520. // TODO: We can privatize more than arguments.
  6521. indicatePessimisticFixpoint();
  6522. }
  6523. ChangeStatus updateImpl(Attributor &A) override {
  6524. llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
  6525. "updateImpl will not be called");
  6526. }
  6527. /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
  6528. std::optional<Type *> identifyPrivatizableType(Attributor &A) override {
  6529. Value *Obj = getUnderlyingObject(&getAssociatedValue());
  6530. if (!Obj) {
  6531. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
  6532. return nullptr;
  6533. }
  6534. if (auto *AI = dyn_cast<AllocaInst>(Obj))
  6535. if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
  6536. if (CI->isOne())
  6537. return AI->getAllocatedType();
  6538. if (auto *Arg = dyn_cast<Argument>(Obj)) {
  6539. auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
  6540. *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
  6541. if (PrivArgAA.isAssumedPrivatizablePtr())
  6542. return PrivArgAA.getPrivatizableType();
  6543. }
  6544. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
  6545. "alloca nor privatizable argument: "
  6546. << *Obj << "!\n");
  6547. return nullptr;
  6548. }
  6549. /// See AbstractAttribute::trackStatistics()
  6550. void trackStatistics() const override {
  6551. STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
  6552. }
  6553. };
  6554. struct AAPrivatizablePtrCallSiteArgument final
  6555. : public AAPrivatizablePtrFloating {
  6556. AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
  6557. : AAPrivatizablePtrFloating(IRP, A) {}
  6558. /// See AbstractAttribute::initialize(...).
  6559. void initialize(Attributor &A) override {
  6560. if (getIRPosition().hasAttr(Attribute::ByVal))
  6561. indicateOptimisticFixpoint();
  6562. }
  6563. /// See AbstractAttribute::updateImpl(...).
  6564. ChangeStatus updateImpl(Attributor &A) override {
  6565. PrivatizableType = identifyPrivatizableType(A);
  6566. if (!PrivatizableType)
  6567. return ChangeStatus::UNCHANGED;
  6568. if (!*PrivatizableType)
  6569. return indicatePessimisticFixpoint();
  6570. const IRPosition &IRP = getIRPosition();
  6571. auto &NoCaptureAA =
  6572. A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
  6573. if (!NoCaptureAA.isAssumedNoCapture()) {
  6574. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
  6575. return indicatePessimisticFixpoint();
  6576. }
  6577. auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
  6578. if (!NoAliasAA.isAssumedNoAlias()) {
  6579. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
  6580. return indicatePessimisticFixpoint();
  6581. }
  6582. bool IsKnown;
  6583. if (!AA::isAssumedReadOnly(A, IRP, *this, IsKnown)) {
  6584. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
  6585. return indicatePessimisticFixpoint();
  6586. }
  6587. return ChangeStatus::UNCHANGED;
  6588. }
  6589. /// See AbstractAttribute::trackStatistics()
  6590. void trackStatistics() const override {
  6591. STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
  6592. }
  6593. };
  6594. struct AAPrivatizablePtrCallSiteReturned final
  6595. : public AAPrivatizablePtrFloating {
  6596. AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
  6597. : AAPrivatizablePtrFloating(IRP, A) {}
  6598. /// See AbstractAttribute::initialize(...).
  6599. void initialize(Attributor &A) override {
  6600. // TODO: We can privatize more than arguments.
  6601. indicatePessimisticFixpoint();
  6602. }
  6603. /// See AbstractAttribute::trackStatistics()
  6604. void trackStatistics() const override {
  6605. STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
  6606. }
  6607. };
  6608. struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
  6609. AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
  6610. : AAPrivatizablePtrFloating(IRP, A) {}
  6611. /// See AbstractAttribute::initialize(...).
  6612. void initialize(Attributor &A) override {
  6613. // TODO: We can privatize more than arguments.
  6614. indicatePessimisticFixpoint();
  6615. }
  6616. /// See AbstractAttribute::trackStatistics()
  6617. void trackStatistics() const override {
  6618. STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
  6619. }
  6620. };
  6621. } // namespace
  6622. /// -------------------- Memory Behavior Attributes ----------------------------
  6623. /// Includes read-none, read-only, and write-only.
  6624. /// ----------------------------------------------------------------------------
  6625. namespace {
  6626. struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
  6627. AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
  6628. : AAMemoryBehavior(IRP, A) {}
  6629. /// See AbstractAttribute::initialize(...).
  6630. void initialize(Attributor &A) override {
  6631. intersectAssumedBits(BEST_STATE);
  6632. getKnownStateFromValue(getIRPosition(), getState());
  6633. AAMemoryBehavior::initialize(A);
  6634. }
  6635. /// Return the memory behavior information encoded in the IR for \p IRP.
  6636. static void getKnownStateFromValue(const IRPosition &IRP,
  6637. BitIntegerState &State,
  6638. bool IgnoreSubsumingPositions = false) {
  6639. SmallVector<Attribute, 2> Attrs;
  6640. IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
  6641. for (const Attribute &Attr : Attrs) {
  6642. switch (Attr.getKindAsEnum()) {
  6643. case Attribute::ReadNone:
  6644. State.addKnownBits(NO_ACCESSES);
  6645. break;
  6646. case Attribute::ReadOnly:
  6647. State.addKnownBits(NO_WRITES);
  6648. break;
  6649. case Attribute::WriteOnly:
  6650. State.addKnownBits(NO_READS);
  6651. break;
  6652. default:
  6653. llvm_unreachable("Unexpected attribute!");
  6654. }
  6655. }
  6656. if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
  6657. if (!I->mayReadFromMemory())
  6658. State.addKnownBits(NO_READS);
  6659. if (!I->mayWriteToMemory())
  6660. State.addKnownBits(NO_WRITES);
  6661. }
  6662. }
  6663. /// See AbstractAttribute::getDeducedAttributes(...).
  6664. void getDeducedAttributes(LLVMContext &Ctx,
  6665. SmallVectorImpl<Attribute> &Attrs) const override {
  6666. assert(Attrs.size() == 0);
  6667. if (isAssumedReadNone())
  6668. Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
  6669. else if (isAssumedReadOnly())
  6670. Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
  6671. else if (isAssumedWriteOnly())
  6672. Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
  6673. assert(Attrs.size() <= 1);
  6674. }
  6675. /// See AbstractAttribute::manifest(...).
  6676. ChangeStatus manifest(Attributor &A) override {
  6677. if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
  6678. return ChangeStatus::UNCHANGED;
  6679. const IRPosition &IRP = getIRPosition();
  6680. // Check if we would improve the existing attributes first.
  6681. SmallVector<Attribute, 4> DeducedAttrs;
  6682. getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
  6683. if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
  6684. return IRP.hasAttr(Attr.getKindAsEnum(),
  6685. /* IgnoreSubsumingPositions */ true);
  6686. }))
  6687. return ChangeStatus::UNCHANGED;
  6688. // Clear existing attributes.
  6689. IRP.removeAttrs(AttrKinds);
  6690. // Use the generic manifest method.
  6691. return IRAttribute::manifest(A);
  6692. }
  6693. /// See AbstractState::getAsStr().
  6694. const std::string getAsStr() const override {
  6695. if (isAssumedReadNone())
  6696. return "readnone";
  6697. if (isAssumedReadOnly())
  6698. return "readonly";
  6699. if (isAssumedWriteOnly())
  6700. return "writeonly";
  6701. return "may-read/write";
  6702. }
  6703. /// The set of IR attributes AAMemoryBehavior deals with.
  6704. static const Attribute::AttrKind AttrKinds[3];
  6705. };
  6706. const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
  6707. Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
  6708. /// Memory behavior attribute for a floating value.
  6709. struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
  6710. AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
  6711. : AAMemoryBehaviorImpl(IRP, A) {}
  6712. /// See AbstractAttribute::updateImpl(...).
  6713. ChangeStatus updateImpl(Attributor &A) override;
  6714. /// See AbstractAttribute::trackStatistics()
  6715. void trackStatistics() const override {
  6716. if (isAssumedReadNone())
  6717. STATS_DECLTRACK_FLOATING_ATTR(readnone)
  6718. else if (isAssumedReadOnly())
  6719. STATS_DECLTRACK_FLOATING_ATTR(readonly)
  6720. else if (isAssumedWriteOnly())
  6721. STATS_DECLTRACK_FLOATING_ATTR(writeonly)
  6722. }
  6723. private:
  6724. /// Return true if users of \p UserI might access the underlying
  6725. /// variable/location described by \p U and should therefore be analyzed.
  6726. bool followUsersOfUseIn(Attributor &A, const Use &U,
  6727. const Instruction *UserI);
  6728. /// Update the state according to the effect of use \p U in \p UserI.
  6729. void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI);
  6730. };
  6731. /// Memory behavior attribute for function argument.
  6732. struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
  6733. AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
  6734. : AAMemoryBehaviorFloating(IRP, A) {}
  6735. /// See AbstractAttribute::initialize(...).
  6736. void initialize(Attributor &A) override {
  6737. intersectAssumedBits(BEST_STATE);
  6738. const IRPosition &IRP = getIRPosition();
  6739. // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
  6740. // can query it when we use has/getAttr. That would allow us to reuse the
  6741. // initialize of the base class here.
  6742. bool HasByVal =
  6743. IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
  6744. getKnownStateFromValue(IRP, getState(),
  6745. /* IgnoreSubsumingPositions */ HasByVal);
  6746. // Initialize the use vector with all direct uses of the associated value.
  6747. Argument *Arg = getAssociatedArgument();
  6748. if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent())))
  6749. indicatePessimisticFixpoint();
  6750. }
  6751. ChangeStatus manifest(Attributor &A) override {
  6752. // TODO: Pointer arguments are not supported on vectors of pointers yet.
  6753. if (!getAssociatedValue().getType()->isPointerTy())
  6754. return ChangeStatus::UNCHANGED;
  6755. // TODO: From readattrs.ll: "inalloca parameters are always
  6756. // considered written"
  6757. if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
  6758. removeKnownBits(NO_WRITES);
  6759. removeAssumedBits(NO_WRITES);
  6760. }
  6761. return AAMemoryBehaviorFloating::manifest(A);
  6762. }
  6763. /// See AbstractAttribute::trackStatistics()
  6764. void trackStatistics() const override {
  6765. if (isAssumedReadNone())
  6766. STATS_DECLTRACK_ARG_ATTR(readnone)
  6767. else if (isAssumedReadOnly())
  6768. STATS_DECLTRACK_ARG_ATTR(readonly)
  6769. else if (isAssumedWriteOnly())
  6770. STATS_DECLTRACK_ARG_ATTR(writeonly)
  6771. }
  6772. };
  6773. struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
  6774. AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
  6775. : AAMemoryBehaviorArgument(IRP, A) {}
  6776. /// See AbstractAttribute::initialize(...).
  6777. void initialize(Attributor &A) override {
  6778. // If we don't have an associated attribute this is either a variadic call
  6779. // or an indirect call, either way, nothing to do here.
  6780. Argument *Arg = getAssociatedArgument();
  6781. if (!Arg) {
  6782. indicatePessimisticFixpoint();
  6783. return;
  6784. }
  6785. if (Arg->hasByValAttr()) {
  6786. addKnownBits(NO_WRITES);
  6787. removeKnownBits(NO_READS);
  6788. removeAssumedBits(NO_READS);
  6789. }
  6790. AAMemoryBehaviorArgument::initialize(A);
  6791. if (getAssociatedFunction()->isDeclaration())
  6792. indicatePessimisticFixpoint();
  6793. }
  6794. /// See AbstractAttribute::updateImpl(...).
  6795. ChangeStatus updateImpl(Attributor &A) override {
  6796. // TODO: Once we have call site specific value information we can provide
  6797. // call site specific liveness liveness information and then it makes
  6798. // sense to specialize attributes for call sites arguments instead of
  6799. // redirecting requests to the callee argument.
  6800. Argument *Arg = getAssociatedArgument();
  6801. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  6802. auto &ArgAA =
  6803. A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
  6804. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  6805. }
  6806. /// See AbstractAttribute::trackStatistics()
  6807. void trackStatistics() const override {
  6808. if (isAssumedReadNone())
  6809. STATS_DECLTRACK_CSARG_ATTR(readnone)
  6810. else if (isAssumedReadOnly())
  6811. STATS_DECLTRACK_CSARG_ATTR(readonly)
  6812. else if (isAssumedWriteOnly())
  6813. STATS_DECLTRACK_CSARG_ATTR(writeonly)
  6814. }
  6815. };
  6816. /// Memory behavior attribute for a call site return position.
  6817. struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
  6818. AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
  6819. : AAMemoryBehaviorFloating(IRP, A) {}
  6820. /// See AbstractAttribute::initialize(...).
  6821. void initialize(Attributor &A) override {
  6822. AAMemoryBehaviorImpl::initialize(A);
  6823. Function *F = getAssociatedFunction();
  6824. if (!F || F->isDeclaration())
  6825. indicatePessimisticFixpoint();
  6826. }
  6827. /// See AbstractAttribute::manifest(...).
  6828. ChangeStatus manifest(Attributor &A) override {
  6829. // We do not annotate returned values.
  6830. return ChangeStatus::UNCHANGED;
  6831. }
  6832. /// See AbstractAttribute::trackStatistics()
  6833. void trackStatistics() const override {}
  6834. };
  6835. /// An AA to represent the memory behavior function attributes.
  6836. struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
  6837. AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
  6838. : AAMemoryBehaviorImpl(IRP, A) {}
  6839. /// See AbstractAttribute::updateImpl(Attributor &A).
  6840. ChangeStatus updateImpl(Attributor &A) override;
  6841. /// See AbstractAttribute::manifest(...).
  6842. ChangeStatus manifest(Attributor &A) override {
  6843. // TODO: It would be better to merge this with AAMemoryLocation, so that
  6844. // we could determine read/write per location. This would also have the
  6845. // benefit of only one place trying to manifest the memory attribute.
  6846. Function &F = cast<Function>(getAnchorValue());
  6847. MemoryEffects ME = MemoryEffects::unknown();
  6848. if (isAssumedReadNone())
  6849. ME = MemoryEffects::none();
  6850. else if (isAssumedReadOnly())
  6851. ME = MemoryEffects::readOnly();
  6852. else if (isAssumedWriteOnly())
  6853. ME = MemoryEffects::writeOnly();
  6854. // Intersect with existing memory attribute, as we currently deduce the
  6855. // location and modref portion separately.
  6856. MemoryEffects ExistingME = F.getMemoryEffects();
  6857. ME &= ExistingME;
  6858. if (ME == ExistingME)
  6859. return ChangeStatus::UNCHANGED;
  6860. return IRAttributeManifest::manifestAttrs(
  6861. A, getIRPosition(), Attribute::getWithMemoryEffects(F.getContext(), ME),
  6862. /*ForceReplace*/ true);
  6863. }
  6864. /// See AbstractAttribute::trackStatistics()
  6865. void trackStatistics() const override {
  6866. if (isAssumedReadNone())
  6867. STATS_DECLTRACK_FN_ATTR(readnone)
  6868. else if (isAssumedReadOnly())
  6869. STATS_DECLTRACK_FN_ATTR(readonly)
  6870. else if (isAssumedWriteOnly())
  6871. STATS_DECLTRACK_FN_ATTR(writeonly)
  6872. }
  6873. };
  6874. /// AAMemoryBehavior attribute for call sites.
  6875. struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
  6876. AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
  6877. : AAMemoryBehaviorImpl(IRP, A) {}
  6878. /// See AbstractAttribute::initialize(...).
  6879. void initialize(Attributor &A) override {
  6880. AAMemoryBehaviorImpl::initialize(A);
  6881. Function *F = getAssociatedFunction();
  6882. if (!F || F->isDeclaration())
  6883. indicatePessimisticFixpoint();
  6884. }
  6885. /// See AbstractAttribute::updateImpl(...).
  6886. ChangeStatus updateImpl(Attributor &A) override {
  6887. // TODO: Once we have call site specific value information we can provide
  6888. // call site specific liveness liveness information and then it makes
  6889. // sense to specialize attributes for call sites arguments instead of
  6890. // redirecting requests to the callee argument.
  6891. Function *F = getAssociatedFunction();
  6892. const IRPosition &FnPos = IRPosition::function(*F);
  6893. auto &FnAA =
  6894. A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
  6895. return clampStateAndIndicateChange(getState(), FnAA.getState());
  6896. }
  6897. /// See AbstractAttribute::manifest(...).
  6898. ChangeStatus manifest(Attributor &A) override {
  6899. // TODO: Deduplicate this with AAMemoryBehaviorFunction.
  6900. CallBase &CB = cast<CallBase>(getAnchorValue());
  6901. MemoryEffects ME = MemoryEffects::unknown();
  6902. if (isAssumedReadNone())
  6903. ME = MemoryEffects::none();
  6904. else if (isAssumedReadOnly())
  6905. ME = MemoryEffects::readOnly();
  6906. else if (isAssumedWriteOnly())
  6907. ME = MemoryEffects::writeOnly();
  6908. // Intersect with existing memory attribute, as we currently deduce the
  6909. // location and modref portion separately.
  6910. MemoryEffects ExistingME = CB.getMemoryEffects();
  6911. ME &= ExistingME;
  6912. if (ME == ExistingME)
  6913. return ChangeStatus::UNCHANGED;
  6914. return IRAttributeManifest::manifestAttrs(
  6915. A, getIRPosition(),
  6916. Attribute::getWithMemoryEffects(CB.getContext(), ME),
  6917. /*ForceReplace*/ true);
  6918. }
  6919. /// See AbstractAttribute::trackStatistics()
  6920. void trackStatistics() const override {
  6921. if (isAssumedReadNone())
  6922. STATS_DECLTRACK_CS_ATTR(readnone)
  6923. else if (isAssumedReadOnly())
  6924. STATS_DECLTRACK_CS_ATTR(readonly)
  6925. else if (isAssumedWriteOnly())
  6926. STATS_DECLTRACK_CS_ATTR(writeonly)
  6927. }
  6928. };
  6929. ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
  6930. // The current assumed state used to determine a change.
  6931. auto AssumedState = getAssumed();
  6932. auto CheckRWInst = [&](Instruction &I) {
  6933. // If the instruction has an own memory behavior state, use it to restrict
  6934. // the local state. No further analysis is required as the other memory
  6935. // state is as optimistic as it gets.
  6936. if (const auto *CB = dyn_cast<CallBase>(&I)) {
  6937. const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
  6938. *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
  6939. intersectAssumedBits(MemBehaviorAA.getAssumed());
  6940. return !isAtFixpoint();
  6941. }
  6942. // Remove access kind modifiers if necessary.
  6943. if (I.mayReadFromMemory())
  6944. removeAssumedBits(NO_READS);
  6945. if (I.mayWriteToMemory())
  6946. removeAssumedBits(NO_WRITES);
  6947. return !isAtFixpoint();
  6948. };
  6949. bool UsedAssumedInformation = false;
  6950. if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
  6951. UsedAssumedInformation))
  6952. return indicatePessimisticFixpoint();
  6953. return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
  6954. : ChangeStatus::UNCHANGED;
  6955. }
  6956. ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
  6957. const IRPosition &IRP = getIRPosition();
  6958. const IRPosition &FnPos = IRPosition::function_scope(IRP);
  6959. AAMemoryBehavior::StateType &S = getState();
  6960. // First, check the function scope. We take the known information and we avoid
  6961. // work if the assumed information implies the current assumed information for
  6962. // this attribute. This is a valid for all but byval arguments.
  6963. Argument *Arg = IRP.getAssociatedArgument();
  6964. AAMemoryBehavior::base_t FnMemAssumedState =
  6965. AAMemoryBehavior::StateType::getWorstState();
  6966. if (!Arg || !Arg->hasByValAttr()) {
  6967. const auto &FnMemAA =
  6968. A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
  6969. FnMemAssumedState = FnMemAA.getAssumed();
  6970. S.addKnownBits(FnMemAA.getKnown());
  6971. if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
  6972. return ChangeStatus::UNCHANGED;
  6973. }
  6974. // The current assumed state used to determine a change.
  6975. auto AssumedState = S.getAssumed();
  6976. // Make sure the value is not captured (except through "return"), if
  6977. // it is, any information derived would be irrelevant anyway as we cannot
  6978. // check the potential aliases introduced by the capture. However, no need
  6979. // to fall back to anythign less optimistic than the function state.
  6980. const auto &ArgNoCaptureAA =
  6981. A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
  6982. if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
  6983. S.intersectAssumedBits(FnMemAssumedState);
  6984. return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
  6985. : ChangeStatus::UNCHANGED;
  6986. }
  6987. // Visit and expand uses until all are analyzed or a fixpoint is reached.
  6988. auto UsePred = [&](const Use &U, bool &Follow) -> bool {
  6989. Instruction *UserI = cast<Instruction>(U.getUser());
  6990. LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI
  6991. << " \n");
  6992. // Droppable users, e.g., llvm::assume does not actually perform any action.
  6993. if (UserI->isDroppable())
  6994. return true;
  6995. // Check if the users of UserI should also be visited.
  6996. Follow = followUsersOfUseIn(A, U, UserI);
  6997. // If UserI might touch memory we analyze the use in detail.
  6998. if (UserI->mayReadOrWriteMemory())
  6999. analyzeUseIn(A, U, UserI);
  7000. return !isAtFixpoint();
  7001. };
  7002. if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
  7003. return indicatePessimisticFixpoint();
  7004. return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
  7005. : ChangeStatus::UNCHANGED;
  7006. }
  7007. bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
  7008. const Instruction *UserI) {
  7009. // The loaded value is unrelated to the pointer argument, no need to
  7010. // follow the users of the load.
  7011. if (isa<LoadInst>(UserI) || isa<ReturnInst>(UserI))
  7012. return false;
  7013. // By default we follow all uses assuming UserI might leak information on U,
  7014. // we have special handling for call sites operands though.
  7015. const auto *CB = dyn_cast<CallBase>(UserI);
  7016. if (!CB || !CB->isArgOperand(&U))
  7017. return true;
  7018. // If the use is a call argument known not to be captured, the users of
  7019. // the call do not need to be visited because they have to be unrelated to
  7020. // the input. Note that this check is not trivial even though we disallow
  7021. // general capturing of the underlying argument. The reason is that the
  7022. // call might the argument "through return", which we allow and for which we
  7023. // need to check call users.
  7024. if (U.get()->getType()->isPointerTy()) {
  7025. unsigned ArgNo = CB->getArgOperandNo(&U);
  7026. const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
  7027. *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
  7028. return !ArgNoCaptureAA.isAssumedNoCapture();
  7029. }
  7030. return true;
  7031. }
  7032. void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U,
  7033. const Instruction *UserI) {
  7034. assert(UserI->mayReadOrWriteMemory());
  7035. switch (UserI->getOpcode()) {
  7036. default:
  7037. // TODO: Handle all atomics and other side-effect operations we know of.
  7038. break;
  7039. case Instruction::Load:
  7040. // Loads cause the NO_READS property to disappear.
  7041. removeAssumedBits(NO_READS);
  7042. return;
  7043. case Instruction::Store:
  7044. // Stores cause the NO_WRITES property to disappear if the use is the
  7045. // pointer operand. Note that while capturing was taken care of somewhere
  7046. // else we need to deal with stores of the value that is not looked through.
  7047. if (cast<StoreInst>(UserI)->getPointerOperand() == U.get())
  7048. removeAssumedBits(NO_WRITES);
  7049. else
  7050. indicatePessimisticFixpoint();
  7051. return;
  7052. case Instruction::Call:
  7053. case Instruction::CallBr:
  7054. case Instruction::Invoke: {
  7055. // For call sites we look at the argument memory behavior attribute (this
  7056. // could be recursive!) in order to restrict our own state.
  7057. const auto *CB = cast<CallBase>(UserI);
  7058. // Give up on operand bundles.
  7059. if (CB->isBundleOperand(&U)) {
  7060. indicatePessimisticFixpoint();
  7061. return;
  7062. }
  7063. // Calling a function does read the function pointer, maybe write it if the
  7064. // function is self-modifying.
  7065. if (CB->isCallee(&U)) {
  7066. removeAssumedBits(NO_READS);
  7067. break;
  7068. }
  7069. // Adjust the possible access behavior based on the information on the
  7070. // argument.
  7071. IRPosition Pos;
  7072. if (U.get()->getType()->isPointerTy())
  7073. Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
  7074. else
  7075. Pos = IRPosition::callsite_function(*CB);
  7076. const auto &MemBehaviorAA =
  7077. A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
  7078. // "assumed" has at most the same bits as the MemBehaviorAA assumed
  7079. // and at least "known".
  7080. intersectAssumedBits(MemBehaviorAA.getAssumed());
  7081. return;
  7082. }
  7083. };
  7084. // Generally, look at the "may-properties" and adjust the assumed state if we
  7085. // did not trigger special handling before.
  7086. if (UserI->mayReadFromMemory())
  7087. removeAssumedBits(NO_READS);
  7088. if (UserI->mayWriteToMemory())
  7089. removeAssumedBits(NO_WRITES);
  7090. }
  7091. } // namespace
  7092. /// -------------------- Memory Locations Attributes ---------------------------
  7093. /// Includes read-none, argmemonly, inaccessiblememonly,
  7094. /// inaccessiblememorargmemonly
  7095. /// ----------------------------------------------------------------------------
  7096. std::string AAMemoryLocation::getMemoryLocationsAsStr(
  7097. AAMemoryLocation::MemoryLocationsKind MLK) {
  7098. if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
  7099. return "all memory";
  7100. if (MLK == AAMemoryLocation::NO_LOCATIONS)
  7101. return "no memory";
  7102. std::string S = "memory:";
  7103. if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
  7104. S += "stack,";
  7105. if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
  7106. S += "constant,";
  7107. if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
  7108. S += "internal global,";
  7109. if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
  7110. S += "external global,";
  7111. if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
  7112. S += "argument,";
  7113. if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
  7114. S += "inaccessible,";
  7115. if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
  7116. S += "malloced,";
  7117. if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
  7118. S += "unknown,";
  7119. S.pop_back();
  7120. return S;
  7121. }
  7122. namespace {
  7123. struct AAMemoryLocationImpl : public AAMemoryLocation {
  7124. AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
  7125. : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
  7126. AccessKind2Accesses.fill(nullptr);
  7127. }
  7128. ~AAMemoryLocationImpl() {
  7129. // The AccessSets are allocated via a BumpPtrAllocator, we call
  7130. // the destructor manually.
  7131. for (AccessSet *AS : AccessKind2Accesses)
  7132. if (AS)
  7133. AS->~AccessSet();
  7134. }
  7135. /// See AbstractAttribute::initialize(...).
  7136. void initialize(Attributor &A) override {
  7137. intersectAssumedBits(BEST_STATE);
  7138. getKnownStateFromValue(A, getIRPosition(), getState());
  7139. AAMemoryLocation::initialize(A);
  7140. }
  7141. /// Return the memory behavior information encoded in the IR for \p IRP.
  7142. static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
  7143. BitIntegerState &State,
  7144. bool IgnoreSubsumingPositions = false) {
  7145. // For internal functions we ignore `argmemonly` and
  7146. // `inaccessiblememorargmemonly` as we might break it via interprocedural
  7147. // constant propagation. It is unclear if this is the best way but it is
  7148. // unlikely this will cause real performance problems. If we are deriving
  7149. // attributes for the anchor function we even remove the attribute in
  7150. // addition to ignoring it.
  7151. // TODO: A better way to handle this would be to add ~NO_GLOBAL_MEM /
  7152. // MemoryEffects::Other as a possible location.
  7153. bool UseArgMemOnly = true;
  7154. Function *AnchorFn = IRP.getAnchorScope();
  7155. if (AnchorFn && A.isRunOn(*AnchorFn))
  7156. UseArgMemOnly = !AnchorFn->hasLocalLinkage();
  7157. SmallVector<Attribute, 2> Attrs;
  7158. IRP.getAttrs({Attribute::Memory}, Attrs, IgnoreSubsumingPositions);
  7159. for (const Attribute &Attr : Attrs) {
  7160. // TODO: We can map MemoryEffects to Attributor locations more precisely.
  7161. MemoryEffects ME = Attr.getMemoryEffects();
  7162. if (ME.doesNotAccessMemory()) {
  7163. State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
  7164. continue;
  7165. }
  7166. if (ME.onlyAccessesInaccessibleMem()) {
  7167. State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
  7168. continue;
  7169. }
  7170. if (ME.onlyAccessesArgPointees()) {
  7171. if (UseArgMemOnly)
  7172. State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
  7173. else {
  7174. // Remove location information, only keep read/write info.
  7175. ME = MemoryEffects(ME.getModRef());
  7176. IRAttributeManifest::manifestAttrs(
  7177. A, IRP,
  7178. Attribute::getWithMemoryEffects(IRP.getAnchorValue().getContext(),
  7179. ME),
  7180. /*ForceReplace*/ true);
  7181. }
  7182. continue;
  7183. }
  7184. if (ME.onlyAccessesInaccessibleOrArgMem()) {
  7185. if (UseArgMemOnly)
  7186. State.addKnownBits(inverseLocation(
  7187. NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
  7188. else {
  7189. // Remove location information, only keep read/write info.
  7190. ME = MemoryEffects(ME.getModRef());
  7191. IRAttributeManifest::manifestAttrs(
  7192. A, IRP,
  7193. Attribute::getWithMemoryEffects(IRP.getAnchorValue().getContext(),
  7194. ME),
  7195. /*ForceReplace*/ true);
  7196. }
  7197. continue;
  7198. }
  7199. }
  7200. }
  7201. /// See AbstractAttribute::getDeducedAttributes(...).
  7202. void getDeducedAttributes(LLVMContext &Ctx,
  7203. SmallVectorImpl<Attribute> &Attrs) const override {
  7204. // TODO: We can map Attributor locations to MemoryEffects more precisely.
  7205. assert(Attrs.size() == 0);
  7206. if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
  7207. if (isAssumedReadNone())
  7208. Attrs.push_back(
  7209. Attribute::getWithMemoryEffects(Ctx, MemoryEffects::none()));
  7210. else if (isAssumedInaccessibleMemOnly())
  7211. Attrs.push_back(Attribute::getWithMemoryEffects(
  7212. Ctx, MemoryEffects::inaccessibleMemOnly()));
  7213. else if (isAssumedArgMemOnly())
  7214. Attrs.push_back(
  7215. Attribute::getWithMemoryEffects(Ctx, MemoryEffects::argMemOnly()));
  7216. else if (isAssumedInaccessibleOrArgMemOnly())
  7217. Attrs.push_back(Attribute::getWithMemoryEffects(
  7218. Ctx, MemoryEffects::inaccessibleOrArgMemOnly()));
  7219. }
  7220. assert(Attrs.size() <= 1);
  7221. }
  7222. /// See AbstractAttribute::manifest(...).
  7223. ChangeStatus manifest(Attributor &A) override {
  7224. // TODO: If AAMemoryLocation and AAMemoryBehavior are merged, we could
  7225. // provide per-location modref information here.
  7226. const IRPosition &IRP = getIRPosition();
  7227. SmallVector<Attribute, 1> DeducedAttrs;
  7228. getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
  7229. if (DeducedAttrs.size() != 1)
  7230. return ChangeStatus::UNCHANGED;
  7231. MemoryEffects ME = DeducedAttrs[0].getMemoryEffects();
  7232. // Intersect with existing memory attribute, as we currently deduce the
  7233. // location and modref portion separately.
  7234. SmallVector<Attribute, 1> ExistingAttrs;
  7235. IRP.getAttrs({Attribute::Memory}, ExistingAttrs,
  7236. /* IgnoreSubsumingPositions */ true);
  7237. if (ExistingAttrs.size() == 1) {
  7238. MemoryEffects ExistingME = ExistingAttrs[0].getMemoryEffects();
  7239. ME &= ExistingME;
  7240. if (ME == ExistingME)
  7241. return ChangeStatus::UNCHANGED;
  7242. }
  7243. return IRAttributeManifest::manifestAttrs(
  7244. A, IRP,
  7245. Attribute::getWithMemoryEffects(IRP.getAnchorValue().getContext(), ME),
  7246. /*ForceReplace*/ true);
  7247. }
  7248. /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
  7249. bool checkForAllAccessesToMemoryKind(
  7250. function_ref<bool(const Instruction *, const Value *, AccessKind,
  7251. MemoryLocationsKind)>
  7252. Pred,
  7253. MemoryLocationsKind RequestedMLK) const override {
  7254. if (!isValidState())
  7255. return false;
  7256. MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
  7257. if (AssumedMLK == NO_LOCATIONS)
  7258. return true;
  7259. unsigned Idx = 0;
  7260. for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
  7261. CurMLK *= 2, ++Idx) {
  7262. if (CurMLK & RequestedMLK)
  7263. continue;
  7264. if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
  7265. for (const AccessInfo &AI : *Accesses)
  7266. if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
  7267. return false;
  7268. }
  7269. return true;
  7270. }
  7271. ChangeStatus indicatePessimisticFixpoint() override {
  7272. // If we give up and indicate a pessimistic fixpoint this instruction will
  7273. // become an access for all potential access kinds:
  7274. // TODO: Add pointers for argmemonly and globals to improve the results of
  7275. // checkForAllAccessesToMemoryKind.
  7276. bool Changed = false;
  7277. MemoryLocationsKind KnownMLK = getKnown();
  7278. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  7279. for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
  7280. if (!(CurMLK & KnownMLK))
  7281. updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
  7282. getAccessKindFromInst(I));
  7283. return AAMemoryLocation::indicatePessimisticFixpoint();
  7284. }
  7285. protected:
  7286. /// Helper struct to tie together an instruction that has a read or write
  7287. /// effect with the pointer it accesses (if any).
  7288. struct AccessInfo {
  7289. /// The instruction that caused the access.
  7290. const Instruction *I;
  7291. /// The base pointer that is accessed, or null if unknown.
  7292. const Value *Ptr;
  7293. /// The kind of access (read/write/read+write).
  7294. AccessKind Kind;
  7295. bool operator==(const AccessInfo &RHS) const {
  7296. return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
  7297. }
  7298. bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
  7299. if (LHS.I != RHS.I)
  7300. return LHS.I < RHS.I;
  7301. if (LHS.Ptr != RHS.Ptr)
  7302. return LHS.Ptr < RHS.Ptr;
  7303. if (LHS.Kind != RHS.Kind)
  7304. return LHS.Kind < RHS.Kind;
  7305. return false;
  7306. }
  7307. };
  7308. /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
  7309. /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
  7310. using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
  7311. std::array<AccessSet *, llvm::CTLog2<VALID_STATE>()> AccessKind2Accesses;
  7312. /// Categorize the pointer arguments of CB that might access memory in
  7313. /// AccessedLoc and update the state and access map accordingly.
  7314. void
  7315. categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
  7316. AAMemoryLocation::StateType &AccessedLocs,
  7317. bool &Changed);
  7318. /// Return the kind(s) of location that may be accessed by \p V.
  7319. AAMemoryLocation::MemoryLocationsKind
  7320. categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
  7321. /// Return the access kind as determined by \p I.
  7322. AccessKind getAccessKindFromInst(const Instruction *I) {
  7323. AccessKind AK = READ_WRITE;
  7324. if (I) {
  7325. AK = I->mayReadFromMemory() ? READ : NONE;
  7326. AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
  7327. }
  7328. return AK;
  7329. }
  7330. /// Update the state \p State and the AccessKind2Accesses given that \p I is
  7331. /// an access of kind \p AK to a \p MLK memory location with the access
  7332. /// pointer \p Ptr.
  7333. void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
  7334. MemoryLocationsKind MLK, const Instruction *I,
  7335. const Value *Ptr, bool &Changed,
  7336. AccessKind AK = READ_WRITE) {
  7337. assert(isPowerOf2_32(MLK) && "Expected a single location set!");
  7338. auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
  7339. if (!Accesses)
  7340. Accesses = new (Allocator) AccessSet();
  7341. Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
  7342. State.removeAssumedBits(MLK);
  7343. }
  7344. /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
  7345. /// arguments, and update the state and access map accordingly.
  7346. void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
  7347. AAMemoryLocation::StateType &State, bool &Changed);
  7348. /// Used to allocate access sets.
  7349. BumpPtrAllocator &Allocator;
  7350. };
  7351. void AAMemoryLocationImpl::categorizePtrValue(
  7352. Attributor &A, const Instruction &I, const Value &Ptr,
  7353. AAMemoryLocation::StateType &State, bool &Changed) {
  7354. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
  7355. << Ptr << " ["
  7356. << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
  7357. auto Pred = [&](Value &Obj) {
  7358. // TODO: recognize the TBAA used for constant accesses.
  7359. MemoryLocationsKind MLK = NO_LOCATIONS;
  7360. if (isa<UndefValue>(&Obj))
  7361. return true;
  7362. if (isa<Argument>(&Obj)) {
  7363. // TODO: For now we do not treat byval arguments as local copies performed
  7364. // on the call edge, though, we should. To make that happen we need to
  7365. // teach various passes, e.g., DSE, about the copy effect of a byval. That
  7366. // would also allow us to mark functions only accessing byval arguments as
  7367. // readnone again, arguably their accesses have no effect outside of the
  7368. // function, like accesses to allocas.
  7369. MLK = NO_ARGUMENT_MEM;
  7370. } else if (auto *GV = dyn_cast<GlobalValue>(&Obj)) {
  7371. // Reading constant memory is not treated as a read "effect" by the
  7372. // function attr pass so we won't neither. Constants defined by TBAA are
  7373. // similar. (We know we do not write it because it is constant.)
  7374. if (auto *GVar = dyn_cast<GlobalVariable>(GV))
  7375. if (GVar->isConstant())
  7376. return true;
  7377. if (GV->hasLocalLinkage())
  7378. MLK = NO_GLOBAL_INTERNAL_MEM;
  7379. else
  7380. MLK = NO_GLOBAL_EXTERNAL_MEM;
  7381. } else if (isa<ConstantPointerNull>(&Obj) &&
  7382. !NullPointerIsDefined(getAssociatedFunction(),
  7383. Ptr.getType()->getPointerAddressSpace())) {
  7384. return true;
  7385. } else if (isa<AllocaInst>(&Obj)) {
  7386. MLK = NO_LOCAL_MEM;
  7387. } else if (const auto *CB = dyn_cast<CallBase>(&Obj)) {
  7388. const auto &NoAliasAA = A.getAAFor<AANoAlias>(
  7389. *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
  7390. if (NoAliasAA.isAssumedNoAlias())
  7391. MLK = NO_MALLOCED_MEM;
  7392. else
  7393. MLK = NO_UNKOWN_MEM;
  7394. } else {
  7395. MLK = NO_UNKOWN_MEM;
  7396. }
  7397. assert(MLK != NO_LOCATIONS && "No location specified!");
  7398. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "
  7399. << Obj << " -> " << getMemoryLocationsAsStr(MLK) << "\n");
  7400. updateStateAndAccessesMap(getState(), MLK, &I, &Obj, Changed,
  7401. getAccessKindFromInst(&I));
  7402. return true;
  7403. };
  7404. const auto &AA = A.getAAFor<AAUnderlyingObjects>(
  7405. *this, IRPosition::value(Ptr), DepClassTy::OPTIONAL);
  7406. if (!AA.forallUnderlyingObjects(Pred, AA::Intraprocedural)) {
  7407. LLVM_DEBUG(
  7408. dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
  7409. updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
  7410. getAccessKindFromInst(&I));
  7411. return;
  7412. }
  7413. LLVM_DEBUG(
  7414. dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "
  7415. << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
  7416. }
  7417. void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
  7418. Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
  7419. bool &Changed) {
  7420. for (unsigned ArgNo = 0, E = CB.arg_size(); ArgNo < E; ++ArgNo) {
  7421. // Skip non-pointer arguments.
  7422. const Value *ArgOp = CB.getArgOperand(ArgNo);
  7423. if (!ArgOp->getType()->isPtrOrPtrVectorTy())
  7424. continue;
  7425. // Skip readnone arguments.
  7426. const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
  7427. const auto &ArgOpMemLocationAA =
  7428. A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
  7429. if (ArgOpMemLocationAA.isAssumedReadNone())
  7430. continue;
  7431. // Categorize potentially accessed pointer arguments as if there was an
  7432. // access instruction with them as pointer.
  7433. categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
  7434. }
  7435. }
  7436. AAMemoryLocation::MemoryLocationsKind
  7437. AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
  7438. bool &Changed) {
  7439. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
  7440. << I << "\n");
  7441. AAMemoryLocation::StateType AccessedLocs;
  7442. AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
  7443. if (auto *CB = dyn_cast<CallBase>(&I)) {
  7444. // First check if we assume any memory is access is visible.
  7445. const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
  7446. *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
  7447. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
  7448. << " [" << CBMemLocationAA << "]\n");
  7449. if (CBMemLocationAA.isAssumedReadNone())
  7450. return NO_LOCATIONS;
  7451. if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
  7452. updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
  7453. Changed, getAccessKindFromInst(&I));
  7454. return AccessedLocs.getAssumed();
  7455. }
  7456. uint32_t CBAssumedNotAccessedLocs =
  7457. CBMemLocationAA.getAssumedNotAccessedLocation();
  7458. // Set the argmemonly and global bit as we handle them separately below.
  7459. uint32_t CBAssumedNotAccessedLocsNoArgMem =
  7460. CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
  7461. for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
  7462. if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
  7463. continue;
  7464. updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
  7465. getAccessKindFromInst(&I));
  7466. }
  7467. // Now handle global memory if it might be accessed. This is slightly tricky
  7468. // as NO_GLOBAL_MEM has multiple bits set.
  7469. bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
  7470. if (HasGlobalAccesses) {
  7471. auto AccessPred = [&](const Instruction *, const Value *Ptr,
  7472. AccessKind Kind, MemoryLocationsKind MLK) {
  7473. updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
  7474. getAccessKindFromInst(&I));
  7475. return true;
  7476. };
  7477. if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
  7478. AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
  7479. return AccessedLocs.getWorstState();
  7480. }
  7481. LLVM_DEBUG(
  7482. dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
  7483. << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
  7484. // Now handle argument memory if it might be accessed.
  7485. bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
  7486. if (HasArgAccesses)
  7487. categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
  7488. LLVM_DEBUG(
  7489. dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
  7490. << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
  7491. return AccessedLocs.getAssumed();
  7492. }
  7493. if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
  7494. LLVM_DEBUG(
  7495. dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
  7496. << I << " [" << *Ptr << "]\n");
  7497. categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
  7498. return AccessedLocs.getAssumed();
  7499. }
  7500. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
  7501. << I << "\n");
  7502. updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
  7503. getAccessKindFromInst(&I));
  7504. return AccessedLocs.getAssumed();
  7505. }
  7506. /// An AA to represent the memory behavior function attributes.
  7507. struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
  7508. AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
  7509. : AAMemoryLocationImpl(IRP, A) {}
  7510. /// See AbstractAttribute::updateImpl(Attributor &A).
  7511. ChangeStatus updateImpl(Attributor &A) override {
  7512. const auto &MemBehaviorAA =
  7513. A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
  7514. if (MemBehaviorAA.isAssumedReadNone()) {
  7515. if (MemBehaviorAA.isKnownReadNone())
  7516. return indicateOptimisticFixpoint();
  7517. assert(isAssumedReadNone() &&
  7518. "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
  7519. A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
  7520. return ChangeStatus::UNCHANGED;
  7521. }
  7522. // The current assumed state used to determine a change.
  7523. auto AssumedState = getAssumed();
  7524. bool Changed = false;
  7525. auto CheckRWInst = [&](Instruction &I) {
  7526. MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
  7527. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
  7528. << ": " << getMemoryLocationsAsStr(MLK) << "\n");
  7529. removeAssumedBits(inverseLocation(MLK, false, false));
  7530. // Stop once only the valid bit set in the *not assumed location*, thus
  7531. // once we don't actually exclude any memory locations in the state.
  7532. return getAssumedNotAccessedLocation() != VALID_STATE;
  7533. };
  7534. bool UsedAssumedInformation = false;
  7535. if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
  7536. UsedAssumedInformation))
  7537. return indicatePessimisticFixpoint();
  7538. Changed |= AssumedState != getAssumed();
  7539. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  7540. }
  7541. /// See AbstractAttribute::trackStatistics()
  7542. void trackStatistics() const override {
  7543. if (isAssumedReadNone())
  7544. STATS_DECLTRACK_FN_ATTR(readnone)
  7545. else if (isAssumedArgMemOnly())
  7546. STATS_DECLTRACK_FN_ATTR(argmemonly)
  7547. else if (isAssumedInaccessibleMemOnly())
  7548. STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
  7549. else if (isAssumedInaccessibleOrArgMemOnly())
  7550. STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
  7551. }
  7552. };
  7553. /// AAMemoryLocation attribute for call sites.
  7554. struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
  7555. AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
  7556. : AAMemoryLocationImpl(IRP, A) {}
  7557. /// See AbstractAttribute::initialize(...).
  7558. void initialize(Attributor &A) override {
  7559. AAMemoryLocationImpl::initialize(A);
  7560. Function *F = getAssociatedFunction();
  7561. if (!F || F->isDeclaration())
  7562. indicatePessimisticFixpoint();
  7563. }
  7564. /// See AbstractAttribute::updateImpl(...).
  7565. ChangeStatus updateImpl(Attributor &A) override {
  7566. // TODO: Once we have call site specific value information we can provide
  7567. // call site specific liveness liveness information and then it makes
  7568. // sense to specialize attributes for call sites arguments instead of
  7569. // redirecting requests to the callee argument.
  7570. Function *F = getAssociatedFunction();
  7571. const IRPosition &FnPos = IRPosition::function(*F);
  7572. auto &FnAA =
  7573. A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
  7574. bool Changed = false;
  7575. auto AccessPred = [&](const Instruction *I, const Value *Ptr,
  7576. AccessKind Kind, MemoryLocationsKind MLK) {
  7577. updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
  7578. getAccessKindFromInst(I));
  7579. return true;
  7580. };
  7581. if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
  7582. return indicatePessimisticFixpoint();
  7583. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  7584. }
  7585. /// See AbstractAttribute::trackStatistics()
  7586. void trackStatistics() const override {
  7587. if (isAssumedReadNone())
  7588. STATS_DECLTRACK_CS_ATTR(readnone)
  7589. }
  7590. };
  7591. } // namespace
  7592. /// ------------------ Value Constant Range Attribute -------------------------
  7593. namespace {
  7594. struct AAValueConstantRangeImpl : AAValueConstantRange {
  7595. using StateType = IntegerRangeState;
  7596. AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
  7597. : AAValueConstantRange(IRP, A) {}
  7598. /// See AbstractAttribute::initialize(..).
  7599. void initialize(Attributor &A) override {
  7600. if (A.hasSimplificationCallback(getIRPosition())) {
  7601. indicatePessimisticFixpoint();
  7602. return;
  7603. }
  7604. // Intersect a range given by SCEV.
  7605. intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
  7606. // Intersect a range given by LVI.
  7607. intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
  7608. }
  7609. /// See AbstractAttribute::getAsStr().
  7610. const std::string getAsStr() const override {
  7611. std::string Str;
  7612. llvm::raw_string_ostream OS(Str);
  7613. OS << "range(" << getBitWidth() << ")<";
  7614. getKnown().print(OS);
  7615. OS << " / ";
  7616. getAssumed().print(OS);
  7617. OS << ">";
  7618. return OS.str();
  7619. }
  7620. /// Helper function to get a SCEV expr for the associated value at program
  7621. /// point \p I.
  7622. const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
  7623. if (!getAnchorScope())
  7624. return nullptr;
  7625. ScalarEvolution *SE =
  7626. A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
  7627. *getAnchorScope());
  7628. LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
  7629. *getAnchorScope());
  7630. if (!SE || !LI)
  7631. return nullptr;
  7632. const SCEV *S = SE->getSCEV(&getAssociatedValue());
  7633. if (!I)
  7634. return S;
  7635. return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
  7636. }
  7637. /// Helper function to get a range from SCEV for the associated value at
  7638. /// program point \p I.
  7639. ConstantRange getConstantRangeFromSCEV(Attributor &A,
  7640. const Instruction *I = nullptr) const {
  7641. if (!getAnchorScope())
  7642. return getWorstState(getBitWidth());
  7643. ScalarEvolution *SE =
  7644. A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
  7645. *getAnchorScope());
  7646. const SCEV *S = getSCEV(A, I);
  7647. if (!SE || !S)
  7648. return getWorstState(getBitWidth());
  7649. return SE->getUnsignedRange(S);
  7650. }
  7651. /// Helper function to get a range from LVI for the associated value at
  7652. /// program point \p I.
  7653. ConstantRange
  7654. getConstantRangeFromLVI(Attributor &A,
  7655. const Instruction *CtxI = nullptr) const {
  7656. if (!getAnchorScope())
  7657. return getWorstState(getBitWidth());
  7658. LazyValueInfo *LVI =
  7659. A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
  7660. *getAnchorScope());
  7661. if (!LVI || !CtxI)
  7662. return getWorstState(getBitWidth());
  7663. return LVI->getConstantRange(&getAssociatedValue(),
  7664. const_cast<Instruction *>(CtxI));
  7665. }
  7666. /// Return true if \p CtxI is valid for querying outside analyses.
  7667. /// This basically makes sure we do not ask intra-procedural analysis
  7668. /// about a context in the wrong function or a context that violates
  7669. /// dominance assumptions they might have. The \p AllowAACtxI flag indicates
  7670. /// if the original context of this AA is OK or should be considered invalid.
  7671. bool isValidCtxInstructionForOutsideAnalysis(Attributor &A,
  7672. const Instruction *CtxI,
  7673. bool AllowAACtxI) const {
  7674. if (!CtxI || (!AllowAACtxI && CtxI == getCtxI()))
  7675. return false;
  7676. // Our context might be in a different function, neither intra-procedural
  7677. // analysis (ScalarEvolution nor LazyValueInfo) can handle that.
  7678. if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction()))
  7679. return false;
  7680. // If the context is not dominated by the value there are paths to the
  7681. // context that do not define the value. This cannot be handled by
  7682. // LazyValueInfo so we need to bail.
  7683. if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) {
  7684. InformationCache &InfoCache = A.getInfoCache();
  7685. const DominatorTree *DT =
  7686. InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
  7687. *I->getFunction());
  7688. return DT && DT->dominates(I, CtxI);
  7689. }
  7690. return true;
  7691. }
  7692. /// See AAValueConstantRange::getKnownConstantRange(..).
  7693. ConstantRange
  7694. getKnownConstantRange(Attributor &A,
  7695. const Instruction *CtxI = nullptr) const override {
  7696. if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
  7697. /* AllowAACtxI */ false))
  7698. return getKnown();
  7699. ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
  7700. ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
  7701. return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
  7702. }
  7703. /// See AAValueConstantRange::getAssumedConstantRange(..).
  7704. ConstantRange
  7705. getAssumedConstantRange(Attributor &A,
  7706. const Instruction *CtxI = nullptr) const override {
  7707. // TODO: Make SCEV use Attributor assumption.
  7708. // We may be able to bound a variable range via assumptions in
  7709. // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
  7710. // evolve to x^2 + x, then we can say that y is in [2, 12].
  7711. if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
  7712. /* AllowAACtxI */ false))
  7713. return getAssumed();
  7714. ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
  7715. ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
  7716. return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
  7717. }
  7718. /// Helper function to create MDNode for range metadata.
  7719. static MDNode *
  7720. getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
  7721. const ConstantRange &AssumedConstantRange) {
  7722. Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
  7723. Ty, AssumedConstantRange.getLower())),
  7724. ConstantAsMetadata::get(ConstantInt::get(
  7725. Ty, AssumedConstantRange.getUpper()))};
  7726. return MDNode::get(Ctx, LowAndHigh);
  7727. }
  7728. /// Return true if \p Assumed is included in \p KnownRanges.
  7729. static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
  7730. if (Assumed.isFullSet())
  7731. return false;
  7732. if (!KnownRanges)
  7733. return true;
  7734. // If multiple ranges are annotated in IR, we give up to annotate assumed
  7735. // range for now.
  7736. // TODO: If there exists a known range which containts assumed range, we
  7737. // can say assumed range is better.
  7738. if (KnownRanges->getNumOperands() > 2)
  7739. return false;
  7740. ConstantInt *Lower =
  7741. mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
  7742. ConstantInt *Upper =
  7743. mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
  7744. ConstantRange Known(Lower->getValue(), Upper->getValue());
  7745. return Known.contains(Assumed) && Known != Assumed;
  7746. }
  7747. /// Helper function to set range metadata.
  7748. static bool
  7749. setRangeMetadataIfisBetterRange(Instruction *I,
  7750. const ConstantRange &AssumedConstantRange) {
  7751. auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
  7752. if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
  7753. if (!AssumedConstantRange.isEmptySet()) {
  7754. I->setMetadata(LLVMContext::MD_range,
  7755. getMDNodeForConstantRange(I->getType(), I->getContext(),
  7756. AssumedConstantRange));
  7757. return true;
  7758. }
  7759. }
  7760. return false;
  7761. }
  7762. /// See AbstractAttribute::manifest()
  7763. ChangeStatus manifest(Attributor &A) override {
  7764. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  7765. ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
  7766. assert(!AssumedConstantRange.isFullSet() && "Invalid state");
  7767. auto &V = getAssociatedValue();
  7768. if (!AssumedConstantRange.isEmptySet() &&
  7769. !AssumedConstantRange.isSingleElement()) {
  7770. if (Instruction *I = dyn_cast<Instruction>(&V)) {
  7771. assert(I == getCtxI() && "Should not annotate an instruction which is "
  7772. "not the context instruction");
  7773. if (isa<CallInst>(I) || isa<LoadInst>(I))
  7774. if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
  7775. Changed = ChangeStatus::CHANGED;
  7776. }
  7777. }
  7778. return Changed;
  7779. }
  7780. };
  7781. struct AAValueConstantRangeArgument final
  7782. : AAArgumentFromCallSiteArguments<
  7783. AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
  7784. true /* BridgeCallBaseContext */> {
  7785. using Base = AAArgumentFromCallSiteArguments<
  7786. AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
  7787. true /* BridgeCallBaseContext */>;
  7788. AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
  7789. : Base(IRP, A) {}
  7790. /// See AbstractAttribute::initialize(..).
  7791. void initialize(Attributor &A) override {
  7792. if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
  7793. indicatePessimisticFixpoint();
  7794. } else {
  7795. Base::initialize(A);
  7796. }
  7797. }
  7798. /// See AbstractAttribute::trackStatistics()
  7799. void trackStatistics() const override {
  7800. STATS_DECLTRACK_ARG_ATTR(value_range)
  7801. }
  7802. };
  7803. struct AAValueConstantRangeReturned
  7804. : AAReturnedFromReturnedValues<AAValueConstantRange,
  7805. AAValueConstantRangeImpl,
  7806. AAValueConstantRangeImpl::StateType,
  7807. /* PropogateCallBaseContext */ true> {
  7808. using Base =
  7809. AAReturnedFromReturnedValues<AAValueConstantRange,
  7810. AAValueConstantRangeImpl,
  7811. AAValueConstantRangeImpl::StateType,
  7812. /* PropogateCallBaseContext */ true>;
  7813. AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
  7814. : Base(IRP, A) {}
  7815. /// See AbstractAttribute::initialize(...).
  7816. void initialize(Attributor &A) override {}
  7817. /// See AbstractAttribute::trackStatistics()
  7818. void trackStatistics() const override {
  7819. STATS_DECLTRACK_FNRET_ATTR(value_range)
  7820. }
  7821. };
  7822. struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
  7823. AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
  7824. : AAValueConstantRangeImpl(IRP, A) {}
  7825. /// See AbstractAttribute::initialize(...).
  7826. void initialize(Attributor &A) override {
  7827. AAValueConstantRangeImpl::initialize(A);
  7828. if (isAtFixpoint())
  7829. return;
  7830. Value &V = getAssociatedValue();
  7831. if (auto *C = dyn_cast<ConstantInt>(&V)) {
  7832. unionAssumed(ConstantRange(C->getValue()));
  7833. indicateOptimisticFixpoint();
  7834. return;
  7835. }
  7836. if (isa<UndefValue>(&V)) {
  7837. // Collapse the undef state to 0.
  7838. unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
  7839. indicateOptimisticFixpoint();
  7840. return;
  7841. }
  7842. if (isa<CallBase>(&V))
  7843. return;
  7844. if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
  7845. return;
  7846. // If it is a load instruction with range metadata, use it.
  7847. if (LoadInst *LI = dyn_cast<LoadInst>(&V))
  7848. if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
  7849. intersectKnown(getConstantRangeFromMetadata(*RangeMD));
  7850. return;
  7851. }
  7852. // We can work with PHI and select instruction as we traverse their operands
  7853. // during update.
  7854. if (isa<SelectInst>(V) || isa<PHINode>(V))
  7855. return;
  7856. // Otherwise we give up.
  7857. indicatePessimisticFixpoint();
  7858. LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
  7859. << getAssociatedValue() << "\n");
  7860. }
  7861. bool calculateBinaryOperator(
  7862. Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
  7863. const Instruction *CtxI,
  7864. SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
  7865. Value *LHS = BinOp->getOperand(0);
  7866. Value *RHS = BinOp->getOperand(1);
  7867. // Simplify the operands first.
  7868. bool UsedAssumedInformation = false;
  7869. const auto &SimplifiedLHS = A.getAssumedSimplified(
  7870. IRPosition::value(*LHS, getCallBaseContext()), *this,
  7871. UsedAssumedInformation, AA::Interprocedural);
  7872. if (!SimplifiedLHS.has_value())
  7873. return true;
  7874. if (!*SimplifiedLHS)
  7875. return false;
  7876. LHS = *SimplifiedLHS;
  7877. const auto &SimplifiedRHS = A.getAssumedSimplified(
  7878. IRPosition::value(*RHS, getCallBaseContext()), *this,
  7879. UsedAssumedInformation, AA::Interprocedural);
  7880. if (!SimplifiedRHS.has_value())
  7881. return true;
  7882. if (!*SimplifiedRHS)
  7883. return false;
  7884. RHS = *SimplifiedRHS;
  7885. // TODO: Allow non integers as well.
  7886. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7887. return false;
  7888. auto &LHSAA = A.getAAFor<AAValueConstantRange>(
  7889. *this, IRPosition::value(*LHS, getCallBaseContext()),
  7890. DepClassTy::REQUIRED);
  7891. QuerriedAAs.push_back(&LHSAA);
  7892. auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
  7893. auto &RHSAA = A.getAAFor<AAValueConstantRange>(
  7894. *this, IRPosition::value(*RHS, getCallBaseContext()),
  7895. DepClassTy::REQUIRED);
  7896. QuerriedAAs.push_back(&RHSAA);
  7897. auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
  7898. auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
  7899. T.unionAssumed(AssumedRange);
  7900. // TODO: Track a known state too.
  7901. return T.isValidState();
  7902. }
  7903. bool calculateCastInst(
  7904. Attributor &A, CastInst *CastI, IntegerRangeState &T,
  7905. const Instruction *CtxI,
  7906. SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
  7907. assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
  7908. // TODO: Allow non integers as well.
  7909. Value *OpV = CastI->getOperand(0);
  7910. // Simplify the operand first.
  7911. bool UsedAssumedInformation = false;
  7912. const auto &SimplifiedOpV = A.getAssumedSimplified(
  7913. IRPosition::value(*OpV, getCallBaseContext()), *this,
  7914. UsedAssumedInformation, AA::Interprocedural);
  7915. if (!SimplifiedOpV.has_value())
  7916. return true;
  7917. if (!*SimplifiedOpV)
  7918. return false;
  7919. OpV = *SimplifiedOpV;
  7920. if (!OpV->getType()->isIntegerTy())
  7921. return false;
  7922. auto &OpAA = A.getAAFor<AAValueConstantRange>(
  7923. *this, IRPosition::value(*OpV, getCallBaseContext()),
  7924. DepClassTy::REQUIRED);
  7925. QuerriedAAs.push_back(&OpAA);
  7926. T.unionAssumed(
  7927. OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
  7928. return T.isValidState();
  7929. }
  7930. bool
  7931. calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
  7932. const Instruction *CtxI,
  7933. SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
  7934. Value *LHS = CmpI->getOperand(0);
  7935. Value *RHS = CmpI->getOperand(1);
  7936. // Simplify the operands first.
  7937. bool UsedAssumedInformation = false;
  7938. const auto &SimplifiedLHS = A.getAssumedSimplified(
  7939. IRPosition::value(*LHS, getCallBaseContext()), *this,
  7940. UsedAssumedInformation, AA::Interprocedural);
  7941. if (!SimplifiedLHS.has_value())
  7942. return true;
  7943. if (!*SimplifiedLHS)
  7944. return false;
  7945. LHS = *SimplifiedLHS;
  7946. const auto &SimplifiedRHS = A.getAssumedSimplified(
  7947. IRPosition::value(*RHS, getCallBaseContext()), *this,
  7948. UsedAssumedInformation, AA::Interprocedural);
  7949. if (!SimplifiedRHS.has_value())
  7950. return true;
  7951. if (!*SimplifiedRHS)
  7952. return false;
  7953. RHS = *SimplifiedRHS;
  7954. // TODO: Allow non integers as well.
  7955. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7956. return false;
  7957. auto &LHSAA = A.getAAFor<AAValueConstantRange>(
  7958. *this, IRPosition::value(*LHS, getCallBaseContext()),
  7959. DepClassTy::REQUIRED);
  7960. QuerriedAAs.push_back(&LHSAA);
  7961. auto &RHSAA = A.getAAFor<AAValueConstantRange>(
  7962. *this, IRPosition::value(*RHS, getCallBaseContext()),
  7963. DepClassTy::REQUIRED);
  7964. QuerriedAAs.push_back(&RHSAA);
  7965. auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
  7966. auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
  7967. // If one of them is empty set, we can't decide.
  7968. if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
  7969. return true;
  7970. bool MustTrue = false, MustFalse = false;
  7971. auto AllowedRegion =
  7972. ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
  7973. if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
  7974. MustFalse = true;
  7975. if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange))
  7976. MustTrue = true;
  7977. assert((!MustTrue || !MustFalse) &&
  7978. "Either MustTrue or MustFalse should be false!");
  7979. if (MustTrue)
  7980. T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
  7981. else if (MustFalse)
  7982. T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
  7983. else
  7984. T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
  7985. LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
  7986. << " " << RHSAA << "\n");
  7987. // TODO: Track a known state too.
  7988. return T.isValidState();
  7989. }
  7990. /// See AbstractAttribute::updateImpl(...).
  7991. ChangeStatus updateImpl(Attributor &A) override {
  7992. IntegerRangeState T(getBitWidth());
  7993. auto VisitValueCB = [&](Value &V, const Instruction *CtxI) -> bool {
  7994. Instruction *I = dyn_cast<Instruction>(&V);
  7995. if (!I || isa<CallBase>(I)) {
  7996. // Simplify the operand first.
  7997. bool UsedAssumedInformation = false;
  7998. const auto &SimplifiedOpV = A.getAssumedSimplified(
  7999. IRPosition::value(V, getCallBaseContext()), *this,
  8000. UsedAssumedInformation, AA::Interprocedural);
  8001. if (!SimplifiedOpV.has_value())
  8002. return true;
  8003. if (!*SimplifiedOpV)
  8004. return false;
  8005. Value *VPtr = *SimplifiedOpV;
  8006. // If the value is not instruction, we query AA to Attributor.
  8007. const auto &AA = A.getAAFor<AAValueConstantRange>(
  8008. *this, IRPosition::value(*VPtr, getCallBaseContext()),
  8009. DepClassTy::REQUIRED);
  8010. // Clamp operator is not used to utilize a program point CtxI.
  8011. T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
  8012. return T.isValidState();
  8013. }
  8014. SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
  8015. if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
  8016. if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
  8017. return false;
  8018. } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
  8019. if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
  8020. return false;
  8021. } else if (auto *CastI = dyn_cast<CastInst>(I)) {
  8022. if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
  8023. return false;
  8024. } else {
  8025. // Give up with other instructions.
  8026. // TODO: Add other instructions
  8027. T.indicatePessimisticFixpoint();
  8028. return false;
  8029. }
  8030. // Catch circular reasoning in a pessimistic way for now.
  8031. // TODO: Check how the range evolves and if we stripped anything, see also
  8032. // AADereferenceable or AAAlign for similar situations.
  8033. for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
  8034. if (QueriedAA != this)
  8035. continue;
  8036. // If we are in a stady state we do not need to worry.
  8037. if (T.getAssumed() == getState().getAssumed())
  8038. continue;
  8039. T.indicatePessimisticFixpoint();
  8040. }
  8041. return T.isValidState();
  8042. };
  8043. if (!VisitValueCB(getAssociatedValue(), getCtxI()))
  8044. return indicatePessimisticFixpoint();
  8045. // Ensure that long def-use chains can't cause circular reasoning either by
  8046. // introducing a cutoff below.
  8047. if (clampStateAndIndicateChange(getState(), T) == ChangeStatus::UNCHANGED)
  8048. return ChangeStatus::UNCHANGED;
  8049. if (++NumChanges > MaxNumChanges) {
  8050. LLVM_DEBUG(dbgs() << "[AAValueConstantRange] performed " << NumChanges
  8051. << " but only " << MaxNumChanges
  8052. << " are allowed to avoid cyclic reasoning.");
  8053. return indicatePessimisticFixpoint();
  8054. }
  8055. return ChangeStatus::CHANGED;
  8056. }
  8057. /// See AbstractAttribute::trackStatistics()
  8058. void trackStatistics() const override {
  8059. STATS_DECLTRACK_FLOATING_ATTR(value_range)
  8060. }
  8061. /// Tracker to bail after too many widening steps of the constant range.
  8062. int NumChanges = 0;
  8063. /// Upper bound for the number of allowed changes (=widening steps) for the
  8064. /// constant range before we give up.
  8065. static constexpr int MaxNumChanges = 5;
  8066. };
  8067. struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
  8068. AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
  8069. : AAValueConstantRangeImpl(IRP, A) {}
  8070. /// See AbstractAttribute::initialize(...).
  8071. ChangeStatus updateImpl(Attributor &A) override {
  8072. llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
  8073. "not be called");
  8074. }
  8075. /// See AbstractAttribute::trackStatistics()
  8076. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
  8077. };
  8078. struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
  8079. AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
  8080. : AAValueConstantRangeFunction(IRP, A) {}
  8081. /// See AbstractAttribute::trackStatistics()
  8082. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
  8083. };
  8084. struct AAValueConstantRangeCallSiteReturned
  8085. : AACallSiteReturnedFromReturned<AAValueConstantRange,
  8086. AAValueConstantRangeImpl,
  8087. AAValueConstantRangeImpl::StateType,
  8088. /* IntroduceCallBaseContext */ true> {
  8089. AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
  8090. : AACallSiteReturnedFromReturned<AAValueConstantRange,
  8091. AAValueConstantRangeImpl,
  8092. AAValueConstantRangeImpl::StateType,
  8093. /* IntroduceCallBaseContext */ true>(IRP,
  8094. A) {
  8095. }
  8096. /// See AbstractAttribute::initialize(...).
  8097. void initialize(Attributor &A) override {
  8098. // If it is a load instruction with range metadata, use the metadata.
  8099. if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
  8100. if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
  8101. intersectKnown(getConstantRangeFromMetadata(*RangeMD));
  8102. AAValueConstantRangeImpl::initialize(A);
  8103. }
  8104. /// See AbstractAttribute::trackStatistics()
  8105. void trackStatistics() const override {
  8106. STATS_DECLTRACK_CSRET_ATTR(value_range)
  8107. }
  8108. };
  8109. struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
  8110. AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
  8111. : AAValueConstantRangeFloating(IRP, A) {}
  8112. /// See AbstractAttribute::manifest()
  8113. ChangeStatus manifest(Attributor &A) override {
  8114. return ChangeStatus::UNCHANGED;
  8115. }
  8116. /// See AbstractAttribute::trackStatistics()
  8117. void trackStatistics() const override {
  8118. STATS_DECLTRACK_CSARG_ATTR(value_range)
  8119. }
  8120. };
  8121. } // namespace
  8122. /// ------------------ Potential Values Attribute -------------------------
  8123. namespace {
  8124. struct AAPotentialConstantValuesImpl : AAPotentialConstantValues {
  8125. using StateType = PotentialConstantIntValuesState;
  8126. AAPotentialConstantValuesImpl(const IRPosition &IRP, Attributor &A)
  8127. : AAPotentialConstantValues(IRP, A) {}
  8128. /// See AbstractAttribute::initialize(..).
  8129. void initialize(Attributor &A) override {
  8130. if (A.hasSimplificationCallback(getIRPosition()))
  8131. indicatePessimisticFixpoint();
  8132. else
  8133. AAPotentialConstantValues::initialize(A);
  8134. }
  8135. bool fillSetWithConstantValues(Attributor &A, const IRPosition &IRP, SetTy &S,
  8136. bool &ContainsUndef, bool ForSelf) {
  8137. SmallVector<AA::ValueAndContext> Values;
  8138. bool UsedAssumedInformation = false;
  8139. if (!A.getAssumedSimplifiedValues(IRP, *this, Values, AA::Interprocedural,
  8140. UsedAssumedInformation)) {
  8141. // Avoid recursion when the caller is computing constant values for this
  8142. // IRP itself.
  8143. if (ForSelf)
  8144. return false;
  8145. if (!IRP.getAssociatedType()->isIntegerTy())
  8146. return false;
  8147. auto &PotentialValuesAA = A.getAAFor<AAPotentialConstantValues>(
  8148. *this, IRP, DepClassTy::REQUIRED);
  8149. if (!PotentialValuesAA.getState().isValidState())
  8150. return false;
  8151. ContainsUndef = PotentialValuesAA.getState().undefIsContained();
  8152. S = PotentialValuesAA.getState().getAssumedSet();
  8153. return true;
  8154. }
  8155. // Copy all the constant values, except UndefValue. ContainsUndef is true
  8156. // iff Values contains only UndefValue instances. If there are other known
  8157. // constants, then UndefValue is dropped.
  8158. ContainsUndef = false;
  8159. for (auto &It : Values) {
  8160. if (isa<UndefValue>(It.getValue())) {
  8161. ContainsUndef = true;
  8162. continue;
  8163. }
  8164. auto *CI = dyn_cast<ConstantInt>(It.getValue());
  8165. if (!CI)
  8166. return false;
  8167. S.insert(CI->getValue());
  8168. }
  8169. ContainsUndef &= S.empty();
  8170. return true;
  8171. }
  8172. /// See AbstractAttribute::getAsStr().
  8173. const std::string getAsStr() const override {
  8174. std::string Str;
  8175. llvm::raw_string_ostream OS(Str);
  8176. OS << getState();
  8177. return OS.str();
  8178. }
  8179. /// See AbstractAttribute::updateImpl(...).
  8180. ChangeStatus updateImpl(Attributor &A) override {
  8181. return indicatePessimisticFixpoint();
  8182. }
  8183. };
  8184. struct AAPotentialConstantValuesArgument final
  8185. : AAArgumentFromCallSiteArguments<AAPotentialConstantValues,
  8186. AAPotentialConstantValuesImpl,
  8187. PotentialConstantIntValuesState> {
  8188. using Base = AAArgumentFromCallSiteArguments<AAPotentialConstantValues,
  8189. AAPotentialConstantValuesImpl,
  8190. PotentialConstantIntValuesState>;
  8191. AAPotentialConstantValuesArgument(const IRPosition &IRP, Attributor &A)
  8192. : Base(IRP, A) {}
  8193. /// See AbstractAttribute::initialize(..).
  8194. void initialize(Attributor &A) override {
  8195. if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
  8196. indicatePessimisticFixpoint();
  8197. } else {
  8198. Base::initialize(A);
  8199. }
  8200. }
  8201. /// See AbstractAttribute::trackStatistics()
  8202. void trackStatistics() const override {
  8203. STATS_DECLTRACK_ARG_ATTR(potential_values)
  8204. }
  8205. };
  8206. struct AAPotentialConstantValuesReturned
  8207. : AAReturnedFromReturnedValues<AAPotentialConstantValues,
  8208. AAPotentialConstantValuesImpl> {
  8209. using Base = AAReturnedFromReturnedValues<AAPotentialConstantValues,
  8210. AAPotentialConstantValuesImpl>;
  8211. AAPotentialConstantValuesReturned(const IRPosition &IRP, Attributor &A)
  8212. : Base(IRP, A) {}
  8213. /// See AbstractAttribute::trackStatistics()
  8214. void trackStatistics() const override {
  8215. STATS_DECLTRACK_FNRET_ATTR(potential_values)
  8216. }
  8217. };
  8218. struct AAPotentialConstantValuesFloating : AAPotentialConstantValuesImpl {
  8219. AAPotentialConstantValuesFloating(const IRPosition &IRP, Attributor &A)
  8220. : AAPotentialConstantValuesImpl(IRP, A) {}
  8221. /// See AbstractAttribute::initialize(..).
  8222. void initialize(Attributor &A) override {
  8223. AAPotentialConstantValuesImpl::initialize(A);
  8224. if (isAtFixpoint())
  8225. return;
  8226. Value &V = getAssociatedValue();
  8227. if (auto *C = dyn_cast<ConstantInt>(&V)) {
  8228. unionAssumed(C->getValue());
  8229. indicateOptimisticFixpoint();
  8230. return;
  8231. }
  8232. if (isa<UndefValue>(&V)) {
  8233. unionAssumedWithUndef();
  8234. indicateOptimisticFixpoint();
  8235. return;
  8236. }
  8237. if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
  8238. return;
  8239. if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V))
  8240. return;
  8241. indicatePessimisticFixpoint();
  8242. LLVM_DEBUG(dbgs() << "[AAPotentialConstantValues] We give up: "
  8243. << getAssociatedValue() << "\n");
  8244. }
  8245. static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
  8246. const APInt &RHS) {
  8247. return ICmpInst::compare(LHS, RHS, ICI->getPredicate());
  8248. }
  8249. static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
  8250. uint32_t ResultBitWidth) {
  8251. Instruction::CastOps CastOp = CI->getOpcode();
  8252. switch (CastOp) {
  8253. default:
  8254. llvm_unreachable("unsupported or not integer cast");
  8255. case Instruction::Trunc:
  8256. return Src.trunc(ResultBitWidth);
  8257. case Instruction::SExt:
  8258. return Src.sext(ResultBitWidth);
  8259. case Instruction::ZExt:
  8260. return Src.zext(ResultBitWidth);
  8261. case Instruction::BitCast:
  8262. return Src;
  8263. }
  8264. }
  8265. static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
  8266. const APInt &LHS, const APInt &RHS,
  8267. bool &SkipOperation, bool &Unsupported) {
  8268. Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
  8269. // Unsupported is set to true when the binary operator is not supported.
  8270. // SkipOperation is set to true when UB occur with the given operand pair
  8271. // (LHS, RHS).
  8272. // TODO: we should look at nsw and nuw keywords to handle operations
  8273. // that create poison or undef value.
  8274. switch (BinOpcode) {
  8275. default:
  8276. Unsupported = true;
  8277. return LHS;
  8278. case Instruction::Add:
  8279. return LHS + RHS;
  8280. case Instruction::Sub:
  8281. return LHS - RHS;
  8282. case Instruction::Mul:
  8283. return LHS * RHS;
  8284. case Instruction::UDiv:
  8285. if (RHS.isZero()) {
  8286. SkipOperation = true;
  8287. return LHS;
  8288. }
  8289. return LHS.udiv(RHS);
  8290. case Instruction::SDiv:
  8291. if (RHS.isZero()) {
  8292. SkipOperation = true;
  8293. return LHS;
  8294. }
  8295. return LHS.sdiv(RHS);
  8296. case Instruction::URem:
  8297. if (RHS.isZero()) {
  8298. SkipOperation = true;
  8299. return LHS;
  8300. }
  8301. return LHS.urem(RHS);
  8302. case Instruction::SRem:
  8303. if (RHS.isZero()) {
  8304. SkipOperation = true;
  8305. return LHS;
  8306. }
  8307. return LHS.srem(RHS);
  8308. case Instruction::Shl:
  8309. return LHS.shl(RHS);
  8310. case Instruction::LShr:
  8311. return LHS.lshr(RHS);
  8312. case Instruction::AShr:
  8313. return LHS.ashr(RHS);
  8314. case Instruction::And:
  8315. return LHS & RHS;
  8316. case Instruction::Or:
  8317. return LHS | RHS;
  8318. case Instruction::Xor:
  8319. return LHS ^ RHS;
  8320. }
  8321. }
  8322. bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
  8323. const APInt &LHS, const APInt &RHS) {
  8324. bool SkipOperation = false;
  8325. bool Unsupported = false;
  8326. APInt Result =
  8327. calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
  8328. if (Unsupported)
  8329. return false;
  8330. // If SkipOperation is true, we can ignore this operand pair (L, R).
  8331. if (!SkipOperation)
  8332. unionAssumed(Result);
  8333. return isValidState();
  8334. }
  8335. ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
  8336. auto AssumedBefore = getAssumed();
  8337. Value *LHS = ICI->getOperand(0);
  8338. Value *RHS = ICI->getOperand(1);
  8339. bool LHSContainsUndef = false, RHSContainsUndef = false;
  8340. SetTy LHSAAPVS, RHSAAPVS;
  8341. if (!fillSetWithConstantValues(A, IRPosition::value(*LHS), LHSAAPVS,
  8342. LHSContainsUndef, /* ForSelf */ false) ||
  8343. !fillSetWithConstantValues(A, IRPosition::value(*RHS), RHSAAPVS,
  8344. RHSContainsUndef, /* ForSelf */ false))
  8345. return indicatePessimisticFixpoint();
  8346. // TODO: make use of undef flag to limit potential values aggressively.
  8347. bool MaybeTrue = false, MaybeFalse = false;
  8348. const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
  8349. if (LHSContainsUndef && RHSContainsUndef) {
  8350. // The result of any comparison between undefs can be soundly replaced
  8351. // with undef.
  8352. unionAssumedWithUndef();
  8353. } else if (LHSContainsUndef) {
  8354. for (const APInt &R : RHSAAPVS) {
  8355. bool CmpResult = calculateICmpInst(ICI, Zero, R);
  8356. MaybeTrue |= CmpResult;
  8357. MaybeFalse |= !CmpResult;
  8358. if (MaybeTrue & MaybeFalse)
  8359. return indicatePessimisticFixpoint();
  8360. }
  8361. } else if (RHSContainsUndef) {
  8362. for (const APInt &L : LHSAAPVS) {
  8363. bool CmpResult = calculateICmpInst(ICI, L, Zero);
  8364. MaybeTrue |= CmpResult;
  8365. MaybeFalse |= !CmpResult;
  8366. if (MaybeTrue & MaybeFalse)
  8367. return indicatePessimisticFixpoint();
  8368. }
  8369. } else {
  8370. for (const APInt &L : LHSAAPVS) {
  8371. for (const APInt &R : RHSAAPVS) {
  8372. bool CmpResult = calculateICmpInst(ICI, L, R);
  8373. MaybeTrue |= CmpResult;
  8374. MaybeFalse |= !CmpResult;
  8375. if (MaybeTrue & MaybeFalse)
  8376. return indicatePessimisticFixpoint();
  8377. }
  8378. }
  8379. }
  8380. if (MaybeTrue)
  8381. unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
  8382. if (MaybeFalse)
  8383. unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
  8384. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8385. : ChangeStatus::CHANGED;
  8386. }
  8387. ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
  8388. auto AssumedBefore = getAssumed();
  8389. Value *LHS = SI->getTrueValue();
  8390. Value *RHS = SI->getFalseValue();
  8391. bool UsedAssumedInformation = false;
  8392. std::optional<Constant *> C = A.getAssumedConstant(
  8393. *SI->getCondition(), *this, UsedAssumedInformation);
  8394. // Check if we only need one operand.
  8395. bool OnlyLeft = false, OnlyRight = false;
  8396. if (C && *C && (*C)->isOneValue())
  8397. OnlyLeft = true;
  8398. else if (C && *C && (*C)->isZeroValue())
  8399. OnlyRight = true;
  8400. bool LHSContainsUndef = false, RHSContainsUndef = false;
  8401. SetTy LHSAAPVS, RHSAAPVS;
  8402. if (!OnlyRight &&
  8403. !fillSetWithConstantValues(A, IRPosition::value(*LHS), LHSAAPVS,
  8404. LHSContainsUndef, /* ForSelf */ false))
  8405. return indicatePessimisticFixpoint();
  8406. if (!OnlyLeft &&
  8407. !fillSetWithConstantValues(A, IRPosition::value(*RHS), RHSAAPVS,
  8408. RHSContainsUndef, /* ForSelf */ false))
  8409. return indicatePessimisticFixpoint();
  8410. if (OnlyLeft || OnlyRight) {
  8411. // select (true/false), lhs, rhs
  8412. auto *OpAA = OnlyLeft ? &LHSAAPVS : &RHSAAPVS;
  8413. auto Undef = OnlyLeft ? LHSContainsUndef : RHSContainsUndef;
  8414. if (Undef)
  8415. unionAssumedWithUndef();
  8416. else {
  8417. for (const auto &It : *OpAA)
  8418. unionAssumed(It);
  8419. }
  8420. } else if (LHSContainsUndef && RHSContainsUndef) {
  8421. // select i1 *, undef , undef => undef
  8422. unionAssumedWithUndef();
  8423. } else {
  8424. for (const auto &It : LHSAAPVS)
  8425. unionAssumed(It);
  8426. for (const auto &It : RHSAAPVS)
  8427. unionAssumed(It);
  8428. }
  8429. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8430. : ChangeStatus::CHANGED;
  8431. }
  8432. ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
  8433. auto AssumedBefore = getAssumed();
  8434. if (!CI->isIntegerCast())
  8435. return indicatePessimisticFixpoint();
  8436. assert(CI->getNumOperands() == 1 && "Expected cast to be unary!");
  8437. uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
  8438. Value *Src = CI->getOperand(0);
  8439. bool SrcContainsUndef = false;
  8440. SetTy SrcPVS;
  8441. if (!fillSetWithConstantValues(A, IRPosition::value(*Src), SrcPVS,
  8442. SrcContainsUndef, /* ForSelf */ false))
  8443. return indicatePessimisticFixpoint();
  8444. if (SrcContainsUndef)
  8445. unionAssumedWithUndef();
  8446. else {
  8447. for (const APInt &S : SrcPVS) {
  8448. APInt T = calculateCastInst(CI, S, ResultBitWidth);
  8449. unionAssumed(T);
  8450. }
  8451. }
  8452. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8453. : ChangeStatus::CHANGED;
  8454. }
  8455. ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
  8456. auto AssumedBefore = getAssumed();
  8457. Value *LHS = BinOp->getOperand(0);
  8458. Value *RHS = BinOp->getOperand(1);
  8459. bool LHSContainsUndef = false, RHSContainsUndef = false;
  8460. SetTy LHSAAPVS, RHSAAPVS;
  8461. if (!fillSetWithConstantValues(A, IRPosition::value(*LHS), LHSAAPVS,
  8462. LHSContainsUndef, /* ForSelf */ false) ||
  8463. !fillSetWithConstantValues(A, IRPosition::value(*RHS), RHSAAPVS,
  8464. RHSContainsUndef, /* ForSelf */ false))
  8465. return indicatePessimisticFixpoint();
  8466. const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
  8467. // TODO: make use of undef flag to limit potential values aggressively.
  8468. if (LHSContainsUndef && RHSContainsUndef) {
  8469. if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
  8470. return indicatePessimisticFixpoint();
  8471. } else if (LHSContainsUndef) {
  8472. for (const APInt &R : RHSAAPVS) {
  8473. if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
  8474. return indicatePessimisticFixpoint();
  8475. }
  8476. } else if (RHSContainsUndef) {
  8477. for (const APInt &L : LHSAAPVS) {
  8478. if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
  8479. return indicatePessimisticFixpoint();
  8480. }
  8481. } else {
  8482. for (const APInt &L : LHSAAPVS) {
  8483. for (const APInt &R : RHSAAPVS) {
  8484. if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
  8485. return indicatePessimisticFixpoint();
  8486. }
  8487. }
  8488. }
  8489. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8490. : ChangeStatus::CHANGED;
  8491. }
  8492. ChangeStatus updateWithInstruction(Attributor &A, Instruction *Inst) {
  8493. auto AssumedBefore = getAssumed();
  8494. SetTy Incoming;
  8495. bool ContainsUndef;
  8496. if (!fillSetWithConstantValues(A, IRPosition::value(*Inst), Incoming,
  8497. ContainsUndef, /* ForSelf */ true))
  8498. return indicatePessimisticFixpoint();
  8499. if (ContainsUndef) {
  8500. unionAssumedWithUndef();
  8501. } else {
  8502. for (const auto &It : Incoming)
  8503. unionAssumed(It);
  8504. }
  8505. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8506. : ChangeStatus::CHANGED;
  8507. }
  8508. /// See AbstractAttribute::updateImpl(...).
  8509. ChangeStatus updateImpl(Attributor &A) override {
  8510. Value &V = getAssociatedValue();
  8511. Instruction *I = dyn_cast<Instruction>(&V);
  8512. if (auto *ICI = dyn_cast<ICmpInst>(I))
  8513. return updateWithICmpInst(A, ICI);
  8514. if (auto *SI = dyn_cast<SelectInst>(I))
  8515. return updateWithSelectInst(A, SI);
  8516. if (auto *CI = dyn_cast<CastInst>(I))
  8517. return updateWithCastInst(A, CI);
  8518. if (auto *BinOp = dyn_cast<BinaryOperator>(I))
  8519. return updateWithBinaryOperator(A, BinOp);
  8520. if (isa<PHINode>(I) || isa<LoadInst>(I))
  8521. return updateWithInstruction(A, I);
  8522. return indicatePessimisticFixpoint();
  8523. }
  8524. /// See AbstractAttribute::trackStatistics()
  8525. void trackStatistics() const override {
  8526. STATS_DECLTRACK_FLOATING_ATTR(potential_values)
  8527. }
  8528. };
  8529. struct AAPotentialConstantValuesFunction : AAPotentialConstantValuesImpl {
  8530. AAPotentialConstantValuesFunction(const IRPosition &IRP, Attributor &A)
  8531. : AAPotentialConstantValuesImpl(IRP, A) {}
  8532. /// See AbstractAttribute::initialize(...).
  8533. ChangeStatus updateImpl(Attributor &A) override {
  8534. llvm_unreachable(
  8535. "AAPotentialConstantValues(Function|CallSite)::updateImpl will "
  8536. "not be called");
  8537. }
  8538. /// See AbstractAttribute::trackStatistics()
  8539. void trackStatistics() const override {
  8540. STATS_DECLTRACK_FN_ATTR(potential_values)
  8541. }
  8542. };
  8543. struct AAPotentialConstantValuesCallSite : AAPotentialConstantValuesFunction {
  8544. AAPotentialConstantValuesCallSite(const IRPosition &IRP, Attributor &A)
  8545. : AAPotentialConstantValuesFunction(IRP, A) {}
  8546. /// See AbstractAttribute::trackStatistics()
  8547. void trackStatistics() const override {
  8548. STATS_DECLTRACK_CS_ATTR(potential_values)
  8549. }
  8550. };
  8551. struct AAPotentialConstantValuesCallSiteReturned
  8552. : AACallSiteReturnedFromReturned<AAPotentialConstantValues,
  8553. AAPotentialConstantValuesImpl> {
  8554. AAPotentialConstantValuesCallSiteReturned(const IRPosition &IRP,
  8555. Attributor &A)
  8556. : AACallSiteReturnedFromReturned<AAPotentialConstantValues,
  8557. AAPotentialConstantValuesImpl>(IRP, A) {}
  8558. /// See AbstractAttribute::trackStatistics()
  8559. void trackStatistics() const override {
  8560. STATS_DECLTRACK_CSRET_ATTR(potential_values)
  8561. }
  8562. };
  8563. struct AAPotentialConstantValuesCallSiteArgument
  8564. : AAPotentialConstantValuesFloating {
  8565. AAPotentialConstantValuesCallSiteArgument(const IRPosition &IRP,
  8566. Attributor &A)
  8567. : AAPotentialConstantValuesFloating(IRP, A) {}
  8568. /// See AbstractAttribute::initialize(..).
  8569. void initialize(Attributor &A) override {
  8570. AAPotentialConstantValuesImpl::initialize(A);
  8571. if (isAtFixpoint())
  8572. return;
  8573. Value &V = getAssociatedValue();
  8574. if (auto *C = dyn_cast<ConstantInt>(&V)) {
  8575. unionAssumed(C->getValue());
  8576. indicateOptimisticFixpoint();
  8577. return;
  8578. }
  8579. if (isa<UndefValue>(&V)) {
  8580. unionAssumedWithUndef();
  8581. indicateOptimisticFixpoint();
  8582. return;
  8583. }
  8584. }
  8585. /// See AbstractAttribute::updateImpl(...).
  8586. ChangeStatus updateImpl(Attributor &A) override {
  8587. Value &V = getAssociatedValue();
  8588. auto AssumedBefore = getAssumed();
  8589. auto &AA = A.getAAFor<AAPotentialConstantValues>(
  8590. *this, IRPosition::value(V), DepClassTy::REQUIRED);
  8591. const auto &S = AA.getAssumed();
  8592. unionAssumed(S);
  8593. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8594. : ChangeStatus::CHANGED;
  8595. }
  8596. /// See AbstractAttribute::trackStatistics()
  8597. void trackStatistics() const override {
  8598. STATS_DECLTRACK_CSARG_ATTR(potential_values)
  8599. }
  8600. };
  8601. /// ------------------------ NoUndef Attribute ---------------------------------
  8602. struct AANoUndefImpl : AANoUndef {
  8603. AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
  8604. /// See AbstractAttribute::initialize(...).
  8605. void initialize(Attributor &A) override {
  8606. if (getIRPosition().hasAttr({Attribute::NoUndef})) {
  8607. indicateOptimisticFixpoint();
  8608. return;
  8609. }
  8610. Value &V = getAssociatedValue();
  8611. if (isa<UndefValue>(V))
  8612. indicatePessimisticFixpoint();
  8613. else if (isa<FreezeInst>(V))
  8614. indicateOptimisticFixpoint();
  8615. else if (getPositionKind() != IRPosition::IRP_RETURNED &&
  8616. isGuaranteedNotToBeUndefOrPoison(&V))
  8617. indicateOptimisticFixpoint();
  8618. else
  8619. AANoUndef::initialize(A);
  8620. }
  8621. /// See followUsesInMBEC
  8622. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  8623. AANoUndef::StateType &State) {
  8624. const Value *UseV = U->get();
  8625. const DominatorTree *DT = nullptr;
  8626. AssumptionCache *AC = nullptr;
  8627. InformationCache &InfoCache = A.getInfoCache();
  8628. if (Function *F = getAnchorScope()) {
  8629. DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
  8630. AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
  8631. }
  8632. State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
  8633. bool TrackUse = false;
  8634. // Track use for instructions which must produce undef or poison bits when
  8635. // at least one operand contains such bits.
  8636. if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
  8637. TrackUse = true;
  8638. return TrackUse;
  8639. }
  8640. /// See AbstractAttribute::getAsStr().
  8641. const std::string getAsStr() const override {
  8642. return getAssumed() ? "noundef" : "may-undef-or-poison";
  8643. }
  8644. ChangeStatus manifest(Attributor &A) override {
  8645. // We don't manifest noundef attribute for dead positions because the
  8646. // associated values with dead positions would be replaced with undef
  8647. // values.
  8648. bool UsedAssumedInformation = false;
  8649. if (A.isAssumedDead(getIRPosition(), nullptr, nullptr,
  8650. UsedAssumedInformation))
  8651. return ChangeStatus::UNCHANGED;
  8652. // A position whose simplified value does not have any value is
  8653. // considered to be dead. We don't manifest noundef in such positions for
  8654. // the same reason above.
  8655. if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation,
  8656. AA::Interprocedural)
  8657. .has_value())
  8658. return ChangeStatus::UNCHANGED;
  8659. return AANoUndef::manifest(A);
  8660. }
  8661. };
  8662. struct AANoUndefFloating : public AANoUndefImpl {
  8663. AANoUndefFloating(const IRPosition &IRP, Attributor &A)
  8664. : AANoUndefImpl(IRP, A) {}
  8665. /// See AbstractAttribute::initialize(...).
  8666. void initialize(Attributor &A) override {
  8667. AANoUndefImpl::initialize(A);
  8668. if (!getState().isAtFixpoint())
  8669. if (Instruction *CtxI = getCtxI())
  8670. followUsesInMBEC(*this, A, getState(), *CtxI);
  8671. }
  8672. /// See AbstractAttribute::updateImpl(...).
  8673. ChangeStatus updateImpl(Attributor &A) override {
  8674. SmallVector<AA::ValueAndContext> Values;
  8675. bool UsedAssumedInformation = false;
  8676. if (!A.getAssumedSimplifiedValues(getIRPosition(), *this, Values,
  8677. AA::AnyScope, UsedAssumedInformation)) {
  8678. Values.push_back({getAssociatedValue(), getCtxI()});
  8679. }
  8680. StateType T;
  8681. auto VisitValueCB = [&](Value &V, const Instruction *CtxI) -> bool {
  8682. const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
  8683. DepClassTy::REQUIRED);
  8684. if (this == &AA) {
  8685. T.indicatePessimisticFixpoint();
  8686. } else {
  8687. const AANoUndef::StateType &S =
  8688. static_cast<const AANoUndef::StateType &>(AA.getState());
  8689. T ^= S;
  8690. }
  8691. return T.isValidState();
  8692. };
  8693. for (const auto &VAC : Values)
  8694. if (!VisitValueCB(*VAC.getValue(), VAC.getCtxI()))
  8695. return indicatePessimisticFixpoint();
  8696. return clampStateAndIndicateChange(getState(), T);
  8697. }
  8698. /// See AbstractAttribute::trackStatistics()
  8699. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
  8700. };
  8701. struct AANoUndefReturned final
  8702. : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
  8703. AANoUndefReturned(const IRPosition &IRP, Attributor &A)
  8704. : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
  8705. /// See AbstractAttribute::trackStatistics()
  8706. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
  8707. };
  8708. struct AANoUndefArgument final
  8709. : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
  8710. AANoUndefArgument(const IRPosition &IRP, Attributor &A)
  8711. : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
  8712. /// See AbstractAttribute::trackStatistics()
  8713. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) }
  8714. };
  8715. struct AANoUndefCallSiteArgument final : AANoUndefFloating {
  8716. AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
  8717. : AANoUndefFloating(IRP, A) {}
  8718. /// See AbstractAttribute::trackStatistics()
  8719. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) }
  8720. };
  8721. struct AANoUndefCallSiteReturned final
  8722. : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
  8723. AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
  8724. : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
  8725. /// See AbstractAttribute::trackStatistics()
  8726. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) }
  8727. };
  8728. struct AACallEdgesImpl : public AACallEdges {
  8729. AACallEdgesImpl(const IRPosition &IRP, Attributor &A) : AACallEdges(IRP, A) {}
  8730. const SetVector<Function *> &getOptimisticEdges() const override {
  8731. return CalledFunctions;
  8732. }
  8733. bool hasUnknownCallee() const override { return HasUnknownCallee; }
  8734. bool hasNonAsmUnknownCallee() const override {
  8735. return HasUnknownCalleeNonAsm;
  8736. }
  8737. const std::string getAsStr() const override {
  8738. return "CallEdges[" + std::to_string(HasUnknownCallee) + "," +
  8739. std::to_string(CalledFunctions.size()) + "]";
  8740. }
  8741. void trackStatistics() const override {}
  8742. protected:
  8743. void addCalledFunction(Function *Fn, ChangeStatus &Change) {
  8744. if (CalledFunctions.insert(Fn)) {
  8745. Change = ChangeStatus::CHANGED;
  8746. LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()
  8747. << "\n");
  8748. }
  8749. }
  8750. void setHasUnknownCallee(bool NonAsm, ChangeStatus &Change) {
  8751. if (!HasUnknownCallee)
  8752. Change = ChangeStatus::CHANGED;
  8753. if (NonAsm && !HasUnknownCalleeNonAsm)
  8754. Change = ChangeStatus::CHANGED;
  8755. HasUnknownCalleeNonAsm |= NonAsm;
  8756. HasUnknownCallee = true;
  8757. }
  8758. private:
  8759. /// Optimistic set of functions that might be called by this position.
  8760. SetVector<Function *> CalledFunctions;
  8761. /// Is there any call with a unknown callee.
  8762. bool HasUnknownCallee = false;
  8763. /// Is there any call with a unknown callee, excluding any inline asm.
  8764. bool HasUnknownCalleeNonAsm = false;
  8765. };
  8766. struct AACallEdgesCallSite : public AACallEdgesImpl {
  8767. AACallEdgesCallSite(const IRPosition &IRP, Attributor &A)
  8768. : AACallEdgesImpl(IRP, A) {}
  8769. /// See AbstractAttribute::updateImpl(...).
  8770. ChangeStatus updateImpl(Attributor &A) override {
  8771. ChangeStatus Change = ChangeStatus::UNCHANGED;
  8772. auto VisitValue = [&](Value &V, const Instruction *CtxI) -> bool {
  8773. if (Function *Fn = dyn_cast<Function>(&V)) {
  8774. addCalledFunction(Fn, Change);
  8775. } else {
  8776. LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n");
  8777. setHasUnknownCallee(true, Change);
  8778. }
  8779. // Explore all values.
  8780. return true;
  8781. };
  8782. SmallVector<AA::ValueAndContext> Values;
  8783. // Process any value that we might call.
  8784. auto ProcessCalledOperand = [&](Value *V, Instruction *CtxI) {
  8785. bool UsedAssumedInformation = false;
  8786. Values.clear();
  8787. if (!A.getAssumedSimplifiedValues(IRPosition::value(*V), *this, Values,
  8788. AA::AnyScope, UsedAssumedInformation)) {
  8789. Values.push_back({*V, CtxI});
  8790. }
  8791. for (auto &VAC : Values)
  8792. VisitValue(*VAC.getValue(), VAC.getCtxI());
  8793. };
  8794. CallBase *CB = cast<CallBase>(getCtxI());
  8795. if (auto *IA = dyn_cast<InlineAsm>(CB->getCalledOperand())) {
  8796. if (IA->hasSideEffects() &&
  8797. !hasAssumption(*CB->getCaller(), "ompx_no_call_asm") &&
  8798. !hasAssumption(*CB, "ompx_no_call_asm")) {
  8799. setHasUnknownCallee(false, Change);
  8800. }
  8801. return Change;
  8802. }
  8803. // Process callee metadata if available.
  8804. if (auto *MD = getCtxI()->getMetadata(LLVMContext::MD_callees)) {
  8805. for (const auto &Op : MD->operands()) {
  8806. Function *Callee = mdconst::dyn_extract_or_null<Function>(Op);
  8807. if (Callee)
  8808. addCalledFunction(Callee, Change);
  8809. }
  8810. return Change;
  8811. }
  8812. // The most simple case.
  8813. ProcessCalledOperand(CB->getCalledOperand(), CB);
  8814. // Process callback functions.
  8815. SmallVector<const Use *, 4u> CallbackUses;
  8816. AbstractCallSite::getCallbackUses(*CB, CallbackUses);
  8817. for (const Use *U : CallbackUses)
  8818. ProcessCalledOperand(U->get(), CB);
  8819. return Change;
  8820. }
  8821. };
  8822. struct AACallEdgesFunction : public AACallEdgesImpl {
  8823. AACallEdgesFunction(const IRPosition &IRP, Attributor &A)
  8824. : AACallEdgesImpl(IRP, A) {}
  8825. /// See AbstractAttribute::updateImpl(...).
  8826. ChangeStatus updateImpl(Attributor &A) override {
  8827. ChangeStatus Change = ChangeStatus::UNCHANGED;
  8828. auto ProcessCallInst = [&](Instruction &Inst) {
  8829. CallBase &CB = cast<CallBase>(Inst);
  8830. auto &CBEdges = A.getAAFor<AACallEdges>(
  8831. *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
  8832. if (CBEdges.hasNonAsmUnknownCallee())
  8833. setHasUnknownCallee(true, Change);
  8834. if (CBEdges.hasUnknownCallee())
  8835. setHasUnknownCallee(false, Change);
  8836. for (Function *F : CBEdges.getOptimisticEdges())
  8837. addCalledFunction(F, Change);
  8838. return true;
  8839. };
  8840. // Visit all callable instructions.
  8841. bool UsedAssumedInformation = false;
  8842. if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this,
  8843. UsedAssumedInformation,
  8844. /* CheckBBLivenessOnly */ true)) {
  8845. // If we haven't looked at all call like instructions, assume that there
  8846. // are unknown callees.
  8847. setHasUnknownCallee(true, Change);
  8848. }
  8849. return Change;
  8850. }
  8851. };
  8852. /// -------------------AAInterFnReachability Attribute--------------------------
  8853. struct AAInterFnReachabilityFunction
  8854. : public CachedReachabilityAA<AAInterFnReachability, Function> {
  8855. AAInterFnReachabilityFunction(const IRPosition &IRP, Attributor &A)
  8856. : CachedReachabilityAA<AAInterFnReachability, Function>(IRP, A) {}
  8857. bool instructionCanReach(
  8858. Attributor &A, const Instruction &From, const Function &To,
  8859. const AA::InstExclusionSetTy *ExclusionSet,
  8860. SmallPtrSet<const Function *, 16> *Visited) const override {
  8861. assert(From.getFunction() == getAnchorScope() && "Queried the wrong AA!");
  8862. auto *NonConstThis = const_cast<AAInterFnReachabilityFunction *>(this);
  8863. RQITy StackRQI(A, From, To, ExclusionSet);
  8864. typename RQITy::Reachable Result;
  8865. if (RQITy *RQIPtr = NonConstThis->checkQueryCache(A, StackRQI, Result))
  8866. return NonConstThis->isReachableImpl(A, *RQIPtr);
  8867. return Result == RQITy::Reachable::Yes;
  8868. }
  8869. bool isReachableImpl(Attributor &A, RQITy &RQI) override {
  8870. return isReachableImpl(A, RQI, nullptr);
  8871. }
  8872. bool isReachableImpl(Attributor &A, RQITy &RQI,
  8873. SmallPtrSet<const Function *, 16> *Visited) {
  8874. SmallPtrSet<const Function *, 16> LocalVisited;
  8875. if (!Visited)
  8876. Visited = &LocalVisited;
  8877. const auto &IntraFnReachability = A.getAAFor<AAIntraFnReachability>(
  8878. *this, IRPosition::function(*RQI.From->getFunction()),
  8879. DepClassTy::OPTIONAL);
  8880. // Determine call like instructions that we can reach from the inst.
  8881. SmallVector<CallBase *> ReachableCallBases;
  8882. auto CheckCallBase = [&](Instruction &CBInst) {
  8883. if (IntraFnReachability.isAssumedReachable(A, *RQI.From, CBInst,
  8884. RQI.ExclusionSet))
  8885. ReachableCallBases.push_back(cast<CallBase>(&CBInst));
  8886. return true;
  8887. };
  8888. bool UsedAssumedInformation = false;
  8889. if (!A.checkForAllCallLikeInstructions(CheckCallBase, *this,
  8890. UsedAssumedInformation,
  8891. /* CheckBBLivenessOnly */ true))
  8892. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  8893. for (CallBase *CB : ReachableCallBases) {
  8894. auto &CBEdges = A.getAAFor<AACallEdges>(
  8895. *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
  8896. if (!CBEdges.getState().isValidState())
  8897. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  8898. // TODO Check To backwards in this case.
  8899. if (CBEdges.hasUnknownCallee())
  8900. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  8901. for (Function *Fn : CBEdges.getOptimisticEdges()) {
  8902. if (Fn == RQI.To)
  8903. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  8904. if (!Visited->insert(Fn).second)
  8905. continue;
  8906. if (Fn->isDeclaration()) {
  8907. if (Fn->hasFnAttribute(Attribute::NoCallback))
  8908. continue;
  8909. // TODO Check To backwards in this case.
  8910. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  8911. }
  8912. const AAInterFnReachability *InterFnReachability = this;
  8913. if (Fn != getAnchorScope())
  8914. InterFnReachability = &A.getAAFor<AAInterFnReachability>(
  8915. *this, IRPosition::function(*Fn), DepClassTy::OPTIONAL);
  8916. const Instruction &FnFirstInst = Fn->getEntryBlock().front();
  8917. if (InterFnReachability->instructionCanReach(A, FnFirstInst, *RQI.To,
  8918. RQI.ExclusionSet, Visited))
  8919. return rememberResult(A, RQITy::Reachable::Yes, RQI);
  8920. }
  8921. }
  8922. return rememberResult(A, RQITy::Reachable::No, RQI);
  8923. }
  8924. void trackStatistics() const override {}
  8925. private:
  8926. SmallVector<RQITy *> QueryVector;
  8927. DenseSet<RQITy *> QueryCache;
  8928. };
  8929. } // namespace
  8930. template <typename AAType>
  8931. static std::optional<Constant *>
  8932. askForAssumedConstant(Attributor &A, const AbstractAttribute &QueryingAA,
  8933. const IRPosition &IRP, Type &Ty) {
  8934. if (!Ty.isIntegerTy())
  8935. return nullptr;
  8936. // This will also pass the call base context.
  8937. const auto &AA = A.getAAFor<AAType>(QueryingAA, IRP, DepClassTy::NONE);
  8938. std::optional<Constant *> COpt = AA.getAssumedConstant(A);
  8939. if (!COpt.has_value()) {
  8940. A.recordDependence(AA, QueryingAA, DepClassTy::OPTIONAL);
  8941. return std::nullopt;
  8942. }
  8943. if (auto *C = *COpt) {
  8944. A.recordDependence(AA, QueryingAA, DepClassTy::OPTIONAL);
  8945. return C;
  8946. }
  8947. return nullptr;
  8948. }
  8949. Value *AAPotentialValues::getSingleValue(
  8950. Attributor &A, const AbstractAttribute &AA, const IRPosition &IRP,
  8951. SmallVectorImpl<AA::ValueAndContext> &Values) {
  8952. Type &Ty = *IRP.getAssociatedType();
  8953. std::optional<Value *> V;
  8954. for (auto &It : Values) {
  8955. V = AA::combineOptionalValuesInAAValueLatice(V, It.getValue(), &Ty);
  8956. if (V.has_value() && !*V)
  8957. break;
  8958. }
  8959. if (!V.has_value())
  8960. return UndefValue::get(&Ty);
  8961. return *V;
  8962. }
  8963. namespace {
  8964. struct AAPotentialValuesImpl : AAPotentialValues {
  8965. using StateType = PotentialLLVMValuesState;
  8966. AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
  8967. : AAPotentialValues(IRP, A) {}
  8968. /// See AbstractAttribute::initialize(..).
  8969. void initialize(Attributor &A) override {
  8970. if (A.hasSimplificationCallback(getIRPosition())) {
  8971. indicatePessimisticFixpoint();
  8972. return;
  8973. }
  8974. Value *Stripped = getAssociatedValue().stripPointerCasts();
  8975. auto *CE = dyn_cast<ConstantExpr>(Stripped);
  8976. if (isa<Constant>(Stripped) &&
  8977. (!CE || CE->getOpcode() != Instruction::ICmp)) {
  8978. addValue(A, getState(), *Stripped, getCtxI(), AA::AnyScope,
  8979. getAnchorScope());
  8980. indicateOptimisticFixpoint();
  8981. return;
  8982. }
  8983. AAPotentialValues::initialize(A);
  8984. }
  8985. /// See AbstractAttribute::getAsStr().
  8986. const std::string getAsStr() const override {
  8987. std::string Str;
  8988. llvm::raw_string_ostream OS(Str);
  8989. OS << getState();
  8990. return OS.str();
  8991. }
  8992. template <typename AAType>
  8993. static std::optional<Value *> askOtherAA(Attributor &A,
  8994. const AbstractAttribute &AA,
  8995. const IRPosition &IRP, Type &Ty) {
  8996. if (isa<Constant>(IRP.getAssociatedValue()))
  8997. return &IRP.getAssociatedValue();
  8998. std::optional<Constant *> C = askForAssumedConstant<AAType>(A, AA, IRP, Ty);
  8999. if (!C)
  9000. return std::nullopt;
  9001. if (*C)
  9002. if (auto *CC = AA::getWithType(**C, Ty))
  9003. return CC;
  9004. return nullptr;
  9005. }
  9006. void addValue(Attributor &A, StateType &State, Value &V,
  9007. const Instruction *CtxI, AA::ValueScope S,
  9008. Function *AnchorScope) const {
  9009. IRPosition ValIRP = IRPosition::value(V);
  9010. if (auto *CB = dyn_cast_or_null<CallBase>(CtxI)) {
  9011. for (const auto &U : CB->args()) {
  9012. if (U.get() != &V)
  9013. continue;
  9014. ValIRP = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
  9015. break;
  9016. }
  9017. }
  9018. Value *VPtr = &V;
  9019. if (ValIRP.getAssociatedType()->isIntegerTy()) {
  9020. Type &Ty = *getAssociatedType();
  9021. std::optional<Value *> SimpleV =
  9022. askOtherAA<AAValueConstantRange>(A, *this, ValIRP, Ty);
  9023. if (SimpleV.has_value() && !*SimpleV) {
  9024. auto &PotentialConstantsAA = A.getAAFor<AAPotentialConstantValues>(
  9025. *this, ValIRP, DepClassTy::OPTIONAL);
  9026. if (PotentialConstantsAA.isValidState()) {
  9027. for (const auto &It : PotentialConstantsAA.getAssumedSet())
  9028. State.unionAssumed({{*ConstantInt::get(&Ty, It), nullptr}, S});
  9029. if (PotentialConstantsAA.undefIsContained())
  9030. State.unionAssumed({{*UndefValue::get(&Ty), nullptr}, S});
  9031. return;
  9032. }
  9033. }
  9034. if (!SimpleV.has_value())
  9035. return;
  9036. if (*SimpleV)
  9037. VPtr = *SimpleV;
  9038. }
  9039. if (isa<ConstantInt>(VPtr))
  9040. CtxI = nullptr;
  9041. if (!AA::isValidInScope(*VPtr, AnchorScope))
  9042. S = AA::ValueScope(S | AA::Interprocedural);
  9043. State.unionAssumed({{*VPtr, CtxI}, S});
  9044. }
  9045. /// Helper struct to tie a value+context pair together with the scope for
  9046. /// which this is the simplified version.
  9047. struct ItemInfo {
  9048. AA::ValueAndContext I;
  9049. AA::ValueScope S;
  9050. bool operator==(const ItemInfo &II) const {
  9051. return II.I == I && II.S == S;
  9052. };
  9053. bool operator<(const ItemInfo &II) const {
  9054. if (I == II.I)
  9055. return S < II.S;
  9056. return I < II.I;
  9057. };
  9058. };
  9059. bool recurseForValue(Attributor &A, const IRPosition &IRP, AA::ValueScope S) {
  9060. SmallMapVector<AA::ValueAndContext, int, 8> ValueScopeMap;
  9061. for (auto CS : {AA::Intraprocedural, AA::Interprocedural}) {
  9062. if (!(CS & S))
  9063. continue;
  9064. bool UsedAssumedInformation = false;
  9065. SmallVector<AA::ValueAndContext> Values;
  9066. if (!A.getAssumedSimplifiedValues(IRP, this, Values, CS,
  9067. UsedAssumedInformation))
  9068. return false;
  9069. for (auto &It : Values)
  9070. ValueScopeMap[It] += CS;
  9071. }
  9072. for (auto &It : ValueScopeMap)
  9073. addValue(A, getState(), *It.first.getValue(), It.first.getCtxI(),
  9074. AA::ValueScope(It.second), getAnchorScope());
  9075. return true;
  9076. }
  9077. void giveUpOnIntraprocedural(Attributor &A) {
  9078. auto NewS = StateType::getBestState(getState());
  9079. for (const auto &It : getAssumedSet()) {
  9080. if (It.second == AA::Intraprocedural)
  9081. continue;
  9082. addValue(A, NewS, *It.first.getValue(), It.first.getCtxI(),
  9083. AA::Interprocedural, getAnchorScope());
  9084. }
  9085. assert(!undefIsContained() && "Undef should be an explicit value!");
  9086. addValue(A, NewS, getAssociatedValue(), getCtxI(), AA::Intraprocedural,
  9087. getAnchorScope());
  9088. getState() = NewS;
  9089. }
  9090. /// See AbstractState::indicatePessimisticFixpoint(...).
  9091. ChangeStatus indicatePessimisticFixpoint() override {
  9092. getState() = StateType::getBestState(getState());
  9093. getState().unionAssumed({{getAssociatedValue(), getCtxI()}, AA::AnyScope});
  9094. AAPotentialValues::indicateOptimisticFixpoint();
  9095. return ChangeStatus::CHANGED;
  9096. }
  9097. /// See AbstractAttribute::updateImpl(...).
  9098. ChangeStatus updateImpl(Attributor &A) override {
  9099. return indicatePessimisticFixpoint();
  9100. }
  9101. /// See AbstractAttribute::manifest(...).
  9102. ChangeStatus manifest(Attributor &A) override {
  9103. SmallVector<AA::ValueAndContext> Values;
  9104. for (AA::ValueScope S : {AA::Interprocedural, AA::Intraprocedural}) {
  9105. Values.clear();
  9106. if (!getAssumedSimplifiedValues(A, Values, S))
  9107. continue;
  9108. Value &OldV = getAssociatedValue();
  9109. if (isa<UndefValue>(OldV))
  9110. continue;
  9111. Value *NewV = getSingleValue(A, *this, getIRPosition(), Values);
  9112. if (!NewV || NewV == &OldV)
  9113. continue;
  9114. if (getCtxI() &&
  9115. !AA::isValidAtPosition({*NewV, *getCtxI()}, A.getInfoCache()))
  9116. continue;
  9117. if (A.changeAfterManifest(getIRPosition(), *NewV))
  9118. return ChangeStatus::CHANGED;
  9119. }
  9120. return ChangeStatus::UNCHANGED;
  9121. }
  9122. bool getAssumedSimplifiedValues(Attributor &A,
  9123. SmallVectorImpl<AA::ValueAndContext> &Values,
  9124. AA::ValueScope S) const override {
  9125. if (!isValidState())
  9126. return false;
  9127. for (const auto &It : getAssumedSet())
  9128. if (It.second & S)
  9129. Values.push_back(It.first);
  9130. assert(!undefIsContained() && "Undef should be an explicit value!");
  9131. return true;
  9132. }
  9133. };
  9134. struct AAPotentialValuesFloating : AAPotentialValuesImpl {
  9135. AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
  9136. : AAPotentialValuesImpl(IRP, A) {}
  9137. /// See AbstractAttribute::updateImpl(...).
  9138. ChangeStatus updateImpl(Attributor &A) override {
  9139. auto AssumedBefore = getAssumed();
  9140. genericValueTraversal(A);
  9141. return (AssumedBefore == getAssumed()) ? ChangeStatus::UNCHANGED
  9142. : ChangeStatus::CHANGED;
  9143. }
  9144. /// Helper struct to remember which AAIsDead instances we actually used.
  9145. struct LivenessInfo {
  9146. const AAIsDead *LivenessAA = nullptr;
  9147. bool AnyDead = false;
  9148. };
  9149. /// Check if \p Cmp is a comparison we can simplify.
  9150. ///
  9151. /// We handle multiple cases, one in which at least one operand is an
  9152. /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
  9153. /// operand. Return true if successful, in that case Worklist will be updated.
  9154. bool handleCmp(Attributor &A, Value &Cmp, Value *LHS, Value *RHS,
  9155. CmpInst::Predicate Pred, ItemInfo II,
  9156. SmallVectorImpl<ItemInfo> &Worklist) {
  9157. // Simplify the operands first.
  9158. bool UsedAssumedInformation = false;
  9159. const auto &SimplifiedLHS = A.getAssumedSimplified(
  9160. IRPosition::value(*LHS, getCallBaseContext()), *this,
  9161. UsedAssumedInformation, AA::Intraprocedural);
  9162. if (!SimplifiedLHS.has_value())
  9163. return true;
  9164. if (!*SimplifiedLHS)
  9165. return false;
  9166. LHS = *SimplifiedLHS;
  9167. const auto &SimplifiedRHS = A.getAssumedSimplified(
  9168. IRPosition::value(*RHS, getCallBaseContext()), *this,
  9169. UsedAssumedInformation, AA::Intraprocedural);
  9170. if (!SimplifiedRHS.has_value())
  9171. return true;
  9172. if (!*SimplifiedRHS)
  9173. return false;
  9174. RHS = *SimplifiedRHS;
  9175. LLVMContext &Ctx = LHS->getContext();
  9176. // Handle the trivial case first in which we don't even need to think about
  9177. // null or non-null.
  9178. if (LHS == RHS &&
  9179. (CmpInst::isTrueWhenEqual(Pred) || CmpInst::isFalseWhenEqual(Pred))) {
  9180. Constant *NewV = ConstantInt::get(Type::getInt1Ty(Ctx),
  9181. CmpInst::isTrueWhenEqual(Pred));
  9182. addValue(A, getState(), *NewV, /* CtxI */ nullptr, II.S,
  9183. getAnchorScope());
  9184. return true;
  9185. }
  9186. // From now on we only handle equalities (==, !=).
  9187. if (!CmpInst::isEquality(Pred))
  9188. return false;
  9189. bool LHSIsNull = isa<ConstantPointerNull>(LHS);
  9190. bool RHSIsNull = isa<ConstantPointerNull>(RHS);
  9191. if (!LHSIsNull && !RHSIsNull)
  9192. return false;
  9193. // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
  9194. // non-nullptr operand and if we assume it's non-null we can conclude the
  9195. // result of the comparison.
  9196. assert((LHSIsNull || RHSIsNull) &&
  9197. "Expected nullptr versus non-nullptr comparison at this point");
  9198. // The index is the operand that we assume is not null.
  9199. unsigned PtrIdx = LHSIsNull;
  9200. auto &PtrNonNullAA = A.getAAFor<AANonNull>(
  9201. *this, IRPosition::value(*(PtrIdx ? RHS : LHS)), DepClassTy::REQUIRED);
  9202. if (!PtrNonNullAA.isAssumedNonNull())
  9203. return false;
  9204. // The new value depends on the predicate, true for != and false for ==.
  9205. Constant *NewV =
  9206. ConstantInt::get(Type::getInt1Ty(Ctx), Pred == CmpInst::ICMP_NE);
  9207. addValue(A, getState(), *NewV, /* CtxI */ nullptr, II.S, getAnchorScope());
  9208. return true;
  9209. }
  9210. bool handleSelectInst(Attributor &A, SelectInst &SI, ItemInfo II,
  9211. SmallVectorImpl<ItemInfo> &Worklist) {
  9212. const Instruction *CtxI = II.I.getCtxI();
  9213. bool UsedAssumedInformation = false;
  9214. std::optional<Constant *> C =
  9215. A.getAssumedConstant(*SI.getCondition(), *this, UsedAssumedInformation);
  9216. bool NoValueYet = !C.has_value();
  9217. if (NoValueYet || isa_and_nonnull<UndefValue>(*C))
  9218. return true;
  9219. if (auto *CI = dyn_cast_or_null<ConstantInt>(*C)) {
  9220. if (CI->isZero())
  9221. Worklist.push_back({{*SI.getFalseValue(), CtxI}, II.S});
  9222. else
  9223. Worklist.push_back({{*SI.getTrueValue(), CtxI}, II.S});
  9224. } else if (&SI == &getAssociatedValue()) {
  9225. // We could not simplify the condition, assume both values.
  9226. Worklist.push_back({{*SI.getTrueValue(), CtxI}, II.S});
  9227. Worklist.push_back({{*SI.getFalseValue(), CtxI}, II.S});
  9228. } else {
  9229. std::optional<Value *> SimpleV = A.getAssumedSimplified(
  9230. IRPosition::inst(SI), *this, UsedAssumedInformation, II.S);
  9231. if (!SimpleV.has_value())
  9232. return true;
  9233. if (*SimpleV) {
  9234. addValue(A, getState(), **SimpleV, CtxI, II.S, getAnchorScope());
  9235. return true;
  9236. }
  9237. return false;
  9238. }
  9239. return true;
  9240. }
  9241. bool handleLoadInst(Attributor &A, LoadInst &LI, ItemInfo II,
  9242. SmallVectorImpl<ItemInfo> &Worklist) {
  9243. SmallSetVector<Value *, 4> PotentialCopies;
  9244. SmallSetVector<Instruction *, 4> PotentialValueOrigins;
  9245. bool UsedAssumedInformation = false;
  9246. if (!AA::getPotentiallyLoadedValues(A, LI, PotentialCopies,
  9247. PotentialValueOrigins, *this,
  9248. UsedAssumedInformation,
  9249. /* OnlyExact */ true)) {
  9250. LLVM_DEBUG(dbgs() << "[AAPotentialValues] Failed to get potentially "
  9251. "loaded values for load instruction "
  9252. << LI << "\n");
  9253. return false;
  9254. }
  9255. // Do not simplify loads that are only used in llvm.assume if we cannot also
  9256. // remove all stores that may feed into the load. The reason is that the
  9257. // assume is probably worth something as long as the stores are around.
  9258. InformationCache &InfoCache = A.getInfoCache();
  9259. if (InfoCache.isOnlyUsedByAssume(LI)) {
  9260. if (!llvm::all_of(PotentialValueOrigins, [&](Instruction *I) {
  9261. if (!I)
  9262. return true;
  9263. if (auto *SI = dyn_cast<StoreInst>(I))
  9264. return A.isAssumedDead(SI->getOperandUse(0), this,
  9265. /* LivenessAA */ nullptr,
  9266. UsedAssumedInformation,
  9267. /* CheckBBLivenessOnly */ false);
  9268. return A.isAssumedDead(*I, this, /* LivenessAA */ nullptr,
  9269. UsedAssumedInformation,
  9270. /* CheckBBLivenessOnly */ false);
  9271. })) {
  9272. LLVM_DEBUG(dbgs() << "[AAPotentialValues] Load is onl used by assumes "
  9273. "and we cannot delete all the stores: "
  9274. << LI << "\n");
  9275. return false;
  9276. }
  9277. }
  9278. // Values have to be dynamically unique or we loose the fact that a
  9279. // single llvm::Value might represent two runtime values (e.g.,
  9280. // stack locations in different recursive calls).
  9281. const Instruction *CtxI = II.I.getCtxI();
  9282. bool ScopeIsLocal = (II.S & AA::Intraprocedural);
  9283. bool AllLocal = ScopeIsLocal;
  9284. bool DynamicallyUnique = llvm::all_of(PotentialCopies, [&](Value *PC) {
  9285. AllLocal &= AA::isValidInScope(*PC, getAnchorScope());
  9286. return AA::isDynamicallyUnique(A, *this, *PC);
  9287. });
  9288. if (!DynamicallyUnique) {
  9289. LLVM_DEBUG(dbgs() << "[AAPotentialValues] Not all potentially loaded "
  9290. "values are dynamically unique: "
  9291. << LI << "\n");
  9292. return false;
  9293. }
  9294. for (auto *PotentialCopy : PotentialCopies) {
  9295. if (AllLocal) {
  9296. Worklist.push_back({{*PotentialCopy, CtxI}, II.S});
  9297. } else {
  9298. Worklist.push_back({{*PotentialCopy, CtxI}, AA::Interprocedural});
  9299. }
  9300. }
  9301. if (!AllLocal && ScopeIsLocal)
  9302. addValue(A, getState(), LI, CtxI, AA::Intraprocedural, getAnchorScope());
  9303. return true;
  9304. }
  9305. bool handlePHINode(
  9306. Attributor &A, PHINode &PHI, ItemInfo II,
  9307. SmallVectorImpl<ItemInfo> &Worklist,
  9308. SmallMapVector<const Function *, LivenessInfo, 4> &LivenessAAs) {
  9309. auto GetLivenessInfo = [&](const Function &F) -> LivenessInfo & {
  9310. LivenessInfo &LI = LivenessAAs[&F];
  9311. if (!LI.LivenessAA)
  9312. LI.LivenessAA = &A.getAAFor<AAIsDead>(*this, IRPosition::function(F),
  9313. DepClassTy::NONE);
  9314. return LI;
  9315. };
  9316. if (&PHI == &getAssociatedValue()) {
  9317. LivenessInfo &LI = GetLivenessInfo(*PHI.getFunction());
  9318. for (unsigned u = 0, e = PHI.getNumIncomingValues(); u < e; u++) {
  9319. BasicBlock *IncomingBB = PHI.getIncomingBlock(u);
  9320. if (LI.LivenessAA->isEdgeDead(IncomingBB, PHI.getParent())) {
  9321. LI.AnyDead = true;
  9322. continue;
  9323. }
  9324. Worklist.push_back(
  9325. {{*PHI.getIncomingValue(u), IncomingBB->getTerminator()}, II.S});
  9326. }
  9327. return true;
  9328. }
  9329. bool UsedAssumedInformation = false;
  9330. std::optional<Value *> SimpleV = A.getAssumedSimplified(
  9331. IRPosition::inst(PHI), *this, UsedAssumedInformation, II.S);
  9332. if (!SimpleV.has_value())
  9333. return true;
  9334. if (!(*SimpleV))
  9335. return false;
  9336. addValue(A, getState(), **SimpleV, &PHI, II.S, getAnchorScope());
  9337. return true;
  9338. }
  9339. /// Use the generic, non-optimistic InstSimplfy functionality if we managed to
  9340. /// simplify any operand of the instruction \p I. Return true if successful,
  9341. /// in that case Worklist will be updated.
  9342. bool handleGenericInst(Attributor &A, Instruction &I, ItemInfo II,
  9343. SmallVectorImpl<ItemInfo> &Worklist) {
  9344. bool SomeSimplified = false;
  9345. bool UsedAssumedInformation = false;
  9346. SmallVector<Value *, 8> NewOps(I.getNumOperands());
  9347. int Idx = 0;
  9348. for (Value *Op : I.operands()) {
  9349. const auto &SimplifiedOp = A.getAssumedSimplified(
  9350. IRPosition::value(*Op, getCallBaseContext()), *this,
  9351. UsedAssumedInformation, AA::Intraprocedural);
  9352. // If we are not sure about any operand we are not sure about the entire
  9353. // instruction, we'll wait.
  9354. if (!SimplifiedOp.has_value())
  9355. return true;
  9356. if (*SimplifiedOp)
  9357. NewOps[Idx] = *SimplifiedOp;
  9358. else
  9359. NewOps[Idx] = Op;
  9360. SomeSimplified |= (NewOps[Idx] != Op);
  9361. ++Idx;
  9362. }
  9363. // We won't bother with the InstSimplify interface if we didn't simplify any
  9364. // operand ourselves.
  9365. if (!SomeSimplified)
  9366. return false;
  9367. InformationCache &InfoCache = A.getInfoCache();
  9368. Function *F = I.getFunction();
  9369. const auto *DT =
  9370. InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
  9371. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  9372. auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
  9373. OptimizationRemarkEmitter *ORE = nullptr;
  9374. const DataLayout &DL = I.getModule()->getDataLayout();
  9375. SimplifyQuery Q(DL, TLI, DT, AC, &I);
  9376. Value *NewV = simplifyInstructionWithOperands(&I, NewOps, Q, ORE);
  9377. if (!NewV || NewV == &I)
  9378. return false;
  9379. LLVM_DEBUG(dbgs() << "Generic inst " << I << " assumed simplified to "
  9380. << *NewV << "\n");
  9381. Worklist.push_back({{*NewV, II.I.getCtxI()}, II.S});
  9382. return true;
  9383. }
  9384. bool simplifyInstruction(
  9385. Attributor &A, Instruction &I, ItemInfo II,
  9386. SmallVectorImpl<ItemInfo> &Worklist,
  9387. SmallMapVector<const Function *, LivenessInfo, 4> &LivenessAAs) {
  9388. if (auto *CI = dyn_cast<CmpInst>(&I))
  9389. if (handleCmp(A, *CI, CI->getOperand(0), CI->getOperand(1),
  9390. CI->getPredicate(), II, Worklist))
  9391. return true;
  9392. switch (I.getOpcode()) {
  9393. case Instruction::Select:
  9394. return handleSelectInst(A, cast<SelectInst>(I), II, Worklist);
  9395. case Instruction::PHI:
  9396. return handlePHINode(A, cast<PHINode>(I), II, Worklist, LivenessAAs);
  9397. case Instruction::Load:
  9398. return handleLoadInst(A, cast<LoadInst>(I), II, Worklist);
  9399. default:
  9400. return handleGenericInst(A, I, II, Worklist);
  9401. };
  9402. return false;
  9403. }
  9404. void genericValueTraversal(Attributor &A) {
  9405. SmallMapVector<const Function *, LivenessInfo, 4> LivenessAAs;
  9406. Value *InitialV = &getAssociatedValue();
  9407. SmallSet<ItemInfo, 16> Visited;
  9408. SmallVector<ItemInfo, 16> Worklist;
  9409. Worklist.push_back({{*InitialV, getCtxI()}, AA::AnyScope});
  9410. int Iteration = 0;
  9411. do {
  9412. ItemInfo II = Worklist.pop_back_val();
  9413. Value *V = II.I.getValue();
  9414. assert(V);
  9415. const Instruction *CtxI = II.I.getCtxI();
  9416. AA::ValueScope S = II.S;
  9417. // Check if we should process the current value. To prevent endless
  9418. // recursion keep a record of the values we followed!
  9419. if (!Visited.insert(II).second)
  9420. continue;
  9421. // Make sure we limit the compile time for complex expressions.
  9422. if (Iteration++ >= MaxPotentialValuesIterations) {
  9423. LLVM_DEBUG(dbgs() << "Generic value traversal reached iteration limit: "
  9424. << Iteration << "!\n");
  9425. addValue(A, getState(), *V, CtxI, S, getAnchorScope());
  9426. continue;
  9427. }
  9428. // Explicitly look through calls with a "returned" attribute if we do
  9429. // not have a pointer as stripPointerCasts only works on them.
  9430. Value *NewV = nullptr;
  9431. if (V->getType()->isPointerTy()) {
  9432. NewV = AA::getWithType(*V->stripPointerCasts(), *V->getType());
  9433. } else {
  9434. auto *CB = dyn_cast<CallBase>(V);
  9435. if (CB && CB->getCalledFunction()) {
  9436. for (Argument &Arg : CB->getCalledFunction()->args())
  9437. if (Arg.hasReturnedAttr()) {
  9438. NewV = CB->getArgOperand(Arg.getArgNo());
  9439. break;
  9440. }
  9441. }
  9442. }
  9443. if (NewV && NewV != V) {
  9444. Worklist.push_back({{*NewV, CtxI}, S});
  9445. continue;
  9446. }
  9447. if (auto *CE = dyn_cast<ConstantExpr>(V)) {
  9448. if (CE->getOpcode() == Instruction::ICmp)
  9449. if (handleCmp(A, *CE, CE->getOperand(0), CE->getOperand(1),
  9450. CmpInst::Predicate(CE->getPredicate()), II, Worklist))
  9451. continue;
  9452. }
  9453. if (auto *I = dyn_cast<Instruction>(V)) {
  9454. if (simplifyInstruction(A, *I, II, Worklist, LivenessAAs))
  9455. continue;
  9456. }
  9457. if (V != InitialV || isa<Argument>(V))
  9458. if (recurseForValue(A, IRPosition::value(*V), II.S))
  9459. continue;
  9460. // If we haven't stripped anything we give up.
  9461. if (V == InitialV && CtxI == getCtxI()) {
  9462. indicatePessimisticFixpoint();
  9463. return;
  9464. }
  9465. addValue(A, getState(), *V, CtxI, S, getAnchorScope());
  9466. } while (!Worklist.empty());
  9467. // If we actually used liveness information so we have to record a
  9468. // dependence.
  9469. for (auto &It : LivenessAAs)
  9470. if (It.second.AnyDead)
  9471. A.recordDependence(*It.second.LivenessAA, *this, DepClassTy::OPTIONAL);
  9472. }
  9473. /// See AbstractAttribute::trackStatistics()
  9474. void trackStatistics() const override {
  9475. STATS_DECLTRACK_FLOATING_ATTR(potential_values)
  9476. }
  9477. };
  9478. struct AAPotentialValuesArgument final : AAPotentialValuesImpl {
  9479. using Base = AAPotentialValuesImpl;
  9480. AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
  9481. : Base(IRP, A) {}
  9482. /// See AbstractAttribute::initialize(..).
  9483. void initialize(Attributor &A) override {
  9484. auto &Arg = cast<Argument>(getAssociatedValue());
  9485. if (Arg.hasPointeeInMemoryValueAttr())
  9486. indicatePessimisticFixpoint();
  9487. }
  9488. /// See AbstractAttribute::updateImpl(...).
  9489. ChangeStatus updateImpl(Attributor &A) override {
  9490. auto AssumedBefore = getAssumed();
  9491. unsigned CSArgNo = getCallSiteArgNo();
  9492. bool UsedAssumedInformation = false;
  9493. SmallVector<AA::ValueAndContext> Values;
  9494. auto CallSitePred = [&](AbstractCallSite ACS) {
  9495. const auto CSArgIRP = IRPosition::callsite_argument(ACS, CSArgNo);
  9496. if (CSArgIRP.getPositionKind() == IRP_INVALID)
  9497. return false;
  9498. if (!A.getAssumedSimplifiedValues(CSArgIRP, this, Values,
  9499. AA::Interprocedural,
  9500. UsedAssumedInformation))
  9501. return false;
  9502. return isValidState();
  9503. };
  9504. if (!A.checkForAllCallSites(CallSitePred, *this,
  9505. /* RequireAllCallSites */ true,
  9506. UsedAssumedInformation))
  9507. return indicatePessimisticFixpoint();
  9508. Function *Fn = getAssociatedFunction();
  9509. bool AnyNonLocal = false;
  9510. for (auto &It : Values) {
  9511. if (isa<Constant>(It.getValue())) {
  9512. addValue(A, getState(), *It.getValue(), It.getCtxI(), AA::AnyScope,
  9513. getAnchorScope());
  9514. continue;
  9515. }
  9516. if (!AA::isDynamicallyUnique(A, *this, *It.getValue()))
  9517. return indicatePessimisticFixpoint();
  9518. if (auto *Arg = dyn_cast<Argument>(It.getValue()))
  9519. if (Arg->getParent() == Fn) {
  9520. addValue(A, getState(), *It.getValue(), It.getCtxI(), AA::AnyScope,
  9521. getAnchorScope());
  9522. continue;
  9523. }
  9524. addValue(A, getState(), *It.getValue(), It.getCtxI(), AA::Interprocedural,
  9525. getAnchorScope());
  9526. AnyNonLocal = true;
  9527. }
  9528. assert(!undefIsContained() && "Undef should be an explicit value!");
  9529. if (AnyNonLocal)
  9530. giveUpOnIntraprocedural(A);
  9531. return (AssumedBefore == getAssumed()) ? ChangeStatus::UNCHANGED
  9532. : ChangeStatus::CHANGED;
  9533. }
  9534. /// See AbstractAttribute::trackStatistics()
  9535. void trackStatistics() const override {
  9536. STATS_DECLTRACK_ARG_ATTR(potential_values)
  9537. }
  9538. };
  9539. struct AAPotentialValuesReturned
  9540. : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
  9541. using Base =
  9542. AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
  9543. AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
  9544. : Base(IRP, A) {}
  9545. /// See AbstractAttribute::initialize(..).
  9546. void initialize(Attributor &A) override {
  9547. if (A.hasSimplificationCallback(getIRPosition()))
  9548. indicatePessimisticFixpoint();
  9549. else
  9550. AAPotentialValues::initialize(A);
  9551. }
  9552. ChangeStatus manifest(Attributor &A) override {
  9553. // We queried AAValueSimplify for the returned values so they will be
  9554. // replaced if a simplified form was found. Nothing to do here.
  9555. return ChangeStatus::UNCHANGED;
  9556. }
  9557. ChangeStatus indicatePessimisticFixpoint() override {
  9558. return AAPotentialValues::indicatePessimisticFixpoint();
  9559. }
  9560. /// See AbstractAttribute::trackStatistics()
  9561. void trackStatistics() const override {
  9562. STATS_DECLTRACK_FNRET_ATTR(potential_values)
  9563. }
  9564. };
  9565. struct AAPotentialValuesFunction : AAPotentialValuesImpl {
  9566. AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
  9567. : AAPotentialValuesImpl(IRP, A) {}
  9568. /// See AbstractAttribute::updateImpl(...).
  9569. ChangeStatus updateImpl(Attributor &A) override {
  9570. llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "
  9571. "not be called");
  9572. }
  9573. /// See AbstractAttribute::trackStatistics()
  9574. void trackStatistics() const override {
  9575. STATS_DECLTRACK_FN_ATTR(potential_values)
  9576. }
  9577. };
  9578. struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
  9579. AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
  9580. : AAPotentialValuesFunction(IRP, A) {}
  9581. /// See AbstractAttribute::trackStatistics()
  9582. void trackStatistics() const override {
  9583. STATS_DECLTRACK_CS_ATTR(potential_values)
  9584. }
  9585. };
  9586. struct AAPotentialValuesCallSiteReturned : AAPotentialValuesImpl {
  9587. AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
  9588. : AAPotentialValuesImpl(IRP, A) {}
  9589. /// See AbstractAttribute::updateImpl(...).
  9590. ChangeStatus updateImpl(Attributor &A) override {
  9591. auto AssumedBefore = getAssumed();
  9592. Function *Callee = getAssociatedFunction();
  9593. if (!Callee)
  9594. return indicatePessimisticFixpoint();
  9595. bool UsedAssumedInformation = false;
  9596. auto *CB = cast<CallBase>(getCtxI());
  9597. if (CB->isMustTailCall() &&
  9598. !A.isAssumedDead(IRPosition::inst(*CB), this, nullptr,
  9599. UsedAssumedInformation))
  9600. return indicatePessimisticFixpoint();
  9601. SmallVector<AA::ValueAndContext> Values;
  9602. if (!A.getAssumedSimplifiedValues(IRPosition::returned(*Callee), this,
  9603. Values, AA::Intraprocedural,
  9604. UsedAssumedInformation))
  9605. return indicatePessimisticFixpoint();
  9606. Function *Caller = CB->getCaller();
  9607. bool AnyNonLocal = false;
  9608. for (auto &It : Values) {
  9609. Value *V = It.getValue();
  9610. std::optional<Value *> CallerV = A.translateArgumentToCallSiteContent(
  9611. V, *CB, *this, UsedAssumedInformation);
  9612. if (!CallerV.has_value()) {
  9613. // Nothing to do as long as no value was determined.
  9614. continue;
  9615. }
  9616. V = *CallerV ? *CallerV : V;
  9617. if (AA::isDynamicallyUnique(A, *this, *V) &&
  9618. AA::isValidInScope(*V, Caller)) {
  9619. if (*CallerV) {
  9620. SmallVector<AA::ValueAndContext> ArgValues;
  9621. IRPosition IRP = IRPosition::value(*V);
  9622. if (auto *Arg = dyn_cast<Argument>(V))
  9623. if (Arg->getParent() == CB->getCalledFunction())
  9624. IRP = IRPosition::callsite_argument(*CB, Arg->getArgNo());
  9625. if (recurseForValue(A, IRP, AA::AnyScope))
  9626. continue;
  9627. }
  9628. addValue(A, getState(), *V, CB, AA::AnyScope, getAnchorScope());
  9629. } else {
  9630. AnyNonLocal = true;
  9631. break;
  9632. }
  9633. }
  9634. if (AnyNonLocal) {
  9635. Values.clear();
  9636. if (!A.getAssumedSimplifiedValues(IRPosition::returned(*Callee), this,
  9637. Values, AA::Interprocedural,
  9638. UsedAssumedInformation))
  9639. return indicatePessimisticFixpoint();
  9640. AnyNonLocal = false;
  9641. getState() = PotentialLLVMValuesState::getBestState();
  9642. for (auto &It : Values) {
  9643. Value *V = It.getValue();
  9644. if (!AA::isDynamicallyUnique(A, *this, *V))
  9645. return indicatePessimisticFixpoint();
  9646. if (AA::isValidInScope(*V, Caller)) {
  9647. addValue(A, getState(), *V, CB, AA::AnyScope, getAnchorScope());
  9648. } else {
  9649. AnyNonLocal = true;
  9650. addValue(A, getState(), *V, CB, AA::Interprocedural,
  9651. getAnchorScope());
  9652. }
  9653. }
  9654. if (AnyNonLocal)
  9655. giveUpOnIntraprocedural(A);
  9656. }
  9657. return (AssumedBefore == getAssumed()) ? ChangeStatus::UNCHANGED
  9658. : ChangeStatus::CHANGED;
  9659. }
  9660. ChangeStatus indicatePessimisticFixpoint() override {
  9661. return AAPotentialValues::indicatePessimisticFixpoint();
  9662. }
  9663. /// See AbstractAttribute::trackStatistics()
  9664. void trackStatistics() const override {
  9665. STATS_DECLTRACK_CSRET_ATTR(potential_values)
  9666. }
  9667. };
  9668. struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
  9669. AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
  9670. : AAPotentialValuesFloating(IRP, A) {}
  9671. /// See AbstractAttribute::trackStatistics()
  9672. void trackStatistics() const override {
  9673. STATS_DECLTRACK_CSARG_ATTR(potential_values)
  9674. }
  9675. };
  9676. } // namespace
  9677. /// ---------------------- Assumption Propagation ------------------------------
  9678. namespace {
  9679. struct AAAssumptionInfoImpl : public AAAssumptionInfo {
  9680. AAAssumptionInfoImpl(const IRPosition &IRP, Attributor &A,
  9681. const DenseSet<StringRef> &Known)
  9682. : AAAssumptionInfo(IRP, A, Known) {}
  9683. bool hasAssumption(const StringRef Assumption) const override {
  9684. return isValidState() && setContains(Assumption);
  9685. }
  9686. /// See AbstractAttribute::getAsStr()
  9687. const std::string getAsStr() const override {
  9688. const SetContents &Known = getKnown();
  9689. const SetContents &Assumed = getAssumed();
  9690. const std::string KnownStr =
  9691. llvm::join(Known.getSet().begin(), Known.getSet().end(), ",");
  9692. const std::string AssumedStr =
  9693. (Assumed.isUniversal())
  9694. ? "Universal"
  9695. : llvm::join(Assumed.getSet().begin(), Assumed.getSet().end(), ",");
  9696. return "Known [" + KnownStr + "]," + " Assumed [" + AssumedStr + "]";
  9697. }
  9698. };
  9699. /// Propagates assumption information from parent functions to all of their
  9700. /// successors. An assumption can be propagated if the containing function
  9701. /// dominates the called function.
  9702. ///
  9703. /// We start with a "known" set of assumptions already valid for the associated
  9704. /// function and an "assumed" set that initially contains all possible
  9705. /// assumptions. The assumed set is inter-procedurally updated by narrowing its
  9706. /// contents as concrete values are known. The concrete values are seeded by the
  9707. /// first nodes that are either entries into the call graph, or contains no
  9708. /// assumptions. Each node is updated as the intersection of the assumed state
  9709. /// with all of its predecessors.
  9710. struct AAAssumptionInfoFunction final : AAAssumptionInfoImpl {
  9711. AAAssumptionInfoFunction(const IRPosition &IRP, Attributor &A)
  9712. : AAAssumptionInfoImpl(IRP, A,
  9713. getAssumptions(*IRP.getAssociatedFunction())) {}
  9714. /// See AbstractAttribute::manifest(...).
  9715. ChangeStatus manifest(Attributor &A) override {
  9716. const auto &Assumptions = getKnown();
  9717. // Don't manifest a universal set if it somehow made it here.
  9718. if (Assumptions.isUniversal())
  9719. return ChangeStatus::UNCHANGED;
  9720. Function *AssociatedFunction = getAssociatedFunction();
  9721. bool Changed = addAssumptions(*AssociatedFunction, Assumptions.getSet());
  9722. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  9723. }
  9724. /// See AbstractAttribute::updateImpl(...).
  9725. ChangeStatus updateImpl(Attributor &A) override {
  9726. bool Changed = false;
  9727. auto CallSitePred = [&](AbstractCallSite ACS) {
  9728. const auto &AssumptionAA = A.getAAFor<AAAssumptionInfo>(
  9729. *this, IRPosition::callsite_function(*ACS.getInstruction()),
  9730. DepClassTy::REQUIRED);
  9731. // Get the set of assumptions shared by all of this function's callers.
  9732. Changed |= getIntersection(AssumptionAA.getAssumed());
  9733. return !getAssumed().empty() || !getKnown().empty();
  9734. };
  9735. bool UsedAssumedInformation = false;
  9736. // Get the intersection of all assumptions held by this node's predecessors.
  9737. // If we don't know all the call sites then this is either an entry into the
  9738. // call graph or an empty node. This node is known to only contain its own
  9739. // assumptions and can be propagated to its successors.
  9740. if (!A.checkForAllCallSites(CallSitePred, *this, true,
  9741. UsedAssumedInformation))
  9742. return indicatePessimisticFixpoint();
  9743. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  9744. }
  9745. void trackStatistics() const override {}
  9746. };
  9747. /// Assumption Info defined for call sites.
  9748. struct AAAssumptionInfoCallSite final : AAAssumptionInfoImpl {
  9749. AAAssumptionInfoCallSite(const IRPosition &IRP, Attributor &A)
  9750. : AAAssumptionInfoImpl(IRP, A, getInitialAssumptions(IRP)) {}
  9751. /// See AbstractAttribute::initialize(...).
  9752. void initialize(Attributor &A) override {
  9753. const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
  9754. A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
  9755. }
  9756. /// See AbstractAttribute::manifest(...).
  9757. ChangeStatus manifest(Attributor &A) override {
  9758. // Don't manifest a universal set if it somehow made it here.
  9759. if (getKnown().isUniversal())
  9760. return ChangeStatus::UNCHANGED;
  9761. CallBase &AssociatedCall = cast<CallBase>(getAssociatedValue());
  9762. bool Changed = addAssumptions(AssociatedCall, getAssumed().getSet());
  9763. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  9764. }
  9765. /// See AbstractAttribute::updateImpl(...).
  9766. ChangeStatus updateImpl(Attributor &A) override {
  9767. const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
  9768. auto &AssumptionAA =
  9769. A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
  9770. bool Changed = getIntersection(AssumptionAA.getAssumed());
  9771. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  9772. }
  9773. /// See AbstractAttribute::trackStatistics()
  9774. void trackStatistics() const override {}
  9775. private:
  9776. /// Helper to initialized the known set as all the assumptions this call and
  9777. /// the callee contain.
  9778. DenseSet<StringRef> getInitialAssumptions(const IRPosition &IRP) {
  9779. const CallBase &CB = cast<CallBase>(IRP.getAssociatedValue());
  9780. auto Assumptions = getAssumptions(CB);
  9781. if (const Function *F = CB.getCaller())
  9782. set_union(Assumptions, getAssumptions(*F));
  9783. if (Function *F = IRP.getAssociatedFunction())
  9784. set_union(Assumptions, getAssumptions(*F));
  9785. return Assumptions;
  9786. }
  9787. };
  9788. } // namespace
  9789. AACallGraphNode *AACallEdgeIterator::operator*() const {
  9790. return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>(
  9791. &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I))));
  9792. }
  9793. void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); }
  9794. /// ------------------------ UnderlyingObjects ---------------------------------
  9795. namespace {
  9796. struct AAUnderlyingObjectsImpl
  9797. : StateWrapper<BooleanState, AAUnderlyingObjects> {
  9798. using BaseTy = StateWrapper<BooleanState, AAUnderlyingObjects>;
  9799. AAUnderlyingObjectsImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {}
  9800. /// See AbstractAttribute::getAsStr().
  9801. const std::string getAsStr() const override {
  9802. return std::string("UnderlyingObjects ") +
  9803. (isValidState()
  9804. ? (std::string("inter #") +
  9805. std::to_string(InterAssumedUnderlyingObjects.size()) +
  9806. " objs" + std::string(", intra #") +
  9807. std::to_string(IntraAssumedUnderlyingObjects.size()) +
  9808. " objs")
  9809. : "<invalid>");
  9810. }
  9811. /// See AbstractAttribute::trackStatistics()
  9812. void trackStatistics() const override {}
  9813. /// See AbstractAttribute::updateImpl(...).
  9814. ChangeStatus updateImpl(Attributor &A) override {
  9815. auto &Ptr = getAssociatedValue();
  9816. auto DoUpdate = [&](SmallSetVector<Value *, 8> &UnderlyingObjects,
  9817. AA::ValueScope Scope) {
  9818. bool UsedAssumedInformation = false;
  9819. SmallPtrSet<Value *, 8> SeenObjects;
  9820. SmallVector<AA::ValueAndContext> Values;
  9821. if (!A.getAssumedSimplifiedValues(IRPosition::value(Ptr), *this, Values,
  9822. Scope, UsedAssumedInformation))
  9823. return UnderlyingObjects.insert(&Ptr);
  9824. bool Changed = false;
  9825. for (unsigned I = 0; I < Values.size(); ++I) {
  9826. auto &VAC = Values[I];
  9827. auto *Obj = VAC.getValue();
  9828. Value *UO = getUnderlyingObject(Obj);
  9829. if (UO && UO != VAC.getValue() && SeenObjects.insert(UO).second) {
  9830. const auto &OtherAA = A.getAAFor<AAUnderlyingObjects>(
  9831. *this, IRPosition::value(*UO), DepClassTy::OPTIONAL);
  9832. auto Pred = [&Values](Value &V) {
  9833. Values.emplace_back(V, nullptr);
  9834. return true;
  9835. };
  9836. if (!OtherAA.forallUnderlyingObjects(Pred, Scope))
  9837. llvm_unreachable(
  9838. "The forall call should not return false at this position");
  9839. continue;
  9840. }
  9841. if (isa<SelectInst>(Obj) || isa<PHINode>(Obj)) {
  9842. Changed |= handleIndirect(A, *Obj, UnderlyingObjects, Scope);
  9843. continue;
  9844. }
  9845. Changed |= UnderlyingObjects.insert(Obj);
  9846. }
  9847. return Changed;
  9848. };
  9849. bool Changed = false;
  9850. Changed |= DoUpdate(IntraAssumedUnderlyingObjects, AA::Intraprocedural);
  9851. Changed |= DoUpdate(InterAssumedUnderlyingObjects, AA::Interprocedural);
  9852. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  9853. }
  9854. bool forallUnderlyingObjects(
  9855. function_ref<bool(Value &)> Pred,
  9856. AA::ValueScope Scope = AA::Interprocedural) const override {
  9857. if (!isValidState())
  9858. return Pred(getAssociatedValue());
  9859. auto &AssumedUnderlyingObjects = Scope == AA::Intraprocedural
  9860. ? IntraAssumedUnderlyingObjects
  9861. : InterAssumedUnderlyingObjects;
  9862. for (Value *Obj : AssumedUnderlyingObjects)
  9863. if (!Pred(*Obj))
  9864. return false;
  9865. return true;
  9866. }
  9867. private:
  9868. /// Handle the case where the value is not the actual underlying value, such
  9869. /// as a phi node or a select instruction.
  9870. bool handleIndirect(Attributor &A, Value &V,
  9871. SmallSetVector<Value *, 8> &UnderlyingObjects,
  9872. AA::ValueScope Scope) {
  9873. bool Changed = false;
  9874. const auto &AA = A.getAAFor<AAUnderlyingObjects>(
  9875. *this, IRPosition::value(V), DepClassTy::OPTIONAL);
  9876. auto Pred = [&](Value &V) {
  9877. Changed |= UnderlyingObjects.insert(&V);
  9878. return true;
  9879. };
  9880. if (!AA.forallUnderlyingObjects(Pred, Scope))
  9881. llvm_unreachable(
  9882. "The forall call should not return false at this position");
  9883. return Changed;
  9884. }
  9885. /// All the underlying objects collected so far via intra procedural scope.
  9886. SmallSetVector<Value *, 8> IntraAssumedUnderlyingObjects;
  9887. /// All the underlying objects collected so far via inter procedural scope.
  9888. SmallSetVector<Value *, 8> InterAssumedUnderlyingObjects;
  9889. };
  9890. struct AAUnderlyingObjectsFloating final : AAUnderlyingObjectsImpl {
  9891. AAUnderlyingObjectsFloating(const IRPosition &IRP, Attributor &A)
  9892. : AAUnderlyingObjectsImpl(IRP, A) {}
  9893. };
  9894. struct AAUnderlyingObjectsArgument final : AAUnderlyingObjectsImpl {
  9895. AAUnderlyingObjectsArgument(const IRPosition &IRP, Attributor &A)
  9896. : AAUnderlyingObjectsImpl(IRP, A) {}
  9897. };
  9898. struct AAUnderlyingObjectsCallSite final : AAUnderlyingObjectsImpl {
  9899. AAUnderlyingObjectsCallSite(const IRPosition &IRP, Attributor &A)
  9900. : AAUnderlyingObjectsImpl(IRP, A) {}
  9901. };
  9902. struct AAUnderlyingObjectsCallSiteArgument final : AAUnderlyingObjectsImpl {
  9903. AAUnderlyingObjectsCallSiteArgument(const IRPosition &IRP, Attributor &A)
  9904. : AAUnderlyingObjectsImpl(IRP, A) {}
  9905. };
  9906. struct AAUnderlyingObjectsReturned final : AAUnderlyingObjectsImpl {
  9907. AAUnderlyingObjectsReturned(const IRPosition &IRP, Attributor &A)
  9908. : AAUnderlyingObjectsImpl(IRP, A) {}
  9909. };
  9910. struct AAUnderlyingObjectsCallSiteReturned final : AAUnderlyingObjectsImpl {
  9911. AAUnderlyingObjectsCallSiteReturned(const IRPosition &IRP, Attributor &A)
  9912. : AAUnderlyingObjectsImpl(IRP, A) {}
  9913. };
  9914. struct AAUnderlyingObjectsFunction final : AAUnderlyingObjectsImpl {
  9915. AAUnderlyingObjectsFunction(const IRPosition &IRP, Attributor &A)
  9916. : AAUnderlyingObjectsImpl(IRP, A) {}
  9917. };
  9918. }
  9919. const char AAReturnedValues::ID = 0;
  9920. const char AANoUnwind::ID = 0;
  9921. const char AANoSync::ID = 0;
  9922. const char AANoFree::ID = 0;
  9923. const char AANonNull::ID = 0;
  9924. const char AANoRecurse::ID = 0;
  9925. const char AAWillReturn::ID = 0;
  9926. const char AAUndefinedBehavior::ID = 0;
  9927. const char AANoAlias::ID = 0;
  9928. const char AAIntraFnReachability::ID = 0;
  9929. const char AANoReturn::ID = 0;
  9930. const char AAIsDead::ID = 0;
  9931. const char AADereferenceable::ID = 0;
  9932. const char AAAlign::ID = 0;
  9933. const char AAInstanceInfo::ID = 0;
  9934. const char AANoCapture::ID = 0;
  9935. const char AAValueSimplify::ID = 0;
  9936. const char AAHeapToStack::ID = 0;
  9937. const char AAPrivatizablePtr::ID = 0;
  9938. const char AAMemoryBehavior::ID = 0;
  9939. const char AAMemoryLocation::ID = 0;
  9940. const char AAValueConstantRange::ID = 0;
  9941. const char AAPotentialConstantValues::ID = 0;
  9942. const char AAPotentialValues::ID = 0;
  9943. const char AANoUndef::ID = 0;
  9944. const char AACallEdges::ID = 0;
  9945. const char AAInterFnReachability::ID = 0;
  9946. const char AAPointerInfo::ID = 0;
  9947. const char AAAssumptionInfo::ID = 0;
  9948. const char AAUnderlyingObjects::ID = 0;
  9949. // Macro magic to create the static generator function for attributes that
  9950. // follow the naming scheme.
  9951. #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
  9952. case IRPosition::PK: \
  9953. llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
  9954. #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
  9955. case IRPosition::PK: \
  9956. AA = new (A.Allocator) CLASS##SUFFIX(IRP, A); \
  9957. ++NumAAs; \
  9958. break;
  9959. #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  9960. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  9961. CLASS *AA = nullptr; \
  9962. switch (IRP.getPositionKind()) { \
  9963. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  9964. SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
  9965. SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
  9966. SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
  9967. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
  9968. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
  9969. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  9970. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
  9971. } \
  9972. return *AA; \
  9973. }
  9974. #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  9975. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  9976. CLASS *AA = nullptr; \
  9977. switch (IRP.getPositionKind()) { \
  9978. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  9979. SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
  9980. SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
  9981. SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
  9982. SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
  9983. SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
  9984. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
  9985. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
  9986. } \
  9987. return *AA; \
  9988. }
  9989. #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  9990. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  9991. CLASS *AA = nullptr; \
  9992. switch (IRP.getPositionKind()) { \
  9993. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  9994. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  9995. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
  9996. SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
  9997. SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
  9998. SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
  9999. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
  10000. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
  10001. } \
  10002. return *AA; \
  10003. }
  10004. #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  10005. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  10006. CLASS *AA = nullptr; \
  10007. switch (IRP.getPositionKind()) { \
  10008. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  10009. SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
  10010. SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
  10011. SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
  10012. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
  10013. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
  10014. SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
  10015. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  10016. } \
  10017. return *AA; \
  10018. }
  10019. #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  10020. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  10021. CLASS *AA = nullptr; \
  10022. switch (IRP.getPositionKind()) { \
  10023. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  10024. SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
  10025. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  10026. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
  10027. SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
  10028. SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
  10029. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
  10030. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
  10031. } \
  10032. return *AA; \
  10033. }
  10034. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
  10035. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
  10036. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
  10037. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
  10038. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
  10039. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
  10040. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
  10041. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges)
  10042. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAssumptionInfo)
  10043. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
  10044. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
  10045. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
  10046. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
  10047. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
  10048. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAInstanceInfo)
  10049. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
  10050. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
  10051. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialConstantValues)
  10052. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
  10053. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
  10054. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
  10055. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
  10056. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
  10057. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
  10058. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUnderlyingObjects)
  10059. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
  10060. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
  10061. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIntraFnReachability)
  10062. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAInterFnReachability)
  10063. CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
  10064. #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
  10065. #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
  10066. #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
  10067. #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
  10068. #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
  10069. #undef SWITCH_PK_CREATE
  10070. #undef SWITCH_PK_INV