ValueTracking.cpp 218 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820
  1. //===- ValueTracking.cpp - Walk computations to compute properties --------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains routines that help analyze properties that chains of
  10. // computations have.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/ValueTracking.h"
  14. #include "llvm/ADT/APFloat.h"
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/ArrayRef.h"
  17. #include "llvm/ADT/None.h"
  18. #include "llvm/ADT/Optional.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallSet.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/ADT/iterator_range.h"
  25. #include "llvm/Analysis/AliasAnalysis.h"
  26. #include "llvm/Analysis/AssumptionCache.h"
  27. #include "llvm/Analysis/GuardUtils.h"
  28. #include "llvm/Analysis/InstructionSimplify.h"
  29. #include "llvm/Analysis/Loads.h"
  30. #include "llvm/Analysis/LoopInfo.h"
  31. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  32. #include "llvm/Analysis/TargetLibraryInfo.h"
  33. #include "llvm/IR/Argument.h"
  34. #include "llvm/IR/Attributes.h"
  35. #include "llvm/IR/BasicBlock.h"
  36. #include "llvm/IR/CallSite.h"
  37. #include "llvm/IR/Constant.h"
  38. #include "llvm/IR/ConstantRange.h"
  39. #include "llvm/IR/Constants.h"
  40. #include "llvm/IR/DerivedTypes.h"
  41. #include "llvm/IR/DiagnosticInfo.h"
  42. #include "llvm/IR/Dominators.h"
  43. #include "llvm/IR/Function.h"
  44. #include "llvm/IR/GetElementPtrTypeIterator.h"
  45. #include "llvm/IR/GlobalAlias.h"
  46. #include "llvm/IR/GlobalValue.h"
  47. #include "llvm/IR/GlobalVariable.h"
  48. #include "llvm/IR/InstrTypes.h"
  49. #include "llvm/IR/Instruction.h"
  50. #include "llvm/IR/Instructions.h"
  51. #include "llvm/IR/IntrinsicInst.h"
  52. #include "llvm/IR/Intrinsics.h"
  53. #include "llvm/IR/LLVMContext.h"
  54. #include "llvm/IR/Metadata.h"
  55. #include "llvm/IR/Module.h"
  56. #include "llvm/IR/Operator.h"
  57. #include "llvm/IR/PatternMatch.h"
  58. #include "llvm/IR/Type.h"
  59. #include "llvm/IR/User.h"
  60. #include "llvm/IR/Value.h"
  61. #include "llvm/Support/Casting.h"
  62. #include "llvm/Support/CommandLine.h"
  63. #include "llvm/Support/Compiler.h"
  64. #include "llvm/Support/ErrorHandling.h"
  65. #include "llvm/Support/KnownBits.h"
  66. #include "llvm/Support/MathExtras.h"
  67. #include <algorithm>
  68. #include <array>
  69. #include <cassert>
  70. #include <cstdint>
  71. #include <iterator>
  72. #include <utility>
  73. using namespace llvm;
  74. using namespace llvm::PatternMatch;
  75. const unsigned MaxDepth = 6;
  76. // Controls the number of uses of the value searched for possible
  77. // dominating comparisons.
  78. static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
  79. cl::Hidden, cl::init(20));
  80. /// Returns the bitwidth of the given scalar or pointer type. For vector types,
  81. /// returns the element type's bitwidth.
  82. static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
  83. if (unsigned BitWidth = Ty->getScalarSizeInBits())
  84. return BitWidth;
  85. return DL.getIndexTypeSizeInBits(Ty);
  86. }
  87. namespace {
  88. // Simplifying using an assume can only be done in a particular control-flow
  89. // context (the context instruction provides that context). If an assume and
  90. // the context instruction are not in the same block then the DT helps in
  91. // figuring out if we can use it.
  92. struct Query {
  93. const DataLayout &DL;
  94. AssumptionCache *AC;
  95. const Instruction *CxtI;
  96. const DominatorTree *DT;
  97. // Unlike the other analyses, this may be a nullptr because not all clients
  98. // provide it currently.
  99. OptimizationRemarkEmitter *ORE;
  100. /// Set of assumptions that should be excluded from further queries.
  101. /// This is because of the potential for mutual recursion to cause
  102. /// computeKnownBits to repeatedly visit the same assume intrinsic. The
  103. /// classic case of this is assume(x = y), which will attempt to determine
  104. /// bits in x from bits in y, which will attempt to determine bits in y from
  105. /// bits in x, etc. Regarding the mutual recursion, computeKnownBits can call
  106. /// isKnownNonZero, which calls computeKnownBits and isKnownToBeAPowerOfTwo
  107. /// (all of which can call computeKnownBits), and so on.
  108. std::array<const Value *, MaxDepth> Excluded;
  109. /// If true, it is safe to use metadata during simplification.
  110. InstrInfoQuery IIQ;
  111. unsigned NumExcluded = 0;
  112. Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
  113. const DominatorTree *DT, bool UseInstrInfo,
  114. OptimizationRemarkEmitter *ORE = nullptr)
  115. : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
  116. Query(const Query &Q, const Value *NewExcl)
  117. : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), ORE(Q.ORE), IIQ(Q.IIQ),
  118. NumExcluded(Q.NumExcluded) {
  119. Excluded = Q.Excluded;
  120. Excluded[NumExcluded++] = NewExcl;
  121. assert(NumExcluded <= Excluded.size());
  122. }
  123. bool isExcluded(const Value *Value) const {
  124. if (NumExcluded == 0)
  125. return false;
  126. auto End = Excluded.begin() + NumExcluded;
  127. return std::find(Excluded.begin(), End, Value) != End;
  128. }
  129. };
  130. } // end anonymous namespace
  131. // Given the provided Value and, potentially, a context instruction, return
  132. // the preferred context instruction (if any).
  133. static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
  134. // If we've been provided with a context instruction, then use that (provided
  135. // it has been inserted).
  136. if (CxtI && CxtI->getParent())
  137. return CxtI;
  138. // If the value is really an already-inserted instruction, then use that.
  139. CxtI = dyn_cast<Instruction>(V);
  140. if (CxtI && CxtI->getParent())
  141. return CxtI;
  142. return nullptr;
  143. }
  144. static void computeKnownBits(const Value *V, KnownBits &Known,
  145. unsigned Depth, const Query &Q);
  146. void llvm::computeKnownBits(const Value *V, KnownBits &Known,
  147. const DataLayout &DL, unsigned Depth,
  148. AssumptionCache *AC, const Instruction *CxtI,
  149. const DominatorTree *DT,
  150. OptimizationRemarkEmitter *ORE, bool UseInstrInfo) {
  151. ::computeKnownBits(V, Known, Depth,
  152. Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
  153. }
  154. static KnownBits computeKnownBits(const Value *V, unsigned Depth,
  155. const Query &Q);
  156. KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL,
  157. unsigned Depth, AssumptionCache *AC,
  158. const Instruction *CxtI,
  159. const DominatorTree *DT,
  160. OptimizationRemarkEmitter *ORE,
  161. bool UseInstrInfo) {
  162. return ::computeKnownBits(
  163. V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
  164. }
  165. bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
  166. const DataLayout &DL, AssumptionCache *AC,
  167. const Instruction *CxtI, const DominatorTree *DT,
  168. bool UseInstrInfo) {
  169. assert(LHS->getType() == RHS->getType() &&
  170. "LHS and RHS should have the same type");
  171. assert(LHS->getType()->isIntOrIntVectorTy() &&
  172. "LHS and RHS should be integers");
  173. // Look for an inverted mask: (X & ~M) op (Y & M).
  174. Value *M;
  175. if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
  176. match(RHS, m_c_And(m_Specific(M), m_Value())))
  177. return true;
  178. if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
  179. match(LHS, m_c_And(m_Specific(M), m_Value())))
  180. return true;
  181. IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
  182. KnownBits LHSKnown(IT->getBitWidth());
  183. KnownBits RHSKnown(IT->getBitWidth());
  184. computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
  185. computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
  186. return (LHSKnown.Zero | RHSKnown.Zero).isAllOnesValue();
  187. }
  188. bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI) {
  189. for (const User *U : CxtI->users()) {
  190. if (const ICmpInst *IC = dyn_cast<ICmpInst>(U))
  191. if (IC->isEquality())
  192. if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
  193. if (C->isNullValue())
  194. continue;
  195. return false;
  196. }
  197. return true;
  198. }
  199. static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
  200. const Query &Q);
  201. bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
  202. bool OrZero, unsigned Depth,
  203. AssumptionCache *AC, const Instruction *CxtI,
  204. const DominatorTree *DT, bool UseInstrInfo) {
  205. return ::isKnownToBeAPowerOfTwo(
  206. V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  207. }
  208. static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q);
  209. bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
  210. AssumptionCache *AC, const Instruction *CxtI,
  211. const DominatorTree *DT, bool UseInstrInfo) {
  212. return ::isKnownNonZero(V, Depth,
  213. Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  214. }
  215. bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
  216. unsigned Depth, AssumptionCache *AC,
  217. const Instruction *CxtI, const DominatorTree *DT,
  218. bool UseInstrInfo) {
  219. KnownBits Known =
  220. computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
  221. return Known.isNonNegative();
  222. }
  223. bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
  224. AssumptionCache *AC, const Instruction *CxtI,
  225. const DominatorTree *DT, bool UseInstrInfo) {
  226. if (auto *CI = dyn_cast<ConstantInt>(V))
  227. return CI->getValue().isStrictlyPositive();
  228. // TODO: We'd doing two recursive queries here. We should factor this such
  229. // that only a single query is needed.
  230. return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
  231. isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
  232. }
  233. bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth,
  234. AssumptionCache *AC, const Instruction *CxtI,
  235. const DominatorTree *DT, bool UseInstrInfo) {
  236. KnownBits Known =
  237. computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
  238. return Known.isNegative();
  239. }
  240. static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q);
  241. bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
  242. const DataLayout &DL, AssumptionCache *AC,
  243. const Instruction *CxtI, const DominatorTree *DT,
  244. bool UseInstrInfo) {
  245. return ::isKnownNonEqual(V1, V2,
  246. Query(DL, AC, safeCxtI(V1, safeCxtI(V2, CxtI)), DT,
  247. UseInstrInfo, /*ORE=*/nullptr));
  248. }
  249. static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
  250. const Query &Q);
  251. bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
  252. const DataLayout &DL, unsigned Depth,
  253. AssumptionCache *AC, const Instruction *CxtI,
  254. const DominatorTree *DT, bool UseInstrInfo) {
  255. return ::MaskedValueIsZero(
  256. V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  257. }
  258. static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
  259. const Query &Q);
  260. unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
  261. unsigned Depth, AssumptionCache *AC,
  262. const Instruction *CxtI,
  263. const DominatorTree *DT, bool UseInstrInfo) {
  264. return ::ComputeNumSignBits(
  265. V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  266. }
  267. static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
  268. bool NSW,
  269. KnownBits &KnownOut, KnownBits &Known2,
  270. unsigned Depth, const Query &Q) {
  271. unsigned BitWidth = KnownOut.getBitWidth();
  272. // If an initial sequence of bits in the result is not needed, the
  273. // corresponding bits in the operands are not needed.
  274. KnownBits LHSKnown(BitWidth);
  275. computeKnownBits(Op0, LHSKnown, Depth + 1, Q);
  276. computeKnownBits(Op1, Known2, Depth + 1, Q);
  277. KnownOut = KnownBits::computeForAddSub(Add, NSW, LHSKnown, Known2);
  278. }
  279. static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
  280. KnownBits &Known, KnownBits &Known2,
  281. unsigned Depth, const Query &Q) {
  282. unsigned BitWidth = Known.getBitWidth();
  283. computeKnownBits(Op1, Known, Depth + 1, Q);
  284. computeKnownBits(Op0, Known2, Depth + 1, Q);
  285. bool isKnownNegative = false;
  286. bool isKnownNonNegative = false;
  287. // If the multiplication is known not to overflow, compute the sign bit.
  288. if (NSW) {
  289. if (Op0 == Op1) {
  290. // The product of a number with itself is non-negative.
  291. isKnownNonNegative = true;
  292. } else {
  293. bool isKnownNonNegativeOp1 = Known.isNonNegative();
  294. bool isKnownNonNegativeOp0 = Known2.isNonNegative();
  295. bool isKnownNegativeOp1 = Known.isNegative();
  296. bool isKnownNegativeOp0 = Known2.isNegative();
  297. // The product of two numbers with the same sign is non-negative.
  298. isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
  299. (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
  300. // The product of a negative number and a non-negative number is either
  301. // negative or zero.
  302. if (!isKnownNonNegative)
  303. isKnownNegative = (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
  304. isKnownNonZero(Op0, Depth, Q)) ||
  305. (isKnownNegativeOp0 && isKnownNonNegativeOp1 &&
  306. isKnownNonZero(Op1, Depth, Q));
  307. }
  308. }
  309. assert(!Known.hasConflict() && !Known2.hasConflict());
  310. // Compute a conservative estimate for high known-0 bits.
  311. unsigned LeadZ = std::max(Known.countMinLeadingZeros() +
  312. Known2.countMinLeadingZeros(),
  313. BitWidth) - BitWidth;
  314. LeadZ = std::min(LeadZ, BitWidth);
  315. // The result of the bottom bits of an integer multiply can be
  316. // inferred by looking at the bottom bits of both operands and
  317. // multiplying them together.
  318. // We can infer at least the minimum number of known trailing bits
  319. // of both operands. Depending on number of trailing zeros, we can
  320. // infer more bits, because (a*b) <=> ((a/m) * (b/n)) * (m*n) assuming
  321. // a and b are divisible by m and n respectively.
  322. // We then calculate how many of those bits are inferrable and set
  323. // the output. For example, the i8 mul:
  324. // a = XXXX1100 (12)
  325. // b = XXXX1110 (14)
  326. // We know the bottom 3 bits are zero since the first can be divided by
  327. // 4 and the second by 2, thus having ((12/4) * (14/2)) * (2*4).
  328. // Applying the multiplication to the trimmed arguments gets:
  329. // XX11 (3)
  330. // X111 (7)
  331. // -------
  332. // XX11
  333. // XX11
  334. // XX11
  335. // XX11
  336. // -------
  337. // XXXXX01
  338. // Which allows us to infer the 2 LSBs. Since we're multiplying the result
  339. // by 8, the bottom 3 bits will be 0, so we can infer a total of 5 bits.
  340. // The proof for this can be described as:
  341. // Pre: (C1 >= 0) && (C1 < (1 << C5)) && (C2 >= 0) && (C2 < (1 << C6)) &&
  342. // (C7 == (1 << (umin(countTrailingZeros(C1), C5) +
  343. // umin(countTrailingZeros(C2), C6) +
  344. // umin(C5 - umin(countTrailingZeros(C1), C5),
  345. // C6 - umin(countTrailingZeros(C2), C6)))) - 1)
  346. // %aa = shl i8 %a, C5
  347. // %bb = shl i8 %b, C6
  348. // %aaa = or i8 %aa, C1
  349. // %bbb = or i8 %bb, C2
  350. // %mul = mul i8 %aaa, %bbb
  351. // %mask = and i8 %mul, C7
  352. // =>
  353. // %mask = i8 ((C1*C2)&C7)
  354. // Where C5, C6 describe the known bits of %a, %b
  355. // C1, C2 describe the known bottom bits of %a, %b.
  356. // C7 describes the mask of the known bits of the result.
  357. APInt Bottom0 = Known.One;
  358. APInt Bottom1 = Known2.One;
  359. // How many times we'd be able to divide each argument by 2 (shr by 1).
  360. // This gives us the number of trailing zeros on the multiplication result.
  361. unsigned TrailBitsKnown0 = (Known.Zero | Known.One).countTrailingOnes();
  362. unsigned TrailBitsKnown1 = (Known2.Zero | Known2.One).countTrailingOnes();
  363. unsigned TrailZero0 = Known.countMinTrailingZeros();
  364. unsigned TrailZero1 = Known2.countMinTrailingZeros();
  365. unsigned TrailZ = TrailZero0 + TrailZero1;
  366. // Figure out the fewest known-bits operand.
  367. unsigned SmallestOperand = std::min(TrailBitsKnown0 - TrailZero0,
  368. TrailBitsKnown1 - TrailZero1);
  369. unsigned ResultBitsKnown = std::min(SmallestOperand + TrailZ, BitWidth);
  370. APInt BottomKnown = Bottom0.getLoBits(TrailBitsKnown0) *
  371. Bottom1.getLoBits(TrailBitsKnown1);
  372. Known.resetAll();
  373. Known.Zero.setHighBits(LeadZ);
  374. Known.Zero |= (~BottomKnown).getLoBits(ResultBitsKnown);
  375. Known.One |= BottomKnown.getLoBits(ResultBitsKnown);
  376. // Only make use of no-wrap flags if we failed to compute the sign bit
  377. // directly. This matters if the multiplication always overflows, in
  378. // which case we prefer to follow the result of the direct computation,
  379. // though as the program is invoking undefined behaviour we can choose
  380. // whatever we like here.
  381. if (isKnownNonNegative && !Known.isNegative())
  382. Known.makeNonNegative();
  383. else if (isKnownNegative && !Known.isNonNegative())
  384. Known.makeNegative();
  385. }
  386. void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
  387. KnownBits &Known) {
  388. unsigned BitWidth = Known.getBitWidth();
  389. unsigned NumRanges = Ranges.getNumOperands() / 2;
  390. assert(NumRanges >= 1);
  391. Known.Zero.setAllBits();
  392. Known.One.setAllBits();
  393. for (unsigned i = 0; i < NumRanges; ++i) {
  394. ConstantInt *Lower =
  395. mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
  396. ConstantInt *Upper =
  397. mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
  398. ConstantRange Range(Lower->getValue(), Upper->getValue());
  399. // The first CommonPrefixBits of all values in Range are equal.
  400. unsigned CommonPrefixBits =
  401. (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros();
  402. APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
  403. Known.One &= Range.getUnsignedMax() & Mask;
  404. Known.Zero &= ~Range.getUnsignedMax() & Mask;
  405. }
  406. }
  407. static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
  408. SmallVector<const Value *, 16> WorkSet(1, I);
  409. SmallPtrSet<const Value *, 32> Visited;
  410. SmallPtrSet<const Value *, 16> EphValues;
  411. // The instruction defining an assumption's condition itself is always
  412. // considered ephemeral to that assumption (even if it has other
  413. // non-ephemeral users). See r246696's test case for an example.
  414. if (is_contained(I->operands(), E))
  415. return true;
  416. while (!WorkSet.empty()) {
  417. const Value *V = WorkSet.pop_back_val();
  418. if (!Visited.insert(V).second)
  419. continue;
  420. // If all uses of this value are ephemeral, then so is this value.
  421. if (llvm::all_of(V->users(), [&](const User *U) {
  422. return EphValues.count(U);
  423. })) {
  424. if (V == E)
  425. return true;
  426. if (V == I || isSafeToSpeculativelyExecute(V)) {
  427. EphValues.insert(V);
  428. if (const User *U = dyn_cast<User>(V))
  429. for (User::const_op_iterator J = U->op_begin(), JE = U->op_end();
  430. J != JE; ++J)
  431. WorkSet.push_back(*J);
  432. }
  433. }
  434. }
  435. return false;
  436. }
  437. // Is this an intrinsic that cannot be speculated but also cannot trap?
  438. bool llvm::isAssumeLikeIntrinsic(const Instruction *I) {
  439. if (const CallInst *CI = dyn_cast<CallInst>(I))
  440. if (Function *F = CI->getCalledFunction())
  441. switch (F->getIntrinsicID()) {
  442. default: break;
  443. // FIXME: This list is repeated from NoTTI::getIntrinsicCost.
  444. case Intrinsic::assume:
  445. case Intrinsic::sideeffect:
  446. case Intrinsic::dbg_declare:
  447. case Intrinsic::dbg_value:
  448. case Intrinsic::dbg_label:
  449. case Intrinsic::invariant_start:
  450. case Intrinsic::invariant_end:
  451. case Intrinsic::lifetime_start:
  452. case Intrinsic::lifetime_end:
  453. case Intrinsic::objectsize:
  454. case Intrinsic::ptr_annotation:
  455. case Intrinsic::var_annotation:
  456. return true;
  457. }
  458. return false;
  459. }
  460. bool llvm::isValidAssumeForContext(const Instruction *Inv,
  461. const Instruction *CxtI,
  462. const DominatorTree *DT) {
  463. // There are two restrictions on the use of an assume:
  464. // 1. The assume must dominate the context (or the control flow must
  465. // reach the assume whenever it reaches the context).
  466. // 2. The context must not be in the assume's set of ephemeral values
  467. // (otherwise we will use the assume to prove that the condition
  468. // feeding the assume is trivially true, thus causing the removal of
  469. // the assume).
  470. if (DT) {
  471. if (DT->dominates(Inv, CxtI))
  472. return true;
  473. } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
  474. // We don't have a DT, but this trivially dominates.
  475. return true;
  476. }
  477. // With or without a DT, the only remaining case we will check is if the
  478. // instructions are in the same BB. Give up if that is not the case.
  479. if (Inv->getParent() != CxtI->getParent())
  480. return false;
  481. // If we have a dom tree, then we now know that the assume doesn't dominate
  482. // the other instruction. If we don't have a dom tree then we can check if
  483. // the assume is first in the BB.
  484. if (!DT) {
  485. // Search forward from the assume until we reach the context (or the end
  486. // of the block); the common case is that the assume will come first.
  487. for (auto I = std::next(BasicBlock::const_iterator(Inv)),
  488. IE = Inv->getParent()->end(); I != IE; ++I)
  489. if (&*I == CxtI)
  490. return true;
  491. }
  492. // Don't let an assume affect itself - this would cause the problems
  493. // `isEphemeralValueOf` is trying to prevent, and it would also make
  494. // the loop below go out of bounds.
  495. if (Inv == CxtI)
  496. return false;
  497. // The context comes first, but they're both in the same block. Make sure
  498. // there is nothing in between that might interrupt the control flow.
  499. for (BasicBlock::const_iterator I =
  500. std::next(BasicBlock::const_iterator(CxtI)), IE(Inv);
  501. I != IE; ++I)
  502. if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
  503. return false;
  504. return !isEphemeralValueOf(Inv, CxtI);
  505. }
  506. static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
  507. unsigned Depth, const Query &Q) {
  508. // Use of assumptions is context-sensitive. If we don't have a context, we
  509. // cannot use them!
  510. if (!Q.AC || !Q.CxtI)
  511. return;
  512. unsigned BitWidth = Known.getBitWidth();
  513. // Note that the patterns below need to be kept in sync with the code
  514. // in AssumptionCache::updateAffectedValues.
  515. for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
  516. if (!AssumeVH)
  517. continue;
  518. CallInst *I = cast<CallInst>(AssumeVH);
  519. assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
  520. "Got assumption for the wrong function!");
  521. if (Q.isExcluded(I))
  522. continue;
  523. // Warning: This loop can end up being somewhat performance sensitive.
  524. // We're running this loop for once for each value queried resulting in a
  525. // runtime of ~O(#assumes * #values).
  526. assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
  527. "must be an assume intrinsic");
  528. Value *Arg = I->getArgOperand(0);
  529. if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  530. assert(BitWidth == 1 && "assume operand is not i1?");
  531. Known.setAllOnes();
  532. return;
  533. }
  534. if (match(Arg, m_Not(m_Specific(V))) &&
  535. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  536. assert(BitWidth == 1 && "assume operand is not i1?");
  537. Known.setAllZero();
  538. return;
  539. }
  540. // The remaining tests are all recursive, so bail out if we hit the limit.
  541. if (Depth == MaxDepth)
  542. continue;
  543. ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
  544. if (!Cmp)
  545. continue;
  546. Value *A, *B;
  547. auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
  548. CmpInst::Predicate Pred;
  549. uint64_t C;
  550. switch (Cmp->getPredicate()) {
  551. default:
  552. break;
  553. case ICmpInst::ICMP_EQ:
  554. // assume(v = a)
  555. if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A))) &&
  556. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  557. KnownBits RHSKnown(BitWidth);
  558. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  559. Known.Zero |= RHSKnown.Zero;
  560. Known.One |= RHSKnown.One;
  561. // assume(v & b = a)
  562. } else if (match(Cmp,
  563. m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
  564. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  565. KnownBits RHSKnown(BitWidth);
  566. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  567. KnownBits MaskKnown(BitWidth);
  568. computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
  569. // For those bits in the mask that are known to be one, we can propagate
  570. // known bits from the RHS to V.
  571. Known.Zero |= RHSKnown.Zero & MaskKnown.One;
  572. Known.One |= RHSKnown.One & MaskKnown.One;
  573. // assume(~(v & b) = a)
  574. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
  575. m_Value(A))) &&
  576. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  577. KnownBits RHSKnown(BitWidth);
  578. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  579. KnownBits MaskKnown(BitWidth);
  580. computeKnownBits(B, MaskKnown, Depth+1, Query(Q, I));
  581. // For those bits in the mask that are known to be one, we can propagate
  582. // inverted known bits from the RHS to V.
  583. Known.Zero |= RHSKnown.One & MaskKnown.One;
  584. Known.One |= RHSKnown.Zero & MaskKnown.One;
  585. // assume(v | b = a)
  586. } else if (match(Cmp,
  587. m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
  588. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  589. KnownBits RHSKnown(BitWidth);
  590. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  591. KnownBits BKnown(BitWidth);
  592. computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
  593. // For those bits in B that are known to be zero, we can propagate known
  594. // bits from the RHS to V.
  595. Known.Zero |= RHSKnown.Zero & BKnown.Zero;
  596. Known.One |= RHSKnown.One & BKnown.Zero;
  597. // assume(~(v | b) = a)
  598. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
  599. m_Value(A))) &&
  600. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  601. KnownBits RHSKnown(BitWidth);
  602. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  603. KnownBits BKnown(BitWidth);
  604. computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
  605. // For those bits in B that are known to be zero, we can propagate
  606. // inverted known bits from the RHS to V.
  607. Known.Zero |= RHSKnown.One & BKnown.Zero;
  608. Known.One |= RHSKnown.Zero & BKnown.Zero;
  609. // assume(v ^ b = a)
  610. } else if (match(Cmp,
  611. m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
  612. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  613. KnownBits RHSKnown(BitWidth);
  614. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  615. KnownBits BKnown(BitWidth);
  616. computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
  617. // For those bits in B that are known to be zero, we can propagate known
  618. // bits from the RHS to V. For those bits in B that are known to be one,
  619. // we can propagate inverted known bits from the RHS to V.
  620. Known.Zero |= RHSKnown.Zero & BKnown.Zero;
  621. Known.One |= RHSKnown.One & BKnown.Zero;
  622. Known.Zero |= RHSKnown.One & BKnown.One;
  623. Known.One |= RHSKnown.Zero & BKnown.One;
  624. // assume(~(v ^ b) = a)
  625. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
  626. m_Value(A))) &&
  627. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  628. KnownBits RHSKnown(BitWidth);
  629. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  630. KnownBits BKnown(BitWidth);
  631. computeKnownBits(B, BKnown, Depth+1, Query(Q, I));
  632. // For those bits in B that are known to be zero, we can propagate
  633. // inverted known bits from the RHS to V. For those bits in B that are
  634. // known to be one, we can propagate known bits from the RHS to V.
  635. Known.Zero |= RHSKnown.One & BKnown.Zero;
  636. Known.One |= RHSKnown.Zero & BKnown.Zero;
  637. Known.Zero |= RHSKnown.Zero & BKnown.One;
  638. Known.One |= RHSKnown.One & BKnown.One;
  639. // assume(v << c = a)
  640. } else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
  641. m_Value(A))) &&
  642. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  643. KnownBits RHSKnown(BitWidth);
  644. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  645. // For those bits in RHS that are known, we can propagate them to known
  646. // bits in V shifted to the right by C.
  647. RHSKnown.Zero.lshrInPlace(C);
  648. Known.Zero |= RHSKnown.Zero;
  649. RHSKnown.One.lshrInPlace(C);
  650. Known.One |= RHSKnown.One;
  651. // assume(~(v << c) = a)
  652. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
  653. m_Value(A))) &&
  654. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  655. KnownBits RHSKnown(BitWidth);
  656. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  657. // For those bits in RHS that are known, we can propagate them inverted
  658. // to known bits in V shifted to the right by C.
  659. RHSKnown.One.lshrInPlace(C);
  660. Known.Zero |= RHSKnown.One;
  661. RHSKnown.Zero.lshrInPlace(C);
  662. Known.One |= RHSKnown.Zero;
  663. // assume(v >> c = a)
  664. } else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)),
  665. m_Value(A))) &&
  666. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  667. KnownBits RHSKnown(BitWidth);
  668. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  669. // For those bits in RHS that are known, we can propagate them to known
  670. // bits in V shifted to the right by C.
  671. Known.Zero |= RHSKnown.Zero << C;
  672. Known.One |= RHSKnown.One << C;
  673. // assume(~(v >> c) = a)
  674. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))),
  675. m_Value(A))) &&
  676. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  677. KnownBits RHSKnown(BitWidth);
  678. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  679. // For those bits in RHS that are known, we can propagate them inverted
  680. // to known bits in V shifted to the right by C.
  681. Known.Zero |= RHSKnown.One << C;
  682. Known.One |= RHSKnown.Zero << C;
  683. }
  684. break;
  685. case ICmpInst::ICMP_SGE:
  686. // assume(v >=_s c) where c is non-negative
  687. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  688. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  689. KnownBits RHSKnown(BitWidth);
  690. computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I));
  691. if (RHSKnown.isNonNegative()) {
  692. // We know that the sign bit is zero.
  693. Known.makeNonNegative();
  694. }
  695. }
  696. break;
  697. case ICmpInst::ICMP_SGT:
  698. // assume(v >_s c) where c is at least -1.
  699. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  700. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  701. KnownBits RHSKnown(BitWidth);
  702. computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I));
  703. if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) {
  704. // We know that the sign bit is zero.
  705. Known.makeNonNegative();
  706. }
  707. }
  708. break;
  709. case ICmpInst::ICMP_SLE:
  710. // assume(v <=_s c) where c is negative
  711. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  712. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  713. KnownBits RHSKnown(BitWidth);
  714. computeKnownBits(A, RHSKnown, Depth + 1, Query(Q, I));
  715. if (RHSKnown.isNegative()) {
  716. // We know that the sign bit is one.
  717. Known.makeNegative();
  718. }
  719. }
  720. break;
  721. case ICmpInst::ICMP_SLT:
  722. // assume(v <_s c) where c is non-positive
  723. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  724. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  725. KnownBits RHSKnown(BitWidth);
  726. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  727. if (RHSKnown.isZero() || RHSKnown.isNegative()) {
  728. // We know that the sign bit is one.
  729. Known.makeNegative();
  730. }
  731. }
  732. break;
  733. case ICmpInst::ICMP_ULE:
  734. // assume(v <=_u c)
  735. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  736. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  737. KnownBits RHSKnown(BitWidth);
  738. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  739. // Whatever high bits in c are zero are known to be zero.
  740. Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
  741. }
  742. break;
  743. case ICmpInst::ICMP_ULT:
  744. // assume(v <_u c)
  745. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  746. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  747. KnownBits RHSKnown(BitWidth);
  748. computeKnownBits(A, RHSKnown, Depth+1, Query(Q, I));
  749. // If the RHS is known zero, then this assumption must be wrong (nothing
  750. // is unsigned less than zero). Signal a conflict and get out of here.
  751. if (RHSKnown.isZero()) {
  752. Known.Zero.setAllBits();
  753. Known.One.setAllBits();
  754. break;
  755. }
  756. // Whatever high bits in c are zero are known to be zero (if c is a power
  757. // of 2, then one more).
  758. if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, Query(Q, I)))
  759. Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1);
  760. else
  761. Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
  762. }
  763. break;
  764. }
  765. }
  766. // If assumptions conflict with each other or previous known bits, then we
  767. // have a logical fallacy. It's possible that the assumption is not reachable,
  768. // so this isn't a real bug. On the other hand, the program may have undefined
  769. // behavior, or we might have a bug in the compiler. We can't assert/crash, so
  770. // clear out the known bits, try to warn the user, and hope for the best.
  771. if (Known.Zero.intersects(Known.One)) {
  772. Known.resetAll();
  773. if (Q.ORE)
  774. Q.ORE->emit([&]() {
  775. auto *CxtI = const_cast<Instruction *>(Q.CxtI);
  776. return OptimizationRemarkAnalysis("value-tracking", "BadAssumption",
  777. CxtI)
  778. << "Detected conflicting code assumptions. Program may "
  779. "have undefined behavior, or compiler may have "
  780. "internal error.";
  781. });
  782. }
  783. }
  784. /// Compute known bits from a shift operator, including those with a
  785. /// non-constant shift amount. Known is the output of this function. Known2 is a
  786. /// pre-allocated temporary with the same bit width as Known. KZF and KOF are
  787. /// operator-specific functions that, given the known-zero or known-one bits
  788. /// respectively, and a shift amount, compute the implied known-zero or
  789. /// known-one bits of the shift operator's result respectively for that shift
  790. /// amount. The results from calling KZF and KOF are conservatively combined for
  791. /// all permitted shift amounts.
  792. static void computeKnownBitsFromShiftOperator(
  793. const Operator *I, KnownBits &Known, KnownBits &Known2,
  794. unsigned Depth, const Query &Q,
  795. function_ref<APInt(const APInt &, unsigned)> KZF,
  796. function_ref<APInt(const APInt &, unsigned)> KOF) {
  797. unsigned BitWidth = Known.getBitWidth();
  798. if (auto *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
  799. unsigned ShiftAmt = SA->getLimitedValue(BitWidth-1);
  800. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  801. Known.Zero = KZF(Known.Zero, ShiftAmt);
  802. Known.One = KOF(Known.One, ShiftAmt);
  803. // If the known bits conflict, this must be an overflowing left shift, so
  804. // the shift result is poison. We can return anything we want. Choose 0 for
  805. // the best folding opportunity.
  806. if (Known.hasConflict())
  807. Known.setAllZero();
  808. return;
  809. }
  810. computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
  811. // If the shift amount could be greater than or equal to the bit-width of the
  812. // LHS, the value could be poison, but bail out because the check below is
  813. // expensive. TODO: Should we just carry on?
  814. if ((~Known.Zero).uge(BitWidth)) {
  815. Known.resetAll();
  816. return;
  817. }
  818. // Note: We cannot use Known.Zero.getLimitedValue() here, because if
  819. // BitWidth > 64 and any upper bits are known, we'll end up returning the
  820. // limit value (which implies all bits are known).
  821. uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue();
  822. uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue();
  823. // It would be more-clearly correct to use the two temporaries for this
  824. // calculation. Reusing the APInts here to prevent unnecessary allocations.
  825. Known.resetAll();
  826. // If we know the shifter operand is nonzero, we can sometimes infer more
  827. // known bits. However this is expensive to compute, so be lazy about it and
  828. // only compute it when absolutely necessary.
  829. Optional<bool> ShifterOperandIsNonZero;
  830. // Early exit if we can't constrain any well-defined shift amount.
  831. if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) &&
  832. !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) {
  833. ShifterOperandIsNonZero = isKnownNonZero(I->getOperand(1), Depth + 1, Q);
  834. if (!*ShifterOperandIsNonZero)
  835. return;
  836. }
  837. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  838. Known.Zero.setAllBits();
  839. Known.One.setAllBits();
  840. for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) {
  841. // Combine the shifted known input bits only for those shift amounts
  842. // compatible with its known constraints.
  843. if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt)
  844. continue;
  845. if ((ShiftAmt | ShiftAmtKO) != ShiftAmt)
  846. continue;
  847. // If we know the shifter is nonzero, we may be able to infer more known
  848. // bits. This check is sunk down as far as possible to avoid the expensive
  849. // call to isKnownNonZero if the cheaper checks above fail.
  850. if (ShiftAmt == 0) {
  851. if (!ShifterOperandIsNonZero.hasValue())
  852. ShifterOperandIsNonZero =
  853. isKnownNonZero(I->getOperand(1), Depth + 1, Q);
  854. if (*ShifterOperandIsNonZero)
  855. continue;
  856. }
  857. Known.Zero &= KZF(Known2.Zero, ShiftAmt);
  858. Known.One &= KOF(Known2.One, ShiftAmt);
  859. }
  860. // If the known bits conflict, the result is poison. Return a 0 and hope the
  861. // caller can further optimize that.
  862. if (Known.hasConflict())
  863. Known.setAllZero();
  864. }
  865. static void computeKnownBitsFromOperator(const Operator *I, KnownBits &Known,
  866. unsigned Depth, const Query &Q) {
  867. unsigned BitWidth = Known.getBitWidth();
  868. KnownBits Known2(Known);
  869. switch (I->getOpcode()) {
  870. default: break;
  871. case Instruction::Load:
  872. if (MDNode *MD =
  873. Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
  874. computeKnownBitsFromRangeMetadata(*MD, Known);
  875. break;
  876. case Instruction::And: {
  877. // If either the LHS or the RHS are Zero, the result is zero.
  878. computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
  879. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  880. // Output known-1 bits are only known if set in both the LHS & RHS.
  881. Known.One &= Known2.One;
  882. // Output known-0 are known to be clear if zero in either the LHS | RHS.
  883. Known.Zero |= Known2.Zero;
  884. // and(x, add (x, -1)) is a common idiom that always clears the low bit;
  885. // here we handle the more general case of adding any odd number by
  886. // matching the form add(x, add(x, y)) where y is odd.
  887. // TODO: This could be generalized to clearing any bit set in y where the
  888. // following bit is known to be unset in y.
  889. Value *X = nullptr, *Y = nullptr;
  890. if (!Known.Zero[0] && !Known.One[0] &&
  891. match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) {
  892. Known2.resetAll();
  893. computeKnownBits(Y, Known2, Depth + 1, Q);
  894. if (Known2.countMinTrailingOnes() > 0)
  895. Known.Zero.setBit(0);
  896. }
  897. break;
  898. }
  899. case Instruction::Or:
  900. computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
  901. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  902. // Output known-0 bits are only known if clear in both the LHS & RHS.
  903. Known.Zero &= Known2.Zero;
  904. // Output known-1 are known to be set if set in either the LHS | RHS.
  905. Known.One |= Known2.One;
  906. break;
  907. case Instruction::Xor: {
  908. computeKnownBits(I->getOperand(1), Known, Depth + 1, Q);
  909. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  910. // Output known-0 bits are known if clear or set in both the LHS & RHS.
  911. APInt KnownZeroOut = (Known.Zero & Known2.Zero) | (Known.One & Known2.One);
  912. // Output known-1 are known to be set if set in only one of the LHS, RHS.
  913. Known.One = (Known.Zero & Known2.One) | (Known.One & Known2.Zero);
  914. Known.Zero = std::move(KnownZeroOut);
  915. break;
  916. }
  917. case Instruction::Mul: {
  918. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  919. computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, Known,
  920. Known2, Depth, Q);
  921. break;
  922. }
  923. case Instruction::UDiv: {
  924. // For the purposes of computing leading zeros we can conservatively
  925. // treat a udiv as a logical right shift by the power of 2 known to
  926. // be less than the denominator.
  927. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  928. unsigned LeadZ = Known2.countMinLeadingZeros();
  929. Known2.resetAll();
  930. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  931. unsigned RHSMaxLeadingZeros = Known2.countMaxLeadingZeros();
  932. if (RHSMaxLeadingZeros != BitWidth)
  933. LeadZ = std::min(BitWidth, LeadZ + BitWidth - RHSMaxLeadingZeros - 1);
  934. Known.Zero.setHighBits(LeadZ);
  935. break;
  936. }
  937. case Instruction::Select: {
  938. const Value *LHS = nullptr, *RHS = nullptr;
  939. SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
  940. if (SelectPatternResult::isMinOrMax(SPF)) {
  941. computeKnownBits(RHS, Known, Depth + 1, Q);
  942. computeKnownBits(LHS, Known2, Depth + 1, Q);
  943. } else {
  944. computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
  945. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  946. }
  947. unsigned MaxHighOnes = 0;
  948. unsigned MaxHighZeros = 0;
  949. if (SPF == SPF_SMAX) {
  950. // If both sides are negative, the result is negative.
  951. if (Known.isNegative() && Known2.isNegative())
  952. // We can derive a lower bound on the result by taking the max of the
  953. // leading one bits.
  954. MaxHighOnes =
  955. std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
  956. // If either side is non-negative, the result is non-negative.
  957. else if (Known.isNonNegative() || Known2.isNonNegative())
  958. MaxHighZeros = 1;
  959. } else if (SPF == SPF_SMIN) {
  960. // If both sides are non-negative, the result is non-negative.
  961. if (Known.isNonNegative() && Known2.isNonNegative())
  962. // We can derive an upper bound on the result by taking the max of the
  963. // leading zero bits.
  964. MaxHighZeros = std::max(Known.countMinLeadingZeros(),
  965. Known2.countMinLeadingZeros());
  966. // If either side is negative, the result is negative.
  967. else if (Known.isNegative() || Known2.isNegative())
  968. MaxHighOnes = 1;
  969. } else if (SPF == SPF_UMAX) {
  970. // We can derive a lower bound on the result by taking the max of the
  971. // leading one bits.
  972. MaxHighOnes =
  973. std::max(Known.countMinLeadingOnes(), Known2.countMinLeadingOnes());
  974. } else if (SPF == SPF_UMIN) {
  975. // We can derive an upper bound on the result by taking the max of the
  976. // leading zero bits.
  977. MaxHighZeros =
  978. std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
  979. } else if (SPF == SPF_ABS) {
  980. // RHS from matchSelectPattern returns the negation part of abs pattern.
  981. // If the negate has an NSW flag we can assume the sign bit of the result
  982. // will be 0 because that makes abs(INT_MIN) undefined.
  983. if (match(RHS, m_Neg(m_Specific(LHS))) &&
  984. Q.IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
  985. MaxHighZeros = 1;
  986. }
  987. // Only known if known in both the LHS and RHS.
  988. Known.One &= Known2.One;
  989. Known.Zero &= Known2.Zero;
  990. if (MaxHighOnes > 0)
  991. Known.One.setHighBits(MaxHighOnes);
  992. if (MaxHighZeros > 0)
  993. Known.Zero.setHighBits(MaxHighZeros);
  994. break;
  995. }
  996. case Instruction::FPTrunc:
  997. case Instruction::FPExt:
  998. case Instruction::FPToUI:
  999. case Instruction::FPToSI:
  1000. case Instruction::SIToFP:
  1001. case Instruction::UIToFP:
  1002. break; // Can't work with floating point.
  1003. case Instruction::PtrToInt:
  1004. case Instruction::IntToPtr:
  1005. // Fall through and handle them the same as zext/trunc.
  1006. LLVM_FALLTHROUGH;
  1007. case Instruction::ZExt:
  1008. case Instruction::Trunc: {
  1009. Type *SrcTy = I->getOperand(0)->getType();
  1010. unsigned SrcBitWidth;
  1011. // Note that we handle pointer operands here because of inttoptr/ptrtoint
  1012. // which fall through here.
  1013. Type *ScalarTy = SrcTy->getScalarType();
  1014. SrcBitWidth = ScalarTy->isPointerTy() ?
  1015. Q.DL.getIndexTypeSizeInBits(ScalarTy) :
  1016. Q.DL.getTypeSizeInBits(ScalarTy);
  1017. assert(SrcBitWidth && "SrcBitWidth can't be zero");
  1018. Known = Known.zextOrTrunc(SrcBitWidth, false);
  1019. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1020. Known = Known.zextOrTrunc(BitWidth, true /* ExtendedBitsAreKnownZero */);
  1021. break;
  1022. }
  1023. case Instruction::BitCast: {
  1024. Type *SrcTy = I->getOperand(0)->getType();
  1025. if (SrcTy->isIntOrPtrTy() &&
  1026. // TODO: For now, not handling conversions like:
  1027. // (bitcast i64 %x to <2 x i32>)
  1028. !I->getType()->isVectorTy()) {
  1029. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1030. break;
  1031. }
  1032. break;
  1033. }
  1034. case Instruction::SExt: {
  1035. // Compute the bits in the result that are not present in the input.
  1036. unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
  1037. Known = Known.trunc(SrcBitWidth);
  1038. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1039. // If the sign bit of the input is known set or clear, then we know the
  1040. // top bits of the result.
  1041. Known = Known.sext(BitWidth);
  1042. break;
  1043. }
  1044. case Instruction::Shl: {
  1045. // (shl X, C1) & C2 == 0 iff (X & C2 >>u C1) == 0
  1046. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  1047. auto KZF = [NSW](const APInt &KnownZero, unsigned ShiftAmt) {
  1048. APInt KZResult = KnownZero << ShiftAmt;
  1049. KZResult.setLowBits(ShiftAmt); // Low bits known 0.
  1050. // If this shift has "nsw" keyword, then the result is either a poison
  1051. // value or has the same sign bit as the first operand.
  1052. if (NSW && KnownZero.isSignBitSet())
  1053. KZResult.setSignBit();
  1054. return KZResult;
  1055. };
  1056. auto KOF = [NSW](const APInt &KnownOne, unsigned ShiftAmt) {
  1057. APInt KOResult = KnownOne << ShiftAmt;
  1058. if (NSW && KnownOne.isSignBitSet())
  1059. KOResult.setSignBit();
  1060. return KOResult;
  1061. };
  1062. computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
  1063. break;
  1064. }
  1065. case Instruction::LShr: {
  1066. // (lshr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
  1067. auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
  1068. APInt KZResult = KnownZero.lshr(ShiftAmt);
  1069. // High bits known zero.
  1070. KZResult.setHighBits(ShiftAmt);
  1071. return KZResult;
  1072. };
  1073. auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
  1074. return KnownOne.lshr(ShiftAmt);
  1075. };
  1076. computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
  1077. break;
  1078. }
  1079. case Instruction::AShr: {
  1080. // (ashr X, C1) & C2 == 0 iff (-1 >> C1) & C2 == 0
  1081. auto KZF = [](const APInt &KnownZero, unsigned ShiftAmt) {
  1082. return KnownZero.ashr(ShiftAmt);
  1083. };
  1084. auto KOF = [](const APInt &KnownOne, unsigned ShiftAmt) {
  1085. return KnownOne.ashr(ShiftAmt);
  1086. };
  1087. computeKnownBitsFromShiftOperator(I, Known, Known2, Depth, Q, KZF, KOF);
  1088. break;
  1089. }
  1090. case Instruction::Sub: {
  1091. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  1092. computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
  1093. Known, Known2, Depth, Q);
  1094. break;
  1095. }
  1096. case Instruction::Add: {
  1097. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  1098. computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
  1099. Known, Known2, Depth, Q);
  1100. break;
  1101. }
  1102. case Instruction::SRem:
  1103. if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
  1104. APInt RA = Rem->getValue().abs();
  1105. if (RA.isPowerOf2()) {
  1106. APInt LowBits = RA - 1;
  1107. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1108. // The low bits of the first operand are unchanged by the srem.
  1109. Known.Zero = Known2.Zero & LowBits;
  1110. Known.One = Known2.One & LowBits;
  1111. // If the first operand is non-negative or has all low bits zero, then
  1112. // the upper bits are all zero.
  1113. if (Known2.isNonNegative() || LowBits.isSubsetOf(Known2.Zero))
  1114. Known.Zero |= ~LowBits;
  1115. // If the first operand is negative and not all low bits are zero, then
  1116. // the upper bits are all one.
  1117. if (Known2.isNegative() && LowBits.intersects(Known2.One))
  1118. Known.One |= ~LowBits;
  1119. assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
  1120. break;
  1121. }
  1122. }
  1123. // The sign bit is the LHS's sign bit, except when the result of the
  1124. // remainder is zero.
  1125. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1126. // If it's known zero, our sign bit is also zero.
  1127. if (Known2.isNonNegative())
  1128. Known.makeNonNegative();
  1129. break;
  1130. case Instruction::URem: {
  1131. if (ConstantInt *Rem = dyn_cast<ConstantInt>(I->getOperand(1))) {
  1132. const APInt &RA = Rem->getValue();
  1133. if (RA.isPowerOf2()) {
  1134. APInt LowBits = (RA - 1);
  1135. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1136. Known.Zero |= ~LowBits;
  1137. Known.One &= LowBits;
  1138. break;
  1139. }
  1140. }
  1141. // Since the result is less than or equal to either operand, any leading
  1142. // zero bits in either operand must also exist in the result.
  1143. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1144. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1145. unsigned Leaders =
  1146. std::max(Known.countMinLeadingZeros(), Known2.countMinLeadingZeros());
  1147. Known.resetAll();
  1148. Known.Zero.setHighBits(Leaders);
  1149. break;
  1150. }
  1151. case Instruction::Alloca: {
  1152. const AllocaInst *AI = cast<AllocaInst>(I);
  1153. unsigned Align = AI->getAlignment();
  1154. if (Align == 0)
  1155. Align = Q.DL.getABITypeAlignment(AI->getAllocatedType());
  1156. if (Align > 0)
  1157. Known.Zero.setLowBits(countTrailingZeros(Align));
  1158. break;
  1159. }
  1160. case Instruction::GetElementPtr: {
  1161. // Analyze all of the subscripts of this getelementptr instruction
  1162. // to determine if we can prove known low zero bits.
  1163. KnownBits LocalKnown(BitWidth);
  1164. computeKnownBits(I->getOperand(0), LocalKnown, Depth + 1, Q);
  1165. unsigned TrailZ = LocalKnown.countMinTrailingZeros();
  1166. gep_type_iterator GTI = gep_type_begin(I);
  1167. for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
  1168. Value *Index = I->getOperand(i);
  1169. if (StructType *STy = GTI.getStructTypeOrNull()) {
  1170. // Handle struct member offset arithmetic.
  1171. // Handle case when index is vector zeroinitializer
  1172. Constant *CIndex = cast<Constant>(Index);
  1173. if (CIndex->isZeroValue())
  1174. continue;
  1175. if (CIndex->getType()->isVectorTy())
  1176. Index = CIndex->getSplatValue();
  1177. unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
  1178. const StructLayout *SL = Q.DL.getStructLayout(STy);
  1179. uint64_t Offset = SL->getElementOffset(Idx);
  1180. TrailZ = std::min<unsigned>(TrailZ,
  1181. countTrailingZeros(Offset));
  1182. } else {
  1183. // Handle array index arithmetic.
  1184. Type *IndexedTy = GTI.getIndexedType();
  1185. if (!IndexedTy->isSized()) {
  1186. TrailZ = 0;
  1187. break;
  1188. }
  1189. unsigned GEPOpiBits = Index->getType()->getScalarSizeInBits();
  1190. uint64_t TypeSize = Q.DL.getTypeAllocSize(IndexedTy);
  1191. LocalKnown.Zero = LocalKnown.One = APInt(GEPOpiBits, 0);
  1192. computeKnownBits(Index, LocalKnown, Depth + 1, Q);
  1193. TrailZ = std::min(TrailZ,
  1194. unsigned(countTrailingZeros(TypeSize) +
  1195. LocalKnown.countMinTrailingZeros()));
  1196. }
  1197. }
  1198. Known.Zero.setLowBits(TrailZ);
  1199. break;
  1200. }
  1201. case Instruction::PHI: {
  1202. const PHINode *P = cast<PHINode>(I);
  1203. // Handle the case of a simple two-predecessor recurrence PHI.
  1204. // There's a lot more that could theoretically be done here, but
  1205. // this is sufficient to catch some interesting cases.
  1206. if (P->getNumIncomingValues() == 2) {
  1207. for (unsigned i = 0; i != 2; ++i) {
  1208. Value *L = P->getIncomingValue(i);
  1209. Value *R = P->getIncomingValue(!i);
  1210. Operator *LU = dyn_cast<Operator>(L);
  1211. if (!LU)
  1212. continue;
  1213. unsigned Opcode = LU->getOpcode();
  1214. // Check for operations that have the property that if
  1215. // both their operands have low zero bits, the result
  1216. // will have low zero bits.
  1217. if (Opcode == Instruction::Add ||
  1218. Opcode == Instruction::Sub ||
  1219. Opcode == Instruction::And ||
  1220. Opcode == Instruction::Or ||
  1221. Opcode == Instruction::Mul) {
  1222. Value *LL = LU->getOperand(0);
  1223. Value *LR = LU->getOperand(1);
  1224. // Find a recurrence.
  1225. if (LL == I)
  1226. L = LR;
  1227. else if (LR == I)
  1228. L = LL;
  1229. else
  1230. continue; // Check for recurrence with L and R flipped.
  1231. // Ok, we have a PHI of the form L op= R. Check for low
  1232. // zero bits.
  1233. computeKnownBits(R, Known2, Depth + 1, Q);
  1234. // We need to take the minimum number of known bits
  1235. KnownBits Known3(Known);
  1236. computeKnownBits(L, Known3, Depth + 1, Q);
  1237. Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
  1238. Known3.countMinTrailingZeros()));
  1239. auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(LU);
  1240. if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
  1241. // If initial value of recurrence is nonnegative, and we are adding
  1242. // a nonnegative number with nsw, the result can only be nonnegative
  1243. // or poison value regardless of the number of times we execute the
  1244. // add in phi recurrence. If initial value is negative and we are
  1245. // adding a negative number with nsw, the result can only be
  1246. // negative or poison value. Similar arguments apply to sub and mul.
  1247. //
  1248. // (add non-negative, non-negative) --> non-negative
  1249. // (add negative, negative) --> negative
  1250. if (Opcode == Instruction::Add) {
  1251. if (Known2.isNonNegative() && Known3.isNonNegative())
  1252. Known.makeNonNegative();
  1253. else if (Known2.isNegative() && Known3.isNegative())
  1254. Known.makeNegative();
  1255. }
  1256. // (sub nsw non-negative, negative) --> non-negative
  1257. // (sub nsw negative, non-negative) --> negative
  1258. else if (Opcode == Instruction::Sub && LL == I) {
  1259. if (Known2.isNonNegative() && Known3.isNegative())
  1260. Known.makeNonNegative();
  1261. else if (Known2.isNegative() && Known3.isNonNegative())
  1262. Known.makeNegative();
  1263. }
  1264. // (mul nsw non-negative, non-negative) --> non-negative
  1265. else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
  1266. Known3.isNonNegative())
  1267. Known.makeNonNegative();
  1268. }
  1269. break;
  1270. }
  1271. }
  1272. }
  1273. // Unreachable blocks may have zero-operand PHI nodes.
  1274. if (P->getNumIncomingValues() == 0)
  1275. break;
  1276. // Otherwise take the unions of the known bit sets of the operands,
  1277. // taking conservative care to avoid excessive recursion.
  1278. if (Depth < MaxDepth - 1 && !Known.Zero && !Known.One) {
  1279. // Skip if every incoming value references to ourself.
  1280. if (dyn_cast_or_null<UndefValue>(P->hasConstantValue()))
  1281. break;
  1282. Known.Zero.setAllBits();
  1283. Known.One.setAllBits();
  1284. for (Value *IncValue : P->incoming_values()) {
  1285. // Skip direct self references.
  1286. if (IncValue == P) continue;
  1287. Known2 = KnownBits(BitWidth);
  1288. // Recurse, but cap the recursion to one level, because we don't
  1289. // want to waste time spinning around in loops.
  1290. computeKnownBits(IncValue, Known2, MaxDepth - 1, Q);
  1291. Known.Zero &= Known2.Zero;
  1292. Known.One &= Known2.One;
  1293. // If all bits have been ruled out, there's no need to check
  1294. // more operands.
  1295. if (!Known.Zero && !Known.One)
  1296. break;
  1297. }
  1298. }
  1299. break;
  1300. }
  1301. case Instruction::Call:
  1302. case Instruction::Invoke:
  1303. // If range metadata is attached to this call, set known bits from that,
  1304. // and then intersect with known bits based on other properties of the
  1305. // function.
  1306. if (MDNode *MD =
  1307. Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
  1308. computeKnownBitsFromRangeMetadata(*MD, Known);
  1309. if (const Value *RV = ImmutableCallSite(I).getReturnedArgOperand()) {
  1310. computeKnownBits(RV, Known2, Depth + 1, Q);
  1311. Known.Zero |= Known2.Zero;
  1312. Known.One |= Known2.One;
  1313. }
  1314. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  1315. switch (II->getIntrinsicID()) {
  1316. default: break;
  1317. case Intrinsic::bitreverse:
  1318. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1319. Known.Zero |= Known2.Zero.reverseBits();
  1320. Known.One |= Known2.One.reverseBits();
  1321. break;
  1322. case Intrinsic::bswap:
  1323. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1324. Known.Zero |= Known2.Zero.byteSwap();
  1325. Known.One |= Known2.One.byteSwap();
  1326. break;
  1327. case Intrinsic::ctlz: {
  1328. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1329. // If we have a known 1, its position is our upper bound.
  1330. unsigned PossibleLZ = Known2.One.countLeadingZeros();
  1331. // If this call is undefined for 0, the result will be less than 2^n.
  1332. if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
  1333. PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
  1334. unsigned LowBits = Log2_32(PossibleLZ)+1;
  1335. Known.Zero.setBitsFrom(LowBits);
  1336. break;
  1337. }
  1338. case Intrinsic::cttz: {
  1339. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1340. // If we have a known 1, its position is our upper bound.
  1341. unsigned PossibleTZ = Known2.One.countTrailingZeros();
  1342. // If this call is undefined for 0, the result will be less than 2^n.
  1343. if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
  1344. PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
  1345. unsigned LowBits = Log2_32(PossibleTZ)+1;
  1346. Known.Zero.setBitsFrom(LowBits);
  1347. break;
  1348. }
  1349. case Intrinsic::ctpop: {
  1350. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1351. // We can bound the space the count needs. Also, bits known to be zero
  1352. // can't contribute to the population.
  1353. unsigned BitsPossiblySet = Known2.countMaxPopulation();
  1354. unsigned LowBits = Log2_32(BitsPossiblySet)+1;
  1355. Known.Zero.setBitsFrom(LowBits);
  1356. // TODO: we could bound KnownOne using the lower bound on the number
  1357. // of bits which might be set provided by popcnt KnownOne2.
  1358. break;
  1359. }
  1360. case Intrinsic::fshr:
  1361. case Intrinsic::fshl: {
  1362. const APInt *SA;
  1363. if (!match(I->getOperand(2), m_APInt(SA)))
  1364. break;
  1365. // Normalize to funnel shift left.
  1366. uint64_t ShiftAmt = SA->urem(BitWidth);
  1367. if (II->getIntrinsicID() == Intrinsic::fshr)
  1368. ShiftAmt = BitWidth - ShiftAmt;
  1369. KnownBits Known3(Known);
  1370. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1371. computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
  1372. Known.Zero =
  1373. Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
  1374. Known.One =
  1375. Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
  1376. break;
  1377. }
  1378. case Intrinsic::uadd_sat:
  1379. case Intrinsic::usub_sat: {
  1380. bool IsAdd = II->getIntrinsicID() == Intrinsic::uadd_sat;
  1381. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1382. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1383. // Add: Leading ones of either operand are preserved.
  1384. // Sub: Leading zeros of LHS and leading ones of RHS are preserved
  1385. // as leading zeros in the result.
  1386. unsigned LeadingKnown;
  1387. if (IsAdd)
  1388. LeadingKnown = std::max(Known.countMinLeadingOnes(),
  1389. Known2.countMinLeadingOnes());
  1390. else
  1391. LeadingKnown = std::max(Known.countMinLeadingZeros(),
  1392. Known2.countMinLeadingOnes());
  1393. Known = KnownBits::computeForAddSub(
  1394. IsAdd, /* NSW */ false, Known, Known2);
  1395. // We select between the operation result and all-ones/zero
  1396. // respectively, so we can preserve known ones/zeros.
  1397. if (IsAdd) {
  1398. Known.One.setHighBits(LeadingKnown);
  1399. Known.Zero.clearAllBits();
  1400. } else {
  1401. Known.Zero.setHighBits(LeadingKnown);
  1402. Known.One.clearAllBits();
  1403. }
  1404. break;
  1405. }
  1406. case Intrinsic::x86_sse42_crc32_64_64:
  1407. Known.Zero.setBitsFrom(32);
  1408. break;
  1409. }
  1410. }
  1411. break;
  1412. case Instruction::ExtractElement:
  1413. // Look through extract element. At the moment we keep this simple and skip
  1414. // tracking the specific element. But at least we might find information
  1415. // valid for all elements of the vector (for example if vector is sign
  1416. // extended, shifted, etc).
  1417. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1418. break;
  1419. case Instruction::ExtractValue:
  1420. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
  1421. const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
  1422. if (EVI->getNumIndices() != 1) break;
  1423. if (EVI->getIndices()[0] == 0) {
  1424. switch (II->getIntrinsicID()) {
  1425. default: break;
  1426. case Intrinsic::uadd_with_overflow:
  1427. case Intrinsic::sadd_with_overflow:
  1428. computeKnownBitsAddSub(true, II->getArgOperand(0),
  1429. II->getArgOperand(1), false, Known, Known2,
  1430. Depth, Q);
  1431. break;
  1432. case Intrinsic::usub_with_overflow:
  1433. case Intrinsic::ssub_with_overflow:
  1434. computeKnownBitsAddSub(false, II->getArgOperand(0),
  1435. II->getArgOperand(1), false, Known, Known2,
  1436. Depth, Q);
  1437. break;
  1438. case Intrinsic::umul_with_overflow:
  1439. case Intrinsic::smul_with_overflow:
  1440. computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
  1441. Known, Known2, Depth, Q);
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. }
  1447. }
  1448. /// Determine which bits of V are known to be either zero or one and return
  1449. /// them.
  1450. KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) {
  1451. KnownBits Known(getBitWidth(V->getType(), Q.DL));
  1452. computeKnownBits(V, Known, Depth, Q);
  1453. return Known;
  1454. }
  1455. /// Determine which bits of V are known to be either zero or one and return
  1456. /// them in the Known bit set.
  1457. ///
  1458. /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
  1459. /// we cannot optimize based on the assumption that it is zero without changing
  1460. /// it to be an explicit zero. If we don't change it to zero, other code could
  1461. /// optimized based on the contradictory assumption that it is non-zero.
  1462. /// Because instcombine aggressively folds operations with undef args anyway,
  1463. /// this won't lose us code quality.
  1464. ///
  1465. /// This function is defined on values with integer type, values with pointer
  1466. /// type, and vectors of integers. In the case
  1467. /// where V is a vector, known zero, and known one values are the
  1468. /// same width as the vector element, and the bit is set only if it is true
  1469. /// for all of the elements in the vector.
  1470. void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
  1471. const Query &Q) {
  1472. assert(V && "No Value?");
  1473. assert(Depth <= MaxDepth && "Limit Search Depth");
  1474. unsigned BitWidth = Known.getBitWidth();
  1475. assert((V->getType()->isIntOrIntVectorTy(BitWidth) ||
  1476. V->getType()->isPtrOrPtrVectorTy()) &&
  1477. "Not integer or pointer type!");
  1478. Type *ScalarTy = V->getType()->getScalarType();
  1479. unsigned ExpectedWidth = ScalarTy->isPointerTy() ?
  1480. Q.DL.getIndexTypeSizeInBits(ScalarTy) : Q.DL.getTypeSizeInBits(ScalarTy);
  1481. assert(ExpectedWidth == BitWidth && "V and Known should have same BitWidth");
  1482. (void)BitWidth;
  1483. (void)ExpectedWidth;
  1484. const APInt *C;
  1485. if (match(V, m_APInt(C))) {
  1486. // We know all of the bits for a scalar constant or a splat vector constant!
  1487. Known.One = *C;
  1488. Known.Zero = ~Known.One;
  1489. return;
  1490. }
  1491. // Null and aggregate-zero are all-zeros.
  1492. if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
  1493. Known.setAllZero();
  1494. return;
  1495. }
  1496. // Handle a constant vector by taking the intersection of the known bits of
  1497. // each element.
  1498. if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(V)) {
  1499. // We know that CDS must be a vector of integers. Take the intersection of
  1500. // each element.
  1501. Known.Zero.setAllBits(); Known.One.setAllBits();
  1502. for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
  1503. APInt Elt = CDS->getElementAsAPInt(i);
  1504. Known.Zero &= ~Elt;
  1505. Known.One &= Elt;
  1506. }
  1507. return;
  1508. }
  1509. if (const auto *CV = dyn_cast<ConstantVector>(V)) {
  1510. // We know that CV must be a vector of integers. Take the intersection of
  1511. // each element.
  1512. Known.Zero.setAllBits(); Known.One.setAllBits();
  1513. for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
  1514. Constant *Element = CV->getAggregateElement(i);
  1515. auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
  1516. if (!ElementCI) {
  1517. Known.resetAll();
  1518. return;
  1519. }
  1520. const APInt &Elt = ElementCI->getValue();
  1521. Known.Zero &= ~Elt;
  1522. Known.One &= Elt;
  1523. }
  1524. return;
  1525. }
  1526. // Start out not knowing anything.
  1527. Known.resetAll();
  1528. // We can't imply anything about undefs.
  1529. if (isa<UndefValue>(V))
  1530. return;
  1531. // There's no point in looking through other users of ConstantData for
  1532. // assumptions. Confirm that we've handled them all.
  1533. assert(!isa<ConstantData>(V) && "Unhandled constant data!");
  1534. // Limit search depth.
  1535. // All recursive calls that increase depth must come after this.
  1536. if (Depth == MaxDepth)
  1537. return;
  1538. // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
  1539. // the bits of its aliasee.
  1540. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
  1541. if (!GA->isInterposable())
  1542. computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
  1543. return;
  1544. }
  1545. if (const Operator *I = dyn_cast<Operator>(V))
  1546. computeKnownBitsFromOperator(I, Known, Depth, Q);
  1547. // Aligned pointers have trailing zeros - refine Known.Zero set
  1548. if (V->getType()->isPointerTy()) {
  1549. const MaybeAlign Align = V->getPointerAlignment(Q.DL);
  1550. if (Align)
  1551. Known.Zero.setLowBits(countTrailingZeros(Align->value()));
  1552. }
  1553. // computeKnownBitsFromAssume strictly refines Known.
  1554. // Therefore, we run them after computeKnownBitsFromOperator.
  1555. // Check whether a nearby assume intrinsic can determine some known bits.
  1556. computeKnownBitsFromAssume(V, Known, Depth, Q);
  1557. assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
  1558. }
  1559. /// Return true if the given value is known to have exactly one
  1560. /// bit set when defined. For vectors return true if every element is known to
  1561. /// be a power of two when defined. Supports values with integer or pointer
  1562. /// types and vectors of integers.
  1563. bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
  1564. const Query &Q) {
  1565. assert(Depth <= MaxDepth && "Limit Search Depth");
  1566. // Attempt to match against constants.
  1567. if (OrZero && match(V, m_Power2OrZero()))
  1568. return true;
  1569. if (match(V, m_Power2()))
  1570. return true;
  1571. // 1 << X is clearly a power of two if the one is not shifted off the end. If
  1572. // it is shifted off the end then the result is undefined.
  1573. if (match(V, m_Shl(m_One(), m_Value())))
  1574. return true;
  1575. // (signmask) >>l X is clearly a power of two if the one is not shifted off
  1576. // the bottom. If it is shifted off the bottom then the result is undefined.
  1577. if (match(V, m_LShr(m_SignMask(), m_Value())))
  1578. return true;
  1579. // The remaining tests are all recursive, so bail out if we hit the limit.
  1580. if (Depth++ == MaxDepth)
  1581. return false;
  1582. Value *X = nullptr, *Y = nullptr;
  1583. // A shift left or a logical shift right of a power of two is a power of two
  1584. // or zero.
  1585. if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
  1586. match(V, m_LShr(m_Value(X), m_Value()))))
  1587. return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q);
  1588. if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V))
  1589. return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q);
  1590. if (const SelectInst *SI = dyn_cast<SelectInst>(V))
  1591. return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) &&
  1592. isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q);
  1593. if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
  1594. // A power of two and'd with anything is a power of two or zero.
  1595. if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) ||
  1596. isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q))
  1597. return true;
  1598. // X & (-X) is always a power of two or zero.
  1599. if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
  1600. return true;
  1601. return false;
  1602. }
  1603. // Adding a power-of-two or zero to the same power-of-two or zero yields
  1604. // either the original power-of-two, a larger power-of-two or zero.
  1605. if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
  1606. const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
  1607. if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
  1608. Q.IIQ.hasNoSignedWrap(VOBO)) {
  1609. if (match(X, m_And(m_Specific(Y), m_Value())) ||
  1610. match(X, m_And(m_Value(), m_Specific(Y))))
  1611. if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q))
  1612. return true;
  1613. if (match(Y, m_And(m_Specific(X), m_Value())) ||
  1614. match(Y, m_And(m_Value(), m_Specific(X))))
  1615. if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q))
  1616. return true;
  1617. unsigned BitWidth = V->getType()->getScalarSizeInBits();
  1618. KnownBits LHSBits(BitWidth);
  1619. computeKnownBits(X, LHSBits, Depth, Q);
  1620. KnownBits RHSBits(BitWidth);
  1621. computeKnownBits(Y, RHSBits, Depth, Q);
  1622. // If i8 V is a power of two or zero:
  1623. // ZeroBits: 1 1 1 0 1 1 1 1
  1624. // ~ZeroBits: 0 0 0 1 0 0 0 0
  1625. if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
  1626. // If OrZero isn't set, we cannot give back a zero result.
  1627. // Make sure either the LHS or RHS has a bit set.
  1628. if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
  1629. return true;
  1630. }
  1631. }
  1632. // An exact divide or right shift can only shift off zero bits, so the result
  1633. // is a power of two only if the first operand is a power of two and not
  1634. // copying a sign bit (sdiv int_min, 2).
  1635. if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
  1636. match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
  1637. return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
  1638. Depth, Q);
  1639. }
  1640. return false;
  1641. }
  1642. /// Test whether a GEP's result is known to be non-null.
  1643. ///
  1644. /// Uses properties inherent in a GEP to try to determine whether it is known
  1645. /// to be non-null.
  1646. ///
  1647. /// Currently this routine does not support vector GEPs.
  1648. static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
  1649. const Query &Q) {
  1650. const Function *F = nullptr;
  1651. if (const Instruction *I = dyn_cast<Instruction>(GEP))
  1652. F = I->getFunction();
  1653. if (!GEP->isInBounds() ||
  1654. NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
  1655. return false;
  1656. // FIXME: Support vector-GEPs.
  1657. assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
  1658. // If the base pointer is non-null, we cannot walk to a null address with an
  1659. // inbounds GEP in address space zero.
  1660. if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q))
  1661. return true;
  1662. // Walk the GEP operands and see if any operand introduces a non-zero offset.
  1663. // If so, then the GEP cannot produce a null pointer, as doing so would
  1664. // inherently violate the inbounds contract within address space zero.
  1665. for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
  1666. GTI != GTE; ++GTI) {
  1667. // Struct types are easy -- they must always be indexed by a constant.
  1668. if (StructType *STy = GTI.getStructTypeOrNull()) {
  1669. ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
  1670. unsigned ElementIdx = OpC->getZExtValue();
  1671. const StructLayout *SL = Q.DL.getStructLayout(STy);
  1672. uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
  1673. if (ElementOffset > 0)
  1674. return true;
  1675. continue;
  1676. }
  1677. // If we have a zero-sized type, the index doesn't matter. Keep looping.
  1678. if (Q.DL.getTypeAllocSize(GTI.getIndexedType()) == 0)
  1679. continue;
  1680. // Fast path the constant operand case both for efficiency and so we don't
  1681. // increment Depth when just zipping down an all-constant GEP.
  1682. if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
  1683. if (!OpC->isZero())
  1684. return true;
  1685. continue;
  1686. }
  1687. // We post-increment Depth here because while isKnownNonZero increments it
  1688. // as well, when we pop back up that increment won't persist. We don't want
  1689. // to recurse 10k times just because we have 10k GEP operands. We don't
  1690. // bail completely out because we want to handle constant GEPs regardless
  1691. // of depth.
  1692. if (Depth++ >= MaxDepth)
  1693. continue;
  1694. if (isKnownNonZero(GTI.getOperand(), Depth, Q))
  1695. return true;
  1696. }
  1697. return false;
  1698. }
  1699. static bool isKnownNonNullFromDominatingCondition(const Value *V,
  1700. const Instruction *CtxI,
  1701. const DominatorTree *DT) {
  1702. assert(V->getType()->isPointerTy() && "V must be pointer type");
  1703. assert(!isa<ConstantData>(V) && "Did not expect ConstantPointerNull");
  1704. if (!CtxI || !DT)
  1705. return false;
  1706. unsigned NumUsesExplored = 0;
  1707. for (auto *U : V->users()) {
  1708. // Avoid massive lists
  1709. if (NumUsesExplored >= DomConditionsMaxUses)
  1710. break;
  1711. NumUsesExplored++;
  1712. // If the value is used as an argument to a call or invoke, then argument
  1713. // attributes may provide an answer about null-ness.
  1714. if (auto CS = ImmutableCallSite(U))
  1715. if (auto *CalledFunc = CS.getCalledFunction())
  1716. for (const Argument &Arg : CalledFunc->args())
  1717. if (CS.getArgOperand(Arg.getArgNo()) == V &&
  1718. Arg.hasNonNullAttr() && DT->dominates(CS.getInstruction(), CtxI))
  1719. return true;
  1720. // Consider only compare instructions uniquely controlling a branch
  1721. CmpInst::Predicate Pred;
  1722. if (!match(const_cast<User *>(U),
  1723. m_c_ICmp(Pred, m_Specific(V), m_Zero())) ||
  1724. (Pred != ICmpInst::ICMP_EQ && Pred != ICmpInst::ICMP_NE))
  1725. continue;
  1726. SmallVector<const User *, 4> WorkList;
  1727. SmallPtrSet<const User *, 4> Visited;
  1728. for (auto *CmpU : U->users()) {
  1729. assert(WorkList.empty() && "Should be!");
  1730. if (Visited.insert(CmpU).second)
  1731. WorkList.push_back(CmpU);
  1732. while (!WorkList.empty()) {
  1733. auto *Curr = WorkList.pop_back_val();
  1734. // If a user is an AND, add all its users to the work list. We only
  1735. // propagate "pred != null" condition through AND because it is only
  1736. // correct to assume that all conditions of AND are met in true branch.
  1737. // TODO: Support similar logic of OR and EQ predicate?
  1738. if (Pred == ICmpInst::ICMP_NE)
  1739. if (auto *BO = dyn_cast<BinaryOperator>(Curr))
  1740. if (BO->getOpcode() == Instruction::And) {
  1741. for (auto *BOU : BO->users())
  1742. if (Visited.insert(BOU).second)
  1743. WorkList.push_back(BOU);
  1744. continue;
  1745. }
  1746. if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
  1747. assert(BI->isConditional() && "uses a comparison!");
  1748. BasicBlock *NonNullSuccessor =
  1749. BI->getSuccessor(Pred == ICmpInst::ICMP_EQ ? 1 : 0);
  1750. BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
  1751. if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
  1752. return true;
  1753. } else if (Pred == ICmpInst::ICMP_NE && isGuard(Curr) &&
  1754. DT->dominates(cast<Instruction>(Curr), CtxI)) {
  1755. return true;
  1756. }
  1757. }
  1758. }
  1759. }
  1760. return false;
  1761. }
  1762. /// Does the 'Range' metadata (which must be a valid MD_range operand list)
  1763. /// ensure that the value it's attached to is never Value? 'RangeType' is
  1764. /// is the type of the value described by the range.
  1765. static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
  1766. const unsigned NumRanges = Ranges->getNumOperands() / 2;
  1767. assert(NumRanges >= 1);
  1768. for (unsigned i = 0; i < NumRanges; ++i) {
  1769. ConstantInt *Lower =
  1770. mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
  1771. ConstantInt *Upper =
  1772. mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
  1773. ConstantRange Range(Lower->getValue(), Upper->getValue());
  1774. if (Range.contains(Value))
  1775. return false;
  1776. }
  1777. return true;
  1778. }
  1779. /// Return true if the given value is known to be non-zero when defined. For
  1780. /// vectors, return true if every element is known to be non-zero when
  1781. /// defined. For pointers, if the context instruction and dominator tree are
  1782. /// specified, perform context-sensitive analysis and return true if the
  1783. /// pointer couldn't possibly be null at the specified instruction.
  1784. /// Supports values with integer or pointer type and vectors of integers.
  1785. bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q) {
  1786. if (auto *C = dyn_cast<Constant>(V)) {
  1787. if (C->isNullValue())
  1788. return false;
  1789. if (isa<ConstantInt>(C))
  1790. // Must be non-zero due to null test above.
  1791. return true;
  1792. if (auto *CE = dyn_cast<ConstantExpr>(C)) {
  1793. // See the comment for IntToPtr/PtrToInt instructions below.
  1794. if (CE->getOpcode() == Instruction::IntToPtr ||
  1795. CE->getOpcode() == Instruction::PtrToInt)
  1796. if (Q.DL.getTypeSizeInBits(CE->getOperand(0)->getType()) <=
  1797. Q.DL.getTypeSizeInBits(CE->getType()))
  1798. return isKnownNonZero(CE->getOperand(0), Depth, Q);
  1799. }
  1800. // For constant vectors, check that all elements are undefined or known
  1801. // non-zero to determine that the whole vector is known non-zero.
  1802. if (auto *VecTy = dyn_cast<VectorType>(C->getType())) {
  1803. for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
  1804. Constant *Elt = C->getAggregateElement(i);
  1805. if (!Elt || Elt->isNullValue())
  1806. return false;
  1807. if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
  1808. return false;
  1809. }
  1810. return true;
  1811. }
  1812. // A global variable in address space 0 is non null unless extern weak
  1813. // or an absolute symbol reference. Other address spaces may have null as a
  1814. // valid address for a global, so we can't assume anything.
  1815. if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
  1816. if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
  1817. GV->getType()->getAddressSpace() == 0)
  1818. return true;
  1819. } else
  1820. return false;
  1821. }
  1822. if (auto *I = dyn_cast<Instruction>(V)) {
  1823. if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) {
  1824. // If the possible ranges don't contain zero, then the value is
  1825. // definitely non-zero.
  1826. if (auto *Ty = dyn_cast<IntegerType>(V->getType())) {
  1827. const APInt ZeroValue(Ty->getBitWidth(), 0);
  1828. if (rangeMetadataExcludesValue(Ranges, ZeroValue))
  1829. return true;
  1830. }
  1831. }
  1832. }
  1833. // Some of the tests below are recursive, so bail out if we hit the limit.
  1834. if (Depth++ >= MaxDepth)
  1835. return false;
  1836. // Check for pointer simplifications.
  1837. if (V->getType()->isPointerTy()) {
  1838. // Alloca never returns null, malloc might.
  1839. if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0)
  1840. return true;
  1841. // A byval, inalloca, or nonnull argument is never null.
  1842. if (const Argument *A = dyn_cast<Argument>(V))
  1843. if (A->hasByValOrInAllocaAttr() || A->hasNonNullAttr())
  1844. return true;
  1845. // A Load tagged with nonnull metadata is never null.
  1846. if (const LoadInst *LI = dyn_cast<LoadInst>(V))
  1847. if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
  1848. return true;
  1849. if (const auto *Call = dyn_cast<CallBase>(V)) {
  1850. if (Call->isReturnNonNull())
  1851. return true;
  1852. if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
  1853. return isKnownNonZero(RP, Depth, Q);
  1854. }
  1855. }
  1856. // Check for recursive pointer simplifications.
  1857. if (V->getType()->isPointerTy()) {
  1858. if (isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
  1859. return true;
  1860. // Look through bitcast operations, GEPs, and int2ptr instructions as they
  1861. // do not alter the value, or at least not the nullness property of the
  1862. // value, e.g., int2ptr is allowed to zero/sign extend the value.
  1863. //
  1864. // Note that we have to take special care to avoid looking through
  1865. // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
  1866. // as casts that can alter the value, e.g., AddrSpaceCasts.
  1867. if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V))
  1868. if (isGEPKnownNonNull(GEP, Depth, Q))
  1869. return true;
  1870. if (auto *BCO = dyn_cast<BitCastOperator>(V))
  1871. return isKnownNonZero(BCO->getOperand(0), Depth, Q);
  1872. if (auto *I2P = dyn_cast<IntToPtrInst>(V))
  1873. if (Q.DL.getTypeSizeInBits(I2P->getSrcTy()) <=
  1874. Q.DL.getTypeSizeInBits(I2P->getDestTy()))
  1875. return isKnownNonZero(I2P->getOperand(0), Depth, Q);
  1876. }
  1877. // Similar to int2ptr above, we can look through ptr2int here if the cast
  1878. // is a no-op or an extend and not a truncate.
  1879. if (auto *P2I = dyn_cast<PtrToIntInst>(V))
  1880. if (Q.DL.getTypeSizeInBits(P2I->getSrcTy()) <=
  1881. Q.DL.getTypeSizeInBits(P2I->getDestTy()))
  1882. return isKnownNonZero(P2I->getOperand(0), Depth, Q);
  1883. unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL);
  1884. // X | Y != 0 if X != 0 or Y != 0.
  1885. Value *X = nullptr, *Y = nullptr;
  1886. if (match(V, m_Or(m_Value(X), m_Value(Y))))
  1887. return isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q);
  1888. // ext X != 0 if X != 0.
  1889. if (isa<SExtInst>(V) || isa<ZExtInst>(V))
  1890. return isKnownNonZero(cast<Instruction>(V)->getOperand(0), Depth, Q);
  1891. // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
  1892. // if the lowest bit is shifted off the end.
  1893. if (match(V, m_Shl(m_Value(X), m_Value(Y)))) {
  1894. // shl nuw can't remove any non-zero bits.
  1895. const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
  1896. if (Q.IIQ.hasNoUnsignedWrap(BO))
  1897. return isKnownNonZero(X, Depth, Q);
  1898. KnownBits Known(BitWidth);
  1899. computeKnownBits(X, Known, Depth, Q);
  1900. if (Known.One[0])
  1901. return true;
  1902. }
  1903. // shr X, Y != 0 if X is negative. Note that the value of the shift is not
  1904. // defined if the sign bit is shifted off the end.
  1905. else if (match(V, m_Shr(m_Value(X), m_Value(Y)))) {
  1906. // shr exact can only shift out zero bits.
  1907. const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
  1908. if (BO->isExact())
  1909. return isKnownNonZero(X, Depth, Q);
  1910. KnownBits Known = computeKnownBits(X, Depth, Q);
  1911. if (Known.isNegative())
  1912. return true;
  1913. // If the shifter operand is a constant, and all of the bits shifted
  1914. // out are known to be zero, and X is known non-zero then at least one
  1915. // non-zero bit must remain.
  1916. if (ConstantInt *Shift = dyn_cast<ConstantInt>(Y)) {
  1917. auto ShiftVal = Shift->getLimitedValue(BitWidth - 1);
  1918. // Is there a known one in the portion not shifted out?
  1919. if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal)
  1920. return true;
  1921. // Are all the bits to be shifted out known zero?
  1922. if (Known.countMinTrailingZeros() >= ShiftVal)
  1923. return isKnownNonZero(X, Depth, Q);
  1924. }
  1925. }
  1926. // div exact can only produce a zero if the dividend is zero.
  1927. else if (match(V, m_Exact(m_IDiv(m_Value(X), m_Value())))) {
  1928. return isKnownNonZero(X, Depth, Q);
  1929. }
  1930. // X + Y.
  1931. else if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
  1932. KnownBits XKnown = computeKnownBits(X, Depth, Q);
  1933. KnownBits YKnown = computeKnownBits(Y, Depth, Q);
  1934. // If X and Y are both non-negative (as signed values) then their sum is not
  1935. // zero unless both X and Y are zero.
  1936. if (XKnown.isNonNegative() && YKnown.isNonNegative())
  1937. if (isKnownNonZero(X, Depth, Q) || isKnownNonZero(Y, Depth, Q))
  1938. return true;
  1939. // If X and Y are both negative (as signed values) then their sum is not
  1940. // zero unless both X and Y equal INT_MIN.
  1941. if (XKnown.isNegative() && YKnown.isNegative()) {
  1942. APInt Mask = APInt::getSignedMaxValue(BitWidth);
  1943. // The sign bit of X is set. If some other bit is set then X is not equal
  1944. // to INT_MIN.
  1945. if (XKnown.One.intersects(Mask))
  1946. return true;
  1947. // The sign bit of Y is set. If some other bit is set then Y is not equal
  1948. // to INT_MIN.
  1949. if (YKnown.One.intersects(Mask))
  1950. return true;
  1951. }
  1952. // The sum of a non-negative number and a power of two is not zero.
  1953. if (XKnown.isNonNegative() &&
  1954. isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
  1955. return true;
  1956. if (YKnown.isNonNegative() &&
  1957. isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
  1958. return true;
  1959. }
  1960. // X * Y.
  1961. else if (match(V, m_Mul(m_Value(X), m_Value(Y)))) {
  1962. const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
  1963. // If X and Y are non-zero then so is X * Y as long as the multiplication
  1964. // does not overflow.
  1965. if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
  1966. isKnownNonZero(X, Depth, Q) && isKnownNonZero(Y, Depth, Q))
  1967. return true;
  1968. }
  1969. // (C ? X : Y) != 0 if X != 0 and Y != 0.
  1970. else if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
  1971. if (isKnownNonZero(SI->getTrueValue(), Depth, Q) &&
  1972. isKnownNonZero(SI->getFalseValue(), Depth, Q))
  1973. return true;
  1974. }
  1975. // PHI
  1976. else if (const PHINode *PN = dyn_cast<PHINode>(V)) {
  1977. // Try and detect a recurrence that monotonically increases from a
  1978. // starting value, as these are common as induction variables.
  1979. if (PN->getNumIncomingValues() == 2) {
  1980. Value *Start = PN->getIncomingValue(0);
  1981. Value *Induction = PN->getIncomingValue(1);
  1982. if (isa<ConstantInt>(Induction) && !isa<ConstantInt>(Start))
  1983. std::swap(Start, Induction);
  1984. if (ConstantInt *C = dyn_cast<ConstantInt>(Start)) {
  1985. if (!C->isZero() && !C->isNegative()) {
  1986. ConstantInt *X;
  1987. if (Q.IIQ.UseInstrInfo &&
  1988. (match(Induction, m_NSWAdd(m_Specific(PN), m_ConstantInt(X))) ||
  1989. match(Induction, m_NUWAdd(m_Specific(PN), m_ConstantInt(X)))) &&
  1990. !X->isNegative())
  1991. return true;
  1992. }
  1993. }
  1994. }
  1995. // Check if all incoming values are non-zero constant.
  1996. bool AllNonZeroConstants = llvm::all_of(PN->operands(), [](Value *V) {
  1997. return isa<ConstantInt>(V) && !cast<ConstantInt>(V)->isZero();
  1998. });
  1999. if (AllNonZeroConstants)
  2000. return true;
  2001. }
  2002. KnownBits Known(BitWidth);
  2003. computeKnownBits(V, Known, Depth, Q);
  2004. return Known.One != 0;
  2005. }
  2006. /// Return true if V2 == V1 + X, where X is known non-zero.
  2007. static bool isAddOfNonZero(const Value *V1, const Value *V2, const Query &Q) {
  2008. const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
  2009. if (!BO || BO->getOpcode() != Instruction::Add)
  2010. return false;
  2011. Value *Op = nullptr;
  2012. if (V2 == BO->getOperand(0))
  2013. Op = BO->getOperand(1);
  2014. else if (V2 == BO->getOperand(1))
  2015. Op = BO->getOperand(0);
  2016. else
  2017. return false;
  2018. return isKnownNonZero(Op, 0, Q);
  2019. }
  2020. /// Return true if it is known that V1 != V2.
  2021. static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q) {
  2022. if (V1 == V2)
  2023. return false;
  2024. if (V1->getType() != V2->getType())
  2025. // We can't look through casts yet.
  2026. return false;
  2027. if (isAddOfNonZero(V1, V2, Q) || isAddOfNonZero(V2, V1, Q))
  2028. return true;
  2029. if (V1->getType()->isIntOrIntVectorTy()) {
  2030. // Are any known bits in V1 contradictory to known bits in V2? If V1
  2031. // has a known zero where V2 has a known one, they must not be equal.
  2032. KnownBits Known1 = computeKnownBits(V1, 0, Q);
  2033. KnownBits Known2 = computeKnownBits(V2, 0, Q);
  2034. if (Known1.Zero.intersects(Known2.One) ||
  2035. Known2.Zero.intersects(Known1.One))
  2036. return true;
  2037. }
  2038. return false;
  2039. }
  2040. /// Return true if 'V & Mask' is known to be zero. We use this predicate to
  2041. /// simplify operations downstream. Mask is known to be zero for bits that V
  2042. /// cannot have.
  2043. ///
  2044. /// This function is defined on values with integer type, values with pointer
  2045. /// type, and vectors of integers. In the case
  2046. /// where V is a vector, the mask, known zero, and known one values are the
  2047. /// same width as the vector element, and the bit is set only if it is true
  2048. /// for all of the elements in the vector.
  2049. bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
  2050. const Query &Q) {
  2051. KnownBits Known(Mask.getBitWidth());
  2052. computeKnownBits(V, Known, Depth, Q);
  2053. return Mask.isSubsetOf(Known.Zero);
  2054. }
  2055. // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
  2056. // Returns the input and lower/upper bounds.
  2057. static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
  2058. const APInt *&CLow, const APInt *&CHigh) {
  2059. assert(isa<Operator>(Select) &&
  2060. cast<Operator>(Select)->getOpcode() == Instruction::Select &&
  2061. "Input should be a Select!");
  2062. const Value *LHS = nullptr, *RHS = nullptr;
  2063. SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor;
  2064. if (SPF != SPF_SMAX && SPF != SPF_SMIN)
  2065. return false;
  2066. if (!match(RHS, m_APInt(CLow)))
  2067. return false;
  2068. const Value *LHS2 = nullptr, *RHS2 = nullptr;
  2069. SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor;
  2070. if (getInverseMinMaxFlavor(SPF) != SPF2)
  2071. return false;
  2072. if (!match(RHS2, m_APInt(CHigh)))
  2073. return false;
  2074. if (SPF == SPF_SMIN)
  2075. std::swap(CLow, CHigh);
  2076. In = LHS2;
  2077. return CLow->sle(*CHigh);
  2078. }
  2079. /// For vector constants, loop over the elements and find the constant with the
  2080. /// minimum number of sign bits. Return 0 if the value is not a vector constant
  2081. /// or if any element was not analyzed; otherwise, return the count for the
  2082. /// element with the minimum number of sign bits.
  2083. static unsigned computeNumSignBitsVectorConstant(const Value *V,
  2084. unsigned TyBits) {
  2085. const auto *CV = dyn_cast<Constant>(V);
  2086. if (!CV || !CV->getType()->isVectorTy())
  2087. return 0;
  2088. unsigned MinSignBits = TyBits;
  2089. unsigned NumElts = CV->getType()->getVectorNumElements();
  2090. for (unsigned i = 0; i != NumElts; ++i) {
  2091. // If we find a non-ConstantInt, bail out.
  2092. auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
  2093. if (!Elt)
  2094. return 0;
  2095. MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
  2096. }
  2097. return MinSignBits;
  2098. }
  2099. static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
  2100. const Query &Q);
  2101. static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
  2102. const Query &Q) {
  2103. unsigned Result = ComputeNumSignBitsImpl(V, Depth, Q);
  2104. assert(Result > 0 && "At least one sign bit needs to be present!");
  2105. return Result;
  2106. }
  2107. /// Return the number of times the sign bit of the register is replicated into
  2108. /// the other bits. We know that at least 1 bit is always equal to the sign bit
  2109. /// (itself), but other cases can give us information. For example, immediately
  2110. /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
  2111. /// other, so we return 3. For vectors, return the number of sign bits for the
  2112. /// vector element with the minimum number of known sign bits.
  2113. static unsigned ComputeNumSignBitsImpl(const Value *V, unsigned Depth,
  2114. const Query &Q) {
  2115. assert(Depth <= MaxDepth && "Limit Search Depth");
  2116. // We return the minimum number of sign bits that are guaranteed to be present
  2117. // in V, so for undef we have to conservatively return 1. We don't have the
  2118. // same behavior for poison though -- that's a FIXME today.
  2119. Type *ScalarTy = V->getType()->getScalarType();
  2120. unsigned TyBits = ScalarTy->isPointerTy() ?
  2121. Q.DL.getIndexTypeSizeInBits(ScalarTy) :
  2122. Q.DL.getTypeSizeInBits(ScalarTy);
  2123. unsigned Tmp, Tmp2;
  2124. unsigned FirstAnswer = 1;
  2125. // Note that ConstantInt is handled by the general computeKnownBits case
  2126. // below.
  2127. if (Depth == MaxDepth)
  2128. return 1; // Limit search depth.
  2129. if (auto *U = dyn_cast<Operator>(V)) {
  2130. switch (Operator::getOpcode(V)) {
  2131. default: break;
  2132. case Instruction::SExt:
  2133. Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
  2134. return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
  2135. case Instruction::SDiv: {
  2136. const APInt *Denominator;
  2137. // sdiv X, C -> adds log(C) sign bits.
  2138. if (match(U->getOperand(1), m_APInt(Denominator))) {
  2139. // Ignore non-positive denominator.
  2140. if (!Denominator->isStrictlyPositive())
  2141. break;
  2142. // Calculate the incoming numerator bits.
  2143. unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2144. // Add floor(log(C)) bits to the numerator bits.
  2145. return std::min(TyBits, NumBits + Denominator->logBase2());
  2146. }
  2147. break;
  2148. }
  2149. case Instruction::SRem: {
  2150. const APInt *Denominator;
  2151. // srem X, C -> we know that the result is within [-C+1,C) when C is a
  2152. // positive constant. This let us put a lower bound on the number of sign
  2153. // bits.
  2154. if (match(U->getOperand(1), m_APInt(Denominator))) {
  2155. // Ignore non-positive denominator.
  2156. if (!Denominator->isStrictlyPositive())
  2157. break;
  2158. // Calculate the incoming numerator bits. SRem by a positive constant
  2159. // can't lower the number of sign bits.
  2160. unsigned NumrBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2161. // Calculate the leading sign bit constraints by examining the
  2162. // denominator. Given that the denominator is positive, there are two
  2163. // cases:
  2164. //
  2165. // 1. the numerator is positive. The result range is [0,C) and [0,C) u<
  2166. // (1 << ceilLogBase2(C)).
  2167. //
  2168. // 2. the numerator is negative. Then the result range is (-C,0] and
  2169. // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
  2170. //
  2171. // Thus a lower bound on the number of sign bits is `TyBits -
  2172. // ceilLogBase2(C)`.
  2173. unsigned ResBits = TyBits - Denominator->ceilLogBase2();
  2174. return std::max(NumrBits, ResBits);
  2175. }
  2176. break;
  2177. }
  2178. case Instruction::AShr: {
  2179. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2180. // ashr X, C -> adds C sign bits. Vectors too.
  2181. const APInt *ShAmt;
  2182. if (match(U->getOperand(1), m_APInt(ShAmt))) {
  2183. if (ShAmt->uge(TyBits))
  2184. break; // Bad shift.
  2185. unsigned ShAmtLimited = ShAmt->getZExtValue();
  2186. Tmp += ShAmtLimited;
  2187. if (Tmp > TyBits) Tmp = TyBits;
  2188. }
  2189. return Tmp;
  2190. }
  2191. case Instruction::Shl: {
  2192. const APInt *ShAmt;
  2193. if (match(U->getOperand(1), m_APInt(ShAmt))) {
  2194. // shl destroys sign bits.
  2195. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2196. if (ShAmt->uge(TyBits) || // Bad shift.
  2197. ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
  2198. Tmp2 = ShAmt->getZExtValue();
  2199. return Tmp - Tmp2;
  2200. }
  2201. break;
  2202. }
  2203. case Instruction::And:
  2204. case Instruction::Or:
  2205. case Instruction::Xor: // NOT is handled here.
  2206. // Logical binary ops preserve the number of sign bits at the worst.
  2207. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2208. if (Tmp != 1) {
  2209. Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2210. FirstAnswer = std::min(Tmp, Tmp2);
  2211. // We computed what we know about the sign bits as our first
  2212. // answer. Now proceed to the generic code that uses
  2213. // computeKnownBits, and pick whichever answer is better.
  2214. }
  2215. break;
  2216. case Instruction::Select: {
  2217. // If we have a clamp pattern, we know that the number of sign bits will
  2218. // be the minimum of the clamp min/max range.
  2219. const Value *X;
  2220. const APInt *CLow, *CHigh;
  2221. if (isSignedMinMaxClamp(U, X, CLow, CHigh))
  2222. return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
  2223. Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2224. if (Tmp == 1) break;
  2225. Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
  2226. return std::min(Tmp, Tmp2);
  2227. }
  2228. case Instruction::Add:
  2229. // Add can have at most one carry bit. Thus we know that the output
  2230. // is, at worst, one more bit than the inputs.
  2231. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2232. if (Tmp == 1) break;
  2233. // Special case decrementing a value (ADD X, -1):
  2234. if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
  2235. if (CRHS->isAllOnesValue()) {
  2236. KnownBits Known(TyBits);
  2237. computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
  2238. // If the input is known to be 0 or 1, the output is 0/-1, which is
  2239. // all sign bits set.
  2240. if ((Known.Zero | 1).isAllOnesValue())
  2241. return TyBits;
  2242. // If we are subtracting one from a positive number, there is no carry
  2243. // out of the result.
  2244. if (Known.isNonNegative())
  2245. return Tmp;
  2246. }
  2247. Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2248. if (Tmp2 == 1) break;
  2249. return std::min(Tmp, Tmp2) - 1;
  2250. case Instruction::Sub:
  2251. Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2252. if (Tmp2 == 1) break;
  2253. // Handle NEG.
  2254. if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
  2255. if (CLHS->isNullValue()) {
  2256. KnownBits Known(TyBits);
  2257. computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
  2258. // If the input is known to be 0 or 1, the output is 0/-1, which is
  2259. // all sign bits set.
  2260. if ((Known.Zero | 1).isAllOnesValue())
  2261. return TyBits;
  2262. // If the input is known to be positive (the sign bit is known clear),
  2263. // the output of the NEG has the same number of sign bits as the
  2264. // input.
  2265. if (Known.isNonNegative())
  2266. return Tmp2;
  2267. // Otherwise, we treat this like a SUB.
  2268. }
  2269. // Sub can have at most one carry bit. Thus we know that the output
  2270. // is, at worst, one more bit than the inputs.
  2271. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2272. if (Tmp == 1) break;
  2273. return std::min(Tmp, Tmp2) - 1;
  2274. case Instruction::Mul: {
  2275. // The output of the Mul can be at most twice the valid bits in the
  2276. // inputs.
  2277. unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2278. if (SignBitsOp0 == 1) break;
  2279. unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2280. if (SignBitsOp1 == 1) break;
  2281. unsigned OutValidBits =
  2282. (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
  2283. return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
  2284. }
  2285. case Instruction::PHI: {
  2286. const PHINode *PN = cast<PHINode>(U);
  2287. unsigned NumIncomingValues = PN->getNumIncomingValues();
  2288. // Don't analyze large in-degree PHIs.
  2289. if (NumIncomingValues > 4) break;
  2290. // Unreachable blocks may have zero-operand PHI nodes.
  2291. if (NumIncomingValues == 0) break;
  2292. // Take the minimum of all incoming values. This can't infinitely loop
  2293. // because of our depth threshold.
  2294. Tmp = ComputeNumSignBits(PN->getIncomingValue(0), Depth + 1, Q);
  2295. for (unsigned i = 1, e = NumIncomingValues; i != e; ++i) {
  2296. if (Tmp == 1) return Tmp;
  2297. Tmp = std::min(
  2298. Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, Q));
  2299. }
  2300. return Tmp;
  2301. }
  2302. case Instruction::Trunc:
  2303. // FIXME: it's tricky to do anything useful for this, but it is an
  2304. // important case for targets like X86.
  2305. break;
  2306. case Instruction::ExtractElement:
  2307. // Look through extract element. At the moment we keep this simple and
  2308. // skip tracking the specific element. But at least we might find
  2309. // information valid for all elements of the vector (for example if vector
  2310. // is sign extended, shifted, etc).
  2311. return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2312. case Instruction::ShuffleVector: {
  2313. // TODO: This is copied almost directly from the SelectionDAG version of
  2314. // ComputeNumSignBits. It would be better if we could share common
  2315. // code. If not, make sure that changes are translated to the DAG.
  2316. // Collect the minimum number of sign bits that are shared by every vector
  2317. // element referenced by the shuffle.
  2318. auto *Shuf = cast<ShuffleVectorInst>(U);
  2319. int NumElts = Shuf->getOperand(0)->getType()->getVectorNumElements();
  2320. int NumMaskElts = Shuf->getMask()->getType()->getVectorNumElements();
  2321. APInt DemandedLHS(NumElts, 0), DemandedRHS(NumElts, 0);
  2322. for (int i = 0; i != NumMaskElts; ++i) {
  2323. int M = Shuf->getMaskValue(i);
  2324. assert(M < NumElts * 2 && "Invalid shuffle mask constant");
  2325. // For undef elements, we don't know anything about the common state of
  2326. // the shuffle result.
  2327. if (M == -1)
  2328. return 1;
  2329. if (M < NumElts)
  2330. DemandedLHS.setBit(M % NumElts);
  2331. else
  2332. DemandedRHS.setBit(M % NumElts);
  2333. }
  2334. Tmp = std::numeric_limits<unsigned>::max();
  2335. if (!!DemandedLHS)
  2336. Tmp = ComputeNumSignBits(Shuf->getOperand(0), Depth + 1, Q);
  2337. if (!!DemandedRHS) {
  2338. Tmp2 = ComputeNumSignBits(Shuf->getOperand(1), Depth + 1, Q);
  2339. Tmp = std::min(Tmp, Tmp2);
  2340. }
  2341. // If we don't know anything, early out and try computeKnownBits
  2342. // fall-back.
  2343. if (Tmp == 1)
  2344. break;
  2345. assert(Tmp <= V->getType()->getScalarSizeInBits() &&
  2346. "Failed to determine minimum sign bits");
  2347. return Tmp;
  2348. }
  2349. }
  2350. }
  2351. // Finally, if we can prove that the top bits of the result are 0's or 1's,
  2352. // use this information.
  2353. // If we can examine all elements of a vector constant successfully, we're
  2354. // done (we can't do any better than that). If not, keep trying.
  2355. if (unsigned VecSignBits = computeNumSignBitsVectorConstant(V, TyBits))
  2356. return VecSignBits;
  2357. KnownBits Known(TyBits);
  2358. computeKnownBits(V, Known, Depth, Q);
  2359. // If we know that the sign bit is either zero or one, determine the number of
  2360. // identical bits in the top of the input value.
  2361. return std::max(FirstAnswer, Known.countMinSignBits());
  2362. }
  2363. /// This function computes the integer multiple of Base that equals V.
  2364. /// If successful, it returns true and returns the multiple in
  2365. /// Multiple. If unsuccessful, it returns false. It looks
  2366. /// through SExt instructions only if LookThroughSExt is true.
  2367. bool llvm::ComputeMultiple(Value *V, unsigned Base, Value *&Multiple,
  2368. bool LookThroughSExt, unsigned Depth) {
  2369. assert(V && "No Value?");
  2370. assert(Depth <= MaxDepth && "Limit Search Depth");
  2371. assert(V->getType()->isIntegerTy() && "Not integer or pointer type!");
  2372. Type *T = V->getType();
  2373. ConstantInt *CI = dyn_cast<ConstantInt>(V);
  2374. if (Base == 0)
  2375. return false;
  2376. if (Base == 1) {
  2377. Multiple = V;
  2378. return true;
  2379. }
  2380. ConstantExpr *CO = dyn_cast<ConstantExpr>(V);
  2381. Constant *BaseVal = ConstantInt::get(T, Base);
  2382. if (CO && CO == BaseVal) {
  2383. // Multiple is 1.
  2384. Multiple = ConstantInt::get(T, 1);
  2385. return true;
  2386. }
  2387. if (CI && CI->getZExtValue() % Base == 0) {
  2388. Multiple = ConstantInt::get(T, CI->getZExtValue() / Base);
  2389. return true;
  2390. }
  2391. if (Depth == MaxDepth) return false; // Limit search depth.
  2392. Operator *I = dyn_cast<Operator>(V);
  2393. if (!I) return false;
  2394. switch (I->getOpcode()) {
  2395. default: break;
  2396. case Instruction::SExt:
  2397. if (!LookThroughSExt) return false;
  2398. // otherwise fall through to ZExt
  2399. LLVM_FALLTHROUGH;
  2400. case Instruction::ZExt:
  2401. return ComputeMultiple(I->getOperand(0), Base, Multiple,
  2402. LookThroughSExt, Depth+1);
  2403. case Instruction::Shl:
  2404. case Instruction::Mul: {
  2405. Value *Op0 = I->getOperand(0);
  2406. Value *Op1 = I->getOperand(1);
  2407. if (I->getOpcode() == Instruction::Shl) {
  2408. ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1);
  2409. if (!Op1CI) return false;
  2410. // Turn Op0 << Op1 into Op0 * 2^Op1
  2411. APInt Op1Int = Op1CI->getValue();
  2412. uint64_t BitToSet = Op1Int.getLimitedValue(Op1Int.getBitWidth() - 1);
  2413. APInt API(Op1Int.getBitWidth(), 0);
  2414. API.setBit(BitToSet);
  2415. Op1 = ConstantInt::get(V->getContext(), API);
  2416. }
  2417. Value *Mul0 = nullptr;
  2418. if (ComputeMultiple(Op0, Base, Mul0, LookThroughSExt, Depth+1)) {
  2419. if (Constant *Op1C = dyn_cast<Constant>(Op1))
  2420. if (Constant *MulC = dyn_cast<Constant>(Mul0)) {
  2421. if (Op1C->getType()->getPrimitiveSizeInBits() <
  2422. MulC->getType()->getPrimitiveSizeInBits())
  2423. Op1C = ConstantExpr::getZExt(Op1C, MulC->getType());
  2424. if (Op1C->getType()->getPrimitiveSizeInBits() >
  2425. MulC->getType()->getPrimitiveSizeInBits())
  2426. MulC = ConstantExpr::getZExt(MulC, Op1C->getType());
  2427. // V == Base * (Mul0 * Op1), so return (Mul0 * Op1)
  2428. Multiple = ConstantExpr::getMul(MulC, Op1C);
  2429. return true;
  2430. }
  2431. if (ConstantInt *Mul0CI = dyn_cast<ConstantInt>(Mul0))
  2432. if (Mul0CI->getValue() == 1) {
  2433. // V == Base * Op1, so return Op1
  2434. Multiple = Op1;
  2435. return true;
  2436. }
  2437. }
  2438. Value *Mul1 = nullptr;
  2439. if (ComputeMultiple(Op1, Base, Mul1, LookThroughSExt, Depth+1)) {
  2440. if (Constant *Op0C = dyn_cast<Constant>(Op0))
  2441. if (Constant *MulC = dyn_cast<Constant>(Mul1)) {
  2442. if (Op0C->getType()->getPrimitiveSizeInBits() <
  2443. MulC->getType()->getPrimitiveSizeInBits())
  2444. Op0C = ConstantExpr::getZExt(Op0C, MulC->getType());
  2445. if (Op0C->getType()->getPrimitiveSizeInBits() >
  2446. MulC->getType()->getPrimitiveSizeInBits())
  2447. MulC = ConstantExpr::getZExt(MulC, Op0C->getType());
  2448. // V == Base * (Mul1 * Op0), so return (Mul1 * Op0)
  2449. Multiple = ConstantExpr::getMul(MulC, Op0C);
  2450. return true;
  2451. }
  2452. if (ConstantInt *Mul1CI = dyn_cast<ConstantInt>(Mul1))
  2453. if (Mul1CI->getValue() == 1) {
  2454. // V == Base * Op0, so return Op0
  2455. Multiple = Op0;
  2456. return true;
  2457. }
  2458. }
  2459. }
  2460. }
  2461. // We could not determine if V is a multiple of Base.
  2462. return false;
  2463. }
  2464. Intrinsic::ID llvm::getIntrinsicForCallSite(ImmutableCallSite ICS,
  2465. const TargetLibraryInfo *TLI) {
  2466. const Function *F = ICS.getCalledFunction();
  2467. if (!F)
  2468. return Intrinsic::not_intrinsic;
  2469. if (F->isIntrinsic())
  2470. return F->getIntrinsicID();
  2471. if (!TLI)
  2472. return Intrinsic::not_intrinsic;
  2473. LibFunc Func;
  2474. // We're going to make assumptions on the semantics of the functions, check
  2475. // that the target knows that it's available in this environment and it does
  2476. // not have local linkage.
  2477. if (!F || F->hasLocalLinkage() || !TLI->getLibFunc(*F, Func))
  2478. return Intrinsic::not_intrinsic;
  2479. if (!ICS.onlyReadsMemory())
  2480. return Intrinsic::not_intrinsic;
  2481. // Otherwise check if we have a call to a function that can be turned into a
  2482. // vector intrinsic.
  2483. switch (Func) {
  2484. default:
  2485. break;
  2486. case LibFunc_sin:
  2487. case LibFunc_sinf:
  2488. case LibFunc_sinl:
  2489. return Intrinsic::sin;
  2490. case LibFunc_cos:
  2491. case LibFunc_cosf:
  2492. case LibFunc_cosl:
  2493. return Intrinsic::cos;
  2494. case LibFunc_exp:
  2495. case LibFunc_expf:
  2496. case LibFunc_expl:
  2497. return Intrinsic::exp;
  2498. case LibFunc_exp2:
  2499. case LibFunc_exp2f:
  2500. case LibFunc_exp2l:
  2501. return Intrinsic::exp2;
  2502. case LibFunc_log:
  2503. case LibFunc_logf:
  2504. case LibFunc_logl:
  2505. return Intrinsic::log;
  2506. case LibFunc_log10:
  2507. case LibFunc_log10f:
  2508. case LibFunc_log10l:
  2509. return Intrinsic::log10;
  2510. case LibFunc_log2:
  2511. case LibFunc_log2f:
  2512. case LibFunc_log2l:
  2513. return Intrinsic::log2;
  2514. case LibFunc_fabs:
  2515. case LibFunc_fabsf:
  2516. case LibFunc_fabsl:
  2517. return Intrinsic::fabs;
  2518. case LibFunc_fmin:
  2519. case LibFunc_fminf:
  2520. case LibFunc_fminl:
  2521. return Intrinsic::minnum;
  2522. case LibFunc_fmax:
  2523. case LibFunc_fmaxf:
  2524. case LibFunc_fmaxl:
  2525. return Intrinsic::maxnum;
  2526. case LibFunc_copysign:
  2527. case LibFunc_copysignf:
  2528. case LibFunc_copysignl:
  2529. return Intrinsic::copysign;
  2530. case LibFunc_floor:
  2531. case LibFunc_floorf:
  2532. case LibFunc_floorl:
  2533. return Intrinsic::floor;
  2534. case LibFunc_ceil:
  2535. case LibFunc_ceilf:
  2536. case LibFunc_ceill:
  2537. return Intrinsic::ceil;
  2538. case LibFunc_trunc:
  2539. case LibFunc_truncf:
  2540. case LibFunc_truncl:
  2541. return Intrinsic::trunc;
  2542. case LibFunc_rint:
  2543. case LibFunc_rintf:
  2544. case LibFunc_rintl:
  2545. return Intrinsic::rint;
  2546. case LibFunc_nearbyint:
  2547. case LibFunc_nearbyintf:
  2548. case LibFunc_nearbyintl:
  2549. return Intrinsic::nearbyint;
  2550. case LibFunc_round:
  2551. case LibFunc_roundf:
  2552. case LibFunc_roundl:
  2553. return Intrinsic::round;
  2554. case LibFunc_pow:
  2555. case LibFunc_powf:
  2556. case LibFunc_powl:
  2557. return Intrinsic::pow;
  2558. case LibFunc_sqrt:
  2559. case LibFunc_sqrtf:
  2560. case LibFunc_sqrtl:
  2561. return Intrinsic::sqrt;
  2562. }
  2563. return Intrinsic::not_intrinsic;
  2564. }
  2565. /// Return true if we can prove that the specified FP value is never equal to
  2566. /// -0.0.
  2567. ///
  2568. /// NOTE: this function will need to be revisited when we support non-default
  2569. /// rounding modes!
  2570. bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
  2571. unsigned Depth) {
  2572. if (auto *CFP = dyn_cast<ConstantFP>(V))
  2573. return !CFP->getValueAPF().isNegZero();
  2574. // Limit search depth.
  2575. if (Depth == MaxDepth)
  2576. return false;
  2577. auto *Op = dyn_cast<Operator>(V);
  2578. if (!Op)
  2579. return false;
  2580. // Check if the nsz fast-math flag is set.
  2581. if (auto *FPO = dyn_cast<FPMathOperator>(Op))
  2582. if (FPO->hasNoSignedZeros())
  2583. return true;
  2584. // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
  2585. if (match(Op, m_FAdd(m_Value(), m_PosZeroFP())))
  2586. return true;
  2587. // sitofp and uitofp turn into +0.0 for zero.
  2588. if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op))
  2589. return true;
  2590. if (auto *Call = dyn_cast<CallInst>(Op)) {
  2591. Intrinsic::ID IID = getIntrinsicForCallSite(Call, TLI);
  2592. switch (IID) {
  2593. default:
  2594. break;
  2595. // sqrt(-0.0) = -0.0, no other negative results are possible.
  2596. case Intrinsic::sqrt:
  2597. case Intrinsic::canonicalize:
  2598. return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1);
  2599. // fabs(x) != -0.0
  2600. case Intrinsic::fabs:
  2601. return true;
  2602. }
  2603. }
  2604. return false;
  2605. }
  2606. /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a
  2607. /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign
  2608. /// bit despite comparing equal.
  2609. static bool cannotBeOrderedLessThanZeroImpl(const Value *V,
  2610. const TargetLibraryInfo *TLI,
  2611. bool SignBitOnly,
  2612. unsigned Depth) {
  2613. // TODO: This function does not do the right thing when SignBitOnly is true
  2614. // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform
  2615. // which flips the sign bits of NaNs. See
  2616. // https://llvm.org/bugs/show_bug.cgi?id=31702.
  2617. if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
  2618. return !CFP->getValueAPF().isNegative() ||
  2619. (!SignBitOnly && CFP->getValueAPF().isZero());
  2620. }
  2621. // Handle vector of constants.
  2622. if (auto *CV = dyn_cast<Constant>(V)) {
  2623. if (CV->getType()->isVectorTy()) {
  2624. unsigned NumElts = CV->getType()->getVectorNumElements();
  2625. for (unsigned i = 0; i != NumElts; ++i) {
  2626. auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
  2627. if (!CFP)
  2628. return false;
  2629. if (CFP->getValueAPF().isNegative() &&
  2630. (SignBitOnly || !CFP->getValueAPF().isZero()))
  2631. return false;
  2632. }
  2633. // All non-negative ConstantFPs.
  2634. return true;
  2635. }
  2636. }
  2637. if (Depth == MaxDepth)
  2638. return false; // Limit search depth.
  2639. const Operator *I = dyn_cast<Operator>(V);
  2640. if (!I)
  2641. return false;
  2642. switch (I->getOpcode()) {
  2643. default:
  2644. break;
  2645. // Unsigned integers are always nonnegative.
  2646. case Instruction::UIToFP:
  2647. return true;
  2648. case Instruction::FMul:
  2649. // x*x is always non-negative or a NaN.
  2650. if (I->getOperand(0) == I->getOperand(1) &&
  2651. (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
  2652. return true;
  2653. LLVM_FALLTHROUGH;
  2654. case Instruction::FAdd:
  2655. case Instruction::FDiv:
  2656. case Instruction::FRem:
  2657. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  2658. Depth + 1) &&
  2659. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  2660. Depth + 1);
  2661. case Instruction::Select:
  2662. return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  2663. Depth + 1) &&
  2664. cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
  2665. Depth + 1);
  2666. case Instruction::FPExt:
  2667. case Instruction::FPTrunc:
  2668. // Widening/narrowing never change sign.
  2669. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  2670. Depth + 1);
  2671. case Instruction::ExtractElement:
  2672. // Look through extract element. At the moment we keep this simple and skip
  2673. // tracking the specific element. But at least we might find information
  2674. // valid for all elements of the vector.
  2675. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  2676. Depth + 1);
  2677. case Instruction::Call:
  2678. const auto *CI = cast<CallInst>(I);
  2679. Intrinsic::ID IID = getIntrinsicForCallSite(CI, TLI);
  2680. switch (IID) {
  2681. default:
  2682. break;
  2683. case Intrinsic::maxnum:
  2684. return (isKnownNeverNaN(I->getOperand(0), TLI) &&
  2685. cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI,
  2686. SignBitOnly, Depth + 1)) ||
  2687. (isKnownNeverNaN(I->getOperand(1), TLI) &&
  2688. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI,
  2689. SignBitOnly, Depth + 1));
  2690. case Intrinsic::maximum:
  2691. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  2692. Depth + 1) ||
  2693. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  2694. Depth + 1);
  2695. case Intrinsic::minnum:
  2696. case Intrinsic::minimum:
  2697. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  2698. Depth + 1) &&
  2699. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  2700. Depth + 1);
  2701. case Intrinsic::exp:
  2702. case Intrinsic::exp2:
  2703. case Intrinsic::fabs:
  2704. return true;
  2705. case Intrinsic::sqrt:
  2706. // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0.
  2707. if (!SignBitOnly)
  2708. return true;
  2709. return CI->hasNoNaNs() && (CI->hasNoSignedZeros() ||
  2710. CannotBeNegativeZero(CI->getOperand(0), TLI));
  2711. case Intrinsic::powi:
  2712. if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) {
  2713. // powi(x,n) is non-negative if n is even.
  2714. if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0)
  2715. return true;
  2716. }
  2717. // TODO: This is not correct. Given that exp is an integer, here are the
  2718. // ways that pow can return a negative value:
  2719. //
  2720. // pow(x, exp) --> negative if exp is odd and x is negative.
  2721. // pow(-0, exp) --> -inf if exp is negative odd.
  2722. // pow(-0, exp) --> -0 if exp is positive odd.
  2723. // pow(-inf, exp) --> -0 if exp is negative odd.
  2724. // pow(-inf, exp) --> -inf if exp is positive odd.
  2725. //
  2726. // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN,
  2727. // but we must return false if x == -0. Unfortunately we do not currently
  2728. // have a way of expressing this constraint. See details in
  2729. // https://llvm.org/bugs/show_bug.cgi?id=31702.
  2730. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  2731. Depth + 1);
  2732. case Intrinsic::fma:
  2733. case Intrinsic::fmuladd:
  2734. // x*x+y is non-negative if y is non-negative.
  2735. return I->getOperand(0) == I->getOperand(1) &&
  2736. (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) &&
  2737. cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
  2738. Depth + 1);
  2739. }
  2740. break;
  2741. }
  2742. return false;
  2743. }
  2744. bool llvm::CannotBeOrderedLessThanZero(const Value *V,
  2745. const TargetLibraryInfo *TLI) {
  2746. return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0);
  2747. }
  2748. bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) {
  2749. return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0);
  2750. }
  2751. bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI,
  2752. unsigned Depth) {
  2753. assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type");
  2754. // If we're told that NaNs won't happen, assume they won't.
  2755. if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
  2756. if (FPMathOp->hasNoNaNs())
  2757. return true;
  2758. // Handle scalar constants.
  2759. if (auto *CFP = dyn_cast<ConstantFP>(V))
  2760. return !CFP->isNaN();
  2761. if (Depth == MaxDepth)
  2762. return false;
  2763. if (auto *Inst = dyn_cast<Instruction>(V)) {
  2764. switch (Inst->getOpcode()) {
  2765. case Instruction::FAdd:
  2766. case Instruction::FMul:
  2767. case Instruction::FSub:
  2768. case Instruction::FDiv:
  2769. case Instruction::FRem: {
  2770. // TODO: Need isKnownNeverInfinity
  2771. return false;
  2772. }
  2773. case Instruction::Select: {
  2774. return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
  2775. isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1);
  2776. }
  2777. case Instruction::SIToFP:
  2778. case Instruction::UIToFP:
  2779. return true;
  2780. case Instruction::FPTrunc:
  2781. case Instruction::FPExt:
  2782. return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1);
  2783. default:
  2784. break;
  2785. }
  2786. }
  2787. if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
  2788. switch (II->getIntrinsicID()) {
  2789. case Intrinsic::canonicalize:
  2790. case Intrinsic::fabs:
  2791. case Intrinsic::copysign:
  2792. case Intrinsic::exp:
  2793. case Intrinsic::exp2:
  2794. case Intrinsic::floor:
  2795. case Intrinsic::ceil:
  2796. case Intrinsic::trunc:
  2797. case Intrinsic::rint:
  2798. case Intrinsic::nearbyint:
  2799. case Intrinsic::round:
  2800. return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1);
  2801. case Intrinsic::sqrt:
  2802. return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) &&
  2803. CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI);
  2804. case Intrinsic::minnum:
  2805. case Intrinsic::maxnum:
  2806. // If either operand is not NaN, the result is not NaN.
  2807. return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) ||
  2808. isKnownNeverNaN(II->getArgOperand(1), TLI, Depth + 1);
  2809. default:
  2810. return false;
  2811. }
  2812. }
  2813. // Bail out for constant expressions, but try to handle vector constants.
  2814. if (!V->getType()->isVectorTy() || !isa<Constant>(V))
  2815. return false;
  2816. // For vectors, verify that each element is not NaN.
  2817. unsigned NumElts = V->getType()->getVectorNumElements();
  2818. for (unsigned i = 0; i != NumElts; ++i) {
  2819. Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
  2820. if (!Elt)
  2821. return false;
  2822. if (isa<UndefValue>(Elt))
  2823. continue;
  2824. auto *CElt = dyn_cast<ConstantFP>(Elt);
  2825. if (!CElt || CElt->isNaN())
  2826. return false;
  2827. }
  2828. // All elements were confirmed not-NaN or undefined.
  2829. return true;
  2830. }
  2831. Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
  2832. // All byte-wide stores are splatable, even of arbitrary variables.
  2833. if (V->getType()->isIntegerTy(8))
  2834. return V;
  2835. LLVMContext &Ctx = V->getContext();
  2836. // Undef don't care.
  2837. auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
  2838. if (isa<UndefValue>(V))
  2839. return UndefInt8;
  2840. const uint64_t Size = DL.getTypeStoreSize(V->getType());
  2841. if (!Size)
  2842. return UndefInt8;
  2843. Constant *C = dyn_cast<Constant>(V);
  2844. if (!C) {
  2845. // Conceptually, we could handle things like:
  2846. // %a = zext i8 %X to i16
  2847. // %b = shl i16 %a, 8
  2848. // %c = or i16 %a, %b
  2849. // but until there is an example that actually needs this, it doesn't seem
  2850. // worth worrying about.
  2851. return nullptr;
  2852. }
  2853. // Handle 'null' ConstantArrayZero etc.
  2854. if (C->isNullValue())
  2855. return Constant::getNullValue(Type::getInt8Ty(Ctx));
  2856. // Constant floating-point values can be handled as integer values if the
  2857. // corresponding integer value is "byteable". An important case is 0.0.
  2858. if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
  2859. Type *Ty = nullptr;
  2860. if (CFP->getType()->isHalfTy())
  2861. Ty = Type::getInt16Ty(Ctx);
  2862. else if (CFP->getType()->isFloatTy())
  2863. Ty = Type::getInt32Ty(Ctx);
  2864. else if (CFP->getType()->isDoubleTy())
  2865. Ty = Type::getInt64Ty(Ctx);
  2866. // Don't handle long double formats, which have strange constraints.
  2867. return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
  2868. : nullptr;
  2869. }
  2870. // We can handle constant integers that are multiple of 8 bits.
  2871. if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
  2872. if (CI->getBitWidth() % 8 == 0) {
  2873. assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
  2874. if (!CI->getValue().isSplat(8))
  2875. return nullptr;
  2876. return ConstantInt::get(Ctx, CI->getValue().trunc(8));
  2877. }
  2878. }
  2879. if (auto *CE = dyn_cast<ConstantExpr>(C)) {
  2880. if (CE->getOpcode() == Instruction::IntToPtr) {
  2881. auto PS = DL.getPointerSizeInBits(
  2882. cast<PointerType>(CE->getType())->getAddressSpace());
  2883. return isBytewiseValue(
  2884. ConstantExpr::getIntegerCast(CE->getOperand(0),
  2885. Type::getIntNTy(Ctx, PS), false),
  2886. DL);
  2887. }
  2888. }
  2889. auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
  2890. if (LHS == RHS)
  2891. return LHS;
  2892. if (!LHS || !RHS)
  2893. return nullptr;
  2894. if (LHS == UndefInt8)
  2895. return RHS;
  2896. if (RHS == UndefInt8)
  2897. return LHS;
  2898. return nullptr;
  2899. };
  2900. if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
  2901. Value *Val = UndefInt8;
  2902. for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
  2903. if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
  2904. return nullptr;
  2905. return Val;
  2906. }
  2907. if (isa<ConstantAggregate>(C)) {
  2908. Value *Val = UndefInt8;
  2909. for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
  2910. if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL))))
  2911. return nullptr;
  2912. return Val;
  2913. }
  2914. // Don't try to handle the handful of other constants.
  2915. return nullptr;
  2916. }
  2917. // This is the recursive version of BuildSubAggregate. It takes a few different
  2918. // arguments. Idxs is the index within the nested struct From that we are
  2919. // looking at now (which is of type IndexedType). IdxSkip is the number of
  2920. // indices from Idxs that should be left out when inserting into the resulting
  2921. // struct. To is the result struct built so far, new insertvalue instructions
  2922. // build on that.
  2923. static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
  2924. SmallVectorImpl<unsigned> &Idxs,
  2925. unsigned IdxSkip,
  2926. Instruction *InsertBefore) {
  2927. StructType *STy = dyn_cast<StructType>(IndexedType);
  2928. if (STy) {
  2929. // Save the original To argument so we can modify it
  2930. Value *OrigTo = To;
  2931. // General case, the type indexed by Idxs is a struct
  2932. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  2933. // Process each struct element recursively
  2934. Idxs.push_back(i);
  2935. Value *PrevTo = To;
  2936. To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
  2937. InsertBefore);
  2938. Idxs.pop_back();
  2939. if (!To) {
  2940. // Couldn't find any inserted value for this index? Cleanup
  2941. while (PrevTo != OrigTo) {
  2942. InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
  2943. PrevTo = Del->getAggregateOperand();
  2944. Del->eraseFromParent();
  2945. }
  2946. // Stop processing elements
  2947. break;
  2948. }
  2949. }
  2950. // If we successfully found a value for each of our subaggregates
  2951. if (To)
  2952. return To;
  2953. }
  2954. // Base case, the type indexed by SourceIdxs is not a struct, or not all of
  2955. // the struct's elements had a value that was inserted directly. In the latter
  2956. // case, perhaps we can't determine each of the subelements individually, but
  2957. // we might be able to find the complete struct somewhere.
  2958. // Find the value that is at that particular spot
  2959. Value *V = FindInsertedValue(From, Idxs);
  2960. if (!V)
  2961. return nullptr;
  2962. // Insert the value in the new (sub) aggregate
  2963. return InsertValueInst::Create(To, V, makeArrayRef(Idxs).slice(IdxSkip),
  2964. "tmp", InsertBefore);
  2965. }
  2966. // This helper takes a nested struct and extracts a part of it (which is again a
  2967. // struct) into a new value. For example, given the struct:
  2968. // { a, { b, { c, d }, e } }
  2969. // and the indices "1, 1" this returns
  2970. // { c, d }.
  2971. //
  2972. // It does this by inserting an insertvalue for each element in the resulting
  2973. // struct, as opposed to just inserting a single struct. This will only work if
  2974. // each of the elements of the substruct are known (ie, inserted into From by an
  2975. // insertvalue instruction somewhere).
  2976. //
  2977. // All inserted insertvalue instructions are inserted before InsertBefore
  2978. static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
  2979. Instruction *InsertBefore) {
  2980. assert(InsertBefore && "Must have someplace to insert!");
  2981. Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
  2982. idx_range);
  2983. Value *To = UndefValue::get(IndexedType);
  2984. SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
  2985. unsigned IdxSkip = Idxs.size();
  2986. return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
  2987. }
  2988. /// Given an aggregate and a sequence of indices, see if the scalar value
  2989. /// indexed is already around as a register, for example if it was inserted
  2990. /// directly into the aggregate.
  2991. ///
  2992. /// If InsertBefore is not null, this function will duplicate (modified)
  2993. /// insertvalues when a part of a nested struct is extracted.
  2994. Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
  2995. Instruction *InsertBefore) {
  2996. // Nothing to index? Just return V then (this is useful at the end of our
  2997. // recursion).
  2998. if (idx_range.empty())
  2999. return V;
  3000. // We have indices, so V should have an indexable type.
  3001. assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
  3002. "Not looking at a struct or array?");
  3003. assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
  3004. "Invalid indices for type?");
  3005. if (Constant *C = dyn_cast<Constant>(V)) {
  3006. C = C->getAggregateElement(idx_range[0]);
  3007. if (!C) return nullptr;
  3008. return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
  3009. }
  3010. if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
  3011. // Loop the indices for the insertvalue instruction in parallel with the
  3012. // requested indices
  3013. const unsigned *req_idx = idx_range.begin();
  3014. for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
  3015. i != e; ++i, ++req_idx) {
  3016. if (req_idx == idx_range.end()) {
  3017. // We can't handle this without inserting insertvalues
  3018. if (!InsertBefore)
  3019. return nullptr;
  3020. // The requested index identifies a part of a nested aggregate. Handle
  3021. // this specially. For example,
  3022. // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
  3023. // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
  3024. // %C = extractvalue {i32, { i32, i32 } } %B, 1
  3025. // This can be changed into
  3026. // %A = insertvalue {i32, i32 } undef, i32 10, 0
  3027. // %C = insertvalue {i32, i32 } %A, i32 11, 1
  3028. // which allows the unused 0,0 element from the nested struct to be
  3029. // removed.
  3030. return BuildSubAggregate(V, makeArrayRef(idx_range.begin(), req_idx),
  3031. InsertBefore);
  3032. }
  3033. // This insert value inserts something else than what we are looking for.
  3034. // See if the (aggregate) value inserted into has the value we are
  3035. // looking for, then.
  3036. if (*req_idx != *i)
  3037. return FindInsertedValue(I->getAggregateOperand(), idx_range,
  3038. InsertBefore);
  3039. }
  3040. // If we end up here, the indices of the insertvalue match with those
  3041. // requested (though possibly only partially). Now we recursively look at
  3042. // the inserted value, passing any remaining indices.
  3043. return FindInsertedValue(I->getInsertedValueOperand(),
  3044. makeArrayRef(req_idx, idx_range.end()),
  3045. InsertBefore);
  3046. }
  3047. if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
  3048. // If we're extracting a value from an aggregate that was extracted from
  3049. // something else, we can extract from that something else directly instead.
  3050. // However, we will need to chain I's indices with the requested indices.
  3051. // Calculate the number of indices required
  3052. unsigned size = I->getNumIndices() + idx_range.size();
  3053. // Allocate some space to put the new indices in
  3054. SmallVector<unsigned, 5> Idxs;
  3055. Idxs.reserve(size);
  3056. // Add indices from the extract value instruction
  3057. Idxs.append(I->idx_begin(), I->idx_end());
  3058. // Add requested indices
  3059. Idxs.append(idx_range.begin(), idx_range.end());
  3060. assert(Idxs.size() == size
  3061. && "Number of indices added not correct?");
  3062. return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
  3063. }
  3064. // Otherwise, we don't know (such as, extracting from a function return value
  3065. // or load instruction)
  3066. return nullptr;
  3067. }
  3068. bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
  3069. unsigned CharSize) {
  3070. // Make sure the GEP has exactly three arguments.
  3071. if (GEP->getNumOperands() != 3)
  3072. return false;
  3073. // Make sure the index-ee is a pointer to array of \p CharSize integers.
  3074. // CharSize.
  3075. ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
  3076. if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
  3077. return false;
  3078. // Check to make sure that the first operand of the GEP is an integer and
  3079. // has value 0 so that we are sure we're indexing into the initializer.
  3080. const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
  3081. if (!FirstIdx || !FirstIdx->isZero())
  3082. return false;
  3083. return true;
  3084. }
  3085. bool llvm::getConstantDataArrayInfo(const Value *V,
  3086. ConstantDataArraySlice &Slice,
  3087. unsigned ElementSize, uint64_t Offset) {
  3088. assert(V);
  3089. // Look through bitcast instructions and geps.
  3090. V = V->stripPointerCasts();
  3091. // If the value is a GEP instruction or constant expression, treat it as an
  3092. // offset.
  3093. if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
  3094. // The GEP operator should be based on a pointer to string constant, and is
  3095. // indexing into the string constant.
  3096. if (!isGEPBasedOnPointerToString(GEP, ElementSize))
  3097. return false;
  3098. // If the second index isn't a ConstantInt, then this is a variable index
  3099. // into the array. If this occurs, we can't say anything meaningful about
  3100. // the string.
  3101. uint64_t StartIdx = 0;
  3102. if (const ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(2)))
  3103. StartIdx = CI->getZExtValue();
  3104. else
  3105. return false;
  3106. return getConstantDataArrayInfo(GEP->getOperand(0), Slice, ElementSize,
  3107. StartIdx + Offset);
  3108. }
  3109. // The GEP instruction, constant or instruction, must reference a global
  3110. // variable that is a constant and is initialized. The referenced constant
  3111. // initializer is the array that we'll use for optimization.
  3112. const GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
  3113. if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
  3114. return false;
  3115. const ConstantDataArray *Array;
  3116. ArrayType *ArrayTy;
  3117. if (GV->getInitializer()->isNullValue()) {
  3118. Type *GVTy = GV->getValueType();
  3119. if ( (ArrayTy = dyn_cast<ArrayType>(GVTy)) ) {
  3120. // A zeroinitializer for the array; there is no ConstantDataArray.
  3121. Array = nullptr;
  3122. } else {
  3123. const DataLayout &DL = GV->getParent()->getDataLayout();
  3124. uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy);
  3125. uint64_t Length = SizeInBytes / (ElementSize / 8);
  3126. if (Length <= Offset)
  3127. return false;
  3128. Slice.Array = nullptr;
  3129. Slice.Offset = 0;
  3130. Slice.Length = Length - Offset;
  3131. return true;
  3132. }
  3133. } else {
  3134. // This must be a ConstantDataArray.
  3135. Array = dyn_cast<ConstantDataArray>(GV->getInitializer());
  3136. if (!Array)
  3137. return false;
  3138. ArrayTy = Array->getType();
  3139. }
  3140. if (!ArrayTy->getElementType()->isIntegerTy(ElementSize))
  3141. return false;
  3142. uint64_t NumElts = ArrayTy->getArrayNumElements();
  3143. if (Offset > NumElts)
  3144. return false;
  3145. Slice.Array = Array;
  3146. Slice.Offset = Offset;
  3147. Slice.Length = NumElts - Offset;
  3148. return true;
  3149. }
  3150. /// This function computes the length of a null-terminated C string pointed to
  3151. /// by V. If successful, it returns true and returns the string in Str.
  3152. /// If unsuccessful, it returns false.
  3153. bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
  3154. uint64_t Offset, bool TrimAtNul) {
  3155. ConstantDataArraySlice Slice;
  3156. if (!getConstantDataArrayInfo(V, Slice, 8, Offset))
  3157. return false;
  3158. if (Slice.Array == nullptr) {
  3159. if (TrimAtNul) {
  3160. Str = StringRef();
  3161. return true;
  3162. }
  3163. if (Slice.Length == 1) {
  3164. Str = StringRef("", 1);
  3165. return true;
  3166. }
  3167. // We cannot instantiate a StringRef as we do not have an appropriate string
  3168. // of 0s at hand.
  3169. return false;
  3170. }
  3171. // Start out with the entire array in the StringRef.
  3172. Str = Slice.Array->getAsString();
  3173. // Skip over 'offset' bytes.
  3174. Str = Str.substr(Slice.Offset);
  3175. if (TrimAtNul) {
  3176. // Trim off the \0 and anything after it. If the array is not nul
  3177. // terminated, we just return the whole end of string. The client may know
  3178. // some other way that the string is length-bound.
  3179. Str = Str.substr(0, Str.find('\0'));
  3180. }
  3181. return true;
  3182. }
  3183. // These next two are very similar to the above, but also look through PHI
  3184. // nodes.
  3185. // TODO: See if we can integrate these two together.
  3186. /// If we can compute the length of the string pointed to by
  3187. /// the specified pointer, return 'len+1'. If we can't, return 0.
  3188. static uint64_t GetStringLengthH(const Value *V,
  3189. SmallPtrSetImpl<const PHINode*> &PHIs,
  3190. unsigned CharSize) {
  3191. // Look through noop bitcast instructions.
  3192. V = V->stripPointerCasts();
  3193. // If this is a PHI node, there are two cases: either we have already seen it
  3194. // or we haven't.
  3195. if (const PHINode *PN = dyn_cast<PHINode>(V)) {
  3196. if (!PHIs.insert(PN).second)
  3197. return ~0ULL; // already in the set.
  3198. // If it was new, see if all the input strings are the same length.
  3199. uint64_t LenSoFar = ~0ULL;
  3200. for (Value *IncValue : PN->incoming_values()) {
  3201. uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
  3202. if (Len == 0) return 0; // Unknown length -> unknown.
  3203. if (Len == ~0ULL) continue;
  3204. if (Len != LenSoFar && LenSoFar != ~0ULL)
  3205. return 0; // Disagree -> unknown.
  3206. LenSoFar = Len;
  3207. }
  3208. // Success, all agree.
  3209. return LenSoFar;
  3210. }
  3211. // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
  3212. if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
  3213. uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
  3214. if (Len1 == 0) return 0;
  3215. uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
  3216. if (Len2 == 0) return 0;
  3217. if (Len1 == ~0ULL) return Len2;
  3218. if (Len2 == ~0ULL) return Len1;
  3219. if (Len1 != Len2) return 0;
  3220. return Len1;
  3221. }
  3222. // Otherwise, see if we can read the string.
  3223. ConstantDataArraySlice Slice;
  3224. if (!getConstantDataArrayInfo(V, Slice, CharSize))
  3225. return 0;
  3226. if (Slice.Array == nullptr)
  3227. return 1;
  3228. // Search for nul characters
  3229. unsigned NullIndex = 0;
  3230. for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
  3231. if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
  3232. break;
  3233. }
  3234. return NullIndex + 1;
  3235. }
  3236. /// If we can compute the length of the string pointed to by
  3237. /// the specified pointer, return 'len+1'. If we can't, return 0.
  3238. uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
  3239. if (!V->getType()->isPointerTy())
  3240. return 0;
  3241. SmallPtrSet<const PHINode*, 32> PHIs;
  3242. uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
  3243. // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
  3244. // an empty string as a length.
  3245. return Len == ~0ULL ? 1 : Len;
  3246. }
  3247. const Value *
  3248. llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call,
  3249. bool MustPreserveNullness) {
  3250. assert(Call &&
  3251. "getArgumentAliasingToReturnedPointer only works on nonnull calls");
  3252. if (const Value *RV = Call->getReturnedArgOperand())
  3253. return RV;
  3254. // This can be used only as a aliasing property.
  3255. if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
  3256. Call, MustPreserveNullness))
  3257. return Call->getArgOperand(0);
  3258. return nullptr;
  3259. }
  3260. bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
  3261. const CallBase *Call, bool MustPreserveNullness) {
  3262. return Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
  3263. Call->getIntrinsicID() == Intrinsic::strip_invariant_group ||
  3264. Call->getIntrinsicID() == Intrinsic::aarch64_irg ||
  3265. Call->getIntrinsicID() == Intrinsic::aarch64_tagp ||
  3266. (!MustPreserveNullness &&
  3267. Call->getIntrinsicID() == Intrinsic::ptrmask);
  3268. }
  3269. /// \p PN defines a loop-variant pointer to an object. Check if the
  3270. /// previous iteration of the loop was referring to the same object as \p PN.
  3271. static bool isSameUnderlyingObjectInLoop(const PHINode *PN,
  3272. const LoopInfo *LI) {
  3273. // Find the loop-defined value.
  3274. Loop *L = LI->getLoopFor(PN->getParent());
  3275. if (PN->getNumIncomingValues() != 2)
  3276. return true;
  3277. // Find the value from previous iteration.
  3278. auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
  3279. if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
  3280. PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
  3281. if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
  3282. return true;
  3283. // If a new pointer is loaded in the loop, the pointer references a different
  3284. // object in every iteration. E.g.:
  3285. // for (i)
  3286. // int *p = a[i];
  3287. // ...
  3288. if (auto *Load = dyn_cast<LoadInst>(PrevValue))
  3289. if (!L->isLoopInvariant(Load->getPointerOperand()))
  3290. return false;
  3291. return true;
  3292. }
  3293. Value *llvm::GetUnderlyingObject(Value *V, const DataLayout &DL,
  3294. unsigned MaxLookup) {
  3295. if (!V->getType()->isPointerTy())
  3296. return V;
  3297. for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
  3298. if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
  3299. V = GEP->getPointerOperand();
  3300. } else if (Operator::getOpcode(V) == Instruction::BitCast ||
  3301. Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
  3302. V = cast<Operator>(V)->getOperand(0);
  3303. } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
  3304. if (GA->isInterposable())
  3305. return V;
  3306. V = GA->getAliasee();
  3307. } else if (isa<AllocaInst>(V)) {
  3308. // An alloca can't be further simplified.
  3309. return V;
  3310. } else {
  3311. if (auto *Call = dyn_cast<CallBase>(V)) {
  3312. // CaptureTracking can know about special capturing properties of some
  3313. // intrinsics like launder.invariant.group, that can't be expressed with
  3314. // the attributes, but have properties like returning aliasing pointer.
  3315. // Because some analysis may assume that nocaptured pointer is not
  3316. // returned from some special intrinsic (because function would have to
  3317. // be marked with returns attribute), it is crucial to use this function
  3318. // because it should be in sync with CaptureTracking. Not using it may
  3319. // cause weird miscompilations where 2 aliasing pointers are assumed to
  3320. // noalias.
  3321. if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
  3322. V = RP;
  3323. continue;
  3324. }
  3325. }
  3326. // See if InstructionSimplify knows any relevant tricks.
  3327. if (Instruction *I = dyn_cast<Instruction>(V))
  3328. // TODO: Acquire a DominatorTree and AssumptionCache and use them.
  3329. if (Value *Simplified = SimplifyInstruction(I, {DL, I})) {
  3330. V = Simplified;
  3331. continue;
  3332. }
  3333. return V;
  3334. }
  3335. assert(V->getType()->isPointerTy() && "Unexpected operand type!");
  3336. }
  3337. return V;
  3338. }
  3339. void llvm::GetUnderlyingObjects(const Value *V,
  3340. SmallVectorImpl<const Value *> &Objects,
  3341. const DataLayout &DL, LoopInfo *LI,
  3342. unsigned MaxLookup) {
  3343. SmallPtrSet<const Value *, 4> Visited;
  3344. SmallVector<const Value *, 4> Worklist;
  3345. Worklist.push_back(V);
  3346. do {
  3347. const Value *P = Worklist.pop_back_val();
  3348. P = GetUnderlyingObject(P, DL, MaxLookup);
  3349. if (!Visited.insert(P).second)
  3350. continue;
  3351. if (auto *SI = dyn_cast<SelectInst>(P)) {
  3352. Worklist.push_back(SI->getTrueValue());
  3353. Worklist.push_back(SI->getFalseValue());
  3354. continue;
  3355. }
  3356. if (auto *PN = dyn_cast<PHINode>(P)) {
  3357. // If this PHI changes the underlying object in every iteration of the
  3358. // loop, don't look through it. Consider:
  3359. // int **A;
  3360. // for (i) {
  3361. // Prev = Curr; // Prev = PHI (Prev_0, Curr)
  3362. // Curr = A[i];
  3363. // *Prev, *Curr;
  3364. //
  3365. // Prev is tracking Curr one iteration behind so they refer to different
  3366. // underlying objects.
  3367. if (!LI || !LI->isLoopHeader(PN->getParent()) ||
  3368. isSameUnderlyingObjectInLoop(PN, LI))
  3369. for (Value *IncValue : PN->incoming_values())
  3370. Worklist.push_back(IncValue);
  3371. continue;
  3372. }
  3373. Objects.push_back(P);
  3374. } while (!Worklist.empty());
  3375. }
  3376. /// This is the function that does the work of looking through basic
  3377. /// ptrtoint+arithmetic+inttoptr sequences.
  3378. static const Value *getUnderlyingObjectFromInt(const Value *V) {
  3379. do {
  3380. if (const Operator *U = dyn_cast<Operator>(V)) {
  3381. // If we find a ptrtoint, we can transfer control back to the
  3382. // regular getUnderlyingObjectFromInt.
  3383. if (U->getOpcode() == Instruction::PtrToInt)
  3384. return U->getOperand(0);
  3385. // If we find an add of a constant, a multiplied value, or a phi, it's
  3386. // likely that the other operand will lead us to the base
  3387. // object. We don't have to worry about the case where the
  3388. // object address is somehow being computed by the multiply,
  3389. // because our callers only care when the result is an
  3390. // identifiable object.
  3391. if (U->getOpcode() != Instruction::Add ||
  3392. (!isa<ConstantInt>(U->getOperand(1)) &&
  3393. Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
  3394. !isa<PHINode>(U->getOperand(1))))
  3395. return V;
  3396. V = U->getOperand(0);
  3397. } else {
  3398. return V;
  3399. }
  3400. assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
  3401. } while (true);
  3402. }
  3403. /// This is a wrapper around GetUnderlyingObjects and adds support for basic
  3404. /// ptrtoint+arithmetic+inttoptr sequences.
  3405. /// It returns false if unidentified object is found in GetUnderlyingObjects.
  3406. bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,
  3407. SmallVectorImpl<Value *> &Objects,
  3408. const DataLayout &DL) {
  3409. SmallPtrSet<const Value *, 16> Visited;
  3410. SmallVector<const Value *, 4> Working(1, V);
  3411. do {
  3412. V = Working.pop_back_val();
  3413. SmallVector<const Value *, 4> Objs;
  3414. GetUnderlyingObjects(V, Objs, DL);
  3415. for (const Value *V : Objs) {
  3416. if (!Visited.insert(V).second)
  3417. continue;
  3418. if (Operator::getOpcode(V) == Instruction::IntToPtr) {
  3419. const Value *O =
  3420. getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
  3421. if (O->getType()->isPointerTy()) {
  3422. Working.push_back(O);
  3423. continue;
  3424. }
  3425. }
  3426. // If GetUnderlyingObjects fails to find an identifiable object,
  3427. // getUnderlyingObjectsForCodeGen also fails for safety.
  3428. if (!isIdentifiedObject(V)) {
  3429. Objects.clear();
  3430. return false;
  3431. }
  3432. Objects.push_back(const_cast<Value *>(V));
  3433. }
  3434. } while (!Working.empty());
  3435. return true;
  3436. }
  3437. /// Return true if the only users of this pointer are lifetime markers.
  3438. bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
  3439. for (const User *U : V->users()) {
  3440. const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
  3441. if (!II) return false;
  3442. if (!II->isLifetimeStartOrEnd())
  3443. return false;
  3444. }
  3445. return true;
  3446. }
  3447. bool llvm::mustSuppressSpeculation(const LoadInst &LI) {
  3448. if (!LI.isUnordered())
  3449. return true;
  3450. const Function &F = *LI.getFunction();
  3451. // Speculative load may create a race that did not exist in the source.
  3452. return F.hasFnAttribute(Attribute::SanitizeThread) ||
  3453. // Speculative load may load data from dirty regions.
  3454. F.hasFnAttribute(Attribute::SanitizeAddress) ||
  3455. F.hasFnAttribute(Attribute::SanitizeHWAddress);
  3456. }
  3457. bool llvm::isSafeToSpeculativelyExecute(const Value *V,
  3458. const Instruction *CtxI,
  3459. const DominatorTree *DT) {
  3460. const Operator *Inst = dyn_cast<Operator>(V);
  3461. if (!Inst)
  3462. return false;
  3463. for (unsigned i = 0, e = Inst->getNumOperands(); i != e; ++i)
  3464. if (Constant *C = dyn_cast<Constant>(Inst->getOperand(i)))
  3465. if (C->canTrap())
  3466. return false;
  3467. switch (Inst->getOpcode()) {
  3468. default:
  3469. return true;
  3470. case Instruction::UDiv:
  3471. case Instruction::URem: {
  3472. // x / y is undefined if y == 0.
  3473. const APInt *V;
  3474. if (match(Inst->getOperand(1), m_APInt(V)))
  3475. return *V != 0;
  3476. return false;
  3477. }
  3478. case Instruction::SDiv:
  3479. case Instruction::SRem: {
  3480. // x / y is undefined if y == 0 or x == INT_MIN and y == -1
  3481. const APInt *Numerator, *Denominator;
  3482. if (!match(Inst->getOperand(1), m_APInt(Denominator)))
  3483. return false;
  3484. // We cannot hoist this division if the denominator is 0.
  3485. if (*Denominator == 0)
  3486. return false;
  3487. // It's safe to hoist if the denominator is not 0 or -1.
  3488. if (*Denominator != -1)
  3489. return true;
  3490. // At this point we know that the denominator is -1. It is safe to hoist as
  3491. // long we know that the numerator is not INT_MIN.
  3492. if (match(Inst->getOperand(0), m_APInt(Numerator)))
  3493. return !Numerator->isMinSignedValue();
  3494. // The numerator *might* be MinSignedValue.
  3495. return false;
  3496. }
  3497. case Instruction::Load: {
  3498. const LoadInst *LI = cast<LoadInst>(Inst);
  3499. if (mustSuppressSpeculation(*LI))
  3500. return false;
  3501. const DataLayout &DL = LI->getModule()->getDataLayout();
  3502. return isDereferenceableAndAlignedPointer(LI->getPointerOperand(),
  3503. LI->getType(), LI->getAlignment(),
  3504. DL, CtxI, DT);
  3505. }
  3506. case Instruction::Call: {
  3507. auto *CI = cast<const CallInst>(Inst);
  3508. const Function *Callee = CI->getCalledFunction();
  3509. // The called function could have undefined behavior or side-effects, even
  3510. // if marked readnone nounwind.
  3511. return Callee && Callee->isSpeculatable();
  3512. }
  3513. case Instruction::VAArg:
  3514. case Instruction::Alloca:
  3515. case Instruction::Invoke:
  3516. case Instruction::CallBr:
  3517. case Instruction::PHI:
  3518. case Instruction::Store:
  3519. case Instruction::Ret:
  3520. case Instruction::Br:
  3521. case Instruction::IndirectBr:
  3522. case Instruction::Switch:
  3523. case Instruction::Unreachable:
  3524. case Instruction::Fence:
  3525. case Instruction::AtomicRMW:
  3526. case Instruction::AtomicCmpXchg:
  3527. case Instruction::LandingPad:
  3528. case Instruction::Resume:
  3529. case Instruction::CatchSwitch:
  3530. case Instruction::CatchPad:
  3531. case Instruction::CatchRet:
  3532. case Instruction::CleanupPad:
  3533. case Instruction::CleanupRet:
  3534. return false; // Misc instructions which have effects
  3535. }
  3536. }
  3537. bool llvm::mayBeMemoryDependent(const Instruction &I) {
  3538. return I.mayReadOrWriteMemory() || !isSafeToSpeculativelyExecute(&I);
  3539. }
  3540. /// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
  3541. static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
  3542. switch (OR) {
  3543. case ConstantRange::OverflowResult::MayOverflow:
  3544. return OverflowResult::MayOverflow;
  3545. case ConstantRange::OverflowResult::AlwaysOverflowsLow:
  3546. return OverflowResult::AlwaysOverflowsLow;
  3547. case ConstantRange::OverflowResult::AlwaysOverflowsHigh:
  3548. return OverflowResult::AlwaysOverflowsHigh;
  3549. case ConstantRange::OverflowResult::NeverOverflows:
  3550. return OverflowResult::NeverOverflows;
  3551. }
  3552. llvm_unreachable("Unknown OverflowResult");
  3553. }
  3554. /// Combine constant ranges from computeConstantRange() and computeKnownBits().
  3555. static ConstantRange computeConstantRangeIncludingKnownBits(
  3556. const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth,
  3557. AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
  3558. OptimizationRemarkEmitter *ORE = nullptr, bool UseInstrInfo = true) {
  3559. KnownBits Known = computeKnownBits(
  3560. V, DL, Depth, AC, CxtI, DT, ORE, UseInstrInfo);
  3561. ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
  3562. ConstantRange CR2 = computeConstantRange(V, UseInstrInfo);
  3563. ConstantRange::PreferredRangeType RangeType =
  3564. ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;
  3565. return CR1.intersectWith(CR2, RangeType);
  3566. }
  3567. OverflowResult llvm::computeOverflowForUnsignedMul(
  3568. const Value *LHS, const Value *RHS, const DataLayout &DL,
  3569. AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
  3570. bool UseInstrInfo) {
  3571. KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
  3572. nullptr, UseInstrInfo);
  3573. KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
  3574. nullptr, UseInstrInfo);
  3575. ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
  3576. ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
  3577. return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
  3578. }
  3579. OverflowResult
  3580. llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
  3581. const DataLayout &DL, AssumptionCache *AC,
  3582. const Instruction *CxtI,
  3583. const DominatorTree *DT, bool UseInstrInfo) {
  3584. // Multiplying n * m significant bits yields a result of n + m significant
  3585. // bits. If the total number of significant bits does not exceed the
  3586. // result bit width (minus 1), there is no overflow.
  3587. // This means if we have enough leading sign bits in the operands
  3588. // we can guarantee that the result does not overflow.
  3589. // Ref: "Hacker's Delight" by Henry Warren
  3590. unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
  3591. // Note that underestimating the number of sign bits gives a more
  3592. // conservative answer.
  3593. unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) +
  3594. ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT);
  3595. // First handle the easy case: if we have enough sign bits there's
  3596. // definitely no overflow.
  3597. if (SignBits > BitWidth + 1)
  3598. return OverflowResult::NeverOverflows;
  3599. // There are two ambiguous cases where there can be no overflow:
  3600. // SignBits == BitWidth + 1 and
  3601. // SignBits == BitWidth
  3602. // The second case is difficult to check, therefore we only handle the
  3603. // first case.
  3604. if (SignBits == BitWidth + 1) {
  3605. // It overflows only when both arguments are negative and the true
  3606. // product is exactly the minimum negative number.
  3607. // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
  3608. // For simplicity we just check if at least one side is not negative.
  3609. KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
  3610. nullptr, UseInstrInfo);
  3611. KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
  3612. nullptr, UseInstrInfo);
  3613. if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
  3614. return OverflowResult::NeverOverflows;
  3615. }
  3616. return OverflowResult::MayOverflow;
  3617. }
  3618. OverflowResult llvm::computeOverflowForUnsignedAdd(
  3619. const Value *LHS, const Value *RHS, const DataLayout &DL,
  3620. AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
  3621. bool UseInstrInfo) {
  3622. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  3623. LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT,
  3624. nullptr, UseInstrInfo);
  3625. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  3626. RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT,
  3627. nullptr, UseInstrInfo);
  3628. return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
  3629. }
  3630. static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
  3631. const Value *RHS,
  3632. const AddOperator *Add,
  3633. const DataLayout &DL,
  3634. AssumptionCache *AC,
  3635. const Instruction *CxtI,
  3636. const DominatorTree *DT) {
  3637. if (Add && Add->hasNoSignedWrap()) {
  3638. return OverflowResult::NeverOverflows;
  3639. }
  3640. // If LHS and RHS each have at least two sign bits, the addition will look
  3641. // like
  3642. //
  3643. // XX..... +
  3644. // YY.....
  3645. //
  3646. // If the carry into the most significant position is 0, X and Y can't both
  3647. // be 1 and therefore the carry out of the addition is also 0.
  3648. //
  3649. // If the carry into the most significant position is 1, X and Y can't both
  3650. // be 0 and therefore the carry out of the addition is also 1.
  3651. //
  3652. // Since the carry into the most significant position is always equal to
  3653. // the carry out of the addition, there is no signed overflow.
  3654. if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
  3655. ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
  3656. return OverflowResult::NeverOverflows;
  3657. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  3658. LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  3659. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  3660. RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  3661. OverflowResult OR =
  3662. mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
  3663. if (OR != OverflowResult::MayOverflow)
  3664. return OR;
  3665. // The remaining code needs Add to be available. Early returns if not so.
  3666. if (!Add)
  3667. return OverflowResult::MayOverflow;
  3668. // If the sign of Add is the same as at least one of the operands, this add
  3669. // CANNOT overflow. If this can be determined from the known bits of the
  3670. // operands the above signedAddMayOverflow() check will have already done so.
  3671. // The only other way to improve on the known bits is from an assumption, so
  3672. // call computeKnownBitsFromAssume() directly.
  3673. bool LHSOrRHSKnownNonNegative =
  3674. (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
  3675. bool LHSOrRHSKnownNegative =
  3676. (LHSRange.isAllNegative() || RHSRange.isAllNegative());
  3677. if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
  3678. KnownBits AddKnown(LHSRange.getBitWidth());
  3679. computeKnownBitsFromAssume(
  3680. Add, AddKnown, /*Depth=*/0, Query(DL, AC, CxtI, DT, true));
  3681. if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
  3682. (AddKnown.isNegative() && LHSOrRHSKnownNegative))
  3683. return OverflowResult::NeverOverflows;
  3684. }
  3685. return OverflowResult::MayOverflow;
  3686. }
  3687. OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
  3688. const Value *RHS,
  3689. const DataLayout &DL,
  3690. AssumptionCache *AC,
  3691. const Instruction *CxtI,
  3692. const DominatorTree *DT) {
  3693. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  3694. LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
  3695. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  3696. RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
  3697. return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
  3698. }
  3699. OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
  3700. const Value *RHS,
  3701. const DataLayout &DL,
  3702. AssumptionCache *AC,
  3703. const Instruction *CxtI,
  3704. const DominatorTree *DT) {
  3705. // If LHS and RHS each have at least two sign bits, the subtraction
  3706. // cannot overflow.
  3707. if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
  3708. ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
  3709. return OverflowResult::NeverOverflows;
  3710. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  3711. LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  3712. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  3713. RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  3714. return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
  3715. }
  3716. bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
  3717. const DominatorTree &DT) {
  3718. SmallVector<const BranchInst *, 2> GuardingBranches;
  3719. SmallVector<const ExtractValueInst *, 2> Results;
  3720. for (const User *U : WO->users()) {
  3721. if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
  3722. assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
  3723. if (EVI->getIndices()[0] == 0)
  3724. Results.push_back(EVI);
  3725. else {
  3726. assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
  3727. for (const auto *U : EVI->users())
  3728. if (const auto *B = dyn_cast<BranchInst>(U)) {
  3729. assert(B->isConditional() && "How else is it using an i1?");
  3730. GuardingBranches.push_back(B);
  3731. }
  3732. }
  3733. } else {
  3734. // We are using the aggregate directly in a way we don't want to analyze
  3735. // here (storing it to a global, say).
  3736. return false;
  3737. }
  3738. }
  3739. auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
  3740. BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
  3741. if (!NoWrapEdge.isSingleEdge())
  3742. return false;
  3743. // Check if all users of the add are provably no-wrap.
  3744. for (const auto *Result : Results) {
  3745. // If the extractvalue itself is not executed on overflow, the we don't
  3746. // need to check each use separately, since domination is transitive.
  3747. if (DT.dominates(NoWrapEdge, Result->getParent()))
  3748. continue;
  3749. for (auto &RU : Result->uses())
  3750. if (!DT.dominates(NoWrapEdge, RU))
  3751. return false;
  3752. }
  3753. return true;
  3754. };
  3755. return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
  3756. }
  3757. OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
  3758. const DataLayout &DL,
  3759. AssumptionCache *AC,
  3760. const Instruction *CxtI,
  3761. const DominatorTree *DT) {
  3762. return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
  3763. Add, DL, AC, CxtI, DT);
  3764. }
  3765. OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS,
  3766. const Value *RHS,
  3767. const DataLayout &DL,
  3768. AssumptionCache *AC,
  3769. const Instruction *CxtI,
  3770. const DominatorTree *DT) {
  3771. return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT);
  3772. }
  3773. bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
  3774. // Note: An atomic operation isn't guaranteed to return in a reasonable amount
  3775. // of time because it's possible for another thread to interfere with it for an
  3776. // arbitrary length of time, but programs aren't allowed to rely on that.
  3777. // If there is no successor, then execution can't transfer to it.
  3778. if (const auto *CRI = dyn_cast<CleanupReturnInst>(I))
  3779. return !CRI->unwindsToCaller();
  3780. if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(I))
  3781. return !CatchSwitch->unwindsToCaller();
  3782. if (isa<ResumeInst>(I))
  3783. return false;
  3784. if (isa<ReturnInst>(I))
  3785. return false;
  3786. if (isa<UnreachableInst>(I))
  3787. return false;
  3788. // Calls can throw, or contain an infinite loop, or kill the process.
  3789. if (auto CS = ImmutableCallSite(I)) {
  3790. // Call sites that throw have implicit non-local control flow.
  3791. if (!CS.doesNotThrow())
  3792. return false;
  3793. // A function which doens't throw and has "willreturn" attribute will
  3794. // always return.
  3795. if (CS.hasFnAttr(Attribute::WillReturn))
  3796. return true;
  3797. // Non-throwing call sites can loop infinitely, call exit/pthread_exit
  3798. // etc. and thus not return. However, LLVM already assumes that
  3799. //
  3800. // - Thread exiting actions are modeled as writes to memory invisible to
  3801. // the program.
  3802. //
  3803. // - Loops that don't have side effects (side effects are volatile/atomic
  3804. // stores and IO) always terminate (see http://llvm.org/PR965).
  3805. // Furthermore IO itself is also modeled as writes to memory invisible to
  3806. // the program.
  3807. //
  3808. // We rely on those assumptions here, and use the memory effects of the call
  3809. // target as a proxy for checking that it always returns.
  3810. // FIXME: This isn't aggressive enough; a call which only writes to a global
  3811. // is guaranteed to return.
  3812. return CS.onlyReadsMemory() || CS.onlyAccessesArgMemory();
  3813. }
  3814. // Other instructions return normally.
  3815. return true;
  3816. }
  3817. bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) {
  3818. // TODO: This is slightly conservative for invoke instruction since exiting
  3819. // via an exception *is* normal control for them.
  3820. for (auto I = BB->begin(), E = BB->end(); I != E; ++I)
  3821. if (!isGuaranteedToTransferExecutionToSuccessor(&*I))
  3822. return false;
  3823. return true;
  3824. }
  3825. bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
  3826. const Loop *L) {
  3827. // The loop header is guaranteed to be executed for every iteration.
  3828. //
  3829. // FIXME: Relax this constraint to cover all basic blocks that are
  3830. // guaranteed to be executed at every iteration.
  3831. if (I->getParent() != L->getHeader()) return false;
  3832. for (const Instruction &LI : *L->getHeader()) {
  3833. if (&LI == I) return true;
  3834. if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
  3835. }
  3836. llvm_unreachable("Instruction not contained in its own parent basic block.");
  3837. }
  3838. bool llvm::propagatesFullPoison(const Instruction *I) {
  3839. // TODO: This should include all instructions apart from phis, selects and
  3840. // call-like instructions.
  3841. switch (I->getOpcode()) {
  3842. case Instruction::Add:
  3843. case Instruction::Sub:
  3844. case Instruction::Xor:
  3845. case Instruction::Trunc:
  3846. case Instruction::BitCast:
  3847. case Instruction::AddrSpaceCast:
  3848. case Instruction::Mul:
  3849. case Instruction::Shl:
  3850. case Instruction::GetElementPtr:
  3851. // These operations all propagate poison unconditionally. Note that poison
  3852. // is not any particular value, so xor or subtraction of poison with
  3853. // itself still yields poison, not zero.
  3854. return true;
  3855. case Instruction::AShr:
  3856. case Instruction::SExt:
  3857. // For these operations, one bit of the input is replicated across
  3858. // multiple output bits. A replicated poison bit is still poison.
  3859. return true;
  3860. case Instruction::ICmp:
  3861. // Comparing poison with any value yields poison. This is why, for
  3862. // instance, x s< (x +nsw 1) can be folded to true.
  3863. return true;
  3864. default:
  3865. return false;
  3866. }
  3867. }
  3868. const Value *llvm::getGuaranteedNonFullPoisonOp(const Instruction *I) {
  3869. switch (I->getOpcode()) {
  3870. case Instruction::Store:
  3871. return cast<StoreInst>(I)->getPointerOperand();
  3872. case Instruction::Load:
  3873. return cast<LoadInst>(I)->getPointerOperand();
  3874. case Instruction::AtomicCmpXchg:
  3875. return cast<AtomicCmpXchgInst>(I)->getPointerOperand();
  3876. case Instruction::AtomicRMW:
  3877. return cast<AtomicRMWInst>(I)->getPointerOperand();
  3878. case Instruction::UDiv:
  3879. case Instruction::SDiv:
  3880. case Instruction::URem:
  3881. case Instruction::SRem:
  3882. return I->getOperand(1);
  3883. default:
  3884. // Note: It's really tempting to think that a conditional branch or
  3885. // switch should be listed here, but that's incorrect. It's not
  3886. // branching off of poison which is UB, it is executing a side effecting
  3887. // instruction which follows the branch.
  3888. return nullptr;
  3889. }
  3890. }
  3891. bool llvm::mustTriggerUB(const Instruction *I,
  3892. const SmallSet<const Value *, 16>& KnownPoison) {
  3893. auto *NotPoison = getGuaranteedNonFullPoisonOp(I);
  3894. return (NotPoison && KnownPoison.count(NotPoison));
  3895. }
  3896. bool llvm::programUndefinedIfFullPoison(const Instruction *PoisonI) {
  3897. // We currently only look for uses of poison values within the same basic
  3898. // block, as that makes it easier to guarantee that the uses will be
  3899. // executed given that PoisonI is executed.
  3900. //
  3901. // FIXME: Expand this to consider uses beyond the same basic block. To do
  3902. // this, look out for the distinction between post-dominance and strong
  3903. // post-dominance.
  3904. const BasicBlock *BB = PoisonI->getParent();
  3905. // Set of instructions that we have proved will yield poison if PoisonI
  3906. // does.
  3907. SmallSet<const Value *, 16> YieldsPoison;
  3908. SmallSet<const BasicBlock *, 4> Visited;
  3909. YieldsPoison.insert(PoisonI);
  3910. Visited.insert(PoisonI->getParent());
  3911. BasicBlock::const_iterator Begin = PoisonI->getIterator(), End = BB->end();
  3912. unsigned Iter = 0;
  3913. while (Iter++ < MaxDepth) {
  3914. for (auto &I : make_range(Begin, End)) {
  3915. if (&I != PoisonI) {
  3916. if (mustTriggerUB(&I, YieldsPoison))
  3917. return true;
  3918. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  3919. return false;
  3920. }
  3921. // Mark poison that propagates from I through uses of I.
  3922. if (YieldsPoison.count(&I)) {
  3923. for (const User *User : I.users()) {
  3924. const Instruction *UserI = cast<Instruction>(User);
  3925. if (propagatesFullPoison(UserI))
  3926. YieldsPoison.insert(User);
  3927. }
  3928. }
  3929. }
  3930. if (auto *NextBB = BB->getSingleSuccessor()) {
  3931. if (Visited.insert(NextBB).second) {
  3932. BB = NextBB;
  3933. Begin = BB->getFirstNonPHI()->getIterator();
  3934. End = BB->end();
  3935. continue;
  3936. }
  3937. }
  3938. break;
  3939. }
  3940. return false;
  3941. }
  3942. static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
  3943. if (FMF.noNaNs())
  3944. return true;
  3945. if (auto *C = dyn_cast<ConstantFP>(V))
  3946. return !C->isNaN();
  3947. if (auto *C = dyn_cast<ConstantDataVector>(V)) {
  3948. if (!C->getElementType()->isFloatingPointTy())
  3949. return false;
  3950. for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
  3951. if (C->getElementAsAPFloat(I).isNaN())
  3952. return false;
  3953. }
  3954. return true;
  3955. }
  3956. return false;
  3957. }
  3958. static bool isKnownNonZero(const Value *V) {
  3959. if (auto *C = dyn_cast<ConstantFP>(V))
  3960. return !C->isZero();
  3961. if (auto *C = dyn_cast<ConstantDataVector>(V)) {
  3962. if (!C->getElementType()->isFloatingPointTy())
  3963. return false;
  3964. for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
  3965. if (C->getElementAsAPFloat(I).isZero())
  3966. return false;
  3967. }
  3968. return true;
  3969. }
  3970. return false;
  3971. }
  3972. /// Match clamp pattern for float types without care about NaNs or signed zeros.
  3973. /// Given non-min/max outer cmp/select from the clamp pattern this
  3974. /// function recognizes if it can be substitued by a "canonical" min/max
  3975. /// pattern.
  3976. static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred,
  3977. Value *CmpLHS, Value *CmpRHS,
  3978. Value *TrueVal, Value *FalseVal,
  3979. Value *&LHS, Value *&RHS) {
  3980. // Try to match
  3981. // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
  3982. // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
  3983. // and return description of the outer Max/Min.
  3984. // First, check if select has inverse order:
  3985. if (CmpRHS == FalseVal) {
  3986. std::swap(TrueVal, FalseVal);
  3987. Pred = CmpInst::getInversePredicate(Pred);
  3988. }
  3989. // Assume success now. If there's no match, callers should not use these anyway.
  3990. LHS = TrueVal;
  3991. RHS = FalseVal;
  3992. const APFloat *FC1;
  3993. if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
  3994. return {SPF_UNKNOWN, SPNB_NA, false};
  3995. const APFloat *FC2;
  3996. switch (Pred) {
  3997. case CmpInst::FCMP_OLT:
  3998. case CmpInst::FCMP_OLE:
  3999. case CmpInst::FCMP_ULT:
  4000. case CmpInst::FCMP_ULE:
  4001. if (match(FalseVal,
  4002. m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)),
  4003. m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
  4004. FC1->compare(*FC2) == APFloat::cmpResult::cmpLessThan)
  4005. return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
  4006. break;
  4007. case CmpInst::FCMP_OGT:
  4008. case CmpInst::FCMP_OGE:
  4009. case CmpInst::FCMP_UGT:
  4010. case CmpInst::FCMP_UGE:
  4011. if (match(FalseVal,
  4012. m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)),
  4013. m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
  4014. FC1->compare(*FC2) == APFloat::cmpResult::cmpGreaterThan)
  4015. return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
  4016. break;
  4017. default:
  4018. break;
  4019. }
  4020. return {SPF_UNKNOWN, SPNB_NA, false};
  4021. }
  4022. /// Recognize variations of:
  4023. /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
  4024. static SelectPatternResult matchClamp(CmpInst::Predicate Pred,
  4025. Value *CmpLHS, Value *CmpRHS,
  4026. Value *TrueVal, Value *FalseVal) {
  4027. // Swap the select operands and predicate to match the patterns below.
  4028. if (CmpRHS != TrueVal) {
  4029. Pred = ICmpInst::getSwappedPredicate(Pred);
  4030. std::swap(TrueVal, FalseVal);
  4031. }
  4032. const APInt *C1;
  4033. if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
  4034. const APInt *C2;
  4035. // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
  4036. if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
  4037. C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
  4038. return {SPF_SMAX, SPNB_NA, false};
  4039. // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
  4040. if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
  4041. C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
  4042. return {SPF_SMIN, SPNB_NA, false};
  4043. // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
  4044. if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
  4045. C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
  4046. return {SPF_UMAX, SPNB_NA, false};
  4047. // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
  4048. if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
  4049. C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
  4050. return {SPF_UMIN, SPNB_NA, false};
  4051. }
  4052. return {SPF_UNKNOWN, SPNB_NA, false};
  4053. }
  4054. /// Recognize variations of:
  4055. /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
  4056. static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,
  4057. Value *CmpLHS, Value *CmpRHS,
  4058. Value *TVal, Value *FVal,
  4059. unsigned Depth) {
  4060. // TODO: Allow FP min/max with nnan/nsz.
  4061. assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
  4062. Value *A = nullptr, *B = nullptr;
  4063. SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
  4064. if (!SelectPatternResult::isMinOrMax(L.Flavor))
  4065. return {SPF_UNKNOWN, SPNB_NA, false};
  4066. Value *C = nullptr, *D = nullptr;
  4067. SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
  4068. if (L.Flavor != R.Flavor)
  4069. return {SPF_UNKNOWN, SPNB_NA, false};
  4070. // We have something like: x Pred y ? min(a, b) : min(c, d).
  4071. // Try to match the compare to the min/max operations of the select operands.
  4072. // First, make sure we have the right compare predicate.
  4073. switch (L.Flavor) {
  4074. case SPF_SMIN:
  4075. if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
  4076. Pred = ICmpInst::getSwappedPredicate(Pred);
  4077. std::swap(CmpLHS, CmpRHS);
  4078. }
  4079. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
  4080. break;
  4081. return {SPF_UNKNOWN, SPNB_NA, false};
  4082. case SPF_SMAX:
  4083. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
  4084. Pred = ICmpInst::getSwappedPredicate(Pred);
  4085. std::swap(CmpLHS, CmpRHS);
  4086. }
  4087. if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
  4088. break;
  4089. return {SPF_UNKNOWN, SPNB_NA, false};
  4090. case SPF_UMIN:
  4091. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
  4092. Pred = ICmpInst::getSwappedPredicate(Pred);
  4093. std::swap(CmpLHS, CmpRHS);
  4094. }
  4095. if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
  4096. break;
  4097. return {SPF_UNKNOWN, SPNB_NA, false};
  4098. case SPF_UMAX:
  4099. if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
  4100. Pred = ICmpInst::getSwappedPredicate(Pred);
  4101. std::swap(CmpLHS, CmpRHS);
  4102. }
  4103. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
  4104. break;
  4105. return {SPF_UNKNOWN, SPNB_NA, false};
  4106. default:
  4107. return {SPF_UNKNOWN, SPNB_NA, false};
  4108. }
  4109. // If there is a common operand in the already matched min/max and the other
  4110. // min/max operands match the compare operands (either directly or inverted),
  4111. // then this is min/max of the same flavor.
  4112. // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
  4113. // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
  4114. if (D == B) {
  4115. if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
  4116. match(A, m_Not(m_Specific(CmpRHS)))))
  4117. return {L.Flavor, SPNB_NA, false};
  4118. }
  4119. // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
  4120. // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
  4121. if (C == B) {
  4122. if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
  4123. match(A, m_Not(m_Specific(CmpRHS)))))
  4124. return {L.Flavor, SPNB_NA, false};
  4125. }
  4126. // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
  4127. // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
  4128. if (D == A) {
  4129. if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
  4130. match(B, m_Not(m_Specific(CmpRHS)))))
  4131. return {L.Flavor, SPNB_NA, false};
  4132. }
  4133. // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
  4134. // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
  4135. if (C == A) {
  4136. if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
  4137. match(B, m_Not(m_Specific(CmpRHS)))))
  4138. return {L.Flavor, SPNB_NA, false};
  4139. }
  4140. return {SPF_UNKNOWN, SPNB_NA, false};
  4141. }
  4142. /// Match non-obvious integer minimum and maximum sequences.
  4143. static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
  4144. Value *CmpLHS, Value *CmpRHS,
  4145. Value *TrueVal, Value *FalseVal,
  4146. Value *&LHS, Value *&RHS,
  4147. unsigned Depth) {
  4148. // Assume success. If there's no match, callers should not use these anyway.
  4149. LHS = TrueVal;
  4150. RHS = FalseVal;
  4151. SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
  4152. if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
  4153. return SPR;
  4154. SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
  4155. if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
  4156. return SPR;
  4157. if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
  4158. return {SPF_UNKNOWN, SPNB_NA, false};
  4159. // Z = X -nsw Y
  4160. // (X >s Y) ? 0 : Z ==> (Z >s 0) ? 0 : Z ==> SMIN(Z, 0)
  4161. // (X <s Y) ? 0 : Z ==> (Z <s 0) ? 0 : Z ==> SMAX(Z, 0)
  4162. if (match(TrueVal, m_Zero()) &&
  4163. match(FalseVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
  4164. return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
  4165. // Z = X -nsw Y
  4166. // (X >s Y) ? Z : 0 ==> (Z >s 0) ? Z : 0 ==> SMAX(Z, 0)
  4167. // (X <s Y) ? Z : 0 ==> (Z <s 0) ? Z : 0 ==> SMIN(Z, 0)
  4168. if (match(FalseVal, m_Zero()) &&
  4169. match(TrueVal, m_NSWSub(m_Specific(CmpLHS), m_Specific(CmpRHS))))
  4170. return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
  4171. const APInt *C1;
  4172. if (!match(CmpRHS, m_APInt(C1)))
  4173. return {SPF_UNKNOWN, SPNB_NA, false};
  4174. // An unsigned min/max can be written with a signed compare.
  4175. const APInt *C2;
  4176. if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
  4177. (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
  4178. // Is the sign bit set?
  4179. // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
  4180. // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
  4181. if (Pred == CmpInst::ICMP_SLT && C1->isNullValue() &&
  4182. C2->isMaxSignedValue())
  4183. return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
  4184. // Is the sign bit clear?
  4185. // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
  4186. // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
  4187. if (Pred == CmpInst::ICMP_SGT && C1->isAllOnesValue() &&
  4188. C2->isMinSignedValue())
  4189. return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
  4190. }
  4191. // Look through 'not' ops to find disguised signed min/max.
  4192. // (X >s C) ? ~X : ~C ==> (~X <s ~C) ? ~X : ~C ==> SMIN(~X, ~C)
  4193. // (X <s C) ? ~X : ~C ==> (~X >s ~C) ? ~X : ~C ==> SMAX(~X, ~C)
  4194. if (match(TrueVal, m_Not(m_Specific(CmpLHS))) &&
  4195. match(FalseVal, m_APInt(C2)) && ~(*C1) == *C2)
  4196. return {Pred == CmpInst::ICMP_SGT ? SPF_SMIN : SPF_SMAX, SPNB_NA, false};
  4197. // (X >s C) ? ~C : ~X ==> (~X <s ~C) ? ~C : ~X ==> SMAX(~C, ~X)
  4198. // (X <s C) ? ~C : ~X ==> (~X >s ~C) ? ~C : ~X ==> SMIN(~C, ~X)
  4199. if (match(FalseVal, m_Not(m_Specific(CmpLHS))) &&
  4200. match(TrueVal, m_APInt(C2)) && ~(*C1) == *C2)
  4201. return {Pred == CmpInst::ICMP_SGT ? SPF_SMAX : SPF_SMIN, SPNB_NA, false};
  4202. return {SPF_UNKNOWN, SPNB_NA, false};
  4203. }
  4204. bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) {
  4205. assert(X && Y && "Invalid operand");
  4206. // X = sub (0, Y) || X = sub nsw (0, Y)
  4207. if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) ||
  4208. (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y)))))
  4209. return true;
  4210. // Y = sub (0, X) || Y = sub nsw (0, X)
  4211. if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) ||
  4212. (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X)))))
  4213. return true;
  4214. // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
  4215. Value *A, *B;
  4216. return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
  4217. match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
  4218. (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
  4219. match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
  4220. }
  4221. static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
  4222. FastMathFlags FMF,
  4223. Value *CmpLHS, Value *CmpRHS,
  4224. Value *TrueVal, Value *FalseVal,
  4225. Value *&LHS, Value *&RHS,
  4226. unsigned Depth) {
  4227. if (CmpInst::isFPPredicate(Pred)) {
  4228. // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
  4229. // 0.0 operand, set the compare's 0.0 operands to that same value for the
  4230. // purpose of identifying min/max. Disregard vector constants with undefined
  4231. // elements because those can not be back-propagated for analysis.
  4232. Value *OutputZeroVal = nullptr;
  4233. if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
  4234. !cast<Constant>(TrueVal)->containsUndefElement())
  4235. OutputZeroVal = TrueVal;
  4236. else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
  4237. !cast<Constant>(FalseVal)->containsUndefElement())
  4238. OutputZeroVal = FalseVal;
  4239. if (OutputZeroVal) {
  4240. if (match(CmpLHS, m_AnyZeroFP()))
  4241. CmpLHS = OutputZeroVal;
  4242. if (match(CmpRHS, m_AnyZeroFP()))
  4243. CmpRHS = OutputZeroVal;
  4244. }
  4245. }
  4246. LHS = CmpLHS;
  4247. RHS = CmpRHS;
  4248. // Signed zero may return inconsistent results between implementations.
  4249. // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
  4250. // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
  4251. // Therefore, we behave conservatively and only proceed if at least one of the
  4252. // operands is known to not be zero or if we don't care about signed zero.
  4253. switch (Pred) {
  4254. default: break;
  4255. // FIXME: Include OGT/OLT/UGT/ULT.
  4256. case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
  4257. case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
  4258. if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
  4259. !isKnownNonZero(CmpRHS))
  4260. return {SPF_UNKNOWN, SPNB_NA, false};
  4261. }
  4262. SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
  4263. bool Ordered = false;
  4264. // When given one NaN and one non-NaN input:
  4265. // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
  4266. // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
  4267. // ordered comparison fails), which could be NaN or non-NaN.
  4268. // so here we discover exactly what NaN behavior is required/accepted.
  4269. if (CmpInst::isFPPredicate(Pred)) {
  4270. bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
  4271. bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
  4272. if (LHSSafe && RHSSafe) {
  4273. // Both operands are known non-NaN.
  4274. NaNBehavior = SPNB_RETURNS_ANY;
  4275. } else if (CmpInst::isOrdered(Pred)) {
  4276. // An ordered comparison will return false when given a NaN, so it
  4277. // returns the RHS.
  4278. Ordered = true;
  4279. if (LHSSafe)
  4280. // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
  4281. NaNBehavior = SPNB_RETURNS_NAN;
  4282. else if (RHSSafe)
  4283. NaNBehavior = SPNB_RETURNS_OTHER;
  4284. else
  4285. // Completely unsafe.
  4286. return {SPF_UNKNOWN, SPNB_NA, false};
  4287. } else {
  4288. Ordered = false;
  4289. // An unordered comparison will return true when given a NaN, so it
  4290. // returns the LHS.
  4291. if (LHSSafe)
  4292. // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
  4293. NaNBehavior = SPNB_RETURNS_OTHER;
  4294. else if (RHSSafe)
  4295. NaNBehavior = SPNB_RETURNS_NAN;
  4296. else
  4297. // Completely unsafe.
  4298. return {SPF_UNKNOWN, SPNB_NA, false};
  4299. }
  4300. }
  4301. if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
  4302. std::swap(CmpLHS, CmpRHS);
  4303. Pred = CmpInst::getSwappedPredicate(Pred);
  4304. if (NaNBehavior == SPNB_RETURNS_NAN)
  4305. NaNBehavior = SPNB_RETURNS_OTHER;
  4306. else if (NaNBehavior == SPNB_RETURNS_OTHER)
  4307. NaNBehavior = SPNB_RETURNS_NAN;
  4308. Ordered = !Ordered;
  4309. }
  4310. // ([if]cmp X, Y) ? X : Y
  4311. if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
  4312. switch (Pred) {
  4313. default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
  4314. case ICmpInst::ICMP_UGT:
  4315. case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
  4316. case ICmpInst::ICMP_SGT:
  4317. case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
  4318. case ICmpInst::ICMP_ULT:
  4319. case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
  4320. case ICmpInst::ICMP_SLT:
  4321. case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
  4322. case FCmpInst::FCMP_UGT:
  4323. case FCmpInst::FCMP_UGE:
  4324. case FCmpInst::FCMP_OGT:
  4325. case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
  4326. case FCmpInst::FCMP_ULT:
  4327. case FCmpInst::FCMP_ULE:
  4328. case FCmpInst::FCMP_OLT:
  4329. case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
  4330. }
  4331. }
  4332. if (isKnownNegation(TrueVal, FalseVal)) {
  4333. // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
  4334. // match against either LHS or sext(LHS).
  4335. auto MaybeSExtCmpLHS =
  4336. m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
  4337. auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
  4338. auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
  4339. if (match(TrueVal, MaybeSExtCmpLHS)) {
  4340. // Set the return values. If the compare uses the negated value (-X >s 0),
  4341. // swap the return values because the negated value is always 'RHS'.
  4342. LHS = TrueVal;
  4343. RHS = FalseVal;
  4344. if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
  4345. std::swap(LHS, RHS);
  4346. // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
  4347. // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
  4348. if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
  4349. return {SPF_ABS, SPNB_NA, false};
  4350. // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
  4351. if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
  4352. return {SPF_ABS, SPNB_NA, false};
  4353. // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
  4354. // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
  4355. if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
  4356. return {SPF_NABS, SPNB_NA, false};
  4357. }
  4358. else if (match(FalseVal, MaybeSExtCmpLHS)) {
  4359. // Set the return values. If the compare uses the negated value (-X >s 0),
  4360. // swap the return values because the negated value is always 'RHS'.
  4361. LHS = FalseVal;
  4362. RHS = TrueVal;
  4363. if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
  4364. std::swap(LHS, RHS);
  4365. // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
  4366. // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
  4367. if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
  4368. return {SPF_NABS, SPNB_NA, false};
  4369. // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
  4370. // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
  4371. if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
  4372. return {SPF_ABS, SPNB_NA, false};
  4373. }
  4374. }
  4375. if (CmpInst::isIntPredicate(Pred))
  4376. return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
  4377. // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
  4378. // may return either -0.0 or 0.0, so fcmp/select pair has stricter
  4379. // semantics than minNum. Be conservative in such case.
  4380. if (NaNBehavior != SPNB_RETURNS_ANY ||
  4381. (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
  4382. !isKnownNonZero(CmpRHS)))
  4383. return {SPF_UNKNOWN, SPNB_NA, false};
  4384. return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
  4385. }
  4386. /// Helps to match a select pattern in case of a type mismatch.
  4387. ///
  4388. /// The function processes the case when type of true and false values of a
  4389. /// select instruction differs from type of the cmp instruction operands because
  4390. /// of a cast instruction. The function checks if it is legal to move the cast
  4391. /// operation after "select". If yes, it returns the new second value of
  4392. /// "select" (with the assumption that cast is moved):
  4393. /// 1. As operand of cast instruction when both values of "select" are same cast
  4394. /// instructions.
  4395. /// 2. As restored constant (by applying reverse cast operation) when the first
  4396. /// value of the "select" is a cast operation and the second value is a
  4397. /// constant.
  4398. /// NOTE: We return only the new second value because the first value could be
  4399. /// accessed as operand of cast instruction.
  4400. static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
  4401. Instruction::CastOps *CastOp) {
  4402. auto *Cast1 = dyn_cast<CastInst>(V1);
  4403. if (!Cast1)
  4404. return nullptr;
  4405. *CastOp = Cast1->getOpcode();
  4406. Type *SrcTy = Cast1->getSrcTy();
  4407. if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
  4408. // If V1 and V2 are both the same cast from the same type, look through V1.
  4409. if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
  4410. return Cast2->getOperand(0);
  4411. return nullptr;
  4412. }
  4413. auto *C = dyn_cast<Constant>(V2);
  4414. if (!C)
  4415. return nullptr;
  4416. Constant *CastedTo = nullptr;
  4417. switch (*CastOp) {
  4418. case Instruction::ZExt:
  4419. if (CmpI->isUnsigned())
  4420. CastedTo = ConstantExpr::getTrunc(C, SrcTy);
  4421. break;
  4422. case Instruction::SExt:
  4423. if (CmpI->isSigned())
  4424. CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
  4425. break;
  4426. case Instruction::Trunc:
  4427. Constant *CmpConst;
  4428. if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
  4429. CmpConst->getType() == SrcTy) {
  4430. // Here we have the following case:
  4431. //
  4432. // %cond = cmp iN %x, CmpConst
  4433. // %tr = trunc iN %x to iK
  4434. // %narrowsel = select i1 %cond, iK %t, iK C
  4435. //
  4436. // We can always move trunc after select operation:
  4437. //
  4438. // %cond = cmp iN %x, CmpConst
  4439. // %widesel = select i1 %cond, iN %x, iN CmpConst
  4440. // %tr = trunc iN %widesel to iK
  4441. //
  4442. // Note that C could be extended in any way because we don't care about
  4443. // upper bits after truncation. It can't be abs pattern, because it would
  4444. // look like:
  4445. //
  4446. // select i1 %cond, x, -x.
  4447. //
  4448. // So only min/max pattern could be matched. Such match requires widened C
  4449. // == CmpConst. That is why set widened C = CmpConst, condition trunc
  4450. // CmpConst == C is checked below.
  4451. CastedTo = CmpConst;
  4452. } else {
  4453. CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned());
  4454. }
  4455. break;
  4456. case Instruction::FPTrunc:
  4457. CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true);
  4458. break;
  4459. case Instruction::FPExt:
  4460. CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true);
  4461. break;
  4462. case Instruction::FPToUI:
  4463. CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true);
  4464. break;
  4465. case Instruction::FPToSI:
  4466. CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true);
  4467. break;
  4468. case Instruction::UIToFP:
  4469. CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true);
  4470. break;
  4471. case Instruction::SIToFP:
  4472. CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true);
  4473. break;
  4474. default:
  4475. break;
  4476. }
  4477. if (!CastedTo)
  4478. return nullptr;
  4479. // Make sure the cast doesn't lose any information.
  4480. Constant *CastedBack =
  4481. ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true);
  4482. if (CastedBack != C)
  4483. return nullptr;
  4484. return CastedTo;
  4485. }
  4486. SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
  4487. Instruction::CastOps *CastOp,
  4488. unsigned Depth) {
  4489. if (Depth >= MaxDepth)
  4490. return {SPF_UNKNOWN, SPNB_NA, false};
  4491. SelectInst *SI = dyn_cast<SelectInst>(V);
  4492. if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
  4493. CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
  4494. if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
  4495. Value *TrueVal = SI->getTrueValue();
  4496. Value *FalseVal = SI->getFalseValue();
  4497. return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
  4498. CastOp, Depth);
  4499. }
  4500. SelectPatternResult llvm::matchDecomposedSelectPattern(
  4501. CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
  4502. Instruction::CastOps *CastOp, unsigned Depth) {
  4503. CmpInst::Predicate Pred = CmpI->getPredicate();
  4504. Value *CmpLHS = CmpI->getOperand(0);
  4505. Value *CmpRHS = CmpI->getOperand(1);
  4506. FastMathFlags FMF;
  4507. if (isa<FPMathOperator>(CmpI))
  4508. FMF = CmpI->getFastMathFlags();
  4509. // Bail out early.
  4510. if (CmpI->isEquality())
  4511. return {SPF_UNKNOWN, SPNB_NA, false};
  4512. // Deal with type mismatches.
  4513. if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
  4514. if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
  4515. // If this is a potential fmin/fmax with a cast to integer, then ignore
  4516. // -0.0 because there is no corresponding integer value.
  4517. if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
  4518. FMF.setNoSignedZeros();
  4519. return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
  4520. cast<CastInst>(TrueVal)->getOperand(0), C,
  4521. LHS, RHS, Depth);
  4522. }
  4523. if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
  4524. // If this is a potential fmin/fmax with a cast to integer, then ignore
  4525. // -0.0 because there is no corresponding integer value.
  4526. if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
  4527. FMF.setNoSignedZeros();
  4528. return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
  4529. C, cast<CastInst>(FalseVal)->getOperand(0),
  4530. LHS, RHS, Depth);
  4531. }
  4532. }
  4533. return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
  4534. LHS, RHS, Depth);
  4535. }
  4536. CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) {
  4537. if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
  4538. if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
  4539. if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
  4540. if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
  4541. if (SPF == SPF_FMINNUM)
  4542. return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
  4543. if (SPF == SPF_FMAXNUM)
  4544. return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
  4545. llvm_unreachable("unhandled!");
  4546. }
  4547. SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {
  4548. if (SPF == SPF_SMIN) return SPF_SMAX;
  4549. if (SPF == SPF_UMIN) return SPF_UMAX;
  4550. if (SPF == SPF_SMAX) return SPF_SMIN;
  4551. if (SPF == SPF_UMAX) return SPF_UMIN;
  4552. llvm_unreachable("unhandled!");
  4553. }
  4554. CmpInst::Predicate llvm::getInverseMinMaxPred(SelectPatternFlavor SPF) {
  4555. return getMinMaxPred(getInverseMinMaxFlavor(SPF));
  4556. }
  4557. /// Return true if "icmp Pred LHS RHS" is always true.
  4558. static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
  4559. const Value *RHS, const DataLayout &DL,
  4560. unsigned Depth) {
  4561. assert(!LHS->getType()->isVectorTy() && "TODO: extend to handle vectors!");
  4562. if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
  4563. return true;
  4564. switch (Pred) {
  4565. default:
  4566. return false;
  4567. case CmpInst::ICMP_SLE: {
  4568. const APInt *C;
  4569. // LHS s<= LHS +_{nsw} C if C >= 0
  4570. if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))))
  4571. return !C->isNegative();
  4572. return false;
  4573. }
  4574. case CmpInst::ICMP_ULE: {
  4575. const APInt *C;
  4576. // LHS u<= LHS +_{nuw} C for any C
  4577. if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
  4578. return true;
  4579. // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
  4580. auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
  4581. const Value *&X,
  4582. const APInt *&CA, const APInt *&CB) {
  4583. if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) &&
  4584. match(B, m_NUWAdd(m_Specific(X), m_APInt(CB))))
  4585. return true;
  4586. // If X & C == 0 then (X | C) == X +_{nuw} C
  4587. if (match(A, m_Or(m_Value(X), m_APInt(CA))) &&
  4588. match(B, m_Or(m_Specific(X), m_APInt(CB)))) {
  4589. KnownBits Known(CA->getBitWidth());
  4590. computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr,
  4591. /*CxtI*/ nullptr, /*DT*/ nullptr);
  4592. if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero))
  4593. return true;
  4594. }
  4595. return false;
  4596. };
  4597. const Value *X;
  4598. const APInt *CLHS, *CRHS;
  4599. if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS))
  4600. return CLHS->ule(*CRHS);
  4601. return false;
  4602. }
  4603. }
  4604. }
  4605. /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
  4606. /// ALHS ARHS" is true. Otherwise, return None.
  4607. static Optional<bool>
  4608. isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
  4609. const Value *ARHS, const Value *BLHS, const Value *BRHS,
  4610. const DataLayout &DL, unsigned Depth) {
  4611. switch (Pred) {
  4612. default:
  4613. return None;
  4614. case CmpInst::ICMP_SLT:
  4615. case CmpInst::ICMP_SLE:
  4616. if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) &&
  4617. isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth))
  4618. return true;
  4619. return None;
  4620. case CmpInst::ICMP_ULT:
  4621. case CmpInst::ICMP_ULE:
  4622. if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) &&
  4623. isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth))
  4624. return true;
  4625. return None;
  4626. }
  4627. }
  4628. /// Return true if the operands of the two compares match. IsSwappedOps is true
  4629. /// when the operands match, but are swapped.
  4630. static bool isMatchingOps(const Value *ALHS, const Value *ARHS,
  4631. const Value *BLHS, const Value *BRHS,
  4632. bool &IsSwappedOps) {
  4633. bool IsMatchingOps = (ALHS == BLHS && ARHS == BRHS);
  4634. IsSwappedOps = (ALHS == BRHS && ARHS == BLHS);
  4635. return IsMatchingOps || IsSwappedOps;
  4636. }
  4637. /// Return true if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is true.
  4638. /// Return false if "icmp1 APred X, Y" implies "icmp2 BPred X, Y" is false.
  4639. /// Otherwise, return None if we can't infer anything.
  4640. static Optional<bool> isImpliedCondMatchingOperands(CmpInst::Predicate APred,
  4641. CmpInst::Predicate BPred,
  4642. bool AreSwappedOps) {
  4643. // Canonicalize the predicate as if the operands were not commuted.
  4644. if (AreSwappedOps)
  4645. BPred = ICmpInst::getSwappedPredicate(BPred);
  4646. if (CmpInst::isImpliedTrueByMatchingCmp(APred, BPred))
  4647. return true;
  4648. if (CmpInst::isImpliedFalseByMatchingCmp(APred, BPred))
  4649. return false;
  4650. return None;
  4651. }
  4652. /// Return true if "icmp APred X, C1" implies "icmp BPred X, C2" is true.
  4653. /// Return false if "icmp APred X, C1" implies "icmp BPred X, C2" is false.
  4654. /// Otherwise, return None if we can't infer anything.
  4655. static Optional<bool>
  4656. isImpliedCondMatchingImmOperands(CmpInst::Predicate APred,
  4657. const ConstantInt *C1,
  4658. CmpInst::Predicate BPred,
  4659. const ConstantInt *C2) {
  4660. ConstantRange DomCR =
  4661. ConstantRange::makeExactICmpRegion(APred, C1->getValue());
  4662. ConstantRange CR =
  4663. ConstantRange::makeAllowedICmpRegion(BPred, C2->getValue());
  4664. ConstantRange Intersection = DomCR.intersectWith(CR);
  4665. ConstantRange Difference = DomCR.difference(CR);
  4666. if (Intersection.isEmptySet())
  4667. return false;
  4668. if (Difference.isEmptySet())
  4669. return true;
  4670. return None;
  4671. }
  4672. /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
  4673. /// false. Otherwise, return None if we can't infer anything.
  4674. static Optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
  4675. const ICmpInst *RHS,
  4676. const DataLayout &DL, bool LHSIsTrue,
  4677. unsigned Depth) {
  4678. Value *ALHS = LHS->getOperand(0);
  4679. Value *ARHS = LHS->getOperand(1);
  4680. // The rest of the logic assumes the LHS condition is true. If that's not the
  4681. // case, invert the predicate to make it so.
  4682. ICmpInst::Predicate APred =
  4683. LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
  4684. Value *BLHS = RHS->getOperand(0);
  4685. Value *BRHS = RHS->getOperand(1);
  4686. ICmpInst::Predicate BPred = RHS->getPredicate();
  4687. // Can we infer anything when the two compares have matching operands?
  4688. bool AreSwappedOps;
  4689. if (isMatchingOps(ALHS, ARHS, BLHS, BRHS, AreSwappedOps)) {
  4690. if (Optional<bool> Implication = isImpliedCondMatchingOperands(
  4691. APred, BPred, AreSwappedOps))
  4692. return Implication;
  4693. // No amount of additional analysis will infer the second condition, so
  4694. // early exit.
  4695. return None;
  4696. }
  4697. // Can we infer anything when the LHS operands match and the RHS operands are
  4698. // constants (not necessarily matching)?
  4699. if (ALHS == BLHS && isa<ConstantInt>(ARHS) && isa<ConstantInt>(BRHS)) {
  4700. if (Optional<bool> Implication = isImpliedCondMatchingImmOperands(
  4701. APred, cast<ConstantInt>(ARHS), BPred, cast<ConstantInt>(BRHS)))
  4702. return Implication;
  4703. // No amount of additional analysis will infer the second condition, so
  4704. // early exit.
  4705. return None;
  4706. }
  4707. if (APred == BPred)
  4708. return isImpliedCondOperands(APred, ALHS, ARHS, BLHS, BRHS, DL, Depth);
  4709. return None;
  4710. }
  4711. /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
  4712. /// false. Otherwise, return None if we can't infer anything. We expect the
  4713. /// RHS to be an icmp and the LHS to be an 'and' or an 'or' instruction.
  4714. static Optional<bool> isImpliedCondAndOr(const BinaryOperator *LHS,
  4715. const ICmpInst *RHS,
  4716. const DataLayout &DL, bool LHSIsTrue,
  4717. unsigned Depth) {
  4718. // The LHS must be an 'or' or an 'and' instruction.
  4719. assert((LHS->getOpcode() == Instruction::And ||
  4720. LHS->getOpcode() == Instruction::Or) &&
  4721. "Expected LHS to be 'and' or 'or'.");
  4722. assert(Depth <= MaxDepth && "Hit recursion limit");
  4723. // If the result of an 'or' is false, then we know both legs of the 'or' are
  4724. // false. Similarly, if the result of an 'and' is true, then we know both
  4725. // legs of the 'and' are true.
  4726. Value *ALHS, *ARHS;
  4727. if ((!LHSIsTrue && match(LHS, m_Or(m_Value(ALHS), m_Value(ARHS)))) ||
  4728. (LHSIsTrue && match(LHS, m_And(m_Value(ALHS), m_Value(ARHS))))) {
  4729. // FIXME: Make this non-recursion.
  4730. if (Optional<bool> Implication =
  4731. isImpliedCondition(ALHS, RHS, DL, LHSIsTrue, Depth + 1))
  4732. return Implication;
  4733. if (Optional<bool> Implication =
  4734. isImpliedCondition(ARHS, RHS, DL, LHSIsTrue, Depth + 1))
  4735. return Implication;
  4736. return None;
  4737. }
  4738. return None;
  4739. }
  4740. Optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
  4741. const DataLayout &DL, bool LHSIsTrue,
  4742. unsigned Depth) {
  4743. // Bail out when we hit the limit.
  4744. if (Depth == MaxDepth)
  4745. return None;
  4746. // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
  4747. // example.
  4748. if (LHS->getType() != RHS->getType())
  4749. return None;
  4750. Type *OpTy = LHS->getType();
  4751. assert(OpTy->isIntOrIntVectorTy(1) && "Expected integer type only!");
  4752. // LHS ==> RHS by definition
  4753. if (LHS == RHS)
  4754. return LHSIsTrue;
  4755. // FIXME: Extending the code below to handle vectors.
  4756. if (OpTy->isVectorTy())
  4757. return None;
  4758. assert(OpTy->isIntegerTy(1) && "implied by above");
  4759. // Both LHS and RHS are icmps.
  4760. const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
  4761. const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS);
  4762. if (LHSCmp && RHSCmp)
  4763. return isImpliedCondICmps(LHSCmp, RHSCmp, DL, LHSIsTrue, Depth);
  4764. // The LHS should be an 'or' or an 'and' instruction. We expect the RHS to be
  4765. // an icmp. FIXME: Add support for and/or on the RHS.
  4766. const BinaryOperator *LHSBO = dyn_cast<BinaryOperator>(LHS);
  4767. if (LHSBO && RHSCmp) {
  4768. if ((LHSBO->getOpcode() == Instruction::And ||
  4769. LHSBO->getOpcode() == Instruction::Or))
  4770. return isImpliedCondAndOr(LHSBO, RHSCmp, DL, LHSIsTrue, Depth);
  4771. }
  4772. return None;
  4773. }
  4774. Optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
  4775. const Instruction *ContextI,
  4776. const DataLayout &DL) {
  4777. assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
  4778. if (!ContextI || !ContextI->getParent())
  4779. return None;
  4780. // TODO: This is a poor/cheap way to determine dominance. Should we use a
  4781. // dominator tree (eg, from a SimplifyQuery) instead?
  4782. const BasicBlock *ContextBB = ContextI->getParent();
  4783. const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
  4784. if (!PredBB)
  4785. return None;
  4786. // We need a conditional branch in the predecessor.
  4787. Value *PredCond;
  4788. BasicBlock *TrueBB, *FalseBB;
  4789. if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
  4790. return None;
  4791. // The branch should get simplified. Don't bother simplifying this condition.
  4792. if (TrueBB == FalseBB)
  4793. return None;
  4794. assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
  4795. "Predecessor block does not point to successor?");
  4796. // Is this condition implied by the predecessor condition?
  4797. bool CondIsTrue = TrueBB == ContextBB;
  4798. return isImpliedCondition(PredCond, Cond, DL, CondIsTrue);
  4799. }
  4800. static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
  4801. APInt &Upper, const InstrInfoQuery &IIQ) {
  4802. unsigned Width = Lower.getBitWidth();
  4803. const APInt *C;
  4804. switch (BO.getOpcode()) {
  4805. case Instruction::Add:
  4806. if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) {
  4807. // FIXME: If we have both nuw and nsw, we should reduce the range further.
  4808. if (IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(&BO))) {
  4809. // 'add nuw x, C' produces [C, UINT_MAX].
  4810. Lower = *C;
  4811. } else if (IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(&BO))) {
  4812. if (C->isNegative()) {
  4813. // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
  4814. Lower = APInt::getSignedMinValue(Width);
  4815. Upper = APInt::getSignedMaxValue(Width) + *C + 1;
  4816. } else {
  4817. // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
  4818. Lower = APInt::getSignedMinValue(Width) + *C;
  4819. Upper = APInt::getSignedMaxValue(Width) + 1;
  4820. }
  4821. }
  4822. }
  4823. break;
  4824. case Instruction::And:
  4825. if (match(BO.getOperand(1), m_APInt(C)))
  4826. // 'and x, C' produces [0, C].
  4827. Upper = *C + 1;
  4828. break;
  4829. case Instruction::Or:
  4830. if (match(BO.getOperand(1), m_APInt(C)))
  4831. // 'or x, C' produces [C, UINT_MAX].
  4832. Lower = *C;
  4833. break;
  4834. case Instruction::AShr:
  4835. if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
  4836. // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
  4837. Lower = APInt::getSignedMinValue(Width).ashr(*C);
  4838. Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
  4839. } else if (match(BO.getOperand(0), m_APInt(C))) {
  4840. unsigned ShiftAmount = Width - 1;
  4841. if (!C->isNullValue() && IIQ.isExact(&BO))
  4842. ShiftAmount = C->countTrailingZeros();
  4843. if (C->isNegative()) {
  4844. // 'ashr C, x' produces [C, C >> (Width-1)]
  4845. Lower = *C;
  4846. Upper = C->ashr(ShiftAmount) + 1;
  4847. } else {
  4848. // 'ashr C, x' produces [C >> (Width-1), C]
  4849. Lower = C->ashr(ShiftAmount);
  4850. Upper = *C + 1;
  4851. }
  4852. }
  4853. break;
  4854. case Instruction::LShr:
  4855. if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
  4856. // 'lshr x, C' produces [0, UINT_MAX >> C].
  4857. Upper = APInt::getAllOnesValue(Width).lshr(*C) + 1;
  4858. } else if (match(BO.getOperand(0), m_APInt(C))) {
  4859. // 'lshr C, x' produces [C >> (Width-1), C].
  4860. unsigned ShiftAmount = Width - 1;
  4861. if (!C->isNullValue() && IIQ.isExact(&BO))
  4862. ShiftAmount = C->countTrailingZeros();
  4863. Lower = C->lshr(ShiftAmount);
  4864. Upper = *C + 1;
  4865. }
  4866. break;
  4867. case Instruction::Shl:
  4868. if (match(BO.getOperand(0), m_APInt(C))) {
  4869. if (IIQ.hasNoUnsignedWrap(&BO)) {
  4870. // 'shl nuw C, x' produces [C, C << CLZ(C)]
  4871. Lower = *C;
  4872. Upper = Lower.shl(Lower.countLeadingZeros()) + 1;
  4873. } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
  4874. if (C->isNegative()) {
  4875. // 'shl nsw C, x' produces [C << CLO(C)-1, C]
  4876. unsigned ShiftAmount = C->countLeadingOnes() - 1;
  4877. Lower = C->shl(ShiftAmount);
  4878. Upper = *C + 1;
  4879. } else {
  4880. // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
  4881. unsigned ShiftAmount = C->countLeadingZeros() - 1;
  4882. Lower = *C;
  4883. Upper = C->shl(ShiftAmount) + 1;
  4884. }
  4885. }
  4886. }
  4887. break;
  4888. case Instruction::SDiv:
  4889. if (match(BO.getOperand(1), m_APInt(C))) {
  4890. APInt IntMin = APInt::getSignedMinValue(Width);
  4891. APInt IntMax = APInt::getSignedMaxValue(Width);
  4892. if (C->isAllOnesValue()) {
  4893. // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
  4894. // where C != -1 and C != 0 and C != 1
  4895. Lower = IntMin + 1;
  4896. Upper = IntMax + 1;
  4897. } else if (C->countLeadingZeros() < Width - 1) {
  4898. // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
  4899. // where C != -1 and C != 0 and C != 1
  4900. Lower = IntMin.sdiv(*C);
  4901. Upper = IntMax.sdiv(*C);
  4902. if (Lower.sgt(Upper))
  4903. std::swap(Lower, Upper);
  4904. Upper = Upper + 1;
  4905. assert(Upper != Lower && "Upper part of range has wrapped!");
  4906. }
  4907. } else if (match(BO.getOperand(0), m_APInt(C))) {
  4908. if (C->isMinSignedValue()) {
  4909. // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
  4910. Lower = *C;
  4911. Upper = Lower.lshr(1) + 1;
  4912. } else {
  4913. // 'sdiv C, x' produces [-|C|, |C|].
  4914. Upper = C->abs() + 1;
  4915. Lower = (-Upper) + 1;
  4916. }
  4917. }
  4918. break;
  4919. case Instruction::UDiv:
  4920. if (match(BO.getOperand(1), m_APInt(C)) && !C->isNullValue()) {
  4921. // 'udiv x, C' produces [0, UINT_MAX / C].
  4922. Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
  4923. } else if (match(BO.getOperand(0), m_APInt(C))) {
  4924. // 'udiv C, x' produces [0, C].
  4925. Upper = *C + 1;
  4926. }
  4927. break;
  4928. case Instruction::SRem:
  4929. if (match(BO.getOperand(1), m_APInt(C))) {
  4930. // 'srem x, C' produces (-|C|, |C|).
  4931. Upper = C->abs();
  4932. Lower = (-Upper) + 1;
  4933. }
  4934. break;
  4935. case Instruction::URem:
  4936. if (match(BO.getOperand(1), m_APInt(C)))
  4937. // 'urem x, C' produces [0, C).
  4938. Upper = *C;
  4939. break;
  4940. default:
  4941. break;
  4942. }
  4943. }
  4944. static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
  4945. APInt &Upper) {
  4946. unsigned Width = Lower.getBitWidth();
  4947. const APInt *C;
  4948. switch (II.getIntrinsicID()) {
  4949. case Intrinsic::uadd_sat:
  4950. // uadd.sat(x, C) produces [C, UINT_MAX].
  4951. if (match(II.getOperand(0), m_APInt(C)) ||
  4952. match(II.getOperand(1), m_APInt(C)))
  4953. Lower = *C;
  4954. break;
  4955. case Intrinsic::sadd_sat:
  4956. if (match(II.getOperand(0), m_APInt(C)) ||
  4957. match(II.getOperand(1), m_APInt(C))) {
  4958. if (C->isNegative()) {
  4959. // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
  4960. Lower = APInt::getSignedMinValue(Width);
  4961. Upper = APInt::getSignedMaxValue(Width) + *C + 1;
  4962. } else {
  4963. // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
  4964. Lower = APInt::getSignedMinValue(Width) + *C;
  4965. Upper = APInt::getSignedMaxValue(Width) + 1;
  4966. }
  4967. }
  4968. break;
  4969. case Intrinsic::usub_sat:
  4970. // usub.sat(C, x) produces [0, C].
  4971. if (match(II.getOperand(0), m_APInt(C)))
  4972. Upper = *C + 1;
  4973. // usub.sat(x, C) produces [0, UINT_MAX - C].
  4974. else if (match(II.getOperand(1), m_APInt(C)))
  4975. Upper = APInt::getMaxValue(Width) - *C + 1;
  4976. break;
  4977. case Intrinsic::ssub_sat:
  4978. if (match(II.getOperand(0), m_APInt(C))) {
  4979. if (C->isNegative()) {
  4980. // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
  4981. Lower = APInt::getSignedMinValue(Width);
  4982. Upper = *C - APInt::getSignedMinValue(Width) + 1;
  4983. } else {
  4984. // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
  4985. Lower = *C - APInt::getSignedMaxValue(Width);
  4986. Upper = APInt::getSignedMaxValue(Width) + 1;
  4987. }
  4988. } else if (match(II.getOperand(1), m_APInt(C))) {
  4989. if (C->isNegative()) {
  4990. // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
  4991. Lower = APInt::getSignedMinValue(Width) - *C;
  4992. Upper = APInt::getSignedMaxValue(Width) + 1;
  4993. } else {
  4994. // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
  4995. Lower = APInt::getSignedMinValue(Width);
  4996. Upper = APInt::getSignedMaxValue(Width) - *C + 1;
  4997. }
  4998. }
  4999. break;
  5000. default:
  5001. break;
  5002. }
  5003. }
  5004. static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
  5005. APInt &Upper, const InstrInfoQuery &IIQ) {
  5006. const Value *LHS = nullptr, *RHS = nullptr;
  5007. SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS);
  5008. if (R.Flavor == SPF_UNKNOWN)
  5009. return;
  5010. unsigned BitWidth = SI.getType()->getScalarSizeInBits();
  5011. if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
  5012. // If the negation part of the abs (in RHS) has the NSW flag,
  5013. // then the result of abs(X) is [0..SIGNED_MAX],
  5014. // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
  5015. Lower = APInt::getNullValue(BitWidth);
  5016. if (match(RHS, m_Neg(m_Specific(LHS))) &&
  5017. IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
  5018. Upper = APInt::getSignedMaxValue(BitWidth) + 1;
  5019. else
  5020. Upper = APInt::getSignedMinValue(BitWidth) + 1;
  5021. return;
  5022. }
  5023. if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
  5024. // The result of -abs(X) is <= 0.
  5025. Lower = APInt::getSignedMinValue(BitWidth);
  5026. Upper = APInt(BitWidth, 1);
  5027. return;
  5028. }
  5029. const APInt *C;
  5030. if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
  5031. return;
  5032. switch (R.Flavor) {
  5033. case SPF_UMIN:
  5034. Upper = *C + 1;
  5035. break;
  5036. case SPF_UMAX:
  5037. Lower = *C;
  5038. break;
  5039. case SPF_SMIN:
  5040. Lower = APInt::getSignedMinValue(BitWidth);
  5041. Upper = *C + 1;
  5042. break;
  5043. case SPF_SMAX:
  5044. Lower = *C;
  5045. Upper = APInt::getSignedMaxValue(BitWidth) + 1;
  5046. break;
  5047. default:
  5048. break;
  5049. }
  5050. }
  5051. ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo) {
  5052. assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
  5053. const APInt *C;
  5054. if (match(V, m_APInt(C)))
  5055. return ConstantRange(*C);
  5056. InstrInfoQuery IIQ(UseInstrInfo);
  5057. unsigned BitWidth = V->getType()->getScalarSizeInBits();
  5058. APInt Lower = APInt(BitWidth, 0);
  5059. APInt Upper = APInt(BitWidth, 0);
  5060. if (auto *BO = dyn_cast<BinaryOperator>(V))
  5061. setLimitsForBinOp(*BO, Lower, Upper, IIQ);
  5062. else if (auto *II = dyn_cast<IntrinsicInst>(V))
  5063. setLimitsForIntrinsic(*II, Lower, Upper);
  5064. else if (auto *SI = dyn_cast<SelectInst>(V))
  5065. setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
  5066. ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper);
  5067. if (auto *I = dyn_cast<Instruction>(V))
  5068. if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
  5069. CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));
  5070. return CR;
  5071. }
  5072. static Optional<int64_t>
  5073. getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
  5074. // Skip over the first indices.
  5075. gep_type_iterator GTI = gep_type_begin(GEP);
  5076. for (unsigned i = 1; i != Idx; ++i, ++GTI)
  5077. /*skip along*/;
  5078. // Compute the offset implied by the rest of the indices.
  5079. int64_t Offset = 0;
  5080. for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
  5081. ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
  5082. if (!OpC)
  5083. return None;
  5084. if (OpC->isZero())
  5085. continue; // No offset.
  5086. // Handle struct indices, which add their field offset to the pointer.
  5087. if (StructType *STy = GTI.getStructTypeOrNull()) {
  5088. Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
  5089. continue;
  5090. }
  5091. // Otherwise, we have a sequential type like an array or vector. Multiply
  5092. // the index by the ElementSize.
  5093. uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
  5094. Offset += Size * OpC->getSExtValue();
  5095. }
  5096. return Offset;
  5097. }
  5098. Optional<int64_t> llvm::isPointerOffset(const Value *Ptr1, const Value *Ptr2,
  5099. const DataLayout &DL) {
  5100. Ptr1 = Ptr1->stripPointerCasts();
  5101. Ptr2 = Ptr2->stripPointerCasts();
  5102. // Handle the trivial case first.
  5103. if (Ptr1 == Ptr2) {
  5104. return 0;
  5105. }
  5106. const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
  5107. const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
  5108. // If one pointer is a GEP see if the GEP is a constant offset from the base,
  5109. // as in "P" and "gep P, 1".
  5110. // Also do this iteratively to handle the the following case:
  5111. // Ptr_t1 = GEP Ptr1, c1
  5112. // Ptr_t2 = GEP Ptr_t1, c2
  5113. // Ptr2 = GEP Ptr_t2, c3
  5114. // where we will return c1+c2+c3.
  5115. // TODO: Handle the case when both Ptr1 and Ptr2 are GEPs of some common base
  5116. // -- replace getOffsetFromBase with getOffsetAndBase, check that the bases
  5117. // are the same, and return the difference between offsets.
  5118. auto getOffsetFromBase = [&DL](const GEPOperator *GEP,
  5119. const Value *Ptr) -> Optional<int64_t> {
  5120. const GEPOperator *GEP_T = GEP;
  5121. int64_t OffsetVal = 0;
  5122. bool HasSameBase = false;
  5123. while (GEP_T) {
  5124. auto Offset = getOffsetFromIndex(GEP_T, 1, DL);
  5125. if (!Offset)
  5126. return None;
  5127. OffsetVal += *Offset;
  5128. auto Op0 = GEP_T->getOperand(0)->stripPointerCasts();
  5129. if (Op0 == Ptr) {
  5130. HasSameBase = true;
  5131. break;
  5132. }
  5133. GEP_T = dyn_cast<GEPOperator>(Op0);
  5134. }
  5135. if (!HasSameBase)
  5136. return None;
  5137. return OffsetVal;
  5138. };
  5139. if (GEP1) {
  5140. auto Offset = getOffsetFromBase(GEP1, Ptr2);
  5141. if (Offset)
  5142. return -*Offset;
  5143. }
  5144. if (GEP2) {
  5145. auto Offset = getOffsetFromBase(GEP2, Ptr1);
  5146. if (Offset)
  5147. return Offset;
  5148. }
  5149. // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
  5150. // base. After that base, they may have some number of common (and
  5151. // potentially variable) indices. After that they handle some constant
  5152. // offset, which determines their offset from each other. At this point, we
  5153. // handle no other case.
  5154. if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0))
  5155. return None;
  5156. // Skip any common indices and track the GEP types.
  5157. unsigned Idx = 1;
  5158. for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
  5159. if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
  5160. break;
  5161. auto Offset1 = getOffsetFromIndex(GEP1, Idx, DL);
  5162. auto Offset2 = getOffsetFromIndex(GEP2, Idx, DL);
  5163. if (!Offset1 || !Offset2)
  5164. return None;
  5165. return *Offset2 - *Offset1;
  5166. }