Attributor.h 186 KB

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