AttributorAttributes.cpp 381 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207
  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/SCCIterator.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SetOperations.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/Analysis/AliasAnalysis.h"
  21. #include "llvm/Analysis/AssumeBundleQueries.h"
  22. #include "llvm/Analysis/AssumptionCache.h"
  23. #include "llvm/Analysis/CaptureTracking.h"
  24. #include "llvm/Analysis/InstructionSimplify.h"
  25. #include "llvm/Analysis/LazyValueInfo.h"
  26. #include "llvm/Analysis/MemoryBuiltins.h"
  27. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  28. #include "llvm/Analysis/ScalarEvolution.h"
  29. #include "llvm/Analysis/TargetTransformInfo.h"
  30. #include "llvm/Analysis/ValueTracking.h"
  31. #include "llvm/IR/Assumptions.h"
  32. #include "llvm/IR/Constants.h"
  33. #include "llvm/IR/DataLayout.h"
  34. #include "llvm/IR/IRBuilder.h"
  35. #include "llvm/IR/Instruction.h"
  36. #include "llvm/IR/Instructions.h"
  37. #include "llvm/IR/IntrinsicInst.h"
  38. #include "llvm/IR/NoFolder.h"
  39. #include "llvm/Support/Alignment.h"
  40. #include "llvm/Support/Casting.h"
  41. #include "llvm/Support/CommandLine.h"
  42. #include "llvm/Support/ErrorHandling.h"
  43. #include "llvm/Support/FileSystem.h"
  44. #include "llvm/Support/raw_ostream.h"
  45. #include "llvm/Transforms/IPO/ArgumentPromotion.h"
  46. #include "llvm/Transforms/Utils/Local.h"
  47. #include <cassert>
  48. using namespace llvm;
  49. #define DEBUG_TYPE "attributor"
  50. static cl::opt<bool> ManifestInternal(
  51. "attributor-manifest-internal", cl::Hidden,
  52. cl::desc("Manifest Attributor internal string attributes."),
  53. cl::init(false));
  54. static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
  55. cl::Hidden);
  56. template <>
  57. unsigned llvm::PotentialConstantIntValuesState::MaxPotentialValues = 0;
  58. static cl::opt<unsigned, true> MaxPotentialValues(
  59. "attributor-max-potential-values", cl::Hidden,
  60. cl::desc("Maximum number of potential values to be "
  61. "tracked for each position."),
  62. cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues),
  63. cl::init(7));
  64. static cl::opt<unsigned>
  65. MaxInterferingWrites("attributor-max-interfering-writes", cl::Hidden,
  66. cl::desc("Maximum number of interfering writes to "
  67. "check before assuming all might interfere."),
  68. cl::init(6));
  69. STATISTIC(NumAAs, "Number of abstract attributes created");
  70. // Some helper macros to deal with statistics tracking.
  71. //
  72. // Usage:
  73. // For simple IR attribute tracking overload trackStatistics in the abstract
  74. // attribute and choose the right STATS_DECLTRACK_********* macro,
  75. // e.g.,:
  76. // void trackStatistics() const override {
  77. // STATS_DECLTRACK_ARG_ATTR(returned)
  78. // }
  79. // If there is a single "increment" side one can use the macro
  80. // STATS_DECLTRACK with a custom message. If there are multiple increment
  81. // sides, STATS_DECL and STATS_TRACK can also be used separately.
  82. //
  83. #define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
  84. ("Number of " #TYPE " marked '" #NAME "'")
  85. #define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
  86. #define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
  87. #define STATS_DECL(NAME, TYPE, MSG) \
  88. STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
  89. #define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
  90. #define STATS_DECLTRACK(NAME, TYPE, MSG) \
  91. { \
  92. STATS_DECL(NAME, TYPE, MSG) \
  93. STATS_TRACK(NAME, TYPE) \
  94. }
  95. #define STATS_DECLTRACK_ARG_ATTR(NAME) \
  96. STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
  97. #define STATS_DECLTRACK_CSARG_ATTR(NAME) \
  98. STATS_DECLTRACK(NAME, CSArguments, \
  99. BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
  100. #define STATS_DECLTRACK_FN_ATTR(NAME) \
  101. STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
  102. #define STATS_DECLTRACK_CS_ATTR(NAME) \
  103. STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
  104. #define STATS_DECLTRACK_FNRET_ATTR(NAME) \
  105. STATS_DECLTRACK(NAME, FunctionReturn, \
  106. BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
  107. #define STATS_DECLTRACK_CSRET_ATTR(NAME) \
  108. STATS_DECLTRACK(NAME, CSReturn, \
  109. BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
  110. #define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
  111. STATS_DECLTRACK(NAME, Floating, \
  112. ("Number of floating values known to be '" #NAME "'"))
  113. // Specialization of the operator<< for abstract attributes subclasses. This
  114. // disambiguates situations where multiple operators are applicable.
  115. namespace llvm {
  116. #define PIPE_OPERATOR(CLASS) \
  117. raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
  118. return OS << static_cast<const AbstractAttribute &>(AA); \
  119. }
  120. PIPE_OPERATOR(AAIsDead)
  121. PIPE_OPERATOR(AANoUnwind)
  122. PIPE_OPERATOR(AANoSync)
  123. PIPE_OPERATOR(AANoRecurse)
  124. PIPE_OPERATOR(AAWillReturn)
  125. PIPE_OPERATOR(AANoReturn)
  126. PIPE_OPERATOR(AAReturnedValues)
  127. PIPE_OPERATOR(AANonNull)
  128. PIPE_OPERATOR(AANoAlias)
  129. PIPE_OPERATOR(AADereferenceable)
  130. PIPE_OPERATOR(AAAlign)
  131. PIPE_OPERATOR(AANoCapture)
  132. PIPE_OPERATOR(AAValueSimplify)
  133. PIPE_OPERATOR(AANoFree)
  134. PIPE_OPERATOR(AAHeapToStack)
  135. PIPE_OPERATOR(AAReachability)
  136. PIPE_OPERATOR(AAMemoryBehavior)
  137. PIPE_OPERATOR(AAMemoryLocation)
  138. PIPE_OPERATOR(AAValueConstantRange)
  139. PIPE_OPERATOR(AAPrivatizablePtr)
  140. PIPE_OPERATOR(AAUndefinedBehavior)
  141. PIPE_OPERATOR(AAPotentialValues)
  142. PIPE_OPERATOR(AANoUndef)
  143. PIPE_OPERATOR(AACallEdges)
  144. PIPE_OPERATOR(AAFunctionReachability)
  145. PIPE_OPERATOR(AAPointerInfo)
  146. PIPE_OPERATOR(AAAssumptionInfo)
  147. #undef PIPE_OPERATOR
  148. template <>
  149. ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
  150. const DerefState &R) {
  151. ChangeStatus CS0 =
  152. clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
  153. ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
  154. return CS0 | CS1;
  155. }
  156. } // namespace llvm
  157. /// Get pointer operand of memory accessing instruction. If \p I is
  158. /// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
  159. /// is set to false and the instruction is volatile, return nullptr.
  160. static const Value *getPointerOperand(const Instruction *I,
  161. bool AllowVolatile) {
  162. if (!AllowVolatile && I->isVolatile())
  163. return nullptr;
  164. if (auto *LI = dyn_cast<LoadInst>(I)) {
  165. return LI->getPointerOperand();
  166. }
  167. if (auto *SI = dyn_cast<StoreInst>(I)) {
  168. return SI->getPointerOperand();
  169. }
  170. if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
  171. return CXI->getPointerOperand();
  172. }
  173. if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
  174. return RMWI->getPointerOperand();
  175. }
  176. return nullptr;
  177. }
  178. /// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
  179. /// advanced by \p Offset bytes. To aid later analysis the method tries to build
  180. /// getelement pointer instructions that traverse the natural type of \p Ptr if
  181. /// possible. If that fails, the remaining offset is adjusted byte-wise, hence
  182. /// through a cast to i8*.
  183. ///
  184. /// TODO: This could probably live somewhere more prominantly if it doesn't
  185. /// already exist.
  186. static Value *constructPointer(Type *ResTy, Type *PtrElemTy, Value *Ptr,
  187. int64_t Offset, IRBuilder<NoFolder> &IRB,
  188. const DataLayout &DL) {
  189. assert(Offset >= 0 && "Negative offset not supported yet!");
  190. LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
  191. << "-bytes as " << *ResTy << "\n");
  192. if (Offset) {
  193. Type *Ty = PtrElemTy;
  194. APInt IntOffset(DL.getIndexTypeSizeInBits(Ptr->getType()), Offset);
  195. SmallVector<APInt> IntIndices = DL.getGEPIndicesForOffset(Ty, IntOffset);
  196. SmallVector<Value *, 4> ValIndices;
  197. std::string GEPName = Ptr->getName().str();
  198. for (const APInt &Index : IntIndices) {
  199. ValIndices.push_back(IRB.getInt(Index));
  200. GEPName += "." + std::to_string(Index.getZExtValue());
  201. }
  202. // Create a GEP for the indices collected above.
  203. Ptr = IRB.CreateGEP(PtrElemTy, Ptr, ValIndices, GEPName);
  204. // If an offset is left we use byte-wise adjustment.
  205. if (IntOffset != 0) {
  206. Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
  207. Ptr = IRB.CreateGEP(IRB.getInt8Ty(), Ptr, IRB.getInt(IntOffset),
  208. GEPName + ".b" + Twine(IntOffset.getZExtValue()));
  209. }
  210. }
  211. // Ensure the result has the requested type.
  212. Ptr = IRB.CreatePointerBitCastOrAddrSpaceCast(Ptr, ResTy,
  213. Ptr->getName() + ".cast");
  214. LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
  215. return Ptr;
  216. }
  217. /// Recursively visit all values that might become \p IRP at some point. This
  218. /// will be done by looking through cast instructions, selects, phis, and calls
  219. /// with the "returned" attribute. Once we cannot look through the value any
  220. /// further, the callback \p VisitValueCB is invoked and passed the current
  221. /// value, the \p State, and a flag to indicate if we stripped anything.
  222. /// Stripped means that we unpacked the value associated with \p IRP at least
  223. /// once. Note that the value used for the callback may still be the value
  224. /// associated with \p IRP (due to PHIs). To limit how much effort is invested,
  225. /// we will never visit more values than specified by \p MaxValues.
  226. /// If \p Intraprocedural is set to true only values valid in the scope of
  227. /// \p CtxI will be visited and simplification into other scopes is prevented.
  228. template <typename StateTy>
  229. static bool genericValueTraversal(
  230. Attributor &A, IRPosition IRP, const AbstractAttribute &QueryingAA,
  231. StateTy &State,
  232. function_ref<bool(Value &, const Instruction *, StateTy &, bool)>
  233. VisitValueCB,
  234. const Instruction *CtxI, bool &UsedAssumedInformation,
  235. bool UseValueSimplify = true, int MaxValues = 16,
  236. function_ref<Value *(Value *)> StripCB = nullptr,
  237. bool Intraprocedural = false) {
  238. struct LivenessInfo {
  239. const AAIsDead *LivenessAA = nullptr;
  240. bool AnyDead = false;
  241. };
  242. DenseMap<const Function *, LivenessInfo> LivenessAAs;
  243. auto GetLivenessInfo = [&](const Function &F) -> LivenessInfo & {
  244. LivenessInfo &LI = LivenessAAs[&F];
  245. if (!LI.LivenessAA)
  246. LI.LivenessAA = &A.getAAFor<AAIsDead>(QueryingAA, IRPosition::function(F),
  247. DepClassTy::NONE);
  248. return LI;
  249. };
  250. Value *InitialV = &IRP.getAssociatedValue();
  251. using Item = std::pair<Value *, const Instruction *>;
  252. SmallSet<Item, 16> Visited;
  253. SmallVector<Item, 16> Worklist;
  254. Worklist.push_back({InitialV, CtxI});
  255. int Iteration = 0;
  256. do {
  257. Item I = Worklist.pop_back_val();
  258. Value *V = I.first;
  259. CtxI = I.second;
  260. if (StripCB)
  261. V = StripCB(V);
  262. // Check if we should process the current value. To prevent endless
  263. // recursion keep a record of the values we followed!
  264. if (!Visited.insert(I).second)
  265. continue;
  266. // Make sure we limit the compile time for complex expressions.
  267. if (Iteration++ >= MaxValues) {
  268. LLVM_DEBUG(dbgs() << "Generic value traversal reached iteration limit: "
  269. << Iteration << "!\n");
  270. return false;
  271. }
  272. // Explicitly look through calls with a "returned" attribute if we do
  273. // not have a pointer as stripPointerCasts only works on them.
  274. Value *NewV = nullptr;
  275. if (V->getType()->isPointerTy()) {
  276. NewV = V->stripPointerCasts();
  277. } else {
  278. auto *CB = dyn_cast<CallBase>(V);
  279. if (CB && CB->getCalledFunction()) {
  280. for (Argument &Arg : CB->getCalledFunction()->args())
  281. if (Arg.hasReturnedAttr()) {
  282. NewV = CB->getArgOperand(Arg.getArgNo());
  283. break;
  284. }
  285. }
  286. }
  287. if (NewV && NewV != V) {
  288. Worklist.push_back({NewV, CtxI});
  289. continue;
  290. }
  291. // Look through select instructions, visit assumed potential values.
  292. if (auto *SI = dyn_cast<SelectInst>(V)) {
  293. Optional<Constant *> C = A.getAssumedConstant(
  294. *SI->getCondition(), QueryingAA, UsedAssumedInformation);
  295. bool NoValueYet = !C.hasValue();
  296. if (NoValueYet || isa_and_nonnull<UndefValue>(*C))
  297. continue;
  298. if (auto *CI = dyn_cast_or_null<ConstantInt>(*C)) {
  299. if (CI->isZero())
  300. Worklist.push_back({SI->getFalseValue(), CtxI});
  301. else
  302. Worklist.push_back({SI->getTrueValue(), CtxI});
  303. continue;
  304. }
  305. // We could not simplify the condition, assume both values.(
  306. Worklist.push_back({SI->getTrueValue(), CtxI});
  307. Worklist.push_back({SI->getFalseValue(), CtxI});
  308. continue;
  309. }
  310. // Look through phi nodes, visit all live operands.
  311. if (auto *PHI = dyn_cast<PHINode>(V)) {
  312. LivenessInfo &LI = GetLivenessInfo(*PHI->getFunction());
  313. for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
  314. BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
  315. if (LI.LivenessAA->isEdgeDead(IncomingBB, PHI->getParent())) {
  316. LI.AnyDead = true;
  317. UsedAssumedInformation |= !LI.LivenessAA->isAtFixpoint();
  318. continue;
  319. }
  320. Worklist.push_back(
  321. {PHI->getIncomingValue(u), IncomingBB->getTerminator()});
  322. }
  323. continue;
  324. }
  325. if (auto *Arg = dyn_cast<Argument>(V)) {
  326. if (!Intraprocedural && !Arg->hasPassPointeeByValueCopyAttr()) {
  327. SmallVector<Item> CallSiteValues;
  328. bool UsedAssumedInformation = false;
  329. if (A.checkForAllCallSites(
  330. [&](AbstractCallSite ACS) {
  331. // Callbacks might not have a corresponding call site operand,
  332. // stick with the argument in that case.
  333. Value *CSOp = ACS.getCallArgOperand(*Arg);
  334. if (!CSOp)
  335. return false;
  336. CallSiteValues.push_back({CSOp, ACS.getInstruction()});
  337. return true;
  338. },
  339. *Arg->getParent(), true, &QueryingAA, UsedAssumedInformation)) {
  340. Worklist.append(CallSiteValues);
  341. continue;
  342. }
  343. }
  344. }
  345. if (UseValueSimplify && !isa<Constant>(V)) {
  346. Optional<Value *> SimpleV =
  347. A.getAssumedSimplified(*V, QueryingAA, UsedAssumedInformation);
  348. if (!SimpleV.hasValue())
  349. continue;
  350. Value *NewV = SimpleV.getValue();
  351. if (NewV && NewV != V) {
  352. if (!Intraprocedural || !CtxI ||
  353. AA::isValidInScope(*NewV, CtxI->getFunction())) {
  354. Worklist.push_back({NewV, CtxI});
  355. continue;
  356. }
  357. }
  358. }
  359. // Once a leaf is reached we inform the user through the callback.
  360. if (!VisitValueCB(*V, CtxI, State, Iteration > 1)) {
  361. LLVM_DEBUG(dbgs() << "Generic value traversal visit callback failed for: "
  362. << *V << "!\n");
  363. return false;
  364. }
  365. } while (!Worklist.empty());
  366. // If we actually used liveness information so we have to record a dependence.
  367. for (auto &It : LivenessAAs)
  368. if (It.second.AnyDead)
  369. A.recordDependence(*It.second.LivenessAA, QueryingAA,
  370. DepClassTy::OPTIONAL);
  371. // All values have been visited.
  372. return true;
  373. }
  374. bool AA::getAssumedUnderlyingObjects(Attributor &A, const Value &Ptr,
  375. SmallVectorImpl<Value *> &Objects,
  376. const AbstractAttribute &QueryingAA,
  377. const Instruction *CtxI,
  378. bool &UsedAssumedInformation,
  379. bool Intraprocedural) {
  380. auto StripCB = [&](Value *V) { return getUnderlyingObject(V); };
  381. SmallPtrSet<Value *, 8> SeenObjects;
  382. auto VisitValueCB = [&SeenObjects](Value &Val, const Instruction *,
  383. SmallVectorImpl<Value *> &Objects,
  384. bool) -> bool {
  385. if (SeenObjects.insert(&Val).second)
  386. Objects.push_back(&Val);
  387. return true;
  388. };
  389. if (!genericValueTraversal<decltype(Objects)>(
  390. A, IRPosition::value(Ptr), QueryingAA, Objects, VisitValueCB, CtxI,
  391. UsedAssumedInformation, true, 32, StripCB, Intraprocedural))
  392. return false;
  393. return true;
  394. }
  395. const Value *stripAndAccumulateMinimalOffsets(
  396. Attributor &A, const AbstractAttribute &QueryingAA, const Value *Val,
  397. const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
  398. bool UseAssumed = false) {
  399. auto AttributorAnalysis = [&](Value &V, APInt &ROffset) -> bool {
  400. const IRPosition &Pos = IRPosition::value(V);
  401. // Only track dependence if we are going to use the assumed info.
  402. const AAValueConstantRange &ValueConstantRangeAA =
  403. A.getAAFor<AAValueConstantRange>(QueryingAA, Pos,
  404. UseAssumed ? DepClassTy::OPTIONAL
  405. : DepClassTy::NONE);
  406. ConstantRange Range = UseAssumed ? ValueConstantRangeAA.getAssumed()
  407. : ValueConstantRangeAA.getKnown();
  408. // We can only use the lower part of the range because the upper part can
  409. // be higher than what the value can really be.
  410. ROffset = Range.getSignedMin();
  411. return true;
  412. };
  413. return Val->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds,
  414. /* AllowInvariant */ false,
  415. AttributorAnalysis);
  416. }
  417. static const Value *
  418. getMinimalBaseOfPointer(Attributor &A, const AbstractAttribute &QueryingAA,
  419. const Value *Ptr, int64_t &BytesOffset,
  420. const DataLayout &DL, bool AllowNonInbounds = false) {
  421. APInt OffsetAPInt(DL.getIndexTypeSizeInBits(Ptr->getType()), 0);
  422. const Value *Base = stripAndAccumulateMinimalOffsets(
  423. A, QueryingAA, Ptr, DL, OffsetAPInt, AllowNonInbounds);
  424. BytesOffset = OffsetAPInt.getSExtValue();
  425. return Base;
  426. }
  427. /// Clamp the information known for all returned values of a function
  428. /// (identified by \p QueryingAA) into \p S.
  429. template <typename AAType, typename StateType = typename AAType::StateType>
  430. static void clampReturnedValueStates(
  431. Attributor &A, const AAType &QueryingAA, StateType &S,
  432. const IRPosition::CallBaseContext *CBContext = nullptr) {
  433. LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
  434. << QueryingAA << " into " << S << "\n");
  435. assert((QueryingAA.getIRPosition().getPositionKind() ==
  436. IRPosition::IRP_RETURNED ||
  437. QueryingAA.getIRPosition().getPositionKind() ==
  438. IRPosition::IRP_CALL_SITE_RETURNED) &&
  439. "Can only clamp returned value states for a function returned or call "
  440. "site returned position!");
  441. // Use an optional state as there might not be any return values and we want
  442. // to join (IntegerState::operator&) the state of all there are.
  443. Optional<StateType> T;
  444. // Callback for each possibly returned value.
  445. auto CheckReturnValue = [&](Value &RV) -> bool {
  446. const IRPosition &RVPos = IRPosition::value(RV, CBContext);
  447. const AAType &AA =
  448. A.getAAFor<AAType>(QueryingAA, RVPos, DepClassTy::REQUIRED);
  449. LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
  450. << " @ " << RVPos << "\n");
  451. const StateType &AAS = AA.getState();
  452. if (T.hasValue())
  453. *T &= AAS;
  454. else
  455. T = AAS;
  456. LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
  457. << "\n");
  458. return T->isValidState();
  459. };
  460. if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
  461. S.indicatePessimisticFixpoint();
  462. else if (T.hasValue())
  463. S ^= *T;
  464. }
  465. namespace {
  466. /// Helper class for generic deduction: return value -> returned position.
  467. template <typename AAType, typename BaseType,
  468. typename StateType = typename BaseType::StateType,
  469. bool PropagateCallBaseContext = false>
  470. struct AAReturnedFromReturnedValues : public BaseType {
  471. AAReturnedFromReturnedValues(const IRPosition &IRP, Attributor &A)
  472. : BaseType(IRP, A) {}
  473. /// See AbstractAttribute::updateImpl(...).
  474. ChangeStatus updateImpl(Attributor &A) override {
  475. StateType S(StateType::getBestState(this->getState()));
  476. clampReturnedValueStates<AAType, StateType>(
  477. A, *this, S,
  478. PropagateCallBaseContext ? this->getCallBaseContext() : nullptr);
  479. // TODO: If we know we visited all returned values, thus no are assumed
  480. // dead, we can take the known information from the state T.
  481. return clampStateAndIndicateChange<StateType>(this->getState(), S);
  482. }
  483. };
  484. /// Clamp the information known at all call sites for a given argument
  485. /// (identified by \p QueryingAA) into \p S.
  486. template <typename AAType, typename StateType = typename AAType::StateType>
  487. static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
  488. StateType &S) {
  489. LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
  490. << QueryingAA << " into " << S << "\n");
  491. assert(QueryingAA.getIRPosition().getPositionKind() ==
  492. IRPosition::IRP_ARGUMENT &&
  493. "Can only clamp call site argument states for an argument position!");
  494. // Use an optional state as there might not be any return values and we want
  495. // to join (IntegerState::operator&) the state of all there are.
  496. Optional<StateType> T;
  497. // The argument number which is also the call site argument number.
  498. unsigned ArgNo = QueryingAA.getIRPosition().getCallSiteArgNo();
  499. auto CallSiteCheck = [&](AbstractCallSite ACS) {
  500. const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
  501. // Check if a coresponding argument was found or if it is on not associated
  502. // (which can happen for callback calls).
  503. if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
  504. return false;
  505. const AAType &AA =
  506. A.getAAFor<AAType>(QueryingAA, ACSArgPos, DepClassTy::REQUIRED);
  507. LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
  508. << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
  509. const StateType &AAS = AA.getState();
  510. if (T.hasValue())
  511. *T &= AAS;
  512. else
  513. T = AAS;
  514. LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
  515. << "\n");
  516. return T->isValidState();
  517. };
  518. bool UsedAssumedInformation = false;
  519. if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
  520. UsedAssumedInformation))
  521. S.indicatePessimisticFixpoint();
  522. else if (T.hasValue())
  523. S ^= *T;
  524. }
  525. /// This function is the bridge between argument position and the call base
  526. /// context.
  527. template <typename AAType, typename BaseType,
  528. typename StateType = typename AAType::StateType>
  529. bool getArgumentStateFromCallBaseContext(Attributor &A,
  530. BaseType &QueryingAttribute,
  531. IRPosition &Pos, StateType &State) {
  532. assert((Pos.getPositionKind() == IRPosition::IRP_ARGUMENT) &&
  533. "Expected an 'argument' position !");
  534. const CallBase *CBContext = Pos.getCallBaseContext();
  535. if (!CBContext)
  536. return false;
  537. int ArgNo = Pos.getCallSiteArgNo();
  538. assert(ArgNo >= 0 && "Invalid Arg No!");
  539. const auto &AA = A.getAAFor<AAType>(
  540. QueryingAttribute, IRPosition::callsite_argument(*CBContext, ArgNo),
  541. DepClassTy::REQUIRED);
  542. const StateType &CBArgumentState =
  543. static_cast<const StateType &>(AA.getState());
  544. LLVM_DEBUG(dbgs() << "[Attributor] Briding Call site context to argument"
  545. << "Position:" << Pos << "CB Arg state:" << CBArgumentState
  546. << "\n");
  547. // NOTE: If we want to do call site grouping it should happen here.
  548. State ^= CBArgumentState;
  549. return true;
  550. }
  551. /// Helper class for generic deduction: call site argument -> argument position.
  552. template <typename AAType, typename BaseType,
  553. typename StateType = typename AAType::StateType,
  554. bool BridgeCallBaseContext = false>
  555. struct AAArgumentFromCallSiteArguments : public BaseType {
  556. AAArgumentFromCallSiteArguments(const IRPosition &IRP, Attributor &A)
  557. : BaseType(IRP, A) {}
  558. /// See AbstractAttribute::updateImpl(...).
  559. ChangeStatus updateImpl(Attributor &A) override {
  560. StateType S = StateType::getBestState(this->getState());
  561. if (BridgeCallBaseContext) {
  562. bool Success =
  563. getArgumentStateFromCallBaseContext<AAType, BaseType, StateType>(
  564. A, *this, this->getIRPosition(), S);
  565. if (Success)
  566. return clampStateAndIndicateChange<StateType>(this->getState(), S);
  567. }
  568. clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
  569. // TODO: If we know we visited all incoming values, thus no are assumed
  570. // dead, we can take the known information from the state T.
  571. return clampStateAndIndicateChange<StateType>(this->getState(), S);
  572. }
  573. };
  574. /// Helper class for generic replication: function returned -> cs returned.
  575. template <typename AAType, typename BaseType,
  576. typename StateType = typename BaseType::StateType,
  577. bool IntroduceCallBaseContext = false>
  578. struct AACallSiteReturnedFromReturned : public BaseType {
  579. AACallSiteReturnedFromReturned(const IRPosition &IRP, Attributor &A)
  580. : BaseType(IRP, A) {}
  581. /// See AbstractAttribute::updateImpl(...).
  582. ChangeStatus updateImpl(Attributor &A) override {
  583. assert(this->getIRPosition().getPositionKind() ==
  584. IRPosition::IRP_CALL_SITE_RETURNED &&
  585. "Can only wrap function returned positions for call site returned "
  586. "positions!");
  587. auto &S = this->getState();
  588. const Function *AssociatedFunction =
  589. this->getIRPosition().getAssociatedFunction();
  590. if (!AssociatedFunction)
  591. return S.indicatePessimisticFixpoint();
  592. CallBase &CBContext = cast<CallBase>(this->getAnchorValue());
  593. if (IntroduceCallBaseContext)
  594. LLVM_DEBUG(dbgs() << "[Attributor] Introducing call base context:"
  595. << CBContext << "\n");
  596. IRPosition FnPos = IRPosition::returned(
  597. *AssociatedFunction, IntroduceCallBaseContext ? &CBContext : nullptr);
  598. const AAType &AA = A.getAAFor<AAType>(*this, FnPos, DepClassTy::REQUIRED);
  599. return clampStateAndIndicateChange(S, AA.getState());
  600. }
  601. };
  602. } // namespace
  603. /// Helper function to accumulate uses.
  604. template <class AAType, typename StateType = typename AAType::StateType>
  605. static void followUsesInContext(AAType &AA, Attributor &A,
  606. MustBeExecutedContextExplorer &Explorer,
  607. const Instruction *CtxI,
  608. SetVector<const Use *> &Uses,
  609. StateType &State) {
  610. auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
  611. for (unsigned u = 0; u < Uses.size(); ++u) {
  612. const Use *U = Uses[u];
  613. if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
  614. bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
  615. if (Found && AA.followUseInMBEC(A, U, UserI, State))
  616. for (const Use &Us : UserI->uses())
  617. Uses.insert(&Us);
  618. }
  619. }
  620. }
  621. /// Use the must-be-executed-context around \p I to add information into \p S.
  622. /// The AAType class is required to have `followUseInMBEC` method with the
  623. /// following signature and behaviour:
  624. ///
  625. /// bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I)
  626. /// U - Underlying use.
  627. /// I - The user of the \p U.
  628. /// Returns true if the value should be tracked transitively.
  629. ///
  630. template <class AAType, typename StateType = typename AAType::StateType>
  631. static void followUsesInMBEC(AAType &AA, Attributor &A, StateType &S,
  632. Instruction &CtxI) {
  633. // Container for (transitive) uses of the associated value.
  634. SetVector<const Use *> Uses;
  635. for (const Use &U : AA.getIRPosition().getAssociatedValue().uses())
  636. Uses.insert(&U);
  637. MustBeExecutedContextExplorer &Explorer =
  638. A.getInfoCache().getMustBeExecutedContextExplorer();
  639. followUsesInContext<AAType>(AA, A, Explorer, &CtxI, Uses, S);
  640. if (S.isAtFixpoint())
  641. return;
  642. SmallVector<const BranchInst *, 4> BrInsts;
  643. auto Pred = [&](const Instruction *I) {
  644. if (const BranchInst *Br = dyn_cast<BranchInst>(I))
  645. if (Br->isConditional())
  646. BrInsts.push_back(Br);
  647. return true;
  648. };
  649. // Here, accumulate conditional branch instructions in the context. We
  650. // explore the child paths and collect the known states. The disjunction of
  651. // those states can be merged to its own state. Let ParentState_i be a state
  652. // to indicate the known information for an i-th branch instruction in the
  653. // context. ChildStates are created for its successors respectively.
  654. //
  655. // ParentS_1 = ChildS_{1, 1} /\ ChildS_{1, 2} /\ ... /\ ChildS_{1, n_1}
  656. // ParentS_2 = ChildS_{2, 1} /\ ChildS_{2, 2} /\ ... /\ ChildS_{2, n_2}
  657. // ...
  658. // ParentS_m = ChildS_{m, 1} /\ ChildS_{m, 2} /\ ... /\ ChildS_{m, n_m}
  659. //
  660. // Known State |= ParentS_1 \/ ParentS_2 \/... \/ ParentS_m
  661. //
  662. // FIXME: Currently, recursive branches are not handled. For example, we
  663. // can't deduce that ptr must be dereferenced in below function.
  664. //
  665. // void f(int a, int c, int *ptr) {
  666. // if(a)
  667. // if (b) {
  668. // *ptr = 0;
  669. // } else {
  670. // *ptr = 1;
  671. // }
  672. // else {
  673. // if (b) {
  674. // *ptr = 0;
  675. // } else {
  676. // *ptr = 1;
  677. // }
  678. // }
  679. // }
  680. Explorer.checkForAllContext(&CtxI, Pred);
  681. for (const BranchInst *Br : BrInsts) {
  682. StateType ParentState;
  683. // The known state of the parent state is a conjunction of children's
  684. // known states so it is initialized with a best state.
  685. ParentState.indicateOptimisticFixpoint();
  686. for (const BasicBlock *BB : Br->successors()) {
  687. StateType ChildState;
  688. size_t BeforeSize = Uses.size();
  689. followUsesInContext(AA, A, Explorer, &BB->front(), Uses, ChildState);
  690. // Erase uses which only appear in the child.
  691. for (auto It = Uses.begin() + BeforeSize; It != Uses.end();)
  692. It = Uses.erase(It);
  693. ParentState &= ChildState;
  694. }
  695. // Use only known state.
  696. S += ParentState;
  697. }
  698. }
  699. /// ------------------------ PointerInfo ---------------------------------------
  700. namespace llvm {
  701. namespace AA {
  702. namespace PointerInfo {
  703. /// An access kind description as used by AAPointerInfo.
  704. struct OffsetAndSize;
  705. struct State;
  706. } // namespace PointerInfo
  707. } // namespace AA
  708. /// Helper for AA::PointerInfo::Acccess DenseMap/Set usage.
  709. template <>
  710. struct DenseMapInfo<AAPointerInfo::Access> : DenseMapInfo<Instruction *> {
  711. using Access = AAPointerInfo::Access;
  712. static inline Access getEmptyKey();
  713. static inline Access getTombstoneKey();
  714. static unsigned getHashValue(const Access &A);
  715. static bool isEqual(const Access &LHS, const Access &RHS);
  716. };
  717. /// Helper that allows OffsetAndSize as a key in a DenseMap.
  718. template <>
  719. struct DenseMapInfo<AA::PointerInfo ::OffsetAndSize>
  720. : DenseMapInfo<std::pair<int64_t, int64_t>> {};
  721. /// Helper for AA::PointerInfo::Acccess DenseMap/Set usage ignoring everythign
  722. /// but the instruction
  723. struct AccessAsInstructionInfo : DenseMapInfo<Instruction *> {
  724. using Base = DenseMapInfo<Instruction *>;
  725. using Access = AAPointerInfo::Access;
  726. static inline Access getEmptyKey();
  727. static inline Access getTombstoneKey();
  728. static unsigned getHashValue(const Access &A);
  729. static bool isEqual(const Access &LHS, const Access &RHS);
  730. };
  731. } // namespace llvm
  732. /// Helper to represent an access offset and size, with logic to deal with
  733. /// uncertainty and check for overlapping accesses.
  734. struct AA::PointerInfo::OffsetAndSize : public std::pair<int64_t, int64_t> {
  735. using BaseTy = std::pair<int64_t, int64_t>;
  736. OffsetAndSize(int64_t Offset, int64_t Size) : BaseTy(Offset, Size) {}
  737. OffsetAndSize(const BaseTy &P) : BaseTy(P) {}
  738. int64_t getOffset() const { return first; }
  739. int64_t getSize() const { return second; }
  740. static OffsetAndSize getUnknown() { return OffsetAndSize(Unknown, Unknown); }
  741. /// Return true if offset or size are unknown.
  742. bool offsetOrSizeAreUnknown() const {
  743. return getOffset() == OffsetAndSize::Unknown ||
  744. getSize() == OffsetAndSize::Unknown;
  745. }
  746. /// Return true if this offset and size pair might describe an address that
  747. /// overlaps with \p OAS.
  748. bool mayOverlap(const OffsetAndSize &OAS) const {
  749. // Any unknown value and we are giving up -> overlap.
  750. if (offsetOrSizeAreUnknown() || OAS.offsetOrSizeAreUnknown())
  751. return true;
  752. // Check if one offset point is in the other interval [offset, offset+size].
  753. return OAS.getOffset() + OAS.getSize() > getOffset() &&
  754. OAS.getOffset() < getOffset() + getSize();
  755. }
  756. /// Constant used to represent unknown offset or sizes.
  757. static constexpr int64_t Unknown = 1 << 31;
  758. };
  759. /// Implementation of the DenseMapInfo.
  760. ///
  761. ///{
  762. inline llvm::AccessAsInstructionInfo::Access
  763. llvm::AccessAsInstructionInfo::getEmptyKey() {
  764. return Access(Base::getEmptyKey(), nullptr, AAPointerInfo::AK_READ, nullptr);
  765. }
  766. inline llvm::AccessAsInstructionInfo::Access
  767. llvm::AccessAsInstructionInfo::getTombstoneKey() {
  768. return Access(Base::getTombstoneKey(), nullptr, AAPointerInfo::AK_READ,
  769. nullptr);
  770. }
  771. unsigned llvm::AccessAsInstructionInfo::getHashValue(
  772. const llvm::AccessAsInstructionInfo::Access &A) {
  773. return Base::getHashValue(A.getRemoteInst());
  774. }
  775. bool llvm::AccessAsInstructionInfo::isEqual(
  776. const llvm::AccessAsInstructionInfo::Access &LHS,
  777. const llvm::AccessAsInstructionInfo::Access &RHS) {
  778. return LHS.getRemoteInst() == RHS.getRemoteInst();
  779. }
  780. inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
  781. llvm::DenseMapInfo<AAPointerInfo::Access>::getEmptyKey() {
  782. return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_READ,
  783. nullptr);
  784. }
  785. inline llvm::DenseMapInfo<AAPointerInfo::Access>::Access
  786. llvm::DenseMapInfo<AAPointerInfo::Access>::getTombstoneKey() {
  787. return AAPointerInfo::Access(nullptr, nullptr, AAPointerInfo::AK_WRITE,
  788. nullptr);
  789. }
  790. unsigned llvm::DenseMapInfo<AAPointerInfo::Access>::getHashValue(
  791. const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &A) {
  792. return detail::combineHashValue(
  793. DenseMapInfo<Instruction *>::getHashValue(A.getRemoteInst()),
  794. (A.isWrittenValueYetUndetermined()
  795. ? ~0
  796. : DenseMapInfo<Value *>::getHashValue(A.getWrittenValue()))) +
  797. A.getKind();
  798. }
  799. bool llvm::DenseMapInfo<AAPointerInfo::Access>::isEqual(
  800. const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &LHS,
  801. const llvm::DenseMapInfo<AAPointerInfo::Access>::Access &RHS) {
  802. return LHS == RHS;
  803. }
  804. ///}
  805. /// A type to track pointer/struct usage and accesses for AAPointerInfo.
  806. struct AA::PointerInfo::State : public AbstractState {
  807. /// Return the best possible representable state.
  808. static State getBestState(const State &SIS) { return State(); }
  809. /// Return the worst possible representable state.
  810. static State getWorstState(const State &SIS) {
  811. State R;
  812. R.indicatePessimisticFixpoint();
  813. return R;
  814. }
  815. State() {}
  816. State(const State &SIS) : AccessBins(SIS.AccessBins) {}
  817. State(State &&SIS) : AccessBins(std::move(SIS.AccessBins)) {}
  818. const State &getAssumed() const { return *this; }
  819. /// See AbstractState::isValidState().
  820. bool isValidState() const override { return BS.isValidState(); }
  821. /// See AbstractState::isAtFixpoint().
  822. bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
  823. /// See AbstractState::indicateOptimisticFixpoint().
  824. ChangeStatus indicateOptimisticFixpoint() override {
  825. BS.indicateOptimisticFixpoint();
  826. return ChangeStatus::UNCHANGED;
  827. }
  828. /// See AbstractState::indicatePessimisticFixpoint().
  829. ChangeStatus indicatePessimisticFixpoint() override {
  830. BS.indicatePessimisticFixpoint();
  831. return ChangeStatus::CHANGED;
  832. }
  833. State &operator=(const State &R) {
  834. if (this == &R)
  835. return *this;
  836. BS = R.BS;
  837. AccessBins = R.AccessBins;
  838. return *this;
  839. }
  840. State &operator=(State &&R) {
  841. if (this == &R)
  842. return *this;
  843. std::swap(BS, R.BS);
  844. std::swap(AccessBins, R.AccessBins);
  845. return *this;
  846. }
  847. bool operator==(const State &R) const {
  848. if (BS != R.BS)
  849. return false;
  850. if (AccessBins.size() != R.AccessBins.size())
  851. return false;
  852. auto It = begin(), RIt = R.begin(), E = end();
  853. while (It != E) {
  854. if (It->getFirst() != RIt->getFirst())
  855. return false;
  856. auto &Accs = It->getSecond();
  857. auto &RAccs = RIt->getSecond();
  858. if (Accs.size() != RAccs.size())
  859. return false;
  860. auto AccIt = Accs.begin(), RAccIt = RAccs.begin(), AccE = Accs.end();
  861. while (AccIt != AccE) {
  862. if (*AccIt != *RAccIt)
  863. return false;
  864. ++AccIt;
  865. ++RAccIt;
  866. }
  867. ++It;
  868. ++RIt;
  869. }
  870. return true;
  871. }
  872. bool operator!=(const State &R) const { return !(*this == R); }
  873. /// We store accesses in a set with the instruction as key.
  874. using Accesses = DenseSet<AAPointerInfo::Access, AccessAsInstructionInfo>;
  875. /// We store all accesses in bins denoted by their offset and size.
  876. using AccessBinsTy = DenseMap<OffsetAndSize, Accesses>;
  877. AccessBinsTy::const_iterator begin() const { return AccessBins.begin(); }
  878. AccessBinsTy::const_iterator end() const { return AccessBins.end(); }
  879. protected:
  880. /// The bins with all the accesses for the associated pointer.
  881. DenseMap<OffsetAndSize, Accesses> AccessBins;
  882. /// Add a new access to the state at offset \p Offset and with size \p Size.
  883. /// The access is associated with \p I, writes \p Content (if anything), and
  884. /// is of kind \p Kind.
  885. /// \Returns CHANGED, if the state changed, UNCHANGED otherwise.
  886. ChangeStatus addAccess(int64_t Offset, int64_t Size, Instruction &I,
  887. Optional<Value *> Content,
  888. AAPointerInfo::AccessKind Kind, Type *Ty,
  889. Instruction *RemoteI = nullptr,
  890. Accesses *BinPtr = nullptr) {
  891. OffsetAndSize Key{Offset, Size};
  892. Accesses &Bin = BinPtr ? *BinPtr : AccessBins[Key];
  893. AAPointerInfo::Access Acc(&I, RemoteI ? RemoteI : &I, Content, Kind, Ty);
  894. // Check if we have an access for this instruction in this bin, if not,
  895. // simply add it.
  896. auto It = Bin.find(Acc);
  897. if (It == Bin.end()) {
  898. Bin.insert(Acc);
  899. return ChangeStatus::CHANGED;
  900. }
  901. // If the existing access is the same as then new one, nothing changed.
  902. AAPointerInfo::Access Before = *It;
  903. // The new one will be combined with the existing one.
  904. *It &= Acc;
  905. return *It == Before ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
  906. }
  907. /// See AAPointerInfo::forallInterferingAccesses.
  908. bool forallInterferingAccesses(
  909. Instruction &I,
  910. function_ref<bool(const AAPointerInfo::Access &, bool)> CB) const {
  911. if (!isValidState())
  912. return false;
  913. // First find the offset and size of I.
  914. OffsetAndSize OAS(-1, -1);
  915. for (auto &It : AccessBins) {
  916. for (auto &Access : It.getSecond()) {
  917. if (Access.getRemoteInst() == &I) {
  918. OAS = It.getFirst();
  919. break;
  920. }
  921. }
  922. if (OAS.getSize() != -1)
  923. break;
  924. }
  925. if (OAS.getSize() == -1)
  926. return true;
  927. // Now that we have an offset and size, find all overlapping ones and use
  928. // the callback on the accesses.
  929. for (auto &It : AccessBins) {
  930. OffsetAndSize ItOAS = It.getFirst();
  931. if (!OAS.mayOverlap(ItOAS))
  932. continue;
  933. bool IsExact = OAS == ItOAS && !OAS.offsetOrSizeAreUnknown();
  934. for (auto &Access : It.getSecond())
  935. if (!CB(Access, IsExact))
  936. return false;
  937. }
  938. return true;
  939. }
  940. private:
  941. /// State to track fixpoint and validity.
  942. BooleanState BS;
  943. };
  944. struct AAPointerInfoImpl
  945. : public StateWrapper<AA::PointerInfo::State, AAPointerInfo> {
  946. using BaseTy = StateWrapper<AA::PointerInfo::State, AAPointerInfo>;
  947. AAPointerInfoImpl(const IRPosition &IRP, Attributor &A) : BaseTy(IRP) {}
  948. /// See AbstractAttribute::initialize(...).
  949. void initialize(Attributor &A) override { AAPointerInfo::initialize(A); }
  950. /// See AbstractAttribute::getAsStr().
  951. const std::string getAsStr() const override {
  952. return std::string("PointerInfo ") +
  953. (isValidState() ? (std::string("#") +
  954. std::to_string(AccessBins.size()) + " bins")
  955. : "<invalid>");
  956. }
  957. /// See AbstractAttribute::manifest(...).
  958. ChangeStatus manifest(Attributor &A) override {
  959. return AAPointerInfo::manifest(A);
  960. }
  961. bool forallInterferingAccesses(
  962. LoadInst &LI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
  963. const override {
  964. return State::forallInterferingAccesses(LI, CB);
  965. }
  966. bool forallInterferingAccesses(
  967. StoreInst &SI, function_ref<bool(const AAPointerInfo::Access &, bool)> CB)
  968. const override {
  969. return State::forallInterferingAccesses(SI, CB);
  970. }
  971. bool forallInterferingWrites(
  972. Attributor &A, const AbstractAttribute &QueryingAA, LoadInst &LI,
  973. function_ref<bool(const Access &, bool)> UserCB) const override {
  974. SmallPtrSet<const Access *, 8> DominatingWrites;
  975. SmallVector<std::pair<const Access *, bool>, 8> InterferingWrites;
  976. Function &Scope = *LI.getFunction();
  977. const auto &NoSyncAA = A.getAAFor<AANoSync>(
  978. QueryingAA, IRPosition::function(Scope), DepClassTy::OPTIONAL);
  979. const auto *ExecDomainAA = A.lookupAAFor<AAExecutionDomain>(
  980. IRPosition::function(Scope), &QueryingAA, DepClassTy::OPTIONAL);
  981. const bool NoSync = NoSyncAA.isAssumedNoSync();
  982. // Helper to determine if we need to consider threading, which we cannot
  983. // right now. However, if the function is (assumed) nosync or the thread
  984. // executing all instructions is the main thread only we can ignore
  985. // threading.
  986. auto CanIgnoreThreading = [&](const Instruction &I) -> bool {
  987. if (NoSync)
  988. return true;
  989. if (ExecDomainAA && ExecDomainAA->isExecutedByInitialThreadOnly(I))
  990. return true;
  991. return false;
  992. };
  993. // Helper to determine if the access is executed by the same thread as the
  994. // load, for now it is sufficient to avoid any potential threading effects
  995. // as we cannot deal with them anyway.
  996. auto IsSameThreadAsLoad = [&](const Access &Acc) -> bool {
  997. return CanIgnoreThreading(*Acc.getLocalInst());
  998. };
  999. // TODO: Use inter-procedural reachability and dominance.
  1000. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  1001. QueryingAA, IRPosition::function(*LI.getFunction()),
  1002. DepClassTy::OPTIONAL);
  1003. const bool CanUseCFGResoning = CanIgnoreThreading(LI);
  1004. InformationCache &InfoCache = A.getInfoCache();
  1005. const DominatorTree *DT =
  1006. NoRecurseAA.isKnownNoRecurse()
  1007. ? InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
  1008. Scope)
  1009. : nullptr;
  1010. enum GPUAddressSpace : unsigned {
  1011. Generic = 0,
  1012. Global = 1,
  1013. Shared = 3,
  1014. Constant = 4,
  1015. Local = 5,
  1016. };
  1017. // Helper to check if a value has "kernel lifetime", that is it will not
  1018. // outlive a GPU kernel. This is true for shared, constant, and local
  1019. // globals on AMD and NVIDIA GPUs.
  1020. auto HasKernelLifetime = [&](Value *V, Module &M) {
  1021. Triple T(M.getTargetTriple());
  1022. if (!(T.isAMDGPU() || T.isNVPTX()))
  1023. return false;
  1024. switch (V->getType()->getPointerAddressSpace()) {
  1025. case GPUAddressSpace::Shared:
  1026. case GPUAddressSpace::Constant:
  1027. case GPUAddressSpace::Local:
  1028. return true;
  1029. default:
  1030. return false;
  1031. };
  1032. };
  1033. // The IsLiveInCalleeCB will be used by the AA::isPotentiallyReachable query
  1034. // to determine if we should look at reachability from the callee. For
  1035. // certain pointers we know the lifetime and we do not have to step into the
  1036. // callee to determine reachability as the pointer would be dead in the
  1037. // callee. See the conditional initialization below.
  1038. std::function<bool(const Function &)> IsLiveInCalleeCB;
  1039. if (auto *AI = dyn_cast<AllocaInst>(&getAssociatedValue())) {
  1040. // If the alloca containing function is not recursive the alloca
  1041. // must be dead in the callee.
  1042. const Function *AIFn = AI->getFunction();
  1043. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  1044. *this, IRPosition::function(*AIFn), DepClassTy::OPTIONAL);
  1045. if (NoRecurseAA.isAssumedNoRecurse()) {
  1046. IsLiveInCalleeCB = [AIFn](const Function &Fn) { return AIFn != &Fn; };
  1047. }
  1048. } else if (auto *GV = dyn_cast<GlobalValue>(&getAssociatedValue())) {
  1049. // If the global has kernel lifetime we can stop if we reach a kernel
  1050. // as it is "dead" in the (unknown) callees.
  1051. if (HasKernelLifetime(GV, *GV->getParent()))
  1052. IsLiveInCalleeCB = [](const Function &Fn) {
  1053. return !Fn.hasFnAttribute("kernel");
  1054. };
  1055. }
  1056. auto AccessCB = [&](const Access &Acc, bool Exact) {
  1057. if (!Acc.isWrite())
  1058. return true;
  1059. // For now we only filter accesses based on CFG reasoning which does not
  1060. // work yet if we have threading effects, or the access is complicated.
  1061. if (CanUseCFGResoning) {
  1062. if (!AA::isPotentiallyReachable(A, *Acc.getLocalInst(), LI, QueryingAA,
  1063. IsLiveInCalleeCB))
  1064. return true;
  1065. if (DT && Exact &&
  1066. (Acc.getLocalInst()->getFunction() == LI.getFunction()) &&
  1067. IsSameThreadAsLoad(Acc)) {
  1068. if (DT->dominates(Acc.getLocalInst(), &LI))
  1069. DominatingWrites.insert(&Acc);
  1070. }
  1071. }
  1072. InterferingWrites.push_back({&Acc, Exact});
  1073. return true;
  1074. };
  1075. if (!State::forallInterferingAccesses(LI, AccessCB))
  1076. return false;
  1077. // If we cannot use CFG reasoning we only filter the non-write accesses
  1078. // and are done here.
  1079. if (!CanUseCFGResoning) {
  1080. for (auto &It : InterferingWrites)
  1081. if (!UserCB(*It.first, It.second))
  1082. return false;
  1083. return true;
  1084. }
  1085. // Helper to determine if we can skip a specific write access. This is in
  1086. // the worst case quadratic as we are looking for another write that will
  1087. // hide the effect of this one.
  1088. auto CanSkipAccess = [&](const Access &Acc, bool Exact) {
  1089. if (!IsSameThreadAsLoad(Acc))
  1090. return false;
  1091. if (!DominatingWrites.count(&Acc))
  1092. return false;
  1093. for (const Access *DomAcc : DominatingWrites) {
  1094. assert(Acc.getLocalInst()->getFunction() ==
  1095. DomAcc->getLocalInst()->getFunction() &&
  1096. "Expected dominating writes to be in the same function!");
  1097. if (DomAcc != &Acc &&
  1098. DT->dominates(Acc.getLocalInst(), DomAcc->getLocalInst())) {
  1099. return true;
  1100. }
  1101. }
  1102. return false;
  1103. };
  1104. // Run the user callback on all writes we cannot skip and return if that
  1105. // succeeded for all or not.
  1106. unsigned NumInterferingWrites = InterferingWrites.size();
  1107. for (auto &It : InterferingWrites) {
  1108. if (!DT || NumInterferingWrites > MaxInterferingWrites ||
  1109. !CanSkipAccess(*It.first, It.second)) {
  1110. if (!UserCB(*It.first, It.second))
  1111. return false;
  1112. }
  1113. }
  1114. return true;
  1115. }
  1116. ChangeStatus translateAndAddCalleeState(Attributor &A,
  1117. const AAPointerInfo &CalleeAA,
  1118. int64_t CallArgOffset, CallBase &CB) {
  1119. using namespace AA::PointerInfo;
  1120. if (!CalleeAA.getState().isValidState() || !isValidState())
  1121. return indicatePessimisticFixpoint();
  1122. const auto &CalleeImplAA = static_cast<const AAPointerInfoImpl &>(CalleeAA);
  1123. bool IsByval = CalleeImplAA.getAssociatedArgument()->hasByValAttr();
  1124. // Combine the accesses bin by bin.
  1125. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1126. for (auto &It : CalleeImplAA.getState()) {
  1127. OffsetAndSize OAS = OffsetAndSize::getUnknown();
  1128. if (CallArgOffset != OffsetAndSize::Unknown)
  1129. OAS = OffsetAndSize(It.first.getOffset() + CallArgOffset,
  1130. It.first.getSize());
  1131. Accesses &Bin = AccessBins[OAS];
  1132. for (const AAPointerInfo::Access &RAcc : It.second) {
  1133. if (IsByval && !RAcc.isRead())
  1134. continue;
  1135. bool UsedAssumedInformation = false;
  1136. Optional<Value *> Content = A.translateArgumentToCallSiteContent(
  1137. RAcc.getContent(), CB, *this, UsedAssumedInformation);
  1138. AccessKind AK =
  1139. AccessKind(RAcc.getKind() & (IsByval ? AccessKind::AK_READ
  1140. : AccessKind::AK_READ_WRITE));
  1141. Changed =
  1142. Changed | addAccess(OAS.getOffset(), OAS.getSize(), CB, Content, AK,
  1143. RAcc.getType(), RAcc.getRemoteInst(), &Bin);
  1144. }
  1145. }
  1146. return Changed;
  1147. }
  1148. /// Statistic tracking for all AAPointerInfo implementations.
  1149. /// See AbstractAttribute::trackStatistics().
  1150. void trackPointerInfoStatistics(const IRPosition &IRP) const {}
  1151. };
  1152. struct AAPointerInfoFloating : public AAPointerInfoImpl {
  1153. using AccessKind = AAPointerInfo::AccessKind;
  1154. AAPointerInfoFloating(const IRPosition &IRP, Attributor &A)
  1155. : AAPointerInfoImpl(IRP, A) {}
  1156. /// See AbstractAttribute::initialize(...).
  1157. void initialize(Attributor &A) override { AAPointerInfoImpl::initialize(A); }
  1158. /// Deal with an access and signal if it was handled successfully.
  1159. bool handleAccess(Attributor &A, Instruction &I, Value &Ptr,
  1160. Optional<Value *> Content, AccessKind Kind, int64_t Offset,
  1161. ChangeStatus &Changed, Type *Ty,
  1162. int64_t Size = AA::PointerInfo::OffsetAndSize::Unknown) {
  1163. using namespace AA::PointerInfo;
  1164. // No need to find a size if one is given or the offset is unknown.
  1165. if (Offset != OffsetAndSize::Unknown && Size == OffsetAndSize::Unknown &&
  1166. Ty) {
  1167. const DataLayout &DL = A.getDataLayout();
  1168. TypeSize AccessSize = DL.getTypeStoreSize(Ty);
  1169. if (!AccessSize.isScalable())
  1170. Size = AccessSize.getFixedSize();
  1171. }
  1172. Changed = Changed | addAccess(Offset, Size, I, Content, Kind, Ty);
  1173. return true;
  1174. };
  1175. /// Helper struct, will support ranges eventually.
  1176. struct OffsetInfo {
  1177. int64_t Offset = AA::PointerInfo::OffsetAndSize::Unknown;
  1178. bool operator==(const OffsetInfo &OI) const { return Offset == OI.Offset; }
  1179. };
  1180. /// See AbstractAttribute::updateImpl(...).
  1181. ChangeStatus updateImpl(Attributor &A) override {
  1182. using namespace AA::PointerInfo;
  1183. State S = getState();
  1184. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1185. Value &AssociatedValue = getAssociatedValue();
  1186. const DataLayout &DL = A.getDataLayout();
  1187. DenseMap<Value *, OffsetInfo> OffsetInfoMap;
  1188. OffsetInfoMap[&AssociatedValue] = OffsetInfo{0};
  1189. auto HandlePassthroughUser = [&](Value *Usr, OffsetInfo &PtrOI,
  1190. bool &Follow) {
  1191. OffsetInfo &UsrOI = OffsetInfoMap[Usr];
  1192. UsrOI = PtrOI;
  1193. Follow = true;
  1194. return true;
  1195. };
  1196. const auto *TLI = getAnchorScope()
  1197. ? A.getInfoCache().getTargetLibraryInfoForFunction(
  1198. *getAnchorScope())
  1199. : nullptr;
  1200. auto UsePred = [&](const Use &U, bool &Follow) -> bool {
  1201. Value *CurPtr = U.get();
  1202. User *Usr = U.getUser();
  1203. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Analyze " << *CurPtr << " in "
  1204. << *Usr << "\n");
  1205. assert(OffsetInfoMap.count(CurPtr) &&
  1206. "The current pointer offset should have been seeded!");
  1207. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) {
  1208. if (CE->isCast())
  1209. return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
  1210. if (CE->isCompare())
  1211. return true;
  1212. if (!isa<GEPOperator>(CE)) {
  1213. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled constant user " << *CE
  1214. << "\n");
  1215. return false;
  1216. }
  1217. }
  1218. if (auto *GEP = dyn_cast<GEPOperator>(Usr)) {
  1219. // Note the order here, the Usr access might change the map, CurPtr is
  1220. // already in it though.
  1221. OffsetInfo &UsrOI = OffsetInfoMap[Usr];
  1222. OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
  1223. UsrOI = PtrOI;
  1224. // TODO: Use range information.
  1225. if (PtrOI.Offset == OffsetAndSize::Unknown ||
  1226. !GEP->hasAllConstantIndices()) {
  1227. UsrOI.Offset = OffsetAndSize::Unknown;
  1228. Follow = true;
  1229. return true;
  1230. }
  1231. SmallVector<Value *, 8> Indices;
  1232. for (Use &Idx : GEP->indices()) {
  1233. if (auto *CIdx = dyn_cast<ConstantInt>(Idx)) {
  1234. Indices.push_back(CIdx);
  1235. continue;
  1236. }
  1237. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Non constant GEP index " << *GEP
  1238. << " : " << *Idx << "\n");
  1239. return false;
  1240. }
  1241. UsrOI.Offset = PtrOI.Offset + DL.getIndexedOffsetInType(
  1242. GEP->getSourceElementType(), Indices);
  1243. Follow = true;
  1244. return true;
  1245. }
  1246. if (isa<CastInst>(Usr) || isa<SelectInst>(Usr))
  1247. return HandlePassthroughUser(Usr, OffsetInfoMap[CurPtr], Follow);
  1248. // For PHIs we need to take care of the recurrence explicitly as the value
  1249. // might change while we iterate through a loop. For now, we give up if
  1250. // the PHI is not invariant.
  1251. if (isa<PHINode>(Usr)) {
  1252. // Note the order here, the Usr access might change the map, CurPtr is
  1253. // already in it though.
  1254. OffsetInfo &UsrOI = OffsetInfoMap[Usr];
  1255. OffsetInfo &PtrOI = OffsetInfoMap[CurPtr];
  1256. // Check if the PHI is invariant (so far).
  1257. if (UsrOI == PtrOI)
  1258. return true;
  1259. // Check if the PHI operand has already an unknown offset as we can't
  1260. // improve on that anymore.
  1261. if (PtrOI.Offset == OffsetAndSize::Unknown) {
  1262. UsrOI = PtrOI;
  1263. Follow = true;
  1264. return true;
  1265. }
  1266. // Check if the PHI operand is not dependent on the PHI itself.
  1267. // TODO: This is not great as we look at the pointer type. However, it
  1268. // is unclear where the Offset size comes from with typeless pointers.
  1269. APInt Offset(
  1270. DL.getIndexSizeInBits(CurPtr->getType()->getPointerAddressSpace()),
  1271. 0);
  1272. if (&AssociatedValue == CurPtr->stripAndAccumulateConstantOffsets(
  1273. DL, Offset, /* AllowNonInbounds */ true)) {
  1274. if (Offset != PtrOI.Offset) {
  1275. LLVM_DEBUG(dbgs()
  1276. << "[AAPointerInfo] PHI operand pointer offset mismatch "
  1277. << *CurPtr << " in " << *Usr << "\n");
  1278. return false;
  1279. }
  1280. return HandlePassthroughUser(Usr, PtrOI, Follow);
  1281. }
  1282. // TODO: Approximate in case we know the direction of the recurrence.
  1283. LLVM_DEBUG(dbgs() << "[AAPointerInfo] PHI operand is too complex "
  1284. << *CurPtr << " in " << *Usr << "\n");
  1285. UsrOI = PtrOI;
  1286. UsrOI.Offset = OffsetAndSize::Unknown;
  1287. Follow = true;
  1288. return true;
  1289. }
  1290. if (auto *LoadI = dyn_cast<LoadInst>(Usr))
  1291. return handleAccess(A, *LoadI, *CurPtr, /* Content */ nullptr,
  1292. AccessKind::AK_READ, OffsetInfoMap[CurPtr].Offset,
  1293. Changed, LoadI->getType());
  1294. if (auto *StoreI = dyn_cast<StoreInst>(Usr)) {
  1295. if (StoreI->getValueOperand() == CurPtr) {
  1296. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Escaping use in store "
  1297. << *StoreI << "\n");
  1298. return false;
  1299. }
  1300. bool UsedAssumedInformation = false;
  1301. Optional<Value *> Content = A.getAssumedSimplified(
  1302. *StoreI->getValueOperand(), *this, UsedAssumedInformation);
  1303. return handleAccess(A, *StoreI, *CurPtr, Content, AccessKind::AK_WRITE,
  1304. OffsetInfoMap[CurPtr].Offset, Changed,
  1305. StoreI->getValueOperand()->getType());
  1306. }
  1307. if (auto *CB = dyn_cast<CallBase>(Usr)) {
  1308. if (CB->isLifetimeStartOrEnd())
  1309. return true;
  1310. if (TLI && isFreeCall(CB, TLI))
  1311. return true;
  1312. if (CB->isArgOperand(&U)) {
  1313. unsigned ArgNo = CB->getArgOperandNo(&U);
  1314. const auto &CSArgPI = A.getAAFor<AAPointerInfo>(
  1315. *this, IRPosition::callsite_argument(*CB, ArgNo),
  1316. DepClassTy::REQUIRED);
  1317. Changed = translateAndAddCalleeState(
  1318. A, CSArgPI, OffsetInfoMap[CurPtr].Offset, *CB) |
  1319. Changed;
  1320. return true;
  1321. }
  1322. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Call user not handled " << *CB
  1323. << "\n");
  1324. // TODO: Allow some call uses
  1325. return false;
  1326. }
  1327. LLVM_DEBUG(dbgs() << "[AAPointerInfo] User not handled " << *Usr << "\n");
  1328. return false;
  1329. };
  1330. auto EquivalentUseCB = [&](const Use &OldU, const Use &NewU) {
  1331. if (OffsetInfoMap.count(NewU))
  1332. return OffsetInfoMap[NewU] == OffsetInfoMap[OldU];
  1333. OffsetInfoMap[NewU] = OffsetInfoMap[OldU];
  1334. return true;
  1335. };
  1336. if (!A.checkForAllUses(UsePred, *this, AssociatedValue,
  1337. /* CheckBBLivenessOnly */ true, DepClassTy::OPTIONAL,
  1338. EquivalentUseCB))
  1339. return indicatePessimisticFixpoint();
  1340. LLVM_DEBUG({
  1341. dbgs() << "Accesses by bin after update:\n";
  1342. for (auto &It : AccessBins) {
  1343. dbgs() << "[" << It.first.getOffset() << "-"
  1344. << It.first.getOffset() + It.first.getSize()
  1345. << "] : " << It.getSecond().size() << "\n";
  1346. for (auto &Acc : It.getSecond()) {
  1347. dbgs() << " - " << Acc.getKind() << " - " << *Acc.getLocalInst()
  1348. << "\n";
  1349. if (Acc.getLocalInst() != Acc.getRemoteInst())
  1350. dbgs() << " --> "
  1351. << *Acc.getRemoteInst() << "\n";
  1352. if (!Acc.isWrittenValueYetUndetermined())
  1353. dbgs() << " - " << Acc.getWrittenValue() << "\n";
  1354. }
  1355. }
  1356. });
  1357. return Changed;
  1358. }
  1359. /// See AbstractAttribute::trackStatistics()
  1360. void trackStatistics() const override {
  1361. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1362. }
  1363. };
  1364. struct AAPointerInfoReturned final : AAPointerInfoImpl {
  1365. AAPointerInfoReturned(const IRPosition &IRP, Attributor &A)
  1366. : AAPointerInfoImpl(IRP, A) {}
  1367. /// See AbstractAttribute::updateImpl(...).
  1368. ChangeStatus updateImpl(Attributor &A) override {
  1369. return indicatePessimisticFixpoint();
  1370. }
  1371. /// See AbstractAttribute::trackStatistics()
  1372. void trackStatistics() const override {
  1373. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1374. }
  1375. };
  1376. struct AAPointerInfoArgument final : AAPointerInfoFloating {
  1377. AAPointerInfoArgument(const IRPosition &IRP, Attributor &A)
  1378. : AAPointerInfoFloating(IRP, A) {}
  1379. /// See AbstractAttribute::initialize(...).
  1380. void initialize(Attributor &A) override {
  1381. AAPointerInfoFloating::initialize(A);
  1382. if (getAnchorScope()->isDeclaration())
  1383. indicatePessimisticFixpoint();
  1384. }
  1385. /// See AbstractAttribute::trackStatistics()
  1386. void trackStatistics() const override {
  1387. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1388. }
  1389. };
  1390. struct AAPointerInfoCallSiteArgument final : AAPointerInfoFloating {
  1391. AAPointerInfoCallSiteArgument(const IRPosition &IRP, Attributor &A)
  1392. : AAPointerInfoFloating(IRP, A) {}
  1393. /// See AbstractAttribute::updateImpl(...).
  1394. ChangeStatus updateImpl(Attributor &A) override {
  1395. using namespace AA::PointerInfo;
  1396. // We handle memory intrinsics explicitly, at least the first (=
  1397. // destination) and second (=source) arguments as we know how they are
  1398. // accessed.
  1399. if (auto *MI = dyn_cast_or_null<MemIntrinsic>(getCtxI())) {
  1400. ConstantInt *Length = dyn_cast<ConstantInt>(MI->getLength());
  1401. int64_t LengthVal = OffsetAndSize::Unknown;
  1402. if (Length)
  1403. LengthVal = Length->getSExtValue();
  1404. Value &Ptr = getAssociatedValue();
  1405. unsigned ArgNo = getIRPosition().getCallSiteArgNo();
  1406. ChangeStatus Changed;
  1407. if (ArgNo == 0) {
  1408. handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_WRITE, 0, Changed,
  1409. nullptr, LengthVal);
  1410. } else if (ArgNo == 1) {
  1411. handleAccess(A, *MI, Ptr, nullptr, AccessKind::AK_READ, 0, Changed,
  1412. nullptr, LengthVal);
  1413. } else {
  1414. LLVM_DEBUG(dbgs() << "[AAPointerInfo] Unhandled memory intrinsic "
  1415. << *MI << "\n");
  1416. return indicatePessimisticFixpoint();
  1417. }
  1418. return Changed;
  1419. }
  1420. // TODO: Once we have call site specific value information we can provide
  1421. // call site specific liveness information and then it makes
  1422. // sense to specialize attributes for call sites arguments instead of
  1423. // redirecting requests to the callee argument.
  1424. Argument *Arg = getAssociatedArgument();
  1425. if (!Arg)
  1426. return indicatePessimisticFixpoint();
  1427. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  1428. auto &ArgAA =
  1429. A.getAAFor<AAPointerInfo>(*this, ArgPos, DepClassTy::REQUIRED);
  1430. return translateAndAddCalleeState(A, ArgAA, 0, *cast<CallBase>(getCtxI()));
  1431. }
  1432. /// See AbstractAttribute::trackStatistics()
  1433. void trackStatistics() const override {
  1434. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1435. }
  1436. };
  1437. struct AAPointerInfoCallSiteReturned final : AAPointerInfoFloating {
  1438. AAPointerInfoCallSiteReturned(const IRPosition &IRP, Attributor &A)
  1439. : AAPointerInfoFloating(IRP, A) {}
  1440. /// See AbstractAttribute::trackStatistics()
  1441. void trackStatistics() const override {
  1442. AAPointerInfoImpl::trackPointerInfoStatistics(getIRPosition());
  1443. }
  1444. };
  1445. /// -----------------------NoUnwind Function Attribute--------------------------
  1446. struct AANoUnwindImpl : AANoUnwind {
  1447. AANoUnwindImpl(const IRPosition &IRP, Attributor &A) : AANoUnwind(IRP, A) {}
  1448. const std::string getAsStr() const override {
  1449. return getAssumed() ? "nounwind" : "may-unwind";
  1450. }
  1451. /// See AbstractAttribute::updateImpl(...).
  1452. ChangeStatus updateImpl(Attributor &A) override {
  1453. auto Opcodes = {
  1454. (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
  1455. (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
  1456. (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
  1457. auto CheckForNoUnwind = [&](Instruction &I) {
  1458. if (!I.mayThrow())
  1459. return true;
  1460. if (const auto *CB = dyn_cast<CallBase>(&I)) {
  1461. const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
  1462. *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
  1463. return NoUnwindAA.isAssumedNoUnwind();
  1464. }
  1465. return false;
  1466. };
  1467. bool UsedAssumedInformation = false;
  1468. if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes,
  1469. UsedAssumedInformation))
  1470. return indicatePessimisticFixpoint();
  1471. return ChangeStatus::UNCHANGED;
  1472. }
  1473. };
  1474. struct AANoUnwindFunction final : public AANoUnwindImpl {
  1475. AANoUnwindFunction(const IRPosition &IRP, Attributor &A)
  1476. : AANoUnwindImpl(IRP, A) {}
  1477. /// See AbstractAttribute::trackStatistics()
  1478. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
  1479. };
  1480. /// NoUnwind attribute deduction for a call sites.
  1481. struct AANoUnwindCallSite final : AANoUnwindImpl {
  1482. AANoUnwindCallSite(const IRPosition &IRP, Attributor &A)
  1483. : AANoUnwindImpl(IRP, A) {}
  1484. /// See AbstractAttribute::initialize(...).
  1485. void initialize(Attributor &A) override {
  1486. AANoUnwindImpl::initialize(A);
  1487. Function *F = getAssociatedFunction();
  1488. if (!F || F->isDeclaration())
  1489. indicatePessimisticFixpoint();
  1490. }
  1491. /// See AbstractAttribute::updateImpl(...).
  1492. ChangeStatus updateImpl(Attributor &A) override {
  1493. // TODO: Once we have call site specific value information we can provide
  1494. // call site specific liveness information and then it makes
  1495. // sense to specialize attributes for call sites arguments instead of
  1496. // redirecting requests to the callee argument.
  1497. Function *F = getAssociatedFunction();
  1498. const IRPosition &FnPos = IRPosition::function(*F);
  1499. auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::REQUIRED);
  1500. return clampStateAndIndicateChange(getState(), FnAA.getState());
  1501. }
  1502. /// See AbstractAttribute::trackStatistics()
  1503. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
  1504. };
  1505. /// --------------------- Function Return Values -------------------------------
  1506. /// "Attribute" that collects all potential returned values and the return
  1507. /// instructions that they arise from.
  1508. ///
  1509. /// If there is a unique returned value R, the manifest method will:
  1510. /// - mark R with the "returned" attribute, if R is an argument.
  1511. class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
  1512. /// Mapping of values potentially returned by the associated function to the
  1513. /// return instructions that might return them.
  1514. MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
  1515. /// State flags
  1516. ///
  1517. ///{
  1518. bool IsFixed = false;
  1519. bool IsValidState = true;
  1520. ///}
  1521. public:
  1522. AAReturnedValuesImpl(const IRPosition &IRP, Attributor &A)
  1523. : AAReturnedValues(IRP, A) {}
  1524. /// See AbstractAttribute::initialize(...).
  1525. void initialize(Attributor &A) override {
  1526. // Reset the state.
  1527. IsFixed = false;
  1528. IsValidState = true;
  1529. ReturnedValues.clear();
  1530. Function *F = getAssociatedFunction();
  1531. if (!F || F->isDeclaration()) {
  1532. indicatePessimisticFixpoint();
  1533. return;
  1534. }
  1535. assert(!F->getReturnType()->isVoidTy() &&
  1536. "Did not expect a void return type!");
  1537. // The map from instruction opcodes to those instructions in the function.
  1538. auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
  1539. // Look through all arguments, if one is marked as returned we are done.
  1540. for (Argument &Arg : F->args()) {
  1541. if (Arg.hasReturnedAttr()) {
  1542. auto &ReturnInstSet = ReturnedValues[&Arg];
  1543. if (auto *Insts = OpcodeInstMap.lookup(Instruction::Ret))
  1544. for (Instruction *RI : *Insts)
  1545. ReturnInstSet.insert(cast<ReturnInst>(RI));
  1546. indicateOptimisticFixpoint();
  1547. return;
  1548. }
  1549. }
  1550. if (!A.isFunctionIPOAmendable(*F))
  1551. indicatePessimisticFixpoint();
  1552. }
  1553. /// See AbstractAttribute::manifest(...).
  1554. ChangeStatus manifest(Attributor &A) override;
  1555. /// See AbstractAttribute::getState(...).
  1556. AbstractState &getState() override { return *this; }
  1557. /// See AbstractAttribute::getState(...).
  1558. const AbstractState &getState() const override { return *this; }
  1559. /// See AbstractAttribute::updateImpl(Attributor &A).
  1560. ChangeStatus updateImpl(Attributor &A) override;
  1561. llvm::iterator_range<iterator> returned_values() override {
  1562. return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
  1563. }
  1564. llvm::iterator_range<const_iterator> returned_values() const override {
  1565. return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
  1566. }
  1567. /// Return the number of potential return values, -1 if unknown.
  1568. size_t getNumReturnValues() const override {
  1569. return isValidState() ? ReturnedValues.size() : -1;
  1570. }
  1571. /// Return an assumed unique return value if a single candidate is found. If
  1572. /// there cannot be one, return a nullptr. If it is not clear yet, return the
  1573. /// Optional::NoneType.
  1574. Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
  1575. /// See AbstractState::checkForAllReturnedValues(...).
  1576. bool checkForAllReturnedValuesAndReturnInsts(
  1577. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
  1578. const override;
  1579. /// Pretty print the attribute similar to the IR representation.
  1580. const std::string getAsStr() const override;
  1581. /// See AbstractState::isAtFixpoint().
  1582. bool isAtFixpoint() const override { return IsFixed; }
  1583. /// See AbstractState::isValidState().
  1584. bool isValidState() const override { return IsValidState; }
  1585. /// See AbstractState::indicateOptimisticFixpoint(...).
  1586. ChangeStatus indicateOptimisticFixpoint() override {
  1587. IsFixed = true;
  1588. return ChangeStatus::UNCHANGED;
  1589. }
  1590. ChangeStatus indicatePessimisticFixpoint() override {
  1591. IsFixed = true;
  1592. IsValidState = false;
  1593. return ChangeStatus::CHANGED;
  1594. }
  1595. };
  1596. ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
  1597. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1598. // Bookkeeping.
  1599. assert(isValidState());
  1600. STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
  1601. "Number of function with known return values");
  1602. // Check if we have an assumed unique return value that we could manifest.
  1603. Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
  1604. if (!UniqueRV.hasValue() || !UniqueRV.getValue())
  1605. return Changed;
  1606. // Bookkeeping.
  1607. STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
  1608. "Number of function with unique return");
  1609. // If the assumed unique return value is an argument, annotate it.
  1610. if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
  1611. if (UniqueRVArg->getType()->canLosslesslyBitCastTo(
  1612. getAssociatedFunction()->getReturnType())) {
  1613. getIRPosition() = IRPosition::argument(*UniqueRVArg);
  1614. Changed = IRAttribute::manifest(A);
  1615. }
  1616. }
  1617. return Changed;
  1618. }
  1619. const std::string AAReturnedValuesImpl::getAsStr() const {
  1620. return (isAtFixpoint() ? "returns(#" : "may-return(#") +
  1621. (isValidState() ? std::to_string(getNumReturnValues()) : "?") + ")";
  1622. }
  1623. Optional<Value *>
  1624. AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
  1625. // If checkForAllReturnedValues provides a unique value, ignoring potential
  1626. // undef values that can also be present, it is assumed to be the actual
  1627. // return value and forwarded to the caller of this method. If there are
  1628. // multiple, a nullptr is returned indicating there cannot be a unique
  1629. // returned value.
  1630. Optional<Value *> UniqueRV;
  1631. Type *Ty = getAssociatedFunction()->getReturnType();
  1632. auto Pred = [&](Value &RV) -> bool {
  1633. UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty);
  1634. return UniqueRV != Optional<Value *>(nullptr);
  1635. };
  1636. if (!A.checkForAllReturnedValues(Pred, *this))
  1637. UniqueRV = nullptr;
  1638. return UniqueRV;
  1639. }
  1640. bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
  1641. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
  1642. const {
  1643. if (!isValidState())
  1644. return false;
  1645. // Check all returned values but ignore call sites as long as we have not
  1646. // encountered an overdefined one during an update.
  1647. for (auto &It : ReturnedValues) {
  1648. Value *RV = It.first;
  1649. if (!Pred(*RV, It.second))
  1650. return false;
  1651. }
  1652. return true;
  1653. }
  1654. ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
  1655. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  1656. auto ReturnValueCB = [&](Value &V, const Instruction *CtxI, ReturnInst &Ret,
  1657. bool) -> bool {
  1658. assert(AA::isValidInScope(V, Ret.getFunction()) &&
  1659. "Assumed returned value should be valid in function scope!");
  1660. if (ReturnedValues[&V].insert(&Ret))
  1661. Changed = ChangeStatus::CHANGED;
  1662. return true;
  1663. };
  1664. bool UsedAssumedInformation = false;
  1665. auto ReturnInstCB = [&](Instruction &I) {
  1666. ReturnInst &Ret = cast<ReturnInst>(I);
  1667. return genericValueTraversal<ReturnInst>(
  1668. A, IRPosition::value(*Ret.getReturnValue()), *this, Ret, ReturnValueCB,
  1669. &I, UsedAssumedInformation, /* UseValueSimplify */ true,
  1670. /* MaxValues */ 16,
  1671. /* StripCB */ nullptr, /* Intraprocedural */ true);
  1672. };
  1673. // Discover returned values from all live returned instructions in the
  1674. // associated function.
  1675. if (!A.checkForAllInstructions(ReturnInstCB, *this, {Instruction::Ret},
  1676. UsedAssumedInformation))
  1677. return indicatePessimisticFixpoint();
  1678. return Changed;
  1679. }
  1680. struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
  1681. AAReturnedValuesFunction(const IRPosition &IRP, Attributor &A)
  1682. : AAReturnedValuesImpl(IRP, A) {}
  1683. /// See AbstractAttribute::trackStatistics()
  1684. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
  1685. };
  1686. /// Returned values information for a call sites.
  1687. struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
  1688. AAReturnedValuesCallSite(const IRPosition &IRP, Attributor &A)
  1689. : AAReturnedValuesImpl(IRP, A) {}
  1690. /// See AbstractAttribute::initialize(...).
  1691. void initialize(Attributor &A) override {
  1692. // TODO: Once we have call site specific value information we can provide
  1693. // call site specific liveness information and then it makes
  1694. // sense to specialize attributes for call sites instead of
  1695. // redirecting requests to the callee.
  1696. llvm_unreachable("Abstract attributes for returned values are not "
  1697. "supported for call sites yet!");
  1698. }
  1699. /// See AbstractAttribute::updateImpl(...).
  1700. ChangeStatus updateImpl(Attributor &A) override {
  1701. return indicatePessimisticFixpoint();
  1702. }
  1703. /// See AbstractAttribute::trackStatistics()
  1704. void trackStatistics() const override {}
  1705. };
  1706. /// ------------------------ NoSync Function Attribute -------------------------
  1707. struct AANoSyncImpl : AANoSync {
  1708. AANoSyncImpl(const IRPosition &IRP, Attributor &A) : AANoSync(IRP, A) {}
  1709. const std::string getAsStr() const override {
  1710. return getAssumed() ? "nosync" : "may-sync";
  1711. }
  1712. /// See AbstractAttribute::updateImpl(...).
  1713. ChangeStatus updateImpl(Attributor &A) override;
  1714. };
  1715. bool AANoSync::isNonRelaxedAtomic(const Instruction *I) {
  1716. if (!I->isAtomic())
  1717. return false;
  1718. if (auto *FI = dyn_cast<FenceInst>(I))
  1719. // All legal orderings for fence are stronger than monotonic.
  1720. return FI->getSyncScopeID() != SyncScope::SingleThread;
  1721. if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) {
  1722. // Unordered is not a legal ordering for cmpxchg.
  1723. return (AI->getSuccessOrdering() != AtomicOrdering::Monotonic ||
  1724. AI->getFailureOrdering() != AtomicOrdering::Monotonic);
  1725. }
  1726. AtomicOrdering Ordering;
  1727. switch (I->getOpcode()) {
  1728. case Instruction::AtomicRMW:
  1729. Ordering = cast<AtomicRMWInst>(I)->getOrdering();
  1730. break;
  1731. case Instruction::Store:
  1732. Ordering = cast<StoreInst>(I)->getOrdering();
  1733. break;
  1734. case Instruction::Load:
  1735. Ordering = cast<LoadInst>(I)->getOrdering();
  1736. break;
  1737. default:
  1738. llvm_unreachable(
  1739. "New atomic operations need to be known in the attributor.");
  1740. }
  1741. return (Ordering != AtomicOrdering::Unordered &&
  1742. Ordering != AtomicOrdering::Monotonic);
  1743. }
  1744. /// Return true if this intrinsic is nosync. This is only used for intrinsics
  1745. /// which would be nosync except that they have a volatile flag. All other
  1746. /// intrinsics are simply annotated with the nosync attribute in Intrinsics.td.
  1747. bool AANoSync::isNoSyncIntrinsic(const Instruction *I) {
  1748. if (auto *MI = dyn_cast<MemIntrinsic>(I))
  1749. return !MI->isVolatile();
  1750. return false;
  1751. }
  1752. ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
  1753. auto CheckRWInstForNoSync = [&](Instruction &I) {
  1754. return AA::isNoSyncInst(A, I, *this);
  1755. };
  1756. auto CheckForNoSync = [&](Instruction &I) {
  1757. // At this point we handled all read/write effects and they are all
  1758. // nosync, so they can be skipped.
  1759. if (I.mayReadOrWriteMemory())
  1760. return true;
  1761. // non-convergent and readnone imply nosync.
  1762. return !cast<CallBase>(I).isConvergent();
  1763. };
  1764. bool UsedAssumedInformation = false;
  1765. if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this,
  1766. UsedAssumedInformation) ||
  1767. !A.checkForAllCallLikeInstructions(CheckForNoSync, *this,
  1768. UsedAssumedInformation))
  1769. return indicatePessimisticFixpoint();
  1770. return ChangeStatus::UNCHANGED;
  1771. }
  1772. struct AANoSyncFunction final : public AANoSyncImpl {
  1773. AANoSyncFunction(const IRPosition &IRP, Attributor &A)
  1774. : AANoSyncImpl(IRP, A) {}
  1775. /// See AbstractAttribute::trackStatistics()
  1776. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
  1777. };
  1778. /// NoSync attribute deduction for a call sites.
  1779. struct AANoSyncCallSite final : AANoSyncImpl {
  1780. AANoSyncCallSite(const IRPosition &IRP, Attributor &A)
  1781. : AANoSyncImpl(IRP, A) {}
  1782. /// See AbstractAttribute::initialize(...).
  1783. void initialize(Attributor &A) override {
  1784. AANoSyncImpl::initialize(A);
  1785. Function *F = getAssociatedFunction();
  1786. if (!F || F->isDeclaration())
  1787. indicatePessimisticFixpoint();
  1788. }
  1789. /// See AbstractAttribute::updateImpl(...).
  1790. ChangeStatus updateImpl(Attributor &A) override {
  1791. // TODO: Once we have call site specific value information we can provide
  1792. // call site specific liveness information and then it makes
  1793. // sense to specialize attributes for call sites arguments instead of
  1794. // redirecting requests to the callee argument.
  1795. Function *F = getAssociatedFunction();
  1796. const IRPosition &FnPos = IRPosition::function(*F);
  1797. auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos, DepClassTy::REQUIRED);
  1798. return clampStateAndIndicateChange(getState(), FnAA.getState());
  1799. }
  1800. /// See AbstractAttribute::trackStatistics()
  1801. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
  1802. };
  1803. /// ------------------------ No-Free Attributes ----------------------------
  1804. struct AANoFreeImpl : public AANoFree {
  1805. AANoFreeImpl(const IRPosition &IRP, Attributor &A) : AANoFree(IRP, A) {}
  1806. /// See AbstractAttribute::updateImpl(...).
  1807. ChangeStatus updateImpl(Attributor &A) override {
  1808. auto CheckForNoFree = [&](Instruction &I) {
  1809. const auto &CB = cast<CallBase>(I);
  1810. if (CB.hasFnAttr(Attribute::NoFree))
  1811. return true;
  1812. const auto &NoFreeAA = A.getAAFor<AANoFree>(
  1813. *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
  1814. return NoFreeAA.isAssumedNoFree();
  1815. };
  1816. bool UsedAssumedInformation = false;
  1817. if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this,
  1818. UsedAssumedInformation))
  1819. return indicatePessimisticFixpoint();
  1820. return ChangeStatus::UNCHANGED;
  1821. }
  1822. /// See AbstractAttribute::getAsStr().
  1823. const std::string getAsStr() const override {
  1824. return getAssumed() ? "nofree" : "may-free";
  1825. }
  1826. };
  1827. struct AANoFreeFunction final : public AANoFreeImpl {
  1828. AANoFreeFunction(const IRPosition &IRP, Attributor &A)
  1829. : AANoFreeImpl(IRP, A) {}
  1830. /// See AbstractAttribute::trackStatistics()
  1831. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
  1832. };
  1833. /// NoFree attribute deduction for a call sites.
  1834. struct AANoFreeCallSite final : AANoFreeImpl {
  1835. AANoFreeCallSite(const IRPosition &IRP, Attributor &A)
  1836. : AANoFreeImpl(IRP, A) {}
  1837. /// See AbstractAttribute::initialize(...).
  1838. void initialize(Attributor &A) override {
  1839. AANoFreeImpl::initialize(A);
  1840. Function *F = getAssociatedFunction();
  1841. if (!F || F->isDeclaration())
  1842. indicatePessimisticFixpoint();
  1843. }
  1844. /// See AbstractAttribute::updateImpl(...).
  1845. ChangeStatus updateImpl(Attributor &A) override {
  1846. // TODO: Once we have call site specific value information we can provide
  1847. // call site specific liveness information and then it makes
  1848. // sense to specialize attributes for call sites arguments instead of
  1849. // redirecting requests to the callee argument.
  1850. Function *F = getAssociatedFunction();
  1851. const IRPosition &FnPos = IRPosition::function(*F);
  1852. auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos, DepClassTy::REQUIRED);
  1853. return clampStateAndIndicateChange(getState(), FnAA.getState());
  1854. }
  1855. /// See AbstractAttribute::trackStatistics()
  1856. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
  1857. };
  1858. /// NoFree attribute for floating values.
  1859. struct AANoFreeFloating : AANoFreeImpl {
  1860. AANoFreeFloating(const IRPosition &IRP, Attributor &A)
  1861. : AANoFreeImpl(IRP, A) {}
  1862. /// See AbstractAttribute::trackStatistics()
  1863. void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
  1864. /// See Abstract Attribute::updateImpl(...).
  1865. ChangeStatus updateImpl(Attributor &A) override {
  1866. const IRPosition &IRP = getIRPosition();
  1867. const auto &NoFreeAA = A.getAAFor<AANoFree>(
  1868. *this, IRPosition::function_scope(IRP), DepClassTy::OPTIONAL);
  1869. if (NoFreeAA.isAssumedNoFree())
  1870. return ChangeStatus::UNCHANGED;
  1871. Value &AssociatedValue = getIRPosition().getAssociatedValue();
  1872. auto Pred = [&](const Use &U, bool &Follow) -> bool {
  1873. Instruction *UserI = cast<Instruction>(U.getUser());
  1874. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  1875. if (CB->isBundleOperand(&U))
  1876. return false;
  1877. if (!CB->isArgOperand(&U))
  1878. return true;
  1879. unsigned ArgNo = CB->getArgOperandNo(&U);
  1880. const auto &NoFreeArg = A.getAAFor<AANoFree>(
  1881. *this, IRPosition::callsite_argument(*CB, ArgNo),
  1882. DepClassTy::REQUIRED);
  1883. return NoFreeArg.isAssumedNoFree();
  1884. }
  1885. if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
  1886. isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
  1887. Follow = true;
  1888. return true;
  1889. }
  1890. if (isa<StoreInst>(UserI) || isa<LoadInst>(UserI) ||
  1891. isa<ReturnInst>(UserI))
  1892. return true;
  1893. // Unknown user.
  1894. return false;
  1895. };
  1896. if (!A.checkForAllUses(Pred, *this, AssociatedValue))
  1897. return indicatePessimisticFixpoint();
  1898. return ChangeStatus::UNCHANGED;
  1899. }
  1900. };
  1901. /// NoFree attribute for a call site argument.
  1902. struct AANoFreeArgument final : AANoFreeFloating {
  1903. AANoFreeArgument(const IRPosition &IRP, Attributor &A)
  1904. : AANoFreeFloating(IRP, A) {}
  1905. /// See AbstractAttribute::trackStatistics()
  1906. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
  1907. };
  1908. /// NoFree attribute for call site arguments.
  1909. struct AANoFreeCallSiteArgument final : AANoFreeFloating {
  1910. AANoFreeCallSiteArgument(const IRPosition &IRP, Attributor &A)
  1911. : AANoFreeFloating(IRP, A) {}
  1912. /// See AbstractAttribute::updateImpl(...).
  1913. ChangeStatus updateImpl(Attributor &A) override {
  1914. // TODO: Once we have call site specific value information we can provide
  1915. // call site specific liveness information and then it makes
  1916. // sense to specialize attributes for call sites arguments instead of
  1917. // redirecting requests to the callee argument.
  1918. Argument *Arg = getAssociatedArgument();
  1919. if (!Arg)
  1920. return indicatePessimisticFixpoint();
  1921. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  1922. auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos, DepClassTy::REQUIRED);
  1923. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  1924. }
  1925. /// See AbstractAttribute::trackStatistics()
  1926. void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
  1927. };
  1928. /// NoFree attribute for function return value.
  1929. struct AANoFreeReturned final : AANoFreeFloating {
  1930. AANoFreeReturned(const IRPosition &IRP, Attributor &A)
  1931. : AANoFreeFloating(IRP, A) {
  1932. llvm_unreachable("NoFree is not applicable to function returns!");
  1933. }
  1934. /// See AbstractAttribute::initialize(...).
  1935. void initialize(Attributor &A) override {
  1936. llvm_unreachable("NoFree is not applicable to function returns!");
  1937. }
  1938. /// See AbstractAttribute::updateImpl(...).
  1939. ChangeStatus updateImpl(Attributor &A) override {
  1940. llvm_unreachable("NoFree is not applicable to function returns!");
  1941. }
  1942. /// See AbstractAttribute::trackStatistics()
  1943. void trackStatistics() const override {}
  1944. };
  1945. /// NoFree attribute deduction for a call site return value.
  1946. struct AANoFreeCallSiteReturned final : AANoFreeFloating {
  1947. AANoFreeCallSiteReturned(const IRPosition &IRP, Attributor &A)
  1948. : AANoFreeFloating(IRP, A) {}
  1949. ChangeStatus manifest(Attributor &A) override {
  1950. return ChangeStatus::UNCHANGED;
  1951. }
  1952. /// See AbstractAttribute::trackStatistics()
  1953. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
  1954. };
  1955. /// ------------------------ NonNull Argument Attribute ------------------------
  1956. static int64_t getKnownNonNullAndDerefBytesForUse(
  1957. Attributor &A, const AbstractAttribute &QueryingAA, Value &AssociatedValue,
  1958. const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
  1959. TrackUse = false;
  1960. const Value *UseV = U->get();
  1961. if (!UseV->getType()->isPointerTy())
  1962. return 0;
  1963. // We need to follow common pointer manipulation uses to the accesses they
  1964. // feed into. We can try to be smart to avoid looking through things we do not
  1965. // like for now, e.g., non-inbounds GEPs.
  1966. if (isa<CastInst>(I)) {
  1967. TrackUse = true;
  1968. return 0;
  1969. }
  1970. if (isa<GetElementPtrInst>(I)) {
  1971. TrackUse = true;
  1972. return 0;
  1973. }
  1974. Type *PtrTy = UseV->getType();
  1975. const Function *F = I->getFunction();
  1976. bool NullPointerIsDefined =
  1977. F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
  1978. const DataLayout &DL = A.getInfoCache().getDL();
  1979. if (const auto *CB = dyn_cast<CallBase>(I)) {
  1980. if (CB->isBundleOperand(U)) {
  1981. if (RetainedKnowledge RK = getKnowledgeFromUse(
  1982. U, {Attribute::NonNull, Attribute::Dereferenceable})) {
  1983. IsNonNull |=
  1984. (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined);
  1985. return RK.ArgValue;
  1986. }
  1987. return 0;
  1988. }
  1989. if (CB->isCallee(U)) {
  1990. IsNonNull |= !NullPointerIsDefined;
  1991. return 0;
  1992. }
  1993. unsigned ArgNo = CB->getArgOperandNo(U);
  1994. IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
  1995. // As long as we only use known information there is no need to track
  1996. // dependences here.
  1997. auto &DerefAA =
  1998. A.getAAFor<AADereferenceable>(QueryingAA, IRP, DepClassTy::NONE);
  1999. IsNonNull |= DerefAA.isKnownNonNull();
  2000. return DerefAA.getKnownDereferenceableBytes();
  2001. }
  2002. Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
  2003. if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
  2004. return 0;
  2005. int64_t Offset;
  2006. const Value *Base =
  2007. getMinimalBaseOfPointer(A, QueryingAA, Loc->Ptr, Offset, DL);
  2008. if (Base && Base == &AssociatedValue) {
  2009. int64_t DerefBytes = Loc->Size.getValue() + Offset;
  2010. IsNonNull |= !NullPointerIsDefined;
  2011. return std::max(int64_t(0), DerefBytes);
  2012. }
  2013. /// Corner case when an offset is 0.
  2014. Base = GetPointerBaseWithConstantOffset(Loc->Ptr, Offset, DL,
  2015. /*AllowNonInbounds*/ true);
  2016. if (Base && Base == &AssociatedValue && Offset == 0) {
  2017. int64_t DerefBytes = Loc->Size.getValue();
  2018. IsNonNull |= !NullPointerIsDefined;
  2019. return std::max(int64_t(0), DerefBytes);
  2020. }
  2021. return 0;
  2022. }
  2023. struct AANonNullImpl : AANonNull {
  2024. AANonNullImpl(const IRPosition &IRP, Attributor &A)
  2025. : AANonNull(IRP, A),
  2026. NullIsDefined(NullPointerIsDefined(
  2027. getAnchorScope(),
  2028. getAssociatedValue().getType()->getPointerAddressSpace())) {}
  2029. /// See AbstractAttribute::initialize(...).
  2030. void initialize(Attributor &A) override {
  2031. Value &V = getAssociatedValue();
  2032. if (!NullIsDefined &&
  2033. hasAttr({Attribute::NonNull, Attribute::Dereferenceable},
  2034. /* IgnoreSubsumingPositions */ false, &A)) {
  2035. indicateOptimisticFixpoint();
  2036. return;
  2037. }
  2038. if (isa<ConstantPointerNull>(V)) {
  2039. indicatePessimisticFixpoint();
  2040. return;
  2041. }
  2042. AANonNull::initialize(A);
  2043. bool CanBeNull, CanBeFreed;
  2044. if (V.getPointerDereferenceableBytes(A.getDataLayout(), CanBeNull,
  2045. CanBeFreed)) {
  2046. if (!CanBeNull) {
  2047. indicateOptimisticFixpoint();
  2048. return;
  2049. }
  2050. }
  2051. if (isa<GlobalValue>(&getAssociatedValue())) {
  2052. indicatePessimisticFixpoint();
  2053. return;
  2054. }
  2055. if (Instruction *CtxI = getCtxI())
  2056. followUsesInMBEC(*this, A, getState(), *CtxI);
  2057. }
  2058. /// See followUsesInMBEC
  2059. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  2060. AANonNull::StateType &State) {
  2061. bool IsNonNull = false;
  2062. bool TrackUse = false;
  2063. getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
  2064. IsNonNull, TrackUse);
  2065. State.setKnown(IsNonNull);
  2066. return TrackUse;
  2067. }
  2068. /// See AbstractAttribute::getAsStr().
  2069. const std::string getAsStr() const override {
  2070. return getAssumed() ? "nonnull" : "may-null";
  2071. }
  2072. /// Flag to determine if the underlying value can be null and still allow
  2073. /// valid accesses.
  2074. const bool NullIsDefined;
  2075. };
  2076. /// NonNull attribute for a floating value.
  2077. struct AANonNullFloating : public AANonNullImpl {
  2078. AANonNullFloating(const IRPosition &IRP, Attributor &A)
  2079. : AANonNullImpl(IRP, A) {}
  2080. /// See AbstractAttribute::updateImpl(...).
  2081. ChangeStatus updateImpl(Attributor &A) override {
  2082. const DataLayout &DL = A.getDataLayout();
  2083. DominatorTree *DT = nullptr;
  2084. AssumptionCache *AC = nullptr;
  2085. InformationCache &InfoCache = A.getInfoCache();
  2086. if (const Function *Fn = getAnchorScope()) {
  2087. DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
  2088. AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*Fn);
  2089. }
  2090. auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
  2091. AANonNull::StateType &T, bool Stripped) -> bool {
  2092. const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V),
  2093. DepClassTy::REQUIRED);
  2094. if (!Stripped && this == &AA) {
  2095. if (!isKnownNonZero(&V, DL, 0, AC, CtxI, DT))
  2096. T.indicatePessimisticFixpoint();
  2097. } else {
  2098. // Use abstract attribute information.
  2099. const AANonNull::StateType &NS = AA.getState();
  2100. T ^= NS;
  2101. }
  2102. return T.isValidState();
  2103. };
  2104. StateType T;
  2105. bool UsedAssumedInformation = false;
  2106. if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
  2107. VisitValueCB, getCtxI(),
  2108. UsedAssumedInformation))
  2109. return indicatePessimisticFixpoint();
  2110. return clampStateAndIndicateChange(getState(), T);
  2111. }
  2112. /// See AbstractAttribute::trackStatistics()
  2113. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
  2114. };
  2115. /// NonNull attribute for function return value.
  2116. struct AANonNullReturned final
  2117. : AAReturnedFromReturnedValues<AANonNull, AANonNull> {
  2118. AANonNullReturned(const IRPosition &IRP, Attributor &A)
  2119. : AAReturnedFromReturnedValues<AANonNull, AANonNull>(IRP, A) {}
  2120. /// See AbstractAttribute::getAsStr().
  2121. const std::string getAsStr() const override {
  2122. return getAssumed() ? "nonnull" : "may-null";
  2123. }
  2124. /// See AbstractAttribute::trackStatistics()
  2125. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
  2126. };
  2127. /// NonNull attribute for function argument.
  2128. struct AANonNullArgument final
  2129. : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl> {
  2130. AANonNullArgument(const IRPosition &IRP, Attributor &A)
  2131. : AAArgumentFromCallSiteArguments<AANonNull, AANonNullImpl>(IRP, A) {}
  2132. /// See AbstractAttribute::trackStatistics()
  2133. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
  2134. };
  2135. struct AANonNullCallSiteArgument final : AANonNullFloating {
  2136. AANonNullCallSiteArgument(const IRPosition &IRP, Attributor &A)
  2137. : AANonNullFloating(IRP, A) {}
  2138. /// See AbstractAttribute::trackStatistics()
  2139. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
  2140. };
  2141. /// NonNull attribute for a call site return position.
  2142. struct AANonNullCallSiteReturned final
  2143. : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl> {
  2144. AANonNullCallSiteReturned(const IRPosition &IRP, Attributor &A)
  2145. : AACallSiteReturnedFromReturned<AANonNull, AANonNullImpl>(IRP, A) {}
  2146. /// See AbstractAttribute::trackStatistics()
  2147. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
  2148. };
  2149. /// ------------------------ No-Recurse Attributes ----------------------------
  2150. struct AANoRecurseImpl : public AANoRecurse {
  2151. AANoRecurseImpl(const IRPosition &IRP, Attributor &A) : AANoRecurse(IRP, A) {}
  2152. /// See AbstractAttribute::getAsStr()
  2153. const std::string getAsStr() const override {
  2154. return getAssumed() ? "norecurse" : "may-recurse";
  2155. }
  2156. };
  2157. struct AANoRecurseFunction final : AANoRecurseImpl {
  2158. AANoRecurseFunction(const IRPosition &IRP, Attributor &A)
  2159. : AANoRecurseImpl(IRP, A) {}
  2160. /// See AbstractAttribute::updateImpl(...).
  2161. ChangeStatus updateImpl(Attributor &A) override {
  2162. // If all live call sites are known to be no-recurse, we are as well.
  2163. auto CallSitePred = [&](AbstractCallSite ACS) {
  2164. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  2165. *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
  2166. DepClassTy::NONE);
  2167. return NoRecurseAA.isKnownNoRecurse();
  2168. };
  2169. bool UsedAssumedInformation = false;
  2170. if (A.checkForAllCallSites(CallSitePred, *this, true,
  2171. UsedAssumedInformation)) {
  2172. // If we know all call sites and all are known no-recurse, we are done.
  2173. // If all known call sites, which might not be all that exist, are known
  2174. // to be no-recurse, we are not done but we can continue to assume
  2175. // no-recurse. If one of the call sites we have not visited will become
  2176. // live, another update is triggered.
  2177. if (!UsedAssumedInformation)
  2178. indicateOptimisticFixpoint();
  2179. return ChangeStatus::UNCHANGED;
  2180. }
  2181. const AAFunctionReachability &EdgeReachability =
  2182. A.getAAFor<AAFunctionReachability>(*this, getIRPosition(),
  2183. DepClassTy::REQUIRED);
  2184. if (EdgeReachability.canReach(A, *getAnchorScope()))
  2185. return indicatePessimisticFixpoint();
  2186. return ChangeStatus::UNCHANGED;
  2187. }
  2188. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
  2189. };
  2190. /// NoRecurse attribute deduction for a call sites.
  2191. struct AANoRecurseCallSite final : AANoRecurseImpl {
  2192. AANoRecurseCallSite(const IRPosition &IRP, Attributor &A)
  2193. : AANoRecurseImpl(IRP, A) {}
  2194. /// See AbstractAttribute::initialize(...).
  2195. void initialize(Attributor &A) override {
  2196. AANoRecurseImpl::initialize(A);
  2197. Function *F = getAssociatedFunction();
  2198. if (!F || F->isDeclaration())
  2199. indicatePessimisticFixpoint();
  2200. }
  2201. /// See AbstractAttribute::updateImpl(...).
  2202. ChangeStatus updateImpl(Attributor &A) override {
  2203. // TODO: Once we have call site specific value information we can provide
  2204. // call site specific liveness information and then it makes
  2205. // sense to specialize attributes for call sites arguments instead of
  2206. // redirecting requests to the callee argument.
  2207. Function *F = getAssociatedFunction();
  2208. const IRPosition &FnPos = IRPosition::function(*F);
  2209. auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos, DepClassTy::REQUIRED);
  2210. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2211. }
  2212. /// See AbstractAttribute::trackStatistics()
  2213. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
  2214. };
  2215. /// -------------------- Undefined-Behavior Attributes ------------------------
  2216. struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
  2217. AAUndefinedBehaviorImpl(const IRPosition &IRP, Attributor &A)
  2218. : AAUndefinedBehavior(IRP, A) {}
  2219. /// See AbstractAttribute::updateImpl(...).
  2220. // through a pointer (i.e. also branches etc.)
  2221. ChangeStatus updateImpl(Attributor &A) override {
  2222. const size_t UBPrevSize = KnownUBInsts.size();
  2223. const size_t NoUBPrevSize = AssumedNoUBInsts.size();
  2224. auto InspectMemAccessInstForUB = [&](Instruction &I) {
  2225. // Lang ref now states volatile store is not UB, let's skip them.
  2226. if (I.isVolatile() && I.mayWriteToMemory())
  2227. return true;
  2228. // Skip instructions that are already saved.
  2229. if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
  2230. return true;
  2231. // If we reach here, we know we have an instruction
  2232. // that accesses memory through a pointer operand,
  2233. // for which getPointerOperand() should give it to us.
  2234. Value *PtrOp =
  2235. const_cast<Value *>(getPointerOperand(&I, /* AllowVolatile */ true));
  2236. assert(PtrOp &&
  2237. "Expected pointer operand of memory accessing instruction");
  2238. // Either we stopped and the appropriate action was taken,
  2239. // or we got back a simplified value to continue.
  2240. Optional<Value *> SimplifiedPtrOp = stopOnUndefOrAssumed(A, PtrOp, &I);
  2241. if (!SimplifiedPtrOp.hasValue() || !SimplifiedPtrOp.getValue())
  2242. return true;
  2243. const Value *PtrOpVal = SimplifiedPtrOp.getValue();
  2244. // A memory access through a pointer is considered UB
  2245. // only if the pointer has constant null value.
  2246. // TODO: Expand it to not only check constant values.
  2247. if (!isa<ConstantPointerNull>(PtrOpVal)) {
  2248. AssumedNoUBInsts.insert(&I);
  2249. return true;
  2250. }
  2251. const Type *PtrTy = PtrOpVal->getType();
  2252. // Because we only consider instructions inside functions,
  2253. // assume that a parent function exists.
  2254. const Function *F = I.getFunction();
  2255. // A memory access using constant null pointer is only considered UB
  2256. // if null pointer is _not_ defined for the target platform.
  2257. if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
  2258. AssumedNoUBInsts.insert(&I);
  2259. else
  2260. KnownUBInsts.insert(&I);
  2261. return true;
  2262. };
  2263. auto InspectBrInstForUB = [&](Instruction &I) {
  2264. // A conditional branch instruction is considered UB if it has `undef`
  2265. // condition.
  2266. // Skip instructions that are already saved.
  2267. if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
  2268. return true;
  2269. // We know we have a branch instruction.
  2270. auto *BrInst = cast<BranchInst>(&I);
  2271. // Unconditional branches are never considered UB.
  2272. if (BrInst->isUnconditional())
  2273. return true;
  2274. // Either we stopped and the appropriate action was taken,
  2275. // or we got back a simplified value to continue.
  2276. Optional<Value *> SimplifiedCond =
  2277. stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
  2278. if (!SimplifiedCond.hasValue() || !SimplifiedCond.getValue())
  2279. return true;
  2280. AssumedNoUBInsts.insert(&I);
  2281. return true;
  2282. };
  2283. auto InspectCallSiteForUB = [&](Instruction &I) {
  2284. // Check whether a callsite always cause UB or not
  2285. // Skip instructions that are already saved.
  2286. if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
  2287. return true;
  2288. // Check nonnull and noundef argument attribute violation for each
  2289. // callsite.
  2290. CallBase &CB = cast<CallBase>(I);
  2291. Function *Callee = CB.getCalledFunction();
  2292. if (!Callee)
  2293. return true;
  2294. for (unsigned idx = 0; idx < CB.arg_size(); idx++) {
  2295. // If current argument is known to be simplified to null pointer and the
  2296. // corresponding argument position is known to have nonnull attribute,
  2297. // the argument is poison. Furthermore, if the argument is poison and
  2298. // the position is known to have noundef attriubte, this callsite is
  2299. // considered UB.
  2300. if (idx >= Callee->arg_size())
  2301. break;
  2302. Value *ArgVal = CB.getArgOperand(idx);
  2303. if (!ArgVal)
  2304. continue;
  2305. // Here, we handle three cases.
  2306. // (1) Not having a value means it is dead. (we can replace the value
  2307. // with undef)
  2308. // (2) Simplified to undef. The argument violate noundef attriubte.
  2309. // (3) Simplified to null pointer where known to be nonnull.
  2310. // The argument is a poison value and violate noundef attribute.
  2311. IRPosition CalleeArgumentIRP = IRPosition::callsite_argument(CB, idx);
  2312. auto &NoUndefAA =
  2313. A.getAAFor<AANoUndef>(*this, CalleeArgumentIRP, DepClassTy::NONE);
  2314. if (!NoUndefAA.isKnownNoUndef())
  2315. continue;
  2316. bool UsedAssumedInformation = false;
  2317. Optional<Value *> SimplifiedVal = A.getAssumedSimplified(
  2318. IRPosition::value(*ArgVal), *this, UsedAssumedInformation);
  2319. if (UsedAssumedInformation)
  2320. continue;
  2321. if (SimplifiedVal.hasValue() && !SimplifiedVal.getValue())
  2322. return true;
  2323. if (!SimplifiedVal.hasValue() ||
  2324. isa<UndefValue>(*SimplifiedVal.getValue())) {
  2325. KnownUBInsts.insert(&I);
  2326. continue;
  2327. }
  2328. if (!ArgVal->getType()->isPointerTy() ||
  2329. !isa<ConstantPointerNull>(*SimplifiedVal.getValue()))
  2330. continue;
  2331. auto &NonNullAA =
  2332. A.getAAFor<AANonNull>(*this, CalleeArgumentIRP, DepClassTy::NONE);
  2333. if (NonNullAA.isKnownNonNull())
  2334. KnownUBInsts.insert(&I);
  2335. }
  2336. return true;
  2337. };
  2338. auto InspectReturnInstForUB = [&](Instruction &I) {
  2339. auto &RI = cast<ReturnInst>(I);
  2340. // Either we stopped and the appropriate action was taken,
  2341. // or we got back a simplified return value to continue.
  2342. Optional<Value *> SimplifiedRetValue =
  2343. stopOnUndefOrAssumed(A, RI.getReturnValue(), &I);
  2344. if (!SimplifiedRetValue.hasValue() || !SimplifiedRetValue.getValue())
  2345. return true;
  2346. // Check if a return instruction always cause UB or not
  2347. // Note: It is guaranteed that the returned position of the anchor
  2348. // scope has noundef attribute when this is called.
  2349. // We also ensure the return position is not "assumed dead"
  2350. // because the returned value was then potentially simplified to
  2351. // `undef` in AAReturnedValues without removing the `noundef`
  2352. // attribute yet.
  2353. // When the returned position has noundef attriubte, UB occurs in the
  2354. // following cases.
  2355. // (1) Returned value is known to be undef.
  2356. // (2) The value is known to be a null pointer and the returned
  2357. // position has nonnull attribute (because the returned value is
  2358. // poison).
  2359. if (isa<ConstantPointerNull>(*SimplifiedRetValue)) {
  2360. auto &NonNullAA = A.getAAFor<AANonNull>(
  2361. *this, IRPosition::returned(*getAnchorScope()), DepClassTy::NONE);
  2362. if (NonNullAA.isKnownNonNull())
  2363. KnownUBInsts.insert(&I);
  2364. }
  2365. return true;
  2366. };
  2367. bool UsedAssumedInformation = false;
  2368. A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
  2369. {Instruction::Load, Instruction::Store,
  2370. Instruction::AtomicCmpXchg,
  2371. Instruction::AtomicRMW},
  2372. UsedAssumedInformation,
  2373. /* CheckBBLivenessOnly */ true);
  2374. A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
  2375. UsedAssumedInformation,
  2376. /* CheckBBLivenessOnly */ true);
  2377. A.checkForAllCallLikeInstructions(InspectCallSiteForUB, *this,
  2378. UsedAssumedInformation);
  2379. // If the returned position of the anchor scope has noundef attriubte, check
  2380. // all returned instructions.
  2381. if (!getAnchorScope()->getReturnType()->isVoidTy()) {
  2382. const IRPosition &ReturnIRP = IRPosition::returned(*getAnchorScope());
  2383. if (!A.isAssumedDead(ReturnIRP, this, nullptr, UsedAssumedInformation)) {
  2384. auto &RetPosNoUndefAA =
  2385. A.getAAFor<AANoUndef>(*this, ReturnIRP, DepClassTy::NONE);
  2386. if (RetPosNoUndefAA.isKnownNoUndef())
  2387. A.checkForAllInstructions(InspectReturnInstForUB, *this,
  2388. {Instruction::Ret}, UsedAssumedInformation,
  2389. /* CheckBBLivenessOnly */ true);
  2390. }
  2391. }
  2392. if (NoUBPrevSize != AssumedNoUBInsts.size() ||
  2393. UBPrevSize != KnownUBInsts.size())
  2394. return ChangeStatus::CHANGED;
  2395. return ChangeStatus::UNCHANGED;
  2396. }
  2397. bool isKnownToCauseUB(Instruction *I) const override {
  2398. return KnownUBInsts.count(I);
  2399. }
  2400. bool isAssumedToCauseUB(Instruction *I) const override {
  2401. // In simple words, if an instruction is not in the assumed to _not_
  2402. // cause UB, then it is assumed UB (that includes those
  2403. // in the KnownUBInsts set). The rest is boilerplate
  2404. // is to ensure that it is one of the instructions we test
  2405. // for UB.
  2406. switch (I->getOpcode()) {
  2407. case Instruction::Load:
  2408. case Instruction::Store:
  2409. case Instruction::AtomicCmpXchg:
  2410. case Instruction::AtomicRMW:
  2411. return !AssumedNoUBInsts.count(I);
  2412. case Instruction::Br: {
  2413. auto BrInst = cast<BranchInst>(I);
  2414. if (BrInst->isUnconditional())
  2415. return false;
  2416. return !AssumedNoUBInsts.count(I);
  2417. } break;
  2418. default:
  2419. return false;
  2420. }
  2421. return false;
  2422. }
  2423. ChangeStatus manifest(Attributor &A) override {
  2424. if (KnownUBInsts.empty())
  2425. return ChangeStatus::UNCHANGED;
  2426. for (Instruction *I : KnownUBInsts)
  2427. A.changeToUnreachableAfterManifest(I);
  2428. return ChangeStatus::CHANGED;
  2429. }
  2430. /// See AbstractAttribute::getAsStr()
  2431. const std::string getAsStr() const override {
  2432. return getAssumed() ? "undefined-behavior" : "no-ub";
  2433. }
  2434. /// Note: The correctness of this analysis depends on the fact that the
  2435. /// following 2 sets will stop changing after some point.
  2436. /// "Change" here means that their size changes.
  2437. /// The size of each set is monotonically increasing
  2438. /// (we only add items to them) and it is upper bounded by the number of
  2439. /// instructions in the processed function (we can never save more
  2440. /// elements in either set than this number). Hence, at some point,
  2441. /// they will stop increasing.
  2442. /// Consequently, at some point, both sets will have stopped
  2443. /// changing, effectively making the analysis reach a fixpoint.
  2444. /// Note: These 2 sets are disjoint and an instruction can be considered
  2445. /// one of 3 things:
  2446. /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
  2447. /// the KnownUBInsts set.
  2448. /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
  2449. /// has a reason to assume it).
  2450. /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
  2451. /// could not find a reason to assume or prove that it can cause UB,
  2452. /// hence it assumes it doesn't. We have a set for these instructions
  2453. /// so that we don't reprocess them in every update.
  2454. /// Note however that instructions in this set may cause UB.
  2455. protected:
  2456. /// A set of all live instructions _known_ to cause UB.
  2457. SmallPtrSet<Instruction *, 8> KnownUBInsts;
  2458. private:
  2459. /// A set of all the (live) instructions that are assumed to _not_ cause UB.
  2460. SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
  2461. // Should be called on updates in which if we're processing an instruction
  2462. // \p I that depends on a value \p V, one of the following has to happen:
  2463. // - If the value is assumed, then stop.
  2464. // - If the value is known but undef, then consider it UB.
  2465. // - Otherwise, do specific processing with the simplified value.
  2466. // We return None in the first 2 cases to signify that an appropriate
  2467. // action was taken and the caller should stop.
  2468. // Otherwise, we return the simplified value that the caller should
  2469. // use for specific processing.
  2470. Optional<Value *> stopOnUndefOrAssumed(Attributor &A, Value *V,
  2471. Instruction *I) {
  2472. bool UsedAssumedInformation = false;
  2473. Optional<Value *> SimplifiedV = A.getAssumedSimplified(
  2474. IRPosition::value(*V), *this, UsedAssumedInformation);
  2475. if (!UsedAssumedInformation) {
  2476. // Don't depend on assumed values.
  2477. if (!SimplifiedV.hasValue()) {
  2478. // If it is known (which we tested above) but it doesn't have a value,
  2479. // then we can assume `undef` and hence the instruction is UB.
  2480. KnownUBInsts.insert(I);
  2481. return llvm::None;
  2482. }
  2483. if (!SimplifiedV.getValue())
  2484. return nullptr;
  2485. V = *SimplifiedV;
  2486. }
  2487. if (isa<UndefValue>(V)) {
  2488. KnownUBInsts.insert(I);
  2489. return llvm::None;
  2490. }
  2491. return V;
  2492. }
  2493. };
  2494. struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
  2495. AAUndefinedBehaviorFunction(const IRPosition &IRP, Attributor &A)
  2496. : AAUndefinedBehaviorImpl(IRP, A) {}
  2497. /// See AbstractAttribute::trackStatistics()
  2498. void trackStatistics() const override {
  2499. STATS_DECL(UndefinedBehaviorInstruction, Instruction,
  2500. "Number of instructions known to have UB");
  2501. BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
  2502. KnownUBInsts.size();
  2503. }
  2504. };
  2505. /// ------------------------ Will-Return Attributes ----------------------------
  2506. // Helper function that checks whether a function has any cycle which we don't
  2507. // know if it is bounded or not.
  2508. // Loops with maximum trip count are considered bounded, any other cycle not.
  2509. static bool mayContainUnboundedCycle(Function &F, Attributor &A) {
  2510. ScalarEvolution *SE =
  2511. A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(F);
  2512. LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(F);
  2513. // If either SCEV or LoopInfo is not available for the function then we assume
  2514. // any cycle to be unbounded cycle.
  2515. // We use scc_iterator which uses Tarjan algorithm to find all the maximal
  2516. // SCCs.To detect if there's a cycle, we only need to find the maximal ones.
  2517. if (!SE || !LI) {
  2518. for (scc_iterator<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI)
  2519. if (SCCI.hasCycle())
  2520. return true;
  2521. return false;
  2522. }
  2523. // If there's irreducible control, the function may contain non-loop cycles.
  2524. if (mayContainIrreducibleControl(F, LI))
  2525. return true;
  2526. // Any loop that does not have a max trip count is considered unbounded cycle.
  2527. for (auto *L : LI->getLoopsInPreorder()) {
  2528. if (!SE->getSmallConstantMaxTripCount(L))
  2529. return true;
  2530. }
  2531. return false;
  2532. }
  2533. struct AAWillReturnImpl : public AAWillReturn {
  2534. AAWillReturnImpl(const IRPosition &IRP, Attributor &A)
  2535. : AAWillReturn(IRP, A) {}
  2536. /// See AbstractAttribute::initialize(...).
  2537. void initialize(Attributor &A) override {
  2538. AAWillReturn::initialize(A);
  2539. if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ true)) {
  2540. indicateOptimisticFixpoint();
  2541. return;
  2542. }
  2543. }
  2544. /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
  2545. bool isImpliedByMustprogressAndReadonly(Attributor &A, bool KnownOnly) {
  2546. // Check for `mustprogress` in the scope and the associated function which
  2547. // might be different if this is a call site.
  2548. if ((!getAnchorScope() || !getAnchorScope()->mustProgress()) &&
  2549. (!getAssociatedFunction() || !getAssociatedFunction()->mustProgress()))
  2550. return false;
  2551. bool IsKnown;
  2552. if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
  2553. return IsKnown || !KnownOnly;
  2554. return false;
  2555. }
  2556. /// See AbstractAttribute::updateImpl(...).
  2557. ChangeStatus updateImpl(Attributor &A) override {
  2558. if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
  2559. return ChangeStatus::UNCHANGED;
  2560. auto CheckForWillReturn = [&](Instruction &I) {
  2561. IRPosition IPos = IRPosition::callsite_function(cast<CallBase>(I));
  2562. const auto &WillReturnAA =
  2563. A.getAAFor<AAWillReturn>(*this, IPos, DepClassTy::REQUIRED);
  2564. if (WillReturnAA.isKnownWillReturn())
  2565. return true;
  2566. if (!WillReturnAA.isAssumedWillReturn())
  2567. return false;
  2568. const auto &NoRecurseAA =
  2569. A.getAAFor<AANoRecurse>(*this, IPos, DepClassTy::REQUIRED);
  2570. return NoRecurseAA.isAssumedNoRecurse();
  2571. };
  2572. bool UsedAssumedInformation = false;
  2573. if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this,
  2574. UsedAssumedInformation))
  2575. return indicatePessimisticFixpoint();
  2576. return ChangeStatus::UNCHANGED;
  2577. }
  2578. /// See AbstractAttribute::getAsStr()
  2579. const std::string getAsStr() const override {
  2580. return getAssumed() ? "willreturn" : "may-noreturn";
  2581. }
  2582. };
  2583. struct AAWillReturnFunction final : AAWillReturnImpl {
  2584. AAWillReturnFunction(const IRPosition &IRP, Attributor &A)
  2585. : AAWillReturnImpl(IRP, A) {}
  2586. /// See AbstractAttribute::initialize(...).
  2587. void initialize(Attributor &A) override {
  2588. AAWillReturnImpl::initialize(A);
  2589. Function *F = getAnchorScope();
  2590. if (!F || F->isDeclaration() || mayContainUnboundedCycle(*F, A))
  2591. indicatePessimisticFixpoint();
  2592. }
  2593. /// See AbstractAttribute::trackStatistics()
  2594. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
  2595. };
  2596. /// WillReturn attribute deduction for a call sites.
  2597. struct AAWillReturnCallSite final : AAWillReturnImpl {
  2598. AAWillReturnCallSite(const IRPosition &IRP, Attributor &A)
  2599. : AAWillReturnImpl(IRP, A) {}
  2600. /// See AbstractAttribute::initialize(...).
  2601. void initialize(Attributor &A) override {
  2602. AAWillReturnImpl::initialize(A);
  2603. Function *F = getAssociatedFunction();
  2604. if (!F || !A.isFunctionIPOAmendable(*F))
  2605. indicatePessimisticFixpoint();
  2606. }
  2607. /// See AbstractAttribute::updateImpl(...).
  2608. ChangeStatus updateImpl(Attributor &A) override {
  2609. if (isImpliedByMustprogressAndReadonly(A, /* KnownOnly */ false))
  2610. return ChangeStatus::UNCHANGED;
  2611. // TODO: Once we have call site specific value information we can provide
  2612. // call site specific liveness information and then it makes
  2613. // sense to specialize attributes for call sites arguments instead of
  2614. // redirecting requests to the callee argument.
  2615. Function *F = getAssociatedFunction();
  2616. const IRPosition &FnPos = IRPosition::function(*F);
  2617. auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos, DepClassTy::REQUIRED);
  2618. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2619. }
  2620. /// See AbstractAttribute::trackStatistics()
  2621. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
  2622. };
  2623. /// -------------------AAReachability Attribute--------------------------
  2624. struct AAReachabilityImpl : AAReachability {
  2625. AAReachabilityImpl(const IRPosition &IRP, Attributor &A)
  2626. : AAReachability(IRP, A) {}
  2627. const std::string getAsStr() const override {
  2628. // TODO: Return the number of reachable queries.
  2629. return "reachable";
  2630. }
  2631. /// See AbstractAttribute::updateImpl(...).
  2632. ChangeStatus updateImpl(Attributor &A) override {
  2633. const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
  2634. *this, IRPosition::function(*getAnchorScope()), DepClassTy::REQUIRED);
  2635. if (!NoRecurseAA.isAssumedNoRecurse())
  2636. return indicatePessimisticFixpoint();
  2637. return ChangeStatus::UNCHANGED;
  2638. }
  2639. };
  2640. struct AAReachabilityFunction final : public AAReachabilityImpl {
  2641. AAReachabilityFunction(const IRPosition &IRP, Attributor &A)
  2642. : AAReachabilityImpl(IRP, A) {}
  2643. /// See AbstractAttribute::trackStatistics()
  2644. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
  2645. };
  2646. /// ------------------------ NoAlias Argument Attribute ------------------------
  2647. struct AANoAliasImpl : AANoAlias {
  2648. AANoAliasImpl(const IRPosition &IRP, Attributor &A) : AANoAlias(IRP, A) {
  2649. assert(getAssociatedType()->isPointerTy() &&
  2650. "Noalias is a pointer attribute");
  2651. }
  2652. const std::string getAsStr() const override {
  2653. return getAssumed() ? "noalias" : "may-alias";
  2654. }
  2655. };
  2656. /// NoAlias attribute for a floating value.
  2657. struct AANoAliasFloating final : AANoAliasImpl {
  2658. AANoAliasFloating(const IRPosition &IRP, Attributor &A)
  2659. : AANoAliasImpl(IRP, A) {}
  2660. /// See AbstractAttribute::initialize(...).
  2661. void initialize(Attributor &A) override {
  2662. AANoAliasImpl::initialize(A);
  2663. Value *Val = &getAssociatedValue();
  2664. do {
  2665. CastInst *CI = dyn_cast<CastInst>(Val);
  2666. if (!CI)
  2667. break;
  2668. Value *Base = CI->getOperand(0);
  2669. if (!Base->hasOneUse())
  2670. break;
  2671. Val = Base;
  2672. } while (true);
  2673. if (!Val->getType()->isPointerTy()) {
  2674. indicatePessimisticFixpoint();
  2675. return;
  2676. }
  2677. if (isa<AllocaInst>(Val))
  2678. indicateOptimisticFixpoint();
  2679. else if (isa<ConstantPointerNull>(Val) &&
  2680. !NullPointerIsDefined(getAnchorScope(),
  2681. Val->getType()->getPointerAddressSpace()))
  2682. indicateOptimisticFixpoint();
  2683. else if (Val != &getAssociatedValue()) {
  2684. const auto &ValNoAliasAA = A.getAAFor<AANoAlias>(
  2685. *this, IRPosition::value(*Val), DepClassTy::OPTIONAL);
  2686. if (ValNoAliasAA.isKnownNoAlias())
  2687. indicateOptimisticFixpoint();
  2688. }
  2689. }
  2690. /// See AbstractAttribute::updateImpl(...).
  2691. ChangeStatus updateImpl(Attributor &A) override {
  2692. // TODO: Implement this.
  2693. return indicatePessimisticFixpoint();
  2694. }
  2695. /// See AbstractAttribute::trackStatistics()
  2696. void trackStatistics() const override {
  2697. STATS_DECLTRACK_FLOATING_ATTR(noalias)
  2698. }
  2699. };
  2700. /// NoAlias attribute for an argument.
  2701. struct AANoAliasArgument final
  2702. : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
  2703. using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
  2704. AANoAliasArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
  2705. /// See AbstractAttribute::initialize(...).
  2706. void initialize(Attributor &A) override {
  2707. Base::initialize(A);
  2708. // See callsite argument attribute and callee argument attribute.
  2709. if (hasAttr({Attribute::ByVal}))
  2710. indicateOptimisticFixpoint();
  2711. }
  2712. /// See AbstractAttribute::update(...).
  2713. ChangeStatus updateImpl(Attributor &A) override {
  2714. // We have to make sure no-alias on the argument does not break
  2715. // synchronization when this is a callback argument, see also [1] below.
  2716. // If synchronization cannot be affected, we delegate to the base updateImpl
  2717. // function, otherwise we give up for now.
  2718. // If the function is no-sync, no-alias cannot break synchronization.
  2719. const auto &NoSyncAA =
  2720. A.getAAFor<AANoSync>(*this, IRPosition::function_scope(getIRPosition()),
  2721. DepClassTy::OPTIONAL);
  2722. if (NoSyncAA.isAssumedNoSync())
  2723. return Base::updateImpl(A);
  2724. // If the argument is read-only, no-alias cannot break synchronization.
  2725. bool IsKnown;
  2726. if (AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
  2727. return Base::updateImpl(A);
  2728. // If the argument is never passed through callbacks, no-alias cannot break
  2729. // synchronization.
  2730. bool UsedAssumedInformation = false;
  2731. if (A.checkForAllCallSites(
  2732. [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
  2733. true, UsedAssumedInformation))
  2734. return Base::updateImpl(A);
  2735. // TODO: add no-alias but make sure it doesn't break synchronization by
  2736. // introducing fake uses. See:
  2737. // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
  2738. // International Workshop on OpenMP 2018,
  2739. // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
  2740. return indicatePessimisticFixpoint();
  2741. }
  2742. /// See AbstractAttribute::trackStatistics()
  2743. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
  2744. };
  2745. struct AANoAliasCallSiteArgument final : AANoAliasImpl {
  2746. AANoAliasCallSiteArgument(const IRPosition &IRP, Attributor &A)
  2747. : AANoAliasImpl(IRP, A) {}
  2748. /// See AbstractAttribute::initialize(...).
  2749. void initialize(Attributor &A) override {
  2750. // See callsite argument attribute and callee argument attribute.
  2751. const auto &CB = cast<CallBase>(getAnchorValue());
  2752. if (CB.paramHasAttr(getCallSiteArgNo(), Attribute::NoAlias))
  2753. indicateOptimisticFixpoint();
  2754. Value &Val = getAssociatedValue();
  2755. if (isa<ConstantPointerNull>(Val) &&
  2756. !NullPointerIsDefined(getAnchorScope(),
  2757. Val.getType()->getPointerAddressSpace()))
  2758. indicateOptimisticFixpoint();
  2759. }
  2760. /// Determine if the underlying value may alias with the call site argument
  2761. /// \p OtherArgNo of \p ICS (= the underlying call site).
  2762. bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
  2763. const AAMemoryBehavior &MemBehaviorAA,
  2764. const CallBase &CB, unsigned OtherArgNo) {
  2765. // We do not need to worry about aliasing with the underlying IRP.
  2766. if (this->getCalleeArgNo() == (int)OtherArgNo)
  2767. return false;
  2768. // If it is not a pointer or pointer vector we do not alias.
  2769. const Value *ArgOp = CB.getArgOperand(OtherArgNo);
  2770. if (!ArgOp->getType()->isPtrOrPtrVectorTy())
  2771. return false;
  2772. auto &CBArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
  2773. *this, IRPosition::callsite_argument(CB, OtherArgNo), DepClassTy::NONE);
  2774. // If the argument is readnone, there is no read-write aliasing.
  2775. if (CBArgMemBehaviorAA.isAssumedReadNone()) {
  2776. A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
  2777. return false;
  2778. }
  2779. // If the argument is readonly and the underlying value is readonly, there
  2780. // is no read-write aliasing.
  2781. bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
  2782. if (CBArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
  2783. A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
  2784. A.recordDependence(CBArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
  2785. return false;
  2786. }
  2787. // We have to utilize actual alias analysis queries so we need the object.
  2788. if (!AAR)
  2789. AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
  2790. // Try to rule it out at the call site.
  2791. bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
  2792. LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
  2793. "callsite arguments: "
  2794. << getAssociatedValue() << " " << *ArgOp << " => "
  2795. << (IsAliasing ? "" : "no-") << "alias \n");
  2796. return IsAliasing;
  2797. }
  2798. bool
  2799. isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
  2800. const AAMemoryBehavior &MemBehaviorAA,
  2801. const AANoAlias &NoAliasAA) {
  2802. // We can deduce "noalias" if the following conditions hold.
  2803. // (i) Associated value is assumed to be noalias in the definition.
  2804. // (ii) Associated value is assumed to be no-capture in all the uses
  2805. // possibly executed before this callsite.
  2806. // (iii) There is no other pointer argument which could alias with the
  2807. // value.
  2808. bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
  2809. if (!AssociatedValueIsNoAliasAtDef) {
  2810. LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
  2811. << " is not no-alias at the definition\n");
  2812. return false;
  2813. }
  2814. A.recordDependence(NoAliasAA, *this, DepClassTy::OPTIONAL);
  2815. const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
  2816. const Function *ScopeFn = VIRP.getAnchorScope();
  2817. auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, VIRP, DepClassTy::NONE);
  2818. // Check whether the value is captured in the scope using AANoCapture.
  2819. // Look at CFG and check only uses possibly executed before this
  2820. // callsite.
  2821. auto UsePred = [&](const Use &U, bool &Follow) -> bool {
  2822. Instruction *UserI = cast<Instruction>(U.getUser());
  2823. // If UserI is the curr instruction and there is a single potential use of
  2824. // the value in UserI we allow the use.
  2825. // TODO: We should inspect the operands and allow those that cannot alias
  2826. // with the value.
  2827. if (UserI == getCtxI() && UserI->getNumOperands() == 1)
  2828. return true;
  2829. if (ScopeFn) {
  2830. const auto &ReachabilityAA = A.getAAFor<AAReachability>(
  2831. *this, IRPosition::function(*ScopeFn), DepClassTy::OPTIONAL);
  2832. if (!ReachabilityAA.isAssumedReachable(A, *UserI, *getCtxI()))
  2833. return true;
  2834. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  2835. if (CB->isArgOperand(&U)) {
  2836. unsigned ArgNo = CB->getArgOperandNo(&U);
  2837. const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
  2838. *this, IRPosition::callsite_argument(*CB, ArgNo),
  2839. DepClassTy::OPTIONAL);
  2840. if (NoCaptureAA.isAssumedNoCapture())
  2841. return true;
  2842. }
  2843. }
  2844. }
  2845. // For cases which can potentially have more users
  2846. if (isa<GetElementPtrInst>(U) || isa<BitCastInst>(U) || isa<PHINode>(U) ||
  2847. isa<SelectInst>(U)) {
  2848. Follow = true;
  2849. return true;
  2850. }
  2851. LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n");
  2852. return false;
  2853. };
  2854. if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
  2855. if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) {
  2856. LLVM_DEBUG(
  2857. dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
  2858. << " cannot be noalias as it is potentially captured\n");
  2859. return false;
  2860. }
  2861. }
  2862. A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
  2863. // Check there is no other pointer argument which could alias with the
  2864. // value passed at this call site.
  2865. // TODO: AbstractCallSite
  2866. const auto &CB = cast<CallBase>(getAnchorValue());
  2867. for (unsigned OtherArgNo = 0; OtherArgNo < CB.arg_size(); OtherArgNo++)
  2868. if (mayAliasWithArgument(A, AAR, MemBehaviorAA, CB, OtherArgNo))
  2869. return false;
  2870. return true;
  2871. }
  2872. /// See AbstractAttribute::updateImpl(...).
  2873. ChangeStatus updateImpl(Attributor &A) override {
  2874. // If the argument is readnone we are done as there are no accesses via the
  2875. // argument.
  2876. auto &MemBehaviorAA =
  2877. A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
  2878. if (MemBehaviorAA.isAssumedReadNone()) {
  2879. A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
  2880. return ChangeStatus::UNCHANGED;
  2881. }
  2882. const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
  2883. const auto &NoAliasAA =
  2884. A.getAAFor<AANoAlias>(*this, VIRP, DepClassTy::NONE);
  2885. AAResults *AAR = nullptr;
  2886. if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
  2887. NoAliasAA)) {
  2888. LLVM_DEBUG(
  2889. dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
  2890. return ChangeStatus::UNCHANGED;
  2891. }
  2892. return indicatePessimisticFixpoint();
  2893. }
  2894. /// See AbstractAttribute::trackStatistics()
  2895. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
  2896. };
  2897. /// NoAlias attribute for function return value.
  2898. struct AANoAliasReturned final : AANoAliasImpl {
  2899. AANoAliasReturned(const IRPosition &IRP, Attributor &A)
  2900. : AANoAliasImpl(IRP, A) {}
  2901. /// See AbstractAttribute::initialize(...).
  2902. void initialize(Attributor &A) override {
  2903. AANoAliasImpl::initialize(A);
  2904. Function *F = getAssociatedFunction();
  2905. if (!F || F->isDeclaration())
  2906. indicatePessimisticFixpoint();
  2907. }
  2908. /// See AbstractAttribute::updateImpl(...).
  2909. virtual ChangeStatus updateImpl(Attributor &A) override {
  2910. auto CheckReturnValue = [&](Value &RV) -> bool {
  2911. if (Constant *C = dyn_cast<Constant>(&RV))
  2912. if (C->isNullValue() || isa<UndefValue>(C))
  2913. return true;
  2914. /// For now, we can only deduce noalias if we have call sites.
  2915. /// FIXME: add more support.
  2916. if (!isa<CallBase>(&RV))
  2917. return false;
  2918. const IRPosition &RVPos = IRPosition::value(RV);
  2919. const auto &NoAliasAA =
  2920. A.getAAFor<AANoAlias>(*this, RVPos, DepClassTy::REQUIRED);
  2921. if (!NoAliasAA.isAssumedNoAlias())
  2922. return false;
  2923. const auto &NoCaptureAA =
  2924. A.getAAFor<AANoCapture>(*this, RVPos, DepClassTy::REQUIRED);
  2925. return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
  2926. };
  2927. if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
  2928. return indicatePessimisticFixpoint();
  2929. return ChangeStatus::UNCHANGED;
  2930. }
  2931. /// See AbstractAttribute::trackStatistics()
  2932. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
  2933. };
  2934. /// NoAlias attribute deduction for a call site return value.
  2935. struct AANoAliasCallSiteReturned final : AANoAliasImpl {
  2936. AANoAliasCallSiteReturned(const IRPosition &IRP, Attributor &A)
  2937. : AANoAliasImpl(IRP, A) {}
  2938. /// See AbstractAttribute::initialize(...).
  2939. void initialize(Attributor &A) override {
  2940. AANoAliasImpl::initialize(A);
  2941. Function *F = getAssociatedFunction();
  2942. if (!F || F->isDeclaration())
  2943. indicatePessimisticFixpoint();
  2944. }
  2945. /// See AbstractAttribute::updateImpl(...).
  2946. ChangeStatus updateImpl(Attributor &A) override {
  2947. // TODO: Once we have call site specific value information we can provide
  2948. // call site specific liveness information and then it makes
  2949. // sense to specialize attributes for call sites arguments instead of
  2950. // redirecting requests to the callee argument.
  2951. Function *F = getAssociatedFunction();
  2952. const IRPosition &FnPos = IRPosition::returned(*F);
  2953. auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos, DepClassTy::REQUIRED);
  2954. return clampStateAndIndicateChange(getState(), FnAA.getState());
  2955. }
  2956. /// See AbstractAttribute::trackStatistics()
  2957. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
  2958. };
  2959. /// -------------------AAIsDead Function Attribute-----------------------
  2960. struct AAIsDeadValueImpl : public AAIsDead {
  2961. AAIsDeadValueImpl(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
  2962. /// See AAIsDead::isAssumedDead().
  2963. bool isAssumedDead() const override { return isAssumed(IS_DEAD); }
  2964. /// See AAIsDead::isKnownDead().
  2965. bool isKnownDead() const override { return isKnown(IS_DEAD); }
  2966. /// See AAIsDead::isAssumedDead(BasicBlock *).
  2967. bool isAssumedDead(const BasicBlock *BB) const override { return false; }
  2968. /// See AAIsDead::isKnownDead(BasicBlock *).
  2969. bool isKnownDead(const BasicBlock *BB) const override { return false; }
  2970. /// See AAIsDead::isAssumedDead(Instruction *I).
  2971. bool isAssumedDead(const Instruction *I) const override {
  2972. return I == getCtxI() && isAssumedDead();
  2973. }
  2974. /// See AAIsDead::isKnownDead(Instruction *I).
  2975. bool isKnownDead(const Instruction *I) const override {
  2976. return isAssumedDead(I) && isKnownDead();
  2977. }
  2978. /// See AbstractAttribute::getAsStr().
  2979. const std::string getAsStr() const override {
  2980. return isAssumedDead() ? "assumed-dead" : "assumed-live";
  2981. }
  2982. /// Check if all uses are assumed dead.
  2983. bool areAllUsesAssumedDead(Attributor &A, Value &V) {
  2984. // Callers might not check the type, void has no uses.
  2985. if (V.getType()->isVoidTy())
  2986. return true;
  2987. // If we replace a value with a constant there are no uses left afterwards.
  2988. if (!isa<Constant>(V)) {
  2989. bool UsedAssumedInformation = false;
  2990. Optional<Constant *> C =
  2991. A.getAssumedConstant(V, *this, UsedAssumedInformation);
  2992. if (!C.hasValue() || *C)
  2993. return true;
  2994. }
  2995. auto UsePred = [&](const Use &U, bool &Follow) { return false; };
  2996. // Explicitly set the dependence class to required because we want a long
  2997. // chain of N dependent instructions to be considered live as soon as one is
  2998. // without going through N update cycles. This is not required for
  2999. // correctness.
  3000. return A.checkForAllUses(UsePred, *this, V, /* CheckBBLivenessOnly */ false,
  3001. DepClassTy::REQUIRED);
  3002. }
  3003. /// Determine if \p I is assumed to be side-effect free.
  3004. bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
  3005. if (!I || wouldInstructionBeTriviallyDead(I))
  3006. return true;
  3007. auto *CB = dyn_cast<CallBase>(I);
  3008. if (!CB || isa<IntrinsicInst>(CB))
  3009. return false;
  3010. const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
  3011. const auto &NoUnwindAA =
  3012. A.getAndUpdateAAFor<AANoUnwind>(*this, CallIRP, DepClassTy::NONE);
  3013. if (!NoUnwindAA.isAssumedNoUnwind())
  3014. return false;
  3015. if (!NoUnwindAA.isKnownNoUnwind())
  3016. A.recordDependence(NoUnwindAA, *this, DepClassTy::OPTIONAL);
  3017. bool IsKnown;
  3018. return AA::isAssumedReadOnly(A, CallIRP, *this, IsKnown);
  3019. }
  3020. };
  3021. struct AAIsDeadFloating : public AAIsDeadValueImpl {
  3022. AAIsDeadFloating(const IRPosition &IRP, Attributor &A)
  3023. : AAIsDeadValueImpl(IRP, A) {}
  3024. /// See AbstractAttribute::initialize(...).
  3025. void initialize(Attributor &A) override {
  3026. if (isa<UndefValue>(getAssociatedValue())) {
  3027. indicatePessimisticFixpoint();
  3028. return;
  3029. }
  3030. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  3031. if (!isAssumedSideEffectFree(A, I)) {
  3032. if (!isa_and_nonnull<StoreInst>(I))
  3033. indicatePessimisticFixpoint();
  3034. else
  3035. removeAssumedBits(HAS_NO_EFFECT);
  3036. }
  3037. }
  3038. bool isDeadStore(Attributor &A, StoreInst &SI) {
  3039. // Lang ref now states volatile store is not UB/dead, let's skip them.
  3040. if (SI.isVolatile())
  3041. return false;
  3042. bool UsedAssumedInformation = false;
  3043. SmallSetVector<Value *, 4> PotentialCopies;
  3044. if (!AA::getPotentialCopiesOfStoredValue(A, SI, PotentialCopies, *this,
  3045. UsedAssumedInformation))
  3046. return false;
  3047. return llvm::all_of(PotentialCopies, [&](Value *V) {
  3048. return A.isAssumedDead(IRPosition::value(*V), this, nullptr,
  3049. UsedAssumedInformation);
  3050. });
  3051. }
  3052. /// See AbstractAttribute::updateImpl(...).
  3053. ChangeStatus updateImpl(Attributor &A) override {
  3054. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  3055. if (auto *SI = dyn_cast_or_null<StoreInst>(I)) {
  3056. if (!isDeadStore(A, *SI))
  3057. return indicatePessimisticFixpoint();
  3058. } else {
  3059. if (!isAssumedSideEffectFree(A, I))
  3060. return indicatePessimisticFixpoint();
  3061. if (!areAllUsesAssumedDead(A, getAssociatedValue()))
  3062. return indicatePessimisticFixpoint();
  3063. }
  3064. return ChangeStatus::UNCHANGED;
  3065. }
  3066. /// See AbstractAttribute::manifest(...).
  3067. ChangeStatus manifest(Attributor &A) override {
  3068. Value &V = getAssociatedValue();
  3069. if (auto *I = dyn_cast<Instruction>(&V)) {
  3070. // If we get here we basically know the users are all dead. We check if
  3071. // isAssumedSideEffectFree returns true here again because it might not be
  3072. // the case and only the users are dead but the instruction (=call) is
  3073. // still needed.
  3074. if (isa<StoreInst>(I) ||
  3075. (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I))) {
  3076. A.deleteAfterManifest(*I);
  3077. return ChangeStatus::CHANGED;
  3078. }
  3079. }
  3080. if (V.use_empty())
  3081. return ChangeStatus::UNCHANGED;
  3082. bool UsedAssumedInformation = false;
  3083. Optional<Constant *> C =
  3084. A.getAssumedConstant(V, *this, UsedAssumedInformation);
  3085. if (C.hasValue() && C.getValue())
  3086. return ChangeStatus::UNCHANGED;
  3087. // Replace the value with undef as it is dead but keep droppable uses around
  3088. // as they provide information we don't want to give up on just yet.
  3089. UndefValue &UV = *UndefValue::get(V.getType());
  3090. bool AnyChange =
  3091. A.changeValueAfterManifest(V, UV, /* ChangeDropppable */ false);
  3092. return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  3093. }
  3094. /// See AbstractAttribute::trackStatistics()
  3095. void trackStatistics() const override {
  3096. STATS_DECLTRACK_FLOATING_ATTR(IsDead)
  3097. }
  3098. };
  3099. struct AAIsDeadArgument : public AAIsDeadFloating {
  3100. AAIsDeadArgument(const IRPosition &IRP, Attributor &A)
  3101. : AAIsDeadFloating(IRP, A) {}
  3102. /// See AbstractAttribute::initialize(...).
  3103. void initialize(Attributor &A) override {
  3104. if (!A.isFunctionIPOAmendable(*getAnchorScope()))
  3105. indicatePessimisticFixpoint();
  3106. }
  3107. /// See AbstractAttribute::manifest(...).
  3108. ChangeStatus manifest(Attributor &A) override {
  3109. ChangeStatus Changed = AAIsDeadFloating::manifest(A);
  3110. Argument &Arg = *getAssociatedArgument();
  3111. if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
  3112. if (A.registerFunctionSignatureRewrite(
  3113. Arg, /* ReplacementTypes */ {},
  3114. Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
  3115. Attributor::ArgumentReplacementInfo::ACSRepairCBTy{})) {
  3116. Arg.dropDroppableUses();
  3117. return ChangeStatus::CHANGED;
  3118. }
  3119. return Changed;
  3120. }
  3121. /// See AbstractAttribute::trackStatistics()
  3122. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
  3123. };
  3124. struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
  3125. AAIsDeadCallSiteArgument(const IRPosition &IRP, Attributor &A)
  3126. : AAIsDeadValueImpl(IRP, A) {}
  3127. /// See AbstractAttribute::initialize(...).
  3128. void initialize(Attributor &A) override {
  3129. if (isa<UndefValue>(getAssociatedValue()))
  3130. indicatePessimisticFixpoint();
  3131. }
  3132. /// See AbstractAttribute::updateImpl(...).
  3133. ChangeStatus updateImpl(Attributor &A) override {
  3134. // TODO: Once we have call site specific value information we can provide
  3135. // call site specific liveness information and then it makes
  3136. // sense to specialize attributes for call sites arguments instead of
  3137. // redirecting requests to the callee argument.
  3138. Argument *Arg = getAssociatedArgument();
  3139. if (!Arg)
  3140. return indicatePessimisticFixpoint();
  3141. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  3142. auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos, DepClassTy::REQUIRED);
  3143. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  3144. }
  3145. /// See AbstractAttribute::manifest(...).
  3146. ChangeStatus manifest(Attributor &A) override {
  3147. CallBase &CB = cast<CallBase>(getAnchorValue());
  3148. Use &U = CB.getArgOperandUse(getCallSiteArgNo());
  3149. assert(!isa<UndefValue>(U.get()) &&
  3150. "Expected undef values to be filtered out!");
  3151. UndefValue &UV = *UndefValue::get(U->getType());
  3152. if (A.changeUseAfterManifest(U, UV))
  3153. return ChangeStatus::CHANGED;
  3154. return ChangeStatus::UNCHANGED;
  3155. }
  3156. /// See AbstractAttribute::trackStatistics()
  3157. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
  3158. };
  3159. struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
  3160. AAIsDeadCallSiteReturned(const IRPosition &IRP, Attributor &A)
  3161. : AAIsDeadFloating(IRP, A), IsAssumedSideEffectFree(true) {}
  3162. /// See AAIsDead::isAssumedDead().
  3163. bool isAssumedDead() const override {
  3164. return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
  3165. }
  3166. /// See AbstractAttribute::initialize(...).
  3167. void initialize(Attributor &A) override {
  3168. if (isa<UndefValue>(getAssociatedValue())) {
  3169. indicatePessimisticFixpoint();
  3170. return;
  3171. }
  3172. // We track this separately as a secondary state.
  3173. IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
  3174. }
  3175. /// See AbstractAttribute::updateImpl(...).
  3176. ChangeStatus updateImpl(Attributor &A) override {
  3177. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  3178. if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
  3179. IsAssumedSideEffectFree = false;
  3180. Changed = ChangeStatus::CHANGED;
  3181. }
  3182. if (!areAllUsesAssumedDead(A, getAssociatedValue()))
  3183. return indicatePessimisticFixpoint();
  3184. return Changed;
  3185. }
  3186. /// See AbstractAttribute::trackStatistics()
  3187. void trackStatistics() const override {
  3188. if (IsAssumedSideEffectFree)
  3189. STATS_DECLTRACK_CSRET_ATTR(IsDead)
  3190. else
  3191. STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
  3192. }
  3193. /// See AbstractAttribute::getAsStr().
  3194. const std::string getAsStr() const override {
  3195. return isAssumedDead()
  3196. ? "assumed-dead"
  3197. : (getAssumed() ? "assumed-dead-users" : "assumed-live");
  3198. }
  3199. private:
  3200. bool IsAssumedSideEffectFree;
  3201. };
  3202. struct AAIsDeadReturned : public AAIsDeadValueImpl {
  3203. AAIsDeadReturned(const IRPosition &IRP, Attributor &A)
  3204. : AAIsDeadValueImpl(IRP, A) {}
  3205. /// See AbstractAttribute::updateImpl(...).
  3206. ChangeStatus updateImpl(Attributor &A) override {
  3207. bool UsedAssumedInformation = false;
  3208. A.checkForAllInstructions([](Instruction &) { return true; }, *this,
  3209. {Instruction::Ret}, UsedAssumedInformation);
  3210. auto PredForCallSite = [&](AbstractCallSite ACS) {
  3211. if (ACS.isCallbackCall() || !ACS.getInstruction())
  3212. return false;
  3213. return areAllUsesAssumedDead(A, *ACS.getInstruction());
  3214. };
  3215. if (!A.checkForAllCallSites(PredForCallSite, *this, true,
  3216. UsedAssumedInformation))
  3217. return indicatePessimisticFixpoint();
  3218. return ChangeStatus::UNCHANGED;
  3219. }
  3220. /// See AbstractAttribute::manifest(...).
  3221. ChangeStatus manifest(Attributor &A) override {
  3222. // TODO: Rewrite the signature to return void?
  3223. bool AnyChange = false;
  3224. UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
  3225. auto RetInstPred = [&](Instruction &I) {
  3226. ReturnInst &RI = cast<ReturnInst>(I);
  3227. if (!isa<UndefValue>(RI.getReturnValue()))
  3228. AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
  3229. return true;
  3230. };
  3231. bool UsedAssumedInformation = false;
  3232. A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret},
  3233. UsedAssumedInformation);
  3234. return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  3235. }
  3236. /// See AbstractAttribute::trackStatistics()
  3237. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
  3238. };
  3239. struct AAIsDeadFunction : public AAIsDead {
  3240. AAIsDeadFunction(const IRPosition &IRP, Attributor &A) : AAIsDead(IRP, A) {}
  3241. /// See AbstractAttribute::initialize(...).
  3242. void initialize(Attributor &A) override {
  3243. const Function *F = getAnchorScope();
  3244. if (F && !F->isDeclaration()) {
  3245. // We only want to compute liveness once. If the function is not part of
  3246. // the SCC, skip it.
  3247. if (A.isRunOn(*const_cast<Function *>(F))) {
  3248. ToBeExploredFrom.insert(&F->getEntryBlock().front());
  3249. assumeLive(A, F->getEntryBlock());
  3250. } else {
  3251. indicatePessimisticFixpoint();
  3252. }
  3253. }
  3254. }
  3255. /// See AbstractAttribute::getAsStr().
  3256. const std::string getAsStr() const override {
  3257. return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
  3258. std::to_string(getAnchorScope()->size()) + "][#TBEP " +
  3259. std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
  3260. std::to_string(KnownDeadEnds.size()) + "]";
  3261. }
  3262. /// See AbstractAttribute::manifest(...).
  3263. ChangeStatus manifest(Attributor &A) override {
  3264. assert(getState().isValidState() &&
  3265. "Attempted to manifest an invalid state!");
  3266. ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
  3267. Function &F = *getAnchorScope();
  3268. if (AssumedLiveBlocks.empty()) {
  3269. A.deleteAfterManifest(F);
  3270. return ChangeStatus::CHANGED;
  3271. }
  3272. // Flag to determine if we can change an invoke to a call assuming the
  3273. // callee is nounwind. This is not possible if the personality of the
  3274. // function allows to catch asynchronous exceptions.
  3275. bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
  3276. KnownDeadEnds.set_union(ToBeExploredFrom);
  3277. for (const Instruction *DeadEndI : KnownDeadEnds) {
  3278. auto *CB = dyn_cast<CallBase>(DeadEndI);
  3279. if (!CB)
  3280. continue;
  3281. const auto &NoReturnAA = A.getAndUpdateAAFor<AANoReturn>(
  3282. *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
  3283. bool MayReturn = !NoReturnAA.isAssumedNoReturn();
  3284. if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
  3285. continue;
  3286. if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
  3287. A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
  3288. else
  3289. A.changeToUnreachableAfterManifest(
  3290. const_cast<Instruction *>(DeadEndI->getNextNode()));
  3291. HasChanged = ChangeStatus::CHANGED;
  3292. }
  3293. STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
  3294. for (BasicBlock &BB : F)
  3295. if (!AssumedLiveBlocks.count(&BB)) {
  3296. A.deleteAfterManifest(BB);
  3297. ++BUILD_STAT_NAME(AAIsDead, BasicBlock);
  3298. HasChanged = ChangeStatus::CHANGED;
  3299. }
  3300. return HasChanged;
  3301. }
  3302. /// See AbstractAttribute::updateImpl(...).
  3303. ChangeStatus updateImpl(Attributor &A) override;
  3304. bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const override {
  3305. assert(From->getParent() == getAnchorScope() &&
  3306. To->getParent() == getAnchorScope() &&
  3307. "Used AAIsDead of the wrong function");
  3308. return isValidState() && !AssumedLiveEdges.count(std::make_pair(From, To));
  3309. }
  3310. /// See AbstractAttribute::trackStatistics()
  3311. void trackStatistics() const override {}
  3312. /// Returns true if the function is assumed dead.
  3313. bool isAssumedDead() const override { return false; }
  3314. /// See AAIsDead::isKnownDead().
  3315. bool isKnownDead() const override { return false; }
  3316. /// See AAIsDead::isAssumedDead(BasicBlock *).
  3317. bool isAssumedDead(const BasicBlock *BB) const override {
  3318. assert(BB->getParent() == getAnchorScope() &&
  3319. "BB must be in the same anchor scope function.");
  3320. if (!getAssumed())
  3321. return false;
  3322. return !AssumedLiveBlocks.count(BB);
  3323. }
  3324. /// See AAIsDead::isKnownDead(BasicBlock *).
  3325. bool isKnownDead(const BasicBlock *BB) const override {
  3326. return getKnown() && isAssumedDead(BB);
  3327. }
  3328. /// See AAIsDead::isAssumed(Instruction *I).
  3329. bool isAssumedDead(const Instruction *I) const override {
  3330. assert(I->getParent()->getParent() == getAnchorScope() &&
  3331. "Instruction must be in the same anchor scope function.");
  3332. if (!getAssumed())
  3333. return false;
  3334. // If it is not in AssumedLiveBlocks then it for sure dead.
  3335. // Otherwise, it can still be after noreturn call in a live block.
  3336. if (!AssumedLiveBlocks.count(I->getParent()))
  3337. return true;
  3338. // If it is not after a liveness barrier it is live.
  3339. const Instruction *PrevI = I->getPrevNode();
  3340. while (PrevI) {
  3341. if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
  3342. return true;
  3343. PrevI = PrevI->getPrevNode();
  3344. }
  3345. return false;
  3346. }
  3347. /// See AAIsDead::isKnownDead(Instruction *I).
  3348. bool isKnownDead(const Instruction *I) const override {
  3349. return getKnown() && isAssumedDead(I);
  3350. }
  3351. /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
  3352. /// that internal function called from \p BB should now be looked at.
  3353. bool assumeLive(Attributor &A, const BasicBlock &BB) {
  3354. if (!AssumedLiveBlocks.insert(&BB).second)
  3355. return false;
  3356. // We assume that all of BB is (probably) live now and if there are calls to
  3357. // internal functions we will assume that those are now live as well. This
  3358. // is a performance optimization for blocks with calls to a lot of internal
  3359. // functions. It can however cause dead functions to be treated as live.
  3360. for (const Instruction &I : BB)
  3361. if (const auto *CB = dyn_cast<CallBase>(&I))
  3362. if (const Function *F = CB->getCalledFunction())
  3363. if (F->hasLocalLinkage())
  3364. A.markLiveInternalFunction(*F);
  3365. return true;
  3366. }
  3367. /// Collection of instructions that need to be explored again, e.g., we
  3368. /// did assume they do not transfer control to (one of their) successors.
  3369. SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
  3370. /// Collection of instructions that are known to not transfer control.
  3371. SmallSetVector<const Instruction *, 8> KnownDeadEnds;
  3372. /// Collection of all assumed live edges
  3373. DenseSet<std::pair<const BasicBlock *, const BasicBlock *>> AssumedLiveEdges;
  3374. /// Collection of all assumed live BasicBlocks.
  3375. DenseSet<const BasicBlock *> AssumedLiveBlocks;
  3376. };
  3377. static bool
  3378. identifyAliveSuccessors(Attributor &A, const CallBase &CB,
  3379. AbstractAttribute &AA,
  3380. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3381. const IRPosition &IPos = IRPosition::callsite_function(CB);
  3382. const auto &NoReturnAA =
  3383. A.getAndUpdateAAFor<AANoReturn>(AA, IPos, DepClassTy::OPTIONAL);
  3384. if (NoReturnAA.isAssumedNoReturn())
  3385. return !NoReturnAA.isKnownNoReturn();
  3386. if (CB.isTerminator())
  3387. AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
  3388. else
  3389. AliveSuccessors.push_back(CB.getNextNode());
  3390. return false;
  3391. }
  3392. static bool
  3393. identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
  3394. AbstractAttribute &AA,
  3395. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3396. bool UsedAssumedInformation =
  3397. identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
  3398. // First, determine if we can change an invoke to a call assuming the
  3399. // callee is nounwind. This is not possible if the personality of the
  3400. // function allows to catch asynchronous exceptions.
  3401. if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
  3402. AliveSuccessors.push_back(&II.getUnwindDest()->front());
  3403. } else {
  3404. const IRPosition &IPos = IRPosition::callsite_function(II);
  3405. const auto &AANoUnw =
  3406. A.getAndUpdateAAFor<AANoUnwind>(AA, IPos, DepClassTy::OPTIONAL);
  3407. if (AANoUnw.isAssumedNoUnwind()) {
  3408. UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
  3409. } else {
  3410. AliveSuccessors.push_back(&II.getUnwindDest()->front());
  3411. }
  3412. }
  3413. return UsedAssumedInformation;
  3414. }
  3415. static bool
  3416. identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
  3417. AbstractAttribute &AA,
  3418. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3419. bool UsedAssumedInformation = false;
  3420. if (BI.getNumSuccessors() == 1) {
  3421. AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
  3422. } else {
  3423. Optional<Constant *> C =
  3424. A.getAssumedConstant(*BI.getCondition(), AA, UsedAssumedInformation);
  3425. if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
  3426. // No value yet, assume both edges are dead.
  3427. } else if (isa_and_nonnull<ConstantInt>(*C)) {
  3428. const BasicBlock *SuccBB =
  3429. BI.getSuccessor(1 - cast<ConstantInt>(*C)->getValue().getZExtValue());
  3430. AliveSuccessors.push_back(&SuccBB->front());
  3431. } else {
  3432. AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
  3433. AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
  3434. UsedAssumedInformation = false;
  3435. }
  3436. }
  3437. return UsedAssumedInformation;
  3438. }
  3439. static bool
  3440. identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
  3441. AbstractAttribute &AA,
  3442. SmallVectorImpl<const Instruction *> &AliveSuccessors) {
  3443. bool UsedAssumedInformation = false;
  3444. Optional<Constant *> C =
  3445. A.getAssumedConstant(*SI.getCondition(), AA, UsedAssumedInformation);
  3446. if (!C.hasValue() || isa_and_nonnull<UndefValue>(C.getValue())) {
  3447. // No value yet, assume all edges are dead.
  3448. } else if (isa_and_nonnull<ConstantInt>(C.getValue())) {
  3449. for (auto &CaseIt : SI.cases()) {
  3450. if (CaseIt.getCaseValue() == C.getValue()) {
  3451. AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
  3452. return UsedAssumedInformation;
  3453. }
  3454. }
  3455. AliveSuccessors.push_back(&SI.getDefaultDest()->front());
  3456. return UsedAssumedInformation;
  3457. } else {
  3458. for (const BasicBlock *SuccBB : successors(SI.getParent()))
  3459. AliveSuccessors.push_back(&SuccBB->front());
  3460. }
  3461. return UsedAssumedInformation;
  3462. }
  3463. ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
  3464. ChangeStatus Change = ChangeStatus::UNCHANGED;
  3465. LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
  3466. << getAnchorScope()->size() << "] BBs and "
  3467. << ToBeExploredFrom.size() << " exploration points and "
  3468. << KnownDeadEnds.size() << " known dead ends\n");
  3469. // Copy and clear the list of instructions we need to explore from. It is
  3470. // refilled with instructions the next update has to look at.
  3471. SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
  3472. ToBeExploredFrom.end());
  3473. decltype(ToBeExploredFrom) NewToBeExploredFrom;
  3474. SmallVector<const Instruction *, 8> AliveSuccessors;
  3475. while (!Worklist.empty()) {
  3476. const Instruction *I = Worklist.pop_back_val();
  3477. LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
  3478. // Fast forward for uninteresting instructions. We could look for UB here
  3479. // though.
  3480. while (!I->isTerminator() && !isa<CallBase>(I))
  3481. I = I->getNextNode();
  3482. AliveSuccessors.clear();
  3483. bool UsedAssumedInformation = false;
  3484. switch (I->getOpcode()) {
  3485. // TODO: look for (assumed) UB to backwards propagate "deadness".
  3486. default:
  3487. assert(I->isTerminator() &&
  3488. "Expected non-terminators to be handled already!");
  3489. for (const BasicBlock *SuccBB : successors(I->getParent()))
  3490. AliveSuccessors.push_back(&SuccBB->front());
  3491. break;
  3492. case Instruction::Call:
  3493. UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
  3494. *this, AliveSuccessors);
  3495. break;
  3496. case Instruction::Invoke:
  3497. UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
  3498. *this, AliveSuccessors);
  3499. break;
  3500. case Instruction::Br:
  3501. UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
  3502. *this, AliveSuccessors);
  3503. break;
  3504. case Instruction::Switch:
  3505. UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
  3506. *this, AliveSuccessors);
  3507. break;
  3508. }
  3509. if (UsedAssumedInformation) {
  3510. NewToBeExploredFrom.insert(I);
  3511. } else if (AliveSuccessors.empty() ||
  3512. (I->isTerminator() &&
  3513. AliveSuccessors.size() < I->getNumSuccessors())) {
  3514. if (KnownDeadEnds.insert(I))
  3515. Change = ChangeStatus::CHANGED;
  3516. }
  3517. LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
  3518. << AliveSuccessors.size() << " UsedAssumedInformation: "
  3519. << UsedAssumedInformation << "\n");
  3520. for (const Instruction *AliveSuccessor : AliveSuccessors) {
  3521. if (!I->isTerminator()) {
  3522. assert(AliveSuccessors.size() == 1 &&
  3523. "Non-terminator expected to have a single successor!");
  3524. Worklist.push_back(AliveSuccessor);
  3525. } else {
  3526. // record the assumed live edge
  3527. auto Edge = std::make_pair(I->getParent(), AliveSuccessor->getParent());
  3528. if (AssumedLiveEdges.insert(Edge).second)
  3529. Change = ChangeStatus::CHANGED;
  3530. if (assumeLive(A, *AliveSuccessor->getParent()))
  3531. Worklist.push_back(AliveSuccessor);
  3532. }
  3533. }
  3534. }
  3535. // Check if the content of ToBeExploredFrom changed, ignore the order.
  3536. if (NewToBeExploredFrom.size() != ToBeExploredFrom.size() ||
  3537. llvm::any_of(NewToBeExploredFrom, [&](const Instruction *I) {
  3538. return !ToBeExploredFrom.count(I);
  3539. })) {
  3540. Change = ChangeStatus::CHANGED;
  3541. ToBeExploredFrom = std::move(NewToBeExploredFrom);
  3542. }
  3543. // If we know everything is live there is no need to query for liveness.
  3544. // Instead, indicating a pessimistic fixpoint will cause the state to be
  3545. // "invalid" and all queries to be answered conservatively without lookups.
  3546. // To be in this state we have to (1) finished the exploration and (3) not
  3547. // discovered any non-trivial dead end and (2) not ruled unreachable code
  3548. // dead.
  3549. if (ToBeExploredFrom.empty() &&
  3550. getAnchorScope()->size() == AssumedLiveBlocks.size() &&
  3551. llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
  3552. return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
  3553. }))
  3554. return indicatePessimisticFixpoint();
  3555. return Change;
  3556. }
  3557. /// Liveness information for a call sites.
  3558. struct AAIsDeadCallSite final : AAIsDeadFunction {
  3559. AAIsDeadCallSite(const IRPosition &IRP, Attributor &A)
  3560. : AAIsDeadFunction(IRP, A) {}
  3561. /// See AbstractAttribute::initialize(...).
  3562. void initialize(Attributor &A) override {
  3563. // TODO: Once we have call site specific value information we can provide
  3564. // call site specific liveness information and then it makes
  3565. // sense to specialize attributes for call sites instead of
  3566. // redirecting requests to the callee.
  3567. llvm_unreachable("Abstract attributes for liveness are not "
  3568. "supported for call sites yet!");
  3569. }
  3570. /// See AbstractAttribute::updateImpl(...).
  3571. ChangeStatus updateImpl(Attributor &A) override {
  3572. return indicatePessimisticFixpoint();
  3573. }
  3574. /// See AbstractAttribute::trackStatistics()
  3575. void trackStatistics() const override {}
  3576. };
  3577. /// -------------------- Dereferenceable Argument Attribute --------------------
  3578. struct AADereferenceableImpl : AADereferenceable {
  3579. AADereferenceableImpl(const IRPosition &IRP, Attributor &A)
  3580. : AADereferenceable(IRP, A) {}
  3581. using StateType = DerefState;
  3582. /// See AbstractAttribute::initialize(...).
  3583. void initialize(Attributor &A) override {
  3584. SmallVector<Attribute, 4> Attrs;
  3585. getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
  3586. Attrs, /* IgnoreSubsumingPositions */ false, &A);
  3587. for (const Attribute &Attr : Attrs)
  3588. takeKnownDerefBytesMaximum(Attr.getValueAsInt());
  3589. const IRPosition &IRP = this->getIRPosition();
  3590. NonNullAA = &A.getAAFor<AANonNull>(*this, IRP, DepClassTy::NONE);
  3591. bool CanBeNull, CanBeFreed;
  3592. takeKnownDerefBytesMaximum(
  3593. IRP.getAssociatedValue().getPointerDereferenceableBytes(
  3594. A.getDataLayout(), CanBeNull, CanBeFreed));
  3595. bool IsFnInterface = IRP.isFnInterfaceKind();
  3596. Function *FnScope = IRP.getAnchorScope();
  3597. if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope))) {
  3598. indicatePessimisticFixpoint();
  3599. return;
  3600. }
  3601. if (Instruction *CtxI = getCtxI())
  3602. followUsesInMBEC(*this, A, getState(), *CtxI);
  3603. }
  3604. /// See AbstractAttribute::getState()
  3605. /// {
  3606. StateType &getState() override { return *this; }
  3607. const StateType &getState() const override { return *this; }
  3608. /// }
  3609. /// Helper function for collecting accessed bytes in must-be-executed-context
  3610. void addAccessedBytesForUse(Attributor &A, const Use *U, const Instruction *I,
  3611. DerefState &State) {
  3612. const Value *UseV = U->get();
  3613. if (!UseV->getType()->isPointerTy())
  3614. return;
  3615. Optional<MemoryLocation> Loc = MemoryLocation::getOrNone(I);
  3616. if (!Loc || Loc->Ptr != UseV || !Loc->Size.isPrecise() || I->isVolatile())
  3617. return;
  3618. int64_t Offset;
  3619. const Value *Base = GetPointerBaseWithConstantOffset(
  3620. Loc->Ptr, Offset, A.getDataLayout(), /*AllowNonInbounds*/ true);
  3621. if (Base && Base == &getAssociatedValue())
  3622. State.addAccessedBytes(Offset, Loc->Size.getValue());
  3623. }
  3624. /// See followUsesInMBEC
  3625. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  3626. AADereferenceable::StateType &State) {
  3627. bool IsNonNull = false;
  3628. bool TrackUse = false;
  3629. int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
  3630. A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
  3631. LLVM_DEBUG(dbgs() << "[AADereferenceable] Deref bytes: " << DerefBytes
  3632. << " for instruction " << *I << "\n");
  3633. addAccessedBytesForUse(A, U, I, State);
  3634. State.takeKnownDerefBytesMaximum(DerefBytes);
  3635. return TrackUse;
  3636. }
  3637. /// See AbstractAttribute::manifest(...).
  3638. ChangeStatus manifest(Attributor &A) override {
  3639. ChangeStatus Change = AADereferenceable::manifest(A);
  3640. if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
  3641. removeAttrs({Attribute::DereferenceableOrNull});
  3642. return ChangeStatus::CHANGED;
  3643. }
  3644. return Change;
  3645. }
  3646. void getDeducedAttributes(LLVMContext &Ctx,
  3647. SmallVectorImpl<Attribute> &Attrs) const override {
  3648. // TODO: Add *_globally support
  3649. if (isAssumedNonNull())
  3650. Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
  3651. Ctx, getAssumedDereferenceableBytes()));
  3652. else
  3653. Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
  3654. Ctx, getAssumedDereferenceableBytes()));
  3655. }
  3656. /// See AbstractAttribute::getAsStr().
  3657. const std::string getAsStr() const override {
  3658. if (!getAssumedDereferenceableBytes())
  3659. return "unknown-dereferenceable";
  3660. return std::string("dereferenceable") +
  3661. (isAssumedNonNull() ? "" : "_or_null") +
  3662. (isAssumedGlobal() ? "_globally" : "") + "<" +
  3663. std::to_string(getKnownDereferenceableBytes()) + "-" +
  3664. std::to_string(getAssumedDereferenceableBytes()) + ">";
  3665. }
  3666. };
  3667. /// Dereferenceable attribute for a floating value.
  3668. struct AADereferenceableFloating : AADereferenceableImpl {
  3669. AADereferenceableFloating(const IRPosition &IRP, Attributor &A)
  3670. : AADereferenceableImpl(IRP, A) {}
  3671. /// See AbstractAttribute::updateImpl(...).
  3672. ChangeStatus updateImpl(Attributor &A) override {
  3673. const DataLayout &DL = A.getDataLayout();
  3674. auto VisitValueCB = [&](const Value &V, const Instruction *, DerefState &T,
  3675. bool Stripped) -> bool {
  3676. unsigned IdxWidth =
  3677. DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
  3678. APInt Offset(IdxWidth, 0);
  3679. const Value *Base =
  3680. stripAndAccumulateMinimalOffsets(A, *this, &V, DL, Offset, false);
  3681. const auto &AA = A.getAAFor<AADereferenceable>(
  3682. *this, IRPosition::value(*Base), DepClassTy::REQUIRED);
  3683. int64_t DerefBytes = 0;
  3684. if (!Stripped && this == &AA) {
  3685. // Use IR information if we did not strip anything.
  3686. // TODO: track globally.
  3687. bool CanBeNull, CanBeFreed;
  3688. DerefBytes =
  3689. Base->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
  3690. T.GlobalState.indicatePessimisticFixpoint();
  3691. } else {
  3692. const DerefState &DS = AA.getState();
  3693. DerefBytes = DS.DerefBytesState.getAssumed();
  3694. T.GlobalState &= DS.GlobalState;
  3695. }
  3696. // For now we do not try to "increase" dereferenceability due to negative
  3697. // indices as we first have to come up with code to deal with loops and
  3698. // for overflows of the dereferenceable bytes.
  3699. int64_t OffsetSExt = Offset.getSExtValue();
  3700. if (OffsetSExt < 0)
  3701. OffsetSExt = 0;
  3702. T.takeAssumedDerefBytesMinimum(
  3703. std::max(int64_t(0), DerefBytes - OffsetSExt));
  3704. if (this == &AA) {
  3705. if (!Stripped) {
  3706. // If nothing was stripped IR information is all we got.
  3707. T.takeKnownDerefBytesMaximum(
  3708. std::max(int64_t(0), DerefBytes - OffsetSExt));
  3709. T.indicatePessimisticFixpoint();
  3710. } else if (OffsetSExt > 0) {
  3711. // If something was stripped but there is circular reasoning we look
  3712. // for the offset. If it is positive we basically decrease the
  3713. // dereferenceable bytes in a circluar loop now, which will simply
  3714. // drive them down to the known value in a very slow way which we
  3715. // can accelerate.
  3716. T.indicatePessimisticFixpoint();
  3717. }
  3718. }
  3719. return T.isValidState();
  3720. };
  3721. DerefState T;
  3722. bool UsedAssumedInformation = false;
  3723. if (!genericValueTraversal<DerefState>(A, getIRPosition(), *this, T,
  3724. VisitValueCB, getCtxI(),
  3725. UsedAssumedInformation))
  3726. return indicatePessimisticFixpoint();
  3727. return clampStateAndIndicateChange(getState(), T);
  3728. }
  3729. /// See AbstractAttribute::trackStatistics()
  3730. void trackStatistics() const override {
  3731. STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
  3732. }
  3733. };
  3734. /// Dereferenceable attribute for a return value.
  3735. struct AADereferenceableReturned final
  3736. : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
  3737. AADereferenceableReturned(const IRPosition &IRP, Attributor &A)
  3738. : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
  3739. IRP, A) {}
  3740. /// See AbstractAttribute::trackStatistics()
  3741. void trackStatistics() const override {
  3742. STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
  3743. }
  3744. };
  3745. /// Dereferenceable attribute for an argument
  3746. struct AADereferenceableArgument final
  3747. : AAArgumentFromCallSiteArguments<AADereferenceable,
  3748. AADereferenceableImpl> {
  3749. using Base =
  3750. AAArgumentFromCallSiteArguments<AADereferenceable, AADereferenceableImpl>;
  3751. AADereferenceableArgument(const IRPosition &IRP, Attributor &A)
  3752. : Base(IRP, A) {}
  3753. /// See AbstractAttribute::trackStatistics()
  3754. void trackStatistics() const override {
  3755. STATS_DECLTRACK_ARG_ATTR(dereferenceable)
  3756. }
  3757. };
  3758. /// Dereferenceable attribute for a call site argument.
  3759. struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
  3760. AADereferenceableCallSiteArgument(const IRPosition &IRP, Attributor &A)
  3761. : AADereferenceableFloating(IRP, A) {}
  3762. /// See AbstractAttribute::trackStatistics()
  3763. void trackStatistics() const override {
  3764. STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
  3765. }
  3766. };
  3767. /// Dereferenceable attribute deduction for a call site return value.
  3768. struct AADereferenceableCallSiteReturned final
  3769. : AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl> {
  3770. using Base =
  3771. AACallSiteReturnedFromReturned<AADereferenceable, AADereferenceableImpl>;
  3772. AADereferenceableCallSiteReturned(const IRPosition &IRP, Attributor &A)
  3773. : Base(IRP, A) {}
  3774. /// See AbstractAttribute::trackStatistics()
  3775. void trackStatistics() const override {
  3776. STATS_DECLTRACK_CS_ATTR(dereferenceable);
  3777. }
  3778. };
  3779. // ------------------------ Align Argument Attribute ------------------------
  3780. static unsigned getKnownAlignForUse(Attributor &A, AAAlign &QueryingAA,
  3781. Value &AssociatedValue, const Use *U,
  3782. const Instruction *I, bool &TrackUse) {
  3783. // We need to follow common pointer manipulation uses to the accesses they
  3784. // feed into.
  3785. if (isa<CastInst>(I)) {
  3786. // Follow all but ptr2int casts.
  3787. TrackUse = !isa<PtrToIntInst>(I);
  3788. return 0;
  3789. }
  3790. if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
  3791. if (GEP->hasAllConstantIndices())
  3792. TrackUse = true;
  3793. return 0;
  3794. }
  3795. MaybeAlign MA;
  3796. if (const auto *CB = dyn_cast<CallBase>(I)) {
  3797. if (CB->isBundleOperand(U) || CB->isCallee(U))
  3798. return 0;
  3799. unsigned ArgNo = CB->getArgOperandNo(U);
  3800. IRPosition IRP = IRPosition::callsite_argument(*CB, ArgNo);
  3801. // As long as we only use known information there is no need to track
  3802. // dependences here.
  3803. auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP, DepClassTy::NONE);
  3804. MA = MaybeAlign(AlignAA.getKnownAlign());
  3805. }
  3806. const DataLayout &DL = A.getDataLayout();
  3807. const Value *UseV = U->get();
  3808. if (auto *SI = dyn_cast<StoreInst>(I)) {
  3809. if (SI->getPointerOperand() == UseV)
  3810. MA = SI->getAlign();
  3811. } else if (auto *LI = dyn_cast<LoadInst>(I)) {
  3812. if (LI->getPointerOperand() == UseV)
  3813. MA = LI->getAlign();
  3814. }
  3815. if (!MA || *MA <= QueryingAA.getKnownAlign())
  3816. return 0;
  3817. unsigned Alignment = MA->value();
  3818. int64_t Offset;
  3819. if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
  3820. if (Base == &AssociatedValue) {
  3821. // BasePointerAddr + Offset = Alignment * Q for some integer Q.
  3822. // So we can say that the maximum power of two which is a divisor of
  3823. // gcd(Offset, Alignment) is an alignment.
  3824. uint32_t gcd =
  3825. greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
  3826. Alignment = llvm::PowerOf2Floor(gcd);
  3827. }
  3828. }
  3829. return Alignment;
  3830. }
  3831. struct AAAlignImpl : AAAlign {
  3832. AAAlignImpl(const IRPosition &IRP, Attributor &A) : AAAlign(IRP, A) {}
  3833. /// See AbstractAttribute::initialize(...).
  3834. void initialize(Attributor &A) override {
  3835. SmallVector<Attribute, 4> Attrs;
  3836. getAttrs({Attribute::Alignment}, Attrs);
  3837. for (const Attribute &Attr : Attrs)
  3838. takeKnownMaximum(Attr.getValueAsInt());
  3839. Value &V = getAssociatedValue();
  3840. // TODO: This is a HACK to avoid getPointerAlignment to introduce a ptr2int
  3841. // use of the function pointer. This was caused by D73131. We want to
  3842. // avoid this for function pointers especially because we iterate
  3843. // their uses and int2ptr is not handled. It is not a correctness
  3844. // problem though!
  3845. if (!V.getType()->getPointerElementType()->isFunctionTy())
  3846. takeKnownMaximum(V.getPointerAlignment(A.getDataLayout()).value());
  3847. if (getIRPosition().isFnInterfaceKind() &&
  3848. (!getAnchorScope() ||
  3849. !A.isFunctionIPOAmendable(*getAssociatedFunction()))) {
  3850. indicatePessimisticFixpoint();
  3851. return;
  3852. }
  3853. if (Instruction *CtxI = getCtxI())
  3854. followUsesInMBEC(*this, A, getState(), *CtxI);
  3855. }
  3856. /// See AbstractAttribute::manifest(...).
  3857. ChangeStatus manifest(Attributor &A) override {
  3858. ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
  3859. // Check for users that allow alignment annotations.
  3860. Value &AssociatedValue = getAssociatedValue();
  3861. for (const Use &U : AssociatedValue.uses()) {
  3862. if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
  3863. if (SI->getPointerOperand() == &AssociatedValue)
  3864. if (SI->getAlignment() < getAssumedAlign()) {
  3865. STATS_DECLTRACK(AAAlign, Store,
  3866. "Number of times alignment added to a store");
  3867. SI->setAlignment(Align(getAssumedAlign()));
  3868. LoadStoreChanged = ChangeStatus::CHANGED;
  3869. }
  3870. } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
  3871. if (LI->getPointerOperand() == &AssociatedValue)
  3872. if (LI->getAlignment() < getAssumedAlign()) {
  3873. LI->setAlignment(Align(getAssumedAlign()));
  3874. STATS_DECLTRACK(AAAlign, Load,
  3875. "Number of times alignment added to a load");
  3876. LoadStoreChanged = ChangeStatus::CHANGED;
  3877. }
  3878. }
  3879. }
  3880. ChangeStatus Changed = AAAlign::manifest(A);
  3881. Align InheritAlign =
  3882. getAssociatedValue().getPointerAlignment(A.getDataLayout());
  3883. if (InheritAlign >= getAssumedAlign())
  3884. return LoadStoreChanged;
  3885. return Changed | LoadStoreChanged;
  3886. }
  3887. // TODO: Provide a helper to determine the implied ABI alignment and check in
  3888. // the existing manifest method and a new one for AAAlignImpl that value
  3889. // to avoid making the alignment explicit if it did not improve.
  3890. /// See AbstractAttribute::getDeducedAttributes
  3891. virtual void
  3892. getDeducedAttributes(LLVMContext &Ctx,
  3893. SmallVectorImpl<Attribute> &Attrs) const override {
  3894. if (getAssumedAlign() > 1)
  3895. Attrs.emplace_back(
  3896. Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
  3897. }
  3898. /// See followUsesInMBEC
  3899. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  3900. AAAlign::StateType &State) {
  3901. bool TrackUse = false;
  3902. unsigned int KnownAlign =
  3903. getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
  3904. State.takeKnownMaximum(KnownAlign);
  3905. return TrackUse;
  3906. }
  3907. /// See AbstractAttribute::getAsStr().
  3908. const std::string getAsStr() const override {
  3909. return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
  3910. "-" + std::to_string(getAssumedAlign()) + ">")
  3911. : "unknown-align";
  3912. }
  3913. };
  3914. /// Align attribute for a floating value.
  3915. struct AAAlignFloating : AAAlignImpl {
  3916. AAAlignFloating(const IRPosition &IRP, Attributor &A) : AAAlignImpl(IRP, A) {}
  3917. /// See AbstractAttribute::updateImpl(...).
  3918. ChangeStatus updateImpl(Attributor &A) override {
  3919. const DataLayout &DL = A.getDataLayout();
  3920. auto VisitValueCB = [&](Value &V, const Instruction *,
  3921. AAAlign::StateType &T, bool Stripped) -> bool {
  3922. const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V),
  3923. DepClassTy::REQUIRED);
  3924. if (!Stripped && this == &AA) {
  3925. int64_t Offset;
  3926. unsigned Alignment = 1;
  3927. if (const Value *Base =
  3928. GetPointerBaseWithConstantOffset(&V, Offset, DL)) {
  3929. Align PA = Base->getPointerAlignment(DL);
  3930. // BasePointerAddr + Offset = Alignment * Q for some integer Q.
  3931. // So we can say that the maximum power of two which is a divisor of
  3932. // gcd(Offset, Alignment) is an alignment.
  3933. uint32_t gcd = greatestCommonDivisor(uint32_t(abs((int32_t)Offset)),
  3934. uint32_t(PA.value()));
  3935. Alignment = llvm::PowerOf2Floor(gcd);
  3936. } else {
  3937. Alignment = V.getPointerAlignment(DL).value();
  3938. }
  3939. // Use only IR information if we did not strip anything.
  3940. T.takeKnownMaximum(Alignment);
  3941. T.indicatePessimisticFixpoint();
  3942. } else {
  3943. // Use abstract attribute information.
  3944. const AAAlign::StateType &DS = AA.getState();
  3945. T ^= DS;
  3946. }
  3947. return T.isValidState();
  3948. };
  3949. StateType T;
  3950. bool UsedAssumedInformation = false;
  3951. if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
  3952. VisitValueCB, getCtxI(),
  3953. UsedAssumedInformation))
  3954. return indicatePessimisticFixpoint();
  3955. // TODO: If we know we visited all incoming values, thus no are assumed
  3956. // dead, we can take the known information from the state T.
  3957. return clampStateAndIndicateChange(getState(), T);
  3958. }
  3959. /// See AbstractAttribute::trackStatistics()
  3960. void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
  3961. };
  3962. /// Align attribute for function return value.
  3963. struct AAAlignReturned final
  3964. : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
  3965. using Base = AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>;
  3966. AAAlignReturned(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
  3967. /// See AbstractAttribute::initialize(...).
  3968. void initialize(Attributor &A) override {
  3969. Base::initialize(A);
  3970. Function *F = getAssociatedFunction();
  3971. if (!F || F->isDeclaration())
  3972. indicatePessimisticFixpoint();
  3973. }
  3974. /// See AbstractAttribute::trackStatistics()
  3975. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
  3976. };
  3977. /// Align attribute for function argument.
  3978. struct AAAlignArgument final
  3979. : AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl> {
  3980. using Base = AAArgumentFromCallSiteArguments<AAAlign, AAAlignImpl>;
  3981. AAAlignArgument(const IRPosition &IRP, Attributor &A) : Base(IRP, A) {}
  3982. /// See AbstractAttribute::manifest(...).
  3983. ChangeStatus manifest(Attributor &A) override {
  3984. // If the associated argument is involved in a must-tail call we give up
  3985. // because we would need to keep the argument alignments of caller and
  3986. // callee in-sync. Just does not seem worth the trouble right now.
  3987. if (A.getInfoCache().isInvolvedInMustTailCall(*getAssociatedArgument()))
  3988. return ChangeStatus::UNCHANGED;
  3989. return Base::manifest(A);
  3990. }
  3991. /// See AbstractAttribute::trackStatistics()
  3992. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
  3993. };
  3994. struct AAAlignCallSiteArgument final : AAAlignFloating {
  3995. AAAlignCallSiteArgument(const IRPosition &IRP, Attributor &A)
  3996. : AAAlignFloating(IRP, A) {}
  3997. /// See AbstractAttribute::manifest(...).
  3998. ChangeStatus manifest(Attributor &A) override {
  3999. // If the associated argument is involved in a must-tail call we give up
  4000. // because we would need to keep the argument alignments of caller and
  4001. // callee in-sync. Just does not seem worth the trouble right now.
  4002. if (Argument *Arg = getAssociatedArgument())
  4003. if (A.getInfoCache().isInvolvedInMustTailCall(*Arg))
  4004. return ChangeStatus::UNCHANGED;
  4005. ChangeStatus Changed = AAAlignImpl::manifest(A);
  4006. Align InheritAlign =
  4007. getAssociatedValue().getPointerAlignment(A.getDataLayout());
  4008. if (InheritAlign >= getAssumedAlign())
  4009. Changed = ChangeStatus::UNCHANGED;
  4010. return Changed;
  4011. }
  4012. /// See AbstractAttribute::updateImpl(Attributor &A).
  4013. ChangeStatus updateImpl(Attributor &A) override {
  4014. ChangeStatus Changed = AAAlignFloating::updateImpl(A);
  4015. if (Argument *Arg = getAssociatedArgument()) {
  4016. // We only take known information from the argument
  4017. // so we do not need to track a dependence.
  4018. const auto &ArgAlignAA = A.getAAFor<AAAlign>(
  4019. *this, IRPosition::argument(*Arg), DepClassTy::NONE);
  4020. takeKnownMaximum(ArgAlignAA.getKnownAlign());
  4021. }
  4022. return Changed;
  4023. }
  4024. /// See AbstractAttribute::trackStatistics()
  4025. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
  4026. };
  4027. /// Align attribute deduction for a call site return value.
  4028. struct AAAlignCallSiteReturned final
  4029. : AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl> {
  4030. using Base = AACallSiteReturnedFromReturned<AAAlign, AAAlignImpl>;
  4031. AAAlignCallSiteReturned(const IRPosition &IRP, Attributor &A)
  4032. : Base(IRP, A) {}
  4033. /// See AbstractAttribute::initialize(...).
  4034. void initialize(Attributor &A) override {
  4035. Base::initialize(A);
  4036. Function *F = getAssociatedFunction();
  4037. if (!F || F->isDeclaration())
  4038. indicatePessimisticFixpoint();
  4039. }
  4040. /// See AbstractAttribute::trackStatistics()
  4041. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
  4042. };
  4043. /// ------------------ Function No-Return Attribute ----------------------------
  4044. struct AANoReturnImpl : public AANoReturn {
  4045. AANoReturnImpl(const IRPosition &IRP, Attributor &A) : AANoReturn(IRP, A) {}
  4046. /// See AbstractAttribute::initialize(...).
  4047. void initialize(Attributor &A) override {
  4048. AANoReturn::initialize(A);
  4049. Function *F = getAssociatedFunction();
  4050. if (!F || F->isDeclaration())
  4051. indicatePessimisticFixpoint();
  4052. }
  4053. /// See AbstractAttribute::getAsStr().
  4054. const std::string getAsStr() const override {
  4055. return getAssumed() ? "noreturn" : "may-return";
  4056. }
  4057. /// See AbstractAttribute::updateImpl(Attributor &A).
  4058. virtual ChangeStatus updateImpl(Attributor &A) override {
  4059. auto CheckForNoReturn = [](Instruction &) { return false; };
  4060. bool UsedAssumedInformation = false;
  4061. if (!A.checkForAllInstructions(CheckForNoReturn, *this,
  4062. {(unsigned)Instruction::Ret},
  4063. UsedAssumedInformation))
  4064. return indicatePessimisticFixpoint();
  4065. return ChangeStatus::UNCHANGED;
  4066. }
  4067. };
  4068. struct AANoReturnFunction final : AANoReturnImpl {
  4069. AANoReturnFunction(const IRPosition &IRP, Attributor &A)
  4070. : AANoReturnImpl(IRP, A) {}
  4071. /// See AbstractAttribute::trackStatistics()
  4072. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
  4073. };
  4074. /// NoReturn attribute deduction for a call sites.
  4075. struct AANoReturnCallSite final : AANoReturnImpl {
  4076. AANoReturnCallSite(const IRPosition &IRP, Attributor &A)
  4077. : AANoReturnImpl(IRP, A) {}
  4078. /// See AbstractAttribute::initialize(...).
  4079. void initialize(Attributor &A) override {
  4080. AANoReturnImpl::initialize(A);
  4081. if (Function *F = getAssociatedFunction()) {
  4082. const IRPosition &FnPos = IRPosition::function(*F);
  4083. auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
  4084. if (!FnAA.isAssumedNoReturn())
  4085. indicatePessimisticFixpoint();
  4086. }
  4087. }
  4088. /// See AbstractAttribute::updateImpl(...).
  4089. ChangeStatus updateImpl(Attributor &A) override {
  4090. // TODO: Once we have call site specific value information we can provide
  4091. // call site specific liveness information and then it makes
  4092. // sense to specialize attributes for call sites arguments instead of
  4093. // redirecting requests to the callee argument.
  4094. Function *F = getAssociatedFunction();
  4095. const IRPosition &FnPos = IRPosition::function(*F);
  4096. auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos, DepClassTy::REQUIRED);
  4097. return clampStateAndIndicateChange(getState(), FnAA.getState());
  4098. }
  4099. /// See AbstractAttribute::trackStatistics()
  4100. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
  4101. };
  4102. /// ----------------------- Variable Capturing ---------------------------------
  4103. /// A class to hold the state of for no-capture attributes.
  4104. struct AANoCaptureImpl : public AANoCapture {
  4105. AANoCaptureImpl(const IRPosition &IRP, Attributor &A) : AANoCapture(IRP, A) {}
  4106. /// See AbstractAttribute::initialize(...).
  4107. void initialize(Attributor &A) override {
  4108. if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
  4109. indicateOptimisticFixpoint();
  4110. return;
  4111. }
  4112. Function *AnchorScope = getAnchorScope();
  4113. if (isFnInterfaceKind() &&
  4114. (!AnchorScope || !A.isFunctionIPOAmendable(*AnchorScope))) {
  4115. indicatePessimisticFixpoint();
  4116. return;
  4117. }
  4118. // You cannot "capture" null in the default address space.
  4119. if (isa<ConstantPointerNull>(getAssociatedValue()) &&
  4120. getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
  4121. indicateOptimisticFixpoint();
  4122. return;
  4123. }
  4124. const Function *F =
  4125. isArgumentPosition() ? getAssociatedFunction() : AnchorScope;
  4126. // Check what state the associated function can actually capture.
  4127. if (F)
  4128. determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
  4129. else
  4130. indicatePessimisticFixpoint();
  4131. }
  4132. /// See AbstractAttribute::updateImpl(...).
  4133. ChangeStatus updateImpl(Attributor &A) override;
  4134. /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
  4135. virtual void
  4136. getDeducedAttributes(LLVMContext &Ctx,
  4137. SmallVectorImpl<Attribute> &Attrs) const override {
  4138. if (!isAssumedNoCaptureMaybeReturned())
  4139. return;
  4140. if (isArgumentPosition()) {
  4141. if (isAssumedNoCapture())
  4142. Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
  4143. else if (ManifestInternal)
  4144. Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
  4145. }
  4146. }
  4147. /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
  4148. /// depending on the ability of the function associated with \p IRP to capture
  4149. /// state in memory and through "returning/throwing", respectively.
  4150. static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
  4151. const Function &F,
  4152. BitIntegerState &State) {
  4153. // TODO: Once we have memory behavior attributes we should use them here.
  4154. // If we know we cannot communicate or write to memory, we do not care about
  4155. // ptr2int anymore.
  4156. if (F.onlyReadsMemory() && F.doesNotThrow() &&
  4157. F.getReturnType()->isVoidTy()) {
  4158. State.addKnownBits(NO_CAPTURE);
  4159. return;
  4160. }
  4161. // A function cannot capture state in memory if it only reads memory, it can
  4162. // however return/throw state and the state might be influenced by the
  4163. // pointer value, e.g., loading from a returned pointer might reveal a bit.
  4164. if (F.onlyReadsMemory())
  4165. State.addKnownBits(NOT_CAPTURED_IN_MEM);
  4166. // A function cannot communicate state back if it does not through
  4167. // exceptions and doesn not return values.
  4168. if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
  4169. State.addKnownBits(NOT_CAPTURED_IN_RET);
  4170. // Check existing "returned" attributes.
  4171. int ArgNo = IRP.getCalleeArgNo();
  4172. if (F.doesNotThrow() && ArgNo >= 0) {
  4173. for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
  4174. if (F.hasParamAttribute(u, Attribute::Returned)) {
  4175. if (u == unsigned(ArgNo))
  4176. State.removeAssumedBits(NOT_CAPTURED_IN_RET);
  4177. else if (F.onlyReadsMemory())
  4178. State.addKnownBits(NO_CAPTURE);
  4179. else
  4180. State.addKnownBits(NOT_CAPTURED_IN_RET);
  4181. break;
  4182. }
  4183. }
  4184. }
  4185. /// See AbstractState::getAsStr().
  4186. const std::string getAsStr() const override {
  4187. if (isKnownNoCapture())
  4188. return "known not-captured";
  4189. if (isAssumedNoCapture())
  4190. return "assumed not-captured";
  4191. if (isKnownNoCaptureMaybeReturned())
  4192. return "known not-captured-maybe-returned";
  4193. if (isAssumedNoCaptureMaybeReturned())
  4194. return "assumed not-captured-maybe-returned";
  4195. return "assumed-captured";
  4196. }
  4197. };
  4198. /// Attributor-aware capture tracker.
  4199. struct AACaptureUseTracker final : public CaptureTracker {
  4200. /// Create a capture tracker that can lookup in-flight abstract attributes
  4201. /// through the Attributor \p A.
  4202. ///
  4203. /// If a use leads to a potential capture, \p CapturedInMemory is set and the
  4204. /// search is stopped. If a use leads to a return instruction,
  4205. /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
  4206. /// If a use leads to a ptr2int which may capture the value,
  4207. /// \p CapturedInInteger is set. If a use is found that is currently assumed
  4208. /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
  4209. /// set. All values in \p PotentialCopies are later tracked as well. For every
  4210. /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
  4211. /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
  4212. /// conservatively set to true.
  4213. AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
  4214. const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
  4215. SmallSetVector<Value *, 4> &PotentialCopies,
  4216. unsigned &RemainingUsesToExplore)
  4217. : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
  4218. PotentialCopies(PotentialCopies),
  4219. RemainingUsesToExplore(RemainingUsesToExplore) {}
  4220. /// Determine if \p V maybe captured. *Also updates the state!*
  4221. bool valueMayBeCaptured(const Value *V) {
  4222. if (V->getType()->isPointerTy()) {
  4223. PointerMayBeCaptured(V, this);
  4224. } else {
  4225. State.indicatePessimisticFixpoint();
  4226. }
  4227. return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
  4228. }
  4229. /// See CaptureTracker::tooManyUses().
  4230. void tooManyUses() override {
  4231. State.removeAssumedBits(AANoCapture::NO_CAPTURE);
  4232. }
  4233. bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
  4234. if (CaptureTracker::isDereferenceableOrNull(O, DL))
  4235. return true;
  4236. const auto &DerefAA = A.getAAFor<AADereferenceable>(
  4237. NoCaptureAA, IRPosition::value(*O), DepClassTy::OPTIONAL);
  4238. return DerefAA.getAssumedDereferenceableBytes();
  4239. }
  4240. /// See CaptureTracker::captured(...).
  4241. bool captured(const Use *U) override {
  4242. Instruction *UInst = cast<Instruction>(U->getUser());
  4243. LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
  4244. << "\n");
  4245. // Because we may reuse the tracker multiple times we keep track of the
  4246. // number of explored uses ourselves as well.
  4247. if (RemainingUsesToExplore-- == 0) {
  4248. LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
  4249. return isCapturedIn(/* Memory */ true, /* Integer */ true,
  4250. /* Return */ true);
  4251. }
  4252. // Deal with ptr2int by following uses.
  4253. if (isa<PtrToIntInst>(UInst)) {
  4254. LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
  4255. return valueMayBeCaptured(UInst);
  4256. }
  4257. // For stores we check if we can follow the value through memory or not.
  4258. if (auto *SI = dyn_cast<StoreInst>(UInst)) {
  4259. if (SI->isVolatile())
  4260. return isCapturedIn(/* Memory */ true, /* Integer */ false,
  4261. /* Return */ false);
  4262. bool UsedAssumedInformation = false;
  4263. if (!AA::getPotentialCopiesOfStoredValue(
  4264. A, *SI, PotentialCopies, NoCaptureAA, UsedAssumedInformation))
  4265. return isCapturedIn(/* Memory */ true, /* Integer */ false,
  4266. /* Return */ false);
  4267. // Not captured directly, potential copies will be checked.
  4268. return isCapturedIn(/* Memory */ false, /* Integer */ false,
  4269. /* Return */ false);
  4270. }
  4271. // Explicitly catch return instructions.
  4272. if (isa<ReturnInst>(UInst)) {
  4273. if (UInst->getFunction() == NoCaptureAA.getAnchorScope())
  4274. return isCapturedIn(/* Memory */ false, /* Integer */ false,
  4275. /* Return */ true);
  4276. return isCapturedIn(/* Memory */ true, /* Integer */ true,
  4277. /* Return */ true);
  4278. }
  4279. // For now we only use special logic for call sites. However, the tracker
  4280. // itself knows about a lot of other non-capturing cases already.
  4281. auto *CB = dyn_cast<CallBase>(UInst);
  4282. if (!CB || !CB->isArgOperand(U))
  4283. return isCapturedIn(/* Memory */ true, /* Integer */ true,
  4284. /* Return */ true);
  4285. unsigned ArgNo = CB->getArgOperandNo(U);
  4286. const IRPosition &CSArgPos = IRPosition::callsite_argument(*CB, ArgNo);
  4287. // If we have a abstract no-capture attribute for the argument we can use
  4288. // it to justify a non-capture attribute here. This allows recursion!
  4289. auto &ArgNoCaptureAA =
  4290. A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos, DepClassTy::REQUIRED);
  4291. if (ArgNoCaptureAA.isAssumedNoCapture())
  4292. return isCapturedIn(/* Memory */ false, /* Integer */ false,
  4293. /* Return */ false);
  4294. if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
  4295. addPotentialCopy(*CB);
  4296. return isCapturedIn(/* Memory */ false, /* Integer */ false,
  4297. /* Return */ false);
  4298. }
  4299. // Lastly, we could not find a reason no-capture can be assumed so we don't.
  4300. return isCapturedIn(/* Memory */ true, /* Integer */ true,
  4301. /* Return */ true);
  4302. }
  4303. /// Register \p CS as potential copy of the value we are checking.
  4304. void addPotentialCopy(CallBase &CB) { PotentialCopies.insert(&CB); }
  4305. /// See CaptureTracker::shouldExplore(...).
  4306. bool shouldExplore(const Use *U) override {
  4307. // Check liveness and ignore droppable users.
  4308. bool UsedAssumedInformation = false;
  4309. return !U->getUser()->isDroppable() &&
  4310. !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA,
  4311. UsedAssumedInformation);
  4312. }
  4313. /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
  4314. /// \p CapturedInRet, then return the appropriate value for use in the
  4315. /// CaptureTracker::captured() interface.
  4316. bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
  4317. bool CapturedInRet) {
  4318. LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
  4319. << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
  4320. if (CapturedInMem)
  4321. State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
  4322. if (CapturedInInt)
  4323. State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
  4324. if (CapturedInRet)
  4325. State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
  4326. return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
  4327. }
  4328. private:
  4329. /// The attributor providing in-flight abstract attributes.
  4330. Attributor &A;
  4331. /// The abstract attribute currently updated.
  4332. AANoCapture &NoCaptureAA;
  4333. /// The abstract liveness state.
  4334. const AAIsDead &IsDeadAA;
  4335. /// The state currently updated.
  4336. AANoCapture::StateType &State;
  4337. /// Set of potential copies of the tracked value.
  4338. SmallSetVector<Value *, 4> &PotentialCopies;
  4339. /// Global counter to limit the number of explored uses.
  4340. unsigned &RemainingUsesToExplore;
  4341. };
  4342. ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
  4343. const IRPosition &IRP = getIRPosition();
  4344. Value *V = isArgumentPosition() ? IRP.getAssociatedArgument()
  4345. : &IRP.getAssociatedValue();
  4346. if (!V)
  4347. return indicatePessimisticFixpoint();
  4348. const Function *F =
  4349. isArgumentPosition() ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
  4350. assert(F && "Expected a function!");
  4351. const IRPosition &FnPos = IRPosition::function(*F);
  4352. const auto &IsDeadAA = A.getAAFor<AAIsDead>(*this, FnPos, DepClassTy::NONE);
  4353. AANoCapture::StateType T;
  4354. // Readonly means we cannot capture through memory.
  4355. bool IsKnown;
  4356. if (AA::isAssumedReadOnly(A, FnPos, *this, IsKnown)) {
  4357. T.addKnownBits(NOT_CAPTURED_IN_MEM);
  4358. if (IsKnown)
  4359. addKnownBits(NOT_CAPTURED_IN_MEM);
  4360. }
  4361. // Make sure all returned values are different than the underlying value.
  4362. // TODO: we could do this in a more sophisticated way inside
  4363. // AAReturnedValues, e.g., track all values that escape through returns
  4364. // directly somehow.
  4365. auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
  4366. bool SeenConstant = false;
  4367. for (auto &It : RVAA.returned_values()) {
  4368. if (isa<Constant>(It.first)) {
  4369. if (SeenConstant)
  4370. return false;
  4371. SeenConstant = true;
  4372. } else if (!isa<Argument>(It.first) ||
  4373. It.first == getAssociatedArgument())
  4374. return false;
  4375. }
  4376. return true;
  4377. };
  4378. const auto &NoUnwindAA =
  4379. A.getAAFor<AANoUnwind>(*this, FnPos, DepClassTy::OPTIONAL);
  4380. if (NoUnwindAA.isAssumedNoUnwind()) {
  4381. bool IsVoidTy = F->getReturnType()->isVoidTy();
  4382. const AAReturnedValues *RVAA =
  4383. IsVoidTy ? nullptr
  4384. : &A.getAAFor<AAReturnedValues>(*this, FnPos,
  4385. DepClassTy::OPTIONAL);
  4386. if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
  4387. T.addKnownBits(NOT_CAPTURED_IN_RET);
  4388. if (T.isKnown(NOT_CAPTURED_IN_MEM))
  4389. return ChangeStatus::UNCHANGED;
  4390. if (NoUnwindAA.isKnownNoUnwind() &&
  4391. (IsVoidTy || RVAA->getState().isAtFixpoint())) {
  4392. addKnownBits(NOT_CAPTURED_IN_RET);
  4393. if (isKnown(NOT_CAPTURED_IN_MEM))
  4394. return indicateOptimisticFixpoint();
  4395. }
  4396. }
  4397. }
  4398. // Use the CaptureTracker interface and logic with the specialized tracker,
  4399. // defined in AACaptureUseTracker, that can look at in-flight abstract
  4400. // attributes and directly updates the assumed state.
  4401. SmallSetVector<Value *, 4> PotentialCopies;
  4402. unsigned RemainingUsesToExplore =
  4403. getDefaultMaxUsesToExploreForCaptureTracking();
  4404. AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
  4405. RemainingUsesToExplore);
  4406. // Check all potential copies of the associated value until we can assume
  4407. // none will be captured or we have to assume at least one might be.
  4408. unsigned Idx = 0;
  4409. PotentialCopies.insert(V);
  4410. while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
  4411. Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
  4412. AANoCapture::StateType &S = getState();
  4413. auto Assumed = S.getAssumed();
  4414. S.intersectAssumedBits(T.getAssumed());
  4415. if (!isAssumedNoCaptureMaybeReturned())
  4416. return indicatePessimisticFixpoint();
  4417. return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
  4418. : ChangeStatus::CHANGED;
  4419. }
  4420. /// NoCapture attribute for function arguments.
  4421. struct AANoCaptureArgument final : AANoCaptureImpl {
  4422. AANoCaptureArgument(const IRPosition &IRP, Attributor &A)
  4423. : AANoCaptureImpl(IRP, A) {}
  4424. /// See AbstractAttribute::trackStatistics()
  4425. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
  4426. };
  4427. /// NoCapture attribute for call site arguments.
  4428. struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
  4429. AANoCaptureCallSiteArgument(const IRPosition &IRP, Attributor &A)
  4430. : AANoCaptureImpl(IRP, A) {}
  4431. /// See AbstractAttribute::initialize(...).
  4432. void initialize(Attributor &A) override {
  4433. if (Argument *Arg = getAssociatedArgument())
  4434. if (Arg->hasByValAttr())
  4435. indicateOptimisticFixpoint();
  4436. AANoCaptureImpl::initialize(A);
  4437. }
  4438. /// See AbstractAttribute::updateImpl(...).
  4439. ChangeStatus updateImpl(Attributor &A) override {
  4440. // TODO: Once we have call site specific value information we can provide
  4441. // call site specific liveness information and then it makes
  4442. // sense to specialize attributes for call sites arguments instead of
  4443. // redirecting requests to the callee argument.
  4444. Argument *Arg = getAssociatedArgument();
  4445. if (!Arg)
  4446. return indicatePessimisticFixpoint();
  4447. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  4448. auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos, DepClassTy::REQUIRED);
  4449. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  4450. }
  4451. /// See AbstractAttribute::trackStatistics()
  4452. void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
  4453. };
  4454. /// NoCapture attribute for floating values.
  4455. struct AANoCaptureFloating final : AANoCaptureImpl {
  4456. AANoCaptureFloating(const IRPosition &IRP, Attributor &A)
  4457. : AANoCaptureImpl(IRP, A) {}
  4458. /// See AbstractAttribute::trackStatistics()
  4459. void trackStatistics() const override {
  4460. STATS_DECLTRACK_FLOATING_ATTR(nocapture)
  4461. }
  4462. };
  4463. /// NoCapture attribute for function return value.
  4464. struct AANoCaptureReturned final : AANoCaptureImpl {
  4465. AANoCaptureReturned(const IRPosition &IRP, Attributor &A)
  4466. : AANoCaptureImpl(IRP, A) {
  4467. llvm_unreachable("NoCapture is not applicable to function returns!");
  4468. }
  4469. /// See AbstractAttribute::initialize(...).
  4470. void initialize(Attributor &A) override {
  4471. llvm_unreachable("NoCapture is not applicable to function returns!");
  4472. }
  4473. /// See AbstractAttribute::updateImpl(...).
  4474. ChangeStatus updateImpl(Attributor &A) override {
  4475. llvm_unreachable("NoCapture is not applicable to function returns!");
  4476. }
  4477. /// See AbstractAttribute::trackStatistics()
  4478. void trackStatistics() const override {}
  4479. };
  4480. /// NoCapture attribute deduction for a call site return value.
  4481. struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
  4482. AANoCaptureCallSiteReturned(const IRPosition &IRP, Attributor &A)
  4483. : AANoCaptureImpl(IRP, A) {}
  4484. /// See AbstractAttribute::initialize(...).
  4485. void initialize(Attributor &A) override {
  4486. const Function *F = getAnchorScope();
  4487. // Check what state the associated function can actually capture.
  4488. determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
  4489. }
  4490. /// See AbstractAttribute::trackStatistics()
  4491. void trackStatistics() const override {
  4492. STATS_DECLTRACK_CSRET_ATTR(nocapture)
  4493. }
  4494. };
  4495. /// ------------------ Value Simplify Attribute ----------------------------
  4496. bool ValueSimplifyStateType::unionAssumed(Optional<Value *> Other) {
  4497. // FIXME: Add a typecast support.
  4498. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  4499. SimplifiedAssociatedValue, Other, Ty);
  4500. if (SimplifiedAssociatedValue == Optional<Value *>(nullptr))
  4501. return false;
  4502. LLVM_DEBUG({
  4503. if (SimplifiedAssociatedValue.hasValue())
  4504. dbgs() << "[ValueSimplify] is assumed to be "
  4505. << **SimplifiedAssociatedValue << "\n";
  4506. else
  4507. dbgs() << "[ValueSimplify] is assumed to be <none>\n";
  4508. });
  4509. return true;
  4510. }
  4511. struct AAValueSimplifyImpl : AAValueSimplify {
  4512. AAValueSimplifyImpl(const IRPosition &IRP, Attributor &A)
  4513. : AAValueSimplify(IRP, A) {}
  4514. /// See AbstractAttribute::initialize(...).
  4515. void initialize(Attributor &A) override {
  4516. if (getAssociatedValue().getType()->isVoidTy())
  4517. indicatePessimisticFixpoint();
  4518. if (A.hasSimplificationCallback(getIRPosition()))
  4519. indicatePessimisticFixpoint();
  4520. }
  4521. /// See AbstractAttribute::getAsStr().
  4522. const std::string getAsStr() const override {
  4523. LLVM_DEBUG({
  4524. errs() << "SAV: " << SimplifiedAssociatedValue << " ";
  4525. if (SimplifiedAssociatedValue && *SimplifiedAssociatedValue)
  4526. errs() << "SAV: " << **SimplifiedAssociatedValue << " ";
  4527. });
  4528. return isValidState() ? (isAtFixpoint() ? "simplified" : "maybe-simple")
  4529. : "not-simple";
  4530. }
  4531. /// See AbstractAttribute::trackStatistics()
  4532. void trackStatistics() const override {}
  4533. /// See AAValueSimplify::getAssumedSimplifiedValue()
  4534. Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
  4535. return SimplifiedAssociatedValue;
  4536. }
  4537. /// Return a value we can use as replacement for the associated one, or
  4538. /// nullptr if we don't have one that makes sense.
  4539. Value *getReplacementValue(Attributor &A) const {
  4540. Value *NewV;
  4541. NewV = SimplifiedAssociatedValue.hasValue()
  4542. ? SimplifiedAssociatedValue.getValue()
  4543. : UndefValue::get(getAssociatedType());
  4544. if (!NewV)
  4545. return nullptr;
  4546. NewV = AA::getWithType(*NewV, *getAssociatedType());
  4547. if (!NewV || NewV == &getAssociatedValue())
  4548. return nullptr;
  4549. const Instruction *CtxI = getCtxI();
  4550. if (CtxI && !AA::isValidAtPosition(*NewV, *CtxI, A.getInfoCache()))
  4551. return nullptr;
  4552. if (!CtxI && !AA::isValidInScope(*NewV, getAnchorScope()))
  4553. return nullptr;
  4554. return NewV;
  4555. }
  4556. /// Helper function for querying AAValueSimplify and updating candicate.
  4557. /// \param IRP The value position we are trying to unify with SimplifiedValue
  4558. bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
  4559. const IRPosition &IRP, bool Simplify = true) {
  4560. bool UsedAssumedInformation = false;
  4561. Optional<Value *> QueryingValueSimplified = &IRP.getAssociatedValue();
  4562. if (Simplify)
  4563. QueryingValueSimplified =
  4564. A.getAssumedSimplified(IRP, QueryingAA, UsedAssumedInformation);
  4565. return unionAssumed(QueryingValueSimplified);
  4566. }
  4567. /// Returns a candidate is found or not
  4568. template <typename AAType> bool askSimplifiedValueFor(Attributor &A) {
  4569. if (!getAssociatedValue().getType()->isIntegerTy())
  4570. return false;
  4571. // This will also pass the call base context.
  4572. const auto &AA =
  4573. A.getAAFor<AAType>(*this, getIRPosition(), DepClassTy::NONE);
  4574. Optional<ConstantInt *> COpt = AA.getAssumedConstantInt(A);
  4575. if (!COpt.hasValue()) {
  4576. SimplifiedAssociatedValue = llvm::None;
  4577. A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
  4578. return true;
  4579. }
  4580. if (auto *C = COpt.getValue()) {
  4581. SimplifiedAssociatedValue = C;
  4582. A.recordDependence(AA, *this, DepClassTy::OPTIONAL);
  4583. return true;
  4584. }
  4585. return false;
  4586. }
  4587. bool askSimplifiedValueForOtherAAs(Attributor &A) {
  4588. if (askSimplifiedValueFor<AAValueConstantRange>(A))
  4589. return true;
  4590. if (askSimplifiedValueFor<AAPotentialValues>(A))
  4591. return true;
  4592. return false;
  4593. }
  4594. /// See AbstractAttribute::manifest(...).
  4595. ChangeStatus manifest(Attributor &A) override {
  4596. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  4597. if (getAssociatedValue().user_empty())
  4598. return Changed;
  4599. if (auto *NewV = getReplacementValue(A)) {
  4600. LLVM_DEBUG(dbgs() << "[ValueSimplify] " << getAssociatedValue() << " -> "
  4601. << *NewV << " :: " << *this << "\n");
  4602. if (A.changeValueAfterManifest(getAssociatedValue(), *NewV))
  4603. Changed = ChangeStatus::CHANGED;
  4604. }
  4605. return Changed | AAValueSimplify::manifest(A);
  4606. }
  4607. /// See AbstractState::indicatePessimisticFixpoint(...).
  4608. ChangeStatus indicatePessimisticFixpoint() override {
  4609. SimplifiedAssociatedValue = &getAssociatedValue();
  4610. return AAValueSimplify::indicatePessimisticFixpoint();
  4611. }
  4612. static bool handleLoad(Attributor &A, const AbstractAttribute &AA,
  4613. LoadInst &L, function_ref<bool(Value &)> Union) {
  4614. auto UnionWrapper = [&](Value &V, Value &Obj) {
  4615. if (isa<AllocaInst>(Obj))
  4616. return Union(V);
  4617. if (!AA::isDynamicallyUnique(A, AA, V))
  4618. return false;
  4619. if (!AA::isValidAtPosition(V, L, A.getInfoCache()))
  4620. return false;
  4621. return Union(V);
  4622. };
  4623. Value &Ptr = *L.getPointerOperand();
  4624. SmallVector<Value *, 8> Objects;
  4625. bool UsedAssumedInformation = false;
  4626. if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, AA, &L,
  4627. UsedAssumedInformation))
  4628. return false;
  4629. const auto *TLI =
  4630. A.getInfoCache().getTargetLibraryInfoForFunction(*L.getFunction());
  4631. for (Value *Obj : Objects) {
  4632. LLVM_DEBUG(dbgs() << "Visit underlying object " << *Obj << "\n");
  4633. if (isa<UndefValue>(Obj))
  4634. continue;
  4635. if (isa<ConstantPointerNull>(Obj)) {
  4636. // A null pointer access can be undefined but any offset from null may
  4637. // be OK. We do not try to optimize the latter.
  4638. if (!NullPointerIsDefined(L.getFunction(),
  4639. Ptr.getType()->getPointerAddressSpace()) &&
  4640. A.getAssumedSimplified(Ptr, AA, UsedAssumedInformation) == Obj)
  4641. continue;
  4642. return false;
  4643. }
  4644. Constant *InitialVal = AA::getInitialValueForObj(*Obj, *L.getType(), TLI);
  4645. if (!InitialVal || !Union(*InitialVal))
  4646. return false;
  4647. LLVM_DEBUG(dbgs() << "Underlying object amenable to load-store "
  4648. "propagation, checking accesses next.\n");
  4649. auto CheckAccess = [&](const AAPointerInfo::Access &Acc, bool IsExact) {
  4650. LLVM_DEBUG(dbgs() << " - visit access " << Acc << "\n");
  4651. if (Acc.isWrittenValueYetUndetermined())
  4652. return true;
  4653. Value *Content = Acc.getWrittenValue();
  4654. if (!Content)
  4655. return false;
  4656. Value *CastedContent =
  4657. AA::getWithType(*Content, *AA.getAssociatedType());
  4658. if (!CastedContent)
  4659. return false;
  4660. if (IsExact)
  4661. return UnionWrapper(*CastedContent, *Obj);
  4662. if (auto *C = dyn_cast<Constant>(CastedContent))
  4663. if (C->isNullValue() || C->isAllOnesValue() || isa<UndefValue>(C))
  4664. return UnionWrapper(*CastedContent, *Obj);
  4665. return false;
  4666. };
  4667. auto &PI = A.getAAFor<AAPointerInfo>(AA, IRPosition::value(*Obj),
  4668. DepClassTy::REQUIRED);
  4669. if (!PI.forallInterferingWrites(A, AA, L, CheckAccess))
  4670. return false;
  4671. }
  4672. return true;
  4673. }
  4674. };
  4675. struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
  4676. AAValueSimplifyArgument(const IRPosition &IRP, Attributor &A)
  4677. : AAValueSimplifyImpl(IRP, A) {}
  4678. void initialize(Attributor &A) override {
  4679. AAValueSimplifyImpl::initialize(A);
  4680. if (!getAnchorScope() || getAnchorScope()->isDeclaration())
  4681. indicatePessimisticFixpoint();
  4682. if (hasAttr({Attribute::InAlloca, Attribute::Preallocated,
  4683. Attribute::StructRet, Attribute::Nest, Attribute::ByVal},
  4684. /* IgnoreSubsumingPositions */ true))
  4685. indicatePessimisticFixpoint();
  4686. // FIXME: This is a hack to prevent us from propagating function poiner in
  4687. // the new pass manager CGSCC pass as it creates call edges the
  4688. // CallGraphUpdater cannot handle yet.
  4689. Value &V = getAssociatedValue();
  4690. if (V.getType()->isPointerTy() &&
  4691. V.getType()->getPointerElementType()->isFunctionTy() &&
  4692. !A.isModulePass())
  4693. indicatePessimisticFixpoint();
  4694. }
  4695. /// See AbstractAttribute::updateImpl(...).
  4696. ChangeStatus updateImpl(Attributor &A) override {
  4697. // Byval is only replacable if it is readonly otherwise we would write into
  4698. // the replaced value and not the copy that byval creates implicitly.
  4699. Argument *Arg = getAssociatedArgument();
  4700. if (Arg->hasByValAttr()) {
  4701. // TODO: We probably need to verify synchronization is not an issue, e.g.,
  4702. // there is no race by not copying a constant byval.
  4703. bool IsKnown;
  4704. if (!AA::isAssumedReadOnly(A, getIRPosition(), *this, IsKnown))
  4705. return indicatePessimisticFixpoint();
  4706. }
  4707. auto Before = SimplifiedAssociatedValue;
  4708. auto PredForCallSite = [&](AbstractCallSite ACS) {
  4709. const IRPosition &ACSArgPos =
  4710. IRPosition::callsite_argument(ACS, getCallSiteArgNo());
  4711. // Check if a coresponding argument was found or if it is on not
  4712. // associated (which can happen for callback calls).
  4713. if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
  4714. return false;
  4715. // Simplify the argument operand explicitly and check if the result is
  4716. // valid in the current scope. This avoids refering to simplified values
  4717. // in other functions, e.g., we don't want to say a an argument in a
  4718. // static function is actually an argument in a different function.
  4719. bool UsedAssumedInformation = false;
  4720. Optional<Constant *> SimpleArgOp =
  4721. A.getAssumedConstant(ACSArgPos, *this, UsedAssumedInformation);
  4722. if (!SimpleArgOp.hasValue())
  4723. return true;
  4724. if (!SimpleArgOp.getValue())
  4725. return false;
  4726. if (!AA::isDynamicallyUnique(A, *this, **SimpleArgOp))
  4727. return false;
  4728. return unionAssumed(*SimpleArgOp);
  4729. };
  4730. // Generate a answer specific to a call site context.
  4731. bool Success;
  4732. bool UsedAssumedInformation = false;
  4733. if (hasCallBaseContext() &&
  4734. getCallBaseContext()->getCalledFunction() == Arg->getParent())
  4735. Success = PredForCallSite(
  4736. AbstractCallSite(&getCallBaseContext()->getCalledOperandUse()));
  4737. else
  4738. Success = A.checkForAllCallSites(PredForCallSite, *this, true,
  4739. UsedAssumedInformation);
  4740. if (!Success)
  4741. if (!askSimplifiedValueForOtherAAs(A))
  4742. return indicatePessimisticFixpoint();
  4743. // If a candicate was found in this update, return CHANGED.
  4744. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  4745. : ChangeStatus ::CHANGED;
  4746. }
  4747. /// See AbstractAttribute::trackStatistics()
  4748. void trackStatistics() const override {
  4749. STATS_DECLTRACK_ARG_ATTR(value_simplify)
  4750. }
  4751. };
  4752. struct AAValueSimplifyReturned : AAValueSimplifyImpl {
  4753. AAValueSimplifyReturned(const IRPosition &IRP, Attributor &A)
  4754. : AAValueSimplifyImpl(IRP, A) {}
  4755. /// See AAValueSimplify::getAssumedSimplifiedValue()
  4756. Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
  4757. if (!isValidState())
  4758. return nullptr;
  4759. return SimplifiedAssociatedValue;
  4760. }
  4761. /// See AbstractAttribute::updateImpl(...).
  4762. ChangeStatus updateImpl(Attributor &A) override {
  4763. auto Before = SimplifiedAssociatedValue;
  4764. auto PredForReturned = [&](Value &V) {
  4765. return checkAndUpdate(A, *this,
  4766. IRPosition::value(V, getCallBaseContext()));
  4767. };
  4768. if (!A.checkForAllReturnedValues(PredForReturned, *this))
  4769. if (!askSimplifiedValueForOtherAAs(A))
  4770. return indicatePessimisticFixpoint();
  4771. // If a candicate was found in this update, return CHANGED.
  4772. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  4773. : ChangeStatus ::CHANGED;
  4774. }
  4775. ChangeStatus manifest(Attributor &A) override {
  4776. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  4777. if (auto *NewV = getReplacementValue(A)) {
  4778. auto PredForReturned =
  4779. [&](Value &, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
  4780. for (ReturnInst *RI : RetInsts) {
  4781. Value *ReturnedVal = RI->getReturnValue();
  4782. if (ReturnedVal == NewV || isa<UndefValue>(ReturnedVal))
  4783. return true;
  4784. assert(RI->getFunction() == getAnchorScope() &&
  4785. "ReturnInst in wrong function!");
  4786. LLVM_DEBUG(dbgs()
  4787. << "[ValueSimplify] " << *ReturnedVal << " -> "
  4788. << *NewV << " in " << *RI << " :: " << *this << "\n");
  4789. if (A.changeUseAfterManifest(RI->getOperandUse(0), *NewV))
  4790. Changed = ChangeStatus::CHANGED;
  4791. }
  4792. return true;
  4793. };
  4794. A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
  4795. }
  4796. return Changed | AAValueSimplify::manifest(A);
  4797. }
  4798. /// See AbstractAttribute::trackStatistics()
  4799. void trackStatistics() const override {
  4800. STATS_DECLTRACK_FNRET_ATTR(value_simplify)
  4801. }
  4802. };
  4803. struct AAValueSimplifyFloating : AAValueSimplifyImpl {
  4804. AAValueSimplifyFloating(const IRPosition &IRP, Attributor &A)
  4805. : AAValueSimplifyImpl(IRP, A) {}
  4806. /// See AbstractAttribute::initialize(...).
  4807. void initialize(Attributor &A) override {
  4808. AAValueSimplifyImpl::initialize(A);
  4809. Value &V = getAnchorValue();
  4810. // TODO: add other stuffs
  4811. if (isa<Constant>(V))
  4812. indicatePessimisticFixpoint();
  4813. }
  4814. /// Check if \p Cmp is a comparison we can simplify.
  4815. ///
  4816. /// We handle multiple cases, one in which at least one operand is an
  4817. /// (assumed) nullptr. If so, try to simplify it using AANonNull on the other
  4818. /// operand. Return true if successful, in that case SimplifiedAssociatedValue
  4819. /// will be updated.
  4820. bool handleCmp(Attributor &A, CmpInst &Cmp) {
  4821. auto Union = [&](Value &V) {
  4822. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  4823. SimplifiedAssociatedValue, &V, V.getType());
  4824. return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
  4825. };
  4826. Value *LHS = Cmp.getOperand(0);
  4827. Value *RHS = Cmp.getOperand(1);
  4828. // Simplify the operands first.
  4829. bool UsedAssumedInformation = false;
  4830. const auto &SimplifiedLHS =
  4831. A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
  4832. *this, UsedAssumedInformation);
  4833. if (!SimplifiedLHS.hasValue())
  4834. return true;
  4835. if (!SimplifiedLHS.getValue())
  4836. return false;
  4837. LHS = *SimplifiedLHS;
  4838. const auto &SimplifiedRHS =
  4839. A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
  4840. *this, UsedAssumedInformation);
  4841. if (!SimplifiedRHS.hasValue())
  4842. return true;
  4843. if (!SimplifiedRHS.getValue())
  4844. return false;
  4845. RHS = *SimplifiedRHS;
  4846. LLVMContext &Ctx = Cmp.getContext();
  4847. // Handle the trivial case first in which we don't even need to think about
  4848. // null or non-null.
  4849. if (LHS == RHS && (Cmp.isTrueWhenEqual() || Cmp.isFalseWhenEqual())) {
  4850. Constant *NewVal =
  4851. ConstantInt::get(Type::getInt1Ty(Ctx), Cmp.isTrueWhenEqual());
  4852. if (!Union(*NewVal))
  4853. return false;
  4854. if (!UsedAssumedInformation)
  4855. indicateOptimisticFixpoint();
  4856. return true;
  4857. }
  4858. // From now on we only handle equalities (==, !=).
  4859. ICmpInst *ICmp = dyn_cast<ICmpInst>(&Cmp);
  4860. if (!ICmp || !ICmp->isEquality())
  4861. return false;
  4862. bool LHSIsNull = isa<ConstantPointerNull>(LHS);
  4863. bool RHSIsNull = isa<ConstantPointerNull>(RHS);
  4864. if (!LHSIsNull && !RHSIsNull)
  4865. return false;
  4866. // Left is the nullptr ==/!= non-nullptr case. We'll use AANonNull on the
  4867. // non-nullptr operand and if we assume it's non-null we can conclude the
  4868. // result of the comparison.
  4869. assert((LHSIsNull || RHSIsNull) &&
  4870. "Expected nullptr versus non-nullptr comparison at this point");
  4871. // The index is the operand that we assume is not null.
  4872. unsigned PtrIdx = LHSIsNull;
  4873. auto &PtrNonNullAA = A.getAAFor<AANonNull>(
  4874. *this, IRPosition::value(*ICmp->getOperand(PtrIdx)),
  4875. DepClassTy::REQUIRED);
  4876. if (!PtrNonNullAA.isAssumedNonNull())
  4877. return false;
  4878. UsedAssumedInformation |= !PtrNonNullAA.isKnownNonNull();
  4879. // The new value depends on the predicate, true for != and false for ==.
  4880. Constant *NewVal = ConstantInt::get(
  4881. Type::getInt1Ty(Ctx), ICmp->getPredicate() == CmpInst::ICMP_NE);
  4882. if (!Union(*NewVal))
  4883. return false;
  4884. if (!UsedAssumedInformation)
  4885. indicateOptimisticFixpoint();
  4886. return true;
  4887. }
  4888. bool updateWithLoad(Attributor &A, LoadInst &L) {
  4889. auto Union = [&](Value &V) {
  4890. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  4891. SimplifiedAssociatedValue, &V, L.getType());
  4892. return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
  4893. };
  4894. return handleLoad(A, *this, L, Union);
  4895. }
  4896. /// Use the generic, non-optimistic InstSimplfy functionality if we managed to
  4897. /// simplify any operand of the instruction \p I. Return true if successful,
  4898. /// in that case SimplifiedAssociatedValue will be updated.
  4899. bool handleGenericInst(Attributor &A, Instruction &I) {
  4900. bool SomeSimplified = false;
  4901. bool UsedAssumedInformation = false;
  4902. SmallVector<Value *, 8> NewOps(I.getNumOperands());
  4903. int Idx = 0;
  4904. for (Value *Op : I.operands()) {
  4905. const auto &SimplifiedOp =
  4906. A.getAssumedSimplified(IRPosition::value(*Op, getCallBaseContext()),
  4907. *this, UsedAssumedInformation);
  4908. // If we are not sure about any operand we are not sure about the entire
  4909. // instruction, we'll wait.
  4910. if (!SimplifiedOp.hasValue())
  4911. return true;
  4912. if (SimplifiedOp.getValue())
  4913. NewOps[Idx] = SimplifiedOp.getValue();
  4914. else
  4915. NewOps[Idx] = Op;
  4916. SomeSimplified |= (NewOps[Idx] != Op);
  4917. ++Idx;
  4918. }
  4919. // We won't bother with the InstSimplify interface if we didn't simplify any
  4920. // operand ourselves.
  4921. if (!SomeSimplified)
  4922. return false;
  4923. InformationCache &InfoCache = A.getInfoCache();
  4924. Function *F = I.getFunction();
  4925. const auto *DT =
  4926. InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
  4927. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  4928. auto *AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
  4929. OptimizationRemarkEmitter *ORE = nullptr;
  4930. const DataLayout &DL = I.getModule()->getDataLayout();
  4931. SimplifyQuery Q(DL, TLI, DT, AC, &I);
  4932. if (Value *SimplifiedI =
  4933. SimplifyInstructionWithOperands(&I, NewOps, Q, ORE)) {
  4934. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  4935. SimplifiedAssociatedValue, SimplifiedI, I.getType());
  4936. return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
  4937. }
  4938. return false;
  4939. }
  4940. /// See AbstractAttribute::updateImpl(...).
  4941. ChangeStatus updateImpl(Attributor &A) override {
  4942. auto Before = SimplifiedAssociatedValue;
  4943. auto VisitValueCB = [&](Value &V, const Instruction *CtxI, bool &,
  4944. bool Stripped) -> bool {
  4945. auto &AA = A.getAAFor<AAValueSimplify>(
  4946. *this, IRPosition::value(V, getCallBaseContext()),
  4947. DepClassTy::REQUIRED);
  4948. if (!Stripped && this == &AA) {
  4949. if (auto *I = dyn_cast<Instruction>(&V)) {
  4950. if (auto *LI = dyn_cast<LoadInst>(&V))
  4951. if (updateWithLoad(A, *LI))
  4952. return true;
  4953. if (auto *Cmp = dyn_cast<CmpInst>(&V))
  4954. if (handleCmp(A, *Cmp))
  4955. return true;
  4956. if (handleGenericInst(A, *I))
  4957. return true;
  4958. }
  4959. // TODO: Look the instruction and check recursively.
  4960. LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
  4961. << "\n");
  4962. return false;
  4963. }
  4964. return checkAndUpdate(A, *this,
  4965. IRPosition::value(V, getCallBaseContext()));
  4966. };
  4967. bool Dummy = false;
  4968. bool UsedAssumedInformation = false;
  4969. if (!genericValueTraversal<bool>(A, getIRPosition(), *this, Dummy,
  4970. VisitValueCB, getCtxI(),
  4971. UsedAssumedInformation,
  4972. /* UseValueSimplify */ false))
  4973. if (!askSimplifiedValueForOtherAAs(A))
  4974. return indicatePessimisticFixpoint();
  4975. // If a candicate was found in this update, return CHANGED.
  4976. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  4977. : ChangeStatus ::CHANGED;
  4978. }
  4979. /// See AbstractAttribute::trackStatistics()
  4980. void trackStatistics() const override {
  4981. STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
  4982. }
  4983. };
  4984. struct AAValueSimplifyFunction : AAValueSimplifyImpl {
  4985. AAValueSimplifyFunction(const IRPosition &IRP, Attributor &A)
  4986. : AAValueSimplifyImpl(IRP, A) {}
  4987. /// See AbstractAttribute::initialize(...).
  4988. void initialize(Attributor &A) override {
  4989. SimplifiedAssociatedValue = nullptr;
  4990. indicateOptimisticFixpoint();
  4991. }
  4992. /// See AbstractAttribute::initialize(...).
  4993. ChangeStatus updateImpl(Attributor &A) override {
  4994. llvm_unreachable(
  4995. "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
  4996. }
  4997. /// See AbstractAttribute::trackStatistics()
  4998. void trackStatistics() const override {
  4999. STATS_DECLTRACK_FN_ATTR(value_simplify)
  5000. }
  5001. };
  5002. struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
  5003. AAValueSimplifyCallSite(const IRPosition &IRP, Attributor &A)
  5004. : AAValueSimplifyFunction(IRP, A) {}
  5005. /// See AbstractAttribute::trackStatistics()
  5006. void trackStatistics() const override {
  5007. STATS_DECLTRACK_CS_ATTR(value_simplify)
  5008. }
  5009. };
  5010. struct AAValueSimplifyCallSiteReturned : AAValueSimplifyImpl {
  5011. AAValueSimplifyCallSiteReturned(const IRPosition &IRP, Attributor &A)
  5012. : AAValueSimplifyImpl(IRP, A) {}
  5013. void initialize(Attributor &A) override {
  5014. AAValueSimplifyImpl::initialize(A);
  5015. if (!getAssociatedFunction())
  5016. indicatePessimisticFixpoint();
  5017. }
  5018. /// See AbstractAttribute::updateImpl(...).
  5019. ChangeStatus updateImpl(Attributor &A) override {
  5020. auto Before = SimplifiedAssociatedValue;
  5021. auto &RetAA = A.getAAFor<AAReturnedValues>(
  5022. *this, IRPosition::function(*getAssociatedFunction()),
  5023. DepClassTy::REQUIRED);
  5024. auto PredForReturned =
  5025. [&](Value &RetVal, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
  5026. bool UsedAssumedInformation = false;
  5027. Optional<Value *> CSRetVal = A.translateArgumentToCallSiteContent(
  5028. &RetVal, *cast<CallBase>(getCtxI()), *this,
  5029. UsedAssumedInformation);
  5030. SimplifiedAssociatedValue = AA::combineOptionalValuesInAAValueLatice(
  5031. SimplifiedAssociatedValue, CSRetVal, getAssociatedType());
  5032. return SimplifiedAssociatedValue != Optional<Value *>(nullptr);
  5033. };
  5034. if (!RetAA.checkForAllReturnedValuesAndReturnInsts(PredForReturned))
  5035. if (!askSimplifiedValueForOtherAAs(A))
  5036. return indicatePessimisticFixpoint();
  5037. return Before == SimplifiedAssociatedValue ? ChangeStatus::UNCHANGED
  5038. : ChangeStatus ::CHANGED;
  5039. }
  5040. void trackStatistics() const override {
  5041. STATS_DECLTRACK_CSRET_ATTR(value_simplify)
  5042. }
  5043. };
  5044. struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
  5045. AAValueSimplifyCallSiteArgument(const IRPosition &IRP, Attributor &A)
  5046. : AAValueSimplifyFloating(IRP, A) {}
  5047. /// See AbstractAttribute::manifest(...).
  5048. ChangeStatus manifest(Attributor &A) override {
  5049. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  5050. if (auto *NewV = getReplacementValue(A)) {
  5051. Use &U = cast<CallBase>(&getAnchorValue())
  5052. ->getArgOperandUse(getCallSiteArgNo());
  5053. if (A.changeUseAfterManifest(U, *NewV))
  5054. Changed = ChangeStatus::CHANGED;
  5055. }
  5056. return Changed | AAValueSimplify::manifest(A);
  5057. }
  5058. void trackStatistics() const override {
  5059. STATS_DECLTRACK_CSARG_ATTR(value_simplify)
  5060. }
  5061. };
  5062. /// ----------------------- Heap-To-Stack Conversion ---------------------------
  5063. struct AAHeapToStackFunction final : public AAHeapToStack {
  5064. struct AllocationInfo {
  5065. /// The call that allocates the memory.
  5066. CallBase *const CB;
  5067. /// The library function id for the allocation.
  5068. LibFunc LibraryFunctionId = NotLibFunc;
  5069. /// The status wrt. a rewrite.
  5070. enum {
  5071. STACK_DUE_TO_USE,
  5072. STACK_DUE_TO_FREE,
  5073. INVALID,
  5074. } Status = STACK_DUE_TO_USE;
  5075. /// Flag to indicate if we encountered a use that might free this allocation
  5076. /// but which is not in the deallocation infos.
  5077. bool HasPotentiallyFreeingUnknownUses = false;
  5078. /// The set of free calls that use this allocation.
  5079. SmallPtrSet<CallBase *, 1> PotentialFreeCalls{};
  5080. };
  5081. struct DeallocationInfo {
  5082. /// The call that deallocates the memory.
  5083. CallBase *const CB;
  5084. /// Flag to indicate if we don't know all objects this deallocation might
  5085. /// free.
  5086. bool MightFreeUnknownObjects = false;
  5087. /// The set of allocation calls that are potentially freed.
  5088. SmallPtrSet<CallBase *, 1> PotentialAllocationCalls{};
  5089. };
  5090. AAHeapToStackFunction(const IRPosition &IRP, Attributor &A)
  5091. : AAHeapToStack(IRP, A) {}
  5092. ~AAHeapToStackFunction() {
  5093. // Ensure we call the destructor so we release any memory allocated in the
  5094. // sets.
  5095. for (auto &It : AllocationInfos)
  5096. It.getSecond()->~AllocationInfo();
  5097. for (auto &It : DeallocationInfos)
  5098. It.getSecond()->~DeallocationInfo();
  5099. }
  5100. void initialize(Attributor &A) override {
  5101. AAHeapToStack::initialize(A);
  5102. const Function *F = getAnchorScope();
  5103. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5104. auto AllocationIdentifierCB = [&](Instruction &I) {
  5105. CallBase *CB = dyn_cast<CallBase>(&I);
  5106. if (!CB)
  5107. return true;
  5108. if (isFreeCall(CB, TLI)) {
  5109. DeallocationInfos[CB] = new (A.Allocator) DeallocationInfo{CB};
  5110. return true;
  5111. }
  5112. // To do heap to stack, we need to know that the allocation itself is
  5113. // removable once uses are rewritten, and that we can initialize the
  5114. // alloca to the same pattern as the original allocation result.
  5115. if (isAllocationFn(CB, TLI) && isAllocRemovable(CB, TLI)) {
  5116. auto *I8Ty = Type::getInt8Ty(CB->getParent()->getContext());
  5117. if (nullptr != getInitialValueOfAllocation(CB, TLI, I8Ty)) {
  5118. AllocationInfo *AI = new (A.Allocator) AllocationInfo{CB};
  5119. AllocationInfos[CB] = AI;
  5120. TLI->getLibFunc(*CB, AI->LibraryFunctionId);
  5121. }
  5122. }
  5123. return true;
  5124. };
  5125. bool UsedAssumedInformation = false;
  5126. bool Success = A.checkForAllCallLikeInstructions(
  5127. AllocationIdentifierCB, *this, UsedAssumedInformation,
  5128. /* CheckBBLivenessOnly */ false,
  5129. /* CheckPotentiallyDead */ true);
  5130. (void)Success;
  5131. assert(Success && "Did not expect the call base visit callback to fail!");
  5132. }
  5133. const std::string getAsStr() const override {
  5134. unsigned NumH2SMallocs = 0, NumInvalidMallocs = 0;
  5135. for (const auto &It : AllocationInfos) {
  5136. if (It.second->Status == AllocationInfo::INVALID)
  5137. ++NumInvalidMallocs;
  5138. else
  5139. ++NumH2SMallocs;
  5140. }
  5141. return "[H2S] Mallocs Good/Bad: " + std::to_string(NumH2SMallocs) + "/" +
  5142. std::to_string(NumInvalidMallocs);
  5143. }
  5144. /// See AbstractAttribute::trackStatistics().
  5145. void trackStatistics() const override {
  5146. STATS_DECL(
  5147. MallocCalls, Function,
  5148. "Number of malloc/calloc/aligned_alloc calls converted to allocas");
  5149. for (auto &It : AllocationInfos)
  5150. if (It.second->Status != AllocationInfo::INVALID)
  5151. ++BUILD_STAT_NAME(MallocCalls, Function);
  5152. }
  5153. bool isAssumedHeapToStack(const CallBase &CB) const override {
  5154. if (isValidState())
  5155. if (AllocationInfo *AI = AllocationInfos.lookup(&CB))
  5156. return AI->Status != AllocationInfo::INVALID;
  5157. return false;
  5158. }
  5159. bool isAssumedHeapToStackRemovedFree(CallBase &CB) const override {
  5160. if (!isValidState())
  5161. return false;
  5162. for (auto &It : AllocationInfos) {
  5163. AllocationInfo &AI = *It.second;
  5164. if (AI.Status == AllocationInfo::INVALID)
  5165. continue;
  5166. if (AI.PotentialFreeCalls.count(&CB))
  5167. return true;
  5168. }
  5169. return false;
  5170. }
  5171. ChangeStatus manifest(Attributor &A) override {
  5172. assert(getState().isValidState() &&
  5173. "Attempted to manifest an invalid state!");
  5174. ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
  5175. Function *F = getAnchorScope();
  5176. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5177. for (auto &It : AllocationInfos) {
  5178. AllocationInfo &AI = *It.second;
  5179. if (AI.Status == AllocationInfo::INVALID)
  5180. continue;
  5181. for (CallBase *FreeCall : AI.PotentialFreeCalls) {
  5182. LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
  5183. A.deleteAfterManifest(*FreeCall);
  5184. HasChanged = ChangeStatus::CHANGED;
  5185. }
  5186. LLVM_DEBUG(dbgs() << "H2S: Removing malloc-like call: " << *AI.CB
  5187. << "\n");
  5188. auto Remark = [&](OptimizationRemark OR) {
  5189. LibFunc IsAllocShared;
  5190. if (TLI->getLibFunc(*AI.CB, IsAllocShared))
  5191. if (IsAllocShared == LibFunc___kmpc_alloc_shared)
  5192. return OR << "Moving globalized variable to the stack.";
  5193. return OR << "Moving memory allocation from the heap to the stack.";
  5194. };
  5195. if (AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
  5196. A.emitRemark<OptimizationRemark>(AI.CB, "OMP110", Remark);
  5197. else
  5198. A.emitRemark<OptimizationRemark>(AI.CB, "HeapToStack", Remark);
  5199. const DataLayout &DL = A.getInfoCache().getDL();
  5200. Value *Size;
  5201. Optional<APInt> SizeAPI = getSize(A, *this, AI);
  5202. if (SizeAPI.hasValue()) {
  5203. Size = ConstantInt::get(AI.CB->getContext(), *SizeAPI);
  5204. } else {
  5205. LLVMContext &Ctx = AI.CB->getContext();
  5206. ObjectSizeOpts Opts;
  5207. ObjectSizeOffsetEvaluator Eval(DL, TLI, Ctx, Opts);
  5208. SizeOffsetEvalType SizeOffsetPair = Eval.compute(AI.CB);
  5209. assert(SizeOffsetPair != ObjectSizeOffsetEvaluator::unknown() &&
  5210. cast<ConstantInt>(SizeOffsetPair.second)->isZero());
  5211. Size = SizeOffsetPair.first;
  5212. }
  5213. Align Alignment(1);
  5214. if (MaybeAlign RetAlign = AI.CB->getRetAlign())
  5215. Alignment = max(Alignment, RetAlign);
  5216. if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
  5217. Optional<APInt> AlignmentAPI = getAPInt(A, *this, *Align);
  5218. assert(AlignmentAPI.hasValue() &&
  5219. "Expected an alignment during manifest!");
  5220. Alignment =
  5221. max(Alignment, MaybeAlign(AlignmentAPI.getValue().getZExtValue()));
  5222. }
  5223. // TODO: Hoist the alloca towards the function entry.
  5224. unsigned AS = DL.getAllocaAddrSpace();
  5225. Instruction *Alloca = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
  5226. Size, Alignment, "", AI.CB);
  5227. if (Alloca->getType() != AI.CB->getType())
  5228. Alloca = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
  5229. Alloca, AI.CB->getType(), "malloc_cast", AI.CB);
  5230. auto *I8Ty = Type::getInt8Ty(F->getContext());
  5231. auto *InitVal = getInitialValueOfAllocation(AI.CB, TLI, I8Ty);
  5232. assert(InitVal &&
  5233. "Must be able to materialize initial memory state of allocation");
  5234. A.changeValueAfterManifest(*AI.CB, *Alloca);
  5235. if (auto *II = dyn_cast<InvokeInst>(AI.CB)) {
  5236. auto *NBB = II->getNormalDest();
  5237. BranchInst::Create(NBB, AI.CB->getParent());
  5238. A.deleteAfterManifest(*AI.CB);
  5239. } else {
  5240. A.deleteAfterManifest(*AI.CB);
  5241. }
  5242. // Initialize the alloca with the same value as used by the allocation
  5243. // function. We can skip undef as the initial value of an alloc is
  5244. // undef, and the memset would simply end up being DSEd.
  5245. if (!isa<UndefValue>(InitVal)) {
  5246. IRBuilder<> Builder(Alloca->getNextNode());
  5247. // TODO: Use alignment above if align!=1
  5248. Builder.CreateMemSet(Alloca, InitVal, Size, None);
  5249. }
  5250. HasChanged = ChangeStatus::CHANGED;
  5251. }
  5252. return HasChanged;
  5253. }
  5254. Optional<APInt> getAPInt(Attributor &A, const AbstractAttribute &AA,
  5255. Value &V) {
  5256. bool UsedAssumedInformation = false;
  5257. Optional<Constant *> SimpleV =
  5258. A.getAssumedConstant(V, AA, UsedAssumedInformation);
  5259. if (!SimpleV.hasValue())
  5260. return APInt(64, 0);
  5261. if (auto *CI = dyn_cast_or_null<ConstantInt>(SimpleV.getValue()))
  5262. return CI->getValue();
  5263. return llvm::None;
  5264. }
  5265. Optional<APInt> getSize(Attributor &A, const AbstractAttribute &AA,
  5266. AllocationInfo &AI) {
  5267. auto Mapper = [&](const Value *V) -> const Value * {
  5268. bool UsedAssumedInformation = false;
  5269. if (Optional<Constant *> SimpleV =
  5270. A.getAssumedConstant(*V, AA, UsedAssumedInformation))
  5271. if (*SimpleV)
  5272. return *SimpleV;
  5273. return V;
  5274. };
  5275. const Function *F = getAnchorScope();
  5276. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5277. return getAllocSize(AI.CB, TLI, Mapper);
  5278. }
  5279. /// Collection of all malloc-like calls in a function with associated
  5280. /// information.
  5281. DenseMap<CallBase *, AllocationInfo *> AllocationInfos;
  5282. /// Collection of all free-like calls in a function with associated
  5283. /// information.
  5284. DenseMap<CallBase *, DeallocationInfo *> DeallocationInfos;
  5285. ChangeStatus updateImpl(Attributor &A) override;
  5286. };
  5287. ChangeStatus AAHeapToStackFunction::updateImpl(Attributor &A) {
  5288. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  5289. const Function *F = getAnchorScope();
  5290. const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
  5291. const auto &LivenessAA =
  5292. A.getAAFor<AAIsDead>(*this, IRPosition::function(*F), DepClassTy::NONE);
  5293. MustBeExecutedContextExplorer &Explorer =
  5294. A.getInfoCache().getMustBeExecutedContextExplorer();
  5295. bool StackIsAccessibleByOtherThreads =
  5296. A.getInfoCache().stackIsAccessibleByOtherThreads();
  5297. // Flag to ensure we update our deallocation information at most once per
  5298. // updateImpl call and only if we use the free check reasoning.
  5299. bool HasUpdatedFrees = false;
  5300. auto UpdateFrees = [&]() {
  5301. HasUpdatedFrees = true;
  5302. for (auto &It : DeallocationInfos) {
  5303. DeallocationInfo &DI = *It.second;
  5304. // For now we cannot use deallocations that have unknown inputs, skip
  5305. // them.
  5306. if (DI.MightFreeUnknownObjects)
  5307. continue;
  5308. // No need to analyze dead calls, ignore them instead.
  5309. bool UsedAssumedInformation = false;
  5310. if (A.isAssumedDead(*DI.CB, this, &LivenessAA, UsedAssumedInformation,
  5311. /* CheckBBLivenessOnly */ true))
  5312. continue;
  5313. // Use the optimistic version to get the freed objects, ignoring dead
  5314. // branches etc.
  5315. SmallVector<Value *, 8> Objects;
  5316. if (!AA::getAssumedUnderlyingObjects(A, *DI.CB->getArgOperand(0), Objects,
  5317. *this, DI.CB,
  5318. UsedAssumedInformation)) {
  5319. LLVM_DEBUG(
  5320. dbgs()
  5321. << "[H2S] Unexpected failure in getAssumedUnderlyingObjects!\n");
  5322. DI.MightFreeUnknownObjects = true;
  5323. continue;
  5324. }
  5325. // Check each object explicitly.
  5326. for (auto *Obj : Objects) {
  5327. // Free of null and undef can be ignored as no-ops (or UB in the latter
  5328. // case).
  5329. if (isa<ConstantPointerNull>(Obj) || isa<UndefValue>(Obj))
  5330. continue;
  5331. CallBase *ObjCB = dyn_cast<CallBase>(Obj);
  5332. if (!ObjCB) {
  5333. LLVM_DEBUG(dbgs()
  5334. << "[H2S] Free of a non-call object: " << *Obj << "\n");
  5335. DI.MightFreeUnknownObjects = true;
  5336. continue;
  5337. }
  5338. AllocationInfo *AI = AllocationInfos.lookup(ObjCB);
  5339. if (!AI) {
  5340. LLVM_DEBUG(dbgs() << "[H2S] Free of a non-allocation object: " << *Obj
  5341. << "\n");
  5342. DI.MightFreeUnknownObjects = true;
  5343. continue;
  5344. }
  5345. DI.PotentialAllocationCalls.insert(ObjCB);
  5346. }
  5347. }
  5348. };
  5349. auto FreeCheck = [&](AllocationInfo &AI) {
  5350. // If the stack is not accessible by other threads, the "must-free" logic
  5351. // doesn't apply as the pointer could be shared and needs to be places in
  5352. // "shareable" memory.
  5353. if (!StackIsAccessibleByOtherThreads) {
  5354. auto &NoSyncAA =
  5355. A.getAAFor<AANoSync>(*this, getIRPosition(), DepClassTy::OPTIONAL);
  5356. if (!NoSyncAA.isAssumedNoSync()) {
  5357. LLVM_DEBUG(
  5358. dbgs() << "[H2S] found an escaping use, stack is not accessible by "
  5359. "other threads and function is not nosync:\n");
  5360. return false;
  5361. }
  5362. }
  5363. if (!HasUpdatedFrees)
  5364. UpdateFrees();
  5365. // TODO: Allow multi exit functions that have different free calls.
  5366. if (AI.PotentialFreeCalls.size() != 1) {
  5367. LLVM_DEBUG(dbgs() << "[H2S] did not find one free call but "
  5368. << AI.PotentialFreeCalls.size() << "\n");
  5369. return false;
  5370. }
  5371. CallBase *UniqueFree = *AI.PotentialFreeCalls.begin();
  5372. DeallocationInfo *DI = DeallocationInfos.lookup(UniqueFree);
  5373. if (!DI) {
  5374. LLVM_DEBUG(
  5375. dbgs() << "[H2S] unique free call was not known as deallocation call "
  5376. << *UniqueFree << "\n");
  5377. return false;
  5378. }
  5379. if (DI->MightFreeUnknownObjects) {
  5380. LLVM_DEBUG(
  5381. dbgs() << "[H2S] unique free call might free unknown allocations\n");
  5382. return false;
  5383. }
  5384. if (DI->PotentialAllocationCalls.size() > 1) {
  5385. LLVM_DEBUG(dbgs() << "[H2S] unique free call might free "
  5386. << DI->PotentialAllocationCalls.size()
  5387. << " different allocations\n");
  5388. return false;
  5389. }
  5390. if (*DI->PotentialAllocationCalls.begin() != AI.CB) {
  5391. LLVM_DEBUG(
  5392. dbgs()
  5393. << "[H2S] unique free call not known to free this allocation but "
  5394. << **DI->PotentialAllocationCalls.begin() << "\n");
  5395. return false;
  5396. }
  5397. Instruction *CtxI = isa<InvokeInst>(AI.CB) ? AI.CB : AI.CB->getNextNode();
  5398. if (!Explorer.findInContextOf(UniqueFree, CtxI)) {
  5399. LLVM_DEBUG(
  5400. dbgs()
  5401. << "[H2S] unique free call might not be executed with the allocation "
  5402. << *UniqueFree << "\n");
  5403. return false;
  5404. }
  5405. return true;
  5406. };
  5407. auto UsesCheck = [&](AllocationInfo &AI) {
  5408. bool ValidUsesOnly = true;
  5409. auto Pred = [&](const Use &U, bool &Follow) -> bool {
  5410. Instruction *UserI = cast<Instruction>(U.getUser());
  5411. if (isa<LoadInst>(UserI))
  5412. return true;
  5413. if (auto *SI = dyn_cast<StoreInst>(UserI)) {
  5414. if (SI->getValueOperand() == U.get()) {
  5415. LLVM_DEBUG(dbgs()
  5416. << "[H2S] escaping store to memory: " << *UserI << "\n");
  5417. ValidUsesOnly = false;
  5418. } else {
  5419. // A store into the malloc'ed memory is fine.
  5420. }
  5421. return true;
  5422. }
  5423. if (auto *CB = dyn_cast<CallBase>(UserI)) {
  5424. if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
  5425. return true;
  5426. if (DeallocationInfos.count(CB)) {
  5427. AI.PotentialFreeCalls.insert(CB);
  5428. return true;
  5429. }
  5430. unsigned ArgNo = CB->getArgOperandNo(&U);
  5431. const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
  5432. *this, IRPosition::callsite_argument(*CB, ArgNo),
  5433. DepClassTy::OPTIONAL);
  5434. // If a call site argument use is nofree, we are fine.
  5435. const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
  5436. *this, IRPosition::callsite_argument(*CB, ArgNo),
  5437. DepClassTy::OPTIONAL);
  5438. bool MaybeCaptured = !NoCaptureAA.isAssumedNoCapture();
  5439. bool MaybeFreed = !ArgNoFreeAA.isAssumedNoFree();
  5440. if (MaybeCaptured ||
  5441. (AI.LibraryFunctionId != LibFunc___kmpc_alloc_shared &&
  5442. MaybeFreed)) {
  5443. AI.HasPotentiallyFreeingUnknownUses |= MaybeFreed;
  5444. // Emit a missed remark if this is missed OpenMP globalization.
  5445. auto Remark = [&](OptimizationRemarkMissed ORM) {
  5446. return ORM
  5447. << "Could not move globalized variable to the stack. "
  5448. "Variable is potentially captured in call. Mark "
  5449. "parameter as `__attribute__((noescape))` to override.";
  5450. };
  5451. if (ValidUsesOnly &&
  5452. AI.LibraryFunctionId == LibFunc___kmpc_alloc_shared)
  5453. A.emitRemark<OptimizationRemarkMissed>(AI.CB, "OMP113", Remark);
  5454. LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
  5455. ValidUsesOnly = false;
  5456. }
  5457. return true;
  5458. }
  5459. if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
  5460. isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
  5461. Follow = true;
  5462. return true;
  5463. }
  5464. // Unknown user for which we can not track uses further (in a way that
  5465. // makes sense).
  5466. LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
  5467. ValidUsesOnly = false;
  5468. return true;
  5469. };
  5470. if (!A.checkForAllUses(Pred, *this, *AI.CB))
  5471. return false;
  5472. return ValidUsesOnly;
  5473. };
  5474. // The actual update starts here. We look at all allocations and depending on
  5475. // their status perform the appropriate check(s).
  5476. for (auto &It : AllocationInfos) {
  5477. AllocationInfo &AI = *It.second;
  5478. if (AI.Status == AllocationInfo::INVALID)
  5479. continue;
  5480. if (Value *Align = getAllocAlignment(AI.CB, TLI)) {
  5481. if (!getAPInt(A, *this, *Align)) {
  5482. // Can't generate an alloca which respects the required alignment
  5483. // on the allocation.
  5484. LLVM_DEBUG(dbgs() << "[H2S] Unknown allocation alignment: " << *AI.CB
  5485. << "\n");
  5486. AI.Status = AllocationInfo::INVALID;
  5487. Changed = ChangeStatus::CHANGED;
  5488. continue;
  5489. }
  5490. }
  5491. if (MaxHeapToStackSize != -1) {
  5492. Optional<APInt> Size = getSize(A, *this, AI);
  5493. if (!Size.hasValue() || Size.getValue().ugt(MaxHeapToStackSize)) {
  5494. LLVM_DEBUG({
  5495. if (!Size.hasValue())
  5496. dbgs() << "[H2S] Unknown allocation size: " << *AI.CB << "\n";
  5497. else
  5498. dbgs() << "[H2S] Allocation size too large: " << *AI.CB << " vs. "
  5499. << MaxHeapToStackSize << "\n";
  5500. });
  5501. AI.Status = AllocationInfo::INVALID;
  5502. Changed = ChangeStatus::CHANGED;
  5503. continue;
  5504. }
  5505. }
  5506. switch (AI.Status) {
  5507. case AllocationInfo::STACK_DUE_TO_USE:
  5508. if (UsesCheck(AI))
  5509. continue;
  5510. AI.Status = AllocationInfo::STACK_DUE_TO_FREE;
  5511. LLVM_FALLTHROUGH;
  5512. case AllocationInfo::STACK_DUE_TO_FREE:
  5513. if (FreeCheck(AI))
  5514. continue;
  5515. AI.Status = AllocationInfo::INVALID;
  5516. Changed = ChangeStatus::CHANGED;
  5517. continue;
  5518. case AllocationInfo::INVALID:
  5519. llvm_unreachable("Invalid allocations should never reach this point!");
  5520. };
  5521. }
  5522. return Changed;
  5523. }
  5524. /// ----------------------- Privatizable Pointers ------------------------------
  5525. struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
  5526. AAPrivatizablePtrImpl(const IRPosition &IRP, Attributor &A)
  5527. : AAPrivatizablePtr(IRP, A), PrivatizableType(llvm::None) {}
  5528. ChangeStatus indicatePessimisticFixpoint() override {
  5529. AAPrivatizablePtr::indicatePessimisticFixpoint();
  5530. PrivatizableType = nullptr;
  5531. return ChangeStatus::CHANGED;
  5532. }
  5533. /// Identify the type we can chose for a private copy of the underlying
  5534. /// argument. None means it is not clear yet, nullptr means there is none.
  5535. virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
  5536. /// Return a privatizable type that encloses both T0 and T1.
  5537. /// TODO: This is merely a stub for now as we should manage a mapping as well.
  5538. Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
  5539. if (!T0.hasValue())
  5540. return T1;
  5541. if (!T1.hasValue())
  5542. return T0;
  5543. if (T0 == T1)
  5544. return T0;
  5545. return nullptr;
  5546. }
  5547. Optional<Type *> getPrivatizableType() const override {
  5548. return PrivatizableType;
  5549. }
  5550. const std::string getAsStr() const override {
  5551. return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
  5552. }
  5553. protected:
  5554. Optional<Type *> PrivatizableType;
  5555. };
  5556. // TODO: Do this for call site arguments (probably also other values) as well.
  5557. struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
  5558. AAPrivatizablePtrArgument(const IRPosition &IRP, Attributor &A)
  5559. : AAPrivatizablePtrImpl(IRP, A) {}
  5560. /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
  5561. Optional<Type *> identifyPrivatizableType(Attributor &A) override {
  5562. // If this is a byval argument and we know all the call sites (so we can
  5563. // rewrite them), there is no need to check them explicitly.
  5564. bool UsedAssumedInformation = false;
  5565. if (getIRPosition().hasAttr(Attribute::ByVal) &&
  5566. A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
  5567. true, UsedAssumedInformation))
  5568. return getAssociatedValue().getType()->getPointerElementType();
  5569. Optional<Type *> Ty;
  5570. unsigned ArgNo = getIRPosition().getCallSiteArgNo();
  5571. // Make sure the associated call site argument has the same type at all call
  5572. // sites and it is an allocation we know is safe to privatize, for now that
  5573. // means we only allow alloca instructions.
  5574. // TODO: We can additionally analyze the accesses in the callee to create
  5575. // the type from that information instead. That is a little more
  5576. // involved and will be done in a follow up patch.
  5577. auto CallSiteCheck = [&](AbstractCallSite ACS) {
  5578. IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
  5579. // Check if a coresponding argument was found or if it is one not
  5580. // associated (which can happen for callback calls).
  5581. if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
  5582. return false;
  5583. // Check that all call sites agree on a type.
  5584. auto &PrivCSArgAA =
  5585. A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos, DepClassTy::REQUIRED);
  5586. Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
  5587. LLVM_DEBUG({
  5588. dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
  5589. if (CSTy.hasValue() && CSTy.getValue())
  5590. CSTy.getValue()->print(dbgs());
  5591. else if (CSTy.hasValue())
  5592. dbgs() << "<nullptr>";
  5593. else
  5594. dbgs() << "<none>";
  5595. });
  5596. Ty = combineTypes(Ty, CSTy);
  5597. LLVM_DEBUG({
  5598. dbgs() << " : New Type: ";
  5599. if (Ty.hasValue() && Ty.getValue())
  5600. Ty.getValue()->print(dbgs());
  5601. else if (Ty.hasValue())
  5602. dbgs() << "<nullptr>";
  5603. else
  5604. dbgs() << "<none>";
  5605. dbgs() << "\n";
  5606. });
  5607. return !Ty.hasValue() || Ty.getValue();
  5608. };
  5609. if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
  5610. UsedAssumedInformation))
  5611. return nullptr;
  5612. return Ty;
  5613. }
  5614. /// See AbstractAttribute::updateImpl(...).
  5615. ChangeStatus updateImpl(Attributor &A) override {
  5616. PrivatizableType = identifyPrivatizableType(A);
  5617. if (!PrivatizableType.hasValue())
  5618. return ChangeStatus::UNCHANGED;
  5619. if (!PrivatizableType.getValue())
  5620. return indicatePessimisticFixpoint();
  5621. // The dependence is optional so we don't give up once we give up on the
  5622. // alignment.
  5623. A.getAAFor<AAAlign>(*this, IRPosition::value(getAssociatedValue()),
  5624. DepClassTy::OPTIONAL);
  5625. // Avoid arguments with padding for now.
  5626. if (!getIRPosition().hasAttr(Attribute::ByVal) &&
  5627. !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
  5628. A.getInfoCache().getDL())) {
  5629. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
  5630. return indicatePessimisticFixpoint();
  5631. }
  5632. // Collect the types that will replace the privatizable type in the function
  5633. // signature.
  5634. SmallVector<Type *, 16> ReplacementTypes;
  5635. identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
  5636. // Verify callee and caller agree on how the promoted argument would be
  5637. // passed.
  5638. Function &Fn = *getIRPosition().getAnchorScope();
  5639. const auto *TTI =
  5640. A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
  5641. if (!TTI) {
  5642. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Missing TTI for function "
  5643. << Fn.getName() << "\n");
  5644. return indicatePessimisticFixpoint();
  5645. }
  5646. auto CallSiteCheck = [&](AbstractCallSite ACS) {
  5647. CallBase *CB = ACS.getInstruction();
  5648. return TTI->areTypesABICompatible(
  5649. CB->getCaller(), CB->getCalledFunction(), ReplacementTypes);
  5650. };
  5651. bool UsedAssumedInformation = false;
  5652. if (!A.checkForAllCallSites(CallSiteCheck, *this, true,
  5653. UsedAssumedInformation)) {
  5654. LLVM_DEBUG(
  5655. dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
  5656. << Fn.getName() << "\n");
  5657. return indicatePessimisticFixpoint();
  5658. }
  5659. // Register a rewrite of the argument.
  5660. Argument *Arg = getAssociatedArgument();
  5661. if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
  5662. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
  5663. return indicatePessimisticFixpoint();
  5664. }
  5665. unsigned ArgNo = Arg->getArgNo();
  5666. // Helper to check if for the given call site the associated argument is
  5667. // passed to a callback where the privatization would be different.
  5668. auto IsCompatiblePrivArgOfCallback = [&](CallBase &CB) {
  5669. SmallVector<const Use *, 4> CallbackUses;
  5670. AbstractCallSite::getCallbackUses(CB, CallbackUses);
  5671. for (const Use *U : CallbackUses) {
  5672. AbstractCallSite CBACS(U);
  5673. assert(CBACS && CBACS.isCallbackCall());
  5674. for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
  5675. int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
  5676. LLVM_DEBUG({
  5677. dbgs()
  5678. << "[AAPrivatizablePtr] Argument " << *Arg
  5679. << "check if can be privatized in the context of its parent ("
  5680. << Arg->getParent()->getName()
  5681. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  5682. "callback ("
  5683. << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
  5684. << ")\n[AAPrivatizablePtr] " << CBArg << " : "
  5685. << CBACS.getCallArgOperand(CBArg) << " vs "
  5686. << CB.getArgOperand(ArgNo) << "\n"
  5687. << "[AAPrivatizablePtr] " << CBArg << " : "
  5688. << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
  5689. });
  5690. if (CBArgNo != int(ArgNo))
  5691. continue;
  5692. const auto &CBArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
  5693. *this, IRPosition::argument(CBArg), DepClassTy::REQUIRED);
  5694. if (CBArgPrivAA.isValidState()) {
  5695. auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
  5696. if (!CBArgPrivTy.hasValue())
  5697. continue;
  5698. if (CBArgPrivTy.getValue() == PrivatizableType)
  5699. continue;
  5700. }
  5701. LLVM_DEBUG({
  5702. dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
  5703. << " cannot be privatized in the context of its parent ("
  5704. << Arg->getParent()->getName()
  5705. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  5706. "callback ("
  5707. << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
  5708. << ").\n[AAPrivatizablePtr] for which the argument "
  5709. "privatization is not compatible.\n";
  5710. });
  5711. return false;
  5712. }
  5713. }
  5714. return true;
  5715. };
  5716. // Helper to check if for the given call site the associated argument is
  5717. // passed to a direct call where the privatization would be different.
  5718. auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
  5719. CallBase *DC = cast<CallBase>(ACS.getInstruction());
  5720. int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
  5721. assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->arg_size() &&
  5722. "Expected a direct call operand for callback call operand");
  5723. LLVM_DEBUG({
  5724. dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
  5725. << " check if be privatized in the context of its parent ("
  5726. << Arg->getParent()->getName()
  5727. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  5728. "direct call of ("
  5729. << DCArgNo << "@" << DC->getCalledFunction()->getName()
  5730. << ").\n";
  5731. });
  5732. Function *DCCallee = DC->getCalledFunction();
  5733. if (unsigned(DCArgNo) < DCCallee->arg_size()) {
  5734. const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
  5735. *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)),
  5736. DepClassTy::REQUIRED);
  5737. if (DCArgPrivAA.isValidState()) {
  5738. auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
  5739. if (!DCArgPrivTy.hasValue())
  5740. return true;
  5741. if (DCArgPrivTy.getValue() == PrivatizableType)
  5742. return true;
  5743. }
  5744. }
  5745. LLVM_DEBUG({
  5746. dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
  5747. << " cannot be privatized in the context of its parent ("
  5748. << Arg->getParent()->getName()
  5749. << ")\n[AAPrivatizablePtr] because it is an argument in a "
  5750. "direct call of ("
  5751. << ACS.getInstruction()->getCalledFunction()->getName()
  5752. << ").\n[AAPrivatizablePtr] for which the argument "
  5753. "privatization is not compatible.\n";
  5754. });
  5755. return false;
  5756. };
  5757. // Helper to check if the associated argument is used at the given abstract
  5758. // call site in a way that is incompatible with the privatization assumed
  5759. // here.
  5760. auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
  5761. if (ACS.isDirectCall())
  5762. return IsCompatiblePrivArgOfCallback(*ACS.getInstruction());
  5763. if (ACS.isCallbackCall())
  5764. return IsCompatiblePrivArgOfDirectCS(ACS);
  5765. return false;
  5766. };
  5767. if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
  5768. UsedAssumedInformation))
  5769. return indicatePessimisticFixpoint();
  5770. return ChangeStatus::UNCHANGED;
  5771. }
  5772. /// Given a type to private \p PrivType, collect the constituates (which are
  5773. /// used) in \p ReplacementTypes.
  5774. static void
  5775. identifyReplacementTypes(Type *PrivType,
  5776. SmallVectorImpl<Type *> &ReplacementTypes) {
  5777. // TODO: For now we expand the privatization type to the fullest which can
  5778. // lead to dead arguments that need to be removed later.
  5779. assert(PrivType && "Expected privatizable type!");
  5780. // Traverse the type, extract constituate types on the outermost level.
  5781. if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
  5782. for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
  5783. ReplacementTypes.push_back(PrivStructType->getElementType(u));
  5784. } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
  5785. ReplacementTypes.append(PrivArrayType->getNumElements(),
  5786. PrivArrayType->getElementType());
  5787. } else {
  5788. ReplacementTypes.push_back(PrivType);
  5789. }
  5790. }
  5791. /// Initialize \p Base according to the type \p PrivType at position \p IP.
  5792. /// The values needed are taken from the arguments of \p F starting at
  5793. /// position \p ArgNo.
  5794. static void createInitialization(Type *PrivType, Value &Base, Function &F,
  5795. unsigned ArgNo, Instruction &IP) {
  5796. assert(PrivType && "Expected privatizable type!");
  5797. IRBuilder<NoFolder> IRB(&IP);
  5798. const DataLayout &DL = F.getParent()->getDataLayout();
  5799. // Traverse the type, build GEPs and stores.
  5800. if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
  5801. const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
  5802. for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
  5803. Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
  5804. Value *Ptr =
  5805. constructPointer(PointeeTy, PrivType, &Base,
  5806. PrivStructLayout->getElementOffset(u), IRB, DL);
  5807. new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
  5808. }
  5809. } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
  5810. Type *PointeeTy = PrivArrayType->getElementType();
  5811. Type *PointeePtrTy = PointeeTy->getPointerTo();
  5812. uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
  5813. for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
  5814. Value *Ptr = constructPointer(PointeePtrTy, PrivType, &Base,
  5815. u * PointeeTySize, IRB, DL);
  5816. new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
  5817. }
  5818. } else {
  5819. new StoreInst(F.getArg(ArgNo), &Base, &IP);
  5820. }
  5821. }
  5822. /// Extract values from \p Base according to the type \p PrivType at the
  5823. /// call position \p ACS. The values are appended to \p ReplacementValues.
  5824. void createReplacementValues(Align Alignment, Type *PrivType,
  5825. AbstractCallSite ACS, Value *Base,
  5826. SmallVectorImpl<Value *> &ReplacementValues) {
  5827. assert(Base && "Expected base value!");
  5828. assert(PrivType && "Expected privatizable type!");
  5829. Instruction *IP = ACS.getInstruction();
  5830. IRBuilder<NoFolder> IRB(IP);
  5831. const DataLayout &DL = IP->getModule()->getDataLayout();
  5832. Type *PrivPtrType = PrivType->getPointerTo();
  5833. if (Base->getType() != PrivPtrType)
  5834. Base = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
  5835. Base, PrivPtrType, "", ACS.getInstruction());
  5836. // Traverse the type, build GEPs and loads.
  5837. if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
  5838. const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
  5839. for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
  5840. Type *PointeeTy = PrivStructType->getElementType(u);
  5841. Value *Ptr =
  5842. constructPointer(PointeeTy->getPointerTo(), PrivType, Base,
  5843. PrivStructLayout->getElementOffset(u), IRB, DL);
  5844. LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
  5845. L->setAlignment(Alignment);
  5846. ReplacementValues.push_back(L);
  5847. }
  5848. } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
  5849. Type *PointeeTy = PrivArrayType->getElementType();
  5850. uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
  5851. Type *PointeePtrTy = PointeeTy->getPointerTo();
  5852. for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
  5853. Value *Ptr = constructPointer(PointeePtrTy, PrivType, Base,
  5854. u * PointeeTySize, IRB, DL);
  5855. LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
  5856. L->setAlignment(Alignment);
  5857. ReplacementValues.push_back(L);
  5858. }
  5859. } else {
  5860. LoadInst *L = new LoadInst(PrivType, Base, "", IP);
  5861. L->setAlignment(Alignment);
  5862. ReplacementValues.push_back(L);
  5863. }
  5864. }
  5865. /// See AbstractAttribute::manifest(...)
  5866. ChangeStatus manifest(Attributor &A) override {
  5867. if (!PrivatizableType.hasValue())
  5868. return ChangeStatus::UNCHANGED;
  5869. assert(PrivatizableType.getValue() && "Expected privatizable type!");
  5870. // Collect all tail calls in the function as we cannot allow new allocas to
  5871. // escape into tail recursion.
  5872. // TODO: Be smarter about new allocas escaping into tail calls.
  5873. SmallVector<CallInst *, 16> TailCalls;
  5874. bool UsedAssumedInformation = false;
  5875. if (!A.checkForAllInstructions(
  5876. [&](Instruction &I) {
  5877. CallInst &CI = cast<CallInst>(I);
  5878. if (CI.isTailCall())
  5879. TailCalls.push_back(&CI);
  5880. return true;
  5881. },
  5882. *this, {Instruction::Call}, UsedAssumedInformation))
  5883. return ChangeStatus::UNCHANGED;
  5884. Argument *Arg = getAssociatedArgument();
  5885. // Query AAAlign attribute for alignment of associated argument to
  5886. // determine the best alignment of loads.
  5887. const auto &AlignAA =
  5888. A.getAAFor<AAAlign>(*this, IRPosition::value(*Arg), DepClassTy::NONE);
  5889. // Callback to repair the associated function. A new alloca is placed at the
  5890. // beginning and initialized with the values passed through arguments. The
  5891. // new alloca replaces the use of the old pointer argument.
  5892. Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
  5893. [=](const Attributor::ArgumentReplacementInfo &ARI,
  5894. Function &ReplacementFn, Function::arg_iterator ArgIt) {
  5895. BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
  5896. Instruction *IP = &*EntryBB.getFirstInsertionPt();
  5897. const DataLayout &DL = IP->getModule()->getDataLayout();
  5898. unsigned AS = DL.getAllocaAddrSpace();
  5899. Instruction *AI = new AllocaInst(PrivatizableType.getValue(), AS,
  5900. Arg->getName() + ".priv", IP);
  5901. createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
  5902. ArgIt->getArgNo(), *IP);
  5903. if (AI->getType() != Arg->getType())
  5904. AI = BitCastInst::CreatePointerBitCastOrAddrSpaceCast(
  5905. AI, Arg->getType(), "", IP);
  5906. Arg->replaceAllUsesWith(AI);
  5907. for (CallInst *CI : TailCalls)
  5908. CI->setTailCall(false);
  5909. };
  5910. // Callback to repair a call site of the associated function. The elements
  5911. // of the privatizable type are loaded prior to the call and passed to the
  5912. // new function version.
  5913. Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
  5914. [=, &AlignAA](const Attributor::ArgumentReplacementInfo &ARI,
  5915. AbstractCallSite ACS,
  5916. SmallVectorImpl<Value *> &NewArgOperands) {
  5917. // When no alignment is specified for the load instruction,
  5918. // natural alignment is assumed.
  5919. createReplacementValues(
  5920. assumeAligned(AlignAA.getAssumedAlign()),
  5921. PrivatizableType.getValue(), ACS,
  5922. ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
  5923. NewArgOperands);
  5924. };
  5925. // Collect the types that will replace the privatizable type in the function
  5926. // signature.
  5927. SmallVector<Type *, 16> ReplacementTypes;
  5928. identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
  5929. // Register a rewrite of the argument.
  5930. if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
  5931. std::move(FnRepairCB),
  5932. std::move(ACSRepairCB)))
  5933. return ChangeStatus::CHANGED;
  5934. return ChangeStatus::UNCHANGED;
  5935. }
  5936. /// See AbstractAttribute::trackStatistics()
  5937. void trackStatistics() const override {
  5938. STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
  5939. }
  5940. };
  5941. struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
  5942. AAPrivatizablePtrFloating(const IRPosition &IRP, Attributor &A)
  5943. : AAPrivatizablePtrImpl(IRP, A) {}
  5944. /// See AbstractAttribute::initialize(...).
  5945. virtual void initialize(Attributor &A) override {
  5946. // TODO: We can privatize more than arguments.
  5947. indicatePessimisticFixpoint();
  5948. }
  5949. ChangeStatus updateImpl(Attributor &A) override {
  5950. llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
  5951. "updateImpl will not be called");
  5952. }
  5953. /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
  5954. Optional<Type *> identifyPrivatizableType(Attributor &A) override {
  5955. Value *Obj = getUnderlyingObject(&getAssociatedValue());
  5956. if (!Obj) {
  5957. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
  5958. return nullptr;
  5959. }
  5960. if (auto *AI = dyn_cast<AllocaInst>(Obj))
  5961. if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
  5962. if (CI->isOne())
  5963. return AI->getAllocatedType();
  5964. if (auto *Arg = dyn_cast<Argument>(Obj)) {
  5965. auto &PrivArgAA = A.getAAFor<AAPrivatizablePtr>(
  5966. *this, IRPosition::argument(*Arg), DepClassTy::REQUIRED);
  5967. if (PrivArgAA.isAssumedPrivatizablePtr())
  5968. return Obj->getType()->getPointerElementType();
  5969. }
  5970. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
  5971. "alloca nor privatizable argument: "
  5972. << *Obj << "!\n");
  5973. return nullptr;
  5974. }
  5975. /// See AbstractAttribute::trackStatistics()
  5976. void trackStatistics() const override {
  5977. STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
  5978. }
  5979. };
  5980. struct AAPrivatizablePtrCallSiteArgument final
  5981. : public AAPrivatizablePtrFloating {
  5982. AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP, Attributor &A)
  5983. : AAPrivatizablePtrFloating(IRP, A) {}
  5984. /// See AbstractAttribute::initialize(...).
  5985. void initialize(Attributor &A) override {
  5986. if (getIRPosition().hasAttr(Attribute::ByVal))
  5987. indicateOptimisticFixpoint();
  5988. }
  5989. /// See AbstractAttribute::updateImpl(...).
  5990. ChangeStatus updateImpl(Attributor &A) override {
  5991. PrivatizableType = identifyPrivatizableType(A);
  5992. if (!PrivatizableType.hasValue())
  5993. return ChangeStatus::UNCHANGED;
  5994. if (!PrivatizableType.getValue())
  5995. return indicatePessimisticFixpoint();
  5996. const IRPosition &IRP = getIRPosition();
  5997. auto &NoCaptureAA =
  5998. A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::REQUIRED);
  5999. if (!NoCaptureAA.isAssumedNoCapture()) {
  6000. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
  6001. return indicatePessimisticFixpoint();
  6002. }
  6003. auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP, DepClassTy::REQUIRED);
  6004. if (!NoAliasAA.isAssumedNoAlias()) {
  6005. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
  6006. return indicatePessimisticFixpoint();
  6007. }
  6008. bool IsKnown;
  6009. if (!AA::isAssumedReadOnly(A, IRP, *this, IsKnown)) {
  6010. LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
  6011. return indicatePessimisticFixpoint();
  6012. }
  6013. return ChangeStatus::UNCHANGED;
  6014. }
  6015. /// See AbstractAttribute::trackStatistics()
  6016. void trackStatistics() const override {
  6017. STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
  6018. }
  6019. };
  6020. struct AAPrivatizablePtrCallSiteReturned final
  6021. : public AAPrivatizablePtrFloating {
  6022. AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP, Attributor &A)
  6023. : AAPrivatizablePtrFloating(IRP, A) {}
  6024. /// See AbstractAttribute::initialize(...).
  6025. void initialize(Attributor &A) override {
  6026. // TODO: We can privatize more than arguments.
  6027. indicatePessimisticFixpoint();
  6028. }
  6029. /// See AbstractAttribute::trackStatistics()
  6030. void trackStatistics() const override {
  6031. STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
  6032. }
  6033. };
  6034. struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
  6035. AAPrivatizablePtrReturned(const IRPosition &IRP, Attributor &A)
  6036. : AAPrivatizablePtrFloating(IRP, A) {}
  6037. /// See AbstractAttribute::initialize(...).
  6038. void initialize(Attributor &A) override {
  6039. // TODO: We can privatize more than arguments.
  6040. indicatePessimisticFixpoint();
  6041. }
  6042. /// See AbstractAttribute::trackStatistics()
  6043. void trackStatistics() const override {
  6044. STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
  6045. }
  6046. };
  6047. /// -------------------- Memory Behavior Attributes ----------------------------
  6048. /// Includes read-none, read-only, and write-only.
  6049. /// ----------------------------------------------------------------------------
  6050. struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
  6051. AAMemoryBehaviorImpl(const IRPosition &IRP, Attributor &A)
  6052. : AAMemoryBehavior(IRP, A) {}
  6053. /// See AbstractAttribute::initialize(...).
  6054. void initialize(Attributor &A) override {
  6055. intersectAssumedBits(BEST_STATE);
  6056. getKnownStateFromValue(getIRPosition(), getState());
  6057. AAMemoryBehavior::initialize(A);
  6058. }
  6059. /// Return the memory behavior information encoded in the IR for \p IRP.
  6060. static void getKnownStateFromValue(const IRPosition &IRP,
  6061. BitIntegerState &State,
  6062. bool IgnoreSubsumingPositions = false) {
  6063. SmallVector<Attribute, 2> Attrs;
  6064. IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
  6065. for (const Attribute &Attr : Attrs) {
  6066. switch (Attr.getKindAsEnum()) {
  6067. case Attribute::ReadNone:
  6068. State.addKnownBits(NO_ACCESSES);
  6069. break;
  6070. case Attribute::ReadOnly:
  6071. State.addKnownBits(NO_WRITES);
  6072. break;
  6073. case Attribute::WriteOnly:
  6074. State.addKnownBits(NO_READS);
  6075. break;
  6076. default:
  6077. llvm_unreachable("Unexpected attribute!");
  6078. }
  6079. }
  6080. if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
  6081. if (!I->mayReadFromMemory())
  6082. State.addKnownBits(NO_READS);
  6083. if (!I->mayWriteToMemory())
  6084. State.addKnownBits(NO_WRITES);
  6085. }
  6086. }
  6087. /// See AbstractAttribute::getDeducedAttributes(...).
  6088. void getDeducedAttributes(LLVMContext &Ctx,
  6089. SmallVectorImpl<Attribute> &Attrs) const override {
  6090. assert(Attrs.size() == 0);
  6091. if (isAssumedReadNone())
  6092. Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
  6093. else if (isAssumedReadOnly())
  6094. Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
  6095. else if (isAssumedWriteOnly())
  6096. Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
  6097. assert(Attrs.size() <= 1);
  6098. }
  6099. /// See AbstractAttribute::manifest(...).
  6100. ChangeStatus manifest(Attributor &A) override {
  6101. if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
  6102. return ChangeStatus::UNCHANGED;
  6103. const IRPosition &IRP = getIRPosition();
  6104. // Check if we would improve the existing attributes first.
  6105. SmallVector<Attribute, 4> DeducedAttrs;
  6106. getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
  6107. if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
  6108. return IRP.hasAttr(Attr.getKindAsEnum(),
  6109. /* IgnoreSubsumingPositions */ true);
  6110. }))
  6111. return ChangeStatus::UNCHANGED;
  6112. // Clear existing attributes.
  6113. IRP.removeAttrs(AttrKinds);
  6114. // Use the generic manifest method.
  6115. return IRAttribute::manifest(A);
  6116. }
  6117. /// See AbstractState::getAsStr().
  6118. const std::string getAsStr() const override {
  6119. if (isAssumedReadNone())
  6120. return "readnone";
  6121. if (isAssumedReadOnly())
  6122. return "readonly";
  6123. if (isAssumedWriteOnly())
  6124. return "writeonly";
  6125. return "may-read/write";
  6126. }
  6127. /// The set of IR attributes AAMemoryBehavior deals with.
  6128. static const Attribute::AttrKind AttrKinds[3];
  6129. };
  6130. const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
  6131. Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
  6132. /// Memory behavior attribute for a floating value.
  6133. struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
  6134. AAMemoryBehaviorFloating(const IRPosition &IRP, Attributor &A)
  6135. : AAMemoryBehaviorImpl(IRP, A) {}
  6136. /// See AbstractAttribute::updateImpl(...).
  6137. ChangeStatus updateImpl(Attributor &A) override;
  6138. /// See AbstractAttribute::trackStatistics()
  6139. void trackStatistics() const override {
  6140. if (isAssumedReadNone())
  6141. STATS_DECLTRACK_FLOATING_ATTR(readnone)
  6142. else if (isAssumedReadOnly())
  6143. STATS_DECLTRACK_FLOATING_ATTR(readonly)
  6144. else if (isAssumedWriteOnly())
  6145. STATS_DECLTRACK_FLOATING_ATTR(writeonly)
  6146. }
  6147. private:
  6148. /// Return true if users of \p UserI might access the underlying
  6149. /// variable/location described by \p U and should therefore be analyzed.
  6150. bool followUsersOfUseIn(Attributor &A, const Use &U,
  6151. const Instruction *UserI);
  6152. /// Update the state according to the effect of use \p U in \p UserI.
  6153. void analyzeUseIn(Attributor &A, const Use &U, const Instruction *UserI);
  6154. };
  6155. /// Memory behavior attribute for function argument.
  6156. struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
  6157. AAMemoryBehaviorArgument(const IRPosition &IRP, Attributor &A)
  6158. : AAMemoryBehaviorFloating(IRP, A) {}
  6159. /// See AbstractAttribute::initialize(...).
  6160. void initialize(Attributor &A) override {
  6161. intersectAssumedBits(BEST_STATE);
  6162. const IRPosition &IRP = getIRPosition();
  6163. // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
  6164. // can query it when we use has/getAttr. That would allow us to reuse the
  6165. // initialize of the base class here.
  6166. bool HasByVal =
  6167. IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
  6168. getKnownStateFromValue(IRP, getState(),
  6169. /* IgnoreSubsumingPositions */ HasByVal);
  6170. // Initialize the use vector with all direct uses of the associated value.
  6171. Argument *Arg = getAssociatedArgument();
  6172. if (!Arg || !A.isFunctionIPOAmendable(*(Arg->getParent())))
  6173. indicatePessimisticFixpoint();
  6174. }
  6175. ChangeStatus manifest(Attributor &A) override {
  6176. // TODO: Pointer arguments are not supported on vectors of pointers yet.
  6177. if (!getAssociatedValue().getType()->isPointerTy())
  6178. return ChangeStatus::UNCHANGED;
  6179. // TODO: From readattrs.ll: "inalloca parameters are always
  6180. // considered written"
  6181. if (hasAttr({Attribute::InAlloca, Attribute::Preallocated})) {
  6182. removeKnownBits(NO_WRITES);
  6183. removeAssumedBits(NO_WRITES);
  6184. }
  6185. return AAMemoryBehaviorFloating::manifest(A);
  6186. }
  6187. /// See AbstractAttribute::trackStatistics()
  6188. void trackStatistics() const override {
  6189. if (isAssumedReadNone())
  6190. STATS_DECLTRACK_ARG_ATTR(readnone)
  6191. else if (isAssumedReadOnly())
  6192. STATS_DECLTRACK_ARG_ATTR(readonly)
  6193. else if (isAssumedWriteOnly())
  6194. STATS_DECLTRACK_ARG_ATTR(writeonly)
  6195. }
  6196. };
  6197. struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
  6198. AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP, Attributor &A)
  6199. : AAMemoryBehaviorArgument(IRP, A) {}
  6200. /// See AbstractAttribute::initialize(...).
  6201. void initialize(Attributor &A) override {
  6202. // If we don't have an associated attribute this is either a variadic call
  6203. // or an indirect call, either way, nothing to do here.
  6204. Argument *Arg = getAssociatedArgument();
  6205. if (!Arg) {
  6206. indicatePessimisticFixpoint();
  6207. return;
  6208. }
  6209. if (Arg->hasByValAttr()) {
  6210. addKnownBits(NO_WRITES);
  6211. removeKnownBits(NO_READS);
  6212. removeAssumedBits(NO_READS);
  6213. }
  6214. AAMemoryBehaviorArgument::initialize(A);
  6215. if (getAssociatedFunction()->isDeclaration())
  6216. indicatePessimisticFixpoint();
  6217. }
  6218. /// See AbstractAttribute::updateImpl(...).
  6219. ChangeStatus updateImpl(Attributor &A) override {
  6220. // TODO: Once we have call site specific value information we can provide
  6221. // call site specific liveness liveness information and then it makes
  6222. // sense to specialize attributes for call sites arguments instead of
  6223. // redirecting requests to the callee argument.
  6224. Argument *Arg = getAssociatedArgument();
  6225. const IRPosition &ArgPos = IRPosition::argument(*Arg);
  6226. auto &ArgAA =
  6227. A.getAAFor<AAMemoryBehavior>(*this, ArgPos, DepClassTy::REQUIRED);
  6228. return clampStateAndIndicateChange(getState(), ArgAA.getState());
  6229. }
  6230. /// See AbstractAttribute::trackStatistics()
  6231. void trackStatistics() const override {
  6232. if (isAssumedReadNone())
  6233. STATS_DECLTRACK_CSARG_ATTR(readnone)
  6234. else if (isAssumedReadOnly())
  6235. STATS_DECLTRACK_CSARG_ATTR(readonly)
  6236. else if (isAssumedWriteOnly())
  6237. STATS_DECLTRACK_CSARG_ATTR(writeonly)
  6238. }
  6239. };
  6240. /// Memory behavior attribute for a call site return position.
  6241. struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
  6242. AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP, Attributor &A)
  6243. : AAMemoryBehaviorFloating(IRP, A) {}
  6244. /// See AbstractAttribute::initialize(...).
  6245. void initialize(Attributor &A) override {
  6246. AAMemoryBehaviorImpl::initialize(A);
  6247. Function *F = getAssociatedFunction();
  6248. if (!F || F->isDeclaration())
  6249. indicatePessimisticFixpoint();
  6250. }
  6251. /// See AbstractAttribute::manifest(...).
  6252. ChangeStatus manifest(Attributor &A) override {
  6253. // We do not annotate returned values.
  6254. return ChangeStatus::UNCHANGED;
  6255. }
  6256. /// See AbstractAttribute::trackStatistics()
  6257. void trackStatistics() const override {}
  6258. };
  6259. /// An AA to represent the memory behavior function attributes.
  6260. struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
  6261. AAMemoryBehaviorFunction(const IRPosition &IRP, Attributor &A)
  6262. : AAMemoryBehaviorImpl(IRP, A) {}
  6263. /// See AbstractAttribute::updateImpl(Attributor &A).
  6264. virtual ChangeStatus updateImpl(Attributor &A) override;
  6265. /// See AbstractAttribute::manifest(...).
  6266. ChangeStatus manifest(Attributor &A) override {
  6267. Function &F = cast<Function>(getAnchorValue());
  6268. if (isAssumedReadNone()) {
  6269. F.removeFnAttr(Attribute::ArgMemOnly);
  6270. F.removeFnAttr(Attribute::InaccessibleMemOnly);
  6271. F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
  6272. }
  6273. return AAMemoryBehaviorImpl::manifest(A);
  6274. }
  6275. /// See AbstractAttribute::trackStatistics()
  6276. void trackStatistics() const override {
  6277. if (isAssumedReadNone())
  6278. STATS_DECLTRACK_FN_ATTR(readnone)
  6279. else if (isAssumedReadOnly())
  6280. STATS_DECLTRACK_FN_ATTR(readonly)
  6281. else if (isAssumedWriteOnly())
  6282. STATS_DECLTRACK_FN_ATTR(writeonly)
  6283. }
  6284. };
  6285. /// AAMemoryBehavior attribute for call sites.
  6286. struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
  6287. AAMemoryBehaviorCallSite(const IRPosition &IRP, Attributor &A)
  6288. : AAMemoryBehaviorImpl(IRP, A) {}
  6289. /// See AbstractAttribute::initialize(...).
  6290. void initialize(Attributor &A) override {
  6291. AAMemoryBehaviorImpl::initialize(A);
  6292. Function *F = getAssociatedFunction();
  6293. if (!F || F->isDeclaration())
  6294. indicatePessimisticFixpoint();
  6295. }
  6296. /// See AbstractAttribute::updateImpl(...).
  6297. ChangeStatus updateImpl(Attributor &A) override {
  6298. // TODO: Once we have call site specific value information we can provide
  6299. // call site specific liveness liveness information and then it makes
  6300. // sense to specialize attributes for call sites arguments instead of
  6301. // redirecting requests to the callee argument.
  6302. Function *F = getAssociatedFunction();
  6303. const IRPosition &FnPos = IRPosition::function(*F);
  6304. auto &FnAA =
  6305. A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::REQUIRED);
  6306. return clampStateAndIndicateChange(getState(), FnAA.getState());
  6307. }
  6308. /// See AbstractAttribute::trackStatistics()
  6309. void trackStatistics() const override {
  6310. if (isAssumedReadNone())
  6311. STATS_DECLTRACK_CS_ATTR(readnone)
  6312. else if (isAssumedReadOnly())
  6313. STATS_DECLTRACK_CS_ATTR(readonly)
  6314. else if (isAssumedWriteOnly())
  6315. STATS_DECLTRACK_CS_ATTR(writeonly)
  6316. }
  6317. };
  6318. ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
  6319. // The current assumed state used to determine a change.
  6320. auto AssumedState = getAssumed();
  6321. auto CheckRWInst = [&](Instruction &I) {
  6322. // If the instruction has an own memory behavior state, use it to restrict
  6323. // the local state. No further analysis is required as the other memory
  6324. // state is as optimistic as it gets.
  6325. if (const auto *CB = dyn_cast<CallBase>(&I)) {
  6326. const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
  6327. *this, IRPosition::callsite_function(*CB), DepClassTy::REQUIRED);
  6328. intersectAssumedBits(MemBehaviorAA.getAssumed());
  6329. return !isAtFixpoint();
  6330. }
  6331. // Remove access kind modifiers if necessary.
  6332. if (I.mayReadFromMemory())
  6333. removeAssumedBits(NO_READS);
  6334. if (I.mayWriteToMemory())
  6335. removeAssumedBits(NO_WRITES);
  6336. return !isAtFixpoint();
  6337. };
  6338. bool UsedAssumedInformation = false;
  6339. if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
  6340. UsedAssumedInformation))
  6341. return indicatePessimisticFixpoint();
  6342. return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
  6343. : ChangeStatus::UNCHANGED;
  6344. }
  6345. ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
  6346. const IRPosition &IRP = getIRPosition();
  6347. const IRPosition &FnPos = IRPosition::function_scope(IRP);
  6348. AAMemoryBehavior::StateType &S = getState();
  6349. // First, check the function scope. We take the known information and we avoid
  6350. // work if the assumed information implies the current assumed information for
  6351. // this attribute. This is a valid for all but byval arguments.
  6352. Argument *Arg = IRP.getAssociatedArgument();
  6353. AAMemoryBehavior::base_t FnMemAssumedState =
  6354. AAMemoryBehavior::StateType::getWorstState();
  6355. if (!Arg || !Arg->hasByValAttr()) {
  6356. const auto &FnMemAA =
  6357. A.getAAFor<AAMemoryBehavior>(*this, FnPos, DepClassTy::OPTIONAL);
  6358. FnMemAssumedState = FnMemAA.getAssumed();
  6359. S.addKnownBits(FnMemAA.getKnown());
  6360. if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
  6361. return ChangeStatus::UNCHANGED;
  6362. }
  6363. // The current assumed state used to determine a change.
  6364. auto AssumedState = S.getAssumed();
  6365. // Make sure the value is not captured (except through "return"), if
  6366. // it is, any information derived would be irrelevant anyway as we cannot
  6367. // check the potential aliases introduced by the capture. However, no need
  6368. // to fall back to anythign less optimistic than the function state.
  6369. const auto &ArgNoCaptureAA =
  6370. A.getAAFor<AANoCapture>(*this, IRP, DepClassTy::OPTIONAL);
  6371. if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
  6372. S.intersectAssumedBits(FnMemAssumedState);
  6373. return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
  6374. : ChangeStatus::UNCHANGED;
  6375. }
  6376. // Visit and expand uses until all are analyzed or a fixpoint is reached.
  6377. auto UsePred = [&](const Use &U, bool &Follow) -> bool {
  6378. Instruction *UserI = cast<Instruction>(U.getUser());
  6379. LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << *U << " in " << *UserI
  6380. << " \n");
  6381. // Droppable users, e.g., llvm::assume does not actually perform any action.
  6382. if (UserI->isDroppable())
  6383. return true;
  6384. // Check if the users of UserI should also be visited.
  6385. Follow = followUsersOfUseIn(A, U, UserI);
  6386. // If UserI might touch memory we analyze the use in detail.
  6387. if (UserI->mayReadOrWriteMemory())
  6388. analyzeUseIn(A, U, UserI);
  6389. return !isAtFixpoint();
  6390. };
  6391. if (!A.checkForAllUses(UsePred, *this, getAssociatedValue()))
  6392. return indicatePessimisticFixpoint();
  6393. return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
  6394. : ChangeStatus::UNCHANGED;
  6395. }
  6396. bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use &U,
  6397. const Instruction *UserI) {
  6398. // The loaded value is unrelated to the pointer argument, no need to
  6399. // follow the users of the load.
  6400. if (isa<LoadInst>(UserI))
  6401. return false;
  6402. // By default we follow all uses assuming UserI might leak information on U,
  6403. // we have special handling for call sites operands though.
  6404. const auto *CB = dyn_cast<CallBase>(UserI);
  6405. if (!CB || !CB->isArgOperand(&U))
  6406. return true;
  6407. // If the use is a call argument known not to be captured, the users of
  6408. // the call do not need to be visited because they have to be unrelated to
  6409. // the input. Note that this check is not trivial even though we disallow
  6410. // general capturing of the underlying argument. The reason is that the
  6411. // call might the argument "through return", which we allow and for which we
  6412. // need to check call users.
  6413. if (U.get()->getType()->isPointerTy()) {
  6414. unsigned ArgNo = CB->getArgOperandNo(&U);
  6415. const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
  6416. *this, IRPosition::callsite_argument(*CB, ArgNo), DepClassTy::OPTIONAL);
  6417. return !ArgNoCaptureAA.isAssumedNoCapture();
  6418. }
  6419. return true;
  6420. }
  6421. void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use &U,
  6422. const Instruction *UserI) {
  6423. assert(UserI->mayReadOrWriteMemory());
  6424. switch (UserI->getOpcode()) {
  6425. default:
  6426. // TODO: Handle all atomics and other side-effect operations we know of.
  6427. break;
  6428. case Instruction::Load:
  6429. // Loads cause the NO_READS property to disappear.
  6430. removeAssumedBits(NO_READS);
  6431. return;
  6432. case Instruction::Store:
  6433. // Stores cause the NO_WRITES property to disappear if the use is the
  6434. // pointer operand. Note that while capturing was taken care of somewhere
  6435. // else we need to deal with stores of the value that is not looked through.
  6436. if (cast<StoreInst>(UserI)->getPointerOperand() == U.get())
  6437. removeAssumedBits(NO_WRITES);
  6438. else
  6439. indicatePessimisticFixpoint();
  6440. return;
  6441. case Instruction::Call:
  6442. case Instruction::CallBr:
  6443. case Instruction::Invoke: {
  6444. // For call sites we look at the argument memory behavior attribute (this
  6445. // could be recursive!) in order to restrict our own state.
  6446. const auto *CB = cast<CallBase>(UserI);
  6447. // Give up on operand bundles.
  6448. if (CB->isBundleOperand(&U)) {
  6449. indicatePessimisticFixpoint();
  6450. return;
  6451. }
  6452. // Calling a function does read the function pointer, maybe write it if the
  6453. // function is self-modifying.
  6454. if (CB->isCallee(&U)) {
  6455. removeAssumedBits(NO_READS);
  6456. break;
  6457. }
  6458. // Adjust the possible access behavior based on the information on the
  6459. // argument.
  6460. IRPosition Pos;
  6461. if (U.get()->getType()->isPointerTy())
  6462. Pos = IRPosition::callsite_argument(*CB, CB->getArgOperandNo(&U));
  6463. else
  6464. Pos = IRPosition::callsite_function(*CB);
  6465. const auto &MemBehaviorAA =
  6466. A.getAAFor<AAMemoryBehavior>(*this, Pos, DepClassTy::OPTIONAL);
  6467. // "assumed" has at most the same bits as the MemBehaviorAA assumed
  6468. // and at least "known".
  6469. intersectAssumedBits(MemBehaviorAA.getAssumed());
  6470. return;
  6471. }
  6472. };
  6473. // Generally, look at the "may-properties" and adjust the assumed state if we
  6474. // did not trigger special handling before.
  6475. if (UserI->mayReadFromMemory())
  6476. removeAssumedBits(NO_READS);
  6477. if (UserI->mayWriteToMemory())
  6478. removeAssumedBits(NO_WRITES);
  6479. }
  6480. /// -------------------- Memory Locations Attributes ---------------------------
  6481. /// Includes read-none, argmemonly, inaccessiblememonly,
  6482. /// inaccessiblememorargmemonly
  6483. /// ----------------------------------------------------------------------------
  6484. std::string AAMemoryLocation::getMemoryLocationsAsStr(
  6485. AAMemoryLocation::MemoryLocationsKind MLK) {
  6486. if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
  6487. return "all memory";
  6488. if (MLK == AAMemoryLocation::NO_LOCATIONS)
  6489. return "no memory";
  6490. std::string S = "memory:";
  6491. if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
  6492. S += "stack,";
  6493. if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
  6494. S += "constant,";
  6495. if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
  6496. S += "internal global,";
  6497. if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
  6498. S += "external global,";
  6499. if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
  6500. S += "argument,";
  6501. if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
  6502. S += "inaccessible,";
  6503. if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
  6504. S += "malloced,";
  6505. if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
  6506. S += "unknown,";
  6507. S.pop_back();
  6508. return S;
  6509. }
  6510. struct AAMemoryLocationImpl : public AAMemoryLocation {
  6511. AAMemoryLocationImpl(const IRPosition &IRP, Attributor &A)
  6512. : AAMemoryLocation(IRP, A), Allocator(A.Allocator) {
  6513. for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
  6514. AccessKind2Accesses[u] = nullptr;
  6515. }
  6516. ~AAMemoryLocationImpl() {
  6517. // The AccessSets are allocated via a BumpPtrAllocator, we call
  6518. // the destructor manually.
  6519. for (unsigned u = 0; u < llvm::CTLog2<VALID_STATE>(); ++u)
  6520. if (AccessKind2Accesses[u])
  6521. AccessKind2Accesses[u]->~AccessSet();
  6522. }
  6523. /// See AbstractAttribute::initialize(...).
  6524. void initialize(Attributor &A) override {
  6525. intersectAssumedBits(BEST_STATE);
  6526. getKnownStateFromValue(A, getIRPosition(), getState());
  6527. AAMemoryLocation::initialize(A);
  6528. }
  6529. /// Return the memory behavior information encoded in the IR for \p IRP.
  6530. static void getKnownStateFromValue(Attributor &A, const IRPosition &IRP,
  6531. BitIntegerState &State,
  6532. bool IgnoreSubsumingPositions = false) {
  6533. // For internal functions we ignore `argmemonly` and
  6534. // `inaccessiblememorargmemonly` as we might break it via interprocedural
  6535. // constant propagation. It is unclear if this is the best way but it is
  6536. // unlikely this will cause real performance problems. If we are deriving
  6537. // attributes for the anchor function we even remove the attribute in
  6538. // addition to ignoring it.
  6539. bool UseArgMemOnly = true;
  6540. Function *AnchorFn = IRP.getAnchorScope();
  6541. if (AnchorFn && A.isRunOn(*AnchorFn))
  6542. UseArgMemOnly = !AnchorFn->hasLocalLinkage();
  6543. SmallVector<Attribute, 2> Attrs;
  6544. IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
  6545. for (const Attribute &Attr : Attrs) {
  6546. switch (Attr.getKindAsEnum()) {
  6547. case Attribute::ReadNone:
  6548. State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
  6549. break;
  6550. case Attribute::InaccessibleMemOnly:
  6551. State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
  6552. break;
  6553. case Attribute::ArgMemOnly:
  6554. if (UseArgMemOnly)
  6555. State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
  6556. else
  6557. IRP.removeAttrs({Attribute::ArgMemOnly});
  6558. break;
  6559. case Attribute::InaccessibleMemOrArgMemOnly:
  6560. if (UseArgMemOnly)
  6561. State.addKnownBits(inverseLocation(
  6562. NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
  6563. else
  6564. IRP.removeAttrs({Attribute::InaccessibleMemOrArgMemOnly});
  6565. break;
  6566. default:
  6567. llvm_unreachable("Unexpected attribute!");
  6568. }
  6569. }
  6570. }
  6571. /// See AbstractAttribute::getDeducedAttributes(...).
  6572. void getDeducedAttributes(LLVMContext &Ctx,
  6573. SmallVectorImpl<Attribute> &Attrs) const override {
  6574. assert(Attrs.size() == 0);
  6575. if (isAssumedReadNone()) {
  6576. Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
  6577. } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
  6578. if (isAssumedInaccessibleMemOnly())
  6579. Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
  6580. else if (isAssumedArgMemOnly())
  6581. Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
  6582. else if (isAssumedInaccessibleOrArgMemOnly())
  6583. Attrs.push_back(
  6584. Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
  6585. }
  6586. assert(Attrs.size() <= 1);
  6587. }
  6588. /// See AbstractAttribute::manifest(...).
  6589. ChangeStatus manifest(Attributor &A) override {
  6590. const IRPosition &IRP = getIRPosition();
  6591. // Check if we would improve the existing attributes first.
  6592. SmallVector<Attribute, 4> DeducedAttrs;
  6593. getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
  6594. if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
  6595. return IRP.hasAttr(Attr.getKindAsEnum(),
  6596. /* IgnoreSubsumingPositions */ true);
  6597. }))
  6598. return ChangeStatus::UNCHANGED;
  6599. // Clear existing attributes.
  6600. IRP.removeAttrs(AttrKinds);
  6601. if (isAssumedReadNone())
  6602. IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
  6603. // Use the generic manifest method.
  6604. return IRAttribute::manifest(A);
  6605. }
  6606. /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
  6607. bool checkForAllAccessesToMemoryKind(
  6608. function_ref<bool(const Instruction *, const Value *, AccessKind,
  6609. MemoryLocationsKind)>
  6610. Pred,
  6611. MemoryLocationsKind RequestedMLK) const override {
  6612. if (!isValidState())
  6613. return false;
  6614. MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
  6615. if (AssumedMLK == NO_LOCATIONS)
  6616. return true;
  6617. unsigned Idx = 0;
  6618. for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS;
  6619. CurMLK *= 2, ++Idx) {
  6620. if (CurMLK & RequestedMLK)
  6621. continue;
  6622. if (const AccessSet *Accesses = AccessKind2Accesses[Idx])
  6623. for (const AccessInfo &AI : *Accesses)
  6624. if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
  6625. return false;
  6626. }
  6627. return true;
  6628. }
  6629. ChangeStatus indicatePessimisticFixpoint() override {
  6630. // If we give up and indicate a pessimistic fixpoint this instruction will
  6631. // become an access for all potential access kinds:
  6632. // TODO: Add pointers for argmemonly and globals to improve the results of
  6633. // checkForAllAccessesToMemoryKind.
  6634. bool Changed = false;
  6635. MemoryLocationsKind KnownMLK = getKnown();
  6636. Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
  6637. for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
  6638. if (!(CurMLK & KnownMLK))
  6639. updateStateAndAccessesMap(getState(), CurMLK, I, nullptr, Changed,
  6640. getAccessKindFromInst(I));
  6641. return AAMemoryLocation::indicatePessimisticFixpoint();
  6642. }
  6643. protected:
  6644. /// Helper struct to tie together an instruction that has a read or write
  6645. /// effect with the pointer it accesses (if any).
  6646. struct AccessInfo {
  6647. /// The instruction that caused the access.
  6648. const Instruction *I;
  6649. /// The base pointer that is accessed, or null if unknown.
  6650. const Value *Ptr;
  6651. /// The kind of access (read/write/read+write).
  6652. AccessKind Kind;
  6653. bool operator==(const AccessInfo &RHS) const {
  6654. return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
  6655. }
  6656. bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
  6657. if (LHS.I != RHS.I)
  6658. return LHS.I < RHS.I;
  6659. if (LHS.Ptr != RHS.Ptr)
  6660. return LHS.Ptr < RHS.Ptr;
  6661. if (LHS.Kind != RHS.Kind)
  6662. return LHS.Kind < RHS.Kind;
  6663. return false;
  6664. }
  6665. };
  6666. /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
  6667. /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
  6668. using AccessSet = SmallSet<AccessInfo, 2, AccessInfo>;
  6669. AccessSet *AccessKind2Accesses[llvm::CTLog2<VALID_STATE>()];
  6670. /// Categorize the pointer arguments of CB that might access memory in
  6671. /// AccessedLoc and update the state and access map accordingly.
  6672. void
  6673. categorizeArgumentPointerLocations(Attributor &A, CallBase &CB,
  6674. AAMemoryLocation::StateType &AccessedLocs,
  6675. bool &Changed);
  6676. /// Return the kind(s) of location that may be accessed by \p V.
  6677. AAMemoryLocation::MemoryLocationsKind
  6678. categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
  6679. /// Return the access kind as determined by \p I.
  6680. AccessKind getAccessKindFromInst(const Instruction *I) {
  6681. AccessKind AK = READ_WRITE;
  6682. if (I) {
  6683. AK = I->mayReadFromMemory() ? READ : NONE;
  6684. AK = AccessKind(AK | (I->mayWriteToMemory() ? WRITE : NONE));
  6685. }
  6686. return AK;
  6687. }
  6688. /// Update the state \p State and the AccessKind2Accesses given that \p I is
  6689. /// an access of kind \p AK to a \p MLK memory location with the access
  6690. /// pointer \p Ptr.
  6691. void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
  6692. MemoryLocationsKind MLK, const Instruction *I,
  6693. const Value *Ptr, bool &Changed,
  6694. AccessKind AK = READ_WRITE) {
  6695. assert(isPowerOf2_32(MLK) && "Expected a single location set!");
  6696. auto *&Accesses = AccessKind2Accesses[llvm::Log2_32(MLK)];
  6697. if (!Accesses)
  6698. Accesses = new (Allocator) AccessSet();
  6699. Changed |= Accesses->insert(AccessInfo{I, Ptr, AK}).second;
  6700. State.removeAssumedBits(MLK);
  6701. }
  6702. /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
  6703. /// arguments, and update the state and access map accordingly.
  6704. void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
  6705. AAMemoryLocation::StateType &State, bool &Changed);
  6706. /// Used to allocate access sets.
  6707. BumpPtrAllocator &Allocator;
  6708. /// The set of IR attributes AAMemoryLocation deals with.
  6709. static const Attribute::AttrKind AttrKinds[4];
  6710. };
  6711. const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
  6712. Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
  6713. Attribute::InaccessibleMemOrArgMemOnly};
  6714. void AAMemoryLocationImpl::categorizePtrValue(
  6715. Attributor &A, const Instruction &I, const Value &Ptr,
  6716. AAMemoryLocation::StateType &State, bool &Changed) {
  6717. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
  6718. << Ptr << " ["
  6719. << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
  6720. SmallVector<Value *, 8> Objects;
  6721. bool UsedAssumedInformation = false;
  6722. if (!AA::getAssumedUnderlyingObjects(A, Ptr, Objects, *this, &I,
  6723. UsedAssumedInformation,
  6724. /* Intraprocedural */ true)) {
  6725. LLVM_DEBUG(
  6726. dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
  6727. updateStateAndAccessesMap(State, NO_UNKOWN_MEM, &I, nullptr, Changed,
  6728. getAccessKindFromInst(&I));
  6729. return;
  6730. }
  6731. for (Value *Obj : Objects) {
  6732. // TODO: recognize the TBAA used for constant accesses.
  6733. MemoryLocationsKind MLK = NO_LOCATIONS;
  6734. if (isa<UndefValue>(Obj))
  6735. continue;
  6736. if (isa<Argument>(Obj)) {
  6737. // TODO: For now we do not treat byval arguments as local copies performed
  6738. // on the call edge, though, we should. To make that happen we need to
  6739. // teach various passes, e.g., DSE, about the copy effect of a byval. That
  6740. // would also allow us to mark functions only accessing byval arguments as
  6741. // readnone again, atguably their acceses have no effect outside of the
  6742. // function, like accesses to allocas.
  6743. MLK = NO_ARGUMENT_MEM;
  6744. } else if (auto *GV = dyn_cast<GlobalValue>(Obj)) {
  6745. // Reading constant memory is not treated as a read "effect" by the
  6746. // function attr pass so we won't neither. Constants defined by TBAA are
  6747. // similar. (We know we do not write it because it is constant.)
  6748. if (auto *GVar = dyn_cast<GlobalVariable>(GV))
  6749. if (GVar->isConstant())
  6750. continue;
  6751. if (GV->hasLocalLinkage())
  6752. MLK = NO_GLOBAL_INTERNAL_MEM;
  6753. else
  6754. MLK = NO_GLOBAL_EXTERNAL_MEM;
  6755. } else if (isa<ConstantPointerNull>(Obj) &&
  6756. !NullPointerIsDefined(getAssociatedFunction(),
  6757. Ptr.getType()->getPointerAddressSpace())) {
  6758. continue;
  6759. } else if (isa<AllocaInst>(Obj)) {
  6760. MLK = NO_LOCAL_MEM;
  6761. } else if (const auto *CB = dyn_cast<CallBase>(Obj)) {
  6762. const auto &NoAliasAA = A.getAAFor<AANoAlias>(
  6763. *this, IRPosition::callsite_returned(*CB), DepClassTy::OPTIONAL);
  6764. if (NoAliasAA.isAssumedNoAlias())
  6765. MLK = NO_MALLOCED_MEM;
  6766. else
  6767. MLK = NO_UNKOWN_MEM;
  6768. } else {
  6769. MLK = NO_UNKOWN_MEM;
  6770. }
  6771. assert(MLK != NO_LOCATIONS && "No location specified!");
  6772. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value can be categorized: "
  6773. << *Obj << " -> " << getMemoryLocationsAsStr(MLK)
  6774. << "\n");
  6775. updateStateAndAccessesMap(getState(), MLK, &I, Obj, Changed,
  6776. getAccessKindFromInst(&I));
  6777. }
  6778. LLVM_DEBUG(
  6779. dbgs() << "[AAMemoryLocation] Accessed locations with pointer locations: "
  6780. << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
  6781. }
  6782. void AAMemoryLocationImpl::categorizeArgumentPointerLocations(
  6783. Attributor &A, CallBase &CB, AAMemoryLocation::StateType &AccessedLocs,
  6784. bool &Changed) {
  6785. for (unsigned ArgNo = 0, E = CB.arg_size(); ArgNo < E; ++ArgNo) {
  6786. // Skip non-pointer arguments.
  6787. const Value *ArgOp = CB.getArgOperand(ArgNo);
  6788. if (!ArgOp->getType()->isPtrOrPtrVectorTy())
  6789. continue;
  6790. // Skip readnone arguments.
  6791. const IRPosition &ArgOpIRP = IRPosition::callsite_argument(CB, ArgNo);
  6792. const auto &ArgOpMemLocationAA =
  6793. A.getAAFor<AAMemoryBehavior>(*this, ArgOpIRP, DepClassTy::OPTIONAL);
  6794. if (ArgOpMemLocationAA.isAssumedReadNone())
  6795. continue;
  6796. // Categorize potentially accessed pointer arguments as if there was an
  6797. // access instruction with them as pointer.
  6798. categorizePtrValue(A, CB, *ArgOp, AccessedLocs, Changed);
  6799. }
  6800. }
  6801. AAMemoryLocation::MemoryLocationsKind
  6802. AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
  6803. bool &Changed) {
  6804. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
  6805. << I << "\n");
  6806. AAMemoryLocation::StateType AccessedLocs;
  6807. AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
  6808. if (auto *CB = dyn_cast<CallBase>(&I)) {
  6809. // First check if we assume any memory is access is visible.
  6810. const auto &CBMemLocationAA = A.getAAFor<AAMemoryLocation>(
  6811. *this, IRPosition::callsite_function(*CB), DepClassTy::OPTIONAL);
  6812. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
  6813. << " [" << CBMemLocationAA << "]\n");
  6814. if (CBMemLocationAA.isAssumedReadNone())
  6815. return NO_LOCATIONS;
  6816. if (CBMemLocationAA.isAssumedInaccessibleMemOnly()) {
  6817. updateStateAndAccessesMap(AccessedLocs, NO_INACCESSIBLE_MEM, &I, nullptr,
  6818. Changed, getAccessKindFromInst(&I));
  6819. return AccessedLocs.getAssumed();
  6820. }
  6821. uint32_t CBAssumedNotAccessedLocs =
  6822. CBMemLocationAA.getAssumedNotAccessedLocation();
  6823. // Set the argmemonly and global bit as we handle them separately below.
  6824. uint32_t CBAssumedNotAccessedLocsNoArgMem =
  6825. CBAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
  6826. for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
  6827. if (CBAssumedNotAccessedLocsNoArgMem & CurMLK)
  6828. continue;
  6829. updateStateAndAccessesMap(AccessedLocs, CurMLK, &I, nullptr, Changed,
  6830. getAccessKindFromInst(&I));
  6831. }
  6832. // Now handle global memory if it might be accessed. This is slightly tricky
  6833. // as NO_GLOBAL_MEM has multiple bits set.
  6834. bool HasGlobalAccesses = ((~CBAssumedNotAccessedLocs) & NO_GLOBAL_MEM);
  6835. if (HasGlobalAccesses) {
  6836. auto AccessPred = [&](const Instruction *, const Value *Ptr,
  6837. AccessKind Kind, MemoryLocationsKind MLK) {
  6838. updateStateAndAccessesMap(AccessedLocs, MLK, &I, Ptr, Changed,
  6839. getAccessKindFromInst(&I));
  6840. return true;
  6841. };
  6842. if (!CBMemLocationAA.checkForAllAccessesToMemoryKind(
  6843. AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
  6844. return AccessedLocs.getWorstState();
  6845. }
  6846. LLVM_DEBUG(
  6847. dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
  6848. << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
  6849. // Now handle argument memory if it might be accessed.
  6850. bool HasArgAccesses = ((~CBAssumedNotAccessedLocs) & NO_ARGUMENT_MEM);
  6851. if (HasArgAccesses)
  6852. categorizeArgumentPointerLocations(A, *CB, AccessedLocs, Changed);
  6853. LLVM_DEBUG(
  6854. dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
  6855. << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
  6856. return AccessedLocs.getAssumed();
  6857. }
  6858. if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
  6859. LLVM_DEBUG(
  6860. dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
  6861. << I << " [" << *Ptr << "]\n");
  6862. categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
  6863. return AccessedLocs.getAssumed();
  6864. }
  6865. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
  6866. << I << "\n");
  6867. updateStateAndAccessesMap(AccessedLocs, NO_UNKOWN_MEM, &I, nullptr, Changed,
  6868. getAccessKindFromInst(&I));
  6869. return AccessedLocs.getAssumed();
  6870. }
  6871. /// An AA to represent the memory behavior function attributes.
  6872. struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
  6873. AAMemoryLocationFunction(const IRPosition &IRP, Attributor &A)
  6874. : AAMemoryLocationImpl(IRP, A) {}
  6875. /// See AbstractAttribute::updateImpl(Attributor &A).
  6876. virtual ChangeStatus updateImpl(Attributor &A) override {
  6877. const auto &MemBehaviorAA =
  6878. A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(), DepClassTy::NONE);
  6879. if (MemBehaviorAA.isAssumedReadNone()) {
  6880. if (MemBehaviorAA.isKnownReadNone())
  6881. return indicateOptimisticFixpoint();
  6882. assert(isAssumedReadNone() &&
  6883. "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
  6884. A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
  6885. return ChangeStatus::UNCHANGED;
  6886. }
  6887. // The current assumed state used to determine a change.
  6888. auto AssumedState = getAssumed();
  6889. bool Changed = false;
  6890. auto CheckRWInst = [&](Instruction &I) {
  6891. MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
  6892. LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
  6893. << ": " << getMemoryLocationsAsStr(MLK) << "\n");
  6894. removeAssumedBits(inverseLocation(MLK, false, false));
  6895. // Stop once only the valid bit set in the *not assumed location*, thus
  6896. // once we don't actually exclude any memory locations in the state.
  6897. return getAssumedNotAccessedLocation() != VALID_STATE;
  6898. };
  6899. bool UsedAssumedInformation = false;
  6900. if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this,
  6901. UsedAssumedInformation))
  6902. return indicatePessimisticFixpoint();
  6903. Changed |= AssumedState != getAssumed();
  6904. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  6905. }
  6906. /// See AbstractAttribute::trackStatistics()
  6907. void trackStatistics() const override {
  6908. if (isAssumedReadNone())
  6909. STATS_DECLTRACK_FN_ATTR(readnone)
  6910. else if (isAssumedArgMemOnly())
  6911. STATS_DECLTRACK_FN_ATTR(argmemonly)
  6912. else if (isAssumedInaccessibleMemOnly())
  6913. STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
  6914. else if (isAssumedInaccessibleOrArgMemOnly())
  6915. STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
  6916. }
  6917. };
  6918. /// AAMemoryLocation attribute for call sites.
  6919. struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
  6920. AAMemoryLocationCallSite(const IRPosition &IRP, Attributor &A)
  6921. : AAMemoryLocationImpl(IRP, A) {}
  6922. /// See AbstractAttribute::initialize(...).
  6923. void initialize(Attributor &A) override {
  6924. AAMemoryLocationImpl::initialize(A);
  6925. Function *F = getAssociatedFunction();
  6926. if (!F || F->isDeclaration())
  6927. indicatePessimisticFixpoint();
  6928. }
  6929. /// See AbstractAttribute::updateImpl(...).
  6930. ChangeStatus updateImpl(Attributor &A) override {
  6931. // TODO: Once we have call site specific value information we can provide
  6932. // call site specific liveness liveness information and then it makes
  6933. // sense to specialize attributes for call sites arguments instead of
  6934. // redirecting requests to the callee argument.
  6935. Function *F = getAssociatedFunction();
  6936. const IRPosition &FnPos = IRPosition::function(*F);
  6937. auto &FnAA =
  6938. A.getAAFor<AAMemoryLocation>(*this, FnPos, DepClassTy::REQUIRED);
  6939. bool Changed = false;
  6940. auto AccessPred = [&](const Instruction *I, const Value *Ptr,
  6941. AccessKind Kind, MemoryLocationsKind MLK) {
  6942. updateStateAndAccessesMap(getState(), MLK, I, Ptr, Changed,
  6943. getAccessKindFromInst(I));
  6944. return true;
  6945. };
  6946. if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
  6947. return indicatePessimisticFixpoint();
  6948. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  6949. }
  6950. /// See AbstractAttribute::trackStatistics()
  6951. void trackStatistics() const override {
  6952. if (isAssumedReadNone())
  6953. STATS_DECLTRACK_CS_ATTR(readnone)
  6954. }
  6955. };
  6956. /// ------------------ Value Constant Range Attribute -------------------------
  6957. struct AAValueConstantRangeImpl : AAValueConstantRange {
  6958. using StateType = IntegerRangeState;
  6959. AAValueConstantRangeImpl(const IRPosition &IRP, Attributor &A)
  6960. : AAValueConstantRange(IRP, A) {}
  6961. /// See AbstractAttribute::initialize(..).
  6962. void initialize(Attributor &A) override {
  6963. if (A.hasSimplificationCallback(getIRPosition())) {
  6964. indicatePessimisticFixpoint();
  6965. return;
  6966. }
  6967. // Intersect a range given by SCEV.
  6968. intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
  6969. // Intersect a range given by LVI.
  6970. intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
  6971. }
  6972. /// See AbstractAttribute::getAsStr().
  6973. const std::string getAsStr() const override {
  6974. std::string Str;
  6975. llvm::raw_string_ostream OS(Str);
  6976. OS << "range(" << getBitWidth() << ")<";
  6977. getKnown().print(OS);
  6978. OS << " / ";
  6979. getAssumed().print(OS);
  6980. OS << ">";
  6981. return OS.str();
  6982. }
  6983. /// Helper function to get a SCEV expr for the associated value at program
  6984. /// point \p I.
  6985. const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
  6986. if (!getAnchorScope())
  6987. return nullptr;
  6988. ScalarEvolution *SE =
  6989. A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
  6990. *getAnchorScope());
  6991. LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
  6992. *getAnchorScope());
  6993. if (!SE || !LI)
  6994. return nullptr;
  6995. const SCEV *S = SE->getSCEV(&getAssociatedValue());
  6996. if (!I)
  6997. return S;
  6998. return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
  6999. }
  7000. /// Helper function to get a range from SCEV for the associated value at
  7001. /// program point \p I.
  7002. ConstantRange getConstantRangeFromSCEV(Attributor &A,
  7003. const Instruction *I = nullptr) const {
  7004. if (!getAnchorScope())
  7005. return getWorstState(getBitWidth());
  7006. ScalarEvolution *SE =
  7007. A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
  7008. *getAnchorScope());
  7009. const SCEV *S = getSCEV(A, I);
  7010. if (!SE || !S)
  7011. return getWorstState(getBitWidth());
  7012. return SE->getUnsignedRange(S);
  7013. }
  7014. /// Helper function to get a range from LVI for the associated value at
  7015. /// program point \p I.
  7016. ConstantRange
  7017. getConstantRangeFromLVI(Attributor &A,
  7018. const Instruction *CtxI = nullptr) const {
  7019. if (!getAnchorScope())
  7020. return getWorstState(getBitWidth());
  7021. LazyValueInfo *LVI =
  7022. A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
  7023. *getAnchorScope());
  7024. if (!LVI || !CtxI)
  7025. return getWorstState(getBitWidth());
  7026. return LVI->getConstantRange(&getAssociatedValue(),
  7027. const_cast<Instruction *>(CtxI));
  7028. }
  7029. /// Return true if \p CtxI is valid for querying outside analyses.
  7030. /// This basically makes sure we do not ask intra-procedural analysis
  7031. /// about a context in the wrong function or a context that violates
  7032. /// dominance assumptions they might have. The \p AllowAACtxI flag indicates
  7033. /// if the original context of this AA is OK or should be considered invalid.
  7034. bool isValidCtxInstructionForOutsideAnalysis(Attributor &A,
  7035. const Instruction *CtxI,
  7036. bool AllowAACtxI) const {
  7037. if (!CtxI || (!AllowAACtxI && CtxI == getCtxI()))
  7038. return false;
  7039. // Our context might be in a different function, neither intra-procedural
  7040. // analysis (ScalarEvolution nor LazyValueInfo) can handle that.
  7041. if (!AA::isValidInScope(getAssociatedValue(), CtxI->getFunction()))
  7042. return false;
  7043. // If the context is not dominated by the value there are paths to the
  7044. // context that do not define the value. This cannot be handled by
  7045. // LazyValueInfo so we need to bail.
  7046. if (auto *I = dyn_cast<Instruction>(&getAssociatedValue())) {
  7047. InformationCache &InfoCache = A.getInfoCache();
  7048. const DominatorTree *DT =
  7049. InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(
  7050. *I->getFunction());
  7051. return DT && DT->dominates(I, CtxI);
  7052. }
  7053. return true;
  7054. }
  7055. /// See AAValueConstantRange::getKnownConstantRange(..).
  7056. ConstantRange
  7057. getKnownConstantRange(Attributor &A,
  7058. const Instruction *CtxI = nullptr) const override {
  7059. if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
  7060. /* AllowAACtxI */ false))
  7061. return getKnown();
  7062. ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
  7063. ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
  7064. return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
  7065. }
  7066. /// See AAValueConstantRange::getAssumedConstantRange(..).
  7067. ConstantRange
  7068. getAssumedConstantRange(Attributor &A,
  7069. const Instruction *CtxI = nullptr) const override {
  7070. // TODO: Make SCEV use Attributor assumption.
  7071. // We may be able to bound a variable range via assumptions in
  7072. // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
  7073. // evolve to x^2 + x, then we can say that y is in [2, 12].
  7074. if (!isValidCtxInstructionForOutsideAnalysis(A, CtxI,
  7075. /* AllowAACtxI */ false))
  7076. return getAssumed();
  7077. ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
  7078. ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
  7079. return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
  7080. }
  7081. /// Helper function to create MDNode for range metadata.
  7082. static MDNode *
  7083. getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
  7084. const ConstantRange &AssumedConstantRange) {
  7085. Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
  7086. Ty, AssumedConstantRange.getLower())),
  7087. ConstantAsMetadata::get(ConstantInt::get(
  7088. Ty, AssumedConstantRange.getUpper()))};
  7089. return MDNode::get(Ctx, LowAndHigh);
  7090. }
  7091. /// Return true if \p Assumed is included in \p KnownRanges.
  7092. static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
  7093. if (Assumed.isFullSet())
  7094. return false;
  7095. if (!KnownRanges)
  7096. return true;
  7097. // If multiple ranges are annotated in IR, we give up to annotate assumed
  7098. // range for now.
  7099. // TODO: If there exists a known range which containts assumed range, we
  7100. // can say assumed range is better.
  7101. if (KnownRanges->getNumOperands() > 2)
  7102. return false;
  7103. ConstantInt *Lower =
  7104. mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
  7105. ConstantInt *Upper =
  7106. mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
  7107. ConstantRange Known(Lower->getValue(), Upper->getValue());
  7108. return Known.contains(Assumed) && Known != Assumed;
  7109. }
  7110. /// Helper function to set range metadata.
  7111. static bool
  7112. setRangeMetadataIfisBetterRange(Instruction *I,
  7113. const ConstantRange &AssumedConstantRange) {
  7114. auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
  7115. if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
  7116. if (!AssumedConstantRange.isEmptySet()) {
  7117. I->setMetadata(LLVMContext::MD_range,
  7118. getMDNodeForConstantRange(I->getType(), I->getContext(),
  7119. AssumedConstantRange));
  7120. return true;
  7121. }
  7122. }
  7123. return false;
  7124. }
  7125. /// See AbstractAttribute::manifest()
  7126. ChangeStatus manifest(Attributor &A) override {
  7127. ChangeStatus Changed = ChangeStatus::UNCHANGED;
  7128. ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
  7129. assert(!AssumedConstantRange.isFullSet() && "Invalid state");
  7130. auto &V = getAssociatedValue();
  7131. if (!AssumedConstantRange.isEmptySet() &&
  7132. !AssumedConstantRange.isSingleElement()) {
  7133. if (Instruction *I = dyn_cast<Instruction>(&V)) {
  7134. assert(I == getCtxI() && "Should not annotate an instruction which is "
  7135. "not the context instruction");
  7136. if (isa<CallInst>(I) || isa<LoadInst>(I))
  7137. if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
  7138. Changed = ChangeStatus::CHANGED;
  7139. }
  7140. }
  7141. return Changed;
  7142. }
  7143. };
  7144. struct AAValueConstantRangeArgument final
  7145. : AAArgumentFromCallSiteArguments<
  7146. AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
  7147. true /* BridgeCallBaseContext */> {
  7148. using Base = AAArgumentFromCallSiteArguments<
  7149. AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState,
  7150. true /* BridgeCallBaseContext */>;
  7151. AAValueConstantRangeArgument(const IRPosition &IRP, Attributor &A)
  7152. : Base(IRP, A) {}
  7153. /// See AbstractAttribute::initialize(..).
  7154. void initialize(Attributor &A) override {
  7155. if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
  7156. indicatePessimisticFixpoint();
  7157. } else {
  7158. Base::initialize(A);
  7159. }
  7160. }
  7161. /// See AbstractAttribute::trackStatistics()
  7162. void trackStatistics() const override {
  7163. STATS_DECLTRACK_ARG_ATTR(value_range)
  7164. }
  7165. };
  7166. struct AAValueConstantRangeReturned
  7167. : AAReturnedFromReturnedValues<AAValueConstantRange,
  7168. AAValueConstantRangeImpl,
  7169. AAValueConstantRangeImpl::StateType,
  7170. /* PropogateCallBaseContext */ true> {
  7171. using Base =
  7172. AAReturnedFromReturnedValues<AAValueConstantRange,
  7173. AAValueConstantRangeImpl,
  7174. AAValueConstantRangeImpl::StateType,
  7175. /* PropogateCallBaseContext */ true>;
  7176. AAValueConstantRangeReturned(const IRPosition &IRP, Attributor &A)
  7177. : Base(IRP, A) {}
  7178. /// See AbstractAttribute::initialize(...).
  7179. void initialize(Attributor &A) override {}
  7180. /// See AbstractAttribute::trackStatistics()
  7181. void trackStatistics() const override {
  7182. STATS_DECLTRACK_FNRET_ATTR(value_range)
  7183. }
  7184. };
  7185. struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
  7186. AAValueConstantRangeFloating(const IRPosition &IRP, Attributor &A)
  7187. : AAValueConstantRangeImpl(IRP, A) {}
  7188. /// See AbstractAttribute::initialize(...).
  7189. void initialize(Attributor &A) override {
  7190. AAValueConstantRangeImpl::initialize(A);
  7191. if (isAtFixpoint())
  7192. return;
  7193. Value &V = getAssociatedValue();
  7194. if (auto *C = dyn_cast<ConstantInt>(&V)) {
  7195. unionAssumed(ConstantRange(C->getValue()));
  7196. indicateOptimisticFixpoint();
  7197. return;
  7198. }
  7199. if (isa<UndefValue>(&V)) {
  7200. // Collapse the undef state to 0.
  7201. unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
  7202. indicateOptimisticFixpoint();
  7203. return;
  7204. }
  7205. if (isa<CallBase>(&V))
  7206. return;
  7207. if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
  7208. return;
  7209. // If it is a load instruction with range metadata, use it.
  7210. if (LoadInst *LI = dyn_cast<LoadInst>(&V))
  7211. if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
  7212. intersectKnown(getConstantRangeFromMetadata(*RangeMD));
  7213. return;
  7214. }
  7215. // We can work with PHI and select instruction as we traverse their operands
  7216. // during update.
  7217. if (isa<SelectInst>(V) || isa<PHINode>(V))
  7218. return;
  7219. // Otherwise we give up.
  7220. indicatePessimisticFixpoint();
  7221. LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
  7222. << getAssociatedValue() << "\n");
  7223. }
  7224. bool calculateBinaryOperator(
  7225. Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
  7226. const Instruction *CtxI,
  7227. SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
  7228. Value *LHS = BinOp->getOperand(0);
  7229. Value *RHS = BinOp->getOperand(1);
  7230. // Simplify the operands first.
  7231. bool UsedAssumedInformation = false;
  7232. const auto &SimplifiedLHS =
  7233. A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
  7234. *this, UsedAssumedInformation);
  7235. if (!SimplifiedLHS.hasValue())
  7236. return true;
  7237. if (!SimplifiedLHS.getValue())
  7238. return false;
  7239. LHS = *SimplifiedLHS;
  7240. const auto &SimplifiedRHS =
  7241. A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
  7242. *this, UsedAssumedInformation);
  7243. if (!SimplifiedRHS.hasValue())
  7244. return true;
  7245. if (!SimplifiedRHS.getValue())
  7246. return false;
  7247. RHS = *SimplifiedRHS;
  7248. // TODO: Allow non integers as well.
  7249. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7250. return false;
  7251. auto &LHSAA = A.getAAFor<AAValueConstantRange>(
  7252. *this, IRPosition::value(*LHS, getCallBaseContext()),
  7253. DepClassTy::REQUIRED);
  7254. QuerriedAAs.push_back(&LHSAA);
  7255. auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
  7256. auto &RHSAA = A.getAAFor<AAValueConstantRange>(
  7257. *this, IRPosition::value(*RHS, getCallBaseContext()),
  7258. DepClassTy::REQUIRED);
  7259. QuerriedAAs.push_back(&RHSAA);
  7260. auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
  7261. auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
  7262. T.unionAssumed(AssumedRange);
  7263. // TODO: Track a known state too.
  7264. return T.isValidState();
  7265. }
  7266. bool calculateCastInst(
  7267. Attributor &A, CastInst *CastI, IntegerRangeState &T,
  7268. const Instruction *CtxI,
  7269. SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
  7270. assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
  7271. // TODO: Allow non integers as well.
  7272. Value *OpV = CastI->getOperand(0);
  7273. // Simplify the operand first.
  7274. bool UsedAssumedInformation = false;
  7275. const auto &SimplifiedOpV =
  7276. A.getAssumedSimplified(IRPosition::value(*OpV, getCallBaseContext()),
  7277. *this, UsedAssumedInformation);
  7278. if (!SimplifiedOpV.hasValue())
  7279. return true;
  7280. if (!SimplifiedOpV.getValue())
  7281. return false;
  7282. OpV = *SimplifiedOpV;
  7283. if (!OpV->getType()->isIntegerTy())
  7284. return false;
  7285. auto &OpAA = A.getAAFor<AAValueConstantRange>(
  7286. *this, IRPosition::value(*OpV, getCallBaseContext()),
  7287. DepClassTy::REQUIRED);
  7288. QuerriedAAs.push_back(&OpAA);
  7289. T.unionAssumed(
  7290. OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
  7291. return T.isValidState();
  7292. }
  7293. bool
  7294. calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
  7295. const Instruction *CtxI,
  7296. SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
  7297. Value *LHS = CmpI->getOperand(0);
  7298. Value *RHS = CmpI->getOperand(1);
  7299. // Simplify the operands first.
  7300. bool UsedAssumedInformation = false;
  7301. const auto &SimplifiedLHS =
  7302. A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
  7303. *this, UsedAssumedInformation);
  7304. if (!SimplifiedLHS.hasValue())
  7305. return true;
  7306. if (!SimplifiedLHS.getValue())
  7307. return false;
  7308. LHS = *SimplifiedLHS;
  7309. const auto &SimplifiedRHS =
  7310. A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
  7311. *this, UsedAssumedInformation);
  7312. if (!SimplifiedRHS.hasValue())
  7313. return true;
  7314. if (!SimplifiedRHS.getValue())
  7315. return false;
  7316. RHS = *SimplifiedRHS;
  7317. // TODO: Allow non integers as well.
  7318. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7319. return false;
  7320. auto &LHSAA = A.getAAFor<AAValueConstantRange>(
  7321. *this, IRPosition::value(*LHS, getCallBaseContext()),
  7322. DepClassTy::REQUIRED);
  7323. QuerriedAAs.push_back(&LHSAA);
  7324. auto &RHSAA = A.getAAFor<AAValueConstantRange>(
  7325. *this, IRPosition::value(*RHS, getCallBaseContext()),
  7326. DepClassTy::REQUIRED);
  7327. QuerriedAAs.push_back(&RHSAA);
  7328. auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
  7329. auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
  7330. // If one of them is empty set, we can't decide.
  7331. if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
  7332. return true;
  7333. bool MustTrue = false, MustFalse = false;
  7334. auto AllowedRegion =
  7335. ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
  7336. if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
  7337. MustFalse = true;
  7338. if (LHSAARange.icmp(CmpI->getPredicate(), RHSAARange))
  7339. MustTrue = true;
  7340. assert((!MustTrue || !MustFalse) &&
  7341. "Either MustTrue or MustFalse should be false!");
  7342. if (MustTrue)
  7343. T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
  7344. else if (MustFalse)
  7345. T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
  7346. else
  7347. T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
  7348. LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
  7349. << " " << RHSAA << "\n");
  7350. // TODO: Track a known state too.
  7351. return T.isValidState();
  7352. }
  7353. /// See AbstractAttribute::updateImpl(...).
  7354. ChangeStatus updateImpl(Attributor &A) override {
  7355. auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
  7356. IntegerRangeState &T, bool Stripped) -> bool {
  7357. Instruction *I = dyn_cast<Instruction>(&V);
  7358. if (!I || isa<CallBase>(I)) {
  7359. // Simplify the operand first.
  7360. bool UsedAssumedInformation = false;
  7361. const auto &SimplifiedOpV =
  7362. A.getAssumedSimplified(IRPosition::value(V, getCallBaseContext()),
  7363. *this, UsedAssumedInformation);
  7364. if (!SimplifiedOpV.hasValue())
  7365. return true;
  7366. if (!SimplifiedOpV.getValue())
  7367. return false;
  7368. Value *VPtr = *SimplifiedOpV;
  7369. // If the value is not instruction, we query AA to Attributor.
  7370. const auto &AA = A.getAAFor<AAValueConstantRange>(
  7371. *this, IRPosition::value(*VPtr, getCallBaseContext()),
  7372. DepClassTy::REQUIRED);
  7373. // Clamp operator is not used to utilize a program point CtxI.
  7374. T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
  7375. return T.isValidState();
  7376. }
  7377. SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
  7378. if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
  7379. if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
  7380. return false;
  7381. } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
  7382. if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
  7383. return false;
  7384. } else if (auto *CastI = dyn_cast<CastInst>(I)) {
  7385. if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
  7386. return false;
  7387. } else {
  7388. // Give up with other instructions.
  7389. // TODO: Add other instructions
  7390. T.indicatePessimisticFixpoint();
  7391. return false;
  7392. }
  7393. // Catch circular reasoning in a pessimistic way for now.
  7394. // TODO: Check how the range evolves and if we stripped anything, see also
  7395. // AADereferenceable or AAAlign for similar situations.
  7396. for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
  7397. if (QueriedAA != this)
  7398. continue;
  7399. // If we are in a stady state we do not need to worry.
  7400. if (T.getAssumed() == getState().getAssumed())
  7401. continue;
  7402. T.indicatePessimisticFixpoint();
  7403. }
  7404. return T.isValidState();
  7405. };
  7406. IntegerRangeState T(getBitWidth());
  7407. bool UsedAssumedInformation = false;
  7408. if (!genericValueTraversal<IntegerRangeState>(A, getIRPosition(), *this, T,
  7409. VisitValueCB, getCtxI(),
  7410. UsedAssumedInformation,
  7411. /* UseValueSimplify */ false))
  7412. return indicatePessimisticFixpoint();
  7413. // Ensure that long def-use chains can't cause circular reasoning either by
  7414. // introducing a cutoff below.
  7415. if (clampStateAndIndicateChange(getState(), T) == ChangeStatus::UNCHANGED)
  7416. return ChangeStatus::UNCHANGED;
  7417. if (++NumChanges > MaxNumChanges) {
  7418. LLVM_DEBUG(dbgs() << "[AAValueConstantRange] performed " << NumChanges
  7419. << " but only " << MaxNumChanges
  7420. << " are allowed to avoid cyclic reasoning.");
  7421. return indicatePessimisticFixpoint();
  7422. }
  7423. return ChangeStatus::CHANGED;
  7424. }
  7425. /// See AbstractAttribute::trackStatistics()
  7426. void trackStatistics() const override {
  7427. STATS_DECLTRACK_FLOATING_ATTR(value_range)
  7428. }
  7429. /// Tracker to bail after too many widening steps of the constant range.
  7430. int NumChanges = 0;
  7431. /// Upper bound for the number of allowed changes (=widening steps) for the
  7432. /// constant range before we give up.
  7433. static constexpr int MaxNumChanges = 5;
  7434. };
  7435. struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
  7436. AAValueConstantRangeFunction(const IRPosition &IRP, Attributor &A)
  7437. : AAValueConstantRangeImpl(IRP, A) {}
  7438. /// See AbstractAttribute::initialize(...).
  7439. ChangeStatus updateImpl(Attributor &A) override {
  7440. llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
  7441. "not be called");
  7442. }
  7443. /// See AbstractAttribute::trackStatistics()
  7444. void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
  7445. };
  7446. struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
  7447. AAValueConstantRangeCallSite(const IRPosition &IRP, Attributor &A)
  7448. : AAValueConstantRangeFunction(IRP, A) {}
  7449. /// See AbstractAttribute::trackStatistics()
  7450. void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
  7451. };
  7452. struct AAValueConstantRangeCallSiteReturned
  7453. : AACallSiteReturnedFromReturned<AAValueConstantRange,
  7454. AAValueConstantRangeImpl,
  7455. AAValueConstantRangeImpl::StateType,
  7456. /* IntroduceCallBaseContext */ true> {
  7457. AAValueConstantRangeCallSiteReturned(const IRPosition &IRP, Attributor &A)
  7458. : AACallSiteReturnedFromReturned<AAValueConstantRange,
  7459. AAValueConstantRangeImpl,
  7460. AAValueConstantRangeImpl::StateType,
  7461. /* IntroduceCallBaseContext */ true>(IRP,
  7462. A) {
  7463. }
  7464. /// See AbstractAttribute::initialize(...).
  7465. void initialize(Attributor &A) override {
  7466. // If it is a load instruction with range metadata, use the metadata.
  7467. if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
  7468. if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
  7469. intersectKnown(getConstantRangeFromMetadata(*RangeMD));
  7470. AAValueConstantRangeImpl::initialize(A);
  7471. }
  7472. /// See AbstractAttribute::trackStatistics()
  7473. void trackStatistics() const override {
  7474. STATS_DECLTRACK_CSRET_ATTR(value_range)
  7475. }
  7476. };
  7477. struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
  7478. AAValueConstantRangeCallSiteArgument(const IRPosition &IRP, Attributor &A)
  7479. : AAValueConstantRangeFloating(IRP, A) {}
  7480. /// See AbstractAttribute::manifest()
  7481. ChangeStatus manifest(Attributor &A) override {
  7482. return ChangeStatus::UNCHANGED;
  7483. }
  7484. /// See AbstractAttribute::trackStatistics()
  7485. void trackStatistics() const override {
  7486. STATS_DECLTRACK_CSARG_ATTR(value_range)
  7487. }
  7488. };
  7489. /// ------------------ Potential Values Attribute -------------------------
  7490. struct AAPotentialValuesImpl : AAPotentialValues {
  7491. using StateType = PotentialConstantIntValuesState;
  7492. AAPotentialValuesImpl(const IRPosition &IRP, Attributor &A)
  7493. : AAPotentialValues(IRP, A) {}
  7494. /// See AbstractAttribute::initialize(..).
  7495. void initialize(Attributor &A) override {
  7496. if (A.hasSimplificationCallback(getIRPosition()))
  7497. indicatePessimisticFixpoint();
  7498. else
  7499. AAPotentialValues::initialize(A);
  7500. }
  7501. /// See AbstractAttribute::getAsStr().
  7502. const std::string getAsStr() const override {
  7503. std::string Str;
  7504. llvm::raw_string_ostream OS(Str);
  7505. OS << getState();
  7506. return OS.str();
  7507. }
  7508. /// See AbstractAttribute::updateImpl(...).
  7509. ChangeStatus updateImpl(Attributor &A) override {
  7510. return indicatePessimisticFixpoint();
  7511. }
  7512. };
  7513. struct AAPotentialValuesArgument final
  7514. : AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
  7515. PotentialConstantIntValuesState> {
  7516. using Base =
  7517. AAArgumentFromCallSiteArguments<AAPotentialValues, AAPotentialValuesImpl,
  7518. PotentialConstantIntValuesState>;
  7519. AAPotentialValuesArgument(const IRPosition &IRP, Attributor &A)
  7520. : Base(IRP, A) {}
  7521. /// See AbstractAttribute::initialize(..).
  7522. void initialize(Attributor &A) override {
  7523. if (!getAnchorScope() || getAnchorScope()->isDeclaration()) {
  7524. indicatePessimisticFixpoint();
  7525. } else {
  7526. Base::initialize(A);
  7527. }
  7528. }
  7529. /// See AbstractAttribute::trackStatistics()
  7530. void trackStatistics() const override {
  7531. STATS_DECLTRACK_ARG_ATTR(potential_values)
  7532. }
  7533. };
  7534. struct AAPotentialValuesReturned
  7535. : AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl> {
  7536. using Base =
  7537. AAReturnedFromReturnedValues<AAPotentialValues, AAPotentialValuesImpl>;
  7538. AAPotentialValuesReturned(const IRPosition &IRP, Attributor &A)
  7539. : Base(IRP, A) {}
  7540. /// See AbstractAttribute::trackStatistics()
  7541. void trackStatistics() const override {
  7542. STATS_DECLTRACK_FNRET_ATTR(potential_values)
  7543. }
  7544. };
  7545. struct AAPotentialValuesFloating : AAPotentialValuesImpl {
  7546. AAPotentialValuesFloating(const IRPosition &IRP, Attributor &A)
  7547. : AAPotentialValuesImpl(IRP, A) {}
  7548. /// See AbstractAttribute::initialize(..).
  7549. void initialize(Attributor &A) override {
  7550. AAPotentialValuesImpl::initialize(A);
  7551. if (isAtFixpoint())
  7552. return;
  7553. Value &V = getAssociatedValue();
  7554. if (auto *C = dyn_cast<ConstantInt>(&V)) {
  7555. unionAssumed(C->getValue());
  7556. indicateOptimisticFixpoint();
  7557. return;
  7558. }
  7559. if (isa<UndefValue>(&V)) {
  7560. unionAssumedWithUndef();
  7561. indicateOptimisticFixpoint();
  7562. return;
  7563. }
  7564. if (isa<BinaryOperator>(&V) || isa<ICmpInst>(&V) || isa<CastInst>(&V))
  7565. return;
  7566. if (isa<SelectInst>(V) || isa<PHINode>(V) || isa<LoadInst>(V))
  7567. return;
  7568. indicatePessimisticFixpoint();
  7569. LLVM_DEBUG(dbgs() << "[AAPotentialValues] We give up: "
  7570. << getAssociatedValue() << "\n");
  7571. }
  7572. static bool calculateICmpInst(const ICmpInst *ICI, const APInt &LHS,
  7573. const APInt &RHS) {
  7574. return ICmpInst::compare(LHS, RHS, ICI->getPredicate());
  7575. }
  7576. static APInt calculateCastInst(const CastInst *CI, const APInt &Src,
  7577. uint32_t ResultBitWidth) {
  7578. Instruction::CastOps CastOp = CI->getOpcode();
  7579. switch (CastOp) {
  7580. default:
  7581. llvm_unreachable("unsupported or not integer cast");
  7582. case Instruction::Trunc:
  7583. return Src.trunc(ResultBitWidth);
  7584. case Instruction::SExt:
  7585. return Src.sext(ResultBitWidth);
  7586. case Instruction::ZExt:
  7587. return Src.zext(ResultBitWidth);
  7588. case Instruction::BitCast:
  7589. return Src;
  7590. }
  7591. }
  7592. static APInt calculateBinaryOperator(const BinaryOperator *BinOp,
  7593. const APInt &LHS, const APInt &RHS,
  7594. bool &SkipOperation, bool &Unsupported) {
  7595. Instruction::BinaryOps BinOpcode = BinOp->getOpcode();
  7596. // Unsupported is set to true when the binary operator is not supported.
  7597. // SkipOperation is set to true when UB occur with the given operand pair
  7598. // (LHS, RHS).
  7599. // TODO: we should look at nsw and nuw keywords to handle operations
  7600. // that create poison or undef value.
  7601. switch (BinOpcode) {
  7602. default:
  7603. Unsupported = true;
  7604. return LHS;
  7605. case Instruction::Add:
  7606. return LHS + RHS;
  7607. case Instruction::Sub:
  7608. return LHS - RHS;
  7609. case Instruction::Mul:
  7610. return LHS * RHS;
  7611. case Instruction::UDiv:
  7612. if (RHS.isZero()) {
  7613. SkipOperation = true;
  7614. return LHS;
  7615. }
  7616. return LHS.udiv(RHS);
  7617. case Instruction::SDiv:
  7618. if (RHS.isZero()) {
  7619. SkipOperation = true;
  7620. return LHS;
  7621. }
  7622. return LHS.sdiv(RHS);
  7623. case Instruction::URem:
  7624. if (RHS.isZero()) {
  7625. SkipOperation = true;
  7626. return LHS;
  7627. }
  7628. return LHS.urem(RHS);
  7629. case Instruction::SRem:
  7630. if (RHS.isZero()) {
  7631. SkipOperation = true;
  7632. return LHS;
  7633. }
  7634. return LHS.srem(RHS);
  7635. case Instruction::Shl:
  7636. return LHS.shl(RHS);
  7637. case Instruction::LShr:
  7638. return LHS.lshr(RHS);
  7639. case Instruction::AShr:
  7640. return LHS.ashr(RHS);
  7641. case Instruction::And:
  7642. return LHS & RHS;
  7643. case Instruction::Or:
  7644. return LHS | RHS;
  7645. case Instruction::Xor:
  7646. return LHS ^ RHS;
  7647. }
  7648. }
  7649. bool calculateBinaryOperatorAndTakeUnion(const BinaryOperator *BinOp,
  7650. const APInt &LHS, const APInt &RHS) {
  7651. bool SkipOperation = false;
  7652. bool Unsupported = false;
  7653. APInt Result =
  7654. calculateBinaryOperator(BinOp, LHS, RHS, SkipOperation, Unsupported);
  7655. if (Unsupported)
  7656. return false;
  7657. // If SkipOperation is true, we can ignore this operand pair (L, R).
  7658. if (!SkipOperation)
  7659. unionAssumed(Result);
  7660. return isValidState();
  7661. }
  7662. ChangeStatus updateWithICmpInst(Attributor &A, ICmpInst *ICI) {
  7663. auto AssumedBefore = getAssumed();
  7664. Value *LHS = ICI->getOperand(0);
  7665. Value *RHS = ICI->getOperand(1);
  7666. // Simplify the operands first.
  7667. bool UsedAssumedInformation = false;
  7668. const auto &SimplifiedLHS =
  7669. A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
  7670. *this, UsedAssumedInformation);
  7671. if (!SimplifiedLHS.hasValue())
  7672. return ChangeStatus::UNCHANGED;
  7673. if (!SimplifiedLHS.getValue())
  7674. return indicatePessimisticFixpoint();
  7675. LHS = *SimplifiedLHS;
  7676. const auto &SimplifiedRHS =
  7677. A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
  7678. *this, UsedAssumedInformation);
  7679. if (!SimplifiedRHS.hasValue())
  7680. return ChangeStatus::UNCHANGED;
  7681. if (!SimplifiedRHS.getValue())
  7682. return indicatePessimisticFixpoint();
  7683. RHS = *SimplifiedRHS;
  7684. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7685. return indicatePessimisticFixpoint();
  7686. auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
  7687. DepClassTy::REQUIRED);
  7688. if (!LHSAA.isValidState())
  7689. return indicatePessimisticFixpoint();
  7690. auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
  7691. DepClassTy::REQUIRED);
  7692. if (!RHSAA.isValidState())
  7693. return indicatePessimisticFixpoint();
  7694. const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
  7695. const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
  7696. // TODO: make use of undef flag to limit potential values aggressively.
  7697. bool MaybeTrue = false, MaybeFalse = false;
  7698. const APInt Zero(RHS->getType()->getIntegerBitWidth(), 0);
  7699. if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
  7700. // The result of any comparison between undefs can be soundly replaced
  7701. // with undef.
  7702. unionAssumedWithUndef();
  7703. } else if (LHSAA.undefIsContained()) {
  7704. for (const APInt &R : RHSAAPVS) {
  7705. bool CmpResult = calculateICmpInst(ICI, Zero, R);
  7706. MaybeTrue |= CmpResult;
  7707. MaybeFalse |= !CmpResult;
  7708. if (MaybeTrue & MaybeFalse)
  7709. return indicatePessimisticFixpoint();
  7710. }
  7711. } else if (RHSAA.undefIsContained()) {
  7712. for (const APInt &L : LHSAAPVS) {
  7713. bool CmpResult = calculateICmpInst(ICI, L, Zero);
  7714. MaybeTrue |= CmpResult;
  7715. MaybeFalse |= !CmpResult;
  7716. if (MaybeTrue & MaybeFalse)
  7717. return indicatePessimisticFixpoint();
  7718. }
  7719. } else {
  7720. for (const APInt &L : LHSAAPVS) {
  7721. for (const APInt &R : RHSAAPVS) {
  7722. bool CmpResult = calculateICmpInst(ICI, L, R);
  7723. MaybeTrue |= CmpResult;
  7724. MaybeFalse |= !CmpResult;
  7725. if (MaybeTrue & MaybeFalse)
  7726. return indicatePessimisticFixpoint();
  7727. }
  7728. }
  7729. }
  7730. if (MaybeTrue)
  7731. unionAssumed(APInt(/* numBits */ 1, /* val */ 1));
  7732. if (MaybeFalse)
  7733. unionAssumed(APInt(/* numBits */ 1, /* val */ 0));
  7734. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  7735. : ChangeStatus::CHANGED;
  7736. }
  7737. ChangeStatus updateWithSelectInst(Attributor &A, SelectInst *SI) {
  7738. auto AssumedBefore = getAssumed();
  7739. Value *LHS = SI->getTrueValue();
  7740. Value *RHS = SI->getFalseValue();
  7741. // Simplify the operands first.
  7742. bool UsedAssumedInformation = false;
  7743. const auto &SimplifiedLHS =
  7744. A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
  7745. *this, UsedAssumedInformation);
  7746. if (!SimplifiedLHS.hasValue())
  7747. return ChangeStatus::UNCHANGED;
  7748. if (!SimplifiedLHS.getValue())
  7749. return indicatePessimisticFixpoint();
  7750. LHS = *SimplifiedLHS;
  7751. const auto &SimplifiedRHS =
  7752. A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
  7753. *this, UsedAssumedInformation);
  7754. if (!SimplifiedRHS.hasValue())
  7755. return ChangeStatus::UNCHANGED;
  7756. if (!SimplifiedRHS.getValue())
  7757. return indicatePessimisticFixpoint();
  7758. RHS = *SimplifiedRHS;
  7759. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7760. return indicatePessimisticFixpoint();
  7761. Optional<Constant *> C = A.getAssumedConstant(*SI->getCondition(), *this,
  7762. UsedAssumedInformation);
  7763. // Check if we only need one operand.
  7764. bool OnlyLeft = false, OnlyRight = false;
  7765. if (C.hasValue() && *C && (*C)->isOneValue())
  7766. OnlyLeft = true;
  7767. else if (C.hasValue() && *C && (*C)->isZeroValue())
  7768. OnlyRight = true;
  7769. const AAPotentialValues *LHSAA = nullptr, *RHSAA = nullptr;
  7770. if (!OnlyRight) {
  7771. LHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
  7772. DepClassTy::REQUIRED);
  7773. if (!LHSAA->isValidState())
  7774. return indicatePessimisticFixpoint();
  7775. }
  7776. if (!OnlyLeft) {
  7777. RHSAA = &A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
  7778. DepClassTy::REQUIRED);
  7779. if (!RHSAA->isValidState())
  7780. return indicatePessimisticFixpoint();
  7781. }
  7782. if (!LHSAA || !RHSAA) {
  7783. // select (true/false), lhs, rhs
  7784. auto *OpAA = LHSAA ? LHSAA : RHSAA;
  7785. if (OpAA->undefIsContained())
  7786. unionAssumedWithUndef();
  7787. else
  7788. unionAssumed(*OpAA);
  7789. } else if (LHSAA->undefIsContained() && RHSAA->undefIsContained()) {
  7790. // select i1 *, undef , undef => undef
  7791. unionAssumedWithUndef();
  7792. } else {
  7793. unionAssumed(*LHSAA);
  7794. unionAssumed(*RHSAA);
  7795. }
  7796. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  7797. : ChangeStatus::CHANGED;
  7798. }
  7799. ChangeStatus updateWithCastInst(Attributor &A, CastInst *CI) {
  7800. auto AssumedBefore = getAssumed();
  7801. if (!CI->isIntegerCast())
  7802. return indicatePessimisticFixpoint();
  7803. assert(CI->getNumOperands() == 1 && "Expected cast to be unary!");
  7804. uint32_t ResultBitWidth = CI->getDestTy()->getIntegerBitWidth();
  7805. Value *Src = CI->getOperand(0);
  7806. // Simplify the operand first.
  7807. bool UsedAssumedInformation = false;
  7808. const auto &SimplifiedSrc =
  7809. A.getAssumedSimplified(IRPosition::value(*Src, getCallBaseContext()),
  7810. *this, UsedAssumedInformation);
  7811. if (!SimplifiedSrc.hasValue())
  7812. return ChangeStatus::UNCHANGED;
  7813. if (!SimplifiedSrc.getValue())
  7814. return indicatePessimisticFixpoint();
  7815. Src = *SimplifiedSrc;
  7816. auto &SrcAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*Src),
  7817. DepClassTy::REQUIRED);
  7818. if (!SrcAA.isValidState())
  7819. return indicatePessimisticFixpoint();
  7820. const DenseSet<APInt> &SrcAAPVS = SrcAA.getAssumedSet();
  7821. if (SrcAA.undefIsContained())
  7822. unionAssumedWithUndef();
  7823. else {
  7824. for (const APInt &S : SrcAAPVS) {
  7825. APInt T = calculateCastInst(CI, S, ResultBitWidth);
  7826. unionAssumed(T);
  7827. }
  7828. }
  7829. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  7830. : ChangeStatus::CHANGED;
  7831. }
  7832. ChangeStatus updateWithBinaryOperator(Attributor &A, BinaryOperator *BinOp) {
  7833. auto AssumedBefore = getAssumed();
  7834. Value *LHS = BinOp->getOperand(0);
  7835. Value *RHS = BinOp->getOperand(1);
  7836. // Simplify the operands first.
  7837. bool UsedAssumedInformation = false;
  7838. const auto &SimplifiedLHS =
  7839. A.getAssumedSimplified(IRPosition::value(*LHS, getCallBaseContext()),
  7840. *this, UsedAssumedInformation);
  7841. if (!SimplifiedLHS.hasValue())
  7842. return ChangeStatus::UNCHANGED;
  7843. if (!SimplifiedLHS.getValue())
  7844. return indicatePessimisticFixpoint();
  7845. LHS = *SimplifiedLHS;
  7846. const auto &SimplifiedRHS =
  7847. A.getAssumedSimplified(IRPosition::value(*RHS, getCallBaseContext()),
  7848. *this, UsedAssumedInformation);
  7849. if (!SimplifiedRHS.hasValue())
  7850. return ChangeStatus::UNCHANGED;
  7851. if (!SimplifiedRHS.getValue())
  7852. return indicatePessimisticFixpoint();
  7853. RHS = *SimplifiedRHS;
  7854. if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
  7855. return indicatePessimisticFixpoint();
  7856. auto &LHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*LHS),
  7857. DepClassTy::REQUIRED);
  7858. if (!LHSAA.isValidState())
  7859. return indicatePessimisticFixpoint();
  7860. auto &RHSAA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(*RHS),
  7861. DepClassTy::REQUIRED);
  7862. if (!RHSAA.isValidState())
  7863. return indicatePessimisticFixpoint();
  7864. const DenseSet<APInt> &LHSAAPVS = LHSAA.getAssumedSet();
  7865. const DenseSet<APInt> &RHSAAPVS = RHSAA.getAssumedSet();
  7866. const APInt Zero = APInt(LHS->getType()->getIntegerBitWidth(), 0);
  7867. // TODO: make use of undef flag to limit potential values aggressively.
  7868. if (LHSAA.undefIsContained() && RHSAA.undefIsContained()) {
  7869. if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, Zero))
  7870. return indicatePessimisticFixpoint();
  7871. } else if (LHSAA.undefIsContained()) {
  7872. for (const APInt &R : RHSAAPVS) {
  7873. if (!calculateBinaryOperatorAndTakeUnion(BinOp, Zero, R))
  7874. return indicatePessimisticFixpoint();
  7875. }
  7876. } else if (RHSAA.undefIsContained()) {
  7877. for (const APInt &L : LHSAAPVS) {
  7878. if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, Zero))
  7879. return indicatePessimisticFixpoint();
  7880. }
  7881. } else {
  7882. for (const APInt &L : LHSAAPVS) {
  7883. for (const APInt &R : RHSAAPVS) {
  7884. if (!calculateBinaryOperatorAndTakeUnion(BinOp, L, R))
  7885. return indicatePessimisticFixpoint();
  7886. }
  7887. }
  7888. }
  7889. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  7890. : ChangeStatus::CHANGED;
  7891. }
  7892. ChangeStatus updateWithPHINode(Attributor &A, PHINode *PHI) {
  7893. auto AssumedBefore = getAssumed();
  7894. for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
  7895. Value *IncomingValue = PHI->getIncomingValue(u);
  7896. // Simplify the operand first.
  7897. bool UsedAssumedInformation = false;
  7898. const auto &SimplifiedIncomingValue = A.getAssumedSimplified(
  7899. IRPosition::value(*IncomingValue, getCallBaseContext()), *this,
  7900. UsedAssumedInformation);
  7901. if (!SimplifiedIncomingValue.hasValue())
  7902. continue;
  7903. if (!SimplifiedIncomingValue.getValue())
  7904. return indicatePessimisticFixpoint();
  7905. IncomingValue = *SimplifiedIncomingValue;
  7906. auto &PotentialValuesAA = A.getAAFor<AAPotentialValues>(
  7907. *this, IRPosition::value(*IncomingValue), DepClassTy::REQUIRED);
  7908. if (!PotentialValuesAA.isValidState())
  7909. return indicatePessimisticFixpoint();
  7910. if (PotentialValuesAA.undefIsContained())
  7911. unionAssumedWithUndef();
  7912. else
  7913. unionAssumed(PotentialValuesAA.getAssumed());
  7914. }
  7915. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  7916. : ChangeStatus::CHANGED;
  7917. }
  7918. ChangeStatus updateWithLoad(Attributor &A, LoadInst &L) {
  7919. if (!L.getType()->isIntegerTy())
  7920. return indicatePessimisticFixpoint();
  7921. auto Union = [&](Value &V) {
  7922. if (isa<UndefValue>(V)) {
  7923. unionAssumedWithUndef();
  7924. return true;
  7925. }
  7926. if (ConstantInt *CI = dyn_cast<ConstantInt>(&V)) {
  7927. unionAssumed(CI->getValue());
  7928. return true;
  7929. }
  7930. return false;
  7931. };
  7932. auto AssumedBefore = getAssumed();
  7933. if (!AAValueSimplifyImpl::handleLoad(A, *this, L, Union))
  7934. return indicatePessimisticFixpoint();
  7935. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  7936. : ChangeStatus::CHANGED;
  7937. }
  7938. /// See AbstractAttribute::updateImpl(...).
  7939. ChangeStatus updateImpl(Attributor &A) override {
  7940. Value &V = getAssociatedValue();
  7941. Instruction *I = dyn_cast<Instruction>(&V);
  7942. if (auto *ICI = dyn_cast<ICmpInst>(I))
  7943. return updateWithICmpInst(A, ICI);
  7944. if (auto *SI = dyn_cast<SelectInst>(I))
  7945. return updateWithSelectInst(A, SI);
  7946. if (auto *CI = dyn_cast<CastInst>(I))
  7947. return updateWithCastInst(A, CI);
  7948. if (auto *BinOp = dyn_cast<BinaryOperator>(I))
  7949. return updateWithBinaryOperator(A, BinOp);
  7950. if (auto *PHI = dyn_cast<PHINode>(I))
  7951. return updateWithPHINode(A, PHI);
  7952. if (auto *L = dyn_cast<LoadInst>(I))
  7953. return updateWithLoad(A, *L);
  7954. return indicatePessimisticFixpoint();
  7955. }
  7956. /// See AbstractAttribute::trackStatistics()
  7957. void trackStatistics() const override {
  7958. STATS_DECLTRACK_FLOATING_ATTR(potential_values)
  7959. }
  7960. };
  7961. struct AAPotentialValuesFunction : AAPotentialValuesImpl {
  7962. AAPotentialValuesFunction(const IRPosition &IRP, Attributor &A)
  7963. : AAPotentialValuesImpl(IRP, A) {}
  7964. /// See AbstractAttribute::initialize(...).
  7965. ChangeStatus updateImpl(Attributor &A) override {
  7966. llvm_unreachable("AAPotentialValues(Function|CallSite)::updateImpl will "
  7967. "not be called");
  7968. }
  7969. /// See AbstractAttribute::trackStatistics()
  7970. void trackStatistics() const override {
  7971. STATS_DECLTRACK_FN_ATTR(potential_values)
  7972. }
  7973. };
  7974. struct AAPotentialValuesCallSite : AAPotentialValuesFunction {
  7975. AAPotentialValuesCallSite(const IRPosition &IRP, Attributor &A)
  7976. : AAPotentialValuesFunction(IRP, A) {}
  7977. /// See AbstractAttribute::trackStatistics()
  7978. void trackStatistics() const override {
  7979. STATS_DECLTRACK_CS_ATTR(potential_values)
  7980. }
  7981. };
  7982. struct AAPotentialValuesCallSiteReturned
  7983. : AACallSiteReturnedFromReturned<AAPotentialValues, AAPotentialValuesImpl> {
  7984. AAPotentialValuesCallSiteReturned(const IRPosition &IRP, Attributor &A)
  7985. : AACallSiteReturnedFromReturned<AAPotentialValues,
  7986. AAPotentialValuesImpl>(IRP, A) {}
  7987. /// See AbstractAttribute::trackStatistics()
  7988. void trackStatistics() const override {
  7989. STATS_DECLTRACK_CSRET_ATTR(potential_values)
  7990. }
  7991. };
  7992. struct AAPotentialValuesCallSiteArgument : AAPotentialValuesFloating {
  7993. AAPotentialValuesCallSiteArgument(const IRPosition &IRP, Attributor &A)
  7994. : AAPotentialValuesFloating(IRP, A) {}
  7995. /// See AbstractAttribute::initialize(..).
  7996. void initialize(Attributor &A) override {
  7997. AAPotentialValuesImpl::initialize(A);
  7998. if (isAtFixpoint())
  7999. return;
  8000. Value &V = getAssociatedValue();
  8001. if (auto *C = dyn_cast<ConstantInt>(&V)) {
  8002. unionAssumed(C->getValue());
  8003. indicateOptimisticFixpoint();
  8004. return;
  8005. }
  8006. if (isa<UndefValue>(&V)) {
  8007. unionAssumedWithUndef();
  8008. indicateOptimisticFixpoint();
  8009. return;
  8010. }
  8011. }
  8012. /// See AbstractAttribute::updateImpl(...).
  8013. ChangeStatus updateImpl(Attributor &A) override {
  8014. Value &V = getAssociatedValue();
  8015. auto AssumedBefore = getAssumed();
  8016. auto &AA = A.getAAFor<AAPotentialValues>(*this, IRPosition::value(V),
  8017. DepClassTy::REQUIRED);
  8018. const auto &S = AA.getAssumed();
  8019. unionAssumed(S);
  8020. return AssumedBefore == getAssumed() ? ChangeStatus::UNCHANGED
  8021. : ChangeStatus::CHANGED;
  8022. }
  8023. /// See AbstractAttribute::trackStatistics()
  8024. void trackStatistics() const override {
  8025. STATS_DECLTRACK_CSARG_ATTR(potential_values)
  8026. }
  8027. };
  8028. /// ------------------------ NoUndef Attribute ---------------------------------
  8029. struct AANoUndefImpl : AANoUndef {
  8030. AANoUndefImpl(const IRPosition &IRP, Attributor &A) : AANoUndef(IRP, A) {}
  8031. /// See AbstractAttribute::initialize(...).
  8032. void initialize(Attributor &A) override {
  8033. if (getIRPosition().hasAttr({Attribute::NoUndef})) {
  8034. indicateOptimisticFixpoint();
  8035. return;
  8036. }
  8037. Value &V = getAssociatedValue();
  8038. if (isa<UndefValue>(V))
  8039. indicatePessimisticFixpoint();
  8040. else if (isa<FreezeInst>(V))
  8041. indicateOptimisticFixpoint();
  8042. else if (getPositionKind() != IRPosition::IRP_RETURNED &&
  8043. isGuaranteedNotToBeUndefOrPoison(&V))
  8044. indicateOptimisticFixpoint();
  8045. else
  8046. AANoUndef::initialize(A);
  8047. }
  8048. /// See followUsesInMBEC
  8049. bool followUseInMBEC(Attributor &A, const Use *U, const Instruction *I,
  8050. AANoUndef::StateType &State) {
  8051. const Value *UseV = U->get();
  8052. const DominatorTree *DT = nullptr;
  8053. AssumptionCache *AC = nullptr;
  8054. InformationCache &InfoCache = A.getInfoCache();
  8055. if (Function *F = getAnchorScope()) {
  8056. DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*F);
  8057. AC = InfoCache.getAnalysisResultForFunction<AssumptionAnalysis>(*F);
  8058. }
  8059. State.setKnown(isGuaranteedNotToBeUndefOrPoison(UseV, AC, I, DT));
  8060. bool TrackUse = false;
  8061. // Track use for instructions which must produce undef or poison bits when
  8062. // at least one operand contains such bits.
  8063. if (isa<CastInst>(*I) || isa<GetElementPtrInst>(*I))
  8064. TrackUse = true;
  8065. return TrackUse;
  8066. }
  8067. /// See AbstractAttribute::getAsStr().
  8068. const std::string getAsStr() const override {
  8069. return getAssumed() ? "noundef" : "may-undef-or-poison";
  8070. }
  8071. ChangeStatus manifest(Attributor &A) override {
  8072. // We don't manifest noundef attribute for dead positions because the
  8073. // associated values with dead positions would be replaced with undef
  8074. // values.
  8075. bool UsedAssumedInformation = false;
  8076. if (A.isAssumedDead(getIRPosition(), nullptr, nullptr,
  8077. UsedAssumedInformation))
  8078. return ChangeStatus::UNCHANGED;
  8079. // A position whose simplified value does not have any value is
  8080. // considered to be dead. We don't manifest noundef in such positions for
  8081. // the same reason above.
  8082. if (!A.getAssumedSimplified(getIRPosition(), *this, UsedAssumedInformation)
  8083. .hasValue())
  8084. return ChangeStatus::UNCHANGED;
  8085. return AANoUndef::manifest(A);
  8086. }
  8087. };
  8088. struct AANoUndefFloating : public AANoUndefImpl {
  8089. AANoUndefFloating(const IRPosition &IRP, Attributor &A)
  8090. : AANoUndefImpl(IRP, A) {}
  8091. /// See AbstractAttribute::initialize(...).
  8092. void initialize(Attributor &A) override {
  8093. AANoUndefImpl::initialize(A);
  8094. if (!getState().isAtFixpoint())
  8095. if (Instruction *CtxI = getCtxI())
  8096. followUsesInMBEC(*this, A, getState(), *CtxI);
  8097. }
  8098. /// See AbstractAttribute::updateImpl(...).
  8099. ChangeStatus updateImpl(Attributor &A) override {
  8100. auto VisitValueCB = [&](Value &V, const Instruction *CtxI,
  8101. AANoUndef::StateType &T, bool Stripped) -> bool {
  8102. const auto &AA = A.getAAFor<AANoUndef>(*this, IRPosition::value(V),
  8103. DepClassTy::REQUIRED);
  8104. if (!Stripped && this == &AA) {
  8105. T.indicatePessimisticFixpoint();
  8106. } else {
  8107. const AANoUndef::StateType &S =
  8108. static_cast<const AANoUndef::StateType &>(AA.getState());
  8109. T ^= S;
  8110. }
  8111. return T.isValidState();
  8112. };
  8113. StateType T;
  8114. bool UsedAssumedInformation = false;
  8115. if (!genericValueTraversal<StateType>(A, getIRPosition(), *this, T,
  8116. VisitValueCB, getCtxI(),
  8117. UsedAssumedInformation))
  8118. return indicatePessimisticFixpoint();
  8119. return clampStateAndIndicateChange(getState(), T);
  8120. }
  8121. /// See AbstractAttribute::trackStatistics()
  8122. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
  8123. };
  8124. struct AANoUndefReturned final
  8125. : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl> {
  8126. AANoUndefReturned(const IRPosition &IRP, Attributor &A)
  8127. : AAReturnedFromReturnedValues<AANoUndef, AANoUndefImpl>(IRP, A) {}
  8128. /// See AbstractAttribute::trackStatistics()
  8129. void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noundef) }
  8130. };
  8131. struct AANoUndefArgument final
  8132. : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl> {
  8133. AANoUndefArgument(const IRPosition &IRP, Attributor &A)
  8134. : AAArgumentFromCallSiteArguments<AANoUndef, AANoUndefImpl>(IRP, A) {}
  8135. /// See AbstractAttribute::trackStatistics()
  8136. void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noundef) }
  8137. };
  8138. struct AANoUndefCallSiteArgument final : AANoUndefFloating {
  8139. AANoUndefCallSiteArgument(const IRPosition &IRP, Attributor &A)
  8140. : AANoUndefFloating(IRP, A) {}
  8141. /// See AbstractAttribute::trackStatistics()
  8142. void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noundef) }
  8143. };
  8144. struct AANoUndefCallSiteReturned final
  8145. : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl> {
  8146. AANoUndefCallSiteReturned(const IRPosition &IRP, Attributor &A)
  8147. : AACallSiteReturnedFromReturned<AANoUndef, AANoUndefImpl>(IRP, A) {}
  8148. /// See AbstractAttribute::trackStatistics()
  8149. void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noundef) }
  8150. };
  8151. struct AACallEdgesImpl : public AACallEdges {
  8152. AACallEdgesImpl(const IRPosition &IRP, Attributor &A) : AACallEdges(IRP, A) {}
  8153. virtual const SetVector<Function *> &getOptimisticEdges() const override {
  8154. return CalledFunctions;
  8155. }
  8156. virtual bool hasUnknownCallee() const override { return HasUnknownCallee; }
  8157. virtual bool hasNonAsmUnknownCallee() const override {
  8158. return HasUnknownCalleeNonAsm;
  8159. }
  8160. const std::string getAsStr() const override {
  8161. return "CallEdges[" + std::to_string(HasUnknownCallee) + "," +
  8162. std::to_string(CalledFunctions.size()) + "]";
  8163. }
  8164. void trackStatistics() const override {}
  8165. protected:
  8166. void addCalledFunction(Function *Fn, ChangeStatus &Change) {
  8167. if (CalledFunctions.insert(Fn)) {
  8168. Change = ChangeStatus::CHANGED;
  8169. LLVM_DEBUG(dbgs() << "[AACallEdges] New call edge: " << Fn->getName()
  8170. << "\n");
  8171. }
  8172. }
  8173. void setHasUnknownCallee(bool NonAsm, ChangeStatus &Change) {
  8174. if (!HasUnknownCallee)
  8175. Change = ChangeStatus::CHANGED;
  8176. if (NonAsm && !HasUnknownCalleeNonAsm)
  8177. Change = ChangeStatus::CHANGED;
  8178. HasUnknownCalleeNonAsm |= NonAsm;
  8179. HasUnknownCallee = true;
  8180. }
  8181. private:
  8182. /// Optimistic set of functions that might be called by this position.
  8183. SetVector<Function *> CalledFunctions;
  8184. /// Is there any call with a unknown callee.
  8185. bool HasUnknownCallee = false;
  8186. /// Is there any call with a unknown callee, excluding any inline asm.
  8187. bool HasUnknownCalleeNonAsm = false;
  8188. };
  8189. struct AACallEdgesCallSite : public AACallEdgesImpl {
  8190. AACallEdgesCallSite(const IRPosition &IRP, Attributor &A)
  8191. : AACallEdgesImpl(IRP, A) {}
  8192. /// See AbstractAttribute::updateImpl(...).
  8193. ChangeStatus updateImpl(Attributor &A) override {
  8194. ChangeStatus Change = ChangeStatus::UNCHANGED;
  8195. auto VisitValue = [&](Value &V, const Instruction *CtxI, bool &HasUnknown,
  8196. bool Stripped) -> bool {
  8197. if (Function *Fn = dyn_cast<Function>(&V)) {
  8198. addCalledFunction(Fn, Change);
  8199. } else {
  8200. LLVM_DEBUG(dbgs() << "[AACallEdges] Unrecognized value: " << V << "\n");
  8201. setHasUnknownCallee(true, Change);
  8202. }
  8203. // Explore all values.
  8204. return true;
  8205. };
  8206. // Process any value that we might call.
  8207. auto ProcessCalledOperand = [&](Value *V) {
  8208. bool DummyValue = false;
  8209. bool UsedAssumedInformation = false;
  8210. if (!genericValueTraversal<bool>(A, IRPosition::value(*V), *this,
  8211. DummyValue, VisitValue, nullptr,
  8212. UsedAssumedInformation, false)) {
  8213. // If we haven't gone through all values, assume that there are unknown
  8214. // callees.
  8215. setHasUnknownCallee(true, Change);
  8216. }
  8217. };
  8218. CallBase *CB = cast<CallBase>(getCtxI());
  8219. if (CB->isInlineAsm()) {
  8220. setHasUnknownCallee(false, Change);
  8221. return Change;
  8222. }
  8223. // Process callee metadata if available.
  8224. if (auto *MD = getCtxI()->getMetadata(LLVMContext::MD_callees)) {
  8225. for (auto &Op : MD->operands()) {
  8226. Function *Callee = mdconst::dyn_extract_or_null<Function>(Op);
  8227. if (Callee)
  8228. addCalledFunction(Callee, Change);
  8229. }
  8230. return Change;
  8231. }
  8232. // The most simple case.
  8233. ProcessCalledOperand(CB->getCalledOperand());
  8234. // Process callback functions.
  8235. SmallVector<const Use *, 4u> CallbackUses;
  8236. AbstractCallSite::getCallbackUses(*CB, CallbackUses);
  8237. for (const Use *U : CallbackUses)
  8238. ProcessCalledOperand(U->get());
  8239. return Change;
  8240. }
  8241. };
  8242. struct AACallEdgesFunction : public AACallEdgesImpl {
  8243. AACallEdgesFunction(const IRPosition &IRP, Attributor &A)
  8244. : AACallEdgesImpl(IRP, A) {}
  8245. /// See AbstractAttribute::updateImpl(...).
  8246. ChangeStatus updateImpl(Attributor &A) override {
  8247. ChangeStatus Change = ChangeStatus::UNCHANGED;
  8248. auto ProcessCallInst = [&](Instruction &Inst) {
  8249. CallBase &CB = cast<CallBase>(Inst);
  8250. auto &CBEdges = A.getAAFor<AACallEdges>(
  8251. *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
  8252. if (CBEdges.hasNonAsmUnknownCallee())
  8253. setHasUnknownCallee(true, Change);
  8254. if (CBEdges.hasUnknownCallee())
  8255. setHasUnknownCallee(false, Change);
  8256. for (Function *F : CBEdges.getOptimisticEdges())
  8257. addCalledFunction(F, Change);
  8258. return true;
  8259. };
  8260. // Visit all callable instructions.
  8261. bool UsedAssumedInformation = false;
  8262. if (!A.checkForAllCallLikeInstructions(ProcessCallInst, *this,
  8263. UsedAssumedInformation,
  8264. /* CheckBBLivenessOnly */ true)) {
  8265. // If we haven't looked at all call like instructions, assume that there
  8266. // are unknown callees.
  8267. setHasUnknownCallee(true, Change);
  8268. }
  8269. return Change;
  8270. }
  8271. };
  8272. struct AAFunctionReachabilityFunction : public AAFunctionReachability {
  8273. private:
  8274. struct QuerySet {
  8275. void markReachable(const Function &Fn) {
  8276. Reachable.insert(&Fn);
  8277. Unreachable.erase(&Fn);
  8278. }
  8279. /// If there is no information about the function None is returned.
  8280. Optional<bool> isCachedReachable(const Function &Fn) {
  8281. // Assume that we can reach the function.
  8282. // TODO: Be more specific with the unknown callee.
  8283. if (CanReachUnknownCallee)
  8284. return true;
  8285. if (Reachable.count(&Fn))
  8286. return true;
  8287. if (Unreachable.count(&Fn))
  8288. return false;
  8289. return llvm::None;
  8290. }
  8291. /// Set of functions that we know for sure is reachable.
  8292. DenseSet<const Function *> Reachable;
  8293. /// Set of functions that are unreachable, but might become reachable.
  8294. DenseSet<const Function *> Unreachable;
  8295. /// If we can reach a function with a call to a unknown function we assume
  8296. /// that we can reach any function.
  8297. bool CanReachUnknownCallee = false;
  8298. };
  8299. struct QueryResolver : public QuerySet {
  8300. ChangeStatus update(Attributor &A, const AAFunctionReachability &AA,
  8301. ArrayRef<const AACallEdges *> AAEdgesList) {
  8302. ChangeStatus Change = ChangeStatus::UNCHANGED;
  8303. for (auto *AAEdges : AAEdgesList) {
  8304. if (AAEdges->hasUnknownCallee()) {
  8305. if (!CanReachUnknownCallee)
  8306. Change = ChangeStatus::CHANGED;
  8307. CanReachUnknownCallee = true;
  8308. return Change;
  8309. }
  8310. }
  8311. for (const Function *Fn : make_early_inc_range(Unreachable)) {
  8312. if (checkIfReachable(A, AA, AAEdgesList, *Fn)) {
  8313. Change = ChangeStatus::CHANGED;
  8314. markReachable(*Fn);
  8315. }
  8316. }
  8317. return Change;
  8318. }
  8319. bool isReachable(Attributor &A, AAFunctionReachability &AA,
  8320. ArrayRef<const AACallEdges *> AAEdgesList,
  8321. const Function &Fn) {
  8322. Optional<bool> Cached = isCachedReachable(Fn);
  8323. if (Cached.hasValue())
  8324. return Cached.getValue();
  8325. // The query was not cached, thus it is new. We need to request an update
  8326. // explicitly to make sure this the information is properly run to a
  8327. // fixpoint.
  8328. A.registerForUpdate(AA);
  8329. // We need to assume that this function can't reach Fn to prevent
  8330. // an infinite loop if this function is recursive.
  8331. Unreachable.insert(&Fn);
  8332. bool Result = checkIfReachable(A, AA, AAEdgesList, Fn);
  8333. if (Result)
  8334. markReachable(Fn);
  8335. return Result;
  8336. }
  8337. bool checkIfReachable(Attributor &A, const AAFunctionReachability &AA,
  8338. ArrayRef<const AACallEdges *> AAEdgesList,
  8339. const Function &Fn) const {
  8340. // Handle the most trivial case first.
  8341. for (auto *AAEdges : AAEdgesList) {
  8342. const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
  8343. if (Edges.count(const_cast<Function *>(&Fn)))
  8344. return true;
  8345. }
  8346. SmallVector<const AAFunctionReachability *, 8> Deps;
  8347. for (auto &AAEdges : AAEdgesList) {
  8348. const SetVector<Function *> &Edges = AAEdges->getOptimisticEdges();
  8349. for (Function *Edge : Edges) {
  8350. // We don't need a dependency if the result is reachable.
  8351. const AAFunctionReachability &EdgeReachability =
  8352. A.getAAFor<AAFunctionReachability>(
  8353. AA, IRPosition::function(*Edge), DepClassTy::NONE);
  8354. Deps.push_back(&EdgeReachability);
  8355. if (EdgeReachability.canReach(A, Fn))
  8356. return true;
  8357. }
  8358. }
  8359. // The result is false for now, set dependencies and leave.
  8360. for (auto *Dep : Deps)
  8361. A.recordDependence(*Dep, AA, DepClassTy::REQUIRED);
  8362. return false;
  8363. }
  8364. };
  8365. /// Get call edges that can be reached by this instruction.
  8366. bool getReachableCallEdges(Attributor &A, const AAReachability &Reachability,
  8367. const Instruction &Inst,
  8368. SmallVector<const AACallEdges *> &Result) const {
  8369. // Determine call like instructions that we can reach from the inst.
  8370. auto CheckCallBase = [&](Instruction &CBInst) {
  8371. if (!Reachability.isAssumedReachable(A, Inst, CBInst))
  8372. return true;
  8373. auto &CB = cast<CallBase>(CBInst);
  8374. const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
  8375. *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
  8376. Result.push_back(&AAEdges);
  8377. return true;
  8378. };
  8379. bool UsedAssumedInformation = false;
  8380. return A.checkForAllCallLikeInstructions(CheckCallBase, *this,
  8381. UsedAssumedInformation,
  8382. /* CheckBBLivenessOnly */ true);
  8383. }
  8384. public:
  8385. AAFunctionReachabilityFunction(const IRPosition &IRP, Attributor &A)
  8386. : AAFunctionReachability(IRP, A) {}
  8387. bool canReach(Attributor &A, const Function &Fn) const override {
  8388. if (!isValidState())
  8389. return true;
  8390. const AACallEdges &AAEdges =
  8391. A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
  8392. // Attributor returns attributes as const, so this function has to be
  8393. // const for users of this attribute to use it without having to do
  8394. // a const_cast.
  8395. // This is a hack for us to be able to cache queries.
  8396. auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
  8397. bool Result = NonConstThis->WholeFunction.isReachable(A, *NonConstThis,
  8398. {&AAEdges}, Fn);
  8399. return Result;
  8400. }
  8401. /// Can \p CB reach \p Fn
  8402. bool canReach(Attributor &A, CallBase &CB,
  8403. const Function &Fn) const override {
  8404. if (!isValidState())
  8405. return true;
  8406. const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
  8407. *this, IRPosition::callsite_function(CB), DepClassTy::REQUIRED);
  8408. // Attributor returns attributes as const, so this function has to be
  8409. // const for users of this attribute to use it without having to do
  8410. // a const_cast.
  8411. // This is a hack for us to be able to cache queries.
  8412. auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
  8413. QueryResolver &CBQuery = NonConstThis->CBQueries[&CB];
  8414. bool Result = CBQuery.isReachable(A, *NonConstThis, {&AAEdges}, Fn);
  8415. return Result;
  8416. }
  8417. bool instructionCanReach(Attributor &A, const Instruction &Inst,
  8418. const Function &Fn,
  8419. bool UseBackwards) const override {
  8420. if (!isValidState())
  8421. return true;
  8422. if (UseBackwards)
  8423. return AA::isPotentiallyReachable(A, Inst, Fn, *this, nullptr);
  8424. const auto &Reachability = A.getAAFor<AAReachability>(
  8425. *this, IRPosition::function(*getAssociatedFunction()),
  8426. DepClassTy::REQUIRED);
  8427. SmallVector<const AACallEdges *> CallEdges;
  8428. bool AllKnown = getReachableCallEdges(A, Reachability, Inst, CallEdges);
  8429. // Attributor returns attributes as const, so this function has to be
  8430. // const for users of this attribute to use it without having to do
  8431. // a const_cast.
  8432. // This is a hack for us to be able to cache queries.
  8433. auto *NonConstThis = const_cast<AAFunctionReachabilityFunction *>(this);
  8434. QueryResolver &InstQSet = NonConstThis->InstQueries[&Inst];
  8435. if (!AllKnown)
  8436. InstQSet.CanReachUnknownCallee = true;
  8437. return InstQSet.isReachable(A, *NonConstThis, CallEdges, Fn);
  8438. }
  8439. /// See AbstractAttribute::updateImpl(...).
  8440. ChangeStatus updateImpl(Attributor &A) override {
  8441. const AACallEdges &AAEdges =
  8442. A.getAAFor<AACallEdges>(*this, getIRPosition(), DepClassTy::REQUIRED);
  8443. ChangeStatus Change = ChangeStatus::UNCHANGED;
  8444. Change |= WholeFunction.update(A, *this, {&AAEdges});
  8445. for (auto &CBPair : CBQueries) {
  8446. const AACallEdges &AAEdges = A.getAAFor<AACallEdges>(
  8447. *this, IRPosition::callsite_function(*CBPair.first),
  8448. DepClassTy::REQUIRED);
  8449. Change |= CBPair.second.update(A, *this, {&AAEdges});
  8450. }
  8451. // Update the Instruction queries.
  8452. const AAReachability *Reachability;
  8453. if (!InstQueries.empty()) {
  8454. Reachability = &A.getAAFor<AAReachability>(
  8455. *this, IRPosition::function(*getAssociatedFunction()),
  8456. DepClassTy::REQUIRED);
  8457. }
  8458. // Check for local callbases first.
  8459. for (auto &InstPair : InstQueries) {
  8460. SmallVector<const AACallEdges *> CallEdges;
  8461. bool AllKnown =
  8462. getReachableCallEdges(A, *Reachability, *InstPair.first, CallEdges);
  8463. // Update will return change if we this effects any queries.
  8464. if (!AllKnown)
  8465. InstPair.second.CanReachUnknownCallee = true;
  8466. Change |= InstPair.second.update(A, *this, CallEdges);
  8467. }
  8468. return Change;
  8469. }
  8470. const std::string getAsStr() const override {
  8471. size_t QueryCount =
  8472. WholeFunction.Reachable.size() + WholeFunction.Unreachable.size();
  8473. return "FunctionReachability [" +
  8474. std::to_string(WholeFunction.Reachable.size()) + "," +
  8475. std::to_string(QueryCount) + "]";
  8476. }
  8477. void trackStatistics() const override {}
  8478. private:
  8479. bool canReachUnknownCallee() const override {
  8480. return WholeFunction.CanReachUnknownCallee;
  8481. }
  8482. /// Used to answer if a the whole function can reacha a specific function.
  8483. QueryResolver WholeFunction;
  8484. /// Used to answer if a call base inside this function can reach a specific
  8485. /// function.
  8486. DenseMap<const CallBase *, QueryResolver> CBQueries;
  8487. /// This is for instruction queries than scan "forward".
  8488. DenseMap<const Instruction *, QueryResolver> InstQueries;
  8489. };
  8490. /// ---------------------- Assumption Propagation ------------------------------
  8491. struct AAAssumptionInfoImpl : public AAAssumptionInfo {
  8492. AAAssumptionInfoImpl(const IRPosition &IRP, Attributor &A,
  8493. const DenseSet<StringRef> &Known)
  8494. : AAAssumptionInfo(IRP, A, Known) {}
  8495. bool hasAssumption(const StringRef Assumption) const override {
  8496. return isValidState() && setContains(Assumption);
  8497. }
  8498. /// See AbstractAttribute::getAsStr()
  8499. const std::string getAsStr() const override {
  8500. const SetContents &Known = getKnown();
  8501. const SetContents &Assumed = getAssumed();
  8502. const std::string KnownStr =
  8503. llvm::join(Known.getSet().begin(), Known.getSet().end(), ",");
  8504. const std::string AssumedStr =
  8505. (Assumed.isUniversal())
  8506. ? "Universal"
  8507. : llvm::join(Assumed.getSet().begin(), Assumed.getSet().end(), ",");
  8508. return "Known [" + KnownStr + "]," + " Assumed [" + AssumedStr + "]";
  8509. }
  8510. };
  8511. /// Propagates assumption information from parent functions to all of their
  8512. /// successors. An assumption can be propagated if the containing function
  8513. /// dominates the called function.
  8514. ///
  8515. /// We start with a "known" set of assumptions already valid for the associated
  8516. /// function and an "assumed" set that initially contains all possible
  8517. /// assumptions. The assumed set is inter-procedurally updated by narrowing its
  8518. /// contents as concrete values are known. The concrete values are seeded by the
  8519. /// first nodes that are either entries into the call graph, or contains no
  8520. /// assumptions. Each node is updated as the intersection of the assumed state
  8521. /// with all of its predecessors.
  8522. struct AAAssumptionInfoFunction final : AAAssumptionInfoImpl {
  8523. AAAssumptionInfoFunction(const IRPosition &IRP, Attributor &A)
  8524. : AAAssumptionInfoImpl(IRP, A,
  8525. getAssumptions(*IRP.getAssociatedFunction())) {}
  8526. /// See AbstractAttribute::manifest(...).
  8527. ChangeStatus manifest(Attributor &A) override {
  8528. const auto &Assumptions = getKnown();
  8529. // Don't manifest a universal set if it somehow made it here.
  8530. if (Assumptions.isUniversal())
  8531. return ChangeStatus::UNCHANGED;
  8532. Function *AssociatedFunction = getAssociatedFunction();
  8533. bool Changed = addAssumptions(*AssociatedFunction, Assumptions.getSet());
  8534. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  8535. }
  8536. /// See AbstractAttribute::updateImpl(...).
  8537. ChangeStatus updateImpl(Attributor &A) override {
  8538. bool Changed = false;
  8539. auto CallSitePred = [&](AbstractCallSite ACS) {
  8540. const auto &AssumptionAA = A.getAAFor<AAAssumptionInfo>(
  8541. *this, IRPosition::callsite_function(*ACS.getInstruction()),
  8542. DepClassTy::REQUIRED);
  8543. // Get the set of assumptions shared by all of this function's callers.
  8544. Changed |= getIntersection(AssumptionAA.getAssumed());
  8545. return !getAssumed().empty() || !getKnown().empty();
  8546. };
  8547. bool UsedAssumedInformation = false;
  8548. // Get the intersection of all assumptions held by this node's predecessors.
  8549. // If we don't know all the call sites then this is either an entry into the
  8550. // call graph or an empty node. This node is known to only contain its own
  8551. // assumptions and can be propagated to its successors.
  8552. if (!A.checkForAllCallSites(CallSitePred, *this, true,
  8553. UsedAssumedInformation))
  8554. return indicatePessimisticFixpoint();
  8555. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  8556. }
  8557. void trackStatistics() const override {}
  8558. };
  8559. /// Assumption Info defined for call sites.
  8560. struct AAAssumptionInfoCallSite final : AAAssumptionInfoImpl {
  8561. AAAssumptionInfoCallSite(const IRPosition &IRP, Attributor &A)
  8562. : AAAssumptionInfoImpl(IRP, A, getInitialAssumptions(IRP)) {}
  8563. /// See AbstractAttribute::initialize(...).
  8564. void initialize(Attributor &A) override {
  8565. const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
  8566. A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
  8567. }
  8568. /// See AbstractAttribute::manifest(...).
  8569. ChangeStatus manifest(Attributor &A) override {
  8570. // Don't manifest a universal set if it somehow made it here.
  8571. if (getKnown().isUniversal())
  8572. return ChangeStatus::UNCHANGED;
  8573. CallBase &AssociatedCall = cast<CallBase>(getAssociatedValue());
  8574. bool Changed = addAssumptions(AssociatedCall, getAssumed().getSet());
  8575. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  8576. }
  8577. /// See AbstractAttribute::updateImpl(...).
  8578. ChangeStatus updateImpl(Attributor &A) override {
  8579. const IRPosition &FnPos = IRPosition::function(*getAnchorScope());
  8580. auto &AssumptionAA =
  8581. A.getAAFor<AAAssumptionInfo>(*this, FnPos, DepClassTy::REQUIRED);
  8582. bool Changed = getIntersection(AssumptionAA.getAssumed());
  8583. return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
  8584. }
  8585. /// See AbstractAttribute::trackStatistics()
  8586. void trackStatistics() const override {}
  8587. private:
  8588. /// Helper to initialized the known set as all the assumptions this call and
  8589. /// the callee contain.
  8590. DenseSet<StringRef> getInitialAssumptions(const IRPosition &IRP) {
  8591. const CallBase &CB = cast<CallBase>(IRP.getAssociatedValue());
  8592. auto Assumptions = getAssumptions(CB);
  8593. if (Function *F = IRP.getAssociatedFunction())
  8594. set_union(Assumptions, getAssumptions(*F));
  8595. if (Function *F = IRP.getAssociatedFunction())
  8596. set_union(Assumptions, getAssumptions(*F));
  8597. return Assumptions;
  8598. }
  8599. };
  8600. AACallGraphNode *AACallEdgeIterator::operator*() const {
  8601. return static_cast<AACallGraphNode *>(const_cast<AACallEdges *>(
  8602. &A.getOrCreateAAFor<AACallEdges>(IRPosition::function(**I))));
  8603. }
  8604. void AttributorCallGraph::print() { llvm::WriteGraph(outs(), this); }
  8605. const char AAReturnedValues::ID = 0;
  8606. const char AANoUnwind::ID = 0;
  8607. const char AANoSync::ID = 0;
  8608. const char AANoFree::ID = 0;
  8609. const char AANonNull::ID = 0;
  8610. const char AANoRecurse::ID = 0;
  8611. const char AAWillReturn::ID = 0;
  8612. const char AAUndefinedBehavior::ID = 0;
  8613. const char AANoAlias::ID = 0;
  8614. const char AAReachability::ID = 0;
  8615. const char AANoReturn::ID = 0;
  8616. const char AAIsDead::ID = 0;
  8617. const char AADereferenceable::ID = 0;
  8618. const char AAAlign::ID = 0;
  8619. const char AANoCapture::ID = 0;
  8620. const char AAValueSimplify::ID = 0;
  8621. const char AAHeapToStack::ID = 0;
  8622. const char AAPrivatizablePtr::ID = 0;
  8623. const char AAMemoryBehavior::ID = 0;
  8624. const char AAMemoryLocation::ID = 0;
  8625. const char AAValueConstantRange::ID = 0;
  8626. const char AAPotentialValues::ID = 0;
  8627. const char AANoUndef::ID = 0;
  8628. const char AACallEdges::ID = 0;
  8629. const char AAFunctionReachability::ID = 0;
  8630. const char AAPointerInfo::ID = 0;
  8631. const char AAAssumptionInfo::ID = 0;
  8632. // Macro magic to create the static generator function for attributes that
  8633. // follow the naming scheme.
  8634. #define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
  8635. case IRPosition::PK: \
  8636. llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
  8637. #define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
  8638. case IRPosition::PK: \
  8639. AA = new (A.Allocator) CLASS##SUFFIX(IRP, A); \
  8640. ++NumAAs; \
  8641. break;
  8642. #define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  8643. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  8644. CLASS *AA = nullptr; \
  8645. switch (IRP.getPositionKind()) { \
  8646. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  8647. SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
  8648. SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
  8649. SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
  8650. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
  8651. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
  8652. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  8653. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
  8654. } \
  8655. return *AA; \
  8656. }
  8657. #define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  8658. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  8659. CLASS *AA = nullptr; \
  8660. switch (IRP.getPositionKind()) { \
  8661. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  8662. SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
  8663. SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
  8664. SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
  8665. SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
  8666. SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
  8667. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
  8668. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
  8669. } \
  8670. return *AA; \
  8671. }
  8672. #define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  8673. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  8674. CLASS *AA = nullptr; \
  8675. switch (IRP.getPositionKind()) { \
  8676. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  8677. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  8678. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
  8679. SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
  8680. SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
  8681. SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
  8682. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
  8683. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
  8684. } \
  8685. return *AA; \
  8686. }
  8687. #define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  8688. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  8689. CLASS *AA = nullptr; \
  8690. switch (IRP.getPositionKind()) { \
  8691. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  8692. SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
  8693. SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
  8694. SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
  8695. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
  8696. SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
  8697. SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
  8698. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  8699. } \
  8700. return *AA; \
  8701. }
  8702. #define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
  8703. CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
  8704. CLASS *AA = nullptr; \
  8705. switch (IRP.getPositionKind()) { \
  8706. SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
  8707. SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
  8708. SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
  8709. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
  8710. SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
  8711. SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
  8712. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
  8713. SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
  8714. } \
  8715. return *AA; \
  8716. }
  8717. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
  8718. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
  8719. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
  8720. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
  8721. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
  8722. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
  8723. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
  8724. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AACallEdges)
  8725. CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAssumptionInfo)
  8726. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
  8727. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
  8728. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
  8729. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
  8730. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
  8731. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
  8732. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
  8733. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPotentialValues)
  8734. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUndef)
  8735. CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
  8736. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
  8737. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
  8738. CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
  8739. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
  8740. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
  8741. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
  8742. CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAFunctionReachability)
  8743. CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
  8744. #undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
  8745. #undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
  8746. #undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
  8747. #undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
  8748. #undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
  8749. #undef SWITCH_PK_CREATE
  8750. #undef SWITCH_PK_INV