Attributor.h 213 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Attributor.h --- Module-wide attribute deduction ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Attributor: An inter procedural (abstract) "attribute" deduction framework.
  15. //
  16. // The Attributor framework is an inter procedural abstract analysis (fixpoint
  17. // iteration analysis). The goal is to allow easy deduction of new attributes as
  18. // well as information exchange between abstract attributes in-flight.
  19. //
  20. // The Attributor class is the driver and the link between the various abstract
  21. // attributes. The Attributor will iterate until a fixpoint state is reached by
  22. // all abstract attributes in-flight, or until it will enforce a pessimistic fix
  23. // point because an iteration limit is reached.
  24. //
  25. // Abstract attributes, derived from the AbstractAttribute class, actually
  26. // describe properties of the code. They can correspond to actual LLVM-IR
  27. // attributes, or they can be more general, ultimately unrelated to LLVM-IR
  28. // attributes. The latter is useful when an abstract attributes provides
  29. // information to other abstract attributes in-flight but we might not want to
  30. // manifest the information. The Attributor allows to query in-flight abstract
  31. // attributes through the `Attributor::getAAFor` method (see the method
  32. // description for an example). If the method is used by an abstract attribute
  33. // P, and it results in an abstract attribute Q, the Attributor will
  34. // automatically capture a potential dependence from Q to P. This dependence
  35. // will cause P to be reevaluated whenever Q changes in the future.
  36. //
  37. // The Attributor will only reevaluate abstract attributes that might have
  38. // changed since the last iteration. That means that the Attribute will not
  39. // revisit all instructions/blocks/functions in the module but only query
  40. // an update from a subset of the abstract attributes.
  41. //
  42. // The update method `AbstractAttribute::updateImpl` is implemented by the
  43. // specific "abstract attribute" subclasses. The method is invoked whenever the
  44. // currently assumed state (see the AbstractState class) might not be valid
  45. // anymore. This can, for example, happen if the state was dependent on another
  46. // abstract attribute that changed. In every invocation, the update method has
  47. // to adjust the internal state of an abstract attribute to a point that is
  48. // justifiable by the underlying IR and the current state of abstract attributes
  49. // in-flight. Since the IR is given and assumed to be valid, the information
  50. // derived from it can be assumed to hold. However, information derived from
  51. // other abstract attributes is conditional on various things. If the justifying
  52. // state changed, the `updateImpl` has to revisit the situation and potentially
  53. // find another justification or limit the optimistic assumes made.
  54. //
  55. // Change is the key in this framework. Until a state of no-change, thus a
  56. // fixpoint, is reached, the Attributor will query the abstract attributes
  57. // in-flight to re-evaluate their state. If the (current) state is too
  58. // optimistic, hence it cannot be justified anymore through other abstract
  59. // attributes or the state of the IR, the state of the abstract attribute will
  60. // have to change. Generally, we assume abstract attribute state to be a finite
  61. // height lattice and the update function to be monotone. However, these
  62. // conditions are not enforced because the iteration limit will guarantee
  63. // termination. If an optimistic fixpoint is reached, or a pessimistic fix
  64. // point is enforced after a timeout, the abstract attributes are tasked to
  65. // manifest their result in the IR for passes to come.
  66. //
  67. // Attribute manifestation is not mandatory. If desired, there is support to
  68. // generate a single or multiple LLVM-IR attributes already in the helper struct
  69. // IRAttribute. In the simplest case, a subclass inherits from IRAttribute with
  70. // a proper Attribute::AttrKind as template parameter. The Attributor
  71. // manifestation framework will then create and place a new attribute if it is
  72. // allowed to do so (based on the abstract state). Other use cases can be
  73. // achieved by overloading AbstractAttribute or IRAttribute methods.
  74. //
  75. //
  76. // The "mechanics" of adding a new "abstract attribute":
  77. // - Define a class (transitively) inheriting from AbstractAttribute and one
  78. // (which could be the same) that (transitively) inherits from AbstractState.
  79. // For the latter, consider the already available BooleanState and
  80. // {Inc,Dec,Bit}IntegerState if they fit your needs, e.g., you require only a
  81. // number tracking or bit-encoding.
  82. // - Implement all pure methods. Also use overloading if the attribute is not
  83. // conforming with the "default" behavior: A (set of) LLVM-IR attribute(s) for
  84. // an argument, call site argument, function return value, or function. See
  85. // the class and method descriptions for more information on the two
  86. // "Abstract" classes and their respective methods.
  87. // - Register opportunities for the new abstract attribute in the
  88. // `Attributor::identifyDefaultAbstractAttributes` method if it should be
  89. // counted as a 'default' attribute.
  90. // - Add sufficient tests.
  91. // - Add a Statistics object for bookkeeping. If it is a simple (set of)
  92. // attribute(s) manifested through the Attributor manifestation framework, see
  93. // the bookkeeping function in Attributor.cpp.
  94. // - If instructions with a certain opcode are interesting to the attribute, add
  95. // that opcode to the switch in `Attributor::identifyAbstractAttributes`. This
  96. // will make it possible to query all those instructions through the
  97. // `InformationCache::getOpcodeInstMapForFunction` interface and eliminate the
  98. // need to traverse the IR repeatedly.
  99. //
  100. //===----------------------------------------------------------------------===//
  101. #ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
  102. #define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
  103. #include "llvm/ADT/DenseSet.h"
  104. #include "llvm/ADT/GraphTraits.h"
  105. #include "llvm/ADT/MapVector.h"
  106. #include "llvm/ADT/STLExtras.h"
  107. #include "llvm/ADT/SetOperations.h"
  108. #include "llvm/ADT/SetVector.h"
  109. #include "llvm/ADT/Triple.h"
  110. #include "llvm/ADT/iterator.h"
  111. #include "llvm/Analysis/AssumeBundleQueries.h"
  112. #include "llvm/Analysis/CFG.h"
  113. #include "llvm/Analysis/CGSCCPassManager.h"
  114. #include "llvm/Analysis/LazyCallGraph.h"
  115. #include "llvm/Analysis/LoopInfo.h"
  116. #include "llvm/Analysis/MemoryLocation.h"
  117. #include "llvm/Analysis/MustExecute.h"
  118. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  119. #include "llvm/Analysis/PostDominators.h"
  120. #include "llvm/Analysis/TargetLibraryInfo.h"
  121. #include "llvm/IR/AbstractCallSite.h"
  122. #include "llvm/IR/ConstantRange.h"
  123. #include "llvm/IR/Constants.h"
  124. #include "llvm/IR/InstIterator.h"
  125. #include "llvm/IR/Instruction.h"
  126. #include "llvm/IR/PassManager.h"
  127. #include "llvm/IR/Value.h"
  128. #include "llvm/Support/Alignment.h"
  129. #include "llvm/Support/Allocator.h"
  130. #include "llvm/Support/Casting.h"
  131. #include "llvm/Support/DOTGraphTraits.h"
  132. #include "llvm/Support/TimeProfiler.h"
  133. #include "llvm/Transforms/Utils/CallGraphUpdater.h"
  134. #include <limits>
  135. #include <map>
  136. #include <optional>
  137. namespace llvm {
  138. class DataLayout;
  139. class LLVMContext;
  140. class Pass;
  141. template <typename Fn> class function_ref;
  142. struct AADepGraphNode;
  143. struct AADepGraph;
  144. struct Attributor;
  145. struct AbstractAttribute;
  146. struct InformationCache;
  147. struct AAIsDead;
  148. struct AttributorCallGraph;
  149. struct IRPosition;
  150. class AAResults;
  151. class Function;
  152. /// Abstract Attribute helper functions.
  153. namespace AA {
  154. using InstExclusionSetTy = SmallPtrSet<Instruction *, 4>;
  155. enum class GPUAddressSpace : unsigned {
  156. Generic = 0,
  157. Global = 1,
  158. Shared = 3,
  159. Constant = 4,
  160. Local = 5,
  161. };
  162. /// Flags to distinguish intra-procedural queries from *potentially*
  163. /// inter-procedural queries. Not that information can be valid for both and
  164. /// therefore both bits might be set.
  165. enum ValueScope : uint8_t {
  166. Intraprocedural = 1,
  167. Interprocedural = 2,
  168. AnyScope = Intraprocedural | Interprocedural,
  169. };
  170. struct ValueAndContext : public std::pair<Value *, const Instruction *> {
  171. using Base = std::pair<Value *, const Instruction *>;
  172. ValueAndContext(const Base &B) : Base(B) {}
  173. ValueAndContext(Value &V, const Instruction *CtxI) : Base(&V, CtxI) {}
  174. ValueAndContext(Value &V, const Instruction &CtxI) : Base(&V, &CtxI) {}
  175. Value *getValue() const { return this->first; }
  176. const Instruction *getCtxI() const { return this->second; }
  177. };
  178. /// Return true if \p I is a `nosync` instruction. Use generic reasoning and
  179. /// potentially the corresponding AANoSync.
  180. bool isNoSyncInst(Attributor &A, const Instruction &I,
  181. const AbstractAttribute &QueryingAA);
  182. /// Return true if \p V is dynamically unique, that is, there are no two
  183. /// "instances" of \p V at runtime with different values.
  184. /// Note: If \p ForAnalysisOnly is set we only check that the Attributor will
  185. /// never use \p V to represent two "instances" not that \p V could not
  186. /// technically represent them.
  187. bool isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
  188. const Value &V, bool ForAnalysisOnly = true);
  189. /// Return true if \p V is a valid value in \p Scope, that is a constant or an
  190. /// instruction/argument of \p Scope.
  191. bool isValidInScope(const Value &V, const Function *Scope);
  192. /// Return true if the value of \p VAC is a valid at the position of \p VAC,
  193. /// that is a constant, an argument of the same function, or an instruction in
  194. /// that function that dominates the position.
  195. bool isValidAtPosition(const ValueAndContext &VAC, InformationCache &InfoCache);
  196. /// Try to convert \p V to type \p Ty without introducing new instructions. If
  197. /// this is not possible return `nullptr`. Note: this function basically knows
  198. /// how to cast various constants.
  199. Value *getWithType(Value &V, Type &Ty);
  200. /// Return the combination of \p A and \p B such that the result is a possible
  201. /// value of both. \p B is potentially casted to match the type \p Ty or the
  202. /// type of \p A if \p Ty is null.
  203. ///
  204. /// Examples:
  205. /// X + none => X
  206. /// not_none + undef => not_none
  207. /// V1 + V2 => nullptr
  208. std::optional<Value *>
  209. combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
  210. const std::optional<Value *> &B, Type *Ty);
  211. /// Helper to represent an access offset and size, with logic to deal with
  212. /// uncertainty and check for overlapping accesses.
  213. struct RangeTy {
  214. int64_t Offset = Unassigned;
  215. int64_t Size = Unassigned;
  216. RangeTy(int64_t Offset, int64_t Size) : Offset(Offset), Size(Size) {}
  217. RangeTy() = default;
  218. static RangeTy getUnknown() { return RangeTy{Unknown, Unknown}; }
  219. /// Return true if offset or size are unknown.
  220. bool offsetOrSizeAreUnknown() const {
  221. return Offset == RangeTy::Unknown || Size == RangeTy::Unknown;
  222. }
  223. /// Return true if offset and size are unknown, thus this is the default
  224. /// unknown object.
  225. bool offsetAndSizeAreUnknown() const {
  226. return Offset == RangeTy::Unknown && Size == RangeTy::Unknown;
  227. }
  228. /// Return true if the offset and size are unassigned.
  229. bool isUnassigned() const {
  230. assert((Offset == RangeTy::Unassigned) == (Size == RangeTy::Unassigned) &&
  231. "Inconsistent state!");
  232. return Offset == RangeTy::Unassigned;
  233. }
  234. /// Return true if this offset and size pair might describe an address that
  235. /// overlaps with \p Range.
  236. bool mayOverlap(const RangeTy &Range) const {
  237. // Any unknown value and we are giving up -> overlap.
  238. if (offsetOrSizeAreUnknown() || Range.offsetOrSizeAreUnknown())
  239. return true;
  240. // Check if one offset point is in the other interval [offset,
  241. // offset+size].
  242. return Range.Offset + Range.Size > Offset && Range.Offset < Offset + Size;
  243. }
  244. RangeTy &operator&=(const RangeTy &R) {
  245. if (Offset == Unassigned)
  246. Offset = R.Offset;
  247. else if (R.Offset != Unassigned && R.Offset != Offset)
  248. Offset = Unknown;
  249. if (Size == Unassigned)
  250. Size = R.Size;
  251. else if (Size == Unknown || R.Size == Unknown)
  252. Size = Unknown;
  253. else if (R.Size != Unassigned)
  254. Size = std::max(Size, R.Size);
  255. return *this;
  256. }
  257. /// Comparison for sorting ranges by offset.
  258. ///
  259. /// Returns true if the offset \p L is less than that of \p R.
  260. inline static bool OffsetLessThan(const RangeTy &L, const RangeTy &R) {
  261. return L.Offset < R.Offset;
  262. }
  263. /// Constants used to represent special offsets or sizes.
  264. /// - We cannot assume that Offsets and Size are non-negative.
  265. /// - The constants should not clash with DenseMapInfo, such as EmptyKey
  266. /// (INT64_MAX) and TombstoneKey (INT64_MIN).
  267. /// We use values "in the middle" of the 64 bit range to represent these
  268. /// special cases.
  269. static constexpr int64_t Unassigned = std::numeric_limits<int32_t>::min();
  270. static constexpr int64_t Unknown = std::numeric_limits<int32_t>::max();
  271. };
  272. inline raw_ostream &operator<<(raw_ostream &OS, const RangeTy &R) {
  273. OS << "[" << R.Offset << ", " << R.Size << "]";
  274. return OS;
  275. }
  276. inline bool operator==(const RangeTy &A, const RangeTy &B) {
  277. return A.Offset == B.Offset && A.Size == B.Size;
  278. }
  279. inline bool operator!=(const RangeTy &A, const RangeTy &B) { return !(A == B); }
  280. /// Return the initial value of \p Obj with type \p Ty if that is a constant.
  281. Constant *getInitialValueForObj(Value &Obj, Type &Ty,
  282. const TargetLibraryInfo *TLI,
  283. const DataLayout &DL,
  284. RangeTy *RangePtr = nullptr);
  285. /// Collect all potential values \p LI could read into \p PotentialValues. That
  286. /// is, the only values read by \p LI are assumed to be known and all are in
  287. /// \p PotentialValues. \p PotentialValueOrigins will contain all the
  288. /// instructions that might have put a potential value into \p PotentialValues.
  289. /// Dependences onto \p QueryingAA are properly tracked, \p
  290. /// UsedAssumedInformation will inform the caller if assumed information was
  291. /// used.
  292. ///
  293. /// \returns True if the assumed potential copies are all in \p PotentialValues,
  294. /// false if something went wrong and the copies could not be
  295. /// determined.
  296. bool getPotentiallyLoadedValues(
  297. Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
  298. SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
  299. const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
  300. bool OnlyExact = false);
  301. /// Collect all potential values of the one stored by \p SI into
  302. /// \p PotentialCopies. That is, the only copies that were made via the
  303. /// store are assumed to be known and all are in \p PotentialCopies. Dependences
  304. /// onto \p QueryingAA are properly tracked, \p UsedAssumedInformation will
  305. /// inform the caller if assumed information was used.
  306. ///
  307. /// \returns True if the assumed potential copies are all in \p PotentialCopies,
  308. /// false if something went wrong and the copies could not be
  309. /// determined.
  310. bool getPotentialCopiesOfStoredValue(
  311. Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
  312. const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
  313. bool OnlyExact = false);
  314. /// Return true if \p IRP is readonly. This will query respective AAs that
  315. /// deduce the information and introduce dependences for \p QueryingAA.
  316. bool isAssumedReadOnly(Attributor &A, const IRPosition &IRP,
  317. const AbstractAttribute &QueryingAA, bool &IsKnown);
  318. /// Return true if \p IRP is readnone. This will query respective AAs that
  319. /// deduce the information and introduce dependences for \p QueryingAA.
  320. bool isAssumedReadNone(Attributor &A, const IRPosition &IRP,
  321. const AbstractAttribute &QueryingAA, bool &IsKnown);
  322. /// Return true if \p ToI is potentially reachable from \p FromI without running
  323. /// into any instruction in \p ExclusionSet The two instructions do not need to
  324. /// be in the same function. \p GoBackwardsCB can be provided to convey domain
  325. /// knowledge about the "lifespan" the user is interested in. By default, the
  326. /// callers of \p FromI are checked as well to determine if \p ToI can be
  327. /// reached. If the query is not interested in callers beyond a certain point,
  328. /// e.g., a GPU kernel entry or the function containing an alloca, the
  329. /// \p GoBackwardsCB should return false.
  330. bool isPotentiallyReachable(
  331. Attributor &A, const Instruction &FromI, const Instruction &ToI,
  332. const AbstractAttribute &QueryingAA,
  333. const AA::InstExclusionSetTy *ExclusionSet = nullptr,
  334. std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
  335. /// Same as above but it is sufficient to reach any instruction in \p ToFn.
  336. bool isPotentiallyReachable(
  337. Attributor &A, const Instruction &FromI, const Function &ToFn,
  338. const AbstractAttribute &QueryingAA,
  339. const AA::InstExclusionSetTy *ExclusionSet = nullptr,
  340. std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
  341. /// Return true if \p Obj is assumed to be a thread local object.
  342. bool isAssumedThreadLocalObject(Attributor &A, Value &Obj,
  343. const AbstractAttribute &QueryingAA);
  344. /// Return true if \p I is potentially affected by a barrier.
  345. bool isPotentiallyAffectedByBarrier(Attributor &A, const Instruction &I,
  346. const AbstractAttribute &QueryingAA);
  347. bool isPotentiallyAffectedByBarrier(Attributor &A, ArrayRef<const Value *> Ptrs,
  348. const AbstractAttribute &QueryingAA,
  349. const Instruction *CtxI);
  350. } // namespace AA
  351. template <>
  352. struct DenseMapInfo<AA::ValueAndContext>
  353. : public DenseMapInfo<AA::ValueAndContext::Base> {
  354. using Base = DenseMapInfo<AA::ValueAndContext::Base>;
  355. static inline AA::ValueAndContext getEmptyKey() {
  356. return Base::getEmptyKey();
  357. }
  358. static inline AA::ValueAndContext getTombstoneKey() {
  359. return Base::getTombstoneKey();
  360. }
  361. static unsigned getHashValue(const AA::ValueAndContext &VAC) {
  362. return Base::getHashValue(VAC);
  363. }
  364. static bool isEqual(const AA::ValueAndContext &LHS,
  365. const AA::ValueAndContext &RHS) {
  366. return Base::isEqual(LHS, RHS);
  367. }
  368. };
  369. template <>
  370. struct DenseMapInfo<AA::ValueScope> : public DenseMapInfo<unsigned char> {
  371. using Base = DenseMapInfo<unsigned char>;
  372. static inline AA::ValueScope getEmptyKey() {
  373. return AA::ValueScope(Base::getEmptyKey());
  374. }
  375. static inline AA::ValueScope getTombstoneKey() {
  376. return AA::ValueScope(Base::getTombstoneKey());
  377. }
  378. static unsigned getHashValue(const AA::ValueScope &S) {
  379. return Base::getHashValue(S);
  380. }
  381. static bool isEqual(const AA::ValueScope &LHS, const AA::ValueScope &RHS) {
  382. return Base::isEqual(LHS, RHS);
  383. }
  384. };
  385. template <>
  386. struct DenseMapInfo<const AA::InstExclusionSetTy *>
  387. : public DenseMapInfo<void *> {
  388. using super = DenseMapInfo<void *>;
  389. static inline const AA::InstExclusionSetTy *getEmptyKey() {
  390. return static_cast<const AA::InstExclusionSetTy *>(super::getEmptyKey());
  391. }
  392. static inline const AA::InstExclusionSetTy *getTombstoneKey() {
  393. return static_cast<const AA::InstExclusionSetTy *>(
  394. super::getTombstoneKey());
  395. }
  396. static unsigned getHashValue(const AA::InstExclusionSetTy *BES) {
  397. unsigned H = 0;
  398. if (BES)
  399. for (const auto *II : *BES)
  400. H += DenseMapInfo<const Instruction *>::getHashValue(II);
  401. return H;
  402. }
  403. static bool isEqual(const AA::InstExclusionSetTy *LHS,
  404. const AA::InstExclusionSetTy *RHS) {
  405. if (LHS == RHS)
  406. return true;
  407. if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
  408. LHS == getTombstoneKey() || RHS == getTombstoneKey())
  409. return false;
  410. if (!LHS || !RHS)
  411. return ((LHS && LHS->empty()) || (RHS && RHS->empty()));
  412. if (LHS->size() != RHS->size())
  413. return false;
  414. return llvm::set_is_subset(*LHS, *RHS);
  415. }
  416. };
  417. /// The value passed to the line option that defines the maximal initialization
  418. /// chain length.
  419. extern unsigned MaxInitializationChainLength;
  420. ///{
  421. enum class ChangeStatus {
  422. CHANGED,
  423. UNCHANGED,
  424. };
  425. ChangeStatus operator|(ChangeStatus l, ChangeStatus r);
  426. ChangeStatus &operator|=(ChangeStatus &l, ChangeStatus r);
  427. ChangeStatus operator&(ChangeStatus l, ChangeStatus r);
  428. ChangeStatus &operator&=(ChangeStatus &l, ChangeStatus r);
  429. enum class DepClassTy {
  430. REQUIRED, ///< The target cannot be valid if the source is not.
  431. OPTIONAL, ///< The target may be valid if the source is not.
  432. NONE, ///< Do not track a dependence between source and target.
  433. };
  434. ///}
  435. /// The data structure for the nodes of a dependency graph
  436. struct AADepGraphNode {
  437. public:
  438. virtual ~AADepGraphNode() = default;
  439. using DepTy = PointerIntPair<AADepGraphNode *, 1>;
  440. protected:
  441. /// Set of dependency graph nodes which should be updated if this one
  442. /// is updated. The bit encodes if it is optional.
  443. TinyPtrVector<DepTy> Deps;
  444. static AADepGraphNode *DepGetVal(DepTy &DT) { return DT.getPointer(); }
  445. static AbstractAttribute *DepGetValAA(DepTy &DT) {
  446. return cast<AbstractAttribute>(DT.getPointer());
  447. }
  448. operator AbstractAttribute *() { return cast<AbstractAttribute>(this); }
  449. public:
  450. using iterator =
  451. mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
  452. using aaiterator =
  453. mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetValAA)>;
  454. aaiterator begin() { return aaiterator(Deps.begin(), &DepGetValAA); }
  455. aaiterator end() { return aaiterator(Deps.end(), &DepGetValAA); }
  456. iterator child_begin() { return iterator(Deps.begin(), &DepGetVal); }
  457. iterator child_end() { return iterator(Deps.end(), &DepGetVal); }
  458. virtual void print(raw_ostream &OS) const { OS << "AADepNode Impl\n"; }
  459. TinyPtrVector<DepTy> &getDeps() { return Deps; }
  460. friend struct Attributor;
  461. friend struct AADepGraph;
  462. };
  463. /// The data structure for the dependency graph
  464. ///
  465. /// Note that in this graph if there is an edge from A to B (A -> B),
  466. /// then it means that B depends on A, and when the state of A is
  467. /// updated, node B should also be updated
  468. struct AADepGraph {
  469. AADepGraph() = default;
  470. ~AADepGraph() = default;
  471. using DepTy = AADepGraphNode::DepTy;
  472. static AADepGraphNode *DepGetVal(DepTy &DT) { return DT.getPointer(); }
  473. using iterator =
  474. mapped_iterator<TinyPtrVector<DepTy>::iterator, decltype(&DepGetVal)>;
  475. /// There is no root node for the dependency graph. But the SCCIterator
  476. /// requires a single entry point, so we maintain a fake("synthetic") root
  477. /// node that depends on every node.
  478. AADepGraphNode SyntheticRoot;
  479. AADepGraphNode *GetEntryNode() { return &SyntheticRoot; }
  480. iterator begin() { return SyntheticRoot.child_begin(); }
  481. iterator end() { return SyntheticRoot.child_end(); }
  482. void viewGraph();
  483. /// Dump graph to file
  484. void dumpGraph();
  485. /// Print dependency graph
  486. void print();
  487. };
  488. /// Helper to describe and deal with positions in the LLVM-IR.
  489. ///
  490. /// A position in the IR is described by an anchor value and an "offset" that
  491. /// could be the argument number, for call sites and arguments, or an indicator
  492. /// of the "position kind". The kinds, specified in the Kind enum below, include
  493. /// the locations in the attribute list, i.a., function scope and return value,
  494. /// as well as a distinction between call sites and functions. Finally, there
  495. /// are floating values that do not have a corresponding attribute list
  496. /// position.
  497. struct IRPosition {
  498. // NOTE: In the future this definition can be changed to support recursive
  499. // functions.
  500. using CallBaseContext = CallBase;
  501. /// The positions we distinguish in the IR.
  502. enum Kind : char {
  503. IRP_INVALID, ///< An invalid position.
  504. IRP_FLOAT, ///< A position that is not associated with a spot suitable
  505. ///< for attributes. This could be any value or instruction.
  506. IRP_RETURNED, ///< An attribute for the function return value.
  507. IRP_CALL_SITE_RETURNED, ///< An attribute for a call site return value.
  508. IRP_FUNCTION, ///< An attribute for a function (scope).
  509. IRP_CALL_SITE, ///< An attribute for a call site (function scope).
  510. IRP_ARGUMENT, ///< An attribute for a function argument.
  511. IRP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument.
  512. };
  513. /// Default constructor available to create invalid positions implicitly. All
  514. /// other positions need to be created explicitly through the appropriate
  515. /// static member function.
  516. IRPosition() : Enc(nullptr, ENC_VALUE) { verify(); }
  517. /// Create a position describing the value of \p V.
  518. static const IRPosition value(const Value &V,
  519. const CallBaseContext *CBContext = nullptr) {
  520. if (auto *Arg = dyn_cast<Argument>(&V))
  521. return IRPosition::argument(*Arg, CBContext);
  522. if (auto *CB = dyn_cast<CallBase>(&V))
  523. return IRPosition::callsite_returned(*CB);
  524. return IRPosition(const_cast<Value &>(V), IRP_FLOAT, CBContext);
  525. }
  526. /// Create a position describing the instruction \p I. This is different from
  527. /// the value version because call sites are treated as intrusctions rather
  528. /// than their return value in this function.
  529. static const IRPosition inst(const Instruction &I,
  530. const CallBaseContext *CBContext = nullptr) {
  531. return IRPosition(const_cast<Instruction &>(I), IRP_FLOAT, CBContext);
  532. }
  533. /// Create a position describing the function scope of \p F.
  534. /// \p CBContext is used for call base specific analysis.
  535. static const IRPosition function(const Function &F,
  536. const CallBaseContext *CBContext = nullptr) {
  537. return IRPosition(const_cast<Function &>(F), IRP_FUNCTION, CBContext);
  538. }
  539. /// Create a position describing the returned value of \p F.
  540. /// \p CBContext is used for call base specific analysis.
  541. static const IRPosition returned(const Function &F,
  542. const CallBaseContext *CBContext = nullptr) {
  543. return IRPosition(const_cast<Function &>(F), IRP_RETURNED, CBContext);
  544. }
  545. /// Create a position describing the argument \p Arg.
  546. /// \p CBContext is used for call base specific analysis.
  547. static const IRPosition argument(const Argument &Arg,
  548. const CallBaseContext *CBContext = nullptr) {
  549. return IRPosition(const_cast<Argument &>(Arg), IRP_ARGUMENT, CBContext);
  550. }
  551. /// Create a position describing the function scope of \p CB.
  552. static const IRPosition callsite_function(const CallBase &CB) {
  553. return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE);
  554. }
  555. /// Create a position describing the returned value of \p CB.
  556. static const IRPosition callsite_returned(const CallBase &CB) {
  557. return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE_RETURNED);
  558. }
  559. /// Create a position describing the argument of \p CB at position \p ArgNo.
  560. static const IRPosition callsite_argument(const CallBase &CB,
  561. unsigned ArgNo) {
  562. return IRPosition(const_cast<Use &>(CB.getArgOperandUse(ArgNo)),
  563. IRP_CALL_SITE_ARGUMENT);
  564. }
  565. /// Create a position describing the argument of \p ACS at position \p ArgNo.
  566. static const IRPosition callsite_argument(AbstractCallSite ACS,
  567. unsigned ArgNo) {
  568. if (ACS.getNumArgOperands() <= ArgNo)
  569. return IRPosition();
  570. int CSArgNo = ACS.getCallArgOperandNo(ArgNo);
  571. if (CSArgNo >= 0)
  572. return IRPosition::callsite_argument(
  573. cast<CallBase>(*ACS.getInstruction()), CSArgNo);
  574. return IRPosition();
  575. }
  576. /// Create a position with function scope matching the "context" of \p IRP.
  577. /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
  578. /// will be a call site position, otherwise the function position of the
  579. /// associated function.
  580. static const IRPosition
  581. function_scope(const IRPosition &IRP,
  582. const CallBaseContext *CBContext = nullptr) {
  583. if (IRP.isAnyCallSitePosition()) {
  584. return IRPosition::callsite_function(
  585. cast<CallBase>(IRP.getAnchorValue()));
  586. }
  587. assert(IRP.getAssociatedFunction());
  588. return IRPosition::function(*IRP.getAssociatedFunction(), CBContext);
  589. }
  590. bool operator==(const IRPosition &RHS) const {
  591. return Enc == RHS.Enc && RHS.CBContext == CBContext;
  592. }
  593. bool operator!=(const IRPosition &RHS) const { return !(*this == RHS); }
  594. /// Return the value this abstract attribute is anchored with.
  595. ///
  596. /// The anchor value might not be the associated value if the latter is not
  597. /// sufficient to determine where arguments will be manifested. This is, so
  598. /// far, only the case for call site arguments as the value is not sufficient
  599. /// to pinpoint them. Instead, we can use the call site as an anchor.
  600. Value &getAnchorValue() const {
  601. switch (getEncodingBits()) {
  602. case ENC_VALUE:
  603. case ENC_RETURNED_VALUE:
  604. case ENC_FLOATING_FUNCTION:
  605. return *getAsValuePtr();
  606. case ENC_CALL_SITE_ARGUMENT_USE:
  607. return *(getAsUsePtr()->getUser());
  608. default:
  609. llvm_unreachable("Unkown encoding!");
  610. };
  611. }
  612. /// Return the associated function, if any.
  613. Function *getAssociatedFunction() const {
  614. if (auto *CB = dyn_cast<CallBase>(&getAnchorValue())) {
  615. // We reuse the logic that associates callback calles to arguments of a
  616. // call site here to identify the callback callee as the associated
  617. // function.
  618. if (Argument *Arg = getAssociatedArgument())
  619. return Arg->getParent();
  620. return CB->getCalledFunction();
  621. }
  622. return getAnchorScope();
  623. }
  624. /// Return the associated argument, if any.
  625. Argument *getAssociatedArgument() const;
  626. /// Return true if the position refers to a function interface, that is the
  627. /// function scope, the function return, or an argument.
  628. bool isFnInterfaceKind() const {
  629. switch (getPositionKind()) {
  630. case IRPosition::IRP_FUNCTION:
  631. case IRPosition::IRP_RETURNED:
  632. case IRPosition::IRP_ARGUMENT:
  633. return true;
  634. default:
  635. return false;
  636. }
  637. }
  638. /// Return the Function surrounding the anchor value.
  639. Function *getAnchorScope() const {
  640. Value &V = getAnchorValue();
  641. if (isa<Function>(V))
  642. return &cast<Function>(V);
  643. if (isa<Argument>(V))
  644. return cast<Argument>(V).getParent();
  645. if (isa<Instruction>(V))
  646. return cast<Instruction>(V).getFunction();
  647. return nullptr;
  648. }
  649. /// Return the context instruction, if any.
  650. Instruction *getCtxI() const {
  651. Value &V = getAnchorValue();
  652. if (auto *I = dyn_cast<Instruction>(&V))
  653. return I;
  654. if (auto *Arg = dyn_cast<Argument>(&V))
  655. if (!Arg->getParent()->isDeclaration())
  656. return &Arg->getParent()->getEntryBlock().front();
  657. if (auto *F = dyn_cast<Function>(&V))
  658. if (!F->isDeclaration())
  659. return &(F->getEntryBlock().front());
  660. return nullptr;
  661. }
  662. /// Return the value this abstract attribute is associated with.
  663. Value &getAssociatedValue() const {
  664. if (getCallSiteArgNo() < 0 || isa<Argument>(&getAnchorValue()))
  665. return getAnchorValue();
  666. assert(isa<CallBase>(&getAnchorValue()) && "Expected a call base!");
  667. return *cast<CallBase>(&getAnchorValue())
  668. ->getArgOperand(getCallSiteArgNo());
  669. }
  670. /// Return the type this abstract attribute is associated with.
  671. Type *getAssociatedType() const {
  672. if (getPositionKind() == IRPosition::IRP_RETURNED)
  673. return getAssociatedFunction()->getReturnType();
  674. return getAssociatedValue().getType();
  675. }
  676. /// Return the callee argument number of the associated value if it is an
  677. /// argument or call site argument, otherwise a negative value. In contrast to
  678. /// `getCallSiteArgNo` this method will always return the "argument number"
  679. /// from the perspective of the callee. This may not the same as the call site
  680. /// if this is a callback call.
  681. int getCalleeArgNo() const {
  682. return getArgNo(/* CallbackCalleeArgIfApplicable */ true);
  683. }
  684. /// Return the call site argument number of the associated value if it is an
  685. /// argument or call site argument, otherwise a negative value. In contrast to
  686. /// `getCalleArgNo` this method will always return the "operand number" from
  687. /// the perspective of the call site. This may not the same as the callee
  688. /// perspective if this is a callback call.
  689. int getCallSiteArgNo() const {
  690. return getArgNo(/* CallbackCalleeArgIfApplicable */ false);
  691. }
  692. /// Return the index in the attribute list for this position.
  693. unsigned getAttrIdx() const {
  694. switch (getPositionKind()) {
  695. case IRPosition::IRP_INVALID:
  696. case IRPosition::IRP_FLOAT:
  697. break;
  698. case IRPosition::IRP_FUNCTION:
  699. case IRPosition::IRP_CALL_SITE:
  700. return AttributeList::FunctionIndex;
  701. case IRPosition::IRP_RETURNED:
  702. case IRPosition::IRP_CALL_SITE_RETURNED:
  703. return AttributeList::ReturnIndex;
  704. case IRPosition::IRP_ARGUMENT:
  705. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  706. return getCallSiteArgNo() + AttributeList::FirstArgIndex;
  707. }
  708. llvm_unreachable(
  709. "There is no attribute index for a floating or invalid position!");
  710. }
  711. /// Return the associated position kind.
  712. Kind getPositionKind() const {
  713. char EncodingBits = getEncodingBits();
  714. if (EncodingBits == ENC_CALL_SITE_ARGUMENT_USE)
  715. return IRP_CALL_SITE_ARGUMENT;
  716. if (EncodingBits == ENC_FLOATING_FUNCTION)
  717. return IRP_FLOAT;
  718. Value *V = getAsValuePtr();
  719. if (!V)
  720. return IRP_INVALID;
  721. if (isa<Argument>(V))
  722. return IRP_ARGUMENT;
  723. if (isa<Function>(V))
  724. return isReturnPosition(EncodingBits) ? IRP_RETURNED : IRP_FUNCTION;
  725. if (isa<CallBase>(V))
  726. return isReturnPosition(EncodingBits) ? IRP_CALL_SITE_RETURNED
  727. : IRP_CALL_SITE;
  728. return IRP_FLOAT;
  729. }
  730. /// TODO: Figure out if the attribute related helper functions should live
  731. /// here or somewhere else.
  732. /// Return true if any kind in \p AKs existing in the IR at a position that
  733. /// will affect this one. See also getAttrs(...).
  734. /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
  735. /// e.g., the function position if this is an
  736. /// argument position, should be ignored.
  737. bool hasAttr(ArrayRef<Attribute::AttrKind> AKs,
  738. bool IgnoreSubsumingPositions = false,
  739. Attributor *A = nullptr) const;
  740. /// Return the attributes of any kind in \p AKs existing in the IR at a
  741. /// position that will affect this one. While each position can only have a
  742. /// single attribute of any kind in \p AKs, there are "subsuming" positions
  743. /// that could have an attribute as well. This method returns all attributes
  744. /// found in \p Attrs.
  745. /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
  746. /// e.g., the function position if this is an
  747. /// argument position, should be ignored.
  748. void getAttrs(ArrayRef<Attribute::AttrKind> AKs,
  749. SmallVectorImpl<Attribute> &Attrs,
  750. bool IgnoreSubsumingPositions = false,
  751. Attributor *A = nullptr) const;
  752. /// Remove the attribute of kind \p AKs existing in the IR at this position.
  753. void removeAttrs(ArrayRef<Attribute::AttrKind> AKs) const {
  754. if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT)
  755. return;
  756. AttributeList AttrList;
  757. auto *CB = dyn_cast<CallBase>(&getAnchorValue());
  758. if (CB)
  759. AttrList = CB->getAttributes();
  760. else
  761. AttrList = getAssociatedFunction()->getAttributes();
  762. LLVMContext &Ctx = getAnchorValue().getContext();
  763. for (Attribute::AttrKind AK : AKs)
  764. AttrList = AttrList.removeAttributeAtIndex(Ctx, getAttrIdx(), AK);
  765. if (CB)
  766. CB->setAttributes(AttrList);
  767. else
  768. getAssociatedFunction()->setAttributes(AttrList);
  769. }
  770. bool isAnyCallSitePosition() const {
  771. switch (getPositionKind()) {
  772. case IRPosition::IRP_CALL_SITE:
  773. case IRPosition::IRP_CALL_SITE_RETURNED:
  774. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  775. return true;
  776. default:
  777. return false;
  778. }
  779. }
  780. /// Return true if the position is an argument or call site argument.
  781. bool isArgumentPosition() const {
  782. switch (getPositionKind()) {
  783. case IRPosition::IRP_ARGUMENT:
  784. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  785. return true;
  786. default:
  787. return false;
  788. }
  789. }
  790. /// Return the same position without the call base context.
  791. IRPosition stripCallBaseContext() const {
  792. IRPosition Result = *this;
  793. Result.CBContext = nullptr;
  794. return Result;
  795. }
  796. /// Get the call base context from the position.
  797. const CallBaseContext *getCallBaseContext() const { return CBContext; }
  798. /// Check if the position has any call base context.
  799. bool hasCallBaseContext() const { return CBContext != nullptr; }
  800. /// Special DenseMap key values.
  801. ///
  802. ///{
  803. static const IRPosition EmptyKey;
  804. static const IRPosition TombstoneKey;
  805. ///}
  806. /// Conversion into a void * to allow reuse of pointer hashing.
  807. operator void *() const { return Enc.getOpaqueValue(); }
  808. private:
  809. /// Private constructor for special values only!
  810. explicit IRPosition(void *Ptr, const CallBaseContext *CBContext = nullptr)
  811. : CBContext(CBContext) {
  812. Enc.setFromOpaqueValue(Ptr);
  813. }
  814. /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
  815. explicit IRPosition(Value &AnchorVal, Kind PK,
  816. const CallBaseContext *CBContext = nullptr)
  817. : CBContext(CBContext) {
  818. switch (PK) {
  819. case IRPosition::IRP_INVALID:
  820. llvm_unreachable("Cannot create invalid IRP with an anchor value!");
  821. break;
  822. case IRPosition::IRP_FLOAT:
  823. // Special case for floating functions.
  824. if (isa<Function>(AnchorVal) || isa<CallBase>(AnchorVal))
  825. Enc = {&AnchorVal, ENC_FLOATING_FUNCTION};
  826. else
  827. Enc = {&AnchorVal, ENC_VALUE};
  828. break;
  829. case IRPosition::IRP_FUNCTION:
  830. case IRPosition::IRP_CALL_SITE:
  831. Enc = {&AnchorVal, ENC_VALUE};
  832. break;
  833. case IRPosition::IRP_RETURNED:
  834. case IRPosition::IRP_CALL_SITE_RETURNED:
  835. Enc = {&AnchorVal, ENC_RETURNED_VALUE};
  836. break;
  837. case IRPosition::IRP_ARGUMENT:
  838. Enc = {&AnchorVal, ENC_VALUE};
  839. break;
  840. case IRPosition::IRP_CALL_SITE_ARGUMENT:
  841. llvm_unreachable(
  842. "Cannot create call site argument IRP with an anchor value!");
  843. break;
  844. }
  845. verify();
  846. }
  847. /// Return the callee argument number of the associated value if it is an
  848. /// argument or call site argument. See also `getCalleeArgNo` and
  849. /// `getCallSiteArgNo`.
  850. int getArgNo(bool CallbackCalleeArgIfApplicable) const {
  851. if (CallbackCalleeArgIfApplicable)
  852. if (Argument *Arg = getAssociatedArgument())
  853. return Arg->getArgNo();
  854. switch (getPositionKind()) {
  855. case IRPosition::IRP_ARGUMENT:
  856. return cast<Argument>(getAsValuePtr())->getArgNo();
  857. case IRPosition::IRP_CALL_SITE_ARGUMENT: {
  858. Use &U = *getAsUsePtr();
  859. return cast<CallBase>(U.getUser())->getArgOperandNo(&U);
  860. }
  861. default:
  862. return -1;
  863. }
  864. }
  865. /// IRPosition for the use \p U. The position kind \p PK needs to be
  866. /// IRP_CALL_SITE_ARGUMENT, the anchor value is the user, the associated value
  867. /// the used value.
  868. explicit IRPosition(Use &U, Kind PK) {
  869. assert(PK == IRP_CALL_SITE_ARGUMENT &&
  870. "Use constructor is for call site arguments only!");
  871. Enc = {&U, ENC_CALL_SITE_ARGUMENT_USE};
  872. verify();
  873. }
  874. /// Verify internal invariants.
  875. void verify();
  876. /// Return the attributes of kind \p AK existing in the IR as attribute.
  877. bool getAttrsFromIRAttr(Attribute::AttrKind AK,
  878. SmallVectorImpl<Attribute> &Attrs) const;
  879. /// Return the attributes of kind \p AK existing in the IR as operand bundles
  880. /// of an llvm.assume.
  881. bool getAttrsFromAssumes(Attribute::AttrKind AK,
  882. SmallVectorImpl<Attribute> &Attrs,
  883. Attributor &A) const;
  884. /// Return the underlying pointer as Value *, valid for all positions but
  885. /// IRP_CALL_SITE_ARGUMENT.
  886. Value *getAsValuePtr() const {
  887. assert(getEncodingBits() != ENC_CALL_SITE_ARGUMENT_USE &&
  888. "Not a value pointer!");
  889. return reinterpret_cast<Value *>(Enc.getPointer());
  890. }
  891. /// Return the underlying pointer as Use *, valid only for
  892. /// IRP_CALL_SITE_ARGUMENT positions.
  893. Use *getAsUsePtr() const {
  894. assert(getEncodingBits() == ENC_CALL_SITE_ARGUMENT_USE &&
  895. "Not a value pointer!");
  896. return reinterpret_cast<Use *>(Enc.getPointer());
  897. }
  898. /// Return true if \p EncodingBits describe a returned or call site returned
  899. /// position.
  900. static bool isReturnPosition(char EncodingBits) {
  901. return EncodingBits == ENC_RETURNED_VALUE;
  902. }
  903. /// Return true if the encoding bits describe a returned or call site returned
  904. /// position.
  905. bool isReturnPosition() const { return isReturnPosition(getEncodingBits()); }
  906. /// The encoding of the IRPosition is a combination of a pointer and two
  907. /// encoding bits. The values of the encoding bits are defined in the enum
  908. /// below. The pointer is either a Value* (for the first three encoding bit
  909. /// combinations) or Use* (for ENC_CALL_SITE_ARGUMENT_USE).
  910. ///
  911. ///{
  912. enum {
  913. ENC_VALUE = 0b00,
  914. ENC_RETURNED_VALUE = 0b01,
  915. ENC_FLOATING_FUNCTION = 0b10,
  916. ENC_CALL_SITE_ARGUMENT_USE = 0b11,
  917. };
  918. // Reserve the maximal amount of bits so there is no need to mask out the
  919. // remaining ones. We will not encode anything else in the pointer anyway.
  920. static constexpr int NumEncodingBits =
  921. PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
  922. static_assert(NumEncodingBits >= 2, "At least two bits are required!");
  923. /// The pointer with the encoding bits.
  924. PointerIntPair<void *, NumEncodingBits, char> Enc;
  925. ///}
  926. /// Call base context. Used for callsite specific analysis.
  927. const CallBaseContext *CBContext = nullptr;
  928. /// Return the encoding bits.
  929. char getEncodingBits() const { return Enc.getInt(); }
  930. };
  931. /// Helper that allows IRPosition as a key in a DenseMap.
  932. template <> struct DenseMapInfo<IRPosition> {
  933. static inline IRPosition getEmptyKey() { return IRPosition::EmptyKey; }
  934. static inline IRPosition getTombstoneKey() {
  935. return IRPosition::TombstoneKey;
  936. }
  937. static unsigned getHashValue(const IRPosition &IRP) {
  938. return (DenseMapInfo<void *>::getHashValue(IRP) << 4) ^
  939. (DenseMapInfo<Value *>::getHashValue(IRP.getCallBaseContext()));
  940. }
  941. static bool isEqual(const IRPosition &a, const IRPosition &b) {
  942. return a == b;
  943. }
  944. };
  945. /// A visitor class for IR positions.
  946. ///
  947. /// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
  948. /// positions" wrt. attributes/information. Thus, if a piece of information
  949. /// holds for a subsuming position, it also holds for the position P.
  950. ///
  951. /// The subsuming positions always include the initial position and then,
  952. /// depending on the position kind, additionally the following ones:
  953. /// - for IRP_RETURNED:
  954. /// - the function (IRP_FUNCTION)
  955. /// - for IRP_ARGUMENT:
  956. /// - the function (IRP_FUNCTION)
  957. /// - for IRP_CALL_SITE:
  958. /// - the callee (IRP_FUNCTION), if known
  959. /// - for IRP_CALL_SITE_RETURNED:
  960. /// - the callee (IRP_RETURNED), if known
  961. /// - the call site (IRP_FUNCTION)
  962. /// - the callee (IRP_FUNCTION), if known
  963. /// - for IRP_CALL_SITE_ARGUMENT:
  964. /// - the argument of the callee (IRP_ARGUMENT), if known
  965. /// - the callee (IRP_FUNCTION), if known
  966. /// - the position the call site argument is associated with if it is not
  967. /// anchored to the call site, e.g., if it is an argument then the argument
  968. /// (IRP_ARGUMENT)
  969. class SubsumingPositionIterator {
  970. SmallVector<IRPosition, 4> IRPositions;
  971. using iterator = decltype(IRPositions)::iterator;
  972. public:
  973. SubsumingPositionIterator(const IRPosition &IRP);
  974. iterator begin() { return IRPositions.begin(); }
  975. iterator end() { return IRPositions.end(); }
  976. };
  977. /// Wrapper for FunctionAnalysisManager.
  978. struct AnalysisGetter {
  979. // The client may be running the old pass manager, in which case, we need to
  980. // map the requested Analysis to its equivalent wrapper in the old pass
  981. // manager. The scheme implemented here does not require every Analysis to be
  982. // updated. Only those new analyses that the client cares about in the old
  983. // pass manager need to expose a LegacyWrapper type, and that wrapper should
  984. // support a getResult() method that matches the new Analysis.
  985. //
  986. // We need SFINAE to check for the LegacyWrapper, but function templates don't
  987. // allow partial specialization, which is needed in this case. So instead, we
  988. // use a constexpr bool to perform the SFINAE, and then use this information
  989. // inside the function template.
  990. template <typename, typename = void> static constexpr bool HasLegacyWrapper = false;
  991. template <typename Analysis>
  992. typename Analysis::Result *getAnalysis(const Function &F) {
  993. if (FAM)
  994. return &FAM->getResult<Analysis>(const_cast<Function &>(F));
  995. if constexpr (HasLegacyWrapper<Analysis>)
  996. if (LegacyPass)
  997. return &LegacyPass
  998. ->getAnalysis<typename Analysis::LegacyWrapper>(
  999. const_cast<Function &>(F))
  1000. .getResult();
  1001. return nullptr;
  1002. }
  1003. AnalysisGetter(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
  1004. AnalysisGetter(Pass *P) : LegacyPass(P) {}
  1005. AnalysisGetter() = default;
  1006. private:
  1007. FunctionAnalysisManager *FAM = nullptr;
  1008. Pass *LegacyPass = nullptr;
  1009. };
  1010. template <typename Analysis>
  1011. constexpr bool AnalysisGetter::HasLegacyWrapper<
  1012. Analysis, std::void_t<typename Analysis::LegacyWrapper>> = true;
  1013. /// Data structure to hold cached (LLVM-IR) information.
  1014. ///
  1015. /// All attributes are given an InformationCache object at creation time to
  1016. /// avoid inspection of the IR by all of them individually. This default
  1017. /// InformationCache will hold information required by 'default' attributes,
  1018. /// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
  1019. /// is called.
  1020. ///
  1021. /// If custom abstract attributes, registered manually through
  1022. /// Attributor::registerAA(...), need more information, especially if it is not
  1023. /// reusable, it is advised to inherit from the InformationCache and cast the
  1024. /// instance down in the abstract attributes.
  1025. struct InformationCache {
  1026. InformationCache(const Module &M, AnalysisGetter &AG,
  1027. BumpPtrAllocator &Allocator, SetVector<Function *> *CGSCC)
  1028. : DL(M.getDataLayout()), Allocator(Allocator),
  1029. Explorer(
  1030. /* ExploreInterBlock */ true, /* ExploreCFGForward */ true,
  1031. /* ExploreCFGBackward */ true,
  1032. /* LIGetter */
  1033. [&](const Function &F) { return AG.getAnalysis<LoopAnalysis>(F); },
  1034. /* DTGetter */
  1035. [&](const Function &F) {
  1036. return AG.getAnalysis<DominatorTreeAnalysis>(F);
  1037. },
  1038. /* PDTGetter */
  1039. [&](const Function &F) {
  1040. return AG.getAnalysis<PostDominatorTreeAnalysis>(F);
  1041. }),
  1042. AG(AG), TargetTriple(M.getTargetTriple()) {
  1043. if (CGSCC)
  1044. initializeModuleSlice(*CGSCC);
  1045. }
  1046. ~InformationCache() {
  1047. // The FunctionInfo objects are allocated via a BumpPtrAllocator, we call
  1048. // the destructor manually.
  1049. for (auto &It : FuncInfoMap)
  1050. It.getSecond()->~FunctionInfo();
  1051. // Same is true for the instruction exclusions sets.
  1052. using AA::InstExclusionSetTy;
  1053. for (auto *BES : BESets)
  1054. BES->~InstExclusionSetTy();
  1055. }
  1056. /// Apply \p CB to all uses of \p F. If \p LookThroughConstantExprUses is
  1057. /// true, constant expression users are not given to \p CB but their uses are
  1058. /// traversed transitively.
  1059. template <typename CBTy>
  1060. static void foreachUse(Function &F, CBTy CB,
  1061. bool LookThroughConstantExprUses = true) {
  1062. SmallVector<Use *, 8> Worklist(make_pointer_range(F.uses()));
  1063. for (unsigned Idx = 0; Idx < Worklist.size(); ++Idx) {
  1064. Use &U = *Worklist[Idx];
  1065. // Allow use in constant bitcasts and simply look through them.
  1066. if (LookThroughConstantExprUses && isa<ConstantExpr>(U.getUser())) {
  1067. for (Use &CEU : cast<ConstantExpr>(U.getUser())->uses())
  1068. Worklist.push_back(&CEU);
  1069. continue;
  1070. }
  1071. CB(U);
  1072. }
  1073. }
  1074. /// Initialize the ModuleSlice member based on \p SCC. ModuleSlices contains
  1075. /// (a subset of) all functions that we can look at during this SCC traversal.
  1076. /// This includes functions (transitively) called from the SCC and the
  1077. /// (transitive) callers of SCC functions. We also can look at a function if
  1078. /// there is a "reference edge", i.a., if the function somehow uses (!=calls)
  1079. /// a function in the SCC or a caller of a function in the SCC.
  1080. void initializeModuleSlice(SetVector<Function *> &SCC) {
  1081. ModuleSlice.insert(SCC.begin(), SCC.end());
  1082. SmallPtrSet<Function *, 16> Seen;
  1083. SmallVector<Function *, 16> Worklist(SCC.begin(), SCC.end());
  1084. while (!Worklist.empty()) {
  1085. Function *F = Worklist.pop_back_val();
  1086. ModuleSlice.insert(F);
  1087. for (Instruction &I : instructions(*F))
  1088. if (auto *CB = dyn_cast<CallBase>(&I))
  1089. if (Function *Callee = CB->getCalledFunction())
  1090. if (Seen.insert(Callee).second)
  1091. Worklist.push_back(Callee);
  1092. }
  1093. Seen.clear();
  1094. Worklist.append(SCC.begin(), SCC.end());
  1095. while (!Worklist.empty()) {
  1096. Function *F = Worklist.pop_back_val();
  1097. ModuleSlice.insert(F);
  1098. // Traverse all transitive uses.
  1099. foreachUse(*F, [&](Use &U) {
  1100. if (auto *UsrI = dyn_cast<Instruction>(U.getUser()))
  1101. if (Seen.insert(UsrI->getFunction()).second)
  1102. Worklist.push_back(UsrI->getFunction());
  1103. });
  1104. }
  1105. }
  1106. /// The slice of the module we are allowed to look at.
  1107. SmallPtrSet<Function *, 8> ModuleSlice;
  1108. /// A vector type to hold instructions.
  1109. using InstructionVectorTy = SmallVector<Instruction *, 8>;
  1110. /// A map type from opcodes to instructions with this opcode.
  1111. using OpcodeInstMapTy = DenseMap<unsigned, InstructionVectorTy *>;
  1112. /// Return the map that relates "interesting" opcodes with all instructions
  1113. /// with that opcode in \p F.
  1114. OpcodeInstMapTy &getOpcodeInstMapForFunction(const Function &F) {
  1115. return getFunctionInfo(F).OpcodeInstMap;
  1116. }
  1117. /// Return the instructions in \p F that may read or write memory.
  1118. InstructionVectorTy &getReadOrWriteInstsForFunction(const Function &F) {
  1119. return getFunctionInfo(F).RWInsts;
  1120. }
  1121. /// Return MustBeExecutedContextExplorer
  1122. MustBeExecutedContextExplorer &getMustBeExecutedContextExplorer() {
  1123. return Explorer;
  1124. }
  1125. /// Return TargetLibraryInfo for function \p F.
  1126. TargetLibraryInfo *getTargetLibraryInfoForFunction(const Function &F) {
  1127. return AG.getAnalysis<TargetLibraryAnalysis>(F);
  1128. }
  1129. /// Return AliasAnalysis Result for function \p F.
  1130. AAResults *getAAResultsForFunction(const Function &F);
  1131. /// Return true if \p Arg is involved in a must-tail call, thus the argument
  1132. /// of the caller or callee.
  1133. bool isInvolvedInMustTailCall(const Argument &Arg) {
  1134. FunctionInfo &FI = getFunctionInfo(*Arg.getParent());
  1135. return FI.CalledViaMustTail || FI.ContainsMustTailCall;
  1136. }
  1137. bool isOnlyUsedByAssume(const Instruction &I) const {
  1138. return AssumeOnlyValues.contains(&I);
  1139. }
  1140. /// Return the analysis result from a pass \p AP for function \p F.
  1141. template <typename AP>
  1142. typename AP::Result *getAnalysisResultForFunction(const Function &F) {
  1143. return AG.getAnalysis<AP>(F);
  1144. }
  1145. /// Return datalayout used in the module.
  1146. const DataLayout &getDL() { return DL; }
  1147. /// Return the map conaining all the knowledge we have from `llvm.assume`s.
  1148. const RetainedKnowledgeMap &getKnowledgeMap() const { return KnowledgeMap; }
  1149. /// Given \p BES, return a uniqued version. \p BES is destroyed in the
  1150. /// process.
  1151. const AA::InstExclusionSetTy *
  1152. getOrCreateUniqueBlockExecutionSet(const AA::InstExclusionSetTy *BES) {
  1153. auto It = BESets.find(BES);
  1154. if (It != BESets.end())
  1155. return *It;
  1156. auto *UniqueBES = new (Allocator) AA::InstExclusionSetTy(*BES);
  1157. BESets.insert(UniqueBES);
  1158. return UniqueBES;
  1159. }
  1160. /// Check whether \p F is part of module slice.
  1161. bool isInModuleSlice(const Function &F) {
  1162. return ModuleSlice.empty() || ModuleSlice.count(const_cast<Function *>(&F));
  1163. }
  1164. /// Return true if the stack (llvm::Alloca) can be accessed by other threads.
  1165. bool stackIsAccessibleByOtherThreads() { return !targetIsGPU(); }
  1166. /// Return true if the target is a GPU.
  1167. bool targetIsGPU() {
  1168. return TargetTriple.isAMDGPU() || TargetTriple.isNVPTX();
  1169. }
  1170. private:
  1171. struct FunctionInfo {
  1172. ~FunctionInfo();
  1173. /// A nested map that remembers all instructions in a function with a
  1174. /// certain instruction opcode (Instruction::getOpcode()).
  1175. OpcodeInstMapTy OpcodeInstMap;
  1176. /// A map from functions to their instructions that may read or write
  1177. /// memory.
  1178. InstructionVectorTy RWInsts;
  1179. /// Function is called by a `musttail` call.
  1180. bool CalledViaMustTail;
  1181. /// Function contains a `musttail` call.
  1182. bool ContainsMustTailCall;
  1183. };
  1184. /// A map type from functions to informatio about it.
  1185. DenseMap<const Function *, FunctionInfo *> FuncInfoMap;
  1186. /// Return information about the function \p F, potentially by creating it.
  1187. FunctionInfo &getFunctionInfo(const Function &F) {
  1188. FunctionInfo *&FI = FuncInfoMap[&F];
  1189. if (!FI) {
  1190. FI = new (Allocator) FunctionInfo();
  1191. initializeInformationCache(F, *FI);
  1192. }
  1193. return *FI;
  1194. }
  1195. /// Initialize the function information cache \p FI for the function \p F.
  1196. ///
  1197. /// This method needs to be called for all function that might be looked at
  1198. /// through the information cache interface *prior* to looking at them.
  1199. void initializeInformationCache(const Function &F, FunctionInfo &FI);
  1200. /// The datalayout used in the module.
  1201. const DataLayout &DL;
  1202. /// The allocator used to allocate memory, e.g. for `FunctionInfo`s.
  1203. BumpPtrAllocator &Allocator;
  1204. /// MustBeExecutedContextExplorer
  1205. MustBeExecutedContextExplorer Explorer;
  1206. /// A map with knowledge retained in `llvm.assume` instructions.
  1207. RetainedKnowledgeMap KnowledgeMap;
  1208. /// A container for all instructions that are only used by `llvm.assume`.
  1209. SetVector<const Instruction *> AssumeOnlyValues;
  1210. /// Cache for block sets to allow reuse.
  1211. DenseSet<AA::InstExclusionSetTy *> BESets;
  1212. /// Getters for analysis.
  1213. AnalysisGetter &AG;
  1214. /// Set of inlineable functions
  1215. SmallPtrSet<const Function *, 8> InlineableFunctions;
  1216. /// The triple describing the target machine.
  1217. Triple TargetTriple;
  1218. /// Give the Attributor access to the members so
  1219. /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
  1220. friend struct Attributor;
  1221. };
  1222. /// Configuration for the Attributor.
  1223. struct AttributorConfig {
  1224. AttributorConfig(CallGraphUpdater &CGUpdater) : CGUpdater(CGUpdater) {}
  1225. /// Is the user of the Attributor a module pass or not. This determines what
  1226. /// IR we can look at and modify. If it is a module pass we might deduce facts
  1227. /// outside the initial function set and modify functions outside that set,
  1228. /// but only as part of the optimization of the functions in the initial
  1229. /// function set. For CGSCC passes we can look at the IR of the module slice
  1230. /// but never run any deduction, or perform any modification, outside the
  1231. /// initial function set (which we assume is the SCC).
  1232. bool IsModulePass = true;
  1233. /// Flag to determine if we can delete functions or keep dead ones around.
  1234. bool DeleteFns = true;
  1235. /// Flag to determine if we rewrite function signatures.
  1236. bool RewriteSignatures = true;
  1237. /// Flag to determine if we want to initialize all default AAs for an internal
  1238. /// function marked live. See also: InitializationCallback>
  1239. bool DefaultInitializeLiveInternals = true;
  1240. /// Callback function to be invoked on internal functions marked live.
  1241. std::function<void(Attributor &A, const Function &F)> InitializationCallback =
  1242. nullptr;
  1243. /// Helper to update an underlying call graph and to delete functions.
  1244. CallGraphUpdater &CGUpdater;
  1245. /// If not null, a set limiting the attribute opportunities.
  1246. DenseSet<const char *> *Allowed = nullptr;
  1247. /// Maximum number of iterations to run until fixpoint.
  1248. std::optional<unsigned> MaxFixpointIterations;
  1249. /// A callback function that returns an ORE object from a Function pointer.
  1250. ///{
  1251. using OptimizationRemarkGetter =
  1252. function_ref<OptimizationRemarkEmitter &(Function *)>;
  1253. OptimizationRemarkGetter OREGetter = nullptr;
  1254. ///}
  1255. /// The name of the pass running the attributor, used to emit remarks.
  1256. const char *PassName = nullptr;
  1257. };
  1258. /// The fixpoint analysis framework that orchestrates the attribute deduction.
  1259. ///
  1260. /// The Attributor provides a general abstract analysis framework (guided
  1261. /// fixpoint iteration) as well as helper functions for the deduction of
  1262. /// (LLVM-IR) attributes. However, also other code properties can be deduced,
  1263. /// propagated, and ultimately manifested through the Attributor framework. This
  1264. /// is particularly useful if these properties interact with attributes and a
  1265. /// co-scheduled deduction allows to improve the solution. Even if not, thus if
  1266. /// attributes/properties are completely isolated, they should use the
  1267. /// Attributor framework to reduce the number of fixpoint iteration frameworks
  1268. /// in the code base. Note that the Attributor design makes sure that isolated
  1269. /// attributes are not impacted, in any way, by others derived at the same time
  1270. /// if there is no cross-reasoning performed.
  1271. ///
  1272. /// The public facing interface of the Attributor is kept simple and basically
  1273. /// allows abstract attributes to one thing, query abstract attributes
  1274. /// in-flight. There are two reasons to do this:
  1275. /// a) The optimistic state of one abstract attribute can justify an
  1276. /// optimistic state of another, allowing to framework to end up with an
  1277. /// optimistic (=best possible) fixpoint instead of one based solely on
  1278. /// information in the IR.
  1279. /// b) This avoids reimplementing various kinds of lookups, e.g., to check
  1280. /// for existing IR attributes, in favor of a single lookups interface
  1281. /// provided by an abstract attribute subclass.
  1282. ///
  1283. /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
  1284. /// described in the file comment.
  1285. struct Attributor {
  1286. /// Constructor
  1287. ///
  1288. /// \param Functions The set of functions we are deriving attributes for.
  1289. /// \param InfoCache Cache to hold various information accessible for
  1290. /// the abstract attributes.
  1291. /// \param Configuration The Attributor configuration which determines what
  1292. /// generic features to use.
  1293. Attributor(SetVector<Function *> &Functions, InformationCache &InfoCache,
  1294. AttributorConfig Configuration)
  1295. : Allocator(InfoCache.Allocator), Functions(Functions),
  1296. InfoCache(InfoCache), Configuration(Configuration) {}
  1297. ~Attributor();
  1298. /// Run the analyses until a fixpoint is reached or enforced (timeout).
  1299. ///
  1300. /// The attributes registered with this Attributor can be used after as long
  1301. /// as the Attributor is not destroyed (it owns the attributes now).
  1302. ///
  1303. /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
  1304. ChangeStatus run();
  1305. /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
  1306. /// no abstract attribute is found equivalent positions are checked, see
  1307. /// SubsumingPositionIterator. Thus, the returned abstract attribute
  1308. /// might be anchored at a different position, e.g., the callee if \p IRP is a
  1309. /// call base.
  1310. ///
  1311. /// This method is the only (supported) way an abstract attribute can retrieve
  1312. /// information from another abstract attribute. As an example, take an
  1313. /// abstract attribute that determines the memory access behavior for a
  1314. /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
  1315. /// most optimistic information for other abstract attributes in-flight, e.g.
  1316. /// the one reasoning about the "captured" state for the argument or the one
  1317. /// reasoning on the memory access behavior of the function as a whole.
  1318. ///
  1319. /// If the DepClass enum is set to `DepClassTy::None` the dependence from
  1320. /// \p QueryingAA to the return abstract attribute is not automatically
  1321. /// recorded. This should only be used if the caller will record the
  1322. /// dependence explicitly if necessary, thus if it the returned abstract
  1323. /// attribute is used for reasoning. To record the dependences explicitly use
  1324. /// the `Attributor::recordDependence` method.
  1325. template <typename AAType>
  1326. const AAType &getAAFor(const AbstractAttribute &QueryingAA,
  1327. const IRPosition &IRP, DepClassTy DepClass) {
  1328. return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
  1329. /* ForceUpdate */ false);
  1330. }
  1331. /// Similar to getAAFor but the return abstract attribute will be updated (via
  1332. /// `AbstractAttribute::update`) even if it is found in the cache. This is
  1333. /// especially useful for AAIsDead as changes in liveness can make updates
  1334. /// possible/useful that were not happening before as the abstract attribute
  1335. /// was assumed dead.
  1336. template <typename AAType>
  1337. const AAType &getAndUpdateAAFor(const AbstractAttribute &QueryingAA,
  1338. const IRPosition &IRP, DepClassTy DepClass) {
  1339. return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
  1340. /* ForceUpdate */ true);
  1341. }
  1342. /// The version of getAAFor that allows to omit a querying abstract
  1343. /// attribute. Using this after Attributor started running is restricted to
  1344. /// only the Attributor itself. Initial seeding of AAs can be done via this
  1345. /// function.
  1346. /// NOTE: ForceUpdate is ignored in any stage other than the update stage.
  1347. template <typename AAType>
  1348. const AAType &getOrCreateAAFor(IRPosition IRP,
  1349. const AbstractAttribute *QueryingAA,
  1350. DepClassTy DepClass, bool ForceUpdate = false,
  1351. bool UpdateAfterInit = true) {
  1352. if (!shouldPropagateCallBaseContext(IRP))
  1353. IRP = IRP.stripCallBaseContext();
  1354. if (AAType *AAPtr = lookupAAFor<AAType>(IRP, QueryingAA, DepClass,
  1355. /* AllowInvalidState */ true)) {
  1356. if (ForceUpdate && Phase == AttributorPhase::UPDATE)
  1357. updateAA(*AAPtr);
  1358. return *AAPtr;
  1359. }
  1360. // No matching attribute found, create one.
  1361. // Use the static create method.
  1362. auto &AA = AAType::createForPosition(IRP, *this);
  1363. // Always register a new attribute to make sure we clean up the allocated
  1364. // memory properly.
  1365. registerAA(AA);
  1366. // If we are currenty seeding attributes, enforce seeding rules.
  1367. if (Phase == AttributorPhase::SEEDING && !shouldSeedAttribute(AA)) {
  1368. AA.getState().indicatePessimisticFixpoint();
  1369. return AA;
  1370. }
  1371. // For now we ignore naked and optnone functions.
  1372. bool Invalidate =
  1373. Configuration.Allowed && !Configuration.Allowed->count(&AAType::ID);
  1374. const Function *AnchorFn = IRP.getAnchorScope();
  1375. if (AnchorFn) {
  1376. Invalidate |=
  1377. AnchorFn->hasFnAttribute(Attribute::Naked) ||
  1378. AnchorFn->hasFnAttribute(Attribute::OptimizeNone) ||
  1379. (!isModulePass() && !getInfoCache().isInModuleSlice(*AnchorFn));
  1380. }
  1381. // Avoid too many nested initializations to prevent a stack overflow.
  1382. Invalidate |= InitializationChainLength > MaxInitializationChainLength;
  1383. // Bootstrap the new attribute with an initial update to propagate
  1384. // information, e.g., function -> call site. If it is not on a given
  1385. // Allowed we will not perform updates at all.
  1386. if (Invalidate) {
  1387. AA.getState().indicatePessimisticFixpoint();
  1388. return AA;
  1389. }
  1390. {
  1391. TimeTraceScope TimeScope(AA.getName() + "::initialize");
  1392. ++InitializationChainLength;
  1393. AA.initialize(*this);
  1394. --InitializationChainLength;
  1395. }
  1396. // We update only AAs associated with functions in the Functions set or
  1397. // call sites of them.
  1398. if ((AnchorFn && !isRunOn(const_cast<Function *>(AnchorFn))) &&
  1399. !isRunOn(IRP.getAssociatedFunction())) {
  1400. AA.getState().indicatePessimisticFixpoint();
  1401. return AA;
  1402. }
  1403. // If this is queried in the manifest stage, we force the AA to indicate
  1404. // pessimistic fixpoint immediately.
  1405. if (Phase == AttributorPhase::MANIFEST ||
  1406. Phase == AttributorPhase::CLEANUP) {
  1407. AA.getState().indicatePessimisticFixpoint();
  1408. return AA;
  1409. }
  1410. // Allow seeded attributes to declare dependencies.
  1411. // Remember the seeding state.
  1412. if (UpdateAfterInit) {
  1413. AttributorPhase OldPhase = Phase;
  1414. Phase = AttributorPhase::UPDATE;
  1415. updateAA(AA);
  1416. Phase = OldPhase;
  1417. }
  1418. if (QueryingAA && AA.getState().isValidState())
  1419. recordDependence(AA, const_cast<AbstractAttribute &>(*QueryingAA),
  1420. DepClass);
  1421. return AA;
  1422. }
  1423. template <typename AAType>
  1424. const AAType &getOrCreateAAFor(const IRPosition &IRP) {
  1425. return getOrCreateAAFor<AAType>(IRP, /* QueryingAA */ nullptr,
  1426. DepClassTy::NONE);
  1427. }
  1428. /// Return the attribute of \p AAType for \p IRP if existing and valid. This
  1429. /// also allows non-AA users lookup.
  1430. template <typename AAType>
  1431. AAType *lookupAAFor(const IRPosition &IRP,
  1432. const AbstractAttribute *QueryingAA = nullptr,
  1433. DepClassTy DepClass = DepClassTy::OPTIONAL,
  1434. bool AllowInvalidState = false) {
  1435. static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
  1436. "Cannot query an attribute with a type not derived from "
  1437. "'AbstractAttribute'!");
  1438. // Lookup the abstract attribute of type AAType. If found, return it after
  1439. // registering a dependence of QueryingAA on the one returned attribute.
  1440. AbstractAttribute *AAPtr = AAMap.lookup({&AAType::ID, IRP});
  1441. if (!AAPtr)
  1442. return nullptr;
  1443. AAType *AA = static_cast<AAType *>(AAPtr);
  1444. // Do not register a dependence on an attribute with an invalid state.
  1445. if (DepClass != DepClassTy::NONE && QueryingAA &&
  1446. AA->getState().isValidState())
  1447. recordDependence(*AA, const_cast<AbstractAttribute &>(*QueryingAA),
  1448. DepClass);
  1449. // Return nullptr if this attribute has an invalid state.
  1450. if (!AllowInvalidState && !AA->getState().isValidState())
  1451. return nullptr;
  1452. return AA;
  1453. }
  1454. /// Allows a query AA to request an update if a new query was received.
  1455. void registerForUpdate(AbstractAttribute &AA);
  1456. /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
  1457. /// \p FromAA changes \p ToAA should be updated as well.
  1458. ///
  1459. /// This method should be used in conjunction with the `getAAFor` method and
  1460. /// with the DepClass enum passed to the method set to None. This can
  1461. /// be beneficial to avoid false dependences but it requires the users of
  1462. /// `getAAFor` to explicitly record true dependences through this method.
  1463. /// The \p DepClass flag indicates if the dependence is striclty necessary.
  1464. /// That means for required dependences, if \p FromAA changes to an invalid
  1465. /// state, \p ToAA can be moved to a pessimistic fixpoint because it required
  1466. /// information from \p FromAA but none are available anymore.
  1467. void recordDependence(const AbstractAttribute &FromAA,
  1468. const AbstractAttribute &ToAA, DepClassTy DepClass);
  1469. /// Introduce a new abstract attribute into the fixpoint analysis.
  1470. ///
  1471. /// Note that ownership of the attribute is given to the Attributor. It will
  1472. /// invoke delete for the Attributor on destruction of the Attributor.
  1473. ///
  1474. /// Attributes are identified by their IR position (AAType::getIRPosition())
  1475. /// and the address of their static member (see AAType::ID).
  1476. template <typename AAType> AAType &registerAA(AAType &AA) {
  1477. static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
  1478. "Cannot register an attribute with a type not derived from "
  1479. "'AbstractAttribute'!");
  1480. // Put the attribute in the lookup map structure and the container we use to
  1481. // keep track of all attributes.
  1482. const IRPosition &IRP = AA.getIRPosition();
  1483. AbstractAttribute *&AAPtr = AAMap[{&AAType::ID, IRP}];
  1484. assert(!AAPtr && "Attribute already in map!");
  1485. AAPtr = &AA;
  1486. // Register AA with the synthetic root only before the manifest stage.
  1487. if (Phase == AttributorPhase::SEEDING || Phase == AttributorPhase::UPDATE)
  1488. DG.SyntheticRoot.Deps.push_back(
  1489. AADepGraphNode::DepTy(&AA, unsigned(DepClassTy::REQUIRED)));
  1490. return AA;
  1491. }
  1492. /// Return the internal information cache.
  1493. InformationCache &getInfoCache() { return InfoCache; }
  1494. /// Return true if this is a module pass, false otherwise.
  1495. bool isModulePass() const { return Configuration.IsModulePass; }
  1496. /// Return true if we derive attributes for \p Fn
  1497. bool isRunOn(Function &Fn) const { return isRunOn(&Fn); }
  1498. bool isRunOn(Function *Fn) const {
  1499. return Functions.empty() || Functions.count(Fn);
  1500. }
  1501. /// Determine opportunities to derive 'default' attributes in \p F and create
  1502. /// abstract attribute objects for them.
  1503. ///
  1504. /// \param F The function that is checked for attribute opportunities.
  1505. ///
  1506. /// Note that abstract attribute instances are generally created even if the
  1507. /// IR already contains the information they would deduce. The most important
  1508. /// reason for this is the single interface, the one of the abstract attribute
  1509. /// instance, which can be queried without the need to look at the IR in
  1510. /// various places.
  1511. void identifyDefaultAbstractAttributes(Function &F);
  1512. /// Determine whether the function \p F is IPO amendable
  1513. ///
  1514. /// If a function is exactly defined or it has alwaysinline attribute
  1515. /// and is viable to be inlined, we say it is IPO amendable
  1516. bool isFunctionIPOAmendable(const Function &F) {
  1517. return F.hasExactDefinition() || InfoCache.InlineableFunctions.count(&F);
  1518. }
  1519. /// Mark the internal function \p F as live.
  1520. ///
  1521. /// This will trigger the identification and initialization of attributes for
  1522. /// \p F.
  1523. void markLiveInternalFunction(const Function &F) {
  1524. assert(F.hasLocalLinkage() &&
  1525. "Only local linkage is assumed dead initially.");
  1526. if (Configuration.DefaultInitializeLiveInternals)
  1527. identifyDefaultAbstractAttributes(const_cast<Function &>(F));
  1528. if (Configuration.InitializationCallback)
  1529. Configuration.InitializationCallback(*this, F);
  1530. }
  1531. /// Helper function to remove callsite.
  1532. void removeCallSite(CallInst *CI) {
  1533. if (!CI)
  1534. return;
  1535. Configuration.CGUpdater.removeCallSite(*CI);
  1536. }
  1537. /// Record that \p U is to be replaces with \p NV after information was
  1538. /// manifested. This also triggers deletion of trivially dead istructions.
  1539. bool changeUseAfterManifest(Use &U, Value &NV) {
  1540. Value *&V = ToBeChangedUses[&U];
  1541. if (V && (V->stripPointerCasts() == NV.stripPointerCasts() ||
  1542. isa_and_nonnull<UndefValue>(V)))
  1543. return false;
  1544. assert((!V || V == &NV || isa<UndefValue>(NV)) &&
  1545. "Use was registered twice for replacement with different values!");
  1546. V = &NV;
  1547. return true;
  1548. }
  1549. /// Helper function to replace all uses associated with \p IRP with \p NV.
  1550. /// Return true if there is any change. The flag \p ChangeDroppable indicates
  1551. /// if dropppable uses should be changed too.
  1552. bool changeAfterManifest(const IRPosition IRP, Value &NV,
  1553. bool ChangeDroppable = true) {
  1554. if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE_ARGUMENT) {
  1555. auto *CB = cast<CallBase>(IRP.getCtxI());
  1556. return changeUseAfterManifest(
  1557. CB->getArgOperandUse(IRP.getCallSiteArgNo()), NV);
  1558. }
  1559. Value &V = IRP.getAssociatedValue();
  1560. auto &Entry = ToBeChangedValues[&V];
  1561. Value *CurNV = get<0>(Entry);
  1562. if (CurNV && (CurNV->stripPointerCasts() == NV.stripPointerCasts() ||
  1563. isa<UndefValue>(CurNV)))
  1564. return false;
  1565. assert((!CurNV || CurNV == &NV || isa<UndefValue>(NV)) &&
  1566. "Value replacement was registered twice with different values!");
  1567. Entry = {&NV, ChangeDroppable};
  1568. return true;
  1569. }
  1570. /// Record that \p I is to be replaced with `unreachable` after information
  1571. /// was manifested.
  1572. void changeToUnreachableAfterManifest(Instruction *I) {
  1573. ToBeChangedToUnreachableInsts.insert(I);
  1574. }
  1575. /// Record that \p II has at least one dead successor block. This information
  1576. /// is used, e.g., to replace \p II with a call, after information was
  1577. /// manifested.
  1578. void registerInvokeWithDeadSuccessor(InvokeInst &II) {
  1579. InvokeWithDeadSuccessor.insert(&II);
  1580. }
  1581. /// Record that \p I is deleted after information was manifested. This also
  1582. /// triggers deletion of trivially dead istructions.
  1583. void deleteAfterManifest(Instruction &I) { ToBeDeletedInsts.insert(&I); }
  1584. /// Record that \p BB is deleted after information was manifested. This also
  1585. /// triggers deletion of trivially dead istructions.
  1586. void deleteAfterManifest(BasicBlock &BB) { ToBeDeletedBlocks.insert(&BB); }
  1587. // Record that \p BB is added during the manifest of an AA. Added basic blocks
  1588. // are preserved in the IR.
  1589. void registerManifestAddedBasicBlock(BasicBlock &BB) {
  1590. ManifestAddedBlocks.insert(&BB);
  1591. }
  1592. /// Record that \p F is deleted after information was manifested.
  1593. void deleteAfterManifest(Function &F) {
  1594. if (Configuration.DeleteFns)
  1595. ToBeDeletedFunctions.insert(&F);
  1596. }
  1597. /// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
  1598. /// return std::nullopt, otherwise return `nullptr`.
  1599. std::optional<Constant *> getAssumedConstant(const IRPosition &IRP,
  1600. const AbstractAttribute &AA,
  1601. bool &UsedAssumedInformation);
  1602. std::optional<Constant *> getAssumedConstant(const Value &V,
  1603. const AbstractAttribute &AA,
  1604. bool &UsedAssumedInformation) {
  1605. return getAssumedConstant(IRPosition::value(V), AA, UsedAssumedInformation);
  1606. }
  1607. /// If \p V is assumed simplified, return it, if it is unclear yet,
  1608. /// return std::nullopt, otherwise return `nullptr`.
  1609. std::optional<Value *> getAssumedSimplified(const IRPosition &IRP,
  1610. const AbstractAttribute &AA,
  1611. bool &UsedAssumedInformation,
  1612. AA::ValueScope S) {
  1613. return getAssumedSimplified(IRP, &AA, UsedAssumedInformation, S);
  1614. }
  1615. std::optional<Value *> getAssumedSimplified(const Value &V,
  1616. const AbstractAttribute &AA,
  1617. bool &UsedAssumedInformation,
  1618. AA::ValueScope S) {
  1619. return getAssumedSimplified(IRPosition::value(V), AA,
  1620. UsedAssumedInformation, S);
  1621. }
  1622. /// If \p V is assumed simplified, return it, if it is unclear yet,
  1623. /// return std::nullopt, otherwise return `nullptr`. Same as the public
  1624. /// version except that it can be used without recording dependences on any \p
  1625. /// AA.
  1626. std::optional<Value *> getAssumedSimplified(const IRPosition &V,
  1627. const AbstractAttribute *AA,
  1628. bool &UsedAssumedInformation,
  1629. AA::ValueScope S);
  1630. /// Try to simplify \p IRP and in the scope \p S. If successful, true is
  1631. /// returned and all potential values \p IRP can take are put into \p Values.
  1632. /// If the result in \p Values contains select or PHI instructions it means
  1633. /// those could not be simplified to a single value. Recursive calls with
  1634. /// these instructions will yield their respective potential values. If false
  1635. /// is returned no other information is valid.
  1636. bool getAssumedSimplifiedValues(const IRPosition &IRP,
  1637. const AbstractAttribute *AA,
  1638. SmallVectorImpl<AA::ValueAndContext> &Values,
  1639. AA::ValueScope S,
  1640. bool &UsedAssumedInformation);
  1641. /// Register \p CB as a simplification callback.
  1642. /// `Attributor::getAssumedSimplified` will use these callbacks before
  1643. /// we it will ask `AAValueSimplify`. It is important to ensure this
  1644. /// is called before `identifyDefaultAbstractAttributes`, assuming the
  1645. /// latter is called at all.
  1646. using SimplifictionCallbackTy = std::function<std::optional<Value *>(
  1647. const IRPosition &, const AbstractAttribute *, bool &)>;
  1648. void registerSimplificationCallback(const IRPosition &IRP,
  1649. const SimplifictionCallbackTy &CB) {
  1650. SimplificationCallbacks[IRP].emplace_back(CB);
  1651. }
  1652. /// Return true if there is a simplification callback for \p IRP.
  1653. bool hasSimplificationCallback(const IRPosition &IRP) {
  1654. return SimplificationCallbacks.count(IRP);
  1655. }
  1656. using VirtualUseCallbackTy =
  1657. std::function<bool(Attributor &, const AbstractAttribute *)>;
  1658. void registerVirtualUseCallback(const Value &V,
  1659. const VirtualUseCallbackTy &CB) {
  1660. VirtualUseCallbacks[&V].emplace_back(CB);
  1661. }
  1662. private:
  1663. /// The vector with all simplification callbacks registered by outside AAs.
  1664. DenseMap<IRPosition, SmallVector<SimplifictionCallbackTy, 1>>
  1665. SimplificationCallbacks;
  1666. DenseMap<const Value *, SmallVector<VirtualUseCallbackTy, 1>>
  1667. VirtualUseCallbacks;
  1668. public:
  1669. /// Translate \p V from the callee context into the call site context.
  1670. std::optional<Value *>
  1671. translateArgumentToCallSiteContent(std::optional<Value *> V, CallBase &CB,
  1672. const AbstractAttribute &AA,
  1673. bool &UsedAssumedInformation);
  1674. /// Return true if \p AA (or its context instruction) is assumed dead.
  1675. ///
  1676. /// If \p LivenessAA is not provided it is queried.
  1677. bool isAssumedDead(const AbstractAttribute &AA, const AAIsDead *LivenessAA,
  1678. bool &UsedAssumedInformation,
  1679. bool CheckBBLivenessOnly = false,
  1680. DepClassTy DepClass = DepClassTy::OPTIONAL);
  1681. /// Return true if \p I is assumed dead.
  1682. ///
  1683. /// If \p LivenessAA is not provided it is queried.
  1684. bool isAssumedDead(const Instruction &I, const AbstractAttribute *QueryingAA,
  1685. const AAIsDead *LivenessAA, bool &UsedAssumedInformation,
  1686. bool CheckBBLivenessOnly = false,
  1687. DepClassTy DepClass = DepClassTy::OPTIONAL,
  1688. bool CheckForDeadStore = false);
  1689. /// Return true if \p U is assumed dead.
  1690. ///
  1691. /// If \p FnLivenessAA is not provided it is queried.
  1692. bool isAssumedDead(const Use &U, const AbstractAttribute *QueryingAA,
  1693. const AAIsDead *FnLivenessAA, bool &UsedAssumedInformation,
  1694. bool CheckBBLivenessOnly = false,
  1695. DepClassTy DepClass = DepClassTy::OPTIONAL);
  1696. /// Return true if \p IRP is assumed dead.
  1697. ///
  1698. /// If \p FnLivenessAA is not provided it is queried.
  1699. bool isAssumedDead(const IRPosition &IRP, const AbstractAttribute *QueryingAA,
  1700. const AAIsDead *FnLivenessAA, bool &UsedAssumedInformation,
  1701. bool CheckBBLivenessOnly = false,
  1702. DepClassTy DepClass = DepClassTy::OPTIONAL);
  1703. /// Return true if \p BB is assumed dead.
  1704. ///
  1705. /// If \p LivenessAA is not provided it is queried.
  1706. bool isAssumedDead(const BasicBlock &BB, const AbstractAttribute *QueryingAA,
  1707. const AAIsDead *FnLivenessAA,
  1708. DepClassTy DepClass = DepClassTy::OPTIONAL);
  1709. /// Check \p Pred on all (transitive) uses of \p V.
  1710. ///
  1711. /// This method will evaluate \p Pred on all (transitive) uses of the
  1712. /// associated value and return true if \p Pred holds every time.
  1713. /// If uses are skipped in favor of equivalent ones, e.g., if we look through
  1714. /// memory, the \p EquivalentUseCB will be used to give the caller an idea
  1715. /// what original used was replaced by a new one (or new ones). The visit is
  1716. /// cut short if \p EquivalentUseCB returns false and the function will return
  1717. /// false as well.
  1718. bool checkForAllUses(function_ref<bool(const Use &, bool &)> Pred,
  1719. const AbstractAttribute &QueryingAA, const Value &V,
  1720. bool CheckBBLivenessOnly = false,
  1721. DepClassTy LivenessDepClass = DepClassTy::OPTIONAL,
  1722. bool IgnoreDroppableUses = true,
  1723. function_ref<bool(const Use &OldU, const Use &NewU)>
  1724. EquivalentUseCB = nullptr);
  1725. /// Emit a remark generically.
  1726. ///
  1727. /// This template function can be used to generically emit a remark. The
  1728. /// RemarkKind should be one of the following:
  1729. /// - OptimizationRemark to indicate a successful optimization attempt
  1730. /// - OptimizationRemarkMissed to report a failed optimization attempt
  1731. /// - OptimizationRemarkAnalysis to provide additional information about an
  1732. /// optimization attempt
  1733. ///
  1734. /// The remark is built using a callback function \p RemarkCB that takes a
  1735. /// RemarkKind as input and returns a RemarkKind.
  1736. template <typename RemarkKind, typename RemarkCallBack>
  1737. void emitRemark(Instruction *I, StringRef RemarkName,
  1738. RemarkCallBack &&RemarkCB) const {
  1739. if (!Configuration.OREGetter)
  1740. return;
  1741. Function *F = I->getFunction();
  1742. auto &ORE = Configuration.OREGetter(F);
  1743. if (RemarkName.startswith("OMP"))
  1744. ORE.emit([&]() {
  1745. return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I))
  1746. << " [" << RemarkName << "]";
  1747. });
  1748. else
  1749. ORE.emit([&]() {
  1750. return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I));
  1751. });
  1752. }
  1753. /// Emit a remark on a function.
  1754. template <typename RemarkKind, typename RemarkCallBack>
  1755. void emitRemark(Function *F, StringRef RemarkName,
  1756. RemarkCallBack &&RemarkCB) const {
  1757. if (!Configuration.OREGetter)
  1758. return;
  1759. auto &ORE = Configuration.OREGetter(F);
  1760. if (RemarkName.startswith("OMP"))
  1761. ORE.emit([&]() {
  1762. return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F))
  1763. << " [" << RemarkName << "]";
  1764. });
  1765. else
  1766. ORE.emit([&]() {
  1767. return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F));
  1768. });
  1769. }
  1770. /// Helper struct used in the communication between an abstract attribute (AA)
  1771. /// that wants to change the signature of a function and the Attributor which
  1772. /// applies the changes. The struct is partially initialized with the
  1773. /// information from the AA (see the constructor). All other members are
  1774. /// provided by the Attributor prior to invoking any callbacks.
  1775. struct ArgumentReplacementInfo {
  1776. /// Callee repair callback type
  1777. ///
  1778. /// The function repair callback is invoked once to rewire the replacement
  1779. /// arguments in the body of the new function. The argument replacement info
  1780. /// is passed, as build from the registerFunctionSignatureRewrite call, as
  1781. /// well as the replacement function and an iteratore to the first
  1782. /// replacement argument.
  1783. using CalleeRepairCBTy = std::function<void(
  1784. const ArgumentReplacementInfo &, Function &, Function::arg_iterator)>;
  1785. /// Abstract call site (ACS) repair callback type
  1786. ///
  1787. /// The abstract call site repair callback is invoked once on every abstract
  1788. /// call site of the replaced function (\see ReplacedFn). The callback needs
  1789. /// to provide the operands for the call to the new replacement function.
  1790. /// The number and type of the operands appended to the provided vector
  1791. /// (second argument) is defined by the number and types determined through
  1792. /// the replacement type vector (\see ReplacementTypes). The first argument
  1793. /// is the ArgumentReplacementInfo object registered with the Attributor
  1794. /// through the registerFunctionSignatureRewrite call.
  1795. using ACSRepairCBTy =
  1796. std::function<void(const ArgumentReplacementInfo &, AbstractCallSite,
  1797. SmallVectorImpl<Value *> &)>;
  1798. /// Simple getters, see the corresponding members for details.
  1799. ///{
  1800. Attributor &getAttributor() const { return A; }
  1801. const Function &getReplacedFn() const { return ReplacedFn; }
  1802. const Argument &getReplacedArg() const { return ReplacedArg; }
  1803. unsigned getNumReplacementArgs() const { return ReplacementTypes.size(); }
  1804. const SmallVectorImpl<Type *> &getReplacementTypes() const {
  1805. return ReplacementTypes;
  1806. }
  1807. ///}
  1808. private:
  1809. /// Constructor that takes the argument to be replaced, the types of
  1810. /// the replacement arguments, as well as callbacks to repair the call sites
  1811. /// and new function after the replacement happened.
  1812. ArgumentReplacementInfo(Attributor &A, Argument &Arg,
  1813. ArrayRef<Type *> ReplacementTypes,
  1814. CalleeRepairCBTy &&CalleeRepairCB,
  1815. ACSRepairCBTy &&ACSRepairCB)
  1816. : A(A), ReplacedFn(*Arg.getParent()), ReplacedArg(Arg),
  1817. ReplacementTypes(ReplacementTypes.begin(), ReplacementTypes.end()),
  1818. CalleeRepairCB(std::move(CalleeRepairCB)),
  1819. ACSRepairCB(std::move(ACSRepairCB)) {}
  1820. /// Reference to the attributor to allow access from the callbacks.
  1821. Attributor &A;
  1822. /// The "old" function replaced by ReplacementFn.
  1823. const Function &ReplacedFn;
  1824. /// The "old" argument replaced by new ones defined via ReplacementTypes.
  1825. const Argument &ReplacedArg;
  1826. /// The types of the arguments replacing ReplacedArg.
  1827. const SmallVector<Type *, 8> ReplacementTypes;
  1828. /// Callee repair callback, see CalleeRepairCBTy.
  1829. const CalleeRepairCBTy CalleeRepairCB;
  1830. /// Abstract call site (ACS) repair callback, see ACSRepairCBTy.
  1831. const ACSRepairCBTy ACSRepairCB;
  1832. /// Allow access to the private members from the Attributor.
  1833. friend struct Attributor;
  1834. };
  1835. /// Check if we can rewrite a function signature.
  1836. ///
  1837. /// The argument \p Arg is replaced with new ones defined by the number,
  1838. /// order, and types in \p ReplacementTypes.
  1839. ///
  1840. /// \returns True, if the replacement can be registered, via
  1841. /// registerFunctionSignatureRewrite, false otherwise.
  1842. bool isValidFunctionSignatureRewrite(Argument &Arg,
  1843. ArrayRef<Type *> ReplacementTypes);
  1844. /// Register a rewrite for a function signature.
  1845. ///
  1846. /// The argument \p Arg is replaced with new ones defined by the number,
  1847. /// order, and types in \p ReplacementTypes. The rewiring at the call sites is
  1848. /// done through \p ACSRepairCB and at the callee site through
  1849. /// \p CalleeRepairCB.
  1850. ///
  1851. /// \returns True, if the replacement was registered, false otherwise.
  1852. bool registerFunctionSignatureRewrite(
  1853. Argument &Arg, ArrayRef<Type *> ReplacementTypes,
  1854. ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
  1855. ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB);
  1856. /// Check \p Pred on all function call sites.
  1857. ///
  1858. /// This method will evaluate \p Pred on call sites and return
  1859. /// true if \p Pred holds in every call sites. However, this is only possible
  1860. /// all call sites are known, hence the function has internal linkage.
  1861. /// If true is returned, \p UsedAssumedInformation is set if assumed
  1862. /// information was used to skip or simplify potential call sites.
  1863. bool checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
  1864. const AbstractAttribute &QueryingAA,
  1865. bool RequireAllCallSites,
  1866. bool &UsedAssumedInformation);
  1867. /// Check \p Pred on all call sites of \p Fn.
  1868. ///
  1869. /// This method will evaluate \p Pred on call sites and return
  1870. /// true if \p Pred holds in every call sites. However, this is only possible
  1871. /// all call sites are known, hence the function has internal linkage.
  1872. /// If true is returned, \p UsedAssumedInformation is set if assumed
  1873. /// information was used to skip or simplify potential call sites.
  1874. bool checkForAllCallSites(function_ref<bool(AbstractCallSite)> Pred,
  1875. const Function &Fn, bool RequireAllCallSites,
  1876. const AbstractAttribute *QueryingAA,
  1877. bool &UsedAssumedInformation,
  1878. bool CheckPotentiallyDead = false);
  1879. /// Check \p Pred on all values potentially returned by \p F.
  1880. ///
  1881. /// This method will evaluate \p Pred on all values potentially returned by
  1882. /// the function associated with \p QueryingAA. The returned values are
  1883. /// matched with their respective return instructions. Returns true if \p Pred
  1884. /// holds on all of them.
  1885. bool checkForAllReturnedValuesAndReturnInsts(
  1886. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
  1887. const AbstractAttribute &QueryingAA);
  1888. /// Check \p Pred on all values potentially returned by the function
  1889. /// associated with \p QueryingAA.
  1890. ///
  1891. /// This is the context insensitive version of the method above.
  1892. bool checkForAllReturnedValues(function_ref<bool(Value &)> Pred,
  1893. const AbstractAttribute &QueryingAA);
  1894. /// Check \p Pred on all instructions in \p Fn with an opcode present in
  1895. /// \p Opcodes.
  1896. ///
  1897. /// This method will evaluate \p Pred on all instructions with an opcode
  1898. /// present in \p Opcode and return true if \p Pred holds on all of them.
  1899. bool checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
  1900. const Function *Fn,
  1901. const AbstractAttribute &QueryingAA,
  1902. const ArrayRef<unsigned> &Opcodes,
  1903. bool &UsedAssumedInformation,
  1904. bool CheckBBLivenessOnly = false,
  1905. bool CheckPotentiallyDead = false);
  1906. /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
  1907. ///
  1908. /// This method will evaluate \p Pred on all instructions with an opcode
  1909. /// present in \p Opcode and return true if \p Pred holds on all of them.
  1910. bool checkForAllInstructions(function_ref<bool(Instruction &)> Pred,
  1911. const AbstractAttribute &QueryingAA,
  1912. const ArrayRef<unsigned> &Opcodes,
  1913. bool &UsedAssumedInformation,
  1914. bool CheckBBLivenessOnly = false,
  1915. bool CheckPotentiallyDead = false);
  1916. /// Check \p Pred on all call-like instructions (=CallBased derived).
  1917. ///
  1918. /// See checkForAllCallLikeInstructions(...) for more information.
  1919. bool checkForAllCallLikeInstructions(function_ref<bool(Instruction &)> Pred,
  1920. const AbstractAttribute &QueryingAA,
  1921. bool &UsedAssumedInformation,
  1922. bool CheckBBLivenessOnly = false,
  1923. bool CheckPotentiallyDead = false) {
  1924. return checkForAllInstructions(
  1925. Pred, QueryingAA,
  1926. {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
  1927. (unsigned)Instruction::Call},
  1928. UsedAssumedInformation, CheckBBLivenessOnly, CheckPotentiallyDead);
  1929. }
  1930. /// Check \p Pred on all Read/Write instructions.
  1931. ///
  1932. /// This method will evaluate \p Pred on all instructions that read or write
  1933. /// to memory present in the information cache and return true if \p Pred
  1934. /// holds on all of them.
  1935. bool checkForAllReadWriteInstructions(function_ref<bool(Instruction &)> Pred,
  1936. AbstractAttribute &QueryingAA,
  1937. bool &UsedAssumedInformation);
  1938. /// Create a shallow wrapper for \p F such that \p F has internal linkage
  1939. /// afterwards. It also sets the original \p F 's name to anonymous
  1940. ///
  1941. /// A wrapper is a function with the same type (and attributes) as \p F
  1942. /// that will only call \p F and return the result, if any.
  1943. ///
  1944. /// Assuming the declaration of looks like:
  1945. /// rty F(aty0 arg0, ..., atyN argN);
  1946. ///
  1947. /// The wrapper will then look as follows:
  1948. /// rty wrapper(aty0 arg0, ..., atyN argN) {
  1949. /// return F(arg0, ..., argN);
  1950. /// }
  1951. ///
  1952. static void createShallowWrapper(Function &F);
  1953. /// Returns true if the function \p F can be internalized. i.e. it has a
  1954. /// compatible linkage.
  1955. static bool isInternalizable(Function &F);
  1956. /// Make another copy of the function \p F such that the copied version has
  1957. /// internal linkage afterwards and can be analysed. Then we replace all uses
  1958. /// of the original function to the copied one
  1959. ///
  1960. /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
  1961. /// linkage can be internalized because these linkages guarantee that other
  1962. /// definitions with the same name have the same semantics as this one.
  1963. ///
  1964. /// This will only be run if the `attributor-allow-deep-wrappers` option is
  1965. /// set, or if the function is called with \p Force set to true.
  1966. ///
  1967. /// If the function \p F failed to be internalized the return value will be a
  1968. /// null pointer.
  1969. static Function *internalizeFunction(Function &F, bool Force = false);
  1970. /// Make copies of each function in the set \p FnSet such that the copied
  1971. /// version has internal linkage afterwards and can be analysed. Then we
  1972. /// replace all uses of the original function to the copied one. The map
  1973. /// \p FnMap contains a mapping of functions to their internalized versions.
  1974. ///
  1975. /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
  1976. /// linkage can be internalized because these linkages guarantee that other
  1977. /// definitions with the same name have the same semantics as this one.
  1978. ///
  1979. /// This version will internalize all the functions in the set \p FnSet at
  1980. /// once and then replace the uses. This prevents internalized functions being
  1981. /// called by external functions when there is an internalized version in the
  1982. /// module.
  1983. static bool internalizeFunctions(SmallPtrSetImpl<Function *> &FnSet,
  1984. DenseMap<Function *, Function *> &FnMap);
  1985. /// Return the data layout associated with the anchor scope.
  1986. const DataLayout &getDataLayout() const { return InfoCache.DL; }
  1987. /// The allocator used to allocate memory, e.g. for `AbstractAttribute`s.
  1988. BumpPtrAllocator &Allocator;
  1989. private:
  1990. /// This method will do fixpoint iteration until fixpoint or the
  1991. /// maximum iteration count is reached.
  1992. ///
  1993. /// If the maximum iteration count is reached, This method will
  1994. /// indicate pessimistic fixpoint on attributes that transitively depend
  1995. /// on attributes that were scheduled for an update.
  1996. void runTillFixpoint();
  1997. /// Gets called after scheduling, manifests attributes to the LLVM IR.
  1998. ChangeStatus manifestAttributes();
  1999. /// Gets called after attributes have been manifested, cleans up the IR.
  2000. /// Deletes dead functions, blocks and instructions.
  2001. /// Rewrites function signitures and updates the call graph.
  2002. ChangeStatus cleanupIR();
  2003. /// Identify internal functions that are effectively dead, thus not reachable
  2004. /// from a live entry point. The functions are added to ToBeDeletedFunctions.
  2005. void identifyDeadInternalFunctions();
  2006. /// Run `::update` on \p AA and track the dependences queried while doing so.
  2007. /// Also adjust the state if we know further updates are not necessary.
  2008. ChangeStatus updateAA(AbstractAttribute &AA);
  2009. /// Remember the dependences on the top of the dependence stack such that they
  2010. /// may trigger further updates. (\see DependenceStack)
  2011. void rememberDependences();
  2012. /// Determine if CallBase context in \p IRP should be propagated.
  2013. bool shouldPropagateCallBaseContext(const IRPosition &IRP);
  2014. /// Apply all requested function signature rewrites
  2015. /// (\see registerFunctionSignatureRewrite) and return Changed if the module
  2016. /// was altered.
  2017. ChangeStatus
  2018. rewriteFunctionSignatures(SmallSetVector<Function *, 8> &ModifiedFns);
  2019. /// Check if the Attribute \p AA should be seeded.
  2020. /// See getOrCreateAAFor.
  2021. bool shouldSeedAttribute(AbstractAttribute &AA);
  2022. /// A nested map to lookup abstract attributes based on the argument position
  2023. /// on the outer level, and the addresses of the static member (AAType::ID) on
  2024. /// the inner level.
  2025. ///{
  2026. using AAMapKeyTy = std::pair<const char *, IRPosition>;
  2027. DenseMap<AAMapKeyTy, AbstractAttribute *> AAMap;
  2028. ///}
  2029. /// Map to remember all requested signature changes (= argument replacements).
  2030. DenseMap<Function *, SmallVector<std::unique_ptr<ArgumentReplacementInfo>, 8>>
  2031. ArgumentReplacementMap;
  2032. /// The set of functions we are deriving attributes for.
  2033. SetVector<Function *> &Functions;
  2034. /// The information cache that holds pre-processed (LLVM-IR) information.
  2035. InformationCache &InfoCache;
  2036. /// Abstract Attribute dependency graph
  2037. AADepGraph DG;
  2038. /// Set of functions for which we modified the content such that it might
  2039. /// impact the call graph.
  2040. SmallSetVector<Function *, 8> CGModifiedFunctions;
  2041. /// Information about a dependence. If FromAA is changed ToAA needs to be
  2042. /// updated as well.
  2043. struct DepInfo {
  2044. const AbstractAttribute *FromAA;
  2045. const AbstractAttribute *ToAA;
  2046. DepClassTy DepClass;
  2047. };
  2048. /// The dependence stack is used to track dependences during an
  2049. /// `AbstractAttribute::update` call. As `AbstractAttribute::update` can be
  2050. /// recursive we might have multiple vectors of dependences in here. The stack
  2051. /// size, should be adjusted according to the expected recursion depth and the
  2052. /// inner dependence vector size to the expected number of dependences per
  2053. /// abstract attribute. Since the inner vectors are actually allocated on the
  2054. /// stack we can be generous with their size.
  2055. using DependenceVector = SmallVector<DepInfo, 8>;
  2056. SmallVector<DependenceVector *, 16> DependenceStack;
  2057. /// A set to remember the functions we already assume to be live and visited.
  2058. DenseSet<const Function *> VisitedFunctions;
  2059. /// Uses we replace with a new value after manifest is done. We will remove
  2060. /// then trivially dead instructions as well.
  2061. SmallMapVector<Use *, Value *, 32> ToBeChangedUses;
  2062. /// Values we replace with a new value after manifest is done. We will remove
  2063. /// then trivially dead instructions as well.
  2064. SmallMapVector<Value *, PointerIntPair<Value *, 1, bool>, 32>
  2065. ToBeChangedValues;
  2066. /// Instructions we replace with `unreachable` insts after manifest is done.
  2067. SmallSetVector<WeakVH, 16> ToBeChangedToUnreachableInsts;
  2068. /// Invoke instructions with at least a single dead successor block.
  2069. SmallSetVector<WeakVH, 16> InvokeWithDeadSuccessor;
  2070. /// A flag that indicates which stage of the process we are in. Initially, the
  2071. /// phase is SEEDING. Phase is changed in `Attributor::run()`
  2072. enum class AttributorPhase {
  2073. SEEDING,
  2074. UPDATE,
  2075. MANIFEST,
  2076. CLEANUP,
  2077. } Phase = AttributorPhase::SEEDING;
  2078. /// The current initialization chain length. Tracked to avoid stack overflows.
  2079. unsigned InitializationChainLength = 0;
  2080. /// Functions, blocks, and instructions we delete after manifest is done.
  2081. ///
  2082. ///{
  2083. SmallPtrSet<BasicBlock *, 8> ManifestAddedBlocks;
  2084. SmallSetVector<Function *, 8> ToBeDeletedFunctions;
  2085. SmallSetVector<BasicBlock *, 8> ToBeDeletedBlocks;
  2086. SmallSetVector<WeakVH, 8> ToBeDeletedInsts;
  2087. ///}
  2088. /// Container with all the query AAs that requested an update via
  2089. /// registerForUpdate.
  2090. SmallSetVector<AbstractAttribute *, 16> QueryAAsAwaitingUpdate;
  2091. /// User provided configuration for this Attributor instance.
  2092. const AttributorConfig Configuration;
  2093. friend AADepGraph;
  2094. friend AttributorCallGraph;
  2095. };
  2096. /// An interface to query the internal state of an abstract attribute.
  2097. ///
  2098. /// The abstract state is a minimal interface that allows the Attributor to
  2099. /// communicate with the abstract attributes about their internal state without
  2100. /// enforcing or exposing implementation details, e.g., the (existence of an)
  2101. /// underlying lattice.
  2102. ///
  2103. /// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
  2104. /// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
  2105. /// was reached or (4) a pessimistic fixpoint was enforced.
  2106. ///
  2107. /// All methods need to be implemented by the subclass. For the common use case,
  2108. /// a single boolean state or a bit-encoded state, the BooleanState and
  2109. /// {Inc,Dec,Bit}IntegerState classes are already provided. An abstract
  2110. /// attribute can inherit from them to get the abstract state interface and
  2111. /// additional methods to directly modify the state based if needed. See the
  2112. /// class comments for help.
  2113. struct AbstractState {
  2114. virtual ~AbstractState() = default;
  2115. /// Return if this abstract state is in a valid state. If false, no
  2116. /// information provided should be used.
  2117. virtual bool isValidState() const = 0;
  2118. /// Return if this abstract state is fixed, thus does not need to be updated
  2119. /// if information changes as it cannot change itself.
  2120. virtual bool isAtFixpoint() const = 0;
  2121. /// Indicate that the abstract state should converge to the optimistic state.
  2122. ///
  2123. /// This will usually make the optimistically assumed state the known to be
  2124. /// true state.
  2125. ///
  2126. /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
  2127. virtual ChangeStatus indicateOptimisticFixpoint() = 0;
  2128. /// Indicate that the abstract state should converge to the pessimistic state.
  2129. ///
  2130. /// This will usually revert the optimistically assumed state to the known to
  2131. /// be true state.
  2132. ///
  2133. /// \returns ChangeStatus::CHANGED as the assumed value may change.
  2134. virtual ChangeStatus indicatePessimisticFixpoint() = 0;
  2135. };
  2136. /// Simple state with integers encoding.
  2137. ///
  2138. /// The interface ensures that the assumed bits are always a subset of the known
  2139. /// bits. Users can only add known bits and, except through adding known bits,
  2140. /// they can only remove assumed bits. This should guarantee monotoniticy and
  2141. /// thereby the existence of a fixpoint (if used corretly). The fixpoint is
  2142. /// reached when the assumed and known state/bits are equal. Users can
  2143. /// force/inidicate a fixpoint. If an optimistic one is indicated, the known
  2144. /// state will catch up with the assumed one, for a pessimistic fixpoint it is
  2145. /// the other way around.
  2146. template <typename base_ty, base_ty BestState, base_ty WorstState>
  2147. struct IntegerStateBase : public AbstractState {
  2148. using base_t = base_ty;
  2149. IntegerStateBase() = default;
  2150. IntegerStateBase(base_t Assumed) : Assumed(Assumed) {}
  2151. /// Return the best possible representable state.
  2152. static constexpr base_t getBestState() { return BestState; }
  2153. static constexpr base_t getBestState(const IntegerStateBase &) {
  2154. return getBestState();
  2155. }
  2156. /// Return the worst possible representable state.
  2157. static constexpr base_t getWorstState() { return WorstState; }
  2158. static constexpr base_t getWorstState(const IntegerStateBase &) {
  2159. return getWorstState();
  2160. }
  2161. /// See AbstractState::isValidState()
  2162. /// NOTE: For now we simply pretend that the worst possible state is invalid.
  2163. bool isValidState() const override { return Assumed != getWorstState(); }
  2164. /// See AbstractState::isAtFixpoint()
  2165. bool isAtFixpoint() const override { return Assumed == Known; }
  2166. /// See AbstractState::indicateOptimisticFixpoint(...)
  2167. ChangeStatus indicateOptimisticFixpoint() override {
  2168. Known = Assumed;
  2169. return ChangeStatus::UNCHANGED;
  2170. }
  2171. /// See AbstractState::indicatePessimisticFixpoint(...)
  2172. ChangeStatus indicatePessimisticFixpoint() override {
  2173. Assumed = Known;
  2174. return ChangeStatus::CHANGED;
  2175. }
  2176. /// Return the known state encoding
  2177. base_t getKnown() const { return Known; }
  2178. /// Return the assumed state encoding.
  2179. base_t getAssumed() const { return Assumed; }
  2180. /// Equality for IntegerStateBase.
  2181. bool
  2182. operator==(const IntegerStateBase<base_t, BestState, WorstState> &R) const {
  2183. return this->getAssumed() == R.getAssumed() &&
  2184. this->getKnown() == R.getKnown();
  2185. }
  2186. /// Inequality for IntegerStateBase.
  2187. bool
  2188. operator!=(const IntegerStateBase<base_t, BestState, WorstState> &R) const {
  2189. return !(*this == R);
  2190. }
  2191. /// "Clamp" this state with \p R. The result is subtype dependent but it is
  2192. /// intended that only information assumed in both states will be assumed in
  2193. /// this one afterwards.
  2194. void operator^=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
  2195. handleNewAssumedValue(R.getAssumed());
  2196. }
  2197. /// "Clamp" this state with \p R. The result is subtype dependent but it is
  2198. /// intended that information known in either state will be known in
  2199. /// this one afterwards.
  2200. void operator+=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
  2201. handleNewKnownValue(R.getKnown());
  2202. }
  2203. void operator|=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
  2204. joinOR(R.getAssumed(), R.getKnown());
  2205. }
  2206. void operator&=(const IntegerStateBase<base_t, BestState, WorstState> &R) {
  2207. joinAND(R.getAssumed(), R.getKnown());
  2208. }
  2209. protected:
  2210. /// Handle a new assumed value \p Value. Subtype dependent.
  2211. virtual void handleNewAssumedValue(base_t Value) = 0;
  2212. /// Handle a new known value \p Value. Subtype dependent.
  2213. virtual void handleNewKnownValue(base_t Value) = 0;
  2214. /// Handle a value \p Value. Subtype dependent.
  2215. virtual void joinOR(base_t AssumedValue, base_t KnownValue) = 0;
  2216. /// Handle a new assumed value \p Value. Subtype dependent.
  2217. virtual void joinAND(base_t AssumedValue, base_t KnownValue) = 0;
  2218. /// The known state encoding in an integer of type base_t.
  2219. base_t Known = getWorstState();
  2220. /// The assumed state encoding in an integer of type base_t.
  2221. base_t Assumed = getBestState();
  2222. };
  2223. /// Specialization of the integer state for a bit-wise encoding.
  2224. template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
  2225. base_ty WorstState = 0>
  2226. struct BitIntegerState
  2227. : public IntegerStateBase<base_ty, BestState, WorstState> {
  2228. using base_t = base_ty;
  2229. /// Return true if the bits set in \p BitsEncoding are "known bits".
  2230. bool isKnown(base_t BitsEncoding) const {
  2231. return (this->Known & BitsEncoding) == BitsEncoding;
  2232. }
  2233. /// Return true if the bits set in \p BitsEncoding are "assumed bits".
  2234. bool isAssumed(base_t BitsEncoding) const {
  2235. return (this->Assumed & BitsEncoding) == BitsEncoding;
  2236. }
  2237. /// Add the bits in \p BitsEncoding to the "known bits".
  2238. BitIntegerState &addKnownBits(base_t Bits) {
  2239. // Make sure we never miss any "known bits".
  2240. this->Assumed |= Bits;
  2241. this->Known |= Bits;
  2242. return *this;
  2243. }
  2244. /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
  2245. BitIntegerState &removeAssumedBits(base_t BitsEncoding) {
  2246. return intersectAssumedBits(~BitsEncoding);
  2247. }
  2248. /// Remove the bits in \p BitsEncoding from the "known bits".
  2249. BitIntegerState &removeKnownBits(base_t BitsEncoding) {
  2250. this->Known = (this->Known & ~BitsEncoding);
  2251. return *this;
  2252. }
  2253. /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
  2254. BitIntegerState &intersectAssumedBits(base_t BitsEncoding) {
  2255. // Make sure we never loose any "known bits".
  2256. this->Assumed = (this->Assumed & BitsEncoding) | this->Known;
  2257. return *this;
  2258. }
  2259. private:
  2260. void handleNewAssumedValue(base_t Value) override {
  2261. intersectAssumedBits(Value);
  2262. }
  2263. void handleNewKnownValue(base_t Value) override { addKnownBits(Value); }
  2264. void joinOR(base_t AssumedValue, base_t KnownValue) override {
  2265. this->Known |= KnownValue;
  2266. this->Assumed |= AssumedValue;
  2267. }
  2268. void joinAND(base_t AssumedValue, base_t KnownValue) override {
  2269. this->Known &= KnownValue;
  2270. this->Assumed &= AssumedValue;
  2271. }
  2272. };
  2273. /// Specialization of the integer state for an increasing value, hence ~0u is
  2274. /// the best state and 0 the worst.
  2275. template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
  2276. base_ty WorstState = 0>
  2277. struct IncIntegerState
  2278. : public IntegerStateBase<base_ty, BestState, WorstState> {
  2279. using super = IntegerStateBase<base_ty, BestState, WorstState>;
  2280. using base_t = base_ty;
  2281. IncIntegerState() : super() {}
  2282. IncIntegerState(base_t Assumed) : super(Assumed) {}
  2283. /// Return the best possible representable state.
  2284. static constexpr base_t getBestState() { return BestState; }
  2285. static constexpr base_t
  2286. getBestState(const IncIntegerState<base_ty, BestState, WorstState> &) {
  2287. return getBestState();
  2288. }
  2289. /// Take minimum of assumed and \p Value.
  2290. IncIntegerState &takeAssumedMinimum(base_t Value) {
  2291. // Make sure we never loose "known value".
  2292. this->Assumed = std::max(std::min(this->Assumed, Value), this->Known);
  2293. return *this;
  2294. }
  2295. /// Take maximum of known and \p Value.
  2296. IncIntegerState &takeKnownMaximum(base_t Value) {
  2297. // Make sure we never loose "known value".
  2298. this->Assumed = std::max(Value, this->Assumed);
  2299. this->Known = std::max(Value, this->Known);
  2300. return *this;
  2301. }
  2302. private:
  2303. void handleNewAssumedValue(base_t Value) override {
  2304. takeAssumedMinimum(Value);
  2305. }
  2306. void handleNewKnownValue(base_t Value) override { takeKnownMaximum(Value); }
  2307. void joinOR(base_t AssumedValue, base_t KnownValue) override {
  2308. this->Known = std::max(this->Known, KnownValue);
  2309. this->Assumed = std::max(this->Assumed, AssumedValue);
  2310. }
  2311. void joinAND(base_t AssumedValue, base_t KnownValue) override {
  2312. this->Known = std::min(this->Known, KnownValue);
  2313. this->Assumed = std::min(this->Assumed, AssumedValue);
  2314. }
  2315. };
  2316. /// Specialization of the integer state for a decreasing value, hence 0 is the
  2317. /// best state and ~0u the worst.
  2318. template <typename base_ty = uint32_t>
  2319. struct DecIntegerState : public IntegerStateBase<base_ty, 0, ~base_ty(0)> {
  2320. using base_t = base_ty;
  2321. /// Take maximum of assumed and \p Value.
  2322. DecIntegerState &takeAssumedMaximum(base_t Value) {
  2323. // Make sure we never loose "known value".
  2324. this->Assumed = std::min(std::max(this->Assumed, Value), this->Known);
  2325. return *this;
  2326. }
  2327. /// Take minimum of known and \p Value.
  2328. DecIntegerState &takeKnownMinimum(base_t Value) {
  2329. // Make sure we never loose "known value".
  2330. this->Assumed = std::min(Value, this->Assumed);
  2331. this->Known = std::min(Value, this->Known);
  2332. return *this;
  2333. }
  2334. private:
  2335. void handleNewAssumedValue(base_t Value) override {
  2336. takeAssumedMaximum(Value);
  2337. }
  2338. void handleNewKnownValue(base_t Value) override { takeKnownMinimum(Value); }
  2339. void joinOR(base_t AssumedValue, base_t KnownValue) override {
  2340. this->Assumed = std::min(this->Assumed, KnownValue);
  2341. this->Assumed = std::min(this->Assumed, AssumedValue);
  2342. }
  2343. void joinAND(base_t AssumedValue, base_t KnownValue) override {
  2344. this->Assumed = std::max(this->Assumed, KnownValue);
  2345. this->Assumed = std::max(this->Assumed, AssumedValue);
  2346. }
  2347. };
  2348. /// Simple wrapper for a single bit (boolean) state.
  2349. struct BooleanState : public IntegerStateBase<bool, true, false> {
  2350. using super = IntegerStateBase<bool, true, false>;
  2351. using base_t = IntegerStateBase::base_t;
  2352. BooleanState() = default;
  2353. BooleanState(base_t Assumed) : super(Assumed) {}
  2354. /// Set the assumed value to \p Value but never below the known one.
  2355. void setAssumed(bool Value) { Assumed &= (Known | Value); }
  2356. /// Set the known and asssumed value to \p Value.
  2357. void setKnown(bool Value) {
  2358. Known |= Value;
  2359. Assumed |= Value;
  2360. }
  2361. /// Return true if the state is assumed to hold.
  2362. bool isAssumed() const { return getAssumed(); }
  2363. /// Return true if the state is known to hold.
  2364. bool isKnown() const { return getKnown(); }
  2365. private:
  2366. void handleNewAssumedValue(base_t Value) override {
  2367. if (!Value)
  2368. Assumed = Known;
  2369. }
  2370. void handleNewKnownValue(base_t Value) override {
  2371. if (Value)
  2372. Known = (Assumed = Value);
  2373. }
  2374. void joinOR(base_t AssumedValue, base_t KnownValue) override {
  2375. Known |= KnownValue;
  2376. Assumed |= AssumedValue;
  2377. }
  2378. void joinAND(base_t AssumedValue, base_t KnownValue) override {
  2379. Known &= KnownValue;
  2380. Assumed &= AssumedValue;
  2381. }
  2382. };
  2383. /// State for an integer range.
  2384. struct IntegerRangeState : public AbstractState {
  2385. /// Bitwidth of the associated value.
  2386. uint32_t BitWidth;
  2387. /// State representing assumed range, initially set to empty.
  2388. ConstantRange Assumed;
  2389. /// State representing known range, initially set to [-inf, inf].
  2390. ConstantRange Known;
  2391. IntegerRangeState(uint32_t BitWidth)
  2392. : BitWidth(BitWidth), Assumed(ConstantRange::getEmpty(BitWidth)),
  2393. Known(ConstantRange::getFull(BitWidth)) {}
  2394. IntegerRangeState(const ConstantRange &CR)
  2395. : BitWidth(CR.getBitWidth()), Assumed(CR),
  2396. Known(getWorstState(CR.getBitWidth())) {}
  2397. /// Return the worst possible representable state.
  2398. static ConstantRange getWorstState(uint32_t BitWidth) {
  2399. return ConstantRange::getFull(BitWidth);
  2400. }
  2401. /// Return the best possible representable state.
  2402. static ConstantRange getBestState(uint32_t BitWidth) {
  2403. return ConstantRange::getEmpty(BitWidth);
  2404. }
  2405. static ConstantRange getBestState(const IntegerRangeState &IRS) {
  2406. return getBestState(IRS.getBitWidth());
  2407. }
  2408. /// Return associated values' bit width.
  2409. uint32_t getBitWidth() const { return BitWidth; }
  2410. /// See AbstractState::isValidState()
  2411. bool isValidState() const override {
  2412. return BitWidth > 0 && !Assumed.isFullSet();
  2413. }
  2414. /// See AbstractState::isAtFixpoint()
  2415. bool isAtFixpoint() const override { return Assumed == Known; }
  2416. /// See AbstractState::indicateOptimisticFixpoint(...)
  2417. ChangeStatus indicateOptimisticFixpoint() override {
  2418. Known = Assumed;
  2419. return ChangeStatus::CHANGED;
  2420. }
  2421. /// See AbstractState::indicatePessimisticFixpoint(...)
  2422. ChangeStatus indicatePessimisticFixpoint() override {
  2423. Assumed = Known;
  2424. return ChangeStatus::CHANGED;
  2425. }
  2426. /// Return the known state encoding
  2427. ConstantRange getKnown() const { return Known; }
  2428. /// Return the assumed state encoding.
  2429. ConstantRange getAssumed() const { return Assumed; }
  2430. /// Unite assumed range with the passed state.
  2431. void unionAssumed(const ConstantRange &R) {
  2432. // Don't loose a known range.
  2433. Assumed = Assumed.unionWith(R).intersectWith(Known);
  2434. }
  2435. /// See IntegerRangeState::unionAssumed(..).
  2436. void unionAssumed(const IntegerRangeState &R) {
  2437. unionAssumed(R.getAssumed());
  2438. }
  2439. /// Intersect known range with the passed state.
  2440. void intersectKnown(const ConstantRange &R) {
  2441. Assumed = Assumed.intersectWith(R);
  2442. Known = Known.intersectWith(R);
  2443. }
  2444. /// See IntegerRangeState::intersectKnown(..).
  2445. void intersectKnown(const IntegerRangeState &R) {
  2446. intersectKnown(R.getKnown());
  2447. }
  2448. /// Equality for IntegerRangeState.
  2449. bool operator==(const IntegerRangeState &R) const {
  2450. return getAssumed() == R.getAssumed() && getKnown() == R.getKnown();
  2451. }
  2452. /// "Clamp" this state with \p R. The result is subtype dependent but it is
  2453. /// intended that only information assumed in both states will be assumed in
  2454. /// this one afterwards.
  2455. IntegerRangeState operator^=(const IntegerRangeState &R) {
  2456. // NOTE: `^=` operator seems like `intersect` but in this case, we need to
  2457. // take `union`.
  2458. unionAssumed(R);
  2459. return *this;
  2460. }
  2461. IntegerRangeState operator&=(const IntegerRangeState &R) {
  2462. // NOTE: `&=` operator seems like `intersect` but in this case, we need to
  2463. // take `union`.
  2464. Known = Known.unionWith(R.getKnown());
  2465. Assumed = Assumed.unionWith(R.getAssumed());
  2466. return *this;
  2467. }
  2468. };
  2469. /// Simple state for a set.
  2470. ///
  2471. /// This represents a state containing a set of values. The interface supports
  2472. /// modelling sets that contain all possible elements. The state's internal
  2473. /// value is modified using union or intersection operations.
  2474. template <typename BaseTy> struct SetState : public AbstractState {
  2475. /// A wrapper around a set that has semantics for handling unions and
  2476. /// intersections with a "universal" set that contains all elements.
  2477. struct SetContents {
  2478. /// Creates a universal set with no concrete elements or an empty set.
  2479. SetContents(bool Universal) : Universal(Universal) {}
  2480. /// Creates a non-universal set with concrete values.
  2481. SetContents(const DenseSet<BaseTy> &Assumptions)
  2482. : Universal(false), Set(Assumptions) {}
  2483. SetContents(bool Universal, const DenseSet<BaseTy> &Assumptions)
  2484. : Universal(Universal), Set(Assumptions) {}
  2485. const DenseSet<BaseTy> &getSet() const { return Set; }
  2486. bool isUniversal() const { return Universal; }
  2487. bool empty() const { return Set.empty() && !Universal; }
  2488. /// Finds A := A ^ B where A or B could be the "Universal" set which
  2489. /// contains every possible attribute. Returns true if changes were made.
  2490. bool getIntersection(const SetContents &RHS) {
  2491. bool IsUniversal = Universal;
  2492. unsigned Size = Set.size();
  2493. // A := A ^ U = A
  2494. if (RHS.isUniversal())
  2495. return false;
  2496. // A := U ^ B = B
  2497. if (Universal)
  2498. Set = RHS.getSet();
  2499. else
  2500. set_intersect(Set, RHS.getSet());
  2501. Universal &= RHS.isUniversal();
  2502. return IsUniversal != Universal || Size != Set.size();
  2503. }
  2504. /// Finds A := A u B where A or B could be the "Universal" set which
  2505. /// contains every possible attribute. returns true if changes were made.
  2506. bool getUnion(const SetContents &RHS) {
  2507. bool IsUniversal = Universal;
  2508. unsigned Size = Set.size();
  2509. // A := A u U = U = U u B
  2510. if (!RHS.isUniversal() && !Universal)
  2511. set_union(Set, RHS.getSet());
  2512. Universal |= RHS.isUniversal();
  2513. return IsUniversal != Universal || Size != Set.size();
  2514. }
  2515. private:
  2516. /// Indicates if this set is "universal", containing every possible element.
  2517. bool Universal;
  2518. /// The set of currently active assumptions.
  2519. DenseSet<BaseTy> Set;
  2520. };
  2521. SetState() : Known(false), Assumed(true), IsAtFixedpoint(false) {}
  2522. /// Initializes the known state with an initial set and initializes the
  2523. /// assumed state as universal.
  2524. SetState(const DenseSet<BaseTy> &Known)
  2525. : Known(Known), Assumed(true), IsAtFixedpoint(false) {}
  2526. /// See AbstractState::isValidState()
  2527. bool isValidState() const override { return !Assumed.empty(); }
  2528. /// See AbstractState::isAtFixpoint()
  2529. bool isAtFixpoint() const override { return IsAtFixedpoint; }
  2530. /// See AbstractState::indicateOptimisticFixpoint(...)
  2531. ChangeStatus indicateOptimisticFixpoint() override {
  2532. IsAtFixedpoint = true;
  2533. Known = Assumed;
  2534. return ChangeStatus::UNCHANGED;
  2535. }
  2536. /// See AbstractState::indicatePessimisticFixpoint(...)
  2537. ChangeStatus indicatePessimisticFixpoint() override {
  2538. IsAtFixedpoint = true;
  2539. Assumed = Known;
  2540. return ChangeStatus::CHANGED;
  2541. }
  2542. /// Return the known state encoding.
  2543. const SetContents &getKnown() const { return Known; }
  2544. /// Return the assumed state encoding.
  2545. const SetContents &getAssumed() const { return Assumed; }
  2546. /// Returns if the set state contains the element.
  2547. bool setContains(const BaseTy &Elem) const {
  2548. return Assumed.getSet().contains(Elem) || Known.getSet().contains(Elem);
  2549. }
  2550. /// Performs the set intersection between this set and \p RHS. Returns true if
  2551. /// changes were made.
  2552. bool getIntersection(const SetContents &RHS) {
  2553. unsigned SizeBefore = Assumed.getSet().size();
  2554. // Get intersection and make sure that the known set is still a proper
  2555. // subset of the assumed set. A := K u (A ^ R).
  2556. Assumed.getIntersection(RHS);
  2557. Assumed.getUnion(Known);
  2558. return SizeBefore != Assumed.getSet().size();
  2559. }
  2560. /// Performs the set union between this set and \p RHS. Returns true if
  2561. /// changes were made.
  2562. bool getUnion(const SetContents &RHS) { return Assumed.getUnion(RHS); }
  2563. private:
  2564. /// The set of values known for this state.
  2565. SetContents Known;
  2566. /// The set of assumed values for this state.
  2567. SetContents Assumed;
  2568. bool IsAtFixedpoint;
  2569. };
  2570. /// Helper struct necessary as the modular build fails if the virtual method
  2571. /// IRAttribute::manifest is defined in the Attributor.cpp.
  2572. struct IRAttributeManifest {
  2573. static ChangeStatus manifestAttrs(Attributor &A, const IRPosition &IRP,
  2574. const ArrayRef<Attribute> &DeducedAttrs,
  2575. bool ForceReplace = false);
  2576. };
  2577. /// Helper to tie a abstract state implementation to an abstract attribute.
  2578. template <typename StateTy, typename BaseType, class... Ts>
  2579. struct StateWrapper : public BaseType, public StateTy {
  2580. /// Provide static access to the type of the state.
  2581. using StateType = StateTy;
  2582. StateWrapper(const IRPosition &IRP, Ts... Args)
  2583. : BaseType(IRP), StateTy(Args...) {}
  2584. /// See AbstractAttribute::getState(...).
  2585. StateType &getState() override { return *this; }
  2586. /// See AbstractAttribute::getState(...).
  2587. const StateType &getState() const override { return *this; }
  2588. };
  2589. /// Helper class that provides common functionality to manifest IR attributes.
  2590. template <Attribute::AttrKind AK, typename BaseType>
  2591. struct IRAttribute : public BaseType {
  2592. IRAttribute(const IRPosition &IRP) : BaseType(IRP) {}
  2593. /// See AbstractAttribute::initialize(...).
  2594. void initialize(Attributor &A) override {
  2595. const IRPosition &IRP = this->getIRPosition();
  2596. if (isa<UndefValue>(IRP.getAssociatedValue()) ||
  2597. this->hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ false,
  2598. &A)) {
  2599. this->getState().indicateOptimisticFixpoint();
  2600. return;
  2601. }
  2602. bool IsFnInterface = IRP.isFnInterfaceKind();
  2603. const Function *FnScope = IRP.getAnchorScope();
  2604. // TODO: Not all attributes require an exact definition. Find a way to
  2605. // enable deduction for some but not all attributes in case the
  2606. // definition might be changed at runtime, see also
  2607. // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
  2608. // TODO: We could always determine abstract attributes and if sufficient
  2609. // information was found we could duplicate the functions that do not
  2610. // have an exact definition.
  2611. if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope)))
  2612. this->getState().indicatePessimisticFixpoint();
  2613. }
  2614. /// See AbstractAttribute::manifest(...).
  2615. ChangeStatus manifest(Attributor &A) override {
  2616. if (isa<UndefValue>(this->getIRPosition().getAssociatedValue()))
  2617. return ChangeStatus::UNCHANGED;
  2618. SmallVector<Attribute, 4> DeducedAttrs;
  2619. getDeducedAttributes(this->getAnchorValue().getContext(), DeducedAttrs);
  2620. return IRAttributeManifest::manifestAttrs(A, this->getIRPosition(),
  2621. DeducedAttrs);
  2622. }
  2623. /// Return the kind that identifies the abstract attribute implementation.
  2624. Attribute::AttrKind getAttrKind() const { return AK; }
  2625. /// Return the deduced attributes in \p Attrs.
  2626. virtual void getDeducedAttributes(LLVMContext &Ctx,
  2627. SmallVectorImpl<Attribute> &Attrs) const {
  2628. Attrs.emplace_back(Attribute::get(Ctx, getAttrKind()));
  2629. }
  2630. };
  2631. /// Base struct for all "concrete attribute" deductions.
  2632. ///
  2633. /// The abstract attribute is a minimal interface that allows the Attributor to
  2634. /// orchestrate the abstract/fixpoint analysis. The design allows to hide away
  2635. /// implementation choices made for the subclasses but also to structure their
  2636. /// implementation and simplify the use of other abstract attributes in-flight.
  2637. ///
  2638. /// To allow easy creation of new attributes, most methods have default
  2639. /// implementations. The ones that do not are generally straight forward, except
  2640. /// `AbstractAttribute::updateImpl` which is the location of most reasoning
  2641. /// associated with the abstract attribute. The update is invoked by the
  2642. /// Attributor in case the situation used to justify the current optimistic
  2643. /// state might have changed. The Attributor determines this automatically
  2644. /// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
  2645. ///
  2646. /// The `updateImpl` method should inspect the IR and other abstract attributes
  2647. /// in-flight to justify the best possible (=optimistic) state. The actual
  2648. /// implementation is, similar to the underlying abstract state encoding, not
  2649. /// exposed. In the most common case, the `updateImpl` will go through a list of
  2650. /// reasons why its optimistic state is valid given the current information. If
  2651. /// any combination of them holds and is sufficient to justify the current
  2652. /// optimistic state, the method shall return UNCHAGED. If not, the optimistic
  2653. /// state is adjusted to the situation and the method shall return CHANGED.
  2654. ///
  2655. /// If the manifestation of the "concrete attribute" deduced by the subclass
  2656. /// differs from the "default" behavior, which is a (set of) LLVM-IR
  2657. /// attribute(s) for an argument, call site argument, function return value, or
  2658. /// function, the `AbstractAttribute::manifest` method should be overloaded.
  2659. ///
  2660. /// NOTE: If the state obtained via getState() is INVALID, thus if
  2661. /// AbstractAttribute::getState().isValidState() returns false, no
  2662. /// information provided by the methods of this class should be used.
  2663. /// NOTE: The Attributor currently has certain limitations to what we can do.
  2664. /// As a general rule of thumb, "concrete" abstract attributes should *for
  2665. /// now* only perform "backward" information propagation. That means
  2666. /// optimistic information obtained through abstract attributes should
  2667. /// only be used at positions that precede the origin of the information
  2668. /// with regards to the program flow. More practically, information can
  2669. /// *now* be propagated from instructions to their enclosing function, but
  2670. /// *not* from call sites to the called function. The mechanisms to allow
  2671. /// both directions will be added in the future.
  2672. /// NOTE: The mechanics of adding a new "concrete" abstract attribute are
  2673. /// described in the file comment.
  2674. struct AbstractAttribute : public IRPosition, public AADepGraphNode {
  2675. using StateType = AbstractState;
  2676. AbstractAttribute(const IRPosition &IRP) : IRPosition(IRP) {}
  2677. /// Virtual destructor.
  2678. virtual ~AbstractAttribute() = default;
  2679. /// This function is used to identify if an \p DGN is of type
  2680. /// AbstractAttribute so that the dyn_cast and cast can use such information
  2681. /// to cast an AADepGraphNode to an AbstractAttribute.
  2682. ///
  2683. /// We eagerly return true here because all AADepGraphNodes except for the
  2684. /// Synthethis Node are of type AbstractAttribute
  2685. static bool classof(const AADepGraphNode *DGN) { return true; }
  2686. /// Initialize the state with the information in the Attributor \p A.
  2687. ///
  2688. /// This function is called by the Attributor once all abstract attributes
  2689. /// have been identified. It can and shall be used for task like:
  2690. /// - identify existing knowledge in the IR and use it for the "known state"
  2691. /// - perform any work that is not going to change over time, e.g., determine
  2692. /// a subset of the IR, or attributes in-flight, that have to be looked at
  2693. /// in the `updateImpl` method.
  2694. virtual void initialize(Attributor &A) {}
  2695. /// A query AA is always scheduled as long as we do updates because it does
  2696. /// lazy computation that cannot be determined to be done from the outside.
  2697. /// However, while query AAs will not be fixed if they do not have outstanding
  2698. /// dependences, we will only schedule them like other AAs. If a query AA that
  2699. /// received a new query it needs to request an update via
  2700. /// `Attributor::requestUpdateForAA`.
  2701. virtual bool isQueryAA() const { return false; }
  2702. /// Return the internal abstract state for inspection.
  2703. virtual StateType &getState() = 0;
  2704. virtual const StateType &getState() const = 0;
  2705. /// Return an IR position, see struct IRPosition.
  2706. const IRPosition &getIRPosition() const { return *this; };
  2707. IRPosition &getIRPosition() { return *this; };
  2708. /// Helper functions, for debug purposes only.
  2709. ///{
  2710. void print(raw_ostream &OS) const override;
  2711. virtual void printWithDeps(raw_ostream &OS) const;
  2712. void dump() const { print(dbgs()); }
  2713. /// This function should return the "summarized" assumed state as string.
  2714. virtual const std::string getAsStr() const = 0;
  2715. /// This function should return the name of the AbstractAttribute
  2716. virtual const std::string getName() const = 0;
  2717. /// This function should return the address of the ID of the AbstractAttribute
  2718. virtual const char *getIdAddr() const = 0;
  2719. ///}
  2720. /// Allow the Attributor access to the protected methods.
  2721. friend struct Attributor;
  2722. protected:
  2723. /// Hook for the Attributor to trigger an update of the internal state.
  2724. ///
  2725. /// If this attribute is already fixed, this method will return UNCHANGED,
  2726. /// otherwise it delegates to `AbstractAttribute::updateImpl`.
  2727. ///
  2728. /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
  2729. ChangeStatus update(Attributor &A);
  2730. /// Hook for the Attributor to trigger the manifestation of the information
  2731. /// represented by the abstract attribute in the LLVM-IR.
  2732. ///
  2733. /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
  2734. virtual ChangeStatus manifest(Attributor &A) {
  2735. return ChangeStatus::UNCHANGED;
  2736. }
  2737. /// Hook to enable custom statistic tracking, called after manifest that
  2738. /// resulted in a change if statistics are enabled.
  2739. ///
  2740. /// We require subclasses to provide an implementation so we remember to
  2741. /// add statistics for them.
  2742. virtual void trackStatistics() const = 0;
  2743. /// The actual update/transfer function which has to be implemented by the
  2744. /// derived classes.
  2745. ///
  2746. /// If it is called, the environment has changed and we have to determine if
  2747. /// the current information is still valid or adjust it otherwise.
  2748. ///
  2749. /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
  2750. virtual ChangeStatus updateImpl(Attributor &A) = 0;
  2751. };
  2752. /// Forward declarations of output streams for debug purposes.
  2753. ///
  2754. ///{
  2755. raw_ostream &operator<<(raw_ostream &OS, const AbstractAttribute &AA);
  2756. raw_ostream &operator<<(raw_ostream &OS, ChangeStatus S);
  2757. raw_ostream &operator<<(raw_ostream &OS, IRPosition::Kind);
  2758. raw_ostream &operator<<(raw_ostream &OS, const IRPosition &);
  2759. raw_ostream &operator<<(raw_ostream &OS, const AbstractState &State);
  2760. template <typename base_ty, base_ty BestState, base_ty WorstState>
  2761. raw_ostream &
  2762. operator<<(raw_ostream &OS,
  2763. const IntegerStateBase<base_ty, BestState, WorstState> &S) {
  2764. return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
  2765. << static_cast<const AbstractState &>(S);
  2766. }
  2767. raw_ostream &operator<<(raw_ostream &OS, const IntegerRangeState &State);
  2768. ///}
  2769. struct AttributorPass : public PassInfoMixin<AttributorPass> {
  2770. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  2771. };
  2772. struct AttributorCGSCCPass : public PassInfoMixin<AttributorCGSCCPass> {
  2773. PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM,
  2774. LazyCallGraph &CG, CGSCCUpdateResult &UR);
  2775. };
  2776. Pass *createAttributorLegacyPass();
  2777. Pass *createAttributorCGSCCLegacyPass();
  2778. /// Helper function to clamp a state \p S of type \p StateType with the
  2779. /// information in \p R and indicate/return if \p S did change (as-in update is
  2780. /// required to be run again).
  2781. template <typename StateType>
  2782. ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
  2783. auto Assumed = S.getAssumed();
  2784. S ^= R;
  2785. return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
  2786. : ChangeStatus::CHANGED;
  2787. }
  2788. /// ----------------------------------------------------------------------------
  2789. /// Abstract Attribute Classes
  2790. /// ----------------------------------------------------------------------------
  2791. /// An abstract attribute for the returned values of a function.
  2792. struct AAReturnedValues
  2793. : public IRAttribute<Attribute::Returned, AbstractAttribute> {
  2794. AAReturnedValues(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  2795. /// Check \p Pred on all returned values.
  2796. ///
  2797. /// This method will evaluate \p Pred on returned values and return
  2798. /// true if (1) all returned values are known, and (2) \p Pred returned true
  2799. /// for all returned values.
  2800. ///
  2801. /// Note: Unlike the Attributor::checkForAllReturnedValuesAndReturnInsts
  2802. /// method, this one will not filter dead return instructions.
  2803. virtual bool checkForAllReturnedValuesAndReturnInsts(
  2804. function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
  2805. const = 0;
  2806. using iterator =
  2807. MapVector<Value *, SmallSetVector<ReturnInst *, 4>>::iterator;
  2808. using const_iterator =
  2809. MapVector<Value *, SmallSetVector<ReturnInst *, 4>>::const_iterator;
  2810. virtual llvm::iterator_range<iterator> returned_values() = 0;
  2811. virtual llvm::iterator_range<const_iterator> returned_values() const = 0;
  2812. virtual size_t getNumReturnValues() const = 0;
  2813. /// Create an abstract attribute view for the position \p IRP.
  2814. static AAReturnedValues &createForPosition(const IRPosition &IRP,
  2815. Attributor &A);
  2816. /// See AbstractAttribute::getName()
  2817. const std::string getName() const override { return "AAReturnedValues"; }
  2818. /// See AbstractAttribute::getIdAddr()
  2819. const char *getIdAddr() const override { return &ID; }
  2820. /// This function should return true if the type of the \p AA is
  2821. /// AAReturnedValues
  2822. static bool classof(const AbstractAttribute *AA) {
  2823. return (AA->getIdAddr() == &ID);
  2824. }
  2825. /// Unique ID (due to the unique address)
  2826. static const char ID;
  2827. };
  2828. struct AANoUnwind
  2829. : public IRAttribute<Attribute::NoUnwind,
  2830. StateWrapper<BooleanState, AbstractAttribute>> {
  2831. AANoUnwind(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  2832. /// Returns true if nounwind is assumed.
  2833. bool isAssumedNoUnwind() const { return getAssumed(); }
  2834. /// Returns true if nounwind is known.
  2835. bool isKnownNoUnwind() const { return getKnown(); }
  2836. /// Create an abstract attribute view for the position \p IRP.
  2837. static AANoUnwind &createForPosition(const IRPosition &IRP, Attributor &A);
  2838. /// See AbstractAttribute::getName()
  2839. const std::string getName() const override { return "AANoUnwind"; }
  2840. /// See AbstractAttribute::getIdAddr()
  2841. const char *getIdAddr() const override { return &ID; }
  2842. /// This function should return true if the type of the \p AA is AANoUnwind
  2843. static bool classof(const AbstractAttribute *AA) {
  2844. return (AA->getIdAddr() == &ID);
  2845. }
  2846. /// Unique ID (due to the unique address)
  2847. static const char ID;
  2848. };
  2849. struct AANoSync
  2850. : public IRAttribute<Attribute::NoSync,
  2851. StateWrapper<BooleanState, AbstractAttribute>> {
  2852. AANoSync(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  2853. /// Returns true if "nosync" is assumed.
  2854. bool isAssumedNoSync() const { return getAssumed(); }
  2855. /// Returns true if "nosync" is known.
  2856. bool isKnownNoSync() const { return getKnown(); }
  2857. /// Helper function used to determine whether an instruction is non-relaxed
  2858. /// atomic. In other words, if an atomic instruction does not have unordered
  2859. /// or monotonic ordering
  2860. static bool isNonRelaxedAtomic(const Instruction *I);
  2861. /// Helper function specific for intrinsics which are potentially volatile.
  2862. static bool isNoSyncIntrinsic(const Instruction *I);
  2863. /// Helper function to determine if \p CB is an aligned (GPU) barrier. Aligned
  2864. /// barriers have to be executed by all threads. The flag \p ExecutedAligned
  2865. /// indicates if the call is executed by all threads in a (thread) block in an
  2866. /// aligned way. If that is the case, non-aligned barriers are effectively
  2867. /// aligned barriers.
  2868. static bool isAlignedBarrier(const CallBase &CB, bool ExecutedAligned);
  2869. /// Create an abstract attribute view for the position \p IRP.
  2870. static AANoSync &createForPosition(const IRPosition &IRP, Attributor &A);
  2871. /// See AbstractAttribute::getName()
  2872. const std::string getName() const override { return "AANoSync"; }
  2873. /// See AbstractAttribute::getIdAddr()
  2874. const char *getIdAddr() const override { return &ID; }
  2875. /// This function should return true if the type of the \p AA is AANoSync
  2876. static bool classof(const AbstractAttribute *AA) {
  2877. return (AA->getIdAddr() == &ID);
  2878. }
  2879. /// Unique ID (due to the unique address)
  2880. static const char ID;
  2881. };
  2882. /// An abstract interface for all nonnull attributes.
  2883. struct AANonNull
  2884. : public IRAttribute<Attribute::NonNull,
  2885. StateWrapper<BooleanState, AbstractAttribute>> {
  2886. AANonNull(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  2887. /// Return true if we assume that the underlying value is nonnull.
  2888. bool isAssumedNonNull() const { return getAssumed(); }
  2889. /// Return true if we know that underlying value is nonnull.
  2890. bool isKnownNonNull() const { return getKnown(); }
  2891. /// Create an abstract attribute view for the position \p IRP.
  2892. static AANonNull &createForPosition(const IRPosition &IRP, Attributor &A);
  2893. /// See AbstractAttribute::getName()
  2894. const std::string getName() const override { return "AANonNull"; }
  2895. /// See AbstractAttribute::getIdAddr()
  2896. const char *getIdAddr() const override { return &ID; }
  2897. /// This function should return true if the type of the \p AA is AANonNull
  2898. static bool classof(const AbstractAttribute *AA) {
  2899. return (AA->getIdAddr() == &ID);
  2900. }
  2901. /// Unique ID (due to the unique address)
  2902. static const char ID;
  2903. };
  2904. /// An abstract attribute for norecurse.
  2905. struct AANoRecurse
  2906. : public IRAttribute<Attribute::NoRecurse,
  2907. StateWrapper<BooleanState, AbstractAttribute>> {
  2908. AANoRecurse(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  2909. /// Return true if "norecurse" is assumed.
  2910. bool isAssumedNoRecurse() const { return getAssumed(); }
  2911. /// Return true if "norecurse" is known.
  2912. bool isKnownNoRecurse() const { return getKnown(); }
  2913. /// Create an abstract attribute view for the position \p IRP.
  2914. static AANoRecurse &createForPosition(const IRPosition &IRP, Attributor &A);
  2915. /// See AbstractAttribute::getName()
  2916. const std::string getName() const override { return "AANoRecurse"; }
  2917. /// See AbstractAttribute::getIdAddr()
  2918. const char *getIdAddr() const override { return &ID; }
  2919. /// This function should return true if the type of the \p AA is AANoRecurse
  2920. static bool classof(const AbstractAttribute *AA) {
  2921. return (AA->getIdAddr() == &ID);
  2922. }
  2923. /// Unique ID (due to the unique address)
  2924. static const char ID;
  2925. };
  2926. /// An abstract attribute for willreturn.
  2927. struct AAWillReturn
  2928. : public IRAttribute<Attribute::WillReturn,
  2929. StateWrapper<BooleanState, AbstractAttribute>> {
  2930. AAWillReturn(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  2931. /// Return true if "willreturn" is assumed.
  2932. bool isAssumedWillReturn() const { return getAssumed(); }
  2933. /// Return true if "willreturn" is known.
  2934. bool isKnownWillReturn() const { return getKnown(); }
  2935. /// Create an abstract attribute view for the position \p IRP.
  2936. static AAWillReturn &createForPosition(const IRPosition &IRP, Attributor &A);
  2937. /// See AbstractAttribute::getName()
  2938. const std::string getName() const override { return "AAWillReturn"; }
  2939. /// See AbstractAttribute::getIdAddr()
  2940. const char *getIdAddr() const override { return &ID; }
  2941. /// This function should return true if the type of the \p AA is AAWillReturn
  2942. static bool classof(const AbstractAttribute *AA) {
  2943. return (AA->getIdAddr() == &ID);
  2944. }
  2945. /// Unique ID (due to the unique address)
  2946. static const char ID;
  2947. };
  2948. /// An abstract attribute for undefined behavior.
  2949. struct AAUndefinedBehavior
  2950. : public StateWrapper<BooleanState, AbstractAttribute> {
  2951. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  2952. AAUndefinedBehavior(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  2953. /// Return true if "undefined behavior" is assumed.
  2954. bool isAssumedToCauseUB() const { return getAssumed(); }
  2955. /// Return true if "undefined behavior" is assumed for a specific instruction.
  2956. virtual bool isAssumedToCauseUB(Instruction *I) const = 0;
  2957. /// Return true if "undefined behavior" is known.
  2958. bool isKnownToCauseUB() const { return getKnown(); }
  2959. /// Return true if "undefined behavior" is known for a specific instruction.
  2960. virtual bool isKnownToCauseUB(Instruction *I) const = 0;
  2961. /// Create an abstract attribute view for the position \p IRP.
  2962. static AAUndefinedBehavior &createForPosition(const IRPosition &IRP,
  2963. Attributor &A);
  2964. /// See AbstractAttribute::getName()
  2965. const std::string getName() const override { return "AAUndefinedBehavior"; }
  2966. /// See AbstractAttribute::getIdAddr()
  2967. const char *getIdAddr() const override { return &ID; }
  2968. /// This function should return true if the type of the \p AA is
  2969. /// AAUndefineBehavior
  2970. static bool classof(const AbstractAttribute *AA) {
  2971. return (AA->getIdAddr() == &ID);
  2972. }
  2973. /// Unique ID (due to the unique address)
  2974. static const char ID;
  2975. };
  2976. /// An abstract interface to determine reachability of point A to B.
  2977. struct AAIntraFnReachability
  2978. : public StateWrapper<BooleanState, AbstractAttribute> {
  2979. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  2980. AAIntraFnReachability(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  2981. /// Returns true if 'From' instruction is assumed to reach, 'To' instruction.
  2982. /// Users should provide two positions they are interested in, and the class
  2983. /// determines (and caches) reachability.
  2984. virtual bool isAssumedReachable(
  2985. Attributor &A, const Instruction &From, const Instruction &To,
  2986. const AA::InstExclusionSetTy *ExclusionSet = nullptr) const = 0;
  2987. /// Create an abstract attribute view for the position \p IRP.
  2988. static AAIntraFnReachability &createForPosition(const IRPosition &IRP,
  2989. Attributor &A);
  2990. /// See AbstractAttribute::getName()
  2991. const std::string getName() const override { return "AAIntraFnReachability"; }
  2992. /// See AbstractAttribute::getIdAddr()
  2993. const char *getIdAddr() const override { return &ID; }
  2994. /// This function should return true if the type of the \p AA is
  2995. /// AAIntraFnReachability
  2996. static bool classof(const AbstractAttribute *AA) {
  2997. return (AA->getIdAddr() == &ID);
  2998. }
  2999. /// Unique ID (due to the unique address)
  3000. static const char ID;
  3001. };
  3002. /// An abstract interface for all noalias attributes.
  3003. struct AANoAlias
  3004. : public IRAttribute<Attribute::NoAlias,
  3005. StateWrapper<BooleanState, AbstractAttribute>> {
  3006. AANoAlias(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3007. /// Return true if we assume that the underlying value is alias.
  3008. bool isAssumedNoAlias() const { return getAssumed(); }
  3009. /// Return true if we know that underlying value is noalias.
  3010. bool isKnownNoAlias() const { return getKnown(); }
  3011. /// Create an abstract attribute view for the position \p IRP.
  3012. static AANoAlias &createForPosition(const IRPosition &IRP, Attributor &A);
  3013. /// See AbstractAttribute::getName()
  3014. const std::string getName() const override { return "AANoAlias"; }
  3015. /// See AbstractAttribute::getIdAddr()
  3016. const char *getIdAddr() const override { return &ID; }
  3017. /// This function should return true if the type of the \p AA is AANoAlias
  3018. static bool classof(const AbstractAttribute *AA) {
  3019. return (AA->getIdAddr() == &ID);
  3020. }
  3021. /// Unique ID (due to the unique address)
  3022. static const char ID;
  3023. };
  3024. /// An AbstractAttribute for nofree.
  3025. struct AANoFree
  3026. : public IRAttribute<Attribute::NoFree,
  3027. StateWrapper<BooleanState, AbstractAttribute>> {
  3028. AANoFree(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3029. /// Return true if "nofree" is assumed.
  3030. bool isAssumedNoFree() const { return getAssumed(); }
  3031. /// Return true if "nofree" is known.
  3032. bool isKnownNoFree() const { return getKnown(); }
  3033. /// Create an abstract attribute view for the position \p IRP.
  3034. static AANoFree &createForPosition(const IRPosition &IRP, Attributor &A);
  3035. /// See AbstractAttribute::getName()
  3036. const std::string getName() const override { return "AANoFree"; }
  3037. /// See AbstractAttribute::getIdAddr()
  3038. const char *getIdAddr() const override { return &ID; }
  3039. /// This function should return true if the type of the \p AA is AANoFree
  3040. static bool classof(const AbstractAttribute *AA) {
  3041. return (AA->getIdAddr() == &ID);
  3042. }
  3043. /// Unique ID (due to the unique address)
  3044. static const char ID;
  3045. };
  3046. /// An AbstractAttribute for noreturn.
  3047. struct AANoReturn
  3048. : public IRAttribute<Attribute::NoReturn,
  3049. StateWrapper<BooleanState, AbstractAttribute>> {
  3050. AANoReturn(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3051. /// Return true if the underlying object is assumed to never return.
  3052. bool isAssumedNoReturn() const { return getAssumed(); }
  3053. /// Return true if the underlying object is known to never return.
  3054. bool isKnownNoReturn() const { return getKnown(); }
  3055. /// Create an abstract attribute view for the position \p IRP.
  3056. static AANoReturn &createForPosition(const IRPosition &IRP, Attributor &A);
  3057. /// See AbstractAttribute::getName()
  3058. const std::string getName() const override { return "AANoReturn"; }
  3059. /// See AbstractAttribute::getIdAddr()
  3060. const char *getIdAddr() const override { return &ID; }
  3061. /// This function should return true if the type of the \p AA is AANoReturn
  3062. static bool classof(const AbstractAttribute *AA) {
  3063. return (AA->getIdAddr() == &ID);
  3064. }
  3065. /// Unique ID (due to the unique address)
  3066. static const char ID;
  3067. };
  3068. /// An abstract interface for liveness abstract attribute.
  3069. struct AAIsDead
  3070. : public StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute> {
  3071. using Base = StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute>;
  3072. AAIsDead(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  3073. /// State encoding bits. A set bit in the state means the property holds.
  3074. enum {
  3075. HAS_NO_EFFECT = 1 << 0,
  3076. IS_REMOVABLE = 1 << 1,
  3077. IS_DEAD = HAS_NO_EFFECT | IS_REMOVABLE,
  3078. };
  3079. static_assert(IS_DEAD == getBestState(), "Unexpected BEST_STATE value");
  3080. protected:
  3081. /// The query functions are protected such that other attributes need to go
  3082. /// through the Attributor interfaces: `Attributor::isAssumedDead(...)`
  3083. /// Returns true if the underlying value is assumed dead.
  3084. virtual bool isAssumedDead() const = 0;
  3085. /// Returns true if the underlying value is known dead.
  3086. virtual bool isKnownDead() const = 0;
  3087. /// Returns true if \p BB is known dead.
  3088. virtual bool isKnownDead(const BasicBlock *BB) const = 0;
  3089. /// Returns true if \p I is assumed dead.
  3090. virtual bool isAssumedDead(const Instruction *I) const = 0;
  3091. /// Returns true if \p I is known dead.
  3092. virtual bool isKnownDead(const Instruction *I) const = 0;
  3093. /// Return true if the underlying value is a store that is known to be
  3094. /// removable. This is different from dead stores as the removable store
  3095. /// can have an effect on live values, especially loads, but that effect
  3096. /// is propagated which allows us to remove the store in turn.
  3097. virtual bool isRemovableStore() const { return false; }
  3098. /// This method is used to check if at least one instruction in a collection
  3099. /// of instructions is live.
  3100. template <typename T> bool isLiveInstSet(T begin, T end) const {
  3101. for (const auto &I : llvm::make_range(begin, end)) {
  3102. assert(I->getFunction() == getIRPosition().getAssociatedFunction() &&
  3103. "Instruction must be in the same anchor scope function.");
  3104. if (!isAssumedDead(I))
  3105. return true;
  3106. }
  3107. return false;
  3108. }
  3109. public:
  3110. /// Create an abstract attribute view for the position \p IRP.
  3111. static AAIsDead &createForPosition(const IRPosition &IRP, Attributor &A);
  3112. /// Determine if \p F might catch asynchronous exceptions.
  3113. static bool mayCatchAsynchronousExceptions(const Function &F) {
  3114. return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
  3115. }
  3116. /// Returns true if \p BB is assumed dead.
  3117. virtual bool isAssumedDead(const BasicBlock *BB) const = 0;
  3118. /// Return if the edge from \p From BB to \p To BB is assumed dead.
  3119. /// This is specifically useful in AAReachability.
  3120. virtual bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const {
  3121. return false;
  3122. }
  3123. /// See AbstractAttribute::getName()
  3124. const std::string getName() const override { return "AAIsDead"; }
  3125. /// See AbstractAttribute::getIdAddr()
  3126. const char *getIdAddr() const override { return &ID; }
  3127. /// This function should return true if the type of the \p AA is AAIsDead
  3128. static bool classof(const AbstractAttribute *AA) {
  3129. return (AA->getIdAddr() == &ID);
  3130. }
  3131. /// Unique ID (due to the unique address)
  3132. static const char ID;
  3133. friend struct Attributor;
  3134. };
  3135. /// State for dereferenceable attribute
  3136. struct DerefState : AbstractState {
  3137. static DerefState getBestState() { return DerefState(); }
  3138. static DerefState getBestState(const DerefState &) { return getBestState(); }
  3139. /// Return the worst possible representable state.
  3140. static DerefState getWorstState() {
  3141. DerefState DS;
  3142. DS.indicatePessimisticFixpoint();
  3143. return DS;
  3144. }
  3145. static DerefState getWorstState(const DerefState &) {
  3146. return getWorstState();
  3147. }
  3148. /// State representing for dereferenceable bytes.
  3149. IncIntegerState<> DerefBytesState;
  3150. /// Map representing for accessed memory offsets and sizes.
  3151. /// A key is Offset and a value is size.
  3152. /// If there is a load/store instruction something like,
  3153. /// p[offset] = v;
  3154. /// (offset, sizeof(v)) will be inserted to this map.
  3155. /// std::map is used because we want to iterate keys in ascending order.
  3156. std::map<int64_t, uint64_t> AccessedBytesMap;
  3157. /// Helper function to calculate dereferenceable bytes from current known
  3158. /// bytes and accessed bytes.
  3159. ///
  3160. /// int f(int *A){
  3161. /// *A = 0;
  3162. /// *(A+2) = 2;
  3163. /// *(A+1) = 1;
  3164. /// *(A+10) = 10;
  3165. /// }
  3166. /// ```
  3167. /// In that case, AccessedBytesMap is `{0:4, 4:4, 8:4, 40:4}`.
  3168. /// AccessedBytesMap is std::map so it is iterated in accending order on
  3169. /// key(Offset). So KnownBytes will be updated like this:
  3170. ///
  3171. /// |Access | KnownBytes
  3172. /// |(0, 4)| 0 -> 4
  3173. /// |(4, 4)| 4 -> 8
  3174. /// |(8, 4)| 8 -> 12
  3175. /// |(40, 4) | 12 (break)
  3176. void computeKnownDerefBytesFromAccessedMap() {
  3177. int64_t KnownBytes = DerefBytesState.getKnown();
  3178. for (auto &Access : AccessedBytesMap) {
  3179. if (KnownBytes < Access.first)
  3180. break;
  3181. KnownBytes = std::max(KnownBytes, Access.first + (int64_t)Access.second);
  3182. }
  3183. DerefBytesState.takeKnownMaximum(KnownBytes);
  3184. }
  3185. /// State representing that whether the value is globaly dereferenceable.
  3186. BooleanState GlobalState;
  3187. /// See AbstractState::isValidState()
  3188. bool isValidState() const override { return DerefBytesState.isValidState(); }
  3189. /// See AbstractState::isAtFixpoint()
  3190. bool isAtFixpoint() const override {
  3191. return !isValidState() ||
  3192. (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint());
  3193. }
  3194. /// See AbstractState::indicateOptimisticFixpoint(...)
  3195. ChangeStatus indicateOptimisticFixpoint() override {
  3196. DerefBytesState.indicateOptimisticFixpoint();
  3197. GlobalState.indicateOptimisticFixpoint();
  3198. return ChangeStatus::UNCHANGED;
  3199. }
  3200. /// See AbstractState::indicatePessimisticFixpoint(...)
  3201. ChangeStatus indicatePessimisticFixpoint() override {
  3202. DerefBytesState.indicatePessimisticFixpoint();
  3203. GlobalState.indicatePessimisticFixpoint();
  3204. return ChangeStatus::CHANGED;
  3205. }
  3206. /// Update known dereferenceable bytes.
  3207. void takeKnownDerefBytesMaximum(uint64_t Bytes) {
  3208. DerefBytesState.takeKnownMaximum(Bytes);
  3209. // Known bytes might increase.
  3210. computeKnownDerefBytesFromAccessedMap();
  3211. }
  3212. /// Update assumed dereferenceable bytes.
  3213. void takeAssumedDerefBytesMinimum(uint64_t Bytes) {
  3214. DerefBytesState.takeAssumedMinimum(Bytes);
  3215. }
  3216. /// Add accessed bytes to the map.
  3217. void addAccessedBytes(int64_t Offset, uint64_t Size) {
  3218. uint64_t &AccessedBytes = AccessedBytesMap[Offset];
  3219. AccessedBytes = std::max(AccessedBytes, Size);
  3220. // Known bytes might increase.
  3221. computeKnownDerefBytesFromAccessedMap();
  3222. }
  3223. /// Equality for DerefState.
  3224. bool operator==(const DerefState &R) const {
  3225. return this->DerefBytesState == R.DerefBytesState &&
  3226. this->GlobalState == R.GlobalState;
  3227. }
  3228. /// Inequality for DerefState.
  3229. bool operator!=(const DerefState &R) const { return !(*this == R); }
  3230. /// See IntegerStateBase::operator^=
  3231. DerefState operator^=(const DerefState &R) {
  3232. DerefBytesState ^= R.DerefBytesState;
  3233. GlobalState ^= R.GlobalState;
  3234. return *this;
  3235. }
  3236. /// See IntegerStateBase::operator+=
  3237. DerefState operator+=(const DerefState &R) {
  3238. DerefBytesState += R.DerefBytesState;
  3239. GlobalState += R.GlobalState;
  3240. return *this;
  3241. }
  3242. /// See IntegerStateBase::operator&=
  3243. DerefState operator&=(const DerefState &R) {
  3244. DerefBytesState &= R.DerefBytesState;
  3245. GlobalState &= R.GlobalState;
  3246. return *this;
  3247. }
  3248. /// See IntegerStateBase::operator|=
  3249. DerefState operator|=(const DerefState &R) {
  3250. DerefBytesState |= R.DerefBytesState;
  3251. GlobalState |= R.GlobalState;
  3252. return *this;
  3253. }
  3254. protected:
  3255. const AANonNull *NonNullAA = nullptr;
  3256. };
  3257. /// An abstract interface for all dereferenceable attribute.
  3258. struct AADereferenceable
  3259. : public IRAttribute<Attribute::Dereferenceable,
  3260. StateWrapper<DerefState, AbstractAttribute>> {
  3261. AADereferenceable(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3262. /// Return true if we assume that the underlying value is nonnull.
  3263. bool isAssumedNonNull() const {
  3264. return NonNullAA && NonNullAA->isAssumedNonNull();
  3265. }
  3266. /// Return true if we know that the underlying value is nonnull.
  3267. bool isKnownNonNull() const {
  3268. return NonNullAA && NonNullAA->isKnownNonNull();
  3269. }
  3270. /// Return true if we assume that underlying value is
  3271. /// dereferenceable(_or_null) globally.
  3272. bool isAssumedGlobal() const { return GlobalState.getAssumed(); }
  3273. /// Return true if we know that underlying value is
  3274. /// dereferenceable(_or_null) globally.
  3275. bool isKnownGlobal() const { return GlobalState.getKnown(); }
  3276. /// Return assumed dereferenceable bytes.
  3277. uint32_t getAssumedDereferenceableBytes() const {
  3278. return DerefBytesState.getAssumed();
  3279. }
  3280. /// Return known dereferenceable bytes.
  3281. uint32_t getKnownDereferenceableBytes() const {
  3282. return DerefBytesState.getKnown();
  3283. }
  3284. /// Create an abstract attribute view for the position \p IRP.
  3285. static AADereferenceable &createForPosition(const IRPosition &IRP,
  3286. Attributor &A);
  3287. /// See AbstractAttribute::getName()
  3288. const std::string getName() const override { return "AADereferenceable"; }
  3289. /// See AbstractAttribute::getIdAddr()
  3290. const char *getIdAddr() const override { return &ID; }
  3291. /// This function should return true if the type of the \p AA is
  3292. /// AADereferenceable
  3293. static bool classof(const AbstractAttribute *AA) {
  3294. return (AA->getIdAddr() == &ID);
  3295. }
  3296. /// Unique ID (due to the unique address)
  3297. static const char ID;
  3298. };
  3299. using AAAlignmentStateType =
  3300. IncIntegerState<uint64_t, Value::MaximumAlignment, 1>;
  3301. /// An abstract interface for all align attributes.
  3302. struct AAAlign : public IRAttribute<
  3303. Attribute::Alignment,
  3304. StateWrapper<AAAlignmentStateType, AbstractAttribute>> {
  3305. AAAlign(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3306. /// Return assumed alignment.
  3307. Align getAssumedAlign() const { return Align(getAssumed()); }
  3308. /// Return known alignment.
  3309. Align getKnownAlign() const { return Align(getKnown()); }
  3310. /// See AbstractAttribute::getName()
  3311. const std::string getName() const override { return "AAAlign"; }
  3312. /// See AbstractAttribute::getIdAddr()
  3313. const char *getIdAddr() const override { return &ID; }
  3314. /// This function should return true if the type of the \p AA is AAAlign
  3315. static bool classof(const AbstractAttribute *AA) {
  3316. return (AA->getIdAddr() == &ID);
  3317. }
  3318. /// Create an abstract attribute view for the position \p IRP.
  3319. static AAAlign &createForPosition(const IRPosition &IRP, Attributor &A);
  3320. /// Unique ID (due to the unique address)
  3321. static const char ID;
  3322. };
  3323. /// An abstract interface to track if a value leaves it's defining function
  3324. /// instance.
  3325. /// TODO: We should make it a ternary AA tracking uniqueness, and uniqueness
  3326. /// wrt. the Attributor analysis separately.
  3327. struct AAInstanceInfo : public StateWrapper<BooleanState, AbstractAttribute> {
  3328. AAInstanceInfo(const IRPosition &IRP, Attributor &A)
  3329. : StateWrapper<BooleanState, AbstractAttribute>(IRP) {}
  3330. /// Return true if we know that the underlying value is unique in its scope
  3331. /// wrt. the Attributor analysis. That means it might not be unique but we can
  3332. /// still use pointer equality without risking to represent two instances with
  3333. /// one `llvm::Value`.
  3334. bool isKnownUniqueForAnalysis() const { return isKnown(); }
  3335. /// Return true if we assume that the underlying value is unique in its scope
  3336. /// wrt. the Attributor analysis. That means it might not be unique but we can
  3337. /// still use pointer equality without risking to represent two instances with
  3338. /// one `llvm::Value`.
  3339. bool isAssumedUniqueForAnalysis() const { return isAssumed(); }
  3340. /// Create an abstract attribute view for the position \p IRP.
  3341. static AAInstanceInfo &createForPosition(const IRPosition &IRP,
  3342. Attributor &A);
  3343. /// See AbstractAttribute::getName()
  3344. const std::string getName() const override { return "AAInstanceInfo"; }
  3345. /// See AbstractAttribute::getIdAddr()
  3346. const char *getIdAddr() const override { return &ID; }
  3347. /// This function should return true if the type of the \p AA is
  3348. /// AAInstanceInfo
  3349. static bool classof(const AbstractAttribute *AA) {
  3350. return (AA->getIdAddr() == &ID);
  3351. }
  3352. /// Unique ID (due to the unique address)
  3353. static const char ID;
  3354. };
  3355. /// An abstract interface for all nocapture attributes.
  3356. struct AANoCapture
  3357. : public IRAttribute<
  3358. Attribute::NoCapture,
  3359. StateWrapper<BitIntegerState<uint16_t, 7, 0>, AbstractAttribute>> {
  3360. AANoCapture(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3361. /// State encoding bits. A set bit in the state means the property holds.
  3362. /// NO_CAPTURE is the best possible state, 0 the worst possible state.
  3363. enum {
  3364. NOT_CAPTURED_IN_MEM = 1 << 0,
  3365. NOT_CAPTURED_IN_INT = 1 << 1,
  3366. NOT_CAPTURED_IN_RET = 1 << 2,
  3367. /// If we do not capture the value in memory or through integers we can only
  3368. /// communicate it back as a derived pointer.
  3369. NO_CAPTURE_MAYBE_RETURNED = NOT_CAPTURED_IN_MEM | NOT_CAPTURED_IN_INT,
  3370. /// If we do not capture the value in memory, through integers, or as a
  3371. /// derived pointer we know it is not captured.
  3372. NO_CAPTURE =
  3373. NOT_CAPTURED_IN_MEM | NOT_CAPTURED_IN_INT | NOT_CAPTURED_IN_RET,
  3374. };
  3375. /// Return true if we know that the underlying value is not captured in its
  3376. /// respective scope.
  3377. bool isKnownNoCapture() const { return isKnown(NO_CAPTURE); }
  3378. /// Return true if we assume that the underlying value is not captured in its
  3379. /// respective scope.
  3380. bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE); }
  3381. /// Return true if we know that the underlying value is not captured in its
  3382. /// respective scope but we allow it to escape through a "return".
  3383. bool isKnownNoCaptureMaybeReturned() const {
  3384. return isKnown(NO_CAPTURE_MAYBE_RETURNED);
  3385. }
  3386. /// Return true if we assume that the underlying value is not captured in its
  3387. /// respective scope but we allow it to escape through a "return".
  3388. bool isAssumedNoCaptureMaybeReturned() const {
  3389. return isAssumed(NO_CAPTURE_MAYBE_RETURNED);
  3390. }
  3391. /// Create an abstract attribute view for the position \p IRP.
  3392. static AANoCapture &createForPosition(const IRPosition &IRP, Attributor &A);
  3393. /// See AbstractAttribute::getName()
  3394. const std::string getName() const override { return "AANoCapture"; }
  3395. /// See AbstractAttribute::getIdAddr()
  3396. const char *getIdAddr() const override { return &ID; }
  3397. /// This function should return true if the type of the \p AA is AANoCapture
  3398. static bool classof(const AbstractAttribute *AA) {
  3399. return (AA->getIdAddr() == &ID);
  3400. }
  3401. /// Unique ID (due to the unique address)
  3402. static const char ID;
  3403. };
  3404. struct ValueSimplifyStateType : public AbstractState {
  3405. ValueSimplifyStateType(Type *Ty) : Ty(Ty) {}
  3406. static ValueSimplifyStateType getBestState(Type *Ty) {
  3407. return ValueSimplifyStateType(Ty);
  3408. }
  3409. static ValueSimplifyStateType getBestState(const ValueSimplifyStateType &VS) {
  3410. return getBestState(VS.Ty);
  3411. }
  3412. /// Return the worst possible representable state.
  3413. static ValueSimplifyStateType getWorstState(Type *Ty) {
  3414. ValueSimplifyStateType DS(Ty);
  3415. DS.indicatePessimisticFixpoint();
  3416. return DS;
  3417. }
  3418. static ValueSimplifyStateType
  3419. getWorstState(const ValueSimplifyStateType &VS) {
  3420. return getWorstState(VS.Ty);
  3421. }
  3422. /// See AbstractState::isValidState(...)
  3423. bool isValidState() const override { return BS.isValidState(); }
  3424. /// See AbstractState::isAtFixpoint(...)
  3425. bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
  3426. /// Return the assumed state encoding.
  3427. ValueSimplifyStateType getAssumed() { return *this; }
  3428. const ValueSimplifyStateType &getAssumed() const { return *this; }
  3429. /// See AbstractState::indicatePessimisticFixpoint(...)
  3430. ChangeStatus indicatePessimisticFixpoint() override {
  3431. return BS.indicatePessimisticFixpoint();
  3432. }
  3433. /// See AbstractState::indicateOptimisticFixpoint(...)
  3434. ChangeStatus indicateOptimisticFixpoint() override {
  3435. return BS.indicateOptimisticFixpoint();
  3436. }
  3437. /// "Clamp" this state with \p PVS.
  3438. ValueSimplifyStateType operator^=(const ValueSimplifyStateType &VS) {
  3439. BS ^= VS.BS;
  3440. unionAssumed(VS.SimplifiedAssociatedValue);
  3441. return *this;
  3442. }
  3443. bool operator==(const ValueSimplifyStateType &RHS) const {
  3444. if (isValidState() != RHS.isValidState())
  3445. return false;
  3446. if (!isValidState() && !RHS.isValidState())
  3447. return true;
  3448. return SimplifiedAssociatedValue == RHS.SimplifiedAssociatedValue;
  3449. }
  3450. protected:
  3451. /// The type of the original value.
  3452. Type *Ty;
  3453. /// Merge \p Other into the currently assumed simplified value
  3454. bool unionAssumed(std::optional<Value *> Other);
  3455. /// Helper to track validity and fixpoint
  3456. BooleanState BS;
  3457. /// An assumed simplified value. Initially, it is set to std::nullopt, which
  3458. /// means that the value is not clear under current assumption. If in the
  3459. /// pessimistic state, getAssumedSimplifiedValue doesn't return this value but
  3460. /// returns orignal associated value.
  3461. std::optional<Value *> SimplifiedAssociatedValue;
  3462. };
  3463. /// An abstract interface for value simplify abstract attribute.
  3464. struct AAValueSimplify
  3465. : public StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *> {
  3466. using Base = StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *>;
  3467. AAValueSimplify(const IRPosition &IRP, Attributor &A)
  3468. : Base(IRP, IRP.getAssociatedType()) {}
  3469. /// Create an abstract attribute view for the position \p IRP.
  3470. static AAValueSimplify &createForPosition(const IRPosition &IRP,
  3471. Attributor &A);
  3472. /// See AbstractAttribute::getName()
  3473. const std::string getName() const override { return "AAValueSimplify"; }
  3474. /// See AbstractAttribute::getIdAddr()
  3475. const char *getIdAddr() const override { return &ID; }
  3476. /// This function should return true if the type of the \p AA is
  3477. /// AAValueSimplify
  3478. static bool classof(const AbstractAttribute *AA) {
  3479. return (AA->getIdAddr() == &ID);
  3480. }
  3481. /// Unique ID (due to the unique address)
  3482. static const char ID;
  3483. private:
  3484. /// Return an assumed simplified value if a single candidate is found. If
  3485. /// there cannot be one, return original value. If it is not clear yet, return
  3486. /// std::nullopt.
  3487. ///
  3488. /// Use `Attributor::getAssumedSimplified` for value simplification.
  3489. virtual std::optional<Value *>
  3490. getAssumedSimplifiedValue(Attributor &A) const = 0;
  3491. friend struct Attributor;
  3492. };
  3493. struct AAHeapToStack : public StateWrapper<BooleanState, AbstractAttribute> {
  3494. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  3495. AAHeapToStack(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  3496. /// Returns true if HeapToStack conversion is assumed to be possible.
  3497. virtual bool isAssumedHeapToStack(const CallBase &CB) const = 0;
  3498. /// Returns true if HeapToStack conversion is assumed and the CB is a
  3499. /// callsite to a free operation to be removed.
  3500. virtual bool isAssumedHeapToStackRemovedFree(CallBase &CB) const = 0;
  3501. /// Create an abstract attribute view for the position \p IRP.
  3502. static AAHeapToStack &createForPosition(const IRPosition &IRP, Attributor &A);
  3503. /// See AbstractAttribute::getName()
  3504. const std::string getName() const override { return "AAHeapToStack"; }
  3505. /// See AbstractAttribute::getIdAddr()
  3506. const char *getIdAddr() const override { return &ID; }
  3507. /// This function should return true if the type of the \p AA is AAHeapToStack
  3508. static bool classof(const AbstractAttribute *AA) {
  3509. return (AA->getIdAddr() == &ID);
  3510. }
  3511. /// Unique ID (due to the unique address)
  3512. static const char ID;
  3513. };
  3514. /// An abstract interface for privatizability.
  3515. ///
  3516. /// A pointer is privatizable if it can be replaced by a new, private one.
  3517. /// Privatizing pointer reduces the use count, interaction between unrelated
  3518. /// code parts.
  3519. ///
  3520. /// In order for a pointer to be privatizable its value cannot be observed
  3521. /// (=nocapture), it is (for now) not written (=readonly & noalias), we know
  3522. /// what values are necessary to make the private copy look like the original
  3523. /// one, and the values we need can be loaded (=dereferenceable).
  3524. struct AAPrivatizablePtr
  3525. : public StateWrapper<BooleanState, AbstractAttribute> {
  3526. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  3527. AAPrivatizablePtr(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  3528. /// Returns true if pointer privatization is assumed to be possible.
  3529. bool isAssumedPrivatizablePtr() const { return getAssumed(); }
  3530. /// Returns true if pointer privatization is known to be possible.
  3531. bool isKnownPrivatizablePtr() const { return getKnown(); }
  3532. /// Return the type we can choose for a private copy of the underlying
  3533. /// value. std::nullopt means it is not clear yet, nullptr means there is
  3534. /// none.
  3535. virtual std::optional<Type *> getPrivatizableType() const = 0;
  3536. /// Create an abstract attribute view for the position \p IRP.
  3537. static AAPrivatizablePtr &createForPosition(const IRPosition &IRP,
  3538. Attributor &A);
  3539. /// See AbstractAttribute::getName()
  3540. const std::string getName() const override { return "AAPrivatizablePtr"; }
  3541. /// See AbstractAttribute::getIdAddr()
  3542. const char *getIdAddr() const override { return &ID; }
  3543. /// This function should return true if the type of the \p AA is
  3544. /// AAPricatizablePtr
  3545. static bool classof(const AbstractAttribute *AA) {
  3546. return (AA->getIdAddr() == &ID);
  3547. }
  3548. /// Unique ID (due to the unique address)
  3549. static const char ID;
  3550. };
  3551. /// An abstract interface for memory access kind related attributes
  3552. /// (readnone/readonly/writeonly).
  3553. struct AAMemoryBehavior
  3554. : public IRAttribute<
  3555. Attribute::ReadNone,
  3556. StateWrapper<BitIntegerState<uint8_t, 3>, AbstractAttribute>> {
  3557. AAMemoryBehavior(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3558. /// State encoding bits. A set bit in the state means the property holds.
  3559. /// BEST_STATE is the best possible state, 0 the worst possible state.
  3560. enum {
  3561. NO_READS = 1 << 0,
  3562. NO_WRITES = 1 << 1,
  3563. NO_ACCESSES = NO_READS | NO_WRITES,
  3564. BEST_STATE = NO_ACCESSES,
  3565. };
  3566. static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
  3567. /// Return true if we know that the underlying value is not read or accessed
  3568. /// in its respective scope.
  3569. bool isKnownReadNone() const { return isKnown(NO_ACCESSES); }
  3570. /// Return true if we assume that the underlying value is not read or accessed
  3571. /// in its respective scope.
  3572. bool isAssumedReadNone() const { return isAssumed(NO_ACCESSES); }
  3573. /// Return true if we know that the underlying value is not accessed
  3574. /// (=written) in its respective scope.
  3575. bool isKnownReadOnly() const { return isKnown(NO_WRITES); }
  3576. /// Return true if we assume that the underlying value is not accessed
  3577. /// (=written) in its respective scope.
  3578. bool isAssumedReadOnly() const { return isAssumed(NO_WRITES); }
  3579. /// Return true if we know that the underlying value is not read in its
  3580. /// respective scope.
  3581. bool isKnownWriteOnly() const { return isKnown(NO_READS); }
  3582. /// Return true if we assume that the underlying value is not read in its
  3583. /// respective scope.
  3584. bool isAssumedWriteOnly() const { return isAssumed(NO_READS); }
  3585. /// Create an abstract attribute view for the position \p IRP.
  3586. static AAMemoryBehavior &createForPosition(const IRPosition &IRP,
  3587. Attributor &A);
  3588. /// See AbstractAttribute::getName()
  3589. const std::string getName() const override { return "AAMemoryBehavior"; }
  3590. /// See AbstractAttribute::getIdAddr()
  3591. const char *getIdAddr() const override { return &ID; }
  3592. /// This function should return true if the type of the \p AA is
  3593. /// AAMemoryBehavior
  3594. static bool classof(const AbstractAttribute *AA) {
  3595. return (AA->getIdAddr() == &ID);
  3596. }
  3597. /// Unique ID (due to the unique address)
  3598. static const char ID;
  3599. };
  3600. /// An abstract interface for all memory location attributes
  3601. /// (readnone/argmemonly/inaccessiblememonly/inaccessibleorargmemonly).
  3602. struct AAMemoryLocation
  3603. : public IRAttribute<
  3604. Attribute::ReadNone,
  3605. StateWrapper<BitIntegerState<uint32_t, 511>, AbstractAttribute>> {
  3606. using MemoryLocationsKind = StateType::base_t;
  3607. AAMemoryLocation(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  3608. /// Encoding of different locations that could be accessed by a memory
  3609. /// access.
  3610. enum {
  3611. ALL_LOCATIONS = 0,
  3612. NO_LOCAL_MEM = 1 << 0,
  3613. NO_CONST_MEM = 1 << 1,
  3614. NO_GLOBAL_INTERNAL_MEM = 1 << 2,
  3615. NO_GLOBAL_EXTERNAL_MEM = 1 << 3,
  3616. NO_GLOBAL_MEM = NO_GLOBAL_INTERNAL_MEM | NO_GLOBAL_EXTERNAL_MEM,
  3617. NO_ARGUMENT_MEM = 1 << 4,
  3618. NO_INACCESSIBLE_MEM = 1 << 5,
  3619. NO_MALLOCED_MEM = 1 << 6,
  3620. NO_UNKOWN_MEM = 1 << 7,
  3621. NO_LOCATIONS = NO_LOCAL_MEM | NO_CONST_MEM | NO_GLOBAL_INTERNAL_MEM |
  3622. NO_GLOBAL_EXTERNAL_MEM | NO_ARGUMENT_MEM |
  3623. NO_INACCESSIBLE_MEM | NO_MALLOCED_MEM | NO_UNKOWN_MEM,
  3624. // Helper bit to track if we gave up or not.
  3625. VALID_STATE = NO_LOCATIONS + 1,
  3626. BEST_STATE = NO_LOCATIONS | VALID_STATE,
  3627. };
  3628. static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
  3629. /// Return true if we know that the associated functions has no observable
  3630. /// accesses.
  3631. bool isKnownReadNone() const { return isKnown(NO_LOCATIONS); }
  3632. /// Return true if we assume that the associated functions has no observable
  3633. /// accesses.
  3634. bool isAssumedReadNone() const {
  3635. return isAssumed(NO_LOCATIONS) || isAssumedStackOnly();
  3636. }
  3637. /// Return true if we know that the associated functions has at most
  3638. /// local/stack accesses.
  3639. bool isKnowStackOnly() const {
  3640. return isKnown(inverseLocation(NO_LOCAL_MEM, true, true));
  3641. }
  3642. /// Return true if we assume that the associated functions has at most
  3643. /// local/stack accesses.
  3644. bool isAssumedStackOnly() const {
  3645. return isAssumed(inverseLocation(NO_LOCAL_MEM, true, true));
  3646. }
  3647. /// Return true if we know that the underlying value will only access
  3648. /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
  3649. bool isKnownInaccessibleMemOnly() const {
  3650. return isKnown(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
  3651. }
  3652. /// Return true if we assume that the underlying value will only access
  3653. /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
  3654. bool isAssumedInaccessibleMemOnly() const {
  3655. return isAssumed(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
  3656. }
  3657. /// Return true if we know that the underlying value will only access
  3658. /// argument pointees (see Attribute::ArgMemOnly).
  3659. bool isKnownArgMemOnly() const {
  3660. return isKnown(inverseLocation(NO_ARGUMENT_MEM, true, true));
  3661. }
  3662. /// Return true if we assume that the underlying value will only access
  3663. /// argument pointees (see Attribute::ArgMemOnly).
  3664. bool isAssumedArgMemOnly() const {
  3665. return isAssumed(inverseLocation(NO_ARGUMENT_MEM, true, true));
  3666. }
  3667. /// Return true if we know that the underlying value will only access
  3668. /// inaccesible memory or argument pointees (see
  3669. /// Attribute::InaccessibleOrArgMemOnly).
  3670. bool isKnownInaccessibleOrArgMemOnly() const {
  3671. return isKnown(
  3672. inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
  3673. }
  3674. /// Return true if we assume that the underlying value will only access
  3675. /// inaccesible memory or argument pointees (see
  3676. /// Attribute::InaccessibleOrArgMemOnly).
  3677. bool isAssumedInaccessibleOrArgMemOnly() const {
  3678. return isAssumed(
  3679. inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
  3680. }
  3681. /// Return true if the underlying value may access memory through arguement
  3682. /// pointers of the associated function, if any.
  3683. bool mayAccessArgMem() const { return !isAssumed(NO_ARGUMENT_MEM); }
  3684. /// Return true if only the memory locations specififed by \p MLK are assumed
  3685. /// to be accessed by the associated function.
  3686. bool isAssumedSpecifiedMemOnly(MemoryLocationsKind MLK) const {
  3687. return isAssumed(MLK);
  3688. }
  3689. /// Return the locations that are assumed to be not accessed by the associated
  3690. /// function, if any.
  3691. MemoryLocationsKind getAssumedNotAccessedLocation() const {
  3692. return getAssumed();
  3693. }
  3694. /// Return the inverse of location \p Loc, thus for NO_XXX the return
  3695. /// describes ONLY_XXX. The flags \p AndLocalMem and \p AndConstMem determine
  3696. /// if local (=stack) and constant memory are allowed as well. Most of the
  3697. /// time we do want them to be included, e.g., argmemonly allows accesses via
  3698. /// argument pointers or local or constant memory accesses.
  3699. static MemoryLocationsKind
  3700. inverseLocation(MemoryLocationsKind Loc, bool AndLocalMem, bool AndConstMem) {
  3701. return NO_LOCATIONS & ~(Loc | (AndLocalMem ? NO_LOCAL_MEM : 0) |
  3702. (AndConstMem ? NO_CONST_MEM : 0));
  3703. };
  3704. /// Return the locations encoded by \p MLK as a readable string.
  3705. static std::string getMemoryLocationsAsStr(MemoryLocationsKind MLK);
  3706. /// Simple enum to distinguish read/write/read-write accesses.
  3707. enum AccessKind {
  3708. NONE = 0,
  3709. READ = 1 << 0,
  3710. WRITE = 1 << 1,
  3711. READ_WRITE = READ | WRITE,
  3712. };
  3713. /// Check \p Pred on all accesses to the memory kinds specified by \p MLK.
  3714. ///
  3715. /// This method will evaluate \p Pred on all accesses (access instruction +
  3716. /// underlying accessed memory pointer) and it will return true if \p Pred
  3717. /// holds every time.
  3718. virtual bool checkForAllAccessesToMemoryKind(
  3719. function_ref<bool(const Instruction *, const Value *, AccessKind,
  3720. MemoryLocationsKind)>
  3721. Pred,
  3722. MemoryLocationsKind MLK) const = 0;
  3723. /// Create an abstract attribute view for the position \p IRP.
  3724. static AAMemoryLocation &createForPosition(const IRPosition &IRP,
  3725. Attributor &A);
  3726. /// See AbstractState::getAsStr().
  3727. const std::string getAsStr() const override {
  3728. return getMemoryLocationsAsStr(getAssumedNotAccessedLocation());
  3729. }
  3730. /// See AbstractAttribute::getName()
  3731. const std::string getName() const override { return "AAMemoryLocation"; }
  3732. /// See AbstractAttribute::getIdAddr()
  3733. const char *getIdAddr() const override { return &ID; }
  3734. /// This function should return true if the type of the \p AA is
  3735. /// AAMemoryLocation
  3736. static bool classof(const AbstractAttribute *AA) {
  3737. return (AA->getIdAddr() == &ID);
  3738. }
  3739. /// Unique ID (due to the unique address)
  3740. static const char ID;
  3741. };
  3742. /// An abstract interface for range value analysis.
  3743. struct AAValueConstantRange
  3744. : public StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t> {
  3745. using Base = StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t>;
  3746. AAValueConstantRange(const IRPosition &IRP, Attributor &A)
  3747. : Base(IRP, IRP.getAssociatedType()->getIntegerBitWidth()) {}
  3748. /// See AbstractAttribute::getState(...).
  3749. IntegerRangeState &getState() override { return *this; }
  3750. const IntegerRangeState &getState() const override { return *this; }
  3751. /// Create an abstract attribute view for the position \p IRP.
  3752. static AAValueConstantRange &createForPosition(const IRPosition &IRP,
  3753. Attributor &A);
  3754. /// Return an assumed range for the associated value a program point \p CtxI.
  3755. /// If \p I is nullptr, simply return an assumed range.
  3756. virtual ConstantRange
  3757. getAssumedConstantRange(Attributor &A,
  3758. const Instruction *CtxI = nullptr) const = 0;
  3759. /// Return a known range for the associated value at a program point \p CtxI.
  3760. /// If \p I is nullptr, simply return a known range.
  3761. virtual ConstantRange
  3762. getKnownConstantRange(Attributor &A,
  3763. const Instruction *CtxI = nullptr) const = 0;
  3764. /// Return an assumed constant for the associated value a program point \p
  3765. /// CtxI.
  3766. std::optional<Constant *>
  3767. getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
  3768. ConstantRange RangeV = getAssumedConstantRange(A, CtxI);
  3769. if (auto *C = RangeV.getSingleElement()) {
  3770. Type *Ty = getAssociatedValue().getType();
  3771. return cast_or_null<Constant>(
  3772. AA::getWithType(*ConstantInt::get(Ty->getContext(), *C), *Ty));
  3773. }
  3774. if (RangeV.isEmptySet())
  3775. return std::nullopt;
  3776. return nullptr;
  3777. }
  3778. /// See AbstractAttribute::getName()
  3779. const std::string getName() const override { return "AAValueConstantRange"; }
  3780. /// See AbstractAttribute::getIdAddr()
  3781. const char *getIdAddr() const override { return &ID; }
  3782. /// This function should return true if the type of the \p AA is
  3783. /// AAValueConstantRange
  3784. static bool classof(const AbstractAttribute *AA) {
  3785. return (AA->getIdAddr() == &ID);
  3786. }
  3787. /// Unique ID (due to the unique address)
  3788. static const char ID;
  3789. };
  3790. /// A class for a set state.
  3791. /// The assumed boolean state indicates whether the corresponding set is full
  3792. /// set or not. If the assumed state is false, this is the worst state. The
  3793. /// worst state (invalid state) of set of potential values is when the set
  3794. /// contains every possible value (i.e. we cannot in any way limit the value
  3795. /// that the target position can take). That never happens naturally, we only
  3796. /// force it. As for the conditions under which we force it, see
  3797. /// AAPotentialConstantValues.
  3798. template <typename MemberTy> struct PotentialValuesState : AbstractState {
  3799. using SetTy = SmallSetVector<MemberTy, 8>;
  3800. PotentialValuesState() : IsValidState(true), UndefIsContained(false) {}
  3801. PotentialValuesState(bool IsValid)
  3802. : IsValidState(IsValid), UndefIsContained(false) {}
  3803. /// See AbstractState::isValidState(...)
  3804. bool isValidState() const override { return IsValidState.isValidState(); }
  3805. /// See AbstractState::isAtFixpoint(...)
  3806. bool isAtFixpoint() const override { return IsValidState.isAtFixpoint(); }
  3807. /// See AbstractState::indicatePessimisticFixpoint(...)
  3808. ChangeStatus indicatePessimisticFixpoint() override {
  3809. return IsValidState.indicatePessimisticFixpoint();
  3810. }
  3811. /// See AbstractState::indicateOptimisticFixpoint(...)
  3812. ChangeStatus indicateOptimisticFixpoint() override {
  3813. return IsValidState.indicateOptimisticFixpoint();
  3814. }
  3815. /// Return the assumed state
  3816. PotentialValuesState &getAssumed() { return *this; }
  3817. const PotentialValuesState &getAssumed() const { return *this; }
  3818. /// Return this set. We should check whether this set is valid or not by
  3819. /// isValidState() before calling this function.
  3820. const SetTy &getAssumedSet() const {
  3821. assert(isValidState() && "This set shoud not be used when it is invalid!");
  3822. return Set;
  3823. }
  3824. /// Returns whether this state contains an undef value or not.
  3825. bool undefIsContained() const {
  3826. assert(isValidState() && "This flag shoud not be used when it is invalid!");
  3827. return UndefIsContained;
  3828. }
  3829. bool operator==(const PotentialValuesState &RHS) const {
  3830. if (isValidState() != RHS.isValidState())
  3831. return false;
  3832. if (!isValidState() && !RHS.isValidState())
  3833. return true;
  3834. if (undefIsContained() != RHS.undefIsContained())
  3835. return false;
  3836. return Set == RHS.getAssumedSet();
  3837. }
  3838. /// Maximum number of potential values to be tracked.
  3839. /// This is set by -attributor-max-potential-values command line option
  3840. static unsigned MaxPotentialValues;
  3841. /// Return empty set as the best state of potential values.
  3842. static PotentialValuesState getBestState() {
  3843. return PotentialValuesState(true);
  3844. }
  3845. static PotentialValuesState getBestState(const PotentialValuesState &PVS) {
  3846. return getBestState();
  3847. }
  3848. /// Return full set as the worst state of potential values.
  3849. static PotentialValuesState getWorstState() {
  3850. return PotentialValuesState(false);
  3851. }
  3852. /// Union assumed set with the passed value.
  3853. void unionAssumed(const MemberTy &C) { insert(C); }
  3854. /// Union assumed set with assumed set of the passed state \p PVS.
  3855. void unionAssumed(const PotentialValuesState &PVS) { unionWith(PVS); }
  3856. /// Union assumed set with an undef value.
  3857. void unionAssumedWithUndef() { unionWithUndef(); }
  3858. /// "Clamp" this state with \p PVS.
  3859. PotentialValuesState operator^=(const PotentialValuesState &PVS) {
  3860. IsValidState ^= PVS.IsValidState;
  3861. unionAssumed(PVS);
  3862. return *this;
  3863. }
  3864. PotentialValuesState operator&=(const PotentialValuesState &PVS) {
  3865. IsValidState &= PVS.IsValidState;
  3866. unionAssumed(PVS);
  3867. return *this;
  3868. }
  3869. bool contains(const MemberTy &V) const {
  3870. return !isValidState() ? true : Set.contains(V);
  3871. }
  3872. protected:
  3873. SetTy &getAssumedSet() {
  3874. assert(isValidState() && "This set shoud not be used when it is invalid!");
  3875. return Set;
  3876. }
  3877. private:
  3878. /// Check the size of this set, and invalidate when the size is no
  3879. /// less than \p MaxPotentialValues threshold.
  3880. void checkAndInvalidate() {
  3881. if (Set.size() >= MaxPotentialValues)
  3882. indicatePessimisticFixpoint();
  3883. else
  3884. reduceUndefValue();
  3885. }
  3886. /// If this state contains both undef and not undef, we can reduce
  3887. /// undef to the not undef value.
  3888. void reduceUndefValue() { UndefIsContained = UndefIsContained & Set.empty(); }
  3889. /// Insert an element into this set.
  3890. void insert(const MemberTy &C) {
  3891. if (!isValidState())
  3892. return;
  3893. Set.insert(C);
  3894. checkAndInvalidate();
  3895. }
  3896. /// Take union with R.
  3897. void unionWith(const PotentialValuesState &R) {
  3898. /// If this is a full set, do nothing.
  3899. if (!isValidState())
  3900. return;
  3901. /// If R is full set, change L to a full set.
  3902. if (!R.isValidState()) {
  3903. indicatePessimisticFixpoint();
  3904. return;
  3905. }
  3906. for (const MemberTy &C : R.Set)
  3907. Set.insert(C);
  3908. UndefIsContained |= R.undefIsContained();
  3909. checkAndInvalidate();
  3910. }
  3911. /// Take union with an undef value.
  3912. void unionWithUndef() {
  3913. UndefIsContained = true;
  3914. reduceUndefValue();
  3915. }
  3916. /// Take intersection with R.
  3917. void intersectWith(const PotentialValuesState &R) {
  3918. /// If R is a full set, do nothing.
  3919. if (!R.isValidState())
  3920. return;
  3921. /// If this is a full set, change this to R.
  3922. if (!isValidState()) {
  3923. *this = R;
  3924. return;
  3925. }
  3926. SetTy IntersectSet;
  3927. for (const MemberTy &C : Set) {
  3928. if (R.Set.count(C))
  3929. IntersectSet.insert(C);
  3930. }
  3931. Set = IntersectSet;
  3932. UndefIsContained &= R.undefIsContained();
  3933. reduceUndefValue();
  3934. }
  3935. /// A helper state which indicate whether this state is valid or not.
  3936. BooleanState IsValidState;
  3937. /// Container for potential values
  3938. SetTy Set;
  3939. /// Flag for undef value
  3940. bool UndefIsContained;
  3941. };
  3942. using PotentialConstantIntValuesState = PotentialValuesState<APInt>;
  3943. using PotentialLLVMValuesState =
  3944. PotentialValuesState<std::pair<AA::ValueAndContext, AA::ValueScope>>;
  3945. raw_ostream &operator<<(raw_ostream &OS,
  3946. const PotentialConstantIntValuesState &R);
  3947. raw_ostream &operator<<(raw_ostream &OS, const PotentialLLVMValuesState &R);
  3948. /// An abstract interface for potential values analysis.
  3949. ///
  3950. /// This AA collects potential values for each IR position.
  3951. /// An assumed set of potential values is initialized with the empty set (the
  3952. /// best state) and it will grow monotonically as we find more potential values
  3953. /// for this position.
  3954. /// The set might be forced to the worst state, that is, to contain every
  3955. /// possible value for this position in 2 cases.
  3956. /// 1. We surpassed the \p MaxPotentialValues threshold. This includes the
  3957. /// case that this position is affected (e.g. because of an operation) by a
  3958. /// Value that is in the worst state.
  3959. /// 2. We tried to initialize on a Value that we cannot handle (e.g. an
  3960. /// operator we do not currently handle).
  3961. ///
  3962. /// For non constant integers see AAPotentialValues.
  3963. struct AAPotentialConstantValues
  3964. : public StateWrapper<PotentialConstantIntValuesState, AbstractAttribute> {
  3965. using Base = StateWrapper<PotentialConstantIntValuesState, AbstractAttribute>;
  3966. AAPotentialConstantValues(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  3967. /// See AbstractAttribute::getState(...).
  3968. PotentialConstantIntValuesState &getState() override { return *this; }
  3969. const PotentialConstantIntValuesState &getState() const override {
  3970. return *this;
  3971. }
  3972. /// Create an abstract attribute view for the position \p IRP.
  3973. static AAPotentialConstantValues &createForPosition(const IRPosition &IRP,
  3974. Attributor &A);
  3975. /// Return assumed constant for the associated value
  3976. std::optional<Constant *>
  3977. getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
  3978. if (!isValidState())
  3979. return nullptr;
  3980. if (getAssumedSet().size() == 1) {
  3981. Type *Ty = getAssociatedValue().getType();
  3982. return cast_or_null<Constant>(AA::getWithType(
  3983. *ConstantInt::get(Ty->getContext(), *(getAssumedSet().begin())),
  3984. *Ty));
  3985. }
  3986. if (getAssumedSet().size() == 0) {
  3987. if (undefIsContained())
  3988. return UndefValue::get(getAssociatedValue().getType());
  3989. return std::nullopt;
  3990. }
  3991. return nullptr;
  3992. }
  3993. /// See AbstractAttribute::getName()
  3994. const std::string getName() const override {
  3995. return "AAPotentialConstantValues";
  3996. }
  3997. /// See AbstractAttribute::getIdAddr()
  3998. const char *getIdAddr() const override { return &ID; }
  3999. /// This function should return true if the type of the \p AA is
  4000. /// AAPotentialConstantValues
  4001. static bool classof(const AbstractAttribute *AA) {
  4002. return (AA->getIdAddr() == &ID);
  4003. }
  4004. /// Unique ID (due to the unique address)
  4005. static const char ID;
  4006. };
  4007. struct AAPotentialValues
  4008. : public StateWrapper<PotentialLLVMValuesState, AbstractAttribute> {
  4009. using Base = StateWrapper<PotentialLLVMValuesState, AbstractAttribute>;
  4010. AAPotentialValues(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  4011. /// See AbstractAttribute::getState(...).
  4012. PotentialLLVMValuesState &getState() override { return *this; }
  4013. const PotentialLLVMValuesState &getState() const override { return *this; }
  4014. /// Create an abstract attribute view for the position \p IRP.
  4015. static AAPotentialValues &createForPosition(const IRPosition &IRP,
  4016. Attributor &A);
  4017. /// Extract the single value in \p Values if any.
  4018. static Value *getSingleValue(Attributor &A, const AbstractAttribute &AA,
  4019. const IRPosition &IRP,
  4020. SmallVectorImpl<AA::ValueAndContext> &Values);
  4021. /// See AbstractAttribute::getName()
  4022. const std::string getName() const override { return "AAPotentialValues"; }
  4023. /// See AbstractAttribute::getIdAddr()
  4024. const char *getIdAddr() const override { return &ID; }
  4025. /// This function should return true if the type of the \p AA is
  4026. /// AAPotentialValues
  4027. static bool classof(const AbstractAttribute *AA) {
  4028. return (AA->getIdAddr() == &ID);
  4029. }
  4030. /// Unique ID (due to the unique address)
  4031. static const char ID;
  4032. private:
  4033. virtual bool
  4034. getAssumedSimplifiedValues(Attributor &A,
  4035. SmallVectorImpl<AA::ValueAndContext> &Values,
  4036. AA::ValueScope) const = 0;
  4037. friend struct Attributor;
  4038. };
  4039. /// An abstract interface for all noundef attributes.
  4040. struct AANoUndef
  4041. : public IRAttribute<Attribute::NoUndef,
  4042. StateWrapper<BooleanState, AbstractAttribute>> {
  4043. AANoUndef(const IRPosition &IRP, Attributor &A) : IRAttribute(IRP) {}
  4044. /// Return true if we assume that the underlying value is noundef.
  4045. bool isAssumedNoUndef() const { return getAssumed(); }
  4046. /// Return true if we know that underlying value is noundef.
  4047. bool isKnownNoUndef() const { return getKnown(); }
  4048. /// Create an abstract attribute view for the position \p IRP.
  4049. static AANoUndef &createForPosition(const IRPosition &IRP, Attributor &A);
  4050. /// See AbstractAttribute::getName()
  4051. const std::string getName() const override { return "AANoUndef"; }
  4052. /// See AbstractAttribute::getIdAddr()
  4053. const char *getIdAddr() const override { return &ID; }
  4054. /// This function should return true if the type of the \p AA is AANoUndef
  4055. static bool classof(const AbstractAttribute *AA) {
  4056. return (AA->getIdAddr() == &ID);
  4057. }
  4058. /// Unique ID (due to the unique address)
  4059. static const char ID;
  4060. };
  4061. struct AACallGraphNode;
  4062. struct AACallEdges;
  4063. /// An Iterator for call edges, creates AACallEdges attributes in a lazy way.
  4064. /// This iterator becomes invalid if the underlying edge list changes.
  4065. /// So This shouldn't outlive a iteration of Attributor.
  4066. class AACallEdgeIterator
  4067. : public iterator_adaptor_base<AACallEdgeIterator,
  4068. SetVector<Function *>::iterator> {
  4069. AACallEdgeIterator(Attributor &A, SetVector<Function *>::iterator Begin)
  4070. : iterator_adaptor_base(Begin), A(A) {}
  4071. public:
  4072. AACallGraphNode *operator*() const;
  4073. private:
  4074. Attributor &A;
  4075. friend AACallEdges;
  4076. friend AttributorCallGraph;
  4077. };
  4078. struct AACallGraphNode {
  4079. AACallGraphNode(Attributor &A) : A(A) {}
  4080. virtual ~AACallGraphNode() = default;
  4081. virtual AACallEdgeIterator optimisticEdgesBegin() const = 0;
  4082. virtual AACallEdgeIterator optimisticEdgesEnd() const = 0;
  4083. /// Iterator range for exploring the call graph.
  4084. iterator_range<AACallEdgeIterator> optimisticEdgesRange() const {
  4085. return iterator_range<AACallEdgeIterator>(optimisticEdgesBegin(),
  4086. optimisticEdgesEnd());
  4087. }
  4088. protected:
  4089. /// Reference to Attributor needed for GraphTraits implementation.
  4090. Attributor &A;
  4091. };
  4092. /// An abstract state for querying live call edges.
  4093. /// This interface uses the Attributor's optimistic liveness
  4094. /// information to compute the edges that are alive.
  4095. struct AACallEdges : public StateWrapper<BooleanState, AbstractAttribute>,
  4096. AACallGraphNode {
  4097. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  4098. AACallEdges(const IRPosition &IRP, Attributor &A)
  4099. : Base(IRP), AACallGraphNode(A) {}
  4100. /// Get the optimistic edges.
  4101. virtual const SetVector<Function *> &getOptimisticEdges() const = 0;
  4102. /// Is there any call with a unknown callee.
  4103. virtual bool hasUnknownCallee() const = 0;
  4104. /// Is there any call with a unknown callee, excluding any inline asm.
  4105. virtual bool hasNonAsmUnknownCallee() const = 0;
  4106. /// Iterator for exploring the call graph.
  4107. AACallEdgeIterator optimisticEdgesBegin() const override {
  4108. return AACallEdgeIterator(A, getOptimisticEdges().begin());
  4109. }
  4110. /// Iterator for exploring the call graph.
  4111. AACallEdgeIterator optimisticEdgesEnd() const override {
  4112. return AACallEdgeIterator(A, getOptimisticEdges().end());
  4113. }
  4114. /// Create an abstract attribute view for the position \p IRP.
  4115. static AACallEdges &createForPosition(const IRPosition &IRP, Attributor &A);
  4116. /// See AbstractAttribute::getName()
  4117. const std::string getName() const override { return "AACallEdges"; }
  4118. /// See AbstractAttribute::getIdAddr()
  4119. const char *getIdAddr() const override { return &ID; }
  4120. /// This function should return true if the type of the \p AA is AACallEdges.
  4121. static bool classof(const AbstractAttribute *AA) {
  4122. return (AA->getIdAddr() == &ID);
  4123. }
  4124. /// Unique ID (due to the unique address)
  4125. static const char ID;
  4126. };
  4127. // Synthetic root node for the Attributor's internal call graph.
  4128. struct AttributorCallGraph : public AACallGraphNode {
  4129. AttributorCallGraph(Attributor &A) : AACallGraphNode(A) {}
  4130. virtual ~AttributorCallGraph() = default;
  4131. AACallEdgeIterator optimisticEdgesBegin() const override {
  4132. return AACallEdgeIterator(A, A.Functions.begin());
  4133. }
  4134. AACallEdgeIterator optimisticEdgesEnd() const override {
  4135. return AACallEdgeIterator(A, A.Functions.end());
  4136. }
  4137. /// Force populate the entire call graph.
  4138. void populateAll() const {
  4139. for (const AACallGraphNode *AA : optimisticEdgesRange()) {
  4140. // Nothing else to do here.
  4141. (void)AA;
  4142. }
  4143. }
  4144. void print();
  4145. };
  4146. template <> struct GraphTraits<AACallGraphNode *> {
  4147. using NodeRef = AACallGraphNode *;
  4148. using ChildIteratorType = AACallEdgeIterator;
  4149. static AACallEdgeIterator child_begin(AACallGraphNode *Node) {
  4150. return Node->optimisticEdgesBegin();
  4151. }
  4152. static AACallEdgeIterator child_end(AACallGraphNode *Node) {
  4153. return Node->optimisticEdgesEnd();
  4154. }
  4155. };
  4156. template <>
  4157. struct GraphTraits<AttributorCallGraph *>
  4158. : public GraphTraits<AACallGraphNode *> {
  4159. using nodes_iterator = AACallEdgeIterator;
  4160. static AACallGraphNode *getEntryNode(AttributorCallGraph *G) {
  4161. return static_cast<AACallGraphNode *>(G);
  4162. }
  4163. static AACallEdgeIterator nodes_begin(const AttributorCallGraph *G) {
  4164. return G->optimisticEdgesBegin();
  4165. }
  4166. static AACallEdgeIterator nodes_end(const AttributorCallGraph *G) {
  4167. return G->optimisticEdgesEnd();
  4168. }
  4169. };
  4170. template <>
  4171. struct DOTGraphTraits<AttributorCallGraph *> : public DefaultDOTGraphTraits {
  4172. DOTGraphTraits(bool Simple = false) : DefaultDOTGraphTraits(Simple) {}
  4173. std::string getNodeLabel(const AACallGraphNode *Node,
  4174. const AttributorCallGraph *Graph) {
  4175. const AACallEdges *AACE = static_cast<const AACallEdges *>(Node);
  4176. return AACE->getAssociatedFunction()->getName().str();
  4177. }
  4178. static bool isNodeHidden(const AACallGraphNode *Node,
  4179. const AttributorCallGraph *Graph) {
  4180. // Hide the synth root.
  4181. return static_cast<const AACallGraphNode *>(Graph) == Node;
  4182. }
  4183. };
  4184. struct AAExecutionDomain
  4185. : public StateWrapper<BooleanState, AbstractAttribute> {
  4186. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  4187. AAExecutionDomain(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  4188. /// Summary about the execution domain of a block or instruction.
  4189. struct ExecutionDomainTy {
  4190. using BarriersSetTy = SmallPtrSet<CallBase *, 2>;
  4191. using AssumesSetTy = SmallPtrSet<AssumeInst *, 4>;
  4192. void addAssumeInst(Attributor &A, AssumeInst &AI) {
  4193. EncounteredAssumes.insert(&AI);
  4194. }
  4195. void addAlignedBarrier(Attributor &A, CallBase &CB) {
  4196. AlignedBarriers.insert(&CB);
  4197. }
  4198. void clearAssumeInstAndAlignedBarriers() {
  4199. EncounteredAssumes.clear();
  4200. AlignedBarriers.clear();
  4201. }
  4202. bool IsExecutedByInitialThreadOnly = true;
  4203. bool IsReachedFromAlignedBarrierOnly = true;
  4204. bool IsReachingAlignedBarrierOnly = true;
  4205. bool EncounteredNonLocalSideEffect = false;
  4206. BarriersSetTy AlignedBarriers;
  4207. AssumesSetTy EncounteredAssumes;
  4208. };
  4209. /// Create an abstract attribute view for the position \p IRP.
  4210. static AAExecutionDomain &createForPosition(const IRPosition &IRP,
  4211. Attributor &A);
  4212. /// See AbstractAttribute::getName().
  4213. const std::string getName() const override { return "AAExecutionDomain"; }
  4214. /// See AbstractAttribute::getIdAddr().
  4215. const char *getIdAddr() const override { return &ID; }
  4216. /// Check if an instruction is executed only by the initial thread.
  4217. bool isExecutedByInitialThreadOnly(const Instruction &I) const {
  4218. return isExecutedByInitialThreadOnly(*I.getParent());
  4219. }
  4220. /// Check if a basic block is executed only by the initial thread.
  4221. virtual bool isExecutedByInitialThreadOnly(const BasicBlock &) const = 0;
  4222. /// Check if the instruction \p I is executed in an aligned region, that is,
  4223. /// the synchronizing effects before and after \p I are both aligned barriers.
  4224. /// This effectively means all threads execute \p I together.
  4225. virtual bool isExecutedInAlignedRegion(Attributor &A,
  4226. const Instruction &I) const = 0;
  4227. virtual ExecutionDomainTy getExecutionDomain(const BasicBlock &) const = 0;
  4228. virtual ExecutionDomainTy getExecutionDomain(const CallBase &) const = 0;
  4229. virtual ExecutionDomainTy getFunctionExecutionDomain() const = 0;
  4230. /// This function should return true if the type of the \p AA is
  4231. /// AAExecutionDomain.
  4232. static bool classof(const AbstractAttribute *AA) {
  4233. return (AA->getIdAddr() == &ID);
  4234. }
  4235. /// Unique ID (due to the unique address)
  4236. static const char ID;
  4237. };
  4238. /// An abstract Attribute for computing reachability between functions.
  4239. struct AAInterFnReachability
  4240. : public StateWrapper<BooleanState, AbstractAttribute> {
  4241. using Base = StateWrapper<BooleanState, AbstractAttribute>;
  4242. AAInterFnReachability(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
  4243. /// If the function represented by this possition can reach \p Fn.
  4244. bool canReach(Attributor &A, const Function &Fn) const {
  4245. Function *Scope = getAnchorScope();
  4246. if (!Scope || Scope->isDeclaration())
  4247. return true;
  4248. return instructionCanReach(A, Scope->getEntryBlock().front(), Fn);
  4249. }
  4250. /// Can \p Inst reach \p Fn.
  4251. /// See also AA::isPotentiallyReachable.
  4252. virtual bool instructionCanReach(
  4253. Attributor &A, const Instruction &Inst, const Function &Fn,
  4254. const AA::InstExclusionSetTy *ExclusionSet = nullptr,
  4255. SmallPtrSet<const Function *, 16> *Visited = nullptr) const = 0;
  4256. /// Create an abstract attribute view for the position \p IRP.
  4257. static AAInterFnReachability &createForPosition(const IRPosition &IRP,
  4258. Attributor &A);
  4259. /// See AbstractAttribute::getName()
  4260. const std::string getName() const override { return "AAInterFnReachability"; }
  4261. /// See AbstractAttribute::getIdAddr()
  4262. const char *getIdAddr() const override { return &ID; }
  4263. /// This function should return true if the type of the \p AA is AACallEdges.
  4264. static bool classof(const AbstractAttribute *AA) {
  4265. return (AA->getIdAddr() == &ID);
  4266. }
  4267. /// Unique ID (due to the unique address)
  4268. static const char ID;
  4269. };
  4270. /// An abstract interface for struct information.
  4271. struct AAPointerInfo : public AbstractAttribute {
  4272. AAPointerInfo(const IRPosition &IRP) : AbstractAttribute(IRP) {}
  4273. enum AccessKind {
  4274. // First two bits to distinguish may and must accesses.
  4275. AK_MUST = 1 << 0,
  4276. AK_MAY = 1 << 1,
  4277. // Then two bits for read and write. These are not exclusive.
  4278. AK_R = 1 << 2,
  4279. AK_W = 1 << 3,
  4280. AK_RW = AK_R | AK_W,
  4281. // One special case for assumptions about memory content. These
  4282. // are neither reads nor writes. They are however always modeled
  4283. // as read to avoid using them for write removal.
  4284. AK_ASSUMPTION = (1 << 4) | AK_MUST,
  4285. // Helper for easy access.
  4286. AK_MAY_READ = AK_MAY | AK_R,
  4287. AK_MAY_WRITE = AK_MAY | AK_W,
  4288. AK_MAY_READ_WRITE = AK_MAY | AK_R | AK_W,
  4289. AK_MUST_READ = AK_MUST | AK_R,
  4290. AK_MUST_WRITE = AK_MUST | AK_W,
  4291. AK_MUST_READ_WRITE = AK_MUST | AK_R | AK_W,
  4292. };
  4293. /// A container for a list of ranges.
  4294. struct RangeList {
  4295. // The set of ranges rarely contains more than one element, and is unlikely
  4296. // to contain more than say four elements. So we find the middle-ground with
  4297. // a sorted vector. This avoids hard-coding a rarely used number like "four"
  4298. // into every instance of a SmallSet.
  4299. using RangeTy = AA::RangeTy;
  4300. using VecTy = SmallVector<RangeTy>;
  4301. using iterator = VecTy::iterator;
  4302. using const_iterator = VecTy::const_iterator;
  4303. VecTy Ranges;
  4304. RangeList(const RangeTy &R) { Ranges.push_back(R); }
  4305. RangeList(ArrayRef<int64_t> Offsets, int64_t Size) {
  4306. Ranges.reserve(Offsets.size());
  4307. for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
  4308. assert(((i + 1 == e) || Offsets[i] < Offsets[i + 1]) &&
  4309. "Expected strictly ascending offsets.");
  4310. Ranges.emplace_back(Offsets[i], Size);
  4311. }
  4312. }
  4313. RangeList() = default;
  4314. iterator begin() { return Ranges.begin(); }
  4315. iterator end() { return Ranges.end(); }
  4316. const_iterator begin() const { return Ranges.begin(); }
  4317. const_iterator end() const { return Ranges.end(); }
  4318. // Helpers required for std::set_difference
  4319. using value_type = RangeTy;
  4320. void push_back(const RangeTy &R) {
  4321. assert((Ranges.empty() || RangeTy::OffsetLessThan(Ranges.back(), R)) &&
  4322. "Ensure the last element is the greatest.");
  4323. Ranges.push_back(R);
  4324. }
  4325. /// Copy ranges from \p L that are not in \p R, into \p D.
  4326. static void set_difference(const RangeList &L, const RangeList &R,
  4327. RangeList &D) {
  4328. std::set_difference(L.begin(), L.end(), R.begin(), R.end(),
  4329. std::back_inserter(D), RangeTy::OffsetLessThan);
  4330. }
  4331. unsigned size() const { return Ranges.size(); }
  4332. bool operator==(const RangeList &OI) const { return Ranges == OI.Ranges; }
  4333. /// Merge the ranges in \p RHS into the current ranges.
  4334. /// - Merging a list of unknown ranges makes the current list unknown.
  4335. /// - Ranges with the same offset are merged according to RangeTy::operator&
  4336. /// \return true if the current RangeList changed.
  4337. bool merge(const RangeList &RHS) {
  4338. if (isUnknown())
  4339. return false;
  4340. if (RHS.isUnknown()) {
  4341. setUnknown();
  4342. return true;
  4343. }
  4344. if (Ranges.empty()) {
  4345. Ranges = RHS.Ranges;
  4346. return true;
  4347. }
  4348. bool Changed = false;
  4349. auto LPos = Ranges.begin();
  4350. for (auto &R : RHS.Ranges) {
  4351. auto Result = insert(LPos, R);
  4352. if (isUnknown())
  4353. return true;
  4354. LPos = Result.first;
  4355. Changed |= Result.second;
  4356. }
  4357. return Changed;
  4358. }
  4359. /// Insert \p R at the given iterator \p Pos, and merge if necessary.
  4360. ///
  4361. /// This assumes that all ranges before \p Pos are OffsetLessThan \p R, and
  4362. /// then maintains the sorted order for the suffix list.
  4363. ///
  4364. /// \return The place of insertion and true iff anything changed.
  4365. std::pair<iterator, bool> insert(iterator Pos, const RangeTy &R) {
  4366. if (isUnknown())
  4367. return std::make_pair(Ranges.begin(), false);
  4368. if (R.offsetOrSizeAreUnknown()) {
  4369. return std::make_pair(setUnknown(), true);
  4370. }
  4371. // Maintain this as a sorted vector of unique entries.
  4372. auto LB = std::lower_bound(Pos, Ranges.end(), R, RangeTy::OffsetLessThan);
  4373. if (LB == Ranges.end() || LB->Offset != R.Offset)
  4374. return std::make_pair(Ranges.insert(LB, R), true);
  4375. bool Changed = *LB != R;
  4376. *LB &= R;
  4377. if (LB->offsetOrSizeAreUnknown())
  4378. return std::make_pair(setUnknown(), true);
  4379. return std::make_pair(LB, Changed);
  4380. }
  4381. /// Insert the given range \p R, maintaining sorted order.
  4382. ///
  4383. /// \return The place of insertion and true iff anything changed.
  4384. std::pair<iterator, bool> insert(const RangeTy &R) {
  4385. return insert(Ranges.begin(), R);
  4386. }
  4387. /// Add the increment \p Inc to the offset of every range.
  4388. void addToAllOffsets(int64_t Inc) {
  4389. assert(!isUnassigned() &&
  4390. "Cannot increment if the offset is not yet computed!");
  4391. if (isUnknown())
  4392. return;
  4393. for (auto &R : Ranges) {
  4394. R.Offset += Inc;
  4395. }
  4396. }
  4397. /// Return true iff there is exactly one range and it is known.
  4398. bool isUnique() const {
  4399. return Ranges.size() == 1 && !Ranges.front().offsetOrSizeAreUnknown();
  4400. }
  4401. /// Return the unique range, assuming it exists.
  4402. const RangeTy &getUnique() const {
  4403. assert(isUnique() && "No unique range to return!");
  4404. return Ranges.front();
  4405. }
  4406. /// Return true iff the list contains an unknown range.
  4407. bool isUnknown() const {
  4408. if (isUnassigned())
  4409. return false;
  4410. if (Ranges.front().offsetOrSizeAreUnknown()) {
  4411. assert(Ranges.size() == 1 && "Unknown is a singleton range.");
  4412. return true;
  4413. }
  4414. return false;
  4415. }
  4416. /// Discard all ranges and insert a single unknown range.
  4417. iterator setUnknown() {
  4418. Ranges.clear();
  4419. Ranges.push_back(RangeTy::getUnknown());
  4420. return Ranges.begin();
  4421. }
  4422. /// Return true if no ranges have been inserted.
  4423. bool isUnassigned() const { return Ranges.size() == 0; }
  4424. };
  4425. /// An access description.
  4426. struct Access {
  4427. Access(Instruction *I, int64_t Offset, int64_t Size,
  4428. std::optional<Value *> Content, AccessKind Kind, Type *Ty)
  4429. : LocalI(I), RemoteI(I), Content(Content), Ranges(Offset, Size),
  4430. Kind(Kind), Ty(Ty) {
  4431. verify();
  4432. }
  4433. Access(Instruction *LocalI, Instruction *RemoteI, const RangeList &Ranges,
  4434. std::optional<Value *> Content, AccessKind K, Type *Ty)
  4435. : LocalI(LocalI), RemoteI(RemoteI), Content(Content), Ranges(Ranges),
  4436. Kind(K), Ty(Ty) {
  4437. if (Ranges.size() > 1) {
  4438. Kind = AccessKind(Kind | AK_MAY);
  4439. Kind = AccessKind(Kind & ~AK_MUST);
  4440. }
  4441. verify();
  4442. }
  4443. Access(Instruction *LocalI, Instruction *RemoteI, int64_t Offset,
  4444. int64_t Size, std::optional<Value *> Content, AccessKind Kind,
  4445. Type *Ty)
  4446. : LocalI(LocalI), RemoteI(RemoteI), Content(Content),
  4447. Ranges(Offset, Size), Kind(Kind), Ty(Ty) {
  4448. verify();
  4449. }
  4450. Access(const Access &Other) = default;
  4451. Access &operator=(const Access &Other) = default;
  4452. bool operator==(const Access &R) const {
  4453. return LocalI == R.LocalI && RemoteI == R.RemoteI && Ranges == R.Ranges &&
  4454. Content == R.Content && Kind == R.Kind;
  4455. }
  4456. bool operator!=(const Access &R) const { return !(*this == R); }
  4457. Access &operator&=(const Access &R) {
  4458. assert(RemoteI == R.RemoteI && "Expected same instruction!");
  4459. assert(LocalI == R.LocalI && "Expected same instruction!");
  4460. // Note that every Access object corresponds to a unique Value, and only
  4461. // accesses to the same Value are merged. Hence we assume that all ranges
  4462. // are the same size. If ranges can be different size, then the contents
  4463. // must be dropped.
  4464. Ranges.merge(R.Ranges);
  4465. Content =
  4466. AA::combineOptionalValuesInAAValueLatice(Content, R.Content, Ty);
  4467. // Combine the access kind, which results in a bitwise union.
  4468. // If there is more than one range, then this must be a MAY.
  4469. // If we combine a may and a must access we clear the must bit.
  4470. Kind = AccessKind(Kind | R.Kind);
  4471. if ((Kind & AK_MAY) || Ranges.size() > 1) {
  4472. Kind = AccessKind(Kind | AK_MAY);
  4473. Kind = AccessKind(Kind & ~AK_MUST);
  4474. }
  4475. verify();
  4476. return *this;
  4477. }
  4478. void verify() {
  4479. assert(isMustAccess() + isMayAccess() == 1 &&
  4480. "Expect must or may access, not both.");
  4481. assert(isAssumption() + isWrite() <= 1 &&
  4482. "Expect assumption access or write access, never both.");
  4483. assert((isMayAccess() || Ranges.size() == 1) &&
  4484. "Cannot be a must access if there are multiple ranges.");
  4485. }
  4486. /// Return the access kind.
  4487. AccessKind getKind() const { return Kind; }
  4488. /// Return true if this is a read access.
  4489. bool isRead() const { return Kind & AK_R; }
  4490. /// Return true if this is a write access.
  4491. bool isWrite() const { return Kind & AK_W; }
  4492. /// Return true if this is a write access.
  4493. bool isWriteOrAssumption() const { return isWrite() || isAssumption(); }
  4494. /// Return true if this is an assumption access.
  4495. bool isAssumption() const { return Kind == AK_ASSUMPTION; }
  4496. bool isMustAccess() const {
  4497. bool MustAccess = Kind & AK_MUST;
  4498. assert((!MustAccess || Ranges.size() < 2) &&
  4499. "Cannot be a must access if there are multiple ranges.");
  4500. return MustAccess;
  4501. }
  4502. bool isMayAccess() const {
  4503. bool MayAccess = Kind & AK_MAY;
  4504. assert((MayAccess || Ranges.size() < 2) &&
  4505. "Cannot be a must access if there are multiple ranges.");
  4506. return MayAccess;
  4507. }
  4508. /// Return the instruction that causes the access with respect to the local
  4509. /// scope of the associated attribute.
  4510. Instruction *getLocalInst() const { return LocalI; }
  4511. /// Return the actual instruction that causes the access.
  4512. Instruction *getRemoteInst() const { return RemoteI; }
  4513. /// Return true if the value written is not known yet.
  4514. bool isWrittenValueYetUndetermined() const { return !Content; }
  4515. /// Return true if the value written cannot be determined at all.
  4516. bool isWrittenValueUnknown() const {
  4517. return Content.has_value() && !*Content;
  4518. }
  4519. /// Set the value written to nullptr, i.e., unknown.
  4520. void setWrittenValueUnknown() { Content = nullptr; }
  4521. /// Return the type associated with the access, if known.
  4522. Type *getType() const { return Ty; }
  4523. /// Return the value writen, if any.
  4524. Value *getWrittenValue() const {
  4525. assert(!isWrittenValueYetUndetermined() &&
  4526. "Value needs to be determined before accessing it.");
  4527. return *Content;
  4528. }
  4529. /// Return the written value which can be `llvm::null` if it is not yet
  4530. /// determined.
  4531. std::optional<Value *> getContent() const { return Content; }
  4532. bool hasUniqueRange() const { return Ranges.isUnique(); }
  4533. const AA::RangeTy &getUniqueRange() const { return Ranges.getUnique(); }
  4534. /// Add a range accessed by this Access.
  4535. ///
  4536. /// If there are multiple ranges, then this is a "may access".
  4537. void addRange(int64_t Offset, int64_t Size) {
  4538. Ranges.insert({Offset, Size});
  4539. if (!hasUniqueRange()) {
  4540. Kind = AccessKind(Kind | AK_MAY);
  4541. Kind = AccessKind(Kind & ~AK_MUST);
  4542. }
  4543. }
  4544. const RangeList &getRanges() const { return Ranges; }
  4545. using const_iterator = RangeList::const_iterator;
  4546. const_iterator begin() const { return Ranges.begin(); }
  4547. const_iterator end() const { return Ranges.end(); }
  4548. private:
  4549. /// The instruction responsible for the access with respect to the local
  4550. /// scope of the associated attribute.
  4551. Instruction *LocalI;
  4552. /// The instruction responsible for the access.
  4553. Instruction *RemoteI;
  4554. /// The value written, if any. `llvm::none` means "not known yet", `nullptr`
  4555. /// cannot be determined.
  4556. std::optional<Value *> Content;
  4557. /// Set of potential ranges accessed from the base pointer.
  4558. RangeList Ranges;
  4559. /// The access kind, e.g., READ, as bitset (could be more than one).
  4560. AccessKind Kind;
  4561. /// The type of the content, thus the type read/written, can be null if not
  4562. /// available.
  4563. Type *Ty;
  4564. };
  4565. /// Create an abstract attribute view for the position \p IRP.
  4566. static AAPointerInfo &createForPosition(const IRPosition &IRP, Attributor &A);
  4567. /// See AbstractAttribute::getName()
  4568. const std::string getName() const override { return "AAPointerInfo"; }
  4569. /// See AbstractAttribute::getIdAddr()
  4570. const char *getIdAddr() const override { return &ID; }
  4571. /// Call \p CB on all accesses that might interfere with \p Range and return
  4572. /// true if all such accesses were known and the callback returned true for
  4573. /// all of them, false otherwise. An access interferes with an offset-size
  4574. /// pair if it might read or write that memory region.
  4575. virtual bool forallInterferingAccesses(
  4576. AA::RangeTy Range, function_ref<bool(const Access &, bool)> CB) const = 0;
  4577. /// Call \p CB on all accesses that might interfere with \p I and
  4578. /// return true if all such accesses were known and the callback returned true
  4579. /// for all of them, false otherwise. In contrast to forallInterferingAccesses
  4580. /// this function will perform reasoning to exclude write accesses that cannot
  4581. /// affect the load even if they on the surface look as if they would. The
  4582. /// flag \p HasBeenWrittenTo will be set to true if we know that \p I does not
  4583. /// read the intial value of the underlying memory.
  4584. virtual bool forallInterferingAccesses(
  4585. Attributor &A, const AbstractAttribute &QueryingAA, Instruction &I,
  4586. function_ref<bool(const Access &, bool)> CB, bool &HasBeenWrittenTo,
  4587. AA::RangeTy &Range) const = 0;
  4588. /// This function should return true if the type of the \p AA is AAPointerInfo
  4589. static bool classof(const AbstractAttribute *AA) {
  4590. return (AA->getIdAddr() == &ID);
  4591. }
  4592. /// Unique ID (due to the unique address)
  4593. static const char ID;
  4594. };
  4595. /// An abstract attribute for getting assumption information.
  4596. struct AAAssumptionInfo
  4597. : public StateWrapper<SetState<StringRef>, AbstractAttribute,
  4598. DenseSet<StringRef>> {
  4599. using Base =
  4600. StateWrapper<SetState<StringRef>, AbstractAttribute, DenseSet<StringRef>>;
  4601. AAAssumptionInfo(const IRPosition &IRP, Attributor &A,
  4602. const DenseSet<StringRef> &Known)
  4603. : Base(IRP, Known) {}
  4604. /// Returns true if the assumption set contains the assumption \p Assumption.
  4605. virtual bool hasAssumption(const StringRef Assumption) const = 0;
  4606. /// Create an abstract attribute view for the position \p IRP.
  4607. static AAAssumptionInfo &createForPosition(const IRPosition &IRP,
  4608. Attributor &A);
  4609. /// See AbstractAttribute::getName()
  4610. const std::string getName() const override { return "AAAssumptionInfo"; }
  4611. /// See AbstractAttribute::getIdAddr()
  4612. const char *getIdAddr() const override { return &ID; }
  4613. /// This function should return true if the type of the \p AA is
  4614. /// AAAssumptionInfo
  4615. static bool classof(const AbstractAttribute *AA) {
  4616. return (AA->getIdAddr() == &ID);
  4617. }
  4618. /// Unique ID (due to the unique address)
  4619. static const char ID;
  4620. };
  4621. /// An abstract attribute for getting all assumption underlying objects.
  4622. struct AAUnderlyingObjects : AbstractAttribute {
  4623. AAUnderlyingObjects(const IRPosition &IRP) : AbstractAttribute(IRP) {}
  4624. /// Create an abstract attribute biew for the position \p IRP.
  4625. static AAUnderlyingObjects &createForPosition(const IRPosition &IRP,
  4626. Attributor &A);
  4627. /// See AbstractAttribute::getName()
  4628. const std::string getName() const override { return "AAUnderlyingObjects"; }
  4629. /// See AbstractAttribute::getIdAddr()
  4630. const char *getIdAddr() const override { return &ID; }
  4631. /// This function should return true if the type of the \p AA is
  4632. /// AAUnderlyingObjects.
  4633. static bool classof(const AbstractAttribute *AA) {
  4634. return (AA->getIdAddr() == &ID);
  4635. }
  4636. /// Unique ID (due to the unique address)
  4637. static const char ID;
  4638. /// Check \p Pred on all underlying objects in \p Scope collected so far.
  4639. ///
  4640. /// This method will evaluate \p Pred on all underlying objects in \p Scope
  4641. /// collected so far and return true if \p Pred holds on all of them.
  4642. virtual bool
  4643. forallUnderlyingObjects(function_ref<bool(Value &)> Pred,
  4644. AA::ValueScope Scope = AA::Interprocedural) const = 0;
  4645. };
  4646. raw_ostream &operator<<(raw_ostream &, const AAPointerInfo::Access &);
  4647. /// Run options, used by the pass manager.
  4648. enum AttributorRunOption {
  4649. NONE = 0,
  4650. MODULE = 1 << 0,
  4651. CGSCC = 1 << 1,
  4652. ALL = MODULE | CGSCC
  4653. };
  4654. } // end namespace llvm
  4655. #endif // LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
  4656. #ifdef __GNUC__
  4657. #pragma GCC diagnostic pop
  4658. #endif