ValueTracking.cpp 280 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535
  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/STLExtras.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/ADT/SmallSet.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ADT/iterator_range.h"
  23. #include "llvm/Analysis/AliasAnalysis.h"
  24. #include "llvm/Analysis/AssumeBundleQueries.h"
  25. #include "llvm/Analysis/AssumptionCache.h"
  26. #include "llvm/Analysis/ConstantFolding.h"
  27. #include "llvm/Analysis/EHPersonalities.h"
  28. #include "llvm/Analysis/GuardUtils.h"
  29. #include "llvm/Analysis/InstructionSimplify.h"
  30. #include "llvm/Analysis/Loads.h"
  31. #include "llvm/Analysis/LoopInfo.h"
  32. #include "llvm/Analysis/OptimizationRemarkEmitter.h"
  33. #include "llvm/Analysis/TargetLibraryInfo.h"
  34. #include "llvm/Analysis/VectorUtils.h"
  35. #include "llvm/IR/Argument.h"
  36. #include "llvm/IR/Attributes.h"
  37. #include "llvm/IR/BasicBlock.h"
  38. #include "llvm/IR/Constant.h"
  39. #include "llvm/IR/ConstantRange.h"
  40. #include "llvm/IR/Constants.h"
  41. #include "llvm/IR/DerivedTypes.h"
  42. #include "llvm/IR/DiagnosticInfo.h"
  43. #include "llvm/IR/Dominators.h"
  44. #include "llvm/IR/Function.h"
  45. #include "llvm/IR/GetElementPtrTypeIterator.h"
  46. #include "llvm/IR/GlobalAlias.h"
  47. #include "llvm/IR/GlobalValue.h"
  48. #include "llvm/IR/GlobalVariable.h"
  49. #include "llvm/IR/InstrTypes.h"
  50. #include "llvm/IR/Instruction.h"
  51. #include "llvm/IR/Instructions.h"
  52. #include "llvm/IR/IntrinsicInst.h"
  53. #include "llvm/IR/Intrinsics.h"
  54. #include "llvm/IR/IntrinsicsAArch64.h"
  55. #include "llvm/IR/IntrinsicsRISCV.h"
  56. #include "llvm/IR/IntrinsicsX86.h"
  57. #include "llvm/IR/LLVMContext.h"
  58. #include "llvm/IR/Metadata.h"
  59. #include "llvm/IR/Module.h"
  60. #include "llvm/IR/Operator.h"
  61. #include "llvm/IR/PatternMatch.h"
  62. #include "llvm/IR/Type.h"
  63. #include "llvm/IR/User.h"
  64. #include "llvm/IR/Value.h"
  65. #include "llvm/Support/Casting.h"
  66. #include "llvm/Support/CommandLine.h"
  67. #include "llvm/Support/Compiler.h"
  68. #include "llvm/Support/ErrorHandling.h"
  69. #include "llvm/Support/KnownBits.h"
  70. #include "llvm/Support/MathExtras.h"
  71. #include <algorithm>
  72. #include <cassert>
  73. #include <cstdint>
  74. #include <optional>
  75. #include <utility>
  76. using namespace llvm;
  77. using namespace llvm::PatternMatch;
  78. // Controls the number of uses of the value searched for possible
  79. // dominating comparisons.
  80. static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
  81. cl::Hidden, cl::init(20));
  82. /// Returns the bitwidth of the given scalar or pointer type. For vector types,
  83. /// returns the element type's bitwidth.
  84. static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
  85. if (unsigned BitWidth = Ty->getScalarSizeInBits())
  86. return BitWidth;
  87. return DL.getPointerTypeSizeInBits(Ty);
  88. }
  89. namespace {
  90. // Simplifying using an assume can only be done in a particular control-flow
  91. // context (the context instruction provides that context). If an assume and
  92. // the context instruction are not in the same block then the DT helps in
  93. // figuring out if we can use it.
  94. struct Query {
  95. const DataLayout &DL;
  96. AssumptionCache *AC;
  97. const Instruction *CxtI;
  98. const DominatorTree *DT;
  99. // Unlike the other analyses, this may be a nullptr because not all clients
  100. // provide it currently.
  101. OptimizationRemarkEmitter *ORE;
  102. /// If true, it is safe to use metadata during simplification.
  103. InstrInfoQuery IIQ;
  104. Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
  105. const DominatorTree *DT, bool UseInstrInfo,
  106. OptimizationRemarkEmitter *ORE = nullptr)
  107. : DL(DL), AC(AC), CxtI(CxtI), DT(DT), ORE(ORE), IIQ(UseInstrInfo) {}
  108. };
  109. } // end anonymous namespace
  110. // Given the provided Value and, potentially, a context instruction, return
  111. // the preferred context instruction (if any).
  112. static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
  113. // If we've been provided with a context instruction, then use that (provided
  114. // it has been inserted).
  115. if (CxtI && CxtI->getParent())
  116. return CxtI;
  117. // If the value is really an already-inserted instruction, then use that.
  118. CxtI = dyn_cast<Instruction>(V);
  119. if (CxtI && CxtI->getParent())
  120. return CxtI;
  121. return nullptr;
  122. }
  123. static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) {
  124. // If we've been provided with a context instruction, then use that (provided
  125. // it has been inserted).
  126. if (CxtI && CxtI->getParent())
  127. return CxtI;
  128. // If the value is really an already-inserted instruction, then use that.
  129. CxtI = dyn_cast<Instruction>(V1);
  130. if (CxtI && CxtI->getParent())
  131. return CxtI;
  132. CxtI = dyn_cast<Instruction>(V2);
  133. if (CxtI && CxtI->getParent())
  134. return CxtI;
  135. return nullptr;
  136. }
  137. static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf,
  138. const APInt &DemandedElts,
  139. APInt &DemandedLHS, APInt &DemandedRHS) {
  140. if (isa<ScalableVectorType>(Shuf->getType())) {
  141. assert(DemandedElts == APInt(1,1));
  142. DemandedLHS = DemandedRHS = DemandedElts;
  143. return true;
  144. }
  145. int NumElts =
  146. cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
  147. return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
  148. DemandedElts, DemandedLHS, DemandedRHS);
  149. }
  150. static void computeKnownBits(const Value *V, const APInt &DemandedElts,
  151. KnownBits &Known, unsigned Depth, const Query &Q);
  152. static void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
  153. const Query &Q) {
  154. // Since the number of lanes in a scalable vector is unknown at compile time,
  155. // we track one bit which is implicitly broadcast to all lanes. This means
  156. // that all lanes in a scalable vector are considered demanded.
  157. auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
  158. APInt DemandedElts =
  159. FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
  160. computeKnownBits(V, DemandedElts, Known, Depth, Q);
  161. }
  162. void llvm::computeKnownBits(const Value *V, KnownBits &Known,
  163. const DataLayout &DL, unsigned Depth,
  164. AssumptionCache *AC, const Instruction *CxtI,
  165. const DominatorTree *DT,
  166. OptimizationRemarkEmitter *ORE, bool UseInstrInfo) {
  167. ::computeKnownBits(V, Known, Depth,
  168. Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
  169. }
  170. void llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
  171. KnownBits &Known, const DataLayout &DL,
  172. unsigned Depth, AssumptionCache *AC,
  173. const Instruction *CxtI, const DominatorTree *DT,
  174. OptimizationRemarkEmitter *ORE, bool UseInstrInfo) {
  175. ::computeKnownBits(V, DemandedElts, Known, Depth,
  176. Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
  177. }
  178. static KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
  179. unsigned Depth, const Query &Q);
  180. static KnownBits computeKnownBits(const Value *V, unsigned Depth,
  181. const Query &Q);
  182. KnownBits llvm::computeKnownBits(const Value *V, const DataLayout &DL,
  183. unsigned Depth, AssumptionCache *AC,
  184. const Instruction *CxtI,
  185. const DominatorTree *DT,
  186. OptimizationRemarkEmitter *ORE,
  187. bool UseInstrInfo) {
  188. return ::computeKnownBits(
  189. V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
  190. }
  191. KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
  192. const DataLayout &DL, unsigned Depth,
  193. AssumptionCache *AC, const Instruction *CxtI,
  194. const DominatorTree *DT,
  195. OptimizationRemarkEmitter *ORE,
  196. bool UseInstrInfo) {
  197. return ::computeKnownBits(
  198. V, DemandedElts, Depth,
  199. Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo, ORE));
  200. }
  201. bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
  202. const DataLayout &DL, AssumptionCache *AC,
  203. const Instruction *CxtI, const DominatorTree *DT,
  204. bool UseInstrInfo) {
  205. assert(LHS->getType() == RHS->getType() &&
  206. "LHS and RHS should have the same type");
  207. assert(LHS->getType()->isIntOrIntVectorTy() &&
  208. "LHS and RHS should be integers");
  209. // Look for an inverted mask: (X & ~M) op (Y & M).
  210. {
  211. Value *M;
  212. if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
  213. match(RHS, m_c_And(m_Specific(M), m_Value())))
  214. return true;
  215. if (match(RHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
  216. match(LHS, m_c_And(m_Specific(M), m_Value())))
  217. return true;
  218. }
  219. // X op (Y & ~X)
  220. if (match(RHS, m_c_And(m_Not(m_Specific(LHS)), m_Value())) ||
  221. match(LHS, m_c_And(m_Not(m_Specific(RHS)), m_Value())))
  222. return true;
  223. // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
  224. // for constant Y.
  225. Value *Y;
  226. if (match(RHS,
  227. m_c_Xor(m_c_And(m_Specific(LHS), m_Value(Y)), m_Deferred(Y))) ||
  228. match(LHS, m_c_Xor(m_c_And(m_Specific(RHS), m_Value(Y)), m_Deferred(Y))))
  229. return true;
  230. // Peek through extends to find a 'not' of the other side:
  231. // (ext Y) op ext(~Y)
  232. // (ext ~Y) op ext(Y)
  233. if ((match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
  234. match(RHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))) ||
  235. (match(RHS, m_ZExtOrSExt(m_Value(Y))) &&
  236. match(LHS, m_ZExtOrSExt(m_Not(m_Specific(Y))))))
  237. return true;
  238. // Look for: (A & B) op ~(A | B)
  239. {
  240. Value *A, *B;
  241. if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
  242. match(RHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
  243. return true;
  244. if (match(RHS, m_And(m_Value(A), m_Value(B))) &&
  245. match(LHS, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
  246. return true;
  247. }
  248. IntegerType *IT = cast<IntegerType>(LHS->getType()->getScalarType());
  249. KnownBits LHSKnown(IT->getBitWidth());
  250. KnownBits RHSKnown(IT->getBitWidth());
  251. computeKnownBits(LHS, LHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
  252. computeKnownBits(RHS, RHSKnown, DL, 0, AC, CxtI, DT, nullptr, UseInstrInfo);
  253. return KnownBits::haveNoCommonBitsSet(LHSKnown, RHSKnown);
  254. }
  255. bool llvm::isOnlyUsedInZeroEqualityComparison(const Instruction *I) {
  256. return !I->user_empty() && all_of(I->users(), [](const User *U) {
  257. ICmpInst::Predicate P;
  258. return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
  259. });
  260. }
  261. static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
  262. const Query &Q);
  263. bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
  264. bool OrZero, unsigned Depth,
  265. AssumptionCache *AC, const Instruction *CxtI,
  266. const DominatorTree *DT, bool UseInstrInfo) {
  267. return ::isKnownToBeAPowerOfTwo(
  268. V, OrZero, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  269. }
  270. static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
  271. unsigned Depth, const Query &Q);
  272. static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q);
  273. bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
  274. AssumptionCache *AC, const Instruction *CxtI,
  275. const DominatorTree *DT, bool UseInstrInfo) {
  276. return ::isKnownNonZero(V, Depth,
  277. Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  278. }
  279. bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
  280. unsigned Depth, AssumptionCache *AC,
  281. const Instruction *CxtI, const DominatorTree *DT,
  282. bool UseInstrInfo) {
  283. KnownBits Known =
  284. computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
  285. return Known.isNonNegative();
  286. }
  287. bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
  288. AssumptionCache *AC, const Instruction *CxtI,
  289. const DominatorTree *DT, bool UseInstrInfo) {
  290. if (auto *CI = dyn_cast<ConstantInt>(V))
  291. return CI->getValue().isStrictlyPositive();
  292. // TODO: We'd doing two recursive queries here. We should factor this such
  293. // that only a single query is needed.
  294. return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT, UseInstrInfo) &&
  295. isKnownNonZero(V, DL, Depth, AC, CxtI, DT, UseInstrInfo);
  296. }
  297. bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth,
  298. AssumptionCache *AC, const Instruction *CxtI,
  299. const DominatorTree *DT, bool UseInstrInfo) {
  300. KnownBits Known =
  301. computeKnownBits(V, DL, Depth, AC, CxtI, DT, nullptr, UseInstrInfo);
  302. return Known.isNegative();
  303. }
  304. static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
  305. const Query &Q);
  306. bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
  307. const DataLayout &DL, AssumptionCache *AC,
  308. const Instruction *CxtI, const DominatorTree *DT,
  309. bool UseInstrInfo) {
  310. return ::isKnownNonEqual(V1, V2, 0,
  311. Query(DL, AC, safeCxtI(V2, V1, CxtI), DT,
  312. UseInstrInfo, /*ORE=*/nullptr));
  313. }
  314. static bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
  315. const Query &Q);
  316. bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
  317. const DataLayout &DL, unsigned Depth,
  318. AssumptionCache *AC, const Instruction *CxtI,
  319. const DominatorTree *DT, bool UseInstrInfo) {
  320. return ::MaskedValueIsZero(
  321. V, Mask, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  322. }
  323. static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
  324. unsigned Depth, const Query &Q);
  325. static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
  326. const Query &Q) {
  327. auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
  328. APInt DemandedElts =
  329. FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
  330. return ComputeNumSignBits(V, DemandedElts, Depth, Q);
  331. }
  332. unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
  333. unsigned Depth, AssumptionCache *AC,
  334. const Instruction *CxtI,
  335. const DominatorTree *DT, bool UseInstrInfo) {
  336. return ::ComputeNumSignBits(
  337. V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT, UseInstrInfo));
  338. }
  339. unsigned llvm::ComputeMaxSignificantBits(const Value *V, const DataLayout &DL,
  340. unsigned Depth, AssumptionCache *AC,
  341. const Instruction *CxtI,
  342. const DominatorTree *DT) {
  343. unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
  344. return V->getType()->getScalarSizeInBits() - SignBits + 1;
  345. }
  346. static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
  347. bool NSW, const APInt &DemandedElts,
  348. KnownBits &KnownOut, KnownBits &Known2,
  349. unsigned Depth, const Query &Q) {
  350. computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
  351. // If one operand is unknown and we have no nowrap information,
  352. // the result will be unknown independently of the second operand.
  353. if (KnownOut.isUnknown() && !NSW)
  354. return;
  355. computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
  356. KnownOut = KnownBits::computeForAddSub(Add, NSW, Known2, KnownOut);
  357. }
  358. static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
  359. const APInt &DemandedElts, KnownBits &Known,
  360. KnownBits &Known2, unsigned Depth,
  361. const Query &Q) {
  362. computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q);
  363. computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
  364. bool isKnownNegative = false;
  365. bool isKnownNonNegative = false;
  366. // If the multiplication is known not to overflow, compute the sign bit.
  367. if (NSW) {
  368. if (Op0 == Op1) {
  369. // The product of a number with itself is non-negative.
  370. isKnownNonNegative = true;
  371. } else {
  372. bool isKnownNonNegativeOp1 = Known.isNonNegative();
  373. bool isKnownNonNegativeOp0 = Known2.isNonNegative();
  374. bool isKnownNegativeOp1 = Known.isNegative();
  375. bool isKnownNegativeOp0 = Known2.isNegative();
  376. // The product of two numbers with the same sign is non-negative.
  377. isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
  378. (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
  379. // The product of a negative number and a non-negative number is either
  380. // negative or zero.
  381. if (!isKnownNonNegative)
  382. isKnownNegative =
  383. (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
  384. Known2.isNonZero()) ||
  385. (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
  386. }
  387. }
  388. bool SelfMultiply = Op0 == Op1;
  389. // TODO: SelfMultiply can be poison, but not undef.
  390. if (SelfMultiply)
  391. SelfMultiply &=
  392. isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
  393. Known = KnownBits::mul(Known, Known2, SelfMultiply);
  394. // Only make use of no-wrap flags if we failed to compute the sign bit
  395. // directly. This matters if the multiplication always overflows, in
  396. // which case we prefer to follow the result of the direct computation,
  397. // though as the program is invoking undefined behaviour we can choose
  398. // whatever we like here.
  399. if (isKnownNonNegative && !Known.isNegative())
  400. Known.makeNonNegative();
  401. else if (isKnownNegative && !Known.isNonNegative())
  402. Known.makeNegative();
  403. }
  404. void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
  405. KnownBits &Known) {
  406. unsigned BitWidth = Known.getBitWidth();
  407. unsigned NumRanges = Ranges.getNumOperands() / 2;
  408. assert(NumRanges >= 1);
  409. Known.Zero.setAllBits();
  410. Known.One.setAllBits();
  411. for (unsigned i = 0; i < NumRanges; ++i) {
  412. ConstantInt *Lower =
  413. mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
  414. ConstantInt *Upper =
  415. mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
  416. ConstantRange Range(Lower->getValue(), Upper->getValue());
  417. // The first CommonPrefixBits of all values in Range are equal.
  418. unsigned CommonPrefixBits =
  419. (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countLeadingZeros();
  420. APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
  421. APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
  422. Known.One &= UnsignedMax & Mask;
  423. Known.Zero &= ~UnsignedMax & Mask;
  424. }
  425. }
  426. static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
  427. SmallVector<const Value *, 16> WorkSet(1, I);
  428. SmallPtrSet<const Value *, 32> Visited;
  429. SmallPtrSet<const Value *, 16> EphValues;
  430. // The instruction defining an assumption's condition itself is always
  431. // considered ephemeral to that assumption (even if it has other
  432. // non-ephemeral users). See r246696's test case for an example.
  433. if (is_contained(I->operands(), E))
  434. return true;
  435. while (!WorkSet.empty()) {
  436. const Value *V = WorkSet.pop_back_val();
  437. if (!Visited.insert(V).second)
  438. continue;
  439. // If all uses of this value are ephemeral, then so is this value.
  440. if (llvm::all_of(V->users(), [&](const User *U) {
  441. return EphValues.count(U);
  442. })) {
  443. if (V == E)
  444. return true;
  445. if (V == I || (isa<Instruction>(V) &&
  446. !cast<Instruction>(V)->mayHaveSideEffects() &&
  447. !cast<Instruction>(V)->isTerminator())) {
  448. EphValues.insert(V);
  449. if (const User *U = dyn_cast<User>(V))
  450. append_range(WorkSet, U->operands());
  451. }
  452. }
  453. }
  454. return false;
  455. }
  456. // Is this an intrinsic that cannot be speculated but also cannot trap?
  457. bool llvm::isAssumeLikeIntrinsic(const Instruction *I) {
  458. if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
  459. return CI->isAssumeLikeIntrinsic();
  460. return false;
  461. }
  462. bool llvm::isValidAssumeForContext(const Instruction *Inv,
  463. const Instruction *CxtI,
  464. const DominatorTree *DT) {
  465. // There are two restrictions on the use of an assume:
  466. // 1. The assume must dominate the context (or the control flow must
  467. // reach the assume whenever it reaches the context).
  468. // 2. The context must not be in the assume's set of ephemeral values
  469. // (otherwise we will use the assume to prove that the condition
  470. // feeding the assume is trivially true, thus causing the removal of
  471. // the assume).
  472. if (Inv->getParent() == CxtI->getParent()) {
  473. // If Inv and CtxI are in the same block, check if the assume (Inv) is first
  474. // in the BB.
  475. if (Inv->comesBefore(CxtI))
  476. return true;
  477. // Don't let an assume affect itself - this would cause the problems
  478. // `isEphemeralValueOf` is trying to prevent, and it would also make
  479. // the loop below go out of bounds.
  480. if (Inv == CxtI)
  481. return false;
  482. // The context comes first, but they're both in the same block.
  483. // Make sure there is nothing in between that might interrupt
  484. // the control flow, not even CxtI itself.
  485. // We limit the scan distance between the assume and its context instruction
  486. // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
  487. // it can be adjusted if needed (could be turned into a cl::opt).
  488. auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
  489. if (!isGuaranteedToTransferExecutionToSuccessor(Range, 15))
  490. return false;
  491. return !isEphemeralValueOf(Inv, CxtI);
  492. }
  493. // Inv and CxtI are in different blocks.
  494. if (DT) {
  495. if (DT->dominates(Inv, CxtI))
  496. return true;
  497. } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
  498. // We don't have a DT, but this trivially dominates.
  499. return true;
  500. }
  501. return false;
  502. }
  503. static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
  504. // v u> y implies v != 0.
  505. if (Pred == ICmpInst::ICMP_UGT)
  506. return true;
  507. // Special-case v != 0 to also handle v != null.
  508. if (Pred == ICmpInst::ICMP_NE)
  509. return match(RHS, m_Zero());
  510. // All other predicates - rely on generic ConstantRange handling.
  511. const APInt *C;
  512. if (!match(RHS, m_APInt(C)))
  513. return false;
  514. ConstantRange TrueValues = ConstantRange::makeExactICmpRegion(Pred, *C);
  515. return !TrueValues.contains(APInt::getZero(C->getBitWidth()));
  516. }
  517. static bool isKnownNonZeroFromAssume(const Value *V, const Query &Q) {
  518. // Use of assumptions is context-sensitive. If we don't have a context, we
  519. // cannot use them!
  520. if (!Q.AC || !Q.CxtI)
  521. return false;
  522. if (Q.CxtI && V->getType()->isPointerTy()) {
  523. SmallVector<Attribute::AttrKind, 2> AttrKinds{Attribute::NonNull};
  524. if (!NullPointerIsDefined(Q.CxtI->getFunction(),
  525. V->getType()->getPointerAddressSpace()))
  526. AttrKinds.push_back(Attribute::Dereferenceable);
  527. if (getKnowledgeValidInContext(V, AttrKinds, Q.CxtI, Q.DT, Q.AC))
  528. return true;
  529. }
  530. for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
  531. if (!AssumeVH)
  532. continue;
  533. CondGuardInst *I = cast<CondGuardInst>(AssumeVH);
  534. assert(I->getFunction() == Q.CxtI->getFunction() &&
  535. "Got assumption for the wrong function!");
  536. // Warning: This loop can end up being somewhat performance sensitive.
  537. // We're running this loop for once for each value queried resulting in a
  538. // runtime of ~O(#assumes * #values).
  539. Value *RHS;
  540. CmpInst::Predicate Pred;
  541. auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
  542. if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
  543. return false;
  544. if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
  545. return true;
  546. }
  547. return false;
  548. }
  549. static void computeKnownBitsFromAssume(const Value *V, KnownBits &Known,
  550. unsigned Depth, const Query &Q) {
  551. // Use of assumptions is context-sensitive. If we don't have a context, we
  552. // cannot use them!
  553. if (!Q.AC || !Q.CxtI)
  554. return;
  555. unsigned BitWidth = Known.getBitWidth();
  556. // Refine Known set if the pointer alignment is set by assume bundles.
  557. if (V->getType()->isPointerTy()) {
  558. if (RetainedKnowledge RK = getKnowledgeValidInContext(
  559. V, {Attribute::Alignment}, Q.CxtI, Q.DT, Q.AC)) {
  560. if (isPowerOf2_64(RK.ArgValue))
  561. Known.Zero.setLowBits(Log2_64(RK.ArgValue));
  562. }
  563. }
  564. // Note that the patterns below need to be kept in sync with the code
  565. // in AssumptionCache::updateAffectedValues.
  566. for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
  567. if (!AssumeVH)
  568. continue;
  569. CondGuardInst *I = cast<CondGuardInst>(AssumeVH);
  570. assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
  571. "Got assumption for the wrong function!");
  572. // Warning: This loop can end up being somewhat performance sensitive.
  573. // We're running this loop for once for each value queried resulting in a
  574. // runtime of ~O(#assumes * #values).
  575. Value *Arg = I->getArgOperand(0);
  576. if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  577. assert(BitWidth == 1 && "assume operand is not i1?");
  578. Known.setAllOnes();
  579. return;
  580. }
  581. if (match(Arg, m_Not(m_Specific(V))) &&
  582. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  583. assert(BitWidth == 1 && "assume operand is not i1?");
  584. Known.setAllZero();
  585. return;
  586. }
  587. // The remaining tests are all recursive, so bail out if we hit the limit.
  588. if (Depth == MaxAnalysisRecursionDepth)
  589. continue;
  590. ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
  591. if (!Cmp)
  592. continue;
  593. // We are attempting to compute known bits for the operands of an assume.
  594. // Do not try to use other assumptions for those recursive calls because
  595. // that can lead to mutual recursion and a compile-time explosion.
  596. // An example of the mutual recursion: computeKnownBits can call
  597. // isKnownNonZero which calls computeKnownBitsFromAssume (this function)
  598. // and so on.
  599. Query QueryNoAC = Q;
  600. QueryNoAC.AC = nullptr;
  601. // Note that ptrtoint may change the bitwidth.
  602. Value *A, *B;
  603. auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
  604. CmpInst::Predicate Pred;
  605. uint64_t C;
  606. switch (Cmp->getPredicate()) {
  607. default:
  608. break;
  609. case ICmpInst::ICMP_EQ:
  610. // assume(v = a)
  611. if (match(Cmp, m_c_ICmp(Pred, m_V, m_Value(A))) &&
  612. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  613. KnownBits RHSKnown =
  614. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  615. Known.Zero |= RHSKnown.Zero;
  616. Known.One |= RHSKnown.One;
  617. // assume(v & b = a)
  618. } else if (match(Cmp,
  619. m_c_ICmp(Pred, m_c_And(m_V, m_Value(B)), m_Value(A))) &&
  620. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  621. KnownBits RHSKnown =
  622. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  623. KnownBits MaskKnown =
  624. computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  625. // For those bits in the mask that are known to be one, we can propagate
  626. // known bits from the RHS to V.
  627. Known.Zero |= RHSKnown.Zero & MaskKnown.One;
  628. Known.One |= RHSKnown.One & MaskKnown.One;
  629. // assume(~(v & b) = a)
  630. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_And(m_V, m_Value(B))),
  631. m_Value(A))) &&
  632. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  633. KnownBits RHSKnown =
  634. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  635. KnownBits MaskKnown =
  636. computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  637. // For those bits in the mask that are known to be one, we can propagate
  638. // inverted known bits from the RHS to V.
  639. Known.Zero |= RHSKnown.One & MaskKnown.One;
  640. Known.One |= RHSKnown.Zero & MaskKnown.One;
  641. // assume(v | b = a)
  642. } else if (match(Cmp,
  643. m_c_ICmp(Pred, m_c_Or(m_V, m_Value(B)), m_Value(A))) &&
  644. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  645. KnownBits RHSKnown =
  646. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  647. KnownBits BKnown =
  648. computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  649. // For those bits in B that are known to be zero, we can propagate known
  650. // bits from the RHS to V.
  651. Known.Zero |= RHSKnown.Zero & BKnown.Zero;
  652. Known.One |= RHSKnown.One & BKnown.Zero;
  653. // assume(~(v | b) = a)
  654. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Or(m_V, m_Value(B))),
  655. m_Value(A))) &&
  656. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  657. KnownBits RHSKnown =
  658. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  659. KnownBits BKnown =
  660. computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  661. // For those bits in B that are known to be zero, we can propagate
  662. // inverted known bits from the RHS to V.
  663. Known.Zero |= RHSKnown.One & BKnown.Zero;
  664. Known.One |= RHSKnown.Zero & BKnown.Zero;
  665. // assume(v ^ b = a)
  666. } else if (match(Cmp,
  667. m_c_ICmp(Pred, m_c_Xor(m_V, m_Value(B)), m_Value(A))) &&
  668. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  669. KnownBits RHSKnown =
  670. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  671. KnownBits BKnown =
  672. computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  673. // For those bits in B that are known to be zero, we can propagate known
  674. // bits from the RHS to V. For those bits in B that are known to be one,
  675. // we can propagate inverted known bits from the RHS to V.
  676. Known.Zero |= RHSKnown.Zero & BKnown.Zero;
  677. Known.One |= RHSKnown.One & BKnown.Zero;
  678. Known.Zero |= RHSKnown.One & BKnown.One;
  679. Known.One |= RHSKnown.Zero & BKnown.One;
  680. // assume(~(v ^ b) = a)
  681. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_c_Xor(m_V, m_Value(B))),
  682. m_Value(A))) &&
  683. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  684. KnownBits RHSKnown =
  685. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  686. KnownBits BKnown =
  687. computeKnownBits(B, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  688. // For those bits in B that are known to be zero, we can propagate
  689. // inverted known bits from the RHS to V. For those bits in B that are
  690. // known to be one, we can propagate known bits from the RHS to V.
  691. Known.Zero |= RHSKnown.One & BKnown.Zero;
  692. Known.One |= RHSKnown.Zero & BKnown.Zero;
  693. Known.Zero |= RHSKnown.Zero & BKnown.One;
  694. Known.One |= RHSKnown.One & BKnown.One;
  695. // assume(v << c = a)
  696. } else if (match(Cmp, m_c_ICmp(Pred, m_Shl(m_V, m_ConstantInt(C)),
  697. m_Value(A))) &&
  698. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  699. KnownBits RHSKnown =
  700. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  701. // For those bits in RHS that are known, we can propagate them to known
  702. // bits in V shifted to the right by C.
  703. RHSKnown.Zero.lshrInPlace(C);
  704. Known.Zero |= RHSKnown.Zero;
  705. RHSKnown.One.lshrInPlace(C);
  706. Known.One |= RHSKnown.One;
  707. // assume(~(v << c) = a)
  708. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shl(m_V, m_ConstantInt(C))),
  709. m_Value(A))) &&
  710. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  711. KnownBits RHSKnown =
  712. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  713. // For those bits in RHS that are known, we can propagate them inverted
  714. // to known bits in V shifted to the right by C.
  715. RHSKnown.One.lshrInPlace(C);
  716. Known.Zero |= RHSKnown.One;
  717. RHSKnown.Zero.lshrInPlace(C);
  718. Known.One |= RHSKnown.Zero;
  719. // assume(v >> c = a)
  720. } else if (match(Cmp, m_c_ICmp(Pred, m_Shr(m_V, m_ConstantInt(C)),
  721. m_Value(A))) &&
  722. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  723. KnownBits RHSKnown =
  724. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  725. // For those bits in RHS that are known, we can propagate them to known
  726. // bits in V shifted to the right by C.
  727. Known.Zero |= RHSKnown.Zero << C;
  728. Known.One |= RHSKnown.One << C;
  729. // assume(~(v >> c) = a)
  730. } else if (match(Cmp, m_c_ICmp(Pred, m_Not(m_Shr(m_V, m_ConstantInt(C))),
  731. m_Value(A))) &&
  732. isValidAssumeForContext(I, Q.CxtI, Q.DT) && C < BitWidth) {
  733. KnownBits RHSKnown =
  734. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  735. // For those bits in RHS that are known, we can propagate them inverted
  736. // to known bits in V shifted to the right by C.
  737. Known.Zero |= RHSKnown.One << C;
  738. Known.One |= RHSKnown.Zero << C;
  739. }
  740. break;
  741. case ICmpInst::ICMP_SGE:
  742. // assume(v >=_s c) where c is non-negative
  743. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  744. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  745. KnownBits RHSKnown =
  746. computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth);
  747. if (RHSKnown.isNonNegative()) {
  748. // We know that the sign bit is zero.
  749. Known.makeNonNegative();
  750. }
  751. }
  752. break;
  753. case ICmpInst::ICMP_SGT:
  754. // assume(v >_s c) where c is at least -1.
  755. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  756. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  757. KnownBits RHSKnown =
  758. computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth);
  759. if (RHSKnown.isAllOnes() || RHSKnown.isNonNegative()) {
  760. // We know that the sign bit is zero.
  761. Known.makeNonNegative();
  762. }
  763. }
  764. break;
  765. case ICmpInst::ICMP_SLE:
  766. // assume(v <=_s c) where c is negative
  767. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  768. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  769. KnownBits RHSKnown =
  770. computeKnownBits(A, Depth + 1, QueryNoAC).anyextOrTrunc(BitWidth);
  771. if (RHSKnown.isNegative()) {
  772. // We know that the sign bit is one.
  773. Known.makeNegative();
  774. }
  775. }
  776. break;
  777. case ICmpInst::ICMP_SLT:
  778. // assume(v <_s c) where c is non-positive
  779. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  780. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  781. KnownBits RHSKnown =
  782. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  783. if (RHSKnown.isZero() || RHSKnown.isNegative()) {
  784. // We know that the sign bit is one.
  785. Known.makeNegative();
  786. }
  787. }
  788. break;
  789. case ICmpInst::ICMP_ULE:
  790. // assume(v <=_u c)
  791. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  792. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  793. KnownBits RHSKnown =
  794. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  795. // Whatever high bits in c are zero are known to be zero.
  796. Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
  797. }
  798. break;
  799. case ICmpInst::ICMP_ULT:
  800. // assume(v <_u c)
  801. if (match(Cmp, m_ICmp(Pred, m_V, m_Value(A))) &&
  802. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  803. KnownBits RHSKnown =
  804. computeKnownBits(A, Depth+1, QueryNoAC).anyextOrTrunc(BitWidth);
  805. // If the RHS is known zero, then this assumption must be wrong (nothing
  806. // is unsigned less than zero). Signal a conflict and get out of here.
  807. if (RHSKnown.isZero()) {
  808. Known.Zero.setAllBits();
  809. Known.One.setAllBits();
  810. break;
  811. }
  812. // Whatever high bits in c are zero are known to be zero (if c is a power
  813. // of 2, then one more).
  814. if (isKnownToBeAPowerOfTwo(A, false, Depth + 1, QueryNoAC))
  815. Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros() + 1);
  816. else
  817. Known.Zero.setHighBits(RHSKnown.countMinLeadingZeros());
  818. }
  819. break;
  820. case ICmpInst::ICMP_NE: {
  821. // assume (v & b != 0) where b is a power of 2
  822. const APInt *BPow2;
  823. if (match(Cmp, m_ICmp(Pred, m_c_And(m_V, m_Power2(BPow2)), m_Zero())) &&
  824. isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
  825. Known.One |= BPow2->zextOrTrunc(BitWidth);
  826. }
  827. } break;
  828. }
  829. }
  830. // If assumptions conflict with each other or previous known bits, then we
  831. // have a logical fallacy. It's possible that the assumption is not reachable,
  832. // so this isn't a real bug. On the other hand, the program may have undefined
  833. // behavior, or we might have a bug in the compiler. We can't assert/crash, so
  834. // clear out the known bits, try to warn the user, and hope for the best.
  835. if (Known.Zero.intersects(Known.One)) {
  836. Known.resetAll();
  837. if (Q.ORE)
  838. Q.ORE->emit([&]() {
  839. auto *CxtI = const_cast<Instruction *>(Q.CxtI);
  840. return OptimizationRemarkAnalysis("value-tracking", "BadAssumption",
  841. CxtI)
  842. << "Detected conflicting code assumptions. Program may "
  843. "have undefined behavior, or compiler may have "
  844. "internal error.";
  845. });
  846. }
  847. }
  848. /// Compute known bits from a shift operator, including those with a
  849. /// non-constant shift amount. Known is the output of this function. Known2 is a
  850. /// pre-allocated temporary with the same bit width as Known and on return
  851. /// contains the known bit of the shift value source. KF is an
  852. /// operator-specific function that, given the known-bits and a shift amount,
  853. /// compute the implied known-bits of the shift operator's result respectively
  854. /// for that shift amount. The results from calling KF are conservatively
  855. /// combined for all permitted shift amounts.
  856. static void computeKnownBitsFromShiftOperator(
  857. const Operator *I, const APInt &DemandedElts, KnownBits &Known,
  858. KnownBits &Known2, unsigned Depth, const Query &Q,
  859. function_ref<KnownBits(const KnownBits &, const KnownBits &)> KF) {
  860. unsigned BitWidth = Known.getBitWidth();
  861. computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
  862. computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
  863. // Note: We cannot use Known.Zero.getLimitedValue() here, because if
  864. // BitWidth > 64 and any upper bits are known, we'll end up returning the
  865. // limit value (which implies all bits are known).
  866. uint64_t ShiftAmtKZ = Known.Zero.zextOrTrunc(64).getZExtValue();
  867. uint64_t ShiftAmtKO = Known.One.zextOrTrunc(64).getZExtValue();
  868. bool ShiftAmtIsConstant = Known.isConstant();
  869. bool MaxShiftAmtIsOutOfRange = Known.getMaxValue().uge(BitWidth);
  870. if (ShiftAmtIsConstant) {
  871. Known = KF(Known2, Known);
  872. // If the known bits conflict, this must be an overflowing left shift, so
  873. // the shift result is poison. We can return anything we want. Choose 0 for
  874. // the best folding opportunity.
  875. if (Known.hasConflict())
  876. Known.setAllZero();
  877. return;
  878. }
  879. // If the shift amount could be greater than or equal to the bit-width of the
  880. // LHS, the value could be poison, but bail out because the check below is
  881. // expensive.
  882. // TODO: Should we just carry on?
  883. if (MaxShiftAmtIsOutOfRange) {
  884. Known.resetAll();
  885. return;
  886. }
  887. // It would be more-clearly correct to use the two temporaries for this
  888. // calculation. Reusing the APInts here to prevent unnecessary allocations.
  889. Known.resetAll();
  890. // If we know the shifter operand is nonzero, we can sometimes infer more
  891. // known bits. However this is expensive to compute, so be lazy about it and
  892. // only compute it when absolutely necessary.
  893. std::optional<bool> ShifterOperandIsNonZero;
  894. // Early exit if we can't constrain any well-defined shift amount.
  895. if (!(ShiftAmtKZ & (PowerOf2Ceil(BitWidth) - 1)) &&
  896. !(ShiftAmtKO & (PowerOf2Ceil(BitWidth) - 1))) {
  897. ShifterOperandIsNonZero =
  898. isKnownNonZero(I->getOperand(1), DemandedElts, Depth + 1, Q);
  899. if (!*ShifterOperandIsNonZero)
  900. return;
  901. }
  902. Known.Zero.setAllBits();
  903. Known.One.setAllBits();
  904. for (unsigned ShiftAmt = 0; ShiftAmt < BitWidth; ++ShiftAmt) {
  905. // Combine the shifted known input bits only for those shift amounts
  906. // compatible with its known constraints.
  907. if ((ShiftAmt & ~ShiftAmtKZ) != ShiftAmt)
  908. continue;
  909. if ((ShiftAmt | ShiftAmtKO) != ShiftAmt)
  910. continue;
  911. // If we know the shifter is nonzero, we may be able to infer more known
  912. // bits. This check is sunk down as far as possible to avoid the expensive
  913. // call to isKnownNonZero if the cheaper checks above fail.
  914. if (ShiftAmt == 0) {
  915. if (!ShifterOperandIsNonZero)
  916. ShifterOperandIsNonZero =
  917. isKnownNonZero(I->getOperand(1), DemandedElts, Depth + 1, Q);
  918. if (*ShifterOperandIsNonZero)
  919. continue;
  920. }
  921. Known = KnownBits::commonBits(
  922. Known, KF(Known2, KnownBits::makeConstant(APInt(32, ShiftAmt))));
  923. }
  924. // If the known bits conflict, the result is poison. Return a 0 and hope the
  925. // caller can further optimize that.
  926. if (Known.hasConflict())
  927. Known.setAllZero();
  928. }
  929. static void computeKnownBitsFromOperator(const Operator *I,
  930. const APInt &DemandedElts,
  931. KnownBits &Known, unsigned Depth,
  932. const Query &Q) {
  933. unsigned BitWidth = Known.getBitWidth();
  934. KnownBits Known2(BitWidth);
  935. switch (I->getOpcode()) {
  936. default: break;
  937. case Instruction::Load:
  938. if (MDNode *MD =
  939. Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
  940. computeKnownBitsFromRangeMetadata(*MD, Known);
  941. break;
  942. case Instruction::And: {
  943. // If either the LHS or the RHS are Zero, the result is zero.
  944. computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
  945. computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
  946. Known &= Known2;
  947. // and(x, add (x, -1)) is a common idiom that always clears the low bit;
  948. // here we handle the more general case of adding any odd number by
  949. // matching the form add(x, add(x, y)) where y is odd.
  950. // TODO: This could be generalized to clearing any bit set in y where the
  951. // following bit is known to be unset in y.
  952. Value *X = nullptr, *Y = nullptr;
  953. if (!Known.Zero[0] && !Known.One[0] &&
  954. match(I, m_c_BinOp(m_Value(X), m_Add(m_Deferred(X), m_Value(Y))))) {
  955. Known2.resetAll();
  956. computeKnownBits(Y, DemandedElts, Known2, Depth + 1, Q);
  957. if (Known2.countMinTrailingOnes() > 0)
  958. Known.Zero.setBit(0);
  959. }
  960. break;
  961. }
  962. case Instruction::Or:
  963. computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
  964. computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
  965. Known |= Known2;
  966. break;
  967. case Instruction::Xor:
  968. computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
  969. computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
  970. Known ^= Known2;
  971. break;
  972. case Instruction::Mul: {
  973. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  974. computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, DemandedElts,
  975. Known, Known2, Depth, Q);
  976. break;
  977. }
  978. case Instruction::UDiv: {
  979. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  980. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  981. Known = KnownBits::udiv(Known, Known2);
  982. break;
  983. }
  984. case Instruction::Select: {
  985. const Value *LHS = nullptr, *RHS = nullptr;
  986. SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
  987. if (SelectPatternResult::isMinOrMax(SPF)) {
  988. computeKnownBits(RHS, Known, Depth + 1, Q);
  989. computeKnownBits(LHS, Known2, Depth + 1, Q);
  990. switch (SPF) {
  991. default:
  992. llvm_unreachable("Unhandled select pattern flavor!");
  993. case SPF_SMAX:
  994. Known = KnownBits::smax(Known, Known2);
  995. break;
  996. case SPF_SMIN:
  997. Known = KnownBits::smin(Known, Known2);
  998. break;
  999. case SPF_UMAX:
  1000. Known = KnownBits::umax(Known, Known2);
  1001. break;
  1002. case SPF_UMIN:
  1003. Known = KnownBits::umin(Known, Known2);
  1004. break;
  1005. }
  1006. break;
  1007. }
  1008. computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
  1009. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1010. // Only known if known in both the LHS and RHS.
  1011. Known = KnownBits::commonBits(Known, Known2);
  1012. if (SPF == SPF_ABS) {
  1013. // RHS from matchSelectPattern returns the negation part of abs pattern.
  1014. // If the negate has an NSW flag we can assume the sign bit of the result
  1015. // will be 0 because that makes abs(INT_MIN) undefined.
  1016. if (match(RHS, m_Neg(m_Specific(LHS))) &&
  1017. Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RHS)))
  1018. Known.Zero.setSignBit();
  1019. }
  1020. break;
  1021. }
  1022. case Instruction::FPTrunc:
  1023. case Instruction::FPExt:
  1024. case Instruction::FPToUI:
  1025. case Instruction::FPToSI:
  1026. case Instruction::SIToFP:
  1027. case Instruction::UIToFP:
  1028. break; // Can't work with floating point.
  1029. case Instruction::PtrToInt:
  1030. case Instruction::IntToPtr:
  1031. // Fall through and handle them the same as zext/trunc.
  1032. [[fallthrough]];
  1033. case Instruction::ZExt:
  1034. case Instruction::Trunc: {
  1035. Type *SrcTy = I->getOperand(0)->getType();
  1036. unsigned SrcBitWidth;
  1037. // Note that we handle pointer operands here because of inttoptr/ptrtoint
  1038. // which fall through here.
  1039. Type *ScalarTy = SrcTy->getScalarType();
  1040. SrcBitWidth = ScalarTy->isPointerTy() ?
  1041. Q.DL.getPointerTypeSizeInBits(ScalarTy) :
  1042. Q.DL.getTypeSizeInBits(ScalarTy);
  1043. assert(SrcBitWidth && "SrcBitWidth can't be zero");
  1044. Known = Known.anyextOrTrunc(SrcBitWidth);
  1045. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1046. Known = Known.zextOrTrunc(BitWidth);
  1047. break;
  1048. }
  1049. case Instruction::BitCast: {
  1050. Type *SrcTy = I->getOperand(0)->getType();
  1051. if (SrcTy->isIntOrPtrTy() &&
  1052. // TODO: For now, not handling conversions like:
  1053. // (bitcast i64 %x to <2 x i32>)
  1054. !I->getType()->isVectorTy()) {
  1055. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1056. break;
  1057. }
  1058. // Handle cast from vector integer type to scalar or vector integer.
  1059. auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
  1060. if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
  1061. !I->getType()->isIntOrIntVectorTy() ||
  1062. isa<ScalableVectorType>(I->getType()))
  1063. break;
  1064. // Look through a cast from narrow vector elements to wider type.
  1065. // Examples: v4i32 -> v2i64, v3i8 -> v24
  1066. unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
  1067. if (BitWidth % SubBitWidth == 0) {
  1068. // Known bits are automatically intersected across demanded elements of a
  1069. // vector. So for example, if a bit is computed as known zero, it must be
  1070. // zero across all demanded elements of the vector.
  1071. //
  1072. // For this bitcast, each demanded element of the output is sub-divided
  1073. // across a set of smaller vector elements in the source vector. To get
  1074. // the known bits for an entire element of the output, compute the known
  1075. // bits for each sub-element sequentially. This is done by shifting the
  1076. // one-set-bit demanded elements parameter across the sub-elements for
  1077. // consecutive calls to computeKnownBits. We are using the demanded
  1078. // elements parameter as a mask operator.
  1079. //
  1080. // The known bits of each sub-element are then inserted into place
  1081. // (dependent on endian) to form the full result of known bits.
  1082. unsigned NumElts = DemandedElts.getBitWidth();
  1083. unsigned SubScale = BitWidth / SubBitWidth;
  1084. APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
  1085. for (unsigned i = 0; i != NumElts; ++i) {
  1086. if (DemandedElts[i])
  1087. SubDemandedElts.setBit(i * SubScale);
  1088. }
  1089. KnownBits KnownSrc(SubBitWidth);
  1090. for (unsigned i = 0; i != SubScale; ++i) {
  1091. computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
  1092. Depth + 1, Q);
  1093. unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
  1094. Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
  1095. }
  1096. }
  1097. break;
  1098. }
  1099. case Instruction::SExt: {
  1100. // Compute the bits in the result that are not present in the input.
  1101. unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
  1102. Known = Known.trunc(SrcBitWidth);
  1103. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1104. // If the sign bit of the input is known set or clear, then we know the
  1105. // top bits of the result.
  1106. Known = Known.sext(BitWidth);
  1107. break;
  1108. }
  1109. case Instruction::Shl: {
  1110. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  1111. auto KF = [NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt) {
  1112. KnownBits Result = KnownBits::shl(KnownVal, KnownAmt);
  1113. // If this shift has "nsw" keyword, then the result is either a poison
  1114. // value or has the same sign bit as the first operand.
  1115. if (NSW) {
  1116. if (KnownVal.Zero.isSignBitSet())
  1117. Result.Zero.setSignBit();
  1118. if (KnownVal.One.isSignBitSet())
  1119. Result.One.setSignBit();
  1120. }
  1121. return Result;
  1122. };
  1123. computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
  1124. KF);
  1125. // Trailing zeros of a right-shifted constant never decrease.
  1126. const APInt *C;
  1127. if (match(I->getOperand(0), m_APInt(C)))
  1128. Known.Zero.setLowBits(C->countTrailingZeros());
  1129. break;
  1130. }
  1131. case Instruction::LShr: {
  1132. auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt) {
  1133. return KnownBits::lshr(KnownVal, KnownAmt);
  1134. };
  1135. computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
  1136. KF);
  1137. // Leading zeros of a left-shifted constant never decrease.
  1138. const APInt *C;
  1139. if (match(I->getOperand(0), m_APInt(C)))
  1140. Known.Zero.setHighBits(C->countLeadingZeros());
  1141. break;
  1142. }
  1143. case Instruction::AShr: {
  1144. auto KF = [](const KnownBits &KnownVal, const KnownBits &KnownAmt) {
  1145. return KnownBits::ashr(KnownVal, KnownAmt);
  1146. };
  1147. computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
  1148. KF);
  1149. break;
  1150. }
  1151. case Instruction::Sub: {
  1152. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  1153. computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW,
  1154. DemandedElts, Known, Known2, Depth, Q);
  1155. break;
  1156. }
  1157. case Instruction::Add: {
  1158. bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
  1159. computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW,
  1160. DemandedElts, Known, Known2, Depth, Q);
  1161. break;
  1162. }
  1163. case Instruction::SRem:
  1164. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1165. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1166. Known = KnownBits::srem(Known, Known2);
  1167. break;
  1168. case Instruction::URem:
  1169. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1170. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1171. Known = KnownBits::urem(Known, Known2);
  1172. break;
  1173. case Instruction::Alloca:
  1174. Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
  1175. break;
  1176. case Instruction::GetElementPtr: {
  1177. // Analyze all of the subscripts of this getelementptr instruction
  1178. // to determine if we can prove known low zero bits.
  1179. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1180. // Accumulate the constant indices in a separate variable
  1181. // to minimize the number of calls to computeForAddSub.
  1182. APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true);
  1183. gep_type_iterator GTI = gep_type_begin(I);
  1184. for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
  1185. // TrailZ can only become smaller, short-circuit if we hit zero.
  1186. if (Known.isUnknown())
  1187. break;
  1188. Value *Index = I->getOperand(i);
  1189. // Handle case when index is zero.
  1190. Constant *CIndex = dyn_cast<Constant>(Index);
  1191. if (CIndex && CIndex->isZeroValue())
  1192. continue;
  1193. if (StructType *STy = GTI.getStructTypeOrNull()) {
  1194. // Handle struct member offset arithmetic.
  1195. assert(CIndex &&
  1196. "Access to structure field must be known at compile time");
  1197. if (CIndex->getType()->isVectorTy())
  1198. Index = CIndex->getSplatValue();
  1199. unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
  1200. const StructLayout *SL = Q.DL.getStructLayout(STy);
  1201. uint64_t Offset = SL->getElementOffset(Idx);
  1202. AccConstIndices += Offset;
  1203. continue;
  1204. }
  1205. // Handle array index arithmetic.
  1206. Type *IndexedTy = GTI.getIndexedType();
  1207. if (!IndexedTy->isSized()) {
  1208. Known.resetAll();
  1209. break;
  1210. }
  1211. unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
  1212. KnownBits IndexBits(IndexBitWidth);
  1213. computeKnownBits(Index, IndexBits, Depth + 1, Q);
  1214. TypeSize IndexTypeSize = Q.DL.getTypeAllocSize(IndexedTy);
  1215. uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue();
  1216. KnownBits ScalingFactor(IndexBitWidth);
  1217. // Multiply by current sizeof type.
  1218. // &A[i] == A + i * sizeof(*A[i]).
  1219. if (IndexTypeSize.isScalable()) {
  1220. // For scalable types the only thing we know about sizeof is
  1221. // that this is a multiple of the minimum size.
  1222. ScalingFactor.Zero.setLowBits(countTrailingZeros(TypeSizeInBytes));
  1223. } else if (IndexBits.isConstant()) {
  1224. APInt IndexConst = IndexBits.getConstant();
  1225. APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
  1226. IndexConst *= ScalingFactor;
  1227. AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
  1228. continue;
  1229. } else {
  1230. ScalingFactor =
  1231. KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes));
  1232. }
  1233. IndexBits = KnownBits::mul(IndexBits, ScalingFactor);
  1234. // If the offsets have a different width from the pointer, according
  1235. // to the language reference we need to sign-extend or truncate them
  1236. // to the width of the pointer.
  1237. IndexBits = IndexBits.sextOrTrunc(BitWidth);
  1238. // Note that inbounds does *not* guarantee nsw for the addition, as only
  1239. // the offset is signed, while the base address is unsigned.
  1240. Known = KnownBits::computeForAddSub(
  1241. /*Add=*/true, /*NSW=*/false, Known, IndexBits);
  1242. }
  1243. if (!Known.isUnknown() && !AccConstIndices.isZero()) {
  1244. KnownBits Index = KnownBits::makeConstant(AccConstIndices);
  1245. Known = KnownBits::computeForAddSub(
  1246. /*Add=*/true, /*NSW=*/false, Known, Index);
  1247. }
  1248. break;
  1249. }
  1250. case Instruction::PHI: {
  1251. const PHINode *P = cast<PHINode>(I);
  1252. BinaryOperator *BO = nullptr;
  1253. Value *R = nullptr, *L = nullptr;
  1254. if (matchSimpleRecurrence(P, BO, R, L)) {
  1255. // Handle the case of a simple two-predecessor recurrence PHI.
  1256. // There's a lot more that could theoretically be done here, but
  1257. // this is sufficient to catch some interesting cases.
  1258. unsigned Opcode = BO->getOpcode();
  1259. // If this is a shift recurrence, we know the bits being shifted in.
  1260. // We can combine that with information about the start value of the
  1261. // recurrence to conclude facts about the result.
  1262. if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr ||
  1263. Opcode == Instruction::Shl) &&
  1264. BO->getOperand(0) == I) {
  1265. // We have matched a recurrence of the form:
  1266. // %iv = [R, %entry], [%iv.next, %backedge]
  1267. // %iv.next = shift_op %iv, L
  1268. // Recurse with the phi context to avoid concern about whether facts
  1269. // inferred hold at original context instruction. TODO: It may be
  1270. // correct to use the original context. IF warranted, explore and
  1271. // add sufficient tests to cover.
  1272. Query RecQ = Q;
  1273. RecQ.CxtI = P;
  1274. computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
  1275. switch (Opcode) {
  1276. case Instruction::Shl:
  1277. // A shl recurrence will only increase the tailing zeros
  1278. Known.Zero.setLowBits(Known2.countMinTrailingZeros());
  1279. break;
  1280. case Instruction::LShr:
  1281. // A lshr recurrence will preserve the leading zeros of the
  1282. // start value
  1283. Known.Zero.setHighBits(Known2.countMinLeadingZeros());
  1284. break;
  1285. case Instruction::AShr:
  1286. // An ashr recurrence will extend the initial sign bit
  1287. Known.Zero.setHighBits(Known2.countMinLeadingZeros());
  1288. Known.One.setHighBits(Known2.countMinLeadingOnes());
  1289. break;
  1290. };
  1291. }
  1292. // Check for operations that have the property that if
  1293. // both their operands have low zero bits, the result
  1294. // will have low zero bits.
  1295. if (Opcode == Instruction::Add ||
  1296. Opcode == Instruction::Sub ||
  1297. Opcode == Instruction::And ||
  1298. Opcode == Instruction::Or ||
  1299. Opcode == Instruction::Mul) {
  1300. // Change the context instruction to the "edge" that flows into the
  1301. // phi. This is important because that is where the value is actually
  1302. // "evaluated" even though it is used later somewhere else. (see also
  1303. // D69571).
  1304. Query RecQ = Q;
  1305. unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
  1306. Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
  1307. Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator();
  1308. // Ok, we have a PHI of the form L op= R. Check for low
  1309. // zero bits.
  1310. RecQ.CxtI = RInst;
  1311. computeKnownBits(R, Known2, Depth + 1, RecQ);
  1312. // We need to take the minimum number of known bits
  1313. KnownBits Known3(BitWidth);
  1314. RecQ.CxtI = LInst;
  1315. computeKnownBits(L, Known3, Depth + 1, RecQ);
  1316. Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
  1317. Known3.countMinTrailingZeros()));
  1318. auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
  1319. if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
  1320. // If initial value of recurrence is nonnegative, and we are adding
  1321. // a nonnegative number with nsw, the result can only be nonnegative
  1322. // or poison value regardless of the number of times we execute the
  1323. // add in phi recurrence. If initial value is negative and we are
  1324. // adding a negative number with nsw, the result can only be
  1325. // negative or poison value. Similar arguments apply to sub and mul.
  1326. //
  1327. // (add non-negative, non-negative) --> non-negative
  1328. // (add negative, negative) --> negative
  1329. if (Opcode == Instruction::Add) {
  1330. if (Known2.isNonNegative() && Known3.isNonNegative())
  1331. Known.makeNonNegative();
  1332. else if (Known2.isNegative() && Known3.isNegative())
  1333. Known.makeNegative();
  1334. }
  1335. // (sub nsw non-negative, negative) --> non-negative
  1336. // (sub nsw negative, non-negative) --> negative
  1337. else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) {
  1338. if (Known2.isNonNegative() && Known3.isNegative())
  1339. Known.makeNonNegative();
  1340. else if (Known2.isNegative() && Known3.isNonNegative())
  1341. Known.makeNegative();
  1342. }
  1343. // (mul nsw non-negative, non-negative) --> non-negative
  1344. else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
  1345. Known3.isNonNegative())
  1346. Known.makeNonNegative();
  1347. }
  1348. break;
  1349. }
  1350. }
  1351. // Unreachable blocks may have zero-operand PHI nodes.
  1352. if (P->getNumIncomingValues() == 0)
  1353. break;
  1354. // Otherwise take the unions of the known bit sets of the operands,
  1355. // taking conservative care to avoid excessive recursion.
  1356. if (Depth < MaxAnalysisRecursionDepth - 1 && !Known.Zero && !Known.One) {
  1357. // Skip if every incoming value references to ourself.
  1358. if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
  1359. break;
  1360. Known.Zero.setAllBits();
  1361. Known.One.setAllBits();
  1362. for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) {
  1363. Value *IncValue = P->getIncomingValue(u);
  1364. // Skip direct self references.
  1365. if (IncValue == P) continue;
  1366. // Change the context instruction to the "edge" that flows into the
  1367. // phi. This is important because that is where the value is actually
  1368. // "evaluated" even though it is used later somewhere else. (see also
  1369. // D69571).
  1370. Query RecQ = Q;
  1371. RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();
  1372. Known2 = KnownBits(BitWidth);
  1373. // Recurse, but cap the recursion to one level, because we don't
  1374. // want to waste time spinning around in loops.
  1375. computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ);
  1376. // If this failed, see if we can use a conditional branch into the phi
  1377. // to help us determine the range of the value.
  1378. if (Known2.isUnknown()) {
  1379. ICmpInst::Predicate Pred;
  1380. const APInt *RHSC;
  1381. BasicBlock *TrueSucc, *FalseSucc;
  1382. // TODO: Use RHS Value and compute range from its known bits.
  1383. if (match(RecQ.CxtI,
  1384. m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
  1385. m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
  1386. // Check for cases of duplicate successors.
  1387. if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) {
  1388. // If we're using the false successor, invert the predicate.
  1389. if (FalseSucc == P->getParent())
  1390. Pred = CmpInst::getInversePredicate(Pred);
  1391. switch (Pred) {
  1392. case CmpInst::Predicate::ICMP_EQ:
  1393. Known2 = KnownBits::makeConstant(*RHSC);
  1394. break;
  1395. case CmpInst::Predicate::ICMP_ULE:
  1396. Known2.Zero.setHighBits(RHSC->countLeadingZeros());
  1397. break;
  1398. case CmpInst::Predicate::ICMP_ULT:
  1399. Known2.Zero.setHighBits((*RHSC - 1).countLeadingZeros());
  1400. break;
  1401. default:
  1402. // TODO - add additional integer predicate handling.
  1403. break;
  1404. }
  1405. }
  1406. }
  1407. }
  1408. Known = KnownBits::commonBits(Known, Known2);
  1409. // If all bits have been ruled out, there's no need to check
  1410. // more operands.
  1411. if (Known.isUnknown())
  1412. break;
  1413. }
  1414. }
  1415. break;
  1416. }
  1417. case Instruction::Call:
  1418. case Instruction::Invoke:
  1419. // If range metadata is attached to this call, set known bits from that,
  1420. // and then intersect with known bits based on other properties of the
  1421. // function.
  1422. if (MDNode *MD =
  1423. Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
  1424. computeKnownBitsFromRangeMetadata(*MD, Known);
  1425. if (const Value *RV = cast<CallBase>(I)->getReturnedArgOperand()) {
  1426. computeKnownBits(RV, Known2, Depth + 1, Q);
  1427. Known.Zero |= Known2.Zero;
  1428. Known.One |= Known2.One;
  1429. }
  1430. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
  1431. switch (II->getIntrinsicID()) {
  1432. default: break;
  1433. case Intrinsic::abs: {
  1434. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1435. bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
  1436. Known = Known2.abs(IntMinIsPoison);
  1437. break;
  1438. }
  1439. case Intrinsic::bitreverse:
  1440. computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
  1441. Known.Zero |= Known2.Zero.reverseBits();
  1442. Known.One |= Known2.One.reverseBits();
  1443. break;
  1444. case Intrinsic::bswap:
  1445. computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
  1446. Known.Zero |= Known2.Zero.byteSwap();
  1447. Known.One |= Known2.One.byteSwap();
  1448. break;
  1449. case Intrinsic::ctlz: {
  1450. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1451. // If we have a known 1, its position is our upper bound.
  1452. unsigned PossibleLZ = Known2.countMaxLeadingZeros();
  1453. // If this call is poison for 0 input, the result will be less than 2^n.
  1454. if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
  1455. PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
  1456. unsigned LowBits = llvm::bit_width(PossibleLZ);
  1457. Known.Zero.setBitsFrom(LowBits);
  1458. break;
  1459. }
  1460. case Intrinsic::cttz: {
  1461. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1462. // If we have a known 1, its position is our upper bound.
  1463. unsigned PossibleTZ = Known2.countMaxTrailingZeros();
  1464. // If this call is poison for 0 input, the result will be less than 2^n.
  1465. if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
  1466. PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
  1467. unsigned LowBits = llvm::bit_width(PossibleTZ);
  1468. Known.Zero.setBitsFrom(LowBits);
  1469. break;
  1470. }
  1471. case Intrinsic::ctpop: {
  1472. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1473. // We can bound the space the count needs. Also, bits known to be zero
  1474. // can't contribute to the population.
  1475. unsigned BitsPossiblySet = Known2.countMaxPopulation();
  1476. unsigned LowBits = llvm::bit_width(BitsPossiblySet);
  1477. Known.Zero.setBitsFrom(LowBits);
  1478. // TODO: we could bound KnownOne using the lower bound on the number
  1479. // of bits which might be set provided by popcnt KnownOne2.
  1480. break;
  1481. }
  1482. case Intrinsic::fshr:
  1483. case Intrinsic::fshl: {
  1484. const APInt *SA;
  1485. if (!match(I->getOperand(2), m_APInt(SA)))
  1486. break;
  1487. // Normalize to funnel shift left.
  1488. uint64_t ShiftAmt = SA->urem(BitWidth);
  1489. if (II->getIntrinsicID() == Intrinsic::fshr)
  1490. ShiftAmt = BitWidth - ShiftAmt;
  1491. KnownBits Known3(BitWidth);
  1492. computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
  1493. computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
  1494. Known.Zero =
  1495. Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
  1496. Known.One =
  1497. Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
  1498. break;
  1499. }
  1500. case Intrinsic::uadd_sat:
  1501. case Intrinsic::usub_sat: {
  1502. bool IsAdd = II->getIntrinsicID() == Intrinsic::uadd_sat;
  1503. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1504. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1505. // Add: Leading ones of either operand are preserved.
  1506. // Sub: Leading zeros of LHS and leading ones of RHS are preserved
  1507. // as leading zeros in the result.
  1508. unsigned LeadingKnown;
  1509. if (IsAdd)
  1510. LeadingKnown = std::max(Known.countMinLeadingOnes(),
  1511. Known2.countMinLeadingOnes());
  1512. else
  1513. LeadingKnown = std::max(Known.countMinLeadingZeros(),
  1514. Known2.countMinLeadingOnes());
  1515. Known = KnownBits::computeForAddSub(
  1516. IsAdd, /* NSW */ false, Known, Known2);
  1517. // We select between the operation result and all-ones/zero
  1518. // respectively, so we can preserve known ones/zeros.
  1519. if (IsAdd) {
  1520. Known.One.setHighBits(LeadingKnown);
  1521. Known.Zero.clearAllBits();
  1522. } else {
  1523. Known.Zero.setHighBits(LeadingKnown);
  1524. Known.One.clearAllBits();
  1525. }
  1526. break;
  1527. }
  1528. case Intrinsic::umin:
  1529. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1530. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1531. Known = KnownBits::umin(Known, Known2);
  1532. break;
  1533. case Intrinsic::umax:
  1534. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1535. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1536. Known = KnownBits::umax(Known, Known2);
  1537. break;
  1538. case Intrinsic::smin:
  1539. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1540. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1541. Known = KnownBits::smin(Known, Known2);
  1542. break;
  1543. case Intrinsic::smax:
  1544. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1545. computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
  1546. Known = KnownBits::smax(Known, Known2);
  1547. break;
  1548. case Intrinsic::x86_sse42_crc32_64_64:
  1549. Known.Zero.setBitsFrom(32);
  1550. break;
  1551. case Intrinsic::riscv_vsetvli:
  1552. case Intrinsic::riscv_vsetvlimax:
  1553. // Assume that VL output is positive and would fit in an int32_t.
  1554. // TODO: VLEN might be capped at 16 bits in a future V spec update.
  1555. if (BitWidth >= 32)
  1556. Known.Zero.setBitsFrom(31);
  1557. break;
  1558. case Intrinsic::vscale: {
  1559. if (!II->getParent() || !II->getFunction() ||
  1560. !II->getFunction()->hasFnAttribute(Attribute::VScaleRange))
  1561. break;
  1562. auto Attr = II->getFunction()->getFnAttribute(Attribute::VScaleRange);
  1563. std::optional<unsigned> VScaleMax = Attr.getVScaleRangeMax();
  1564. if (!VScaleMax)
  1565. break;
  1566. unsigned VScaleMin = Attr.getVScaleRangeMin();
  1567. // If vscale min = max then we know the exact value at compile time
  1568. // and hence we know the exact bits.
  1569. if (VScaleMin == VScaleMax) {
  1570. Known.One = VScaleMin;
  1571. Known.Zero = VScaleMin;
  1572. Known.Zero.flipAllBits();
  1573. break;
  1574. }
  1575. unsigned FirstZeroHighBit = llvm::bit_width(*VScaleMax);
  1576. if (FirstZeroHighBit < BitWidth)
  1577. Known.Zero.setBitsFrom(FirstZeroHighBit);
  1578. break;
  1579. }
  1580. }
  1581. }
  1582. break;
  1583. case Instruction::ShuffleVector: {
  1584. auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
  1585. // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
  1586. if (!Shuf) {
  1587. Known.resetAll();
  1588. return;
  1589. }
  1590. // For undef elements, we don't know anything about the common state of
  1591. // the shuffle result.
  1592. APInt DemandedLHS, DemandedRHS;
  1593. if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
  1594. Known.resetAll();
  1595. return;
  1596. }
  1597. Known.One.setAllBits();
  1598. Known.Zero.setAllBits();
  1599. if (!!DemandedLHS) {
  1600. const Value *LHS = Shuf->getOperand(0);
  1601. computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
  1602. // If we don't know any bits, early out.
  1603. if (Known.isUnknown())
  1604. break;
  1605. }
  1606. if (!!DemandedRHS) {
  1607. const Value *RHS = Shuf->getOperand(1);
  1608. computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
  1609. Known = KnownBits::commonBits(Known, Known2);
  1610. }
  1611. break;
  1612. }
  1613. case Instruction::InsertElement: {
  1614. if (isa<ScalableVectorType>(I->getType())) {
  1615. Known.resetAll();
  1616. return;
  1617. }
  1618. const Value *Vec = I->getOperand(0);
  1619. const Value *Elt = I->getOperand(1);
  1620. auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
  1621. // Early out if the index is non-constant or out-of-range.
  1622. unsigned NumElts = DemandedElts.getBitWidth();
  1623. if (!CIdx || CIdx->getValue().uge(NumElts)) {
  1624. Known.resetAll();
  1625. return;
  1626. }
  1627. Known.One.setAllBits();
  1628. Known.Zero.setAllBits();
  1629. unsigned EltIdx = CIdx->getZExtValue();
  1630. // Do we demand the inserted element?
  1631. if (DemandedElts[EltIdx]) {
  1632. computeKnownBits(Elt, Known, Depth + 1, Q);
  1633. // If we don't know any bits, early out.
  1634. if (Known.isUnknown())
  1635. break;
  1636. }
  1637. // We don't need the base vector element that has been inserted.
  1638. APInt DemandedVecElts = DemandedElts;
  1639. DemandedVecElts.clearBit(EltIdx);
  1640. if (!!DemandedVecElts) {
  1641. computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
  1642. Known = KnownBits::commonBits(Known, Known2);
  1643. }
  1644. break;
  1645. }
  1646. case Instruction::ExtractElement: {
  1647. // Look through extract element. If the index is non-constant or
  1648. // out-of-range demand all elements, otherwise just the extracted element.
  1649. const Value *Vec = I->getOperand(0);
  1650. const Value *Idx = I->getOperand(1);
  1651. auto *CIdx = dyn_cast<ConstantInt>(Idx);
  1652. if (isa<ScalableVectorType>(Vec->getType())) {
  1653. // FIXME: there's probably *something* we can do with scalable vectors
  1654. Known.resetAll();
  1655. break;
  1656. }
  1657. unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
  1658. APInt DemandedVecElts = APInt::getAllOnes(NumElts);
  1659. if (CIdx && CIdx->getValue().ult(NumElts))
  1660. DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
  1661. computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
  1662. break;
  1663. }
  1664. case Instruction::ExtractValue:
  1665. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
  1666. const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
  1667. if (EVI->getNumIndices() != 1) break;
  1668. if (EVI->getIndices()[0] == 0) {
  1669. switch (II->getIntrinsicID()) {
  1670. default: break;
  1671. case Intrinsic::uadd_with_overflow:
  1672. case Intrinsic::sadd_with_overflow:
  1673. computeKnownBitsAddSub(true, II->getArgOperand(0),
  1674. II->getArgOperand(1), false, DemandedElts,
  1675. Known, Known2, Depth, Q);
  1676. break;
  1677. case Intrinsic::usub_with_overflow:
  1678. case Intrinsic::ssub_with_overflow:
  1679. computeKnownBitsAddSub(false, II->getArgOperand(0),
  1680. II->getArgOperand(1), false, DemandedElts,
  1681. Known, Known2, Depth, Q);
  1682. break;
  1683. case Intrinsic::umul_with_overflow:
  1684. case Intrinsic::smul_with_overflow:
  1685. computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
  1686. DemandedElts, Known, Known2, Depth, Q);
  1687. break;
  1688. }
  1689. }
  1690. }
  1691. break;
  1692. case Instruction::Freeze:
  1693. if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
  1694. Depth + 1))
  1695. computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
  1696. break;
  1697. }
  1698. }
  1699. /// Determine which bits of V are known to be either zero or one and return
  1700. /// them.
  1701. KnownBits computeKnownBits(const Value *V, const APInt &DemandedElts,
  1702. unsigned Depth, const Query &Q) {
  1703. KnownBits Known(getBitWidth(V->getType(), Q.DL));
  1704. computeKnownBits(V, DemandedElts, Known, Depth, Q);
  1705. return Known;
  1706. }
  1707. /// Determine which bits of V are known to be either zero or one and return
  1708. /// them.
  1709. KnownBits computeKnownBits(const Value *V, unsigned Depth, const Query &Q) {
  1710. KnownBits Known(getBitWidth(V->getType(), Q.DL));
  1711. computeKnownBits(V, Known, Depth, Q);
  1712. return Known;
  1713. }
  1714. /// Determine which bits of V are known to be either zero or one and return
  1715. /// them in the Known bit set.
  1716. ///
  1717. /// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
  1718. /// we cannot optimize based on the assumption that it is zero without changing
  1719. /// it to be an explicit zero. If we don't change it to zero, other code could
  1720. /// optimized based on the contradictory assumption that it is non-zero.
  1721. /// Because instcombine aggressively folds operations with undef args anyway,
  1722. /// this won't lose us code quality.
  1723. ///
  1724. /// This function is defined on values with integer type, values with pointer
  1725. /// type, and vectors of integers. In the case
  1726. /// where V is a vector, known zero, and known one values are the
  1727. /// same width as the vector element, and the bit is set only if it is true
  1728. /// for all of the demanded elements in the vector specified by DemandedElts.
  1729. void computeKnownBits(const Value *V, const APInt &DemandedElts,
  1730. KnownBits &Known, unsigned Depth, const Query &Q) {
  1731. if (!DemandedElts) {
  1732. // No demanded elts, better to assume we don't know anything.
  1733. Known.resetAll();
  1734. return;
  1735. }
  1736. assert(V && "No Value?");
  1737. assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
  1738. #ifndef NDEBUG
  1739. Type *Ty = V->getType();
  1740. unsigned BitWidth = Known.getBitWidth();
  1741. assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
  1742. "Not integer or pointer type!");
  1743. if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
  1744. assert(
  1745. FVTy->getNumElements() == DemandedElts.getBitWidth() &&
  1746. "DemandedElt width should equal the fixed vector number of elements");
  1747. } else {
  1748. assert(DemandedElts == APInt(1, 1) &&
  1749. "DemandedElt width should be 1 for scalars or scalable vectors");
  1750. }
  1751. Type *ScalarTy = Ty->getScalarType();
  1752. if (ScalarTy->isPointerTy()) {
  1753. assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
  1754. "V and Known should have same BitWidth");
  1755. } else {
  1756. assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
  1757. "V and Known should have same BitWidth");
  1758. }
  1759. #endif
  1760. const APInt *C;
  1761. if (match(V, m_APInt(C))) {
  1762. // We know all of the bits for a scalar constant or a splat vector constant!
  1763. Known = KnownBits::makeConstant(*C);
  1764. return;
  1765. }
  1766. // Null and aggregate-zero are all-zeros.
  1767. if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
  1768. Known.setAllZero();
  1769. return;
  1770. }
  1771. // Handle a constant vector by taking the intersection of the known bits of
  1772. // each element.
  1773. if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
  1774. assert(!isa<ScalableVectorType>(V->getType()));
  1775. // We know that CDV must be a vector of integers. Take the intersection of
  1776. // each element.
  1777. Known.Zero.setAllBits(); Known.One.setAllBits();
  1778. for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
  1779. if (!DemandedElts[i])
  1780. continue;
  1781. APInt Elt = CDV->getElementAsAPInt(i);
  1782. Known.Zero &= ~Elt;
  1783. Known.One &= Elt;
  1784. }
  1785. return;
  1786. }
  1787. if (const auto *CV = dyn_cast<ConstantVector>(V)) {
  1788. assert(!isa<ScalableVectorType>(V->getType()));
  1789. // We know that CV must be a vector of integers. Take the intersection of
  1790. // each element.
  1791. Known.Zero.setAllBits(); Known.One.setAllBits();
  1792. for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
  1793. if (!DemandedElts[i])
  1794. continue;
  1795. Constant *Element = CV->getAggregateElement(i);
  1796. auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
  1797. if (!ElementCI) {
  1798. Known.resetAll();
  1799. return;
  1800. }
  1801. const APInt &Elt = ElementCI->getValue();
  1802. Known.Zero &= ~Elt;
  1803. Known.One &= Elt;
  1804. }
  1805. return;
  1806. }
  1807. // Start out not knowing anything.
  1808. Known.resetAll();
  1809. // We can't imply anything about undefs.
  1810. if (isa<UndefValue>(V))
  1811. return;
  1812. // There's no point in looking through other users of ConstantData for
  1813. // assumptions. Confirm that we've handled them all.
  1814. assert(!isa<ConstantData>(V) && "Unhandled constant data!");
  1815. // All recursive calls that increase depth must come after this.
  1816. if (Depth == MaxAnalysisRecursionDepth)
  1817. return;
  1818. // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
  1819. // the bits of its aliasee.
  1820. if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
  1821. if (!GA->isInterposable())
  1822. computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
  1823. return;
  1824. }
  1825. if (const Operator *I = dyn_cast<Operator>(V))
  1826. computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
  1827. // Aligned pointers have trailing zeros - refine Known.Zero set
  1828. if (isa<PointerType>(V->getType())) {
  1829. Align Alignment = V->getPointerAlignment(Q.DL);
  1830. Known.Zero.setLowBits(Log2(Alignment));
  1831. }
  1832. // computeKnownBitsFromAssume strictly refines Known.
  1833. // Therefore, we run them after computeKnownBitsFromOperator.
  1834. // Check whether a nearby assume intrinsic can determine some known bits.
  1835. computeKnownBitsFromAssume(V, Known, Depth, Q);
  1836. assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
  1837. }
  1838. /// Try to detect a recurrence that the value of the induction variable is
  1839. /// always a power of two (or zero).
  1840. static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
  1841. unsigned Depth, Query &Q) {
  1842. BinaryOperator *BO = nullptr;
  1843. Value *Start = nullptr, *Step = nullptr;
  1844. if (!matchSimpleRecurrence(PN, BO, Start, Step))
  1845. return false;
  1846. // Initial value must be a power of two.
  1847. for (const Use &U : PN->operands()) {
  1848. if (U.get() == Start) {
  1849. // Initial value comes from a different BB, need to adjust context
  1850. // instruction for analysis.
  1851. Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
  1852. if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q))
  1853. return false;
  1854. }
  1855. }
  1856. // Except for Mul, the induction variable must be on the left side of the
  1857. // increment expression, otherwise its value can be arbitrary.
  1858. if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
  1859. return false;
  1860. Q.CxtI = BO->getParent()->getTerminator();
  1861. switch (BO->getOpcode()) {
  1862. case Instruction::Mul:
  1863. // Power of two is closed under multiplication.
  1864. return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
  1865. Q.IIQ.hasNoSignedWrap(BO)) &&
  1866. isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q);
  1867. case Instruction::SDiv:
  1868. // Start value must not be signmask for signed division, so simply being a
  1869. // power of two is not sufficient, and it has to be a constant.
  1870. if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
  1871. return false;
  1872. [[fallthrough]];
  1873. case Instruction::UDiv:
  1874. // Divisor must be a power of two.
  1875. // If OrZero is false, cannot guarantee induction variable is non-zero after
  1876. // division, same for Shr, unless it is exact division.
  1877. return (OrZero || Q.IIQ.isExact(BO)) &&
  1878. isKnownToBeAPowerOfTwo(Step, false, Depth, Q);
  1879. case Instruction::Shl:
  1880. return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
  1881. case Instruction::AShr:
  1882. if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
  1883. return false;
  1884. [[fallthrough]];
  1885. case Instruction::LShr:
  1886. return OrZero || Q.IIQ.isExact(BO);
  1887. default:
  1888. return false;
  1889. }
  1890. }
  1891. /// Return true if the given value is known to have exactly one
  1892. /// bit set when defined. For vectors return true if every element is known to
  1893. /// be a power of two when defined. Supports values with integer or pointer
  1894. /// types and vectors of integers.
  1895. bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
  1896. const Query &Q) {
  1897. assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
  1898. // Attempt to match against constants.
  1899. if (OrZero && match(V, m_Power2OrZero()))
  1900. return true;
  1901. if (match(V, m_Power2()))
  1902. return true;
  1903. // 1 << X is clearly a power of two if the one is not shifted off the end. If
  1904. // it is shifted off the end then the result is undefined.
  1905. if (match(V, m_Shl(m_One(), m_Value())))
  1906. return true;
  1907. // (signmask) >>l X is clearly a power of two if the one is not shifted off
  1908. // the bottom. If it is shifted off the bottom then the result is undefined.
  1909. if (match(V, m_LShr(m_SignMask(), m_Value())))
  1910. return true;
  1911. // The remaining tests are all recursive, so bail out if we hit the limit.
  1912. if (Depth++ == MaxAnalysisRecursionDepth)
  1913. return false;
  1914. Value *X = nullptr, *Y = nullptr;
  1915. // A shift left or a logical shift right of a power of two is a power of two
  1916. // or zero.
  1917. if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
  1918. match(V, m_LShr(m_Value(X), m_Value()))))
  1919. return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q);
  1920. if (const ZExtInst *ZI = dyn_cast<ZExtInst>(V))
  1921. return isKnownToBeAPowerOfTwo(ZI->getOperand(0), OrZero, Depth, Q);
  1922. if (const SelectInst *SI = dyn_cast<SelectInst>(V))
  1923. return isKnownToBeAPowerOfTwo(SI->getTrueValue(), OrZero, Depth, Q) &&
  1924. isKnownToBeAPowerOfTwo(SI->getFalseValue(), OrZero, Depth, Q);
  1925. // Peek through min/max.
  1926. if (match(V, m_MaxOrMin(m_Value(X), m_Value(Y)))) {
  1927. return isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q) &&
  1928. isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q);
  1929. }
  1930. if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
  1931. // A power of two and'd with anything is a power of two or zero.
  1932. if (isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q) ||
  1933. isKnownToBeAPowerOfTwo(Y, /*OrZero*/ true, Depth, Q))
  1934. return true;
  1935. // X & (-X) is always a power of two or zero.
  1936. if (match(X, m_Neg(m_Specific(Y))) || match(Y, m_Neg(m_Specific(X))))
  1937. return true;
  1938. return false;
  1939. }
  1940. // Adding a power-of-two or zero to the same power-of-two or zero yields
  1941. // either the original power-of-two, a larger power-of-two or zero.
  1942. if (match(V, m_Add(m_Value(X), m_Value(Y)))) {
  1943. const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
  1944. if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
  1945. Q.IIQ.hasNoSignedWrap(VOBO)) {
  1946. if (match(X, m_And(m_Specific(Y), m_Value())) ||
  1947. match(X, m_And(m_Value(), m_Specific(Y))))
  1948. if (isKnownToBeAPowerOfTwo(Y, OrZero, Depth, Q))
  1949. return true;
  1950. if (match(Y, m_And(m_Specific(X), m_Value())) ||
  1951. match(Y, m_And(m_Value(), m_Specific(X))))
  1952. if (isKnownToBeAPowerOfTwo(X, OrZero, Depth, Q))
  1953. return true;
  1954. unsigned BitWidth = V->getType()->getScalarSizeInBits();
  1955. KnownBits LHSBits(BitWidth);
  1956. computeKnownBits(X, LHSBits, Depth, Q);
  1957. KnownBits RHSBits(BitWidth);
  1958. computeKnownBits(Y, RHSBits, Depth, Q);
  1959. // If i8 V is a power of two or zero:
  1960. // ZeroBits: 1 1 1 0 1 1 1 1
  1961. // ~ZeroBits: 0 0 0 1 0 0 0 0
  1962. if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
  1963. // If OrZero isn't set, we cannot give back a zero result.
  1964. // Make sure either the LHS or RHS has a bit set.
  1965. if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
  1966. return true;
  1967. }
  1968. }
  1969. // A PHI node is power of two if all incoming values are power of two, or if
  1970. // it is an induction variable where in each step its value is a power of two.
  1971. if (const PHINode *PN = dyn_cast<PHINode>(V)) {
  1972. Query RecQ = Q;
  1973. // Check if it is an induction variable and always power of two.
  1974. if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))
  1975. return true;
  1976. // Recursively check all incoming values. Limit recursion to 2 levels, so
  1977. // that search complexity is limited to number of operands^2.
  1978. unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
  1979. return llvm::all_of(PN->operands(), [&](const Use &U) {
  1980. // Value is power of 2 if it is coming from PHI node itself by induction.
  1981. if (U.get() == PN)
  1982. return true;
  1983. // Change the context instruction to the incoming block where it is
  1984. // evaluated.
  1985. RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
  1986. return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
  1987. });
  1988. }
  1989. // An exact divide or right shift can only shift off zero bits, so the result
  1990. // is a power of two only if the first operand is a power of two and not
  1991. // copying a sign bit (sdiv int_min, 2).
  1992. if (match(V, m_Exact(m_LShr(m_Value(), m_Value()))) ||
  1993. match(V, m_Exact(m_UDiv(m_Value(), m_Value())))) {
  1994. return isKnownToBeAPowerOfTwo(cast<Operator>(V)->getOperand(0), OrZero,
  1995. Depth, Q);
  1996. }
  1997. return false;
  1998. }
  1999. /// Test whether a GEP's result is known to be non-null.
  2000. ///
  2001. /// Uses properties inherent in a GEP to try to determine whether it is known
  2002. /// to be non-null.
  2003. ///
  2004. /// Currently this routine does not support vector GEPs.
  2005. static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
  2006. const Query &Q) {
  2007. const Function *F = nullptr;
  2008. if (const Instruction *I = dyn_cast<Instruction>(GEP))
  2009. F = I->getFunction();
  2010. if (!GEP->isInBounds() ||
  2011. NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
  2012. return false;
  2013. // FIXME: Support vector-GEPs.
  2014. assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
  2015. // If the base pointer is non-null, we cannot walk to a null address with an
  2016. // inbounds GEP in address space zero.
  2017. if (isKnownNonZero(GEP->getPointerOperand(), Depth, Q))
  2018. return true;
  2019. // Walk the GEP operands and see if any operand introduces a non-zero offset.
  2020. // If so, then the GEP cannot produce a null pointer, as doing so would
  2021. // inherently violate the inbounds contract within address space zero.
  2022. for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
  2023. GTI != GTE; ++GTI) {
  2024. // Struct types are easy -- they must always be indexed by a constant.
  2025. if (StructType *STy = GTI.getStructTypeOrNull()) {
  2026. ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
  2027. unsigned ElementIdx = OpC->getZExtValue();
  2028. const StructLayout *SL = Q.DL.getStructLayout(STy);
  2029. uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
  2030. if (ElementOffset > 0)
  2031. return true;
  2032. continue;
  2033. }
  2034. // If we have a zero-sized type, the index doesn't matter. Keep looping.
  2035. if (Q.DL.getTypeAllocSize(GTI.getIndexedType()).isZero())
  2036. continue;
  2037. // Fast path the constant operand case both for efficiency and so we don't
  2038. // increment Depth when just zipping down an all-constant GEP.
  2039. if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
  2040. if (!OpC->isZero())
  2041. return true;
  2042. continue;
  2043. }
  2044. // We post-increment Depth here because while isKnownNonZero increments it
  2045. // as well, when we pop back up that increment won't persist. We don't want
  2046. // to recurse 10k times just because we have 10k GEP operands. We don't
  2047. // bail completely out because we want to handle constant GEPs regardless
  2048. // of depth.
  2049. if (Depth++ >= MaxAnalysisRecursionDepth)
  2050. continue;
  2051. if (isKnownNonZero(GTI.getOperand(), Depth, Q))
  2052. return true;
  2053. }
  2054. return false;
  2055. }
  2056. static bool isKnownNonNullFromDominatingCondition(const Value *V,
  2057. const Instruction *CtxI,
  2058. const DominatorTree *DT) {
  2059. if (isa<Constant>(V))
  2060. return false;
  2061. if (!CtxI || !DT)
  2062. return false;
  2063. unsigned NumUsesExplored = 0;
  2064. for (const auto *U : V->users()) {
  2065. // Avoid massive lists
  2066. if (NumUsesExplored >= DomConditionsMaxUses)
  2067. break;
  2068. NumUsesExplored++;
  2069. // If the value is used as an argument to a call or invoke, then argument
  2070. // attributes may provide an answer about null-ness.
  2071. if (const auto *CB = dyn_cast<CallBase>(U))
  2072. if (auto *CalledFunc = CB->getCalledFunction())
  2073. for (const Argument &Arg : CalledFunc->args())
  2074. if (CB->getArgOperand(Arg.getArgNo()) == V &&
  2075. Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) &&
  2076. DT->dominates(CB, CtxI))
  2077. return true;
  2078. // If the value is used as a load/store, then the pointer must be non null.
  2079. if (V == getLoadStorePointerOperand(U)) {
  2080. const Instruction *I = cast<Instruction>(U);
  2081. if (!NullPointerIsDefined(I->getFunction(),
  2082. V->getType()->getPointerAddressSpace()) &&
  2083. DT->dominates(I, CtxI))
  2084. return true;
  2085. }
  2086. // Consider only compare instructions uniquely controlling a branch
  2087. Value *RHS;
  2088. CmpInst::Predicate Pred;
  2089. if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
  2090. continue;
  2091. bool NonNullIfTrue;
  2092. if (cmpExcludesZero(Pred, RHS))
  2093. NonNullIfTrue = true;
  2094. else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS))
  2095. NonNullIfTrue = false;
  2096. else
  2097. continue;
  2098. SmallVector<const User *, 4> WorkList;
  2099. SmallPtrSet<const User *, 4> Visited;
  2100. for (const auto *CmpU : U->users()) {
  2101. assert(WorkList.empty() && "Should be!");
  2102. if (Visited.insert(CmpU).second)
  2103. WorkList.push_back(CmpU);
  2104. while (!WorkList.empty()) {
  2105. auto *Curr = WorkList.pop_back_val();
  2106. // If a user is an AND, add all its users to the work list. We only
  2107. // propagate "pred != null" condition through AND because it is only
  2108. // correct to assume that all conditions of AND are met in true branch.
  2109. // TODO: Support similar logic of OR and EQ predicate?
  2110. if (NonNullIfTrue)
  2111. if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
  2112. for (const auto *CurrU : Curr->users())
  2113. if (Visited.insert(CurrU).second)
  2114. WorkList.push_back(CurrU);
  2115. continue;
  2116. }
  2117. if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
  2118. assert(BI->isConditional() && "uses a comparison!");
  2119. BasicBlock *NonNullSuccessor =
  2120. BI->getSuccessor(NonNullIfTrue ? 0 : 1);
  2121. BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
  2122. if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
  2123. return true;
  2124. } else if (NonNullIfTrue && isGuard(Curr) &&
  2125. DT->dominates(cast<Instruction>(Curr), CtxI)) {
  2126. return true;
  2127. }
  2128. }
  2129. }
  2130. }
  2131. return false;
  2132. }
  2133. /// Does the 'Range' metadata (which must be a valid MD_range operand list)
  2134. /// ensure that the value it's attached to is never Value? 'RangeType' is
  2135. /// is the type of the value described by the range.
  2136. static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
  2137. const unsigned NumRanges = Ranges->getNumOperands() / 2;
  2138. assert(NumRanges >= 1);
  2139. for (unsigned i = 0; i < NumRanges; ++i) {
  2140. ConstantInt *Lower =
  2141. mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
  2142. ConstantInt *Upper =
  2143. mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
  2144. ConstantRange Range(Lower->getValue(), Upper->getValue());
  2145. if (Range.contains(Value))
  2146. return false;
  2147. }
  2148. return true;
  2149. }
  2150. /// Try to detect a recurrence that monotonically increases/decreases from a
  2151. /// non-zero starting value. These are common as induction variables.
  2152. static bool isNonZeroRecurrence(const PHINode *PN) {
  2153. BinaryOperator *BO = nullptr;
  2154. Value *Start = nullptr, *Step = nullptr;
  2155. const APInt *StartC, *StepC;
  2156. if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
  2157. !match(Start, m_APInt(StartC)) || StartC->isZero())
  2158. return false;
  2159. switch (BO->getOpcode()) {
  2160. case Instruction::Add:
  2161. // Starting from non-zero and stepping away from zero can never wrap back
  2162. // to zero.
  2163. return BO->hasNoUnsignedWrap() ||
  2164. (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
  2165. StartC->isNegative() == StepC->isNegative());
  2166. case Instruction::Mul:
  2167. return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
  2168. match(Step, m_APInt(StepC)) && !StepC->isZero();
  2169. case Instruction::Shl:
  2170. return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
  2171. case Instruction::AShr:
  2172. case Instruction::LShr:
  2173. return BO->isExact();
  2174. default:
  2175. return false;
  2176. }
  2177. }
  2178. /// Return true if the given value is known to be non-zero when defined. For
  2179. /// vectors, return true if every demanded element is known to be non-zero when
  2180. /// defined. For pointers, if the context instruction and dominator tree are
  2181. /// specified, perform context-sensitive analysis and return true if the
  2182. /// pointer couldn't possibly be null at the specified instruction.
  2183. /// Supports values with integer or pointer type and vectors of integers.
  2184. bool isKnownNonZero(const Value *V, const APInt &DemandedElts, unsigned Depth,
  2185. const Query &Q) {
  2186. #ifndef NDEBUG
  2187. Type *Ty = V->getType();
  2188. assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
  2189. if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
  2190. assert(
  2191. FVTy->getNumElements() == DemandedElts.getBitWidth() &&
  2192. "DemandedElt width should equal the fixed vector number of elements");
  2193. } else {
  2194. assert(DemandedElts == APInt(1, 1) &&
  2195. "DemandedElt width should be 1 for scalars");
  2196. }
  2197. #endif
  2198. if (auto *C = dyn_cast<Constant>(V)) {
  2199. if (C->isNullValue())
  2200. return false;
  2201. if (isa<ConstantInt>(C))
  2202. // Must be non-zero due to null test above.
  2203. return true;
  2204. // For constant vectors, check that all elements are undefined or known
  2205. // non-zero to determine that the whole vector is known non-zero.
  2206. if (auto *VecTy = dyn_cast<FixedVectorType>(C->getType())) {
  2207. for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
  2208. if (!DemandedElts[i])
  2209. continue;
  2210. Constant *Elt = C->getAggregateElement(i);
  2211. if (!Elt || Elt->isNullValue())
  2212. return false;
  2213. if (!isa<UndefValue>(Elt) && !isa<ConstantInt>(Elt))
  2214. return false;
  2215. }
  2216. return true;
  2217. }
  2218. // A global variable in address space 0 is non null unless extern weak
  2219. // or an absolute symbol reference. Other address spaces may have null as a
  2220. // valid address for a global, so we can't assume anything.
  2221. if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
  2222. if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
  2223. GV->getType()->getAddressSpace() == 0)
  2224. return true;
  2225. }
  2226. // For constant expressions, fall through to the Operator code below.
  2227. if (!isa<ConstantExpr>(V))
  2228. return false;
  2229. }
  2230. if (auto *I = dyn_cast<Instruction>(V)) {
  2231. if (MDNode *Ranges = Q.IIQ.getMetadata(I, LLVMContext::MD_range)) {
  2232. // If the possible ranges don't contain zero, then the value is
  2233. // definitely non-zero.
  2234. if (auto *Ty = dyn_cast<IntegerType>(V->getType())) {
  2235. const APInt ZeroValue(Ty->getBitWidth(), 0);
  2236. if (rangeMetadataExcludesValue(Ranges, ZeroValue))
  2237. return true;
  2238. }
  2239. }
  2240. }
  2241. if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
  2242. return true;
  2243. // Some of the tests below are recursive, so bail out if we hit the limit.
  2244. if (Depth++ >= MaxAnalysisRecursionDepth)
  2245. return false;
  2246. // Check for pointer simplifications.
  2247. if (PointerType *PtrTy = dyn_cast<PointerType>(V->getType())) {
  2248. // Alloca never returns null, malloc might.
  2249. if (isa<AllocaInst>(V) && Q.DL.getAllocaAddrSpace() == 0)
  2250. return true;
  2251. // A byval, inalloca may not be null in a non-default addres space. A
  2252. // nonnull argument is assumed never 0.
  2253. if (const Argument *A = dyn_cast<Argument>(V)) {
  2254. if (((A->hasPassPointeeByValueCopyAttr() &&
  2255. !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
  2256. A->hasNonNullAttr()))
  2257. return true;
  2258. }
  2259. // A Load tagged with nonnull metadata is never null.
  2260. if (const LoadInst *LI = dyn_cast<LoadInst>(V))
  2261. if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull))
  2262. return true;
  2263. if (const auto *Call = dyn_cast<CallBase>(V)) {
  2264. if (Call->isReturnNonNull())
  2265. return true;
  2266. if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
  2267. return isKnownNonZero(RP, Depth, Q);
  2268. }
  2269. }
  2270. if (!isa<Constant>(V) &&
  2271. isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
  2272. return true;
  2273. const Operator *I = dyn_cast<Operator>(V);
  2274. if (!I)
  2275. return false;
  2276. unsigned BitWidth = getBitWidth(V->getType()->getScalarType(), Q.DL);
  2277. switch (I->getOpcode()) {
  2278. case Instruction::GetElementPtr:
  2279. if (I->getType()->isPointerTy())
  2280. return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
  2281. break;
  2282. case Instruction::BitCast:
  2283. if (I->getType()->isPointerTy())
  2284. return isKnownNonZero(I->getOperand(0), Depth, Q);
  2285. break;
  2286. case Instruction::IntToPtr:
  2287. // Note that we have to take special care to avoid looking through
  2288. // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
  2289. // as casts that can alter the value, e.g., AddrSpaceCasts.
  2290. if (!isa<ScalableVectorType>(I->getType()) &&
  2291. Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
  2292. Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
  2293. return isKnownNonZero(I->getOperand(0), Depth, Q);
  2294. break;
  2295. case Instruction::PtrToInt:
  2296. // Similar to int2ptr above, we can look through ptr2int here if the cast
  2297. // is a no-op or an extend and not a truncate.
  2298. if (!isa<ScalableVectorType>(I->getType()) &&
  2299. Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
  2300. Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
  2301. return isKnownNonZero(I->getOperand(0), Depth, Q);
  2302. break;
  2303. case Instruction::Or:
  2304. // X | Y != 0 if X != 0 or Y != 0.
  2305. return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||
  2306. isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q);
  2307. case Instruction::SExt:
  2308. case Instruction::ZExt:
  2309. // ext X != 0 if X != 0.
  2310. return isKnownNonZero(I->getOperand(0), Depth, Q);
  2311. case Instruction::Shl: {
  2312. // shl nuw can't remove any non-zero bits.
  2313. const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
  2314. if (Q.IIQ.hasNoUnsignedWrap(BO))
  2315. return isKnownNonZero(I->getOperand(0), Depth, Q);
  2316. // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
  2317. // if the lowest bit is shifted off the end.
  2318. KnownBits Known(BitWidth);
  2319. computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
  2320. if (Known.One[0])
  2321. return true;
  2322. break;
  2323. }
  2324. case Instruction::LShr:
  2325. case Instruction::AShr: {
  2326. // shr exact can only shift out zero bits.
  2327. const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(V);
  2328. if (BO->isExact())
  2329. return isKnownNonZero(I->getOperand(0), Depth, Q);
  2330. // shr X, Y != 0 if X is negative. Note that the value of the shift is not
  2331. // defined if the sign bit is shifted off the end.
  2332. KnownBits Known =
  2333. computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
  2334. if (Known.isNegative())
  2335. return true;
  2336. // If the shifter operand is a constant, and all of the bits shifted
  2337. // out are known to be zero, and X is known non-zero then at least one
  2338. // non-zero bit must remain.
  2339. if (ConstantInt *Shift = dyn_cast<ConstantInt>(I->getOperand(1))) {
  2340. auto ShiftVal = Shift->getLimitedValue(BitWidth - 1);
  2341. // Is there a known one in the portion not shifted out?
  2342. if (Known.countMaxLeadingZeros() < BitWidth - ShiftVal)
  2343. return true;
  2344. // Are all the bits to be shifted out known zero?
  2345. if (Known.countMinTrailingZeros() >= ShiftVal)
  2346. return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
  2347. }
  2348. break;
  2349. }
  2350. case Instruction::UDiv:
  2351. case Instruction::SDiv:
  2352. // div exact can only produce a zero if the dividend is zero.
  2353. if (cast<PossiblyExactOperator>(I)->isExact())
  2354. return isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q);
  2355. break;
  2356. case Instruction::Add: {
  2357. // X + Y.
  2358. KnownBits XKnown =
  2359. computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
  2360. KnownBits YKnown =
  2361. computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
  2362. // If X and Y are both non-negative (as signed values) then their sum is not
  2363. // zero unless both X and Y are zero.
  2364. if (XKnown.isNonNegative() && YKnown.isNonNegative())
  2365. if (isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) ||
  2366. isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
  2367. return true;
  2368. // If X and Y are both negative (as signed values) then their sum is not
  2369. // zero unless both X and Y equal INT_MIN.
  2370. if (XKnown.isNegative() && YKnown.isNegative()) {
  2371. APInt Mask = APInt::getSignedMaxValue(BitWidth);
  2372. // The sign bit of X is set. If some other bit is set then X is not equal
  2373. // to INT_MIN.
  2374. if (XKnown.One.intersects(Mask))
  2375. return true;
  2376. // The sign bit of Y is set. If some other bit is set then Y is not equal
  2377. // to INT_MIN.
  2378. if (YKnown.One.intersects(Mask))
  2379. return true;
  2380. }
  2381. // The sum of a non-negative number and a power of two is not zero.
  2382. if (XKnown.isNonNegative() &&
  2383. isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ false, Depth, Q))
  2384. return true;
  2385. if (YKnown.isNonNegative() &&
  2386. isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ false, Depth, Q))
  2387. return true;
  2388. break;
  2389. }
  2390. case Instruction::Mul: {
  2391. // If X and Y are non-zero then so is X * Y as long as the multiplication
  2392. // does not overflow.
  2393. const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(V);
  2394. if ((Q.IIQ.hasNoSignedWrap(BO) || Q.IIQ.hasNoUnsignedWrap(BO)) &&
  2395. isKnownNonZero(I->getOperand(0), DemandedElts, Depth, Q) &&
  2396. isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q))
  2397. return true;
  2398. break;
  2399. }
  2400. case Instruction::Select:
  2401. // (C ? X : Y) != 0 if X != 0 and Y != 0.
  2402. if (isKnownNonZero(I->getOperand(1), DemandedElts, Depth, Q) &&
  2403. isKnownNonZero(I->getOperand(2), DemandedElts, Depth, Q))
  2404. return true;
  2405. break;
  2406. case Instruction::PHI: {
  2407. auto *PN = cast<PHINode>(I);
  2408. if (Q.IIQ.UseInstrInfo && isNonZeroRecurrence(PN))
  2409. return true;
  2410. // Check if all incoming values are non-zero using recursion.
  2411. Query RecQ = Q;
  2412. unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
  2413. return llvm::all_of(PN->operands(), [&](const Use &U) {
  2414. if (U.get() == PN)
  2415. return true;
  2416. RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
  2417. return isKnownNonZero(U.get(), DemandedElts, NewDepth, RecQ);
  2418. });
  2419. }
  2420. case Instruction::ExtractElement:
  2421. if (const auto *EEI = dyn_cast<ExtractElementInst>(V)) {
  2422. const Value *Vec = EEI->getVectorOperand();
  2423. const Value *Idx = EEI->getIndexOperand();
  2424. auto *CIdx = dyn_cast<ConstantInt>(Idx);
  2425. if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
  2426. unsigned NumElts = VecTy->getNumElements();
  2427. APInt DemandedVecElts = APInt::getAllOnes(NumElts);
  2428. if (CIdx && CIdx->getValue().ult(NumElts))
  2429. DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
  2430. return isKnownNonZero(Vec, DemandedVecElts, Depth, Q);
  2431. }
  2432. }
  2433. break;
  2434. case Instruction::Freeze:
  2435. return isKnownNonZero(I->getOperand(0), Depth, Q) &&
  2436. isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
  2437. Depth);
  2438. case Instruction::Call:
  2439. if (cast<CallInst>(I)->getIntrinsicID() == Intrinsic::vscale)
  2440. return true;
  2441. break;
  2442. }
  2443. KnownBits Known(BitWidth);
  2444. computeKnownBits(V, DemandedElts, Known, Depth, Q);
  2445. return Known.One != 0;
  2446. }
  2447. bool isKnownNonZero(const Value* V, unsigned Depth, const Query& Q) {
  2448. auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
  2449. APInt DemandedElts =
  2450. FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
  2451. return isKnownNonZero(V, DemandedElts, Depth, Q);
  2452. }
  2453. /// If the pair of operators are the same invertible function, return the
  2454. /// the operands of the function corresponding to each input. Otherwise,
  2455. /// return std::nullopt. An invertible function is one that is 1-to-1 and maps
  2456. /// every input value to exactly one output value. This is equivalent to
  2457. /// saying that Op1 and Op2 are equal exactly when the specified pair of
  2458. /// operands are equal, (except that Op1 and Op2 may be poison more often.)
  2459. static std::optional<std::pair<Value*, Value*>>
  2460. getInvertibleOperands(const Operator *Op1,
  2461. const Operator *Op2) {
  2462. if (Op1->getOpcode() != Op2->getOpcode())
  2463. return std::nullopt;
  2464. auto getOperands = [&](unsigned OpNum) -> auto {
  2465. return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
  2466. };
  2467. switch (Op1->getOpcode()) {
  2468. default:
  2469. break;
  2470. case Instruction::Add:
  2471. case Instruction::Sub:
  2472. if (Op1->getOperand(0) == Op2->getOperand(0))
  2473. return getOperands(1);
  2474. if (Op1->getOperand(1) == Op2->getOperand(1))
  2475. return getOperands(0);
  2476. break;
  2477. case Instruction::Mul: {
  2478. // invertible if A * B == (A * B) mod 2^N where A, and B are integers
  2479. // and N is the bitwdith. The nsw case is non-obvious, but proven by
  2480. // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
  2481. auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
  2482. auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
  2483. if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
  2484. (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
  2485. break;
  2486. // Assume operand order has been canonicalized
  2487. if (Op1->getOperand(1) == Op2->getOperand(1) &&
  2488. isa<ConstantInt>(Op1->getOperand(1)) &&
  2489. !cast<ConstantInt>(Op1->getOperand(1))->isZero())
  2490. return getOperands(0);
  2491. break;
  2492. }
  2493. case Instruction::Shl: {
  2494. // Same as multiplies, with the difference that we don't need to check
  2495. // for a non-zero multiply. Shifts always multiply by non-zero.
  2496. auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
  2497. auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
  2498. if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
  2499. (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
  2500. break;
  2501. if (Op1->getOperand(1) == Op2->getOperand(1))
  2502. return getOperands(0);
  2503. break;
  2504. }
  2505. case Instruction::AShr:
  2506. case Instruction::LShr: {
  2507. auto *PEO1 = cast<PossiblyExactOperator>(Op1);
  2508. auto *PEO2 = cast<PossiblyExactOperator>(Op2);
  2509. if (!PEO1->isExact() || !PEO2->isExact())
  2510. break;
  2511. if (Op1->getOperand(1) == Op2->getOperand(1))
  2512. return getOperands(0);
  2513. break;
  2514. }
  2515. case Instruction::SExt:
  2516. case Instruction::ZExt:
  2517. if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
  2518. return getOperands(0);
  2519. break;
  2520. case Instruction::PHI: {
  2521. const PHINode *PN1 = cast<PHINode>(Op1);
  2522. const PHINode *PN2 = cast<PHINode>(Op2);
  2523. // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
  2524. // are a single invertible function of the start values? Note that repeated
  2525. // application of an invertible function is also invertible
  2526. BinaryOperator *BO1 = nullptr;
  2527. Value *Start1 = nullptr, *Step1 = nullptr;
  2528. BinaryOperator *BO2 = nullptr;
  2529. Value *Start2 = nullptr, *Step2 = nullptr;
  2530. if (PN1->getParent() != PN2->getParent() ||
  2531. !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
  2532. !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
  2533. break;
  2534. auto Values = getInvertibleOperands(cast<Operator>(BO1),
  2535. cast<Operator>(BO2));
  2536. if (!Values)
  2537. break;
  2538. // We have to be careful of mutually defined recurrences here. Ex:
  2539. // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
  2540. // * X_i = Y_i = X_(i-1) OP Y_(i-1)
  2541. // The invertibility of these is complicated, and not worth reasoning
  2542. // about (yet?).
  2543. if (Values->first != PN1 || Values->second != PN2)
  2544. break;
  2545. return std::make_pair(Start1, Start2);
  2546. }
  2547. }
  2548. return std::nullopt;
  2549. }
  2550. /// Return true if V2 == V1 + X, where X is known non-zero.
  2551. static bool isAddOfNonZero(const Value *V1, const Value *V2, unsigned Depth,
  2552. const Query &Q) {
  2553. const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
  2554. if (!BO || BO->getOpcode() != Instruction::Add)
  2555. return false;
  2556. Value *Op = nullptr;
  2557. if (V2 == BO->getOperand(0))
  2558. Op = BO->getOperand(1);
  2559. else if (V2 == BO->getOperand(1))
  2560. Op = BO->getOperand(0);
  2561. else
  2562. return false;
  2563. return isKnownNonZero(Op, Depth + 1, Q);
  2564. }
  2565. /// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
  2566. /// the multiplication is nuw or nsw.
  2567. static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth,
  2568. const Query &Q) {
  2569. if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
  2570. const APInt *C;
  2571. return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
  2572. (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
  2573. !C->isZero() && !C->isOne() && isKnownNonZero(V1, Depth + 1, Q);
  2574. }
  2575. return false;
  2576. }
  2577. /// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
  2578. /// the shift is nuw or nsw.
  2579. static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth,
  2580. const Query &Q) {
  2581. if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
  2582. const APInt *C;
  2583. return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
  2584. (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
  2585. !C->isZero() && isKnownNonZero(V1, Depth + 1, Q);
  2586. }
  2587. return false;
  2588. }
  2589. static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
  2590. unsigned Depth, const Query &Q) {
  2591. // Check two PHIs are in same block.
  2592. if (PN1->getParent() != PN2->getParent())
  2593. return false;
  2594. SmallPtrSet<const BasicBlock *, 8> VisitedBBs;
  2595. bool UsedFullRecursion = false;
  2596. for (const BasicBlock *IncomBB : PN1->blocks()) {
  2597. if (!VisitedBBs.insert(IncomBB).second)
  2598. continue; // Don't reprocess blocks that we have dealt with already.
  2599. const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
  2600. const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
  2601. const APInt *C1, *C2;
  2602. if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
  2603. continue;
  2604. // Only one pair of phi operands is allowed for full recursion.
  2605. if (UsedFullRecursion)
  2606. return false;
  2607. Query RecQ = Q;
  2608. RecQ.CxtI = IncomBB->getTerminator();
  2609. if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ))
  2610. return false;
  2611. UsedFullRecursion = true;
  2612. }
  2613. return true;
  2614. }
  2615. /// Return true if it is known that V1 != V2.
  2616. static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
  2617. const Query &Q) {
  2618. if (V1 == V2)
  2619. return false;
  2620. if (V1->getType() != V2->getType())
  2621. // We can't look through casts yet.
  2622. return false;
  2623. if (Depth >= MaxAnalysisRecursionDepth)
  2624. return false;
  2625. // See if we can recurse through (exactly one of) our operands. This
  2626. // requires our operation be 1-to-1 and map every input value to exactly
  2627. // one output value. Such an operation is invertible.
  2628. auto *O1 = dyn_cast<Operator>(V1);
  2629. auto *O2 = dyn_cast<Operator>(V2);
  2630. if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
  2631. if (auto Values = getInvertibleOperands(O1, O2))
  2632. return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q);
  2633. if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
  2634. const PHINode *PN2 = cast<PHINode>(V2);
  2635. // FIXME: This is missing a generalization to handle the case where one is
  2636. // a PHI and another one isn't.
  2637. if (isNonEqualPHIs(PN1, PN2, Depth, Q))
  2638. return true;
  2639. };
  2640. }
  2641. if (isAddOfNonZero(V1, V2, Depth, Q) || isAddOfNonZero(V2, V1, Depth, Q))
  2642. return true;
  2643. if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q))
  2644. return true;
  2645. if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q))
  2646. return true;
  2647. if (V1->getType()->isIntOrIntVectorTy()) {
  2648. // Are any known bits in V1 contradictory to known bits in V2? If V1
  2649. // has a known zero where V2 has a known one, they must not be equal.
  2650. KnownBits Known1 = computeKnownBits(V1, Depth, Q);
  2651. KnownBits Known2 = computeKnownBits(V2, Depth, Q);
  2652. if (Known1.Zero.intersects(Known2.One) ||
  2653. Known2.Zero.intersects(Known1.One))
  2654. return true;
  2655. }
  2656. return false;
  2657. }
  2658. /// Return true if 'V & Mask' is known to be zero. We use this predicate to
  2659. /// simplify operations downstream. Mask is known to be zero for bits that V
  2660. /// cannot have.
  2661. ///
  2662. /// This function is defined on values with integer type, values with pointer
  2663. /// type, and vectors of integers. In the case
  2664. /// where V is a vector, the mask, known zero, and known one values are the
  2665. /// same width as the vector element, and the bit is set only if it is true
  2666. /// for all of the elements in the vector.
  2667. bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth,
  2668. const Query &Q) {
  2669. KnownBits Known(Mask.getBitWidth());
  2670. computeKnownBits(V, Known, Depth, Q);
  2671. return Mask.isSubsetOf(Known.Zero);
  2672. }
  2673. // Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
  2674. // Returns the input and lower/upper bounds.
  2675. static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
  2676. const APInt *&CLow, const APInt *&CHigh) {
  2677. assert(isa<Operator>(Select) &&
  2678. cast<Operator>(Select)->getOpcode() == Instruction::Select &&
  2679. "Input should be a Select!");
  2680. const Value *LHS = nullptr, *RHS = nullptr;
  2681. SelectPatternFlavor SPF = matchSelectPattern(Select, LHS, RHS).Flavor;
  2682. if (SPF != SPF_SMAX && SPF != SPF_SMIN)
  2683. return false;
  2684. if (!match(RHS, m_APInt(CLow)))
  2685. return false;
  2686. const Value *LHS2 = nullptr, *RHS2 = nullptr;
  2687. SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor;
  2688. if (getInverseMinMaxFlavor(SPF) != SPF2)
  2689. return false;
  2690. if (!match(RHS2, m_APInt(CHigh)))
  2691. return false;
  2692. if (SPF == SPF_SMIN)
  2693. std::swap(CLow, CHigh);
  2694. In = LHS2;
  2695. return CLow->sle(*CHigh);
  2696. }
  2697. static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II,
  2698. const APInt *&CLow,
  2699. const APInt *&CHigh) {
  2700. assert((II->getIntrinsicID() == Intrinsic::smin ||
  2701. II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax");
  2702. Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
  2703. auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
  2704. if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
  2705. !match(II->getArgOperand(1), m_APInt(CLow)) ||
  2706. !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
  2707. return false;
  2708. if (II->getIntrinsicID() == Intrinsic::smin)
  2709. std::swap(CLow, CHigh);
  2710. return CLow->sle(*CHigh);
  2711. }
  2712. /// For vector constants, loop over the elements and find the constant with the
  2713. /// minimum number of sign bits. Return 0 if the value is not a vector constant
  2714. /// or if any element was not analyzed; otherwise, return the count for the
  2715. /// element with the minimum number of sign bits.
  2716. static unsigned computeNumSignBitsVectorConstant(const Value *V,
  2717. const APInt &DemandedElts,
  2718. unsigned TyBits) {
  2719. const auto *CV = dyn_cast<Constant>(V);
  2720. if (!CV || !isa<FixedVectorType>(CV->getType()))
  2721. return 0;
  2722. unsigned MinSignBits = TyBits;
  2723. unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
  2724. for (unsigned i = 0; i != NumElts; ++i) {
  2725. if (!DemandedElts[i])
  2726. continue;
  2727. // If we find a non-ConstantInt, bail out.
  2728. auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
  2729. if (!Elt)
  2730. return 0;
  2731. MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
  2732. }
  2733. return MinSignBits;
  2734. }
  2735. static unsigned ComputeNumSignBitsImpl(const Value *V,
  2736. const APInt &DemandedElts,
  2737. unsigned Depth, const Query &Q);
  2738. static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
  2739. unsigned Depth, const Query &Q) {
  2740. unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
  2741. assert(Result > 0 && "At least one sign bit needs to be present!");
  2742. return Result;
  2743. }
  2744. /// Return the number of times the sign bit of the register is replicated into
  2745. /// the other bits. We know that at least 1 bit is always equal to the sign bit
  2746. /// (itself), but other cases can give us information. For example, immediately
  2747. /// after an "ashr X, 2", we know that the top 3 bits are all equal to each
  2748. /// other, so we return 3. For vectors, return the number of sign bits for the
  2749. /// vector element with the minimum number of known sign bits of the demanded
  2750. /// elements in the vector specified by DemandedElts.
  2751. static unsigned ComputeNumSignBitsImpl(const Value *V,
  2752. const APInt &DemandedElts,
  2753. unsigned Depth, const Query &Q) {
  2754. Type *Ty = V->getType();
  2755. #ifndef NDEBUG
  2756. assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
  2757. if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
  2758. assert(
  2759. FVTy->getNumElements() == DemandedElts.getBitWidth() &&
  2760. "DemandedElt width should equal the fixed vector number of elements");
  2761. } else {
  2762. assert(DemandedElts == APInt(1, 1) &&
  2763. "DemandedElt width should be 1 for scalars");
  2764. }
  2765. #endif
  2766. // We return the minimum number of sign bits that are guaranteed to be present
  2767. // in V, so for undef we have to conservatively return 1. We don't have the
  2768. // same behavior for poison though -- that's a FIXME today.
  2769. Type *ScalarTy = Ty->getScalarType();
  2770. unsigned TyBits = ScalarTy->isPointerTy() ?
  2771. Q.DL.getPointerTypeSizeInBits(ScalarTy) :
  2772. Q.DL.getTypeSizeInBits(ScalarTy);
  2773. unsigned Tmp, Tmp2;
  2774. unsigned FirstAnswer = 1;
  2775. // Note that ConstantInt is handled by the general computeKnownBits case
  2776. // below.
  2777. if (Depth == MaxAnalysisRecursionDepth)
  2778. return 1;
  2779. if (auto *U = dyn_cast<Operator>(V)) {
  2780. switch (Operator::getOpcode(V)) {
  2781. default: break;
  2782. case Instruction::SExt:
  2783. Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
  2784. return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
  2785. case Instruction::SDiv: {
  2786. const APInt *Denominator;
  2787. // sdiv X, C -> adds log(C) sign bits.
  2788. if (match(U->getOperand(1), m_APInt(Denominator))) {
  2789. // Ignore non-positive denominator.
  2790. if (!Denominator->isStrictlyPositive())
  2791. break;
  2792. // Calculate the incoming numerator bits.
  2793. unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2794. // Add floor(log(C)) bits to the numerator bits.
  2795. return std::min(TyBits, NumBits + Denominator->logBase2());
  2796. }
  2797. break;
  2798. }
  2799. case Instruction::SRem: {
  2800. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2801. const APInt *Denominator;
  2802. // srem X, C -> we know that the result is within [-C+1,C) when C is a
  2803. // positive constant. This let us put a lower bound on the number of sign
  2804. // bits.
  2805. if (match(U->getOperand(1), m_APInt(Denominator))) {
  2806. // Ignore non-positive denominator.
  2807. if (Denominator->isStrictlyPositive()) {
  2808. // Calculate the leading sign bit constraints by examining the
  2809. // denominator. Given that the denominator is positive, there are two
  2810. // cases:
  2811. //
  2812. // 1. The numerator is positive. The result range is [0,C) and
  2813. // [0,C) u< (1 << ceilLogBase2(C)).
  2814. //
  2815. // 2. The numerator is negative. Then the result range is (-C,0] and
  2816. // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
  2817. //
  2818. // Thus a lower bound on the number of sign bits is `TyBits -
  2819. // ceilLogBase2(C)`.
  2820. unsigned ResBits = TyBits - Denominator->ceilLogBase2();
  2821. Tmp = std::max(Tmp, ResBits);
  2822. }
  2823. }
  2824. return Tmp;
  2825. }
  2826. case Instruction::AShr: {
  2827. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2828. // ashr X, C -> adds C sign bits. Vectors too.
  2829. const APInt *ShAmt;
  2830. if (match(U->getOperand(1), m_APInt(ShAmt))) {
  2831. if (ShAmt->uge(TyBits))
  2832. break; // Bad shift.
  2833. unsigned ShAmtLimited = ShAmt->getZExtValue();
  2834. Tmp += ShAmtLimited;
  2835. if (Tmp > TyBits) Tmp = TyBits;
  2836. }
  2837. return Tmp;
  2838. }
  2839. case Instruction::Shl: {
  2840. const APInt *ShAmt;
  2841. if (match(U->getOperand(1), m_APInt(ShAmt))) {
  2842. // shl destroys sign bits.
  2843. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2844. if (ShAmt->uge(TyBits) || // Bad shift.
  2845. ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
  2846. Tmp2 = ShAmt->getZExtValue();
  2847. return Tmp - Tmp2;
  2848. }
  2849. break;
  2850. }
  2851. case Instruction::And:
  2852. case Instruction::Or:
  2853. case Instruction::Xor: // NOT is handled here.
  2854. // Logical binary ops preserve the number of sign bits at the worst.
  2855. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2856. if (Tmp != 1) {
  2857. Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2858. FirstAnswer = std::min(Tmp, Tmp2);
  2859. // We computed what we know about the sign bits as our first
  2860. // answer. Now proceed to the generic code that uses
  2861. // computeKnownBits, and pick whichever answer is better.
  2862. }
  2863. break;
  2864. case Instruction::Select: {
  2865. // If we have a clamp pattern, we know that the number of sign bits will
  2866. // be the minimum of the clamp min/max range.
  2867. const Value *X;
  2868. const APInt *CLow, *CHigh;
  2869. if (isSignedMinMaxClamp(U, X, CLow, CHigh))
  2870. return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
  2871. Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2872. if (Tmp == 1) break;
  2873. Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
  2874. return std::min(Tmp, Tmp2);
  2875. }
  2876. case Instruction::Add:
  2877. // Add can have at most one carry bit. Thus we know that the output
  2878. // is, at worst, one more bit than the inputs.
  2879. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2880. if (Tmp == 1) break;
  2881. // Special case decrementing a value (ADD X, -1):
  2882. if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
  2883. if (CRHS->isAllOnesValue()) {
  2884. KnownBits Known(TyBits);
  2885. computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
  2886. // If the input is known to be 0 or 1, the output is 0/-1, which is
  2887. // all sign bits set.
  2888. if ((Known.Zero | 1).isAllOnes())
  2889. return TyBits;
  2890. // If we are subtracting one from a positive number, there is no carry
  2891. // out of the result.
  2892. if (Known.isNonNegative())
  2893. return Tmp;
  2894. }
  2895. Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2896. if (Tmp2 == 1) break;
  2897. return std::min(Tmp, Tmp2) - 1;
  2898. case Instruction::Sub:
  2899. Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2900. if (Tmp2 == 1) break;
  2901. // Handle NEG.
  2902. if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
  2903. if (CLHS->isNullValue()) {
  2904. KnownBits Known(TyBits);
  2905. computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
  2906. // If the input is known to be 0 or 1, the output is 0/-1, which is
  2907. // all sign bits set.
  2908. if ((Known.Zero | 1).isAllOnes())
  2909. return TyBits;
  2910. // If the input is known to be positive (the sign bit is known clear),
  2911. // the output of the NEG has the same number of sign bits as the
  2912. // input.
  2913. if (Known.isNonNegative())
  2914. return Tmp2;
  2915. // Otherwise, we treat this like a SUB.
  2916. }
  2917. // Sub can have at most one carry bit. Thus we know that the output
  2918. // is, at worst, one more bit than the inputs.
  2919. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2920. if (Tmp == 1) break;
  2921. return std::min(Tmp, Tmp2) - 1;
  2922. case Instruction::Mul: {
  2923. // The output of the Mul can be at most twice the valid bits in the
  2924. // inputs.
  2925. unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2926. if (SignBitsOp0 == 1) break;
  2927. unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
  2928. if (SignBitsOp1 == 1) break;
  2929. unsigned OutValidBits =
  2930. (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
  2931. return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
  2932. }
  2933. case Instruction::PHI: {
  2934. const PHINode *PN = cast<PHINode>(U);
  2935. unsigned NumIncomingValues = PN->getNumIncomingValues();
  2936. // Don't analyze large in-degree PHIs.
  2937. if (NumIncomingValues > 4) break;
  2938. // Unreachable blocks may have zero-operand PHI nodes.
  2939. if (NumIncomingValues == 0) break;
  2940. // Take the minimum of all incoming values. This can't infinitely loop
  2941. // because of our depth threshold.
  2942. Query RecQ = Q;
  2943. Tmp = TyBits;
  2944. for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
  2945. if (Tmp == 1) return Tmp;
  2946. RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
  2947. Tmp = std::min(
  2948. Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ));
  2949. }
  2950. return Tmp;
  2951. }
  2952. case Instruction::Trunc: {
  2953. // If the input contained enough sign bits that some remain after the
  2954. // truncation, then we can make use of that. Otherwise we don't know
  2955. // anything.
  2956. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2957. unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
  2958. if (Tmp > (OperandTyBits - TyBits))
  2959. return Tmp - (OperandTyBits - TyBits);
  2960. return 1;
  2961. }
  2962. case Instruction::ExtractElement:
  2963. // Look through extract element. At the moment we keep this simple and
  2964. // skip tracking the specific element. But at least we might find
  2965. // information valid for all elements of the vector (for example if vector
  2966. // is sign extended, shifted, etc).
  2967. return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  2968. case Instruction::ShuffleVector: {
  2969. // Collect the minimum number of sign bits that are shared by every vector
  2970. // element referenced by the shuffle.
  2971. auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
  2972. if (!Shuf) {
  2973. // FIXME: Add support for shufflevector constant expressions.
  2974. return 1;
  2975. }
  2976. APInt DemandedLHS, DemandedRHS;
  2977. // For undef elements, we don't know anything about the common state of
  2978. // the shuffle result.
  2979. if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
  2980. return 1;
  2981. Tmp = std::numeric_limits<unsigned>::max();
  2982. if (!!DemandedLHS) {
  2983. const Value *LHS = Shuf->getOperand(0);
  2984. Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
  2985. }
  2986. // If we don't know anything, early out and try computeKnownBits
  2987. // fall-back.
  2988. if (Tmp == 1)
  2989. break;
  2990. if (!!DemandedRHS) {
  2991. const Value *RHS = Shuf->getOperand(1);
  2992. Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
  2993. Tmp = std::min(Tmp, Tmp2);
  2994. }
  2995. // If we don't know anything, early out and try computeKnownBits
  2996. // fall-back.
  2997. if (Tmp == 1)
  2998. break;
  2999. assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
  3000. return Tmp;
  3001. }
  3002. case Instruction::Call: {
  3003. if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
  3004. switch (II->getIntrinsicID()) {
  3005. default: break;
  3006. case Intrinsic::abs:
  3007. Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
  3008. if (Tmp == 1) break;
  3009. // Absolute value reduces number of sign bits by at most 1.
  3010. return Tmp - 1;
  3011. case Intrinsic::smin:
  3012. case Intrinsic::smax: {
  3013. const APInt *CLow, *CHigh;
  3014. if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
  3015. return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
  3016. }
  3017. }
  3018. }
  3019. }
  3020. }
  3021. }
  3022. // Finally, if we can prove that the top bits of the result are 0's or 1's,
  3023. // use this information.
  3024. // If we can examine all elements of a vector constant successfully, we're
  3025. // done (we can't do any better than that). If not, keep trying.
  3026. if (unsigned VecSignBits =
  3027. computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
  3028. return VecSignBits;
  3029. KnownBits Known(TyBits);
  3030. computeKnownBits(V, DemandedElts, Known, Depth, Q);
  3031. // If we know that the sign bit is either zero or one, determine the number of
  3032. // identical bits in the top of the input value.
  3033. return std::max(FirstAnswer, Known.countMinSignBits());
  3034. }
  3035. Intrinsic::ID llvm::getIntrinsicForCallSite(const CallBase &CB,
  3036. const TargetLibraryInfo *TLI) {
  3037. const Function *F = CB.getCalledFunction();
  3038. if (!F)
  3039. return Intrinsic::not_intrinsic;
  3040. if (F->isIntrinsic())
  3041. return F->getIntrinsicID();
  3042. // We are going to infer semantics of a library function based on mapping it
  3043. // to an LLVM intrinsic. Check that the library function is available from
  3044. // this callbase and in this environment.
  3045. LibFunc Func;
  3046. if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
  3047. !CB.onlyReadsMemory())
  3048. return Intrinsic::not_intrinsic;
  3049. switch (Func) {
  3050. default:
  3051. break;
  3052. case LibFunc_sin:
  3053. case LibFunc_sinf:
  3054. case LibFunc_sinl:
  3055. return Intrinsic::sin;
  3056. case LibFunc_cos:
  3057. case LibFunc_cosf:
  3058. case LibFunc_cosl:
  3059. return Intrinsic::cos;
  3060. case LibFunc_exp:
  3061. case LibFunc_expf:
  3062. case LibFunc_expl:
  3063. return Intrinsic::exp;
  3064. case LibFunc_exp2:
  3065. case LibFunc_exp2f:
  3066. case LibFunc_exp2l:
  3067. return Intrinsic::exp2;
  3068. case LibFunc_log:
  3069. case LibFunc_logf:
  3070. case LibFunc_logl:
  3071. return Intrinsic::log;
  3072. case LibFunc_log10:
  3073. case LibFunc_log10f:
  3074. case LibFunc_log10l:
  3075. return Intrinsic::log10;
  3076. case LibFunc_log2:
  3077. case LibFunc_log2f:
  3078. case LibFunc_log2l:
  3079. return Intrinsic::log2;
  3080. case LibFunc_fabs:
  3081. case LibFunc_fabsf:
  3082. case LibFunc_fabsl:
  3083. return Intrinsic::fabs;
  3084. case LibFunc_fmin:
  3085. case LibFunc_fminf:
  3086. case LibFunc_fminl:
  3087. return Intrinsic::minnum;
  3088. case LibFunc_fmax:
  3089. case LibFunc_fmaxf:
  3090. case LibFunc_fmaxl:
  3091. return Intrinsic::maxnum;
  3092. case LibFunc_copysign:
  3093. case LibFunc_copysignf:
  3094. case LibFunc_copysignl:
  3095. return Intrinsic::copysign;
  3096. case LibFunc_floor:
  3097. case LibFunc_floorf:
  3098. case LibFunc_floorl:
  3099. return Intrinsic::floor;
  3100. case LibFunc_ceil:
  3101. case LibFunc_ceilf:
  3102. case LibFunc_ceill:
  3103. return Intrinsic::ceil;
  3104. case LibFunc_trunc:
  3105. case LibFunc_truncf:
  3106. case LibFunc_truncl:
  3107. return Intrinsic::trunc;
  3108. case LibFunc_rint:
  3109. case LibFunc_rintf:
  3110. case LibFunc_rintl:
  3111. return Intrinsic::rint;
  3112. case LibFunc_nearbyint:
  3113. case LibFunc_nearbyintf:
  3114. case LibFunc_nearbyintl:
  3115. return Intrinsic::nearbyint;
  3116. case LibFunc_round:
  3117. case LibFunc_roundf:
  3118. case LibFunc_roundl:
  3119. return Intrinsic::round;
  3120. case LibFunc_roundeven:
  3121. case LibFunc_roundevenf:
  3122. case LibFunc_roundevenl:
  3123. return Intrinsic::roundeven;
  3124. case LibFunc_pow:
  3125. case LibFunc_powf:
  3126. case LibFunc_powl:
  3127. return Intrinsic::pow;
  3128. case LibFunc_sqrt:
  3129. case LibFunc_sqrtf:
  3130. case LibFunc_sqrtl:
  3131. return Intrinsic::sqrt;
  3132. }
  3133. return Intrinsic::not_intrinsic;
  3134. }
  3135. /// Return true if we can prove that the specified FP value is never equal to
  3136. /// -0.0.
  3137. /// NOTE: Do not check 'nsz' here because that fast-math-flag does not guarantee
  3138. /// that a value is not -0.0. It only guarantees that -0.0 may be treated
  3139. /// the same as +0.0 in floating-point ops.
  3140. bool llvm::CannotBeNegativeZero(const Value *V, const TargetLibraryInfo *TLI,
  3141. unsigned Depth) {
  3142. if (auto *CFP = dyn_cast<ConstantFP>(V))
  3143. return !CFP->getValueAPF().isNegZero();
  3144. if (Depth == MaxAnalysisRecursionDepth)
  3145. return false;
  3146. auto *Op = dyn_cast<Operator>(V);
  3147. if (!Op)
  3148. return false;
  3149. // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
  3150. if (match(Op, m_FAdd(m_Value(), m_PosZeroFP())))
  3151. return true;
  3152. // sitofp and uitofp turn into +0.0 for zero.
  3153. if (isa<SIToFPInst>(Op) || isa<UIToFPInst>(Op))
  3154. return true;
  3155. if (auto *Call = dyn_cast<CallInst>(Op)) {
  3156. Intrinsic::ID IID = getIntrinsicForCallSite(*Call, TLI);
  3157. switch (IID) {
  3158. default:
  3159. break;
  3160. // sqrt(-0.0) = -0.0, no other negative results are possible.
  3161. case Intrinsic::sqrt:
  3162. case Intrinsic::canonicalize:
  3163. return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1);
  3164. case Intrinsic::experimental_constrained_sqrt: {
  3165. // NOTE: This rounding mode restriction may be too strict.
  3166. const auto *CI = cast<ConstrainedFPIntrinsic>(Call);
  3167. if (CI->getRoundingMode() == RoundingMode::NearestTiesToEven)
  3168. return CannotBeNegativeZero(Call->getArgOperand(0), TLI, Depth + 1);
  3169. else
  3170. return false;
  3171. }
  3172. // fabs(x) != -0.0
  3173. case Intrinsic::fabs:
  3174. return true;
  3175. // sitofp and uitofp turn into +0.0 for zero.
  3176. case Intrinsic::experimental_constrained_sitofp:
  3177. case Intrinsic::experimental_constrained_uitofp:
  3178. return true;
  3179. }
  3180. }
  3181. return false;
  3182. }
  3183. /// If \p SignBitOnly is true, test for a known 0 sign bit rather than a
  3184. /// standard ordered compare. e.g. make -0.0 olt 0.0 be true because of the sign
  3185. /// bit despite comparing equal.
  3186. static bool cannotBeOrderedLessThanZeroImpl(const Value *V,
  3187. const TargetLibraryInfo *TLI,
  3188. bool SignBitOnly,
  3189. unsigned Depth) {
  3190. // TODO: This function does not do the right thing when SignBitOnly is true
  3191. // and we're lowering to a hypothetical IEEE 754-compliant-but-evil platform
  3192. // which flips the sign bits of NaNs. See
  3193. // https://llvm.org/bugs/show_bug.cgi?id=31702.
  3194. if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V)) {
  3195. return !CFP->getValueAPF().isNegative() ||
  3196. (!SignBitOnly && CFP->getValueAPF().isZero());
  3197. }
  3198. // Handle vector of constants.
  3199. if (auto *CV = dyn_cast<Constant>(V)) {
  3200. if (auto *CVFVTy = dyn_cast<FixedVectorType>(CV->getType())) {
  3201. unsigned NumElts = CVFVTy->getNumElements();
  3202. for (unsigned i = 0; i != NumElts; ++i) {
  3203. auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
  3204. if (!CFP)
  3205. return false;
  3206. if (CFP->getValueAPF().isNegative() &&
  3207. (SignBitOnly || !CFP->getValueAPF().isZero()))
  3208. return false;
  3209. }
  3210. // All non-negative ConstantFPs.
  3211. return true;
  3212. }
  3213. }
  3214. if (Depth == MaxAnalysisRecursionDepth)
  3215. return false;
  3216. const Operator *I = dyn_cast<Operator>(V);
  3217. if (!I)
  3218. return false;
  3219. switch (I->getOpcode()) {
  3220. default:
  3221. break;
  3222. // Unsigned integers are always nonnegative.
  3223. case Instruction::UIToFP:
  3224. return true;
  3225. case Instruction::FDiv:
  3226. // X / X is always exactly 1.0 or a NaN.
  3227. if (I->getOperand(0) == I->getOperand(1) &&
  3228. (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
  3229. return true;
  3230. // Set SignBitOnly for RHS, because X / -0.0 is -Inf (or NaN).
  3231. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3232. Depth + 1) &&
  3233. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI,
  3234. /*SignBitOnly*/ true, Depth + 1);
  3235. case Instruction::FMul:
  3236. // X * X is always non-negative or a NaN.
  3237. if (I->getOperand(0) == I->getOperand(1) &&
  3238. (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()))
  3239. return true;
  3240. [[fallthrough]];
  3241. case Instruction::FAdd:
  3242. case Instruction::FRem:
  3243. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3244. Depth + 1) &&
  3245. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  3246. Depth + 1);
  3247. case Instruction::Select:
  3248. return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  3249. Depth + 1) &&
  3250. cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
  3251. Depth + 1);
  3252. case Instruction::FPExt:
  3253. case Instruction::FPTrunc:
  3254. // Widening/narrowing never change sign.
  3255. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3256. Depth + 1);
  3257. case Instruction::ExtractElement:
  3258. // Look through extract element. At the moment we keep this simple and skip
  3259. // tracking the specific element. But at least we might find information
  3260. // valid for all elements of the vector.
  3261. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3262. Depth + 1);
  3263. case Instruction::Call:
  3264. const auto *CI = cast<CallInst>(I);
  3265. Intrinsic::ID IID = getIntrinsicForCallSite(*CI, TLI);
  3266. switch (IID) {
  3267. default:
  3268. break;
  3269. case Intrinsic::canonicalize:
  3270. case Intrinsic::arithmetic_fence:
  3271. case Intrinsic::floor:
  3272. case Intrinsic::ceil:
  3273. case Intrinsic::trunc:
  3274. case Intrinsic::rint:
  3275. case Intrinsic::nearbyint:
  3276. case Intrinsic::round:
  3277. case Intrinsic::roundeven:
  3278. case Intrinsic::fptrunc_round:
  3279. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly, Depth + 1);
  3280. case Intrinsic::maxnum: {
  3281. Value *V0 = I->getOperand(0), *V1 = I->getOperand(1);
  3282. auto isPositiveNum = [&](Value *V) {
  3283. if (SignBitOnly) {
  3284. // With SignBitOnly, this is tricky because the result of
  3285. // maxnum(+0.0, -0.0) is unspecified. Just check if the operand is
  3286. // a constant strictly greater than 0.0.
  3287. const APFloat *C;
  3288. return match(V, m_APFloat(C)) &&
  3289. *C > APFloat::getZero(C->getSemantics());
  3290. }
  3291. // -0.0 compares equal to 0.0, so if this operand is at least -0.0,
  3292. // maxnum can't be ordered-less-than-zero.
  3293. return isKnownNeverNaN(V, TLI) &&
  3294. cannotBeOrderedLessThanZeroImpl(V, TLI, false, Depth + 1);
  3295. };
  3296. // TODO: This could be improved. We could also check that neither operand
  3297. // has its sign bit set (and at least 1 is not-NAN?).
  3298. return isPositiveNum(V0) || isPositiveNum(V1);
  3299. }
  3300. case Intrinsic::maximum:
  3301. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3302. Depth + 1) ||
  3303. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  3304. Depth + 1);
  3305. case Intrinsic::minnum:
  3306. case Intrinsic::minimum:
  3307. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3308. Depth + 1) &&
  3309. cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, SignBitOnly,
  3310. Depth + 1);
  3311. case Intrinsic::exp:
  3312. case Intrinsic::exp2:
  3313. case Intrinsic::fabs:
  3314. return true;
  3315. case Intrinsic::copysign:
  3316. // Only the sign operand matters.
  3317. return cannotBeOrderedLessThanZeroImpl(I->getOperand(1), TLI, true,
  3318. Depth + 1);
  3319. case Intrinsic::sqrt:
  3320. // sqrt(x) is always >= -0 or NaN. Moreover, sqrt(x) == -0 iff x == -0.
  3321. if (!SignBitOnly)
  3322. return true;
  3323. return CI->hasNoNaNs() && (CI->hasNoSignedZeros() ||
  3324. CannotBeNegativeZero(CI->getOperand(0), TLI));
  3325. case Intrinsic::powi:
  3326. if (ConstantInt *Exponent = dyn_cast<ConstantInt>(I->getOperand(1))) {
  3327. // powi(x,n) is non-negative if n is even.
  3328. if (Exponent->getBitWidth() <= 64 && Exponent->getSExtValue() % 2u == 0)
  3329. return true;
  3330. }
  3331. // TODO: This is not correct. Given that exp is an integer, here are the
  3332. // ways that pow can return a negative value:
  3333. //
  3334. // pow(x, exp) --> negative if exp is odd and x is negative.
  3335. // pow(-0, exp) --> -inf if exp is negative odd.
  3336. // pow(-0, exp) --> -0 if exp is positive odd.
  3337. // pow(-inf, exp) --> -0 if exp is negative odd.
  3338. // pow(-inf, exp) --> -inf if exp is positive odd.
  3339. //
  3340. // Therefore, if !SignBitOnly, we can return true if x >= +0 or x is NaN,
  3341. // but we must return false if x == -0. Unfortunately we do not currently
  3342. // have a way of expressing this constraint. See details in
  3343. // https://llvm.org/bugs/show_bug.cgi?id=31702.
  3344. return cannotBeOrderedLessThanZeroImpl(I->getOperand(0), TLI, SignBitOnly,
  3345. Depth + 1);
  3346. case Intrinsic::fma:
  3347. case Intrinsic::fmuladd:
  3348. // x*x+y is non-negative if y is non-negative.
  3349. return I->getOperand(0) == I->getOperand(1) &&
  3350. (!SignBitOnly || cast<FPMathOperator>(I)->hasNoNaNs()) &&
  3351. cannotBeOrderedLessThanZeroImpl(I->getOperand(2), TLI, SignBitOnly,
  3352. Depth + 1);
  3353. }
  3354. break;
  3355. }
  3356. return false;
  3357. }
  3358. bool llvm::CannotBeOrderedLessThanZero(const Value *V,
  3359. const TargetLibraryInfo *TLI) {
  3360. return cannotBeOrderedLessThanZeroImpl(V, TLI, false, 0);
  3361. }
  3362. bool llvm::SignBitMustBeZero(const Value *V, const TargetLibraryInfo *TLI) {
  3363. return cannotBeOrderedLessThanZeroImpl(V, TLI, true, 0);
  3364. }
  3365. bool llvm::isKnownNeverInfinity(const Value *V, const TargetLibraryInfo *TLI,
  3366. unsigned Depth) {
  3367. assert(V->getType()->isFPOrFPVectorTy() && "Querying for Inf on non-FP type");
  3368. // If we're told that infinities won't happen, assume they won't.
  3369. if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
  3370. if (FPMathOp->hasNoInfs())
  3371. return true;
  3372. // Handle scalar constants.
  3373. if (auto *CFP = dyn_cast<ConstantFP>(V))
  3374. return !CFP->isInfinity();
  3375. if (Depth == MaxAnalysisRecursionDepth)
  3376. return false;
  3377. if (auto *Inst = dyn_cast<Instruction>(V)) {
  3378. switch (Inst->getOpcode()) {
  3379. case Instruction::Select: {
  3380. return isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1) &&
  3381. isKnownNeverInfinity(Inst->getOperand(2), TLI, Depth + 1);
  3382. }
  3383. case Instruction::SIToFP:
  3384. case Instruction::UIToFP: {
  3385. // Get width of largest magnitude integer (remove a bit if signed).
  3386. // This still works for a signed minimum value because the largest FP
  3387. // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
  3388. int IntSize = Inst->getOperand(0)->getType()->getScalarSizeInBits();
  3389. if (Inst->getOpcode() == Instruction::SIToFP)
  3390. --IntSize;
  3391. // If the exponent of the largest finite FP value can hold the largest
  3392. // integer, the result of the cast must be finite.
  3393. Type *FPTy = Inst->getType()->getScalarType();
  3394. return ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize;
  3395. }
  3396. case Instruction::FNeg:
  3397. case Instruction::FPExt: {
  3398. // Peek through to source op. If it is not infinity, this is not infinity.
  3399. return isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1);
  3400. }
  3401. case Instruction::FPTrunc: {
  3402. // Need a range check.
  3403. return false;
  3404. }
  3405. default:
  3406. break;
  3407. }
  3408. if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
  3409. switch (II->getIntrinsicID()) {
  3410. case Intrinsic::sin:
  3411. case Intrinsic::cos:
  3412. // Return NaN on infinite inputs.
  3413. return true;
  3414. case Intrinsic::fabs:
  3415. case Intrinsic::sqrt:
  3416. case Intrinsic::canonicalize:
  3417. case Intrinsic::copysign:
  3418. case Intrinsic::arithmetic_fence:
  3419. case Intrinsic::trunc:
  3420. return isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1);
  3421. case Intrinsic::floor:
  3422. case Intrinsic::ceil:
  3423. case Intrinsic::rint:
  3424. case Intrinsic::nearbyint:
  3425. case Intrinsic::round:
  3426. case Intrinsic::roundeven:
  3427. // PPC_FP128 is a special case.
  3428. if (V->getType()->isMultiUnitFPType())
  3429. return false;
  3430. return isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1);
  3431. case Intrinsic::fptrunc_round:
  3432. // Requires knowing the value range.
  3433. return false;
  3434. case Intrinsic::minnum:
  3435. case Intrinsic::maxnum:
  3436. case Intrinsic::minimum:
  3437. case Intrinsic::maximum:
  3438. return isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) &&
  3439. isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1);
  3440. case Intrinsic::log:
  3441. case Intrinsic::log10:
  3442. case Intrinsic::log2:
  3443. // log(+inf) -> +inf
  3444. // log([+-]0.0) -> -inf
  3445. // log(-inf) -> nan
  3446. // log(-x) -> nan
  3447. // TODO: We lack API to check the == 0 case.
  3448. return false;
  3449. case Intrinsic::exp:
  3450. case Intrinsic::exp2:
  3451. case Intrinsic::pow:
  3452. case Intrinsic::powi:
  3453. case Intrinsic::fma:
  3454. case Intrinsic::fmuladd:
  3455. // These can return infinities on overflow cases, so it's hard to prove
  3456. // anything about it.
  3457. return false;
  3458. default:
  3459. break;
  3460. }
  3461. }
  3462. }
  3463. // try to handle fixed width vector constants
  3464. auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
  3465. if (VFVTy && isa<Constant>(V)) {
  3466. // For vectors, verify that each element is not infinity.
  3467. unsigned NumElts = VFVTy->getNumElements();
  3468. for (unsigned i = 0; i != NumElts; ++i) {
  3469. Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
  3470. if (!Elt)
  3471. return false;
  3472. if (isa<UndefValue>(Elt))
  3473. continue;
  3474. auto *CElt = dyn_cast<ConstantFP>(Elt);
  3475. if (!CElt || CElt->isInfinity())
  3476. return false;
  3477. }
  3478. // All elements were confirmed non-infinity or undefined.
  3479. return true;
  3480. }
  3481. // was not able to prove that V never contains infinity
  3482. return false;
  3483. }
  3484. bool llvm::isKnownNeverNaN(const Value *V, const TargetLibraryInfo *TLI,
  3485. unsigned Depth) {
  3486. assert(V->getType()->isFPOrFPVectorTy() && "Querying for NaN on non-FP type");
  3487. // If we're told that NaNs won't happen, assume they won't.
  3488. if (auto *FPMathOp = dyn_cast<FPMathOperator>(V))
  3489. if (FPMathOp->hasNoNaNs())
  3490. return true;
  3491. // Handle scalar constants.
  3492. if (auto *CFP = dyn_cast<ConstantFP>(V))
  3493. return !CFP->isNaN();
  3494. if (Depth == MaxAnalysisRecursionDepth)
  3495. return false;
  3496. if (auto *Inst = dyn_cast<Instruction>(V)) {
  3497. switch (Inst->getOpcode()) {
  3498. case Instruction::FAdd:
  3499. case Instruction::FSub:
  3500. // Adding positive and negative infinity produces NaN.
  3501. return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1) &&
  3502. isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
  3503. (isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) ||
  3504. isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1));
  3505. case Instruction::FMul:
  3506. // Zero multiplied with infinity produces NaN.
  3507. // FIXME: If neither side can be zero fmul never produces NaN.
  3508. return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1) &&
  3509. isKnownNeverInfinity(Inst->getOperand(0), TLI, Depth + 1) &&
  3510. isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
  3511. isKnownNeverInfinity(Inst->getOperand(1), TLI, Depth + 1);
  3512. case Instruction::FDiv:
  3513. case Instruction::FRem:
  3514. // FIXME: Only 0/0, Inf/Inf, Inf REM x and x REM 0 produce NaN.
  3515. return false;
  3516. case Instruction::Select: {
  3517. return isKnownNeverNaN(Inst->getOperand(1), TLI, Depth + 1) &&
  3518. isKnownNeverNaN(Inst->getOperand(2), TLI, Depth + 1);
  3519. }
  3520. case Instruction::SIToFP:
  3521. case Instruction::UIToFP:
  3522. return true;
  3523. case Instruction::FPTrunc:
  3524. case Instruction::FPExt:
  3525. case Instruction::FNeg:
  3526. return isKnownNeverNaN(Inst->getOperand(0), TLI, Depth + 1);
  3527. default:
  3528. break;
  3529. }
  3530. }
  3531. if (const auto *II = dyn_cast<IntrinsicInst>(V)) {
  3532. switch (II->getIntrinsicID()) {
  3533. case Intrinsic::canonicalize:
  3534. case Intrinsic::fabs:
  3535. case Intrinsic::copysign:
  3536. case Intrinsic::exp:
  3537. case Intrinsic::exp2:
  3538. case Intrinsic::floor:
  3539. case Intrinsic::ceil:
  3540. case Intrinsic::trunc:
  3541. case Intrinsic::rint:
  3542. case Intrinsic::nearbyint:
  3543. case Intrinsic::round:
  3544. case Intrinsic::roundeven:
  3545. case Intrinsic::arithmetic_fence:
  3546. return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1);
  3547. case Intrinsic::sqrt:
  3548. return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) &&
  3549. CannotBeOrderedLessThanZero(II->getArgOperand(0), TLI);
  3550. case Intrinsic::minnum:
  3551. case Intrinsic::maxnum:
  3552. // If either operand is not NaN, the result is not NaN.
  3553. return isKnownNeverNaN(II->getArgOperand(0), TLI, Depth + 1) ||
  3554. isKnownNeverNaN(II->getArgOperand(1), TLI, Depth + 1);
  3555. default:
  3556. return false;
  3557. }
  3558. }
  3559. // Try to handle fixed width vector constants
  3560. auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
  3561. if (VFVTy && isa<Constant>(V)) {
  3562. // For vectors, verify that each element is not NaN.
  3563. unsigned NumElts = VFVTy->getNumElements();
  3564. for (unsigned i = 0; i != NumElts; ++i) {
  3565. Constant *Elt = cast<Constant>(V)->getAggregateElement(i);
  3566. if (!Elt)
  3567. return false;
  3568. if (isa<UndefValue>(Elt))
  3569. continue;
  3570. auto *CElt = dyn_cast<ConstantFP>(Elt);
  3571. if (!CElt || CElt->isNaN())
  3572. return false;
  3573. }
  3574. // All elements were confirmed not-NaN or undefined.
  3575. return true;
  3576. }
  3577. // Was not able to prove that V never contains NaN
  3578. return false;
  3579. }
  3580. Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
  3581. // All byte-wide stores are splatable, even of arbitrary variables.
  3582. if (V->getType()->isIntegerTy(8))
  3583. return V;
  3584. LLVMContext &Ctx = V->getContext();
  3585. // Undef don't care.
  3586. auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
  3587. if (isa<UndefValue>(V))
  3588. return UndefInt8;
  3589. // Return Undef for zero-sized type.
  3590. if (!DL.getTypeStoreSize(V->getType()).isNonZero())
  3591. return UndefInt8;
  3592. Constant *C = dyn_cast<Constant>(V);
  3593. if (!C) {
  3594. // Conceptually, we could handle things like:
  3595. // %a = zext i8 %X to i16
  3596. // %b = shl i16 %a, 8
  3597. // %c = or i16 %a, %b
  3598. // but until there is an example that actually needs this, it doesn't seem
  3599. // worth worrying about.
  3600. return nullptr;
  3601. }
  3602. // Handle 'null' ConstantArrayZero etc.
  3603. if (C->isNullValue())
  3604. return Constant::getNullValue(Type::getInt8Ty(Ctx));
  3605. // Constant floating-point values can be handled as integer values if the
  3606. // corresponding integer value is "byteable". An important case is 0.0.
  3607. if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
  3608. Type *Ty = nullptr;
  3609. if (CFP->getType()->isHalfTy())
  3610. Ty = Type::getInt16Ty(Ctx);
  3611. else if (CFP->getType()->isFloatTy())
  3612. Ty = Type::getInt32Ty(Ctx);
  3613. else if (CFP->getType()->isDoubleTy())
  3614. Ty = Type::getInt64Ty(Ctx);
  3615. // Don't handle long double formats, which have strange constraints.
  3616. return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
  3617. : nullptr;
  3618. }
  3619. // We can handle constant integers that are multiple of 8 bits.
  3620. if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
  3621. if (CI->getBitWidth() % 8 == 0) {
  3622. assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
  3623. if (!CI->getValue().isSplat(8))
  3624. return nullptr;
  3625. return ConstantInt::get(Ctx, CI->getValue().trunc(8));
  3626. }
  3627. }
  3628. if (auto *CE = dyn_cast<ConstantExpr>(C)) {
  3629. if (CE->getOpcode() == Instruction::IntToPtr) {
  3630. if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
  3631. unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
  3632. return isBytewiseValue(
  3633. ConstantExpr::getIntegerCast(CE->getOperand(0),
  3634. Type::getIntNTy(Ctx, BitWidth), false),
  3635. DL);
  3636. }
  3637. }
  3638. }
  3639. auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
  3640. if (LHS == RHS)
  3641. return LHS;
  3642. if (!LHS || !RHS)
  3643. return nullptr;
  3644. if (LHS == UndefInt8)
  3645. return RHS;
  3646. if (RHS == UndefInt8)
  3647. return LHS;
  3648. return nullptr;
  3649. };
  3650. if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
  3651. Value *Val = UndefInt8;
  3652. for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
  3653. if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
  3654. return nullptr;
  3655. return Val;
  3656. }
  3657. if (isa<ConstantAggregate>(C)) {
  3658. Value *Val = UndefInt8;
  3659. for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
  3660. if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL))))
  3661. return nullptr;
  3662. return Val;
  3663. }
  3664. // Don't try to handle the handful of other constants.
  3665. return nullptr;
  3666. }
  3667. // This is the recursive version of BuildSubAggregate. It takes a few different
  3668. // arguments. Idxs is the index within the nested struct From that we are
  3669. // looking at now (which is of type IndexedType). IdxSkip is the number of
  3670. // indices from Idxs that should be left out when inserting into the resulting
  3671. // struct. To is the result struct built so far, new insertvalue instructions
  3672. // build on that.
  3673. static Value *BuildSubAggregate(Value *From, Value* To, Type *IndexedType,
  3674. SmallVectorImpl<unsigned> &Idxs,
  3675. unsigned IdxSkip,
  3676. Instruction *InsertBefore) {
  3677. StructType *STy = dyn_cast<StructType>(IndexedType);
  3678. if (STy) {
  3679. // Save the original To argument so we can modify it
  3680. Value *OrigTo = To;
  3681. // General case, the type indexed by Idxs is a struct
  3682. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
  3683. // Process each struct element recursively
  3684. Idxs.push_back(i);
  3685. Value *PrevTo = To;
  3686. To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
  3687. InsertBefore);
  3688. Idxs.pop_back();
  3689. if (!To) {
  3690. // Couldn't find any inserted value for this index? Cleanup
  3691. while (PrevTo != OrigTo) {
  3692. InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
  3693. PrevTo = Del->getAggregateOperand();
  3694. Del->eraseFromParent();
  3695. }
  3696. // Stop processing elements
  3697. break;
  3698. }
  3699. }
  3700. // If we successfully found a value for each of our subaggregates
  3701. if (To)
  3702. return To;
  3703. }
  3704. // Base case, the type indexed by SourceIdxs is not a struct, or not all of
  3705. // the struct's elements had a value that was inserted directly. In the latter
  3706. // case, perhaps we can't determine each of the subelements individually, but
  3707. // we might be able to find the complete struct somewhere.
  3708. // Find the value that is at that particular spot
  3709. Value *V = FindInsertedValue(From, Idxs);
  3710. if (!V)
  3711. return nullptr;
  3712. // Insert the value in the new (sub) aggregate
  3713. return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
  3714. InsertBefore);
  3715. }
  3716. // This helper takes a nested struct and extracts a part of it (which is again a
  3717. // struct) into a new value. For example, given the struct:
  3718. // { a, { b, { c, d }, e } }
  3719. // and the indices "1, 1" this returns
  3720. // { c, d }.
  3721. //
  3722. // It does this by inserting an insertvalue for each element in the resulting
  3723. // struct, as opposed to just inserting a single struct. This will only work if
  3724. // each of the elements of the substruct are known (ie, inserted into From by an
  3725. // insertvalue instruction somewhere).
  3726. //
  3727. // All inserted insertvalue instructions are inserted before InsertBefore
  3728. static Value *BuildSubAggregate(Value *From, ArrayRef<unsigned> idx_range,
  3729. Instruction *InsertBefore) {
  3730. assert(InsertBefore && "Must have someplace to insert!");
  3731. Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
  3732. idx_range);
  3733. Value *To = PoisonValue::get(IndexedType);
  3734. SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
  3735. unsigned IdxSkip = Idxs.size();
  3736. return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
  3737. }
  3738. /// Given an aggregate and a sequence of indices, see if the scalar value
  3739. /// indexed is already around as a register, for example if it was inserted
  3740. /// directly into the aggregate.
  3741. ///
  3742. /// If InsertBefore is not null, this function will duplicate (modified)
  3743. /// insertvalues when a part of a nested struct is extracted.
  3744. Value *llvm::FindInsertedValue(Value *V, ArrayRef<unsigned> idx_range,
  3745. Instruction *InsertBefore) {
  3746. // Nothing to index? Just return V then (this is useful at the end of our
  3747. // recursion).
  3748. if (idx_range.empty())
  3749. return V;
  3750. // We have indices, so V should have an indexable type.
  3751. assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
  3752. "Not looking at a struct or array?");
  3753. assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
  3754. "Invalid indices for type?");
  3755. if (Constant *C = dyn_cast<Constant>(V)) {
  3756. C = C->getAggregateElement(idx_range[0]);
  3757. if (!C) return nullptr;
  3758. return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
  3759. }
  3760. if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
  3761. // Loop the indices for the insertvalue instruction in parallel with the
  3762. // requested indices
  3763. const unsigned *req_idx = idx_range.begin();
  3764. for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
  3765. i != e; ++i, ++req_idx) {
  3766. if (req_idx == idx_range.end()) {
  3767. // We can't handle this without inserting insertvalues
  3768. if (!InsertBefore)
  3769. return nullptr;
  3770. // The requested index identifies a part of a nested aggregate. Handle
  3771. // this specially. For example,
  3772. // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
  3773. // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
  3774. // %C = extractvalue {i32, { i32, i32 } } %B, 1
  3775. // This can be changed into
  3776. // %A = insertvalue {i32, i32 } undef, i32 10, 0
  3777. // %C = insertvalue {i32, i32 } %A, i32 11, 1
  3778. // which allows the unused 0,0 element from the nested struct to be
  3779. // removed.
  3780. return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
  3781. InsertBefore);
  3782. }
  3783. // This insert value inserts something else than what we are looking for.
  3784. // See if the (aggregate) value inserted into has the value we are
  3785. // looking for, then.
  3786. if (*req_idx != *i)
  3787. return FindInsertedValue(I->getAggregateOperand(), idx_range,
  3788. InsertBefore);
  3789. }
  3790. // If we end up here, the indices of the insertvalue match with those
  3791. // requested (though possibly only partially). Now we recursively look at
  3792. // the inserted value, passing any remaining indices.
  3793. return FindInsertedValue(I->getInsertedValueOperand(),
  3794. ArrayRef(req_idx, idx_range.end()), InsertBefore);
  3795. }
  3796. if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
  3797. // If we're extracting a value from an aggregate that was extracted from
  3798. // something else, we can extract from that something else directly instead.
  3799. // However, we will need to chain I's indices with the requested indices.
  3800. // Calculate the number of indices required
  3801. unsigned size = I->getNumIndices() + idx_range.size();
  3802. // Allocate some space to put the new indices in
  3803. SmallVector<unsigned, 5> Idxs;
  3804. Idxs.reserve(size);
  3805. // Add indices from the extract value instruction
  3806. Idxs.append(I->idx_begin(), I->idx_end());
  3807. // Add requested indices
  3808. Idxs.append(idx_range.begin(), idx_range.end());
  3809. assert(Idxs.size() == size
  3810. && "Number of indices added not correct?");
  3811. return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
  3812. }
  3813. // Otherwise, we don't know (such as, extracting from a function return value
  3814. // or load instruction)
  3815. return nullptr;
  3816. }
  3817. bool llvm::isGEPBasedOnPointerToString(const GEPOperator *GEP,
  3818. unsigned CharSize) {
  3819. // Make sure the GEP has exactly three arguments.
  3820. if (GEP->getNumOperands() != 3)
  3821. return false;
  3822. // Make sure the index-ee is a pointer to array of \p CharSize integers.
  3823. // CharSize.
  3824. ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
  3825. if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
  3826. return false;
  3827. // Check to make sure that the first operand of the GEP is an integer and
  3828. // has value 0 so that we are sure we're indexing into the initializer.
  3829. const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
  3830. if (!FirstIdx || !FirstIdx->isZero())
  3831. return false;
  3832. return true;
  3833. }
  3834. // If V refers to an initialized global constant, set Slice either to
  3835. // its initializer if the size of its elements equals ElementSize, or,
  3836. // for ElementSize == 8, to its representation as an array of unsiged
  3837. // char. Return true on success.
  3838. // Offset is in the unit "nr of ElementSize sized elements".
  3839. bool llvm::getConstantDataArrayInfo(const Value *V,
  3840. ConstantDataArraySlice &Slice,
  3841. unsigned ElementSize, uint64_t Offset) {
  3842. assert(V && "V should not be null.");
  3843. assert((ElementSize % 8) == 0 &&
  3844. "ElementSize expected to be a multiple of the size of a byte.");
  3845. unsigned ElementSizeInBytes = ElementSize / 8;
  3846. // Drill down into the pointer expression V, ignoring any intervening
  3847. // casts, and determine the identity of the object it references along
  3848. // with the cumulative byte offset into it.
  3849. const GlobalVariable *GV =
  3850. dyn_cast<GlobalVariable>(getUnderlyingObject(V));
  3851. if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
  3852. // Fail if V is not based on constant global object.
  3853. return false;
  3854. const DataLayout &DL = GV->getParent()->getDataLayout();
  3855. APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
  3856. if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
  3857. /*AllowNonInbounds*/ true))
  3858. // Fail if a constant offset could not be determined.
  3859. return false;
  3860. uint64_t StartIdx = Off.getLimitedValue();
  3861. if (StartIdx == UINT64_MAX)
  3862. // Fail if the constant offset is excessive.
  3863. return false;
  3864. // Off/StartIdx is in the unit of bytes. So we need to convert to number of
  3865. // elements. Simply bail out if that isn't possible.
  3866. if ((StartIdx % ElementSizeInBytes) != 0)
  3867. return false;
  3868. Offset += StartIdx / ElementSizeInBytes;
  3869. ConstantDataArray *Array = nullptr;
  3870. ArrayType *ArrayTy = nullptr;
  3871. if (GV->getInitializer()->isNullValue()) {
  3872. Type *GVTy = GV->getValueType();
  3873. uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
  3874. uint64_t Length = SizeInBytes / ElementSizeInBytes;
  3875. Slice.Array = nullptr;
  3876. Slice.Offset = 0;
  3877. // Return an empty Slice for undersized constants to let callers
  3878. // transform even undefined library calls into simpler, well-defined
  3879. // expressions. This is preferable to making the calls although it
  3880. // prevents sanitizers from detecting such calls.
  3881. Slice.Length = Length < Offset ? 0 : Length - Offset;
  3882. return true;
  3883. }
  3884. auto *Init = const_cast<Constant *>(GV->getInitializer());
  3885. if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
  3886. Type *InitElTy = ArrayInit->getElementType();
  3887. if (InitElTy->isIntegerTy(ElementSize)) {
  3888. // If Init is an initializer for an array of the expected type
  3889. // and size, use it as is.
  3890. Array = ArrayInit;
  3891. ArrayTy = ArrayInit->getType();
  3892. }
  3893. }
  3894. if (!Array) {
  3895. if (ElementSize != 8)
  3896. // TODO: Handle conversions to larger integral types.
  3897. return false;
  3898. // Otherwise extract the portion of the initializer starting
  3899. // at Offset as an array of bytes, and reset Offset.
  3900. Init = ReadByteArrayFromGlobal(GV, Offset);
  3901. if (!Init)
  3902. return false;
  3903. Offset = 0;
  3904. Array = dyn_cast<ConstantDataArray>(Init);
  3905. ArrayTy = dyn_cast<ArrayType>(Init->getType());
  3906. }
  3907. uint64_t NumElts = ArrayTy->getArrayNumElements();
  3908. if (Offset > NumElts)
  3909. return false;
  3910. Slice.Array = Array;
  3911. Slice.Offset = Offset;
  3912. Slice.Length = NumElts - Offset;
  3913. return true;
  3914. }
  3915. /// Extract bytes from the initializer of the constant array V, which need
  3916. /// not be a nul-terminated string. On success, store the bytes in Str and
  3917. /// return true. When TrimAtNul is set, Str will contain only the bytes up
  3918. /// to but not including the first nul. Return false on failure.
  3919. bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
  3920. bool TrimAtNul) {
  3921. ConstantDataArraySlice Slice;
  3922. if (!getConstantDataArrayInfo(V, Slice, 8))
  3923. return false;
  3924. if (Slice.Array == nullptr) {
  3925. if (TrimAtNul) {
  3926. // Return a nul-terminated string even for an empty Slice. This is
  3927. // safe because all existing SimplifyLibcalls callers require string
  3928. // arguments and the behavior of the functions they fold is undefined
  3929. // otherwise. Folding the calls this way is preferable to making
  3930. // the undefined library calls, even though it prevents sanitizers
  3931. // from reporting such calls.
  3932. Str = StringRef();
  3933. return true;
  3934. }
  3935. if (Slice.Length == 1) {
  3936. Str = StringRef("", 1);
  3937. return true;
  3938. }
  3939. // We cannot instantiate a StringRef as we do not have an appropriate string
  3940. // of 0s at hand.
  3941. return false;
  3942. }
  3943. // Start out with the entire array in the StringRef.
  3944. Str = Slice.Array->getAsString();
  3945. // Skip over 'offset' bytes.
  3946. Str = Str.substr(Slice.Offset);
  3947. if (TrimAtNul) {
  3948. // Trim off the \0 and anything after it. If the array is not nul
  3949. // terminated, we just return the whole end of string. The client may know
  3950. // some other way that the string is length-bound.
  3951. Str = Str.substr(0, Str.find('\0'));
  3952. }
  3953. return true;
  3954. }
  3955. // These next two are very similar to the above, but also look through PHI
  3956. // nodes.
  3957. // TODO: See if we can integrate these two together.
  3958. /// If we can compute the length of the string pointed to by
  3959. /// the specified pointer, return 'len+1'. If we can't, return 0.
  3960. static uint64_t GetStringLengthH(const Value *V,
  3961. SmallPtrSetImpl<const PHINode*> &PHIs,
  3962. unsigned CharSize) {
  3963. // Look through noop bitcast instructions.
  3964. V = V->stripPointerCasts();
  3965. // If this is a PHI node, there are two cases: either we have already seen it
  3966. // or we haven't.
  3967. if (const PHINode *PN = dyn_cast<PHINode>(V)) {
  3968. if (!PHIs.insert(PN).second)
  3969. return ~0ULL; // already in the set.
  3970. // If it was new, see if all the input strings are the same length.
  3971. uint64_t LenSoFar = ~0ULL;
  3972. for (Value *IncValue : PN->incoming_values()) {
  3973. uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
  3974. if (Len == 0) return 0; // Unknown length -> unknown.
  3975. if (Len == ~0ULL) continue;
  3976. if (Len != LenSoFar && LenSoFar != ~0ULL)
  3977. return 0; // Disagree -> unknown.
  3978. LenSoFar = Len;
  3979. }
  3980. // Success, all agree.
  3981. return LenSoFar;
  3982. }
  3983. // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
  3984. if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
  3985. uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
  3986. if (Len1 == 0) return 0;
  3987. uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
  3988. if (Len2 == 0) return 0;
  3989. if (Len1 == ~0ULL) return Len2;
  3990. if (Len2 == ~0ULL) return Len1;
  3991. if (Len1 != Len2) return 0;
  3992. return Len1;
  3993. }
  3994. // Otherwise, see if we can read the string.
  3995. ConstantDataArraySlice Slice;
  3996. if (!getConstantDataArrayInfo(V, Slice, CharSize))
  3997. return 0;
  3998. if (Slice.Array == nullptr)
  3999. // Zeroinitializer (including an empty one).
  4000. return 1;
  4001. // Search for the first nul character. Return a conservative result even
  4002. // when there is no nul. This is safe since otherwise the string function
  4003. // being folded such as strlen is undefined, and can be preferable to
  4004. // making the undefined library call.
  4005. unsigned NullIndex = 0;
  4006. for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
  4007. if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
  4008. break;
  4009. }
  4010. return NullIndex + 1;
  4011. }
  4012. /// If we can compute the length of the string pointed to by
  4013. /// the specified pointer, return 'len+1'. If we can't, return 0.
  4014. uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
  4015. if (!V->getType()->isPointerTy())
  4016. return 0;
  4017. SmallPtrSet<const PHINode*, 32> PHIs;
  4018. uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
  4019. // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
  4020. // an empty string as a length.
  4021. return Len == ~0ULL ? 1 : Len;
  4022. }
  4023. const Value *
  4024. llvm::getArgumentAliasingToReturnedPointer(const CallBase *Call,
  4025. bool MustPreserveNullness) {
  4026. assert(Call &&
  4027. "getArgumentAliasingToReturnedPointer only works on nonnull calls");
  4028. if (const Value *RV = Call->getReturnedArgOperand())
  4029. return RV;
  4030. // This can be used only as a aliasing property.
  4031. if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
  4032. Call, MustPreserveNullness))
  4033. return Call->getArgOperand(0);
  4034. return nullptr;
  4035. }
  4036. bool llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
  4037. const CallBase *Call, bool MustPreserveNullness) {
  4038. switch (Call->getIntrinsicID()) {
  4039. case Intrinsic::launder_invariant_group:
  4040. case Intrinsic::strip_invariant_group:
  4041. case Intrinsic::aarch64_irg:
  4042. case Intrinsic::aarch64_tagp:
  4043. return true;
  4044. case Intrinsic::ptrmask:
  4045. return !MustPreserveNullness;
  4046. default:
  4047. return false;
  4048. }
  4049. }
  4050. /// \p PN defines a loop-variant pointer to an object. Check if the
  4051. /// previous iteration of the loop was referring to the same object as \p PN.
  4052. static bool isSameUnderlyingObjectInLoop(const PHINode *PN,
  4053. const LoopInfo *LI) {
  4054. // Find the loop-defined value.
  4055. Loop *L = LI->getLoopFor(PN->getParent());
  4056. if (PN->getNumIncomingValues() != 2)
  4057. return true;
  4058. // Find the value from previous iteration.
  4059. auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
  4060. if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
  4061. PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
  4062. if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
  4063. return true;
  4064. // If a new pointer is loaded in the loop, the pointer references a different
  4065. // object in every iteration. E.g.:
  4066. // for (i)
  4067. // int *p = a[i];
  4068. // ...
  4069. if (auto *Load = dyn_cast<LoadInst>(PrevValue))
  4070. if (!L->isLoopInvariant(Load->getPointerOperand()))
  4071. return false;
  4072. return true;
  4073. }
  4074. const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
  4075. if (!V->getType()->isPointerTy())
  4076. return V;
  4077. for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
  4078. if (auto *GEP = dyn_cast<GEPOperator>(V)) {
  4079. V = GEP->getPointerOperand();
  4080. } else if (Operator::getOpcode(V) == Instruction::BitCast ||
  4081. Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
  4082. V = cast<Operator>(V)->getOperand(0);
  4083. if (!V->getType()->isPointerTy())
  4084. return V;
  4085. } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
  4086. if (GA->isInterposable())
  4087. return V;
  4088. V = GA->getAliasee();
  4089. } else {
  4090. if (auto *PHI = dyn_cast<PHINode>(V)) {
  4091. // Look through single-arg phi nodes created by LCSSA.
  4092. if (PHI->getNumIncomingValues() == 1) {
  4093. V = PHI->getIncomingValue(0);
  4094. continue;
  4095. }
  4096. } else if (auto *Call = dyn_cast<CallBase>(V)) {
  4097. // CaptureTracking can know about special capturing properties of some
  4098. // intrinsics like launder.invariant.group, that can't be expressed with
  4099. // the attributes, but have properties like returning aliasing pointer.
  4100. // Because some analysis may assume that nocaptured pointer is not
  4101. // returned from some special intrinsic (because function would have to
  4102. // be marked with returns attribute), it is crucial to use this function
  4103. // because it should be in sync with CaptureTracking. Not using it may
  4104. // cause weird miscompilations where 2 aliasing pointers are assumed to
  4105. // noalias.
  4106. if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
  4107. V = RP;
  4108. continue;
  4109. }
  4110. }
  4111. return V;
  4112. }
  4113. assert(V->getType()->isPointerTy() && "Unexpected operand type!");
  4114. }
  4115. return V;
  4116. }
  4117. void llvm::getUnderlyingObjects(const Value *V,
  4118. SmallVectorImpl<const Value *> &Objects,
  4119. LoopInfo *LI, unsigned MaxLookup) {
  4120. SmallPtrSet<const Value *, 4> Visited;
  4121. SmallVector<const Value *, 4> Worklist;
  4122. Worklist.push_back(V);
  4123. do {
  4124. const Value *P = Worklist.pop_back_val();
  4125. P = getUnderlyingObject(P, MaxLookup);
  4126. if (!Visited.insert(P).second)
  4127. continue;
  4128. if (auto *SI = dyn_cast<SelectInst>(P)) {
  4129. Worklist.push_back(SI->getTrueValue());
  4130. Worklist.push_back(SI->getFalseValue());
  4131. continue;
  4132. }
  4133. if (auto *PN = dyn_cast<PHINode>(P)) {
  4134. // If this PHI changes the underlying object in every iteration of the
  4135. // loop, don't look through it. Consider:
  4136. // int **A;
  4137. // for (i) {
  4138. // Prev = Curr; // Prev = PHI (Prev_0, Curr)
  4139. // Curr = A[i];
  4140. // *Prev, *Curr;
  4141. //
  4142. // Prev is tracking Curr one iteration behind so they refer to different
  4143. // underlying objects.
  4144. if (!LI || !LI->isLoopHeader(PN->getParent()) ||
  4145. isSameUnderlyingObjectInLoop(PN, LI))
  4146. append_range(Worklist, PN->incoming_values());
  4147. continue;
  4148. }
  4149. Objects.push_back(P);
  4150. } while (!Worklist.empty());
  4151. }
  4152. /// This is the function that does the work of looking through basic
  4153. /// ptrtoint+arithmetic+inttoptr sequences.
  4154. static const Value *getUnderlyingObjectFromInt(const Value *V) {
  4155. do {
  4156. if (const Operator *U = dyn_cast<Operator>(V)) {
  4157. // If we find a ptrtoint, we can transfer control back to the
  4158. // regular getUnderlyingObjectFromInt.
  4159. if (U->getOpcode() == Instruction::PtrToInt)
  4160. return U->getOperand(0);
  4161. // If we find an add of a constant, a multiplied value, or a phi, it's
  4162. // likely that the other operand will lead us to the base
  4163. // object. We don't have to worry about the case where the
  4164. // object address is somehow being computed by the multiply,
  4165. // because our callers only care when the result is an
  4166. // identifiable object.
  4167. if (U->getOpcode() != Instruction::Add ||
  4168. (!isa<ConstantInt>(U->getOperand(1)) &&
  4169. Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
  4170. !isa<PHINode>(U->getOperand(1))))
  4171. return V;
  4172. V = U->getOperand(0);
  4173. } else {
  4174. return V;
  4175. }
  4176. assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
  4177. } while (true);
  4178. }
  4179. /// This is a wrapper around getUnderlyingObjects and adds support for basic
  4180. /// ptrtoint+arithmetic+inttoptr sequences.
  4181. /// It returns false if unidentified object is found in getUnderlyingObjects.
  4182. bool llvm::getUnderlyingObjectsForCodeGen(const Value *V,
  4183. SmallVectorImpl<Value *> &Objects) {
  4184. SmallPtrSet<const Value *, 16> Visited;
  4185. SmallVector<const Value *, 4> Working(1, V);
  4186. do {
  4187. V = Working.pop_back_val();
  4188. SmallVector<const Value *, 4> Objs;
  4189. getUnderlyingObjects(V, Objs);
  4190. for (const Value *V : Objs) {
  4191. if (!Visited.insert(V).second)
  4192. continue;
  4193. if (Operator::getOpcode(V) == Instruction::IntToPtr) {
  4194. const Value *O =
  4195. getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
  4196. if (O->getType()->isPointerTy()) {
  4197. Working.push_back(O);
  4198. continue;
  4199. }
  4200. }
  4201. // If getUnderlyingObjects fails to find an identifiable object,
  4202. // getUnderlyingObjectsForCodeGen also fails for safety.
  4203. if (!isIdentifiedObject(V)) {
  4204. Objects.clear();
  4205. return false;
  4206. }
  4207. Objects.push_back(const_cast<Value *>(V));
  4208. }
  4209. } while (!Working.empty());
  4210. return true;
  4211. }
  4212. AllocaInst *llvm::findAllocaForValue(Value *V, bool OffsetZero) {
  4213. AllocaInst *Result = nullptr;
  4214. SmallPtrSet<Value *, 4> Visited;
  4215. SmallVector<Value *, 4> Worklist;
  4216. auto AddWork = [&](Value *V) {
  4217. if (Visited.insert(V).second)
  4218. Worklist.push_back(V);
  4219. };
  4220. AddWork(V);
  4221. do {
  4222. V = Worklist.pop_back_val();
  4223. assert(Visited.count(V));
  4224. if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
  4225. if (Result && Result != AI)
  4226. return nullptr;
  4227. Result = AI;
  4228. } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
  4229. AddWork(CI->getOperand(0));
  4230. } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
  4231. for (Value *IncValue : PN->incoming_values())
  4232. AddWork(IncValue);
  4233. } else if (auto *SI = dyn_cast<SelectInst>(V)) {
  4234. AddWork(SI->getTrueValue());
  4235. AddWork(SI->getFalseValue());
  4236. } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
  4237. if (OffsetZero && !GEP->hasAllZeroIndices())
  4238. return nullptr;
  4239. AddWork(GEP->getPointerOperand());
  4240. } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
  4241. Value *Returned = CB->getReturnedArgOperand();
  4242. if (Returned)
  4243. AddWork(Returned);
  4244. else
  4245. return nullptr;
  4246. } else {
  4247. return nullptr;
  4248. }
  4249. } while (!Worklist.empty());
  4250. return Result;
  4251. }
  4252. static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(
  4253. const Value *V, bool AllowLifetime, bool AllowDroppable) {
  4254. for (const User *U : V->users()) {
  4255. const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
  4256. if (!II)
  4257. return false;
  4258. if (AllowLifetime && II->isLifetimeStartOrEnd())
  4259. continue;
  4260. if (AllowDroppable && II->isDroppable())
  4261. continue;
  4262. return false;
  4263. }
  4264. return true;
  4265. }
  4266. bool llvm::onlyUsedByLifetimeMarkers(const Value *V) {
  4267. return onlyUsedByLifetimeMarkersOrDroppableInstsHelper(
  4268. V, /* AllowLifetime */ true, /* AllowDroppable */ false);
  4269. }
  4270. bool llvm::onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V) {
  4271. return onlyUsedByLifetimeMarkersOrDroppableInstsHelper(
  4272. V, /* AllowLifetime */ true, /* AllowDroppable */ true);
  4273. }
  4274. bool llvm::mustSuppressSpeculation(const LoadInst &LI) {
  4275. if (!LI.isUnordered())
  4276. return true;
  4277. const Function &F = *LI.getFunction();
  4278. // Speculative load may create a race that did not exist in the source.
  4279. return F.hasFnAttribute(Attribute::SanitizeThread) ||
  4280. // Speculative load may load data from dirty regions.
  4281. F.hasFnAttribute(Attribute::SanitizeAddress) ||
  4282. F.hasFnAttribute(Attribute::SanitizeHWAddress);
  4283. }
  4284. bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
  4285. const Instruction *CtxI,
  4286. AssumptionCache *AC,
  4287. const DominatorTree *DT,
  4288. const TargetLibraryInfo *TLI) {
  4289. return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
  4290. AC, DT, TLI);
  4291. }
  4292. bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
  4293. unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
  4294. AssumptionCache *AC, const DominatorTree *DT,
  4295. const TargetLibraryInfo *TLI) {
  4296. #ifndef NDEBUG
  4297. if (Inst->getOpcode() != Opcode) {
  4298. // Check that the operands are actually compatible with the Opcode override.
  4299. auto hasEqualReturnAndLeadingOperandTypes =
  4300. [](const Instruction *Inst, unsigned NumLeadingOperands) {
  4301. if (Inst->getNumOperands() < NumLeadingOperands)
  4302. return false;
  4303. const Type *ExpectedType = Inst->getType();
  4304. for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
  4305. if (Inst->getOperand(ItOp)->getType() != ExpectedType)
  4306. return false;
  4307. return true;
  4308. };
  4309. assert(!Instruction::isBinaryOp(Opcode) ||
  4310. hasEqualReturnAndLeadingOperandTypes(Inst, 2));
  4311. assert(!Instruction::isUnaryOp(Opcode) ||
  4312. hasEqualReturnAndLeadingOperandTypes(Inst, 1));
  4313. }
  4314. #endif
  4315. switch (Opcode) {
  4316. default:
  4317. return true;
  4318. case Instruction::UDiv:
  4319. case Instruction::URem: {
  4320. // x / y is undefined if y == 0.
  4321. const APInt *V;
  4322. if (match(Inst->getOperand(1), m_APInt(V)))
  4323. return *V != 0;
  4324. return false;
  4325. }
  4326. case Instruction::SDiv:
  4327. case Instruction::SRem: {
  4328. // x / y is undefined if y == 0 or x == INT_MIN and y == -1
  4329. const APInt *Numerator, *Denominator;
  4330. if (!match(Inst->getOperand(1), m_APInt(Denominator)))
  4331. return false;
  4332. // We cannot hoist this division if the denominator is 0.
  4333. if (*Denominator == 0)
  4334. return false;
  4335. // It's safe to hoist if the denominator is not 0 or -1.
  4336. if (!Denominator->isAllOnes())
  4337. return true;
  4338. // At this point we know that the denominator is -1. It is safe to hoist as
  4339. // long we know that the numerator is not INT_MIN.
  4340. if (match(Inst->getOperand(0), m_APInt(Numerator)))
  4341. return !Numerator->isMinSignedValue();
  4342. // The numerator *might* be MinSignedValue.
  4343. return false;
  4344. }
  4345. case Instruction::Load: {
  4346. const LoadInst *LI = dyn_cast<LoadInst>(Inst);
  4347. if (!LI)
  4348. return false;
  4349. if (mustSuppressSpeculation(*LI))
  4350. return false;
  4351. const DataLayout &DL = LI->getModule()->getDataLayout();
  4352. return isDereferenceableAndAlignedPointer(LI->getPointerOperand(),
  4353. LI->getType(), LI->getAlign(), DL,
  4354. CtxI, AC, DT, TLI);
  4355. }
  4356. case Instruction::Call: {
  4357. auto *CI = dyn_cast<const CallInst>(Inst);
  4358. if (!CI)
  4359. return false;
  4360. const Function *Callee = CI->getCalledFunction();
  4361. // The called function could have undefined behavior or side-effects, even
  4362. // if marked readnone nounwind.
  4363. return Callee && Callee->isSpeculatable();
  4364. }
  4365. case Instruction::VAArg:
  4366. case Instruction::Alloca:
  4367. case Instruction::Invoke:
  4368. case Instruction::CallBr:
  4369. case Instruction::PHI:
  4370. case Instruction::Store:
  4371. case Instruction::Ret:
  4372. case Instruction::Br:
  4373. case Instruction::IndirectBr:
  4374. case Instruction::Switch:
  4375. case Instruction::Unreachable:
  4376. case Instruction::Fence:
  4377. case Instruction::AtomicRMW:
  4378. case Instruction::AtomicCmpXchg:
  4379. case Instruction::LandingPad:
  4380. case Instruction::Resume:
  4381. case Instruction::CatchSwitch:
  4382. case Instruction::CatchPad:
  4383. case Instruction::CatchRet:
  4384. case Instruction::CleanupPad:
  4385. case Instruction::CleanupRet:
  4386. return false; // Misc instructions which have effects
  4387. }
  4388. }
  4389. bool llvm::mayHaveNonDefUseDependency(const Instruction &I) {
  4390. if (I.mayReadOrWriteMemory())
  4391. // Memory dependency possible
  4392. return true;
  4393. if (!isSafeToSpeculativelyExecute(&I))
  4394. // Can't move above a maythrow call or infinite loop. Or if an
  4395. // inalloca alloca, above a stacksave call.
  4396. return true;
  4397. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  4398. // 1) Can't reorder two inf-loop calls, even if readonly
  4399. // 2) Also can't reorder an inf-loop call below a instruction which isn't
  4400. // safe to speculative execute. (Inverse of above)
  4401. return true;
  4402. return false;
  4403. }
  4404. /// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
  4405. static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR) {
  4406. switch (OR) {
  4407. case ConstantRange::OverflowResult::MayOverflow:
  4408. return OverflowResult::MayOverflow;
  4409. case ConstantRange::OverflowResult::AlwaysOverflowsLow:
  4410. return OverflowResult::AlwaysOverflowsLow;
  4411. case ConstantRange::OverflowResult::AlwaysOverflowsHigh:
  4412. return OverflowResult::AlwaysOverflowsHigh;
  4413. case ConstantRange::OverflowResult::NeverOverflows:
  4414. return OverflowResult::NeverOverflows;
  4415. }
  4416. llvm_unreachable("Unknown OverflowResult");
  4417. }
  4418. /// Combine constant ranges from computeConstantRange() and computeKnownBits().
  4419. static ConstantRange computeConstantRangeIncludingKnownBits(
  4420. const Value *V, bool ForSigned, const DataLayout &DL, unsigned Depth,
  4421. AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
  4422. OptimizationRemarkEmitter *ORE = nullptr, bool UseInstrInfo = true) {
  4423. KnownBits Known = computeKnownBits(
  4424. V, DL, Depth, AC, CxtI, DT, ORE, UseInstrInfo);
  4425. ConstantRange CR1 = ConstantRange::fromKnownBits(Known, ForSigned);
  4426. ConstantRange CR2 = computeConstantRange(V, UseInstrInfo);
  4427. ConstantRange::PreferredRangeType RangeType =
  4428. ForSigned ? ConstantRange::Signed : ConstantRange::Unsigned;
  4429. return CR1.intersectWith(CR2, RangeType);
  4430. }
  4431. OverflowResult llvm::computeOverflowForUnsignedMul(
  4432. const Value *LHS, const Value *RHS, const DataLayout &DL,
  4433. AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
  4434. bool UseInstrInfo) {
  4435. KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
  4436. nullptr, UseInstrInfo);
  4437. KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
  4438. nullptr, UseInstrInfo);
  4439. ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
  4440. ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
  4441. return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
  4442. }
  4443. OverflowResult
  4444. llvm::computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
  4445. const DataLayout &DL, AssumptionCache *AC,
  4446. const Instruction *CxtI,
  4447. const DominatorTree *DT, bool UseInstrInfo) {
  4448. // Multiplying n * m significant bits yields a result of n + m significant
  4449. // bits. If the total number of significant bits does not exceed the
  4450. // result bit width (minus 1), there is no overflow.
  4451. // This means if we have enough leading sign bits in the operands
  4452. // we can guarantee that the result does not overflow.
  4453. // Ref: "Hacker's Delight" by Henry Warren
  4454. unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
  4455. // Note that underestimating the number of sign bits gives a more
  4456. // conservative answer.
  4457. unsigned SignBits = ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) +
  4458. ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT);
  4459. // First handle the easy case: if we have enough sign bits there's
  4460. // definitely no overflow.
  4461. if (SignBits > BitWidth + 1)
  4462. return OverflowResult::NeverOverflows;
  4463. // There are two ambiguous cases where there can be no overflow:
  4464. // SignBits == BitWidth + 1 and
  4465. // SignBits == BitWidth
  4466. // The second case is difficult to check, therefore we only handle the
  4467. // first case.
  4468. if (SignBits == BitWidth + 1) {
  4469. // It overflows only when both arguments are negative and the true
  4470. // product is exactly the minimum negative number.
  4471. // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
  4472. // For simplicity we just check if at least one side is not negative.
  4473. KnownBits LHSKnown = computeKnownBits(LHS, DL, /*Depth=*/0, AC, CxtI, DT,
  4474. nullptr, UseInstrInfo);
  4475. KnownBits RHSKnown = computeKnownBits(RHS, DL, /*Depth=*/0, AC, CxtI, DT,
  4476. nullptr, UseInstrInfo);
  4477. if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
  4478. return OverflowResult::NeverOverflows;
  4479. }
  4480. return OverflowResult::MayOverflow;
  4481. }
  4482. OverflowResult llvm::computeOverflowForUnsignedAdd(
  4483. const Value *LHS, const Value *RHS, const DataLayout &DL,
  4484. AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT,
  4485. bool UseInstrInfo) {
  4486. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  4487. LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT,
  4488. nullptr, UseInstrInfo);
  4489. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  4490. RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT,
  4491. nullptr, UseInstrInfo);
  4492. return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
  4493. }
  4494. static OverflowResult computeOverflowForSignedAdd(const Value *LHS,
  4495. const Value *RHS,
  4496. const AddOperator *Add,
  4497. const DataLayout &DL,
  4498. AssumptionCache *AC,
  4499. const Instruction *CxtI,
  4500. const DominatorTree *DT) {
  4501. if (Add && Add->hasNoSignedWrap()) {
  4502. return OverflowResult::NeverOverflows;
  4503. }
  4504. // If LHS and RHS each have at least two sign bits, the addition will look
  4505. // like
  4506. //
  4507. // XX..... +
  4508. // YY.....
  4509. //
  4510. // If the carry into the most significant position is 0, X and Y can't both
  4511. // be 1 and therefore the carry out of the addition is also 0.
  4512. //
  4513. // If the carry into the most significant position is 1, X and Y can't both
  4514. // be 0 and therefore the carry out of the addition is also 1.
  4515. //
  4516. // Since the carry into the most significant position is always equal to
  4517. // the carry out of the addition, there is no signed overflow.
  4518. if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
  4519. ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
  4520. return OverflowResult::NeverOverflows;
  4521. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  4522. LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  4523. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  4524. RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  4525. OverflowResult OR =
  4526. mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
  4527. if (OR != OverflowResult::MayOverflow)
  4528. return OR;
  4529. // The remaining code needs Add to be available. Early returns if not so.
  4530. if (!Add)
  4531. return OverflowResult::MayOverflow;
  4532. // If the sign of Add is the same as at least one of the operands, this add
  4533. // CANNOT overflow. If this can be determined from the known bits of the
  4534. // operands the above signedAddMayOverflow() check will have already done so.
  4535. // The only other way to improve on the known bits is from an assumption, so
  4536. // call computeKnownBitsFromAssume() directly.
  4537. bool LHSOrRHSKnownNonNegative =
  4538. (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
  4539. bool LHSOrRHSKnownNegative =
  4540. (LHSRange.isAllNegative() || RHSRange.isAllNegative());
  4541. if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
  4542. KnownBits AddKnown(LHSRange.getBitWidth());
  4543. computeKnownBitsFromAssume(
  4544. Add, AddKnown, /*Depth=*/0, Query(DL, AC, CxtI, DT, true));
  4545. if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
  4546. (AddKnown.isNegative() && LHSOrRHSKnownNegative))
  4547. return OverflowResult::NeverOverflows;
  4548. }
  4549. return OverflowResult::MayOverflow;
  4550. }
  4551. OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
  4552. const Value *RHS,
  4553. const DataLayout &DL,
  4554. AssumptionCache *AC,
  4555. const Instruction *CxtI,
  4556. const DominatorTree *DT) {
  4557. // X - (X % ?)
  4558. // The remainder of a value can't have greater magnitude than itself,
  4559. // so the subtraction can't overflow.
  4560. // X - (X -nuw ?)
  4561. // In the minimal case, this would simplify to "?", so there's no subtract
  4562. // at all. But if this analysis is used to peek through casts, for example,
  4563. // then determining no-overflow may allow other transforms.
  4564. // TODO: There are other patterns like this.
  4565. // See simplifyICmpWithBinOpOnLHS() for candidates.
  4566. if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
  4567. match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
  4568. if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT))
  4569. return OverflowResult::NeverOverflows;
  4570. // Checking for conditions implied by dominating conditions may be expensive.
  4571. // Limit it to usub_with_overflow calls for now.
  4572. if (match(CxtI,
  4573. m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
  4574. if (auto C =
  4575. isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, CxtI, DL)) {
  4576. if (*C)
  4577. return OverflowResult::NeverOverflows;
  4578. return OverflowResult::AlwaysOverflowsLow;
  4579. }
  4580. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  4581. LHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
  4582. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  4583. RHS, /*ForSigned=*/false, DL, /*Depth=*/0, AC, CxtI, DT);
  4584. return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
  4585. }
  4586. OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
  4587. const Value *RHS,
  4588. const DataLayout &DL,
  4589. AssumptionCache *AC,
  4590. const Instruction *CxtI,
  4591. const DominatorTree *DT) {
  4592. // X - (X % ?)
  4593. // The remainder of a value can't have greater magnitude than itself,
  4594. // so the subtraction can't overflow.
  4595. // X - (X -nsw ?)
  4596. // In the minimal case, this would simplify to "?", so there's no subtract
  4597. // at all. But if this analysis is used to peek through casts, for example,
  4598. // then determining no-overflow may allow other transforms.
  4599. if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
  4600. match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
  4601. if (isGuaranteedNotToBeUndefOrPoison(LHS, AC, CxtI, DT))
  4602. return OverflowResult::NeverOverflows;
  4603. // If LHS and RHS each have at least two sign bits, the subtraction
  4604. // cannot overflow.
  4605. if (ComputeNumSignBits(LHS, DL, 0, AC, CxtI, DT) > 1 &&
  4606. ComputeNumSignBits(RHS, DL, 0, AC, CxtI, DT) > 1)
  4607. return OverflowResult::NeverOverflows;
  4608. ConstantRange LHSRange = computeConstantRangeIncludingKnownBits(
  4609. LHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  4610. ConstantRange RHSRange = computeConstantRangeIncludingKnownBits(
  4611. RHS, /*ForSigned=*/true, DL, /*Depth=*/0, AC, CxtI, DT);
  4612. return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
  4613. }
  4614. bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO,
  4615. const DominatorTree &DT) {
  4616. SmallVector<const BranchInst *, 2> GuardingBranches;
  4617. SmallVector<const ExtractValueInst *, 2> Results;
  4618. for (const User *U : WO->users()) {
  4619. if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
  4620. assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
  4621. if (EVI->getIndices()[0] == 0)
  4622. Results.push_back(EVI);
  4623. else {
  4624. assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
  4625. for (const auto *U : EVI->users())
  4626. if (const auto *B = dyn_cast<BranchInst>(U)) {
  4627. assert(B->isConditional() && "How else is it using an i1?");
  4628. GuardingBranches.push_back(B);
  4629. }
  4630. }
  4631. } else {
  4632. // We are using the aggregate directly in a way we don't want to analyze
  4633. // here (storing it to a global, say).
  4634. return false;
  4635. }
  4636. }
  4637. auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
  4638. BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
  4639. if (!NoWrapEdge.isSingleEdge())
  4640. return false;
  4641. // Check if all users of the add are provably no-wrap.
  4642. for (const auto *Result : Results) {
  4643. // If the extractvalue itself is not executed on overflow, the we don't
  4644. // need to check each use separately, since domination is transitive.
  4645. if (DT.dominates(NoWrapEdge, Result->getParent()))
  4646. continue;
  4647. for (const auto &RU : Result->uses())
  4648. if (!DT.dominates(NoWrapEdge, RU))
  4649. return false;
  4650. }
  4651. return true;
  4652. };
  4653. return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
  4654. }
  4655. /// Shifts return poison if shiftwidth is larger than the bitwidth.
  4656. static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
  4657. auto *C = dyn_cast<Constant>(ShiftAmount);
  4658. if (!C)
  4659. return false;
  4660. // Shifts return poison if shiftwidth is larger than the bitwidth.
  4661. SmallVector<const Constant *, 4> ShiftAmounts;
  4662. if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
  4663. unsigned NumElts = FVTy->getNumElements();
  4664. for (unsigned i = 0; i < NumElts; ++i)
  4665. ShiftAmounts.push_back(C->getAggregateElement(i));
  4666. } else if (isa<ScalableVectorType>(C->getType()))
  4667. return false; // Can't tell, just return false to be safe
  4668. else
  4669. ShiftAmounts.push_back(C);
  4670. bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
  4671. auto *CI = dyn_cast_or_null<ConstantInt>(C);
  4672. return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
  4673. });
  4674. return Safe;
  4675. }
  4676. static bool canCreateUndefOrPoison(const Operator *Op, bool PoisonOnly,
  4677. bool ConsiderFlagsAndMetadata) {
  4678. if (ConsiderFlagsAndMetadata && Op->hasPoisonGeneratingFlagsOrMetadata())
  4679. return true;
  4680. unsigned Opcode = Op->getOpcode();
  4681. // Check whether opcode is a poison/undef-generating operation
  4682. switch (Opcode) {
  4683. case Instruction::Shl:
  4684. case Instruction::AShr:
  4685. case Instruction::LShr:
  4686. return !shiftAmountKnownInRange(Op->getOperand(1));
  4687. case Instruction::FPToSI:
  4688. case Instruction::FPToUI:
  4689. // fptosi/ui yields poison if the resulting value does not fit in the
  4690. // destination type.
  4691. return true;
  4692. case Instruction::Call:
  4693. if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
  4694. switch (II->getIntrinsicID()) {
  4695. // TODO: Add more intrinsics.
  4696. case Intrinsic::ctlz:
  4697. case Intrinsic::cttz:
  4698. case Intrinsic::abs:
  4699. if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
  4700. return false;
  4701. break;
  4702. case Intrinsic::ctpop:
  4703. case Intrinsic::bswap:
  4704. case Intrinsic::bitreverse:
  4705. case Intrinsic::fshl:
  4706. case Intrinsic::fshr:
  4707. case Intrinsic::smax:
  4708. case Intrinsic::smin:
  4709. case Intrinsic::umax:
  4710. case Intrinsic::umin:
  4711. case Intrinsic::ptrmask:
  4712. case Intrinsic::fptoui_sat:
  4713. case Intrinsic::fptosi_sat:
  4714. case Intrinsic::sadd_with_overflow:
  4715. case Intrinsic::ssub_with_overflow:
  4716. case Intrinsic::smul_with_overflow:
  4717. case Intrinsic::uadd_with_overflow:
  4718. case Intrinsic::usub_with_overflow:
  4719. case Intrinsic::umul_with_overflow:
  4720. case Intrinsic::sadd_sat:
  4721. case Intrinsic::uadd_sat:
  4722. case Intrinsic::ssub_sat:
  4723. case Intrinsic::usub_sat:
  4724. return false;
  4725. case Intrinsic::sshl_sat:
  4726. case Intrinsic::ushl_sat:
  4727. return !shiftAmountKnownInRange(II->getArgOperand(1));
  4728. case Intrinsic::fma:
  4729. case Intrinsic::fmuladd:
  4730. case Intrinsic::sqrt:
  4731. case Intrinsic::powi:
  4732. case Intrinsic::sin:
  4733. case Intrinsic::cos:
  4734. case Intrinsic::pow:
  4735. case Intrinsic::log:
  4736. case Intrinsic::log10:
  4737. case Intrinsic::log2:
  4738. case Intrinsic::exp:
  4739. case Intrinsic::exp2:
  4740. case Intrinsic::fabs:
  4741. case Intrinsic::copysign:
  4742. case Intrinsic::floor:
  4743. case Intrinsic::ceil:
  4744. case Intrinsic::trunc:
  4745. case Intrinsic::rint:
  4746. case Intrinsic::nearbyint:
  4747. case Intrinsic::round:
  4748. case Intrinsic::roundeven:
  4749. case Intrinsic::fptrunc_round:
  4750. case Intrinsic::canonicalize:
  4751. case Intrinsic::arithmetic_fence:
  4752. case Intrinsic::minnum:
  4753. case Intrinsic::maxnum:
  4754. case Intrinsic::minimum:
  4755. case Intrinsic::maximum:
  4756. case Intrinsic::is_fpclass:
  4757. return false;
  4758. case Intrinsic::lround:
  4759. case Intrinsic::llround:
  4760. case Intrinsic::lrint:
  4761. case Intrinsic::llrint:
  4762. // If the value doesn't fit an unspecified value is returned (but this
  4763. // is not poison).
  4764. return false;
  4765. }
  4766. }
  4767. [[fallthrough]];
  4768. case Instruction::CallBr:
  4769. case Instruction::Invoke: {
  4770. const auto *CB = cast<CallBase>(Op);
  4771. return !CB->hasRetAttr(Attribute::NoUndef);
  4772. }
  4773. case Instruction::InsertElement:
  4774. case Instruction::ExtractElement: {
  4775. // If index exceeds the length of the vector, it returns poison
  4776. auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
  4777. unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
  4778. auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
  4779. if (!Idx || Idx->getValue().uge(VTy->getElementCount().getKnownMinValue()))
  4780. return true;
  4781. return false;
  4782. }
  4783. case Instruction::ShuffleVector: {
  4784. // shufflevector may return undef.
  4785. if (PoisonOnly)
  4786. return false;
  4787. ArrayRef<int> Mask = isa<ConstantExpr>(Op)
  4788. ? cast<ConstantExpr>(Op)->getShuffleMask()
  4789. : cast<ShuffleVectorInst>(Op)->getShuffleMask();
  4790. return is_contained(Mask, UndefMaskElem);
  4791. }
  4792. case Instruction::FNeg:
  4793. case Instruction::PHI:
  4794. case Instruction::Select:
  4795. case Instruction::URem:
  4796. case Instruction::SRem:
  4797. case Instruction::ExtractValue:
  4798. case Instruction::InsertValue:
  4799. case Instruction::Freeze:
  4800. case Instruction::ICmp:
  4801. case Instruction::FCmp:
  4802. case Instruction::FAdd:
  4803. case Instruction::FSub:
  4804. case Instruction::FMul:
  4805. case Instruction::FDiv:
  4806. case Instruction::FRem:
  4807. return false;
  4808. case Instruction::GetElementPtr:
  4809. // inbounds is handled above
  4810. // TODO: what about inrange on constexpr?
  4811. return false;
  4812. default: {
  4813. const auto *CE = dyn_cast<ConstantExpr>(Op);
  4814. if (isa<CastInst>(Op) || (CE && CE->isCast()))
  4815. return false;
  4816. else if (Instruction::isBinaryOp(Opcode))
  4817. return false;
  4818. // Be conservative and return true.
  4819. return true;
  4820. }
  4821. }
  4822. }
  4823. bool llvm::canCreateUndefOrPoison(const Operator *Op,
  4824. bool ConsiderFlagsAndMetadata) {
  4825. return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/false,
  4826. ConsiderFlagsAndMetadata);
  4827. }
  4828. bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
  4829. return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/true,
  4830. ConsiderFlagsAndMetadata);
  4831. }
  4832. static bool directlyImpliesPoison(const Value *ValAssumedPoison,
  4833. const Value *V, unsigned Depth) {
  4834. if (ValAssumedPoison == V)
  4835. return true;
  4836. const unsigned MaxDepth = 2;
  4837. if (Depth >= MaxDepth)
  4838. return false;
  4839. if (const auto *I = dyn_cast<Instruction>(V)) {
  4840. if (any_of(I->operands(), [=](const Use &Op) {
  4841. return propagatesPoison(Op) &&
  4842. directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
  4843. }))
  4844. return true;
  4845. // V = extractvalue V0, idx
  4846. // V2 = extractvalue V0, idx2
  4847. // V0's elements are all poison or not. (e.g., add_with_overflow)
  4848. const WithOverflowInst *II;
  4849. if (match(I, m_ExtractValue(m_WithOverflowInst(II))) &&
  4850. (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
  4851. llvm::is_contained(II->args(), ValAssumedPoison)))
  4852. return true;
  4853. }
  4854. return false;
  4855. }
  4856. static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
  4857. unsigned Depth) {
  4858. if (isGuaranteedNotToBeUndefOrPoison(ValAssumedPoison))
  4859. return true;
  4860. if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
  4861. return true;
  4862. const unsigned MaxDepth = 2;
  4863. if (Depth >= MaxDepth)
  4864. return false;
  4865. const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
  4866. if (I && !canCreatePoison(cast<Operator>(I))) {
  4867. return all_of(I->operands(), [=](const Value *Op) {
  4868. return impliesPoison(Op, V, Depth + 1);
  4869. });
  4870. }
  4871. return false;
  4872. }
  4873. bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
  4874. return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
  4875. }
  4876. static bool programUndefinedIfUndefOrPoison(const Value *V,
  4877. bool PoisonOnly);
  4878. static bool isGuaranteedNotToBeUndefOrPoison(const Value *V,
  4879. AssumptionCache *AC,
  4880. const Instruction *CtxI,
  4881. const DominatorTree *DT,
  4882. unsigned Depth, bool PoisonOnly) {
  4883. if (Depth >= MaxAnalysisRecursionDepth)
  4884. return false;
  4885. if (isa<MetadataAsValue>(V))
  4886. return false;
  4887. if (const auto *A = dyn_cast<Argument>(V)) {
  4888. if (A->hasAttribute(Attribute::NoUndef))
  4889. return true;
  4890. }
  4891. if (auto *C = dyn_cast<Constant>(V)) {
  4892. if (isa<UndefValue>(C))
  4893. return PoisonOnly && !isa<PoisonValue>(C);
  4894. if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
  4895. isa<ConstantPointerNull>(C) || isa<Function>(C))
  4896. return true;
  4897. if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C))
  4898. return (PoisonOnly ? !C->containsPoisonElement()
  4899. : !C->containsUndefOrPoisonElement()) &&
  4900. !C->containsConstantExpression();
  4901. }
  4902. // Strip cast operations from a pointer value.
  4903. // Note that stripPointerCastsSameRepresentation can strip off getelementptr
  4904. // inbounds with zero offset. To guarantee that the result isn't poison, the
  4905. // stripped pointer is checked as it has to be pointing into an allocated
  4906. // object or be null `null` to ensure `inbounds` getelement pointers with a
  4907. // zero offset could not produce poison.
  4908. // It can strip off addrspacecast that do not change bit representation as
  4909. // well. We believe that such addrspacecast is equivalent to no-op.
  4910. auto *StrippedV = V->stripPointerCastsSameRepresentation();
  4911. if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
  4912. isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
  4913. return true;
  4914. auto OpCheck = [&](const Value *V) {
  4915. return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1,
  4916. PoisonOnly);
  4917. };
  4918. if (auto *Opr = dyn_cast<Operator>(V)) {
  4919. // If the value is a freeze instruction, then it can never
  4920. // be undef or poison.
  4921. if (isa<FreezeInst>(V))
  4922. return true;
  4923. if (const auto *CB = dyn_cast<CallBase>(V)) {
  4924. if (CB->hasRetAttr(Attribute::NoUndef))
  4925. return true;
  4926. }
  4927. if (const auto *PN = dyn_cast<PHINode>(V)) {
  4928. unsigned Num = PN->getNumIncomingValues();
  4929. bool IsWellDefined = true;
  4930. for (unsigned i = 0; i < Num; ++i) {
  4931. auto *TI = PN->getIncomingBlock(i)->getTerminator();
  4932. if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
  4933. DT, Depth + 1, PoisonOnly)) {
  4934. IsWellDefined = false;
  4935. break;
  4936. }
  4937. }
  4938. if (IsWellDefined)
  4939. return true;
  4940. } else if (!canCreateUndefOrPoison(Opr) && all_of(Opr->operands(), OpCheck))
  4941. return true;
  4942. }
  4943. if (auto *I = dyn_cast<LoadInst>(V))
  4944. if (I->hasMetadata(LLVMContext::MD_noundef) ||
  4945. I->hasMetadata(LLVMContext::MD_dereferenceable) ||
  4946. I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
  4947. return true;
  4948. if (programUndefinedIfUndefOrPoison(V, PoisonOnly))
  4949. return true;
  4950. // CxtI may be null or a cloned instruction.
  4951. if (!CtxI || !CtxI->getParent() || !DT)
  4952. return false;
  4953. auto *DNode = DT->getNode(CtxI->getParent());
  4954. if (!DNode)
  4955. // Unreachable block
  4956. return false;
  4957. // If V is used as a branch condition before reaching CtxI, V cannot be
  4958. // undef or poison.
  4959. // br V, BB1, BB2
  4960. // BB1:
  4961. // CtxI ; V cannot be undef or poison here
  4962. auto *Dominator = DNode->getIDom();
  4963. while (Dominator) {
  4964. auto *TI = Dominator->getBlock()->getTerminator();
  4965. Value *Cond = nullptr;
  4966. if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
  4967. if (BI->isConditional())
  4968. Cond = BI->getCondition();
  4969. } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
  4970. Cond = SI->getCondition();
  4971. }
  4972. if (Cond) {
  4973. if (Cond == V)
  4974. return true;
  4975. else if (PoisonOnly && isa<Operator>(Cond)) {
  4976. // For poison, we can analyze further
  4977. auto *Opr = cast<Operator>(Cond);
  4978. if (any_of(Opr->operands(),
  4979. [V](const Use &U) { return V == U && propagatesPoison(U); }))
  4980. return true;
  4981. }
  4982. }
  4983. Dominator = Dominator->getIDom();
  4984. }
  4985. if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
  4986. return true;
  4987. return false;
  4988. }
  4989. bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC,
  4990. const Instruction *CtxI,
  4991. const DominatorTree *DT,
  4992. unsigned Depth) {
  4993. return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, false);
  4994. }
  4995. bool llvm::isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC,
  4996. const Instruction *CtxI,
  4997. const DominatorTree *DT, unsigned Depth) {
  4998. return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, true);
  4999. }
  5000. OverflowResult llvm::computeOverflowForSignedAdd(const AddOperator *Add,
  5001. const DataLayout &DL,
  5002. AssumptionCache *AC,
  5003. const Instruction *CxtI,
  5004. const DominatorTree *DT) {
  5005. return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
  5006. Add, DL, AC, CxtI, DT);
  5007. }
  5008. OverflowResult llvm::computeOverflowForSignedAdd(const Value *LHS,
  5009. const Value *RHS,
  5010. const DataLayout &DL,
  5011. AssumptionCache *AC,
  5012. const Instruction *CxtI,
  5013. const DominatorTree *DT) {
  5014. return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, DL, AC, CxtI, DT);
  5015. }
  5016. bool llvm::isGuaranteedToTransferExecutionToSuccessor(const Instruction *I) {
  5017. // Note: An atomic operation isn't guaranteed to return in a reasonable amount
  5018. // of time because it's possible for another thread to interfere with it for an
  5019. // arbitrary length of time, but programs aren't allowed to rely on that.
  5020. // If there is no successor, then execution can't transfer to it.
  5021. if (isa<ReturnInst>(I))
  5022. return false;
  5023. if (isa<UnreachableInst>(I))
  5024. return false;
  5025. // Note: Do not add new checks here; instead, change Instruction::mayThrow or
  5026. // Instruction::willReturn.
  5027. //
  5028. // FIXME: Move this check into Instruction::willReturn.
  5029. if (isa<CatchPadInst>(I)) {
  5030. switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
  5031. default:
  5032. // A catchpad may invoke exception object constructors and such, which
  5033. // in some languages can be arbitrary code, so be conservative by default.
  5034. return false;
  5035. case EHPersonality::CoreCLR:
  5036. // For CoreCLR, it just involves a type test.
  5037. return true;
  5038. }
  5039. }
  5040. // An instruction that returns without throwing must transfer control flow
  5041. // to a successor.
  5042. return !I->mayThrow() && I->willReturn();
  5043. }
  5044. bool llvm::isGuaranteedToTransferExecutionToSuccessor(const BasicBlock *BB) {
  5045. // TODO: This is slightly conservative for invoke instruction since exiting
  5046. // via an exception *is* normal control for them.
  5047. for (const Instruction &I : *BB)
  5048. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  5049. return false;
  5050. return true;
  5051. }
  5052. bool llvm::isGuaranteedToTransferExecutionToSuccessor(
  5053. BasicBlock::const_iterator Begin, BasicBlock::const_iterator End,
  5054. unsigned ScanLimit) {
  5055. return isGuaranteedToTransferExecutionToSuccessor(make_range(Begin, End),
  5056. ScanLimit);
  5057. }
  5058. bool llvm::isGuaranteedToTransferExecutionToSuccessor(
  5059. iterator_range<BasicBlock::const_iterator> Range, unsigned ScanLimit) {
  5060. assert(ScanLimit && "scan limit must be non-zero");
  5061. for (const Instruction &I : Range) {
  5062. if (isa<DbgInfoIntrinsic>(I))
  5063. continue;
  5064. if (--ScanLimit == 0)
  5065. return false;
  5066. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  5067. return false;
  5068. }
  5069. return true;
  5070. }
  5071. bool llvm::isGuaranteedToExecuteForEveryIteration(const Instruction *I,
  5072. const Loop *L) {
  5073. // The loop header is guaranteed to be executed for every iteration.
  5074. //
  5075. // FIXME: Relax this constraint to cover all basic blocks that are
  5076. // guaranteed to be executed at every iteration.
  5077. if (I->getParent() != L->getHeader()) return false;
  5078. for (const Instruction &LI : *L->getHeader()) {
  5079. if (&LI == I) return true;
  5080. if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
  5081. }
  5082. llvm_unreachable("Instruction not contained in its own parent basic block.");
  5083. }
  5084. bool llvm::propagatesPoison(const Use &PoisonOp) {
  5085. const Operator *I = cast<Operator>(PoisonOp.getUser());
  5086. switch (I->getOpcode()) {
  5087. case Instruction::Freeze:
  5088. case Instruction::PHI:
  5089. case Instruction::Invoke:
  5090. return false;
  5091. case Instruction::Select:
  5092. return PoisonOp.getOperandNo() == 0;
  5093. case Instruction::Call:
  5094. if (auto *II = dyn_cast<IntrinsicInst>(I)) {
  5095. switch (II->getIntrinsicID()) {
  5096. // TODO: Add more intrinsics.
  5097. case Intrinsic::sadd_with_overflow:
  5098. case Intrinsic::ssub_with_overflow:
  5099. case Intrinsic::smul_with_overflow:
  5100. case Intrinsic::uadd_with_overflow:
  5101. case Intrinsic::usub_with_overflow:
  5102. case Intrinsic::umul_with_overflow:
  5103. // If an input is a vector containing a poison element, the
  5104. // two output vectors (calculated results, overflow bits)'
  5105. // corresponding lanes are poison.
  5106. return true;
  5107. case Intrinsic::ctpop:
  5108. return true;
  5109. }
  5110. }
  5111. return false;
  5112. case Instruction::ICmp:
  5113. case Instruction::FCmp:
  5114. case Instruction::GetElementPtr:
  5115. return true;
  5116. default:
  5117. if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
  5118. return true;
  5119. // Be conservative and return false.
  5120. return false;
  5121. }
  5122. }
  5123. void llvm::getGuaranteedWellDefinedOps(
  5124. const Instruction *I, SmallVectorImpl<const Value *> &Operands) {
  5125. switch (I->getOpcode()) {
  5126. case Instruction::Store:
  5127. Operands.push_back(cast<StoreInst>(I)->getPointerOperand());
  5128. break;
  5129. case Instruction::Load:
  5130. Operands.push_back(cast<LoadInst>(I)->getPointerOperand());
  5131. break;
  5132. // Since dereferenceable attribute imply noundef, atomic operations
  5133. // also implicitly have noundef pointers too
  5134. case Instruction::AtomicCmpXchg:
  5135. Operands.push_back(cast<AtomicCmpXchgInst>(I)->getPointerOperand());
  5136. break;
  5137. case Instruction::AtomicRMW:
  5138. Operands.push_back(cast<AtomicRMWInst>(I)->getPointerOperand());
  5139. break;
  5140. case Instruction::Call:
  5141. case Instruction::Invoke: {
  5142. const CallBase *CB = cast<CallBase>(I);
  5143. if (CB->isIndirectCall())
  5144. Operands.push_back(CB->getCalledOperand());
  5145. for (unsigned i = 0; i < CB->arg_size(); ++i) {
  5146. if (CB->paramHasAttr(i, Attribute::NoUndef) ||
  5147. CB->paramHasAttr(i, Attribute::Dereferenceable))
  5148. Operands.push_back(CB->getArgOperand(i));
  5149. }
  5150. break;
  5151. }
  5152. case Instruction::Ret:
  5153. if (I->getFunction()->hasRetAttribute(Attribute::NoUndef))
  5154. Operands.push_back(I->getOperand(0));
  5155. break;
  5156. case Instruction::Switch:
  5157. Operands.push_back(cast<SwitchInst>(I)->getCondition());
  5158. break;
  5159. case Instruction::Br: {
  5160. auto *BR = cast<BranchInst>(I);
  5161. if (BR->isConditional())
  5162. Operands.push_back(BR->getCondition());
  5163. break;
  5164. }
  5165. default:
  5166. break;
  5167. }
  5168. }
  5169. void llvm::getGuaranteedNonPoisonOps(const Instruction *I,
  5170. SmallVectorImpl<const Value *> &Operands) {
  5171. getGuaranteedWellDefinedOps(I, Operands);
  5172. switch (I->getOpcode()) {
  5173. // Divisors of these operations are allowed to be partially undef.
  5174. case Instruction::UDiv:
  5175. case Instruction::SDiv:
  5176. case Instruction::URem:
  5177. case Instruction::SRem:
  5178. Operands.push_back(I->getOperand(1));
  5179. break;
  5180. default:
  5181. break;
  5182. }
  5183. }
  5184. bool llvm::mustTriggerUB(const Instruction *I,
  5185. const SmallSet<const Value *, 16>& KnownPoison) {
  5186. SmallVector<const Value *, 4> NonPoisonOps;
  5187. getGuaranteedNonPoisonOps(I, NonPoisonOps);
  5188. for (const auto *V : NonPoisonOps)
  5189. if (KnownPoison.count(V))
  5190. return true;
  5191. return false;
  5192. }
  5193. static bool programUndefinedIfUndefOrPoison(const Value *V,
  5194. bool PoisonOnly) {
  5195. // We currently only look for uses of values within the same basic
  5196. // block, as that makes it easier to guarantee that the uses will be
  5197. // executed given that Inst is executed.
  5198. //
  5199. // FIXME: Expand this to consider uses beyond the same basic block. To do
  5200. // this, look out for the distinction between post-dominance and strong
  5201. // post-dominance.
  5202. const BasicBlock *BB = nullptr;
  5203. BasicBlock::const_iterator Begin;
  5204. if (const auto *Inst = dyn_cast<Instruction>(V)) {
  5205. BB = Inst->getParent();
  5206. Begin = Inst->getIterator();
  5207. Begin++;
  5208. } else if (const auto *Arg = dyn_cast<Argument>(V)) {
  5209. BB = &Arg->getParent()->getEntryBlock();
  5210. Begin = BB->begin();
  5211. } else {
  5212. return false;
  5213. }
  5214. // Limit number of instructions we look at, to avoid scanning through large
  5215. // blocks. The current limit is chosen arbitrarily.
  5216. unsigned ScanLimit = 32;
  5217. BasicBlock::const_iterator End = BB->end();
  5218. if (!PoisonOnly) {
  5219. // Since undef does not propagate eagerly, be conservative & just check
  5220. // whether a value is directly passed to an instruction that must take
  5221. // well-defined operands.
  5222. for (const auto &I : make_range(Begin, End)) {
  5223. if (isa<DbgInfoIntrinsic>(I))
  5224. continue;
  5225. if (--ScanLimit == 0)
  5226. break;
  5227. SmallVector<const Value *, 4> WellDefinedOps;
  5228. getGuaranteedWellDefinedOps(&I, WellDefinedOps);
  5229. if (is_contained(WellDefinedOps, V))
  5230. return true;
  5231. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  5232. break;
  5233. }
  5234. return false;
  5235. }
  5236. // Set of instructions that we have proved will yield poison if Inst
  5237. // does.
  5238. SmallSet<const Value *, 16> YieldsPoison;
  5239. SmallSet<const BasicBlock *, 4> Visited;
  5240. YieldsPoison.insert(V);
  5241. Visited.insert(BB);
  5242. while (true) {
  5243. for (const auto &I : make_range(Begin, End)) {
  5244. if (isa<DbgInfoIntrinsic>(I))
  5245. continue;
  5246. if (--ScanLimit == 0)
  5247. return false;
  5248. if (mustTriggerUB(&I, YieldsPoison))
  5249. return true;
  5250. if (!isGuaranteedToTransferExecutionToSuccessor(&I))
  5251. return false;
  5252. // If an operand is poison and propagates it, mark I as yielding poison.
  5253. for (const Use &Op : I.operands()) {
  5254. if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
  5255. YieldsPoison.insert(&I);
  5256. break;
  5257. }
  5258. }
  5259. }
  5260. BB = BB->getSingleSuccessor();
  5261. if (!BB || !Visited.insert(BB).second)
  5262. break;
  5263. Begin = BB->getFirstNonPHI()->getIterator();
  5264. End = BB->end();
  5265. }
  5266. return false;
  5267. }
  5268. bool llvm::programUndefinedIfUndefOrPoison(const Instruction *Inst) {
  5269. return ::programUndefinedIfUndefOrPoison(Inst, false);
  5270. }
  5271. bool llvm::programUndefinedIfPoison(const Instruction *Inst) {
  5272. return ::programUndefinedIfUndefOrPoison(Inst, true);
  5273. }
  5274. static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
  5275. if (FMF.noNaNs())
  5276. return true;
  5277. if (auto *C = dyn_cast<ConstantFP>(V))
  5278. return !C->isNaN();
  5279. if (auto *C = dyn_cast<ConstantDataVector>(V)) {
  5280. if (!C->getElementType()->isFloatingPointTy())
  5281. return false;
  5282. for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
  5283. if (C->getElementAsAPFloat(I).isNaN())
  5284. return false;
  5285. }
  5286. return true;
  5287. }
  5288. if (isa<ConstantAggregateZero>(V))
  5289. return true;
  5290. return false;
  5291. }
  5292. static bool isKnownNonZero(const Value *V) {
  5293. if (auto *C = dyn_cast<ConstantFP>(V))
  5294. return !C->isZero();
  5295. if (auto *C = dyn_cast<ConstantDataVector>(V)) {
  5296. if (!C->getElementType()->isFloatingPointTy())
  5297. return false;
  5298. for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
  5299. if (C->getElementAsAPFloat(I).isZero())
  5300. return false;
  5301. }
  5302. return true;
  5303. }
  5304. return false;
  5305. }
  5306. /// Match clamp pattern for float types without care about NaNs or signed zeros.
  5307. /// Given non-min/max outer cmp/select from the clamp pattern this
  5308. /// function recognizes if it can be substitued by a "canonical" min/max
  5309. /// pattern.
  5310. static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred,
  5311. Value *CmpLHS, Value *CmpRHS,
  5312. Value *TrueVal, Value *FalseVal,
  5313. Value *&LHS, Value *&RHS) {
  5314. // Try to match
  5315. // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
  5316. // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
  5317. // and return description of the outer Max/Min.
  5318. // First, check if select has inverse order:
  5319. if (CmpRHS == FalseVal) {
  5320. std::swap(TrueVal, FalseVal);
  5321. Pred = CmpInst::getInversePredicate(Pred);
  5322. }
  5323. // Assume success now. If there's no match, callers should not use these anyway.
  5324. LHS = TrueVal;
  5325. RHS = FalseVal;
  5326. const APFloat *FC1;
  5327. if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
  5328. return {SPF_UNKNOWN, SPNB_NA, false};
  5329. const APFloat *FC2;
  5330. switch (Pred) {
  5331. case CmpInst::FCMP_OLT:
  5332. case CmpInst::FCMP_OLE:
  5333. case CmpInst::FCMP_ULT:
  5334. case CmpInst::FCMP_ULE:
  5335. if (match(FalseVal,
  5336. m_CombineOr(m_OrdFMin(m_Specific(CmpLHS), m_APFloat(FC2)),
  5337. m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
  5338. *FC1 < *FC2)
  5339. return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
  5340. break;
  5341. case CmpInst::FCMP_OGT:
  5342. case CmpInst::FCMP_OGE:
  5343. case CmpInst::FCMP_UGT:
  5344. case CmpInst::FCMP_UGE:
  5345. if (match(FalseVal,
  5346. m_CombineOr(m_OrdFMax(m_Specific(CmpLHS), m_APFloat(FC2)),
  5347. m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
  5348. *FC1 > *FC2)
  5349. return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
  5350. break;
  5351. default:
  5352. break;
  5353. }
  5354. return {SPF_UNKNOWN, SPNB_NA, false};
  5355. }
  5356. /// Recognize variations of:
  5357. /// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
  5358. static SelectPatternResult matchClamp(CmpInst::Predicate Pred,
  5359. Value *CmpLHS, Value *CmpRHS,
  5360. Value *TrueVal, Value *FalseVal) {
  5361. // Swap the select operands and predicate to match the patterns below.
  5362. if (CmpRHS != TrueVal) {
  5363. Pred = ICmpInst::getSwappedPredicate(Pred);
  5364. std::swap(TrueVal, FalseVal);
  5365. }
  5366. const APInt *C1;
  5367. if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
  5368. const APInt *C2;
  5369. // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
  5370. if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
  5371. C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
  5372. return {SPF_SMAX, SPNB_NA, false};
  5373. // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
  5374. if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
  5375. C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
  5376. return {SPF_SMIN, SPNB_NA, false};
  5377. // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
  5378. if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
  5379. C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
  5380. return {SPF_UMAX, SPNB_NA, false};
  5381. // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
  5382. if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
  5383. C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
  5384. return {SPF_UMIN, SPNB_NA, false};
  5385. }
  5386. return {SPF_UNKNOWN, SPNB_NA, false};
  5387. }
  5388. /// Recognize variations of:
  5389. /// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
  5390. static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred,
  5391. Value *CmpLHS, Value *CmpRHS,
  5392. Value *TVal, Value *FVal,
  5393. unsigned Depth) {
  5394. // TODO: Allow FP min/max with nnan/nsz.
  5395. assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
  5396. Value *A = nullptr, *B = nullptr;
  5397. SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
  5398. if (!SelectPatternResult::isMinOrMax(L.Flavor))
  5399. return {SPF_UNKNOWN, SPNB_NA, false};
  5400. Value *C = nullptr, *D = nullptr;
  5401. SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
  5402. if (L.Flavor != R.Flavor)
  5403. return {SPF_UNKNOWN, SPNB_NA, false};
  5404. // We have something like: x Pred y ? min(a, b) : min(c, d).
  5405. // Try to match the compare to the min/max operations of the select operands.
  5406. // First, make sure we have the right compare predicate.
  5407. switch (L.Flavor) {
  5408. case SPF_SMIN:
  5409. if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
  5410. Pred = ICmpInst::getSwappedPredicate(Pred);
  5411. std::swap(CmpLHS, CmpRHS);
  5412. }
  5413. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
  5414. break;
  5415. return {SPF_UNKNOWN, SPNB_NA, false};
  5416. case SPF_SMAX:
  5417. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
  5418. Pred = ICmpInst::getSwappedPredicate(Pred);
  5419. std::swap(CmpLHS, CmpRHS);
  5420. }
  5421. if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
  5422. break;
  5423. return {SPF_UNKNOWN, SPNB_NA, false};
  5424. case SPF_UMIN:
  5425. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
  5426. Pred = ICmpInst::getSwappedPredicate(Pred);
  5427. std::swap(CmpLHS, CmpRHS);
  5428. }
  5429. if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
  5430. break;
  5431. return {SPF_UNKNOWN, SPNB_NA, false};
  5432. case SPF_UMAX:
  5433. if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
  5434. Pred = ICmpInst::getSwappedPredicate(Pred);
  5435. std::swap(CmpLHS, CmpRHS);
  5436. }
  5437. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
  5438. break;
  5439. return {SPF_UNKNOWN, SPNB_NA, false};
  5440. default:
  5441. return {SPF_UNKNOWN, SPNB_NA, false};
  5442. }
  5443. // If there is a common operand in the already matched min/max and the other
  5444. // min/max operands match the compare operands (either directly or inverted),
  5445. // then this is min/max of the same flavor.
  5446. // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
  5447. // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
  5448. if (D == B) {
  5449. if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
  5450. match(A, m_Not(m_Specific(CmpRHS)))))
  5451. return {L.Flavor, SPNB_NA, false};
  5452. }
  5453. // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
  5454. // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
  5455. if (C == B) {
  5456. if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
  5457. match(A, m_Not(m_Specific(CmpRHS)))))
  5458. return {L.Flavor, SPNB_NA, false};
  5459. }
  5460. // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
  5461. // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
  5462. if (D == A) {
  5463. if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
  5464. match(B, m_Not(m_Specific(CmpRHS)))))
  5465. return {L.Flavor, SPNB_NA, false};
  5466. }
  5467. // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
  5468. // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
  5469. if (C == A) {
  5470. if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
  5471. match(B, m_Not(m_Specific(CmpRHS)))))
  5472. return {L.Flavor, SPNB_NA, false};
  5473. }
  5474. return {SPF_UNKNOWN, SPNB_NA, false};
  5475. }
  5476. /// If the input value is the result of a 'not' op, constant integer, or vector
  5477. /// splat of a constant integer, return the bitwise-not source value.
  5478. /// TODO: This could be extended to handle non-splat vector integer constants.
  5479. static Value *getNotValue(Value *V) {
  5480. Value *NotV;
  5481. if (match(V, m_Not(m_Value(NotV))))
  5482. return NotV;
  5483. const APInt *C;
  5484. if (match(V, m_APInt(C)))
  5485. return ConstantInt::get(V->getType(), ~(*C));
  5486. return nullptr;
  5487. }
  5488. /// Match non-obvious integer minimum and maximum sequences.
  5489. static SelectPatternResult matchMinMax(CmpInst::Predicate Pred,
  5490. Value *CmpLHS, Value *CmpRHS,
  5491. Value *TrueVal, Value *FalseVal,
  5492. Value *&LHS, Value *&RHS,
  5493. unsigned Depth) {
  5494. // Assume success. If there's no match, callers should not use these anyway.
  5495. LHS = TrueVal;
  5496. RHS = FalseVal;
  5497. SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
  5498. if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
  5499. return SPR;
  5500. SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
  5501. if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
  5502. return SPR;
  5503. // Look through 'not' ops to find disguised min/max.
  5504. // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
  5505. // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
  5506. if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
  5507. switch (Pred) {
  5508. case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
  5509. case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
  5510. case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
  5511. case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
  5512. default: break;
  5513. }
  5514. }
  5515. // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
  5516. // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
  5517. if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
  5518. switch (Pred) {
  5519. case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
  5520. case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
  5521. case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
  5522. case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
  5523. default: break;
  5524. }
  5525. }
  5526. if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
  5527. return {SPF_UNKNOWN, SPNB_NA, false};
  5528. const APInt *C1;
  5529. if (!match(CmpRHS, m_APInt(C1)))
  5530. return {SPF_UNKNOWN, SPNB_NA, false};
  5531. // An unsigned min/max can be written with a signed compare.
  5532. const APInt *C2;
  5533. if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
  5534. (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
  5535. // Is the sign bit set?
  5536. // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
  5537. // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
  5538. if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
  5539. return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
  5540. // Is the sign bit clear?
  5541. // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
  5542. // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
  5543. if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
  5544. return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
  5545. }
  5546. return {SPF_UNKNOWN, SPNB_NA, false};
  5547. }
  5548. bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) {
  5549. assert(X && Y && "Invalid operand");
  5550. // X = sub (0, Y) || X = sub nsw (0, Y)
  5551. if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) ||
  5552. (NeedNSW && match(X, m_NSWSub(m_ZeroInt(), m_Specific(Y)))))
  5553. return true;
  5554. // Y = sub (0, X) || Y = sub nsw (0, X)
  5555. if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) ||
  5556. (NeedNSW && match(Y, m_NSWSub(m_ZeroInt(), m_Specific(X)))))
  5557. return true;
  5558. // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
  5559. Value *A, *B;
  5560. return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
  5561. match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
  5562. (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
  5563. match(Y, m_NSWSub(m_Specific(B), m_Specific(A)))));
  5564. }
  5565. static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred,
  5566. FastMathFlags FMF,
  5567. Value *CmpLHS, Value *CmpRHS,
  5568. Value *TrueVal, Value *FalseVal,
  5569. Value *&LHS, Value *&RHS,
  5570. unsigned Depth) {
  5571. bool HasMismatchedZeros = false;
  5572. if (CmpInst::isFPPredicate(Pred)) {
  5573. // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
  5574. // 0.0 operand, set the compare's 0.0 operands to that same value for the
  5575. // purpose of identifying min/max. Disregard vector constants with undefined
  5576. // elements because those can not be back-propagated for analysis.
  5577. Value *OutputZeroVal = nullptr;
  5578. if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
  5579. !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
  5580. OutputZeroVal = TrueVal;
  5581. else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
  5582. !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
  5583. OutputZeroVal = FalseVal;
  5584. if (OutputZeroVal) {
  5585. if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
  5586. HasMismatchedZeros = true;
  5587. CmpLHS = OutputZeroVal;
  5588. }
  5589. if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
  5590. HasMismatchedZeros = true;
  5591. CmpRHS = OutputZeroVal;
  5592. }
  5593. }
  5594. }
  5595. LHS = CmpLHS;
  5596. RHS = CmpRHS;
  5597. // Signed zero may return inconsistent results between implementations.
  5598. // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
  5599. // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
  5600. // Therefore, we behave conservatively and only proceed if at least one of the
  5601. // operands is known to not be zero or if we don't care about signed zero.
  5602. switch (Pred) {
  5603. default: break;
  5604. case CmpInst::FCMP_OGT: case CmpInst::FCMP_OLT:
  5605. case CmpInst::FCMP_UGT: case CmpInst::FCMP_ULT:
  5606. if (!HasMismatchedZeros)
  5607. break;
  5608. [[fallthrough]];
  5609. case CmpInst::FCMP_OGE: case CmpInst::FCMP_OLE:
  5610. case CmpInst::FCMP_UGE: case CmpInst::FCMP_ULE:
  5611. if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
  5612. !isKnownNonZero(CmpRHS))
  5613. return {SPF_UNKNOWN, SPNB_NA, false};
  5614. }
  5615. SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
  5616. bool Ordered = false;
  5617. // When given one NaN and one non-NaN input:
  5618. // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
  5619. // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
  5620. // ordered comparison fails), which could be NaN or non-NaN.
  5621. // so here we discover exactly what NaN behavior is required/accepted.
  5622. if (CmpInst::isFPPredicate(Pred)) {
  5623. bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
  5624. bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
  5625. if (LHSSafe && RHSSafe) {
  5626. // Both operands are known non-NaN.
  5627. NaNBehavior = SPNB_RETURNS_ANY;
  5628. } else if (CmpInst::isOrdered(Pred)) {
  5629. // An ordered comparison will return false when given a NaN, so it
  5630. // returns the RHS.
  5631. Ordered = true;
  5632. if (LHSSafe)
  5633. // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
  5634. NaNBehavior = SPNB_RETURNS_NAN;
  5635. else if (RHSSafe)
  5636. NaNBehavior = SPNB_RETURNS_OTHER;
  5637. else
  5638. // Completely unsafe.
  5639. return {SPF_UNKNOWN, SPNB_NA, false};
  5640. } else {
  5641. Ordered = false;
  5642. // An unordered comparison will return true when given a NaN, so it
  5643. // returns the LHS.
  5644. if (LHSSafe)
  5645. // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
  5646. NaNBehavior = SPNB_RETURNS_OTHER;
  5647. else if (RHSSafe)
  5648. NaNBehavior = SPNB_RETURNS_NAN;
  5649. else
  5650. // Completely unsafe.
  5651. return {SPF_UNKNOWN, SPNB_NA, false};
  5652. }
  5653. }
  5654. if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
  5655. std::swap(CmpLHS, CmpRHS);
  5656. Pred = CmpInst::getSwappedPredicate(Pred);
  5657. if (NaNBehavior == SPNB_RETURNS_NAN)
  5658. NaNBehavior = SPNB_RETURNS_OTHER;
  5659. else if (NaNBehavior == SPNB_RETURNS_OTHER)
  5660. NaNBehavior = SPNB_RETURNS_NAN;
  5661. Ordered = !Ordered;
  5662. }
  5663. // ([if]cmp X, Y) ? X : Y
  5664. if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
  5665. switch (Pred) {
  5666. default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
  5667. case ICmpInst::ICMP_UGT:
  5668. case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
  5669. case ICmpInst::ICMP_SGT:
  5670. case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
  5671. case ICmpInst::ICMP_ULT:
  5672. case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
  5673. case ICmpInst::ICMP_SLT:
  5674. case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
  5675. case FCmpInst::FCMP_UGT:
  5676. case FCmpInst::FCMP_UGE:
  5677. case FCmpInst::FCMP_OGT:
  5678. case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
  5679. case FCmpInst::FCMP_ULT:
  5680. case FCmpInst::FCMP_ULE:
  5681. case FCmpInst::FCMP_OLT:
  5682. case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
  5683. }
  5684. }
  5685. if (isKnownNegation(TrueVal, FalseVal)) {
  5686. // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
  5687. // match against either LHS or sext(LHS).
  5688. auto MaybeSExtCmpLHS =
  5689. m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
  5690. auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
  5691. auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
  5692. if (match(TrueVal, MaybeSExtCmpLHS)) {
  5693. // Set the return values. If the compare uses the negated value (-X >s 0),
  5694. // swap the return values because the negated value is always 'RHS'.
  5695. LHS = TrueVal;
  5696. RHS = FalseVal;
  5697. if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
  5698. std::swap(LHS, RHS);
  5699. // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
  5700. // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
  5701. if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
  5702. return {SPF_ABS, SPNB_NA, false};
  5703. // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
  5704. if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
  5705. return {SPF_ABS, SPNB_NA, false};
  5706. // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
  5707. // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
  5708. if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
  5709. return {SPF_NABS, SPNB_NA, false};
  5710. }
  5711. else if (match(FalseVal, MaybeSExtCmpLHS)) {
  5712. // Set the return values. If the compare uses the negated value (-X >s 0),
  5713. // swap the return values because the negated value is always 'RHS'.
  5714. LHS = FalseVal;
  5715. RHS = TrueVal;
  5716. if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
  5717. std::swap(LHS, RHS);
  5718. // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
  5719. // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
  5720. if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
  5721. return {SPF_NABS, SPNB_NA, false};
  5722. // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
  5723. // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
  5724. if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
  5725. return {SPF_ABS, SPNB_NA, false};
  5726. }
  5727. }
  5728. if (CmpInst::isIntPredicate(Pred))
  5729. return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
  5730. // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
  5731. // may return either -0.0 or 0.0, so fcmp/select pair has stricter
  5732. // semantics than minNum. Be conservative in such case.
  5733. if (NaNBehavior != SPNB_RETURNS_ANY ||
  5734. (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
  5735. !isKnownNonZero(CmpRHS)))
  5736. return {SPF_UNKNOWN, SPNB_NA, false};
  5737. return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
  5738. }
  5739. /// Helps to match a select pattern in case of a type mismatch.
  5740. ///
  5741. /// The function processes the case when type of true and false values of a
  5742. /// select instruction differs from type of the cmp instruction operands because
  5743. /// of a cast instruction. The function checks if it is legal to move the cast
  5744. /// operation after "select". If yes, it returns the new second value of
  5745. /// "select" (with the assumption that cast is moved):
  5746. /// 1. As operand of cast instruction when both values of "select" are same cast
  5747. /// instructions.
  5748. /// 2. As restored constant (by applying reverse cast operation) when the first
  5749. /// value of the "select" is a cast operation and the second value is a
  5750. /// constant.
  5751. /// NOTE: We return only the new second value because the first value could be
  5752. /// accessed as operand of cast instruction.
  5753. static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
  5754. Instruction::CastOps *CastOp) {
  5755. auto *Cast1 = dyn_cast<CastInst>(V1);
  5756. if (!Cast1)
  5757. return nullptr;
  5758. *CastOp = Cast1->getOpcode();
  5759. Type *SrcTy = Cast1->getSrcTy();
  5760. if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
  5761. // If V1 and V2 are both the same cast from the same type, look through V1.
  5762. if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
  5763. return Cast2->getOperand(0);
  5764. return nullptr;
  5765. }
  5766. auto *C = dyn_cast<Constant>(V2);
  5767. if (!C)
  5768. return nullptr;
  5769. Constant *CastedTo = nullptr;
  5770. switch (*CastOp) {
  5771. case Instruction::ZExt:
  5772. if (CmpI->isUnsigned())
  5773. CastedTo = ConstantExpr::getTrunc(C, SrcTy);
  5774. break;
  5775. case Instruction::SExt:
  5776. if (CmpI->isSigned())
  5777. CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
  5778. break;
  5779. case Instruction::Trunc:
  5780. Constant *CmpConst;
  5781. if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
  5782. CmpConst->getType() == SrcTy) {
  5783. // Here we have the following case:
  5784. //
  5785. // %cond = cmp iN %x, CmpConst
  5786. // %tr = trunc iN %x to iK
  5787. // %narrowsel = select i1 %cond, iK %t, iK C
  5788. //
  5789. // We can always move trunc after select operation:
  5790. //
  5791. // %cond = cmp iN %x, CmpConst
  5792. // %widesel = select i1 %cond, iN %x, iN CmpConst
  5793. // %tr = trunc iN %widesel to iK
  5794. //
  5795. // Note that C could be extended in any way because we don't care about
  5796. // upper bits after truncation. It can't be abs pattern, because it would
  5797. // look like:
  5798. //
  5799. // select i1 %cond, x, -x.
  5800. //
  5801. // So only min/max pattern could be matched. Such match requires widened C
  5802. // == CmpConst. That is why set widened C = CmpConst, condition trunc
  5803. // CmpConst == C is checked below.
  5804. CastedTo = CmpConst;
  5805. } else {
  5806. CastedTo = ConstantExpr::getIntegerCast(C, SrcTy, CmpI->isSigned());
  5807. }
  5808. break;
  5809. case Instruction::FPTrunc:
  5810. CastedTo = ConstantExpr::getFPExtend(C, SrcTy, true);
  5811. break;
  5812. case Instruction::FPExt:
  5813. CastedTo = ConstantExpr::getFPTrunc(C, SrcTy, true);
  5814. break;
  5815. case Instruction::FPToUI:
  5816. CastedTo = ConstantExpr::getUIToFP(C, SrcTy, true);
  5817. break;
  5818. case Instruction::FPToSI:
  5819. CastedTo = ConstantExpr::getSIToFP(C, SrcTy, true);
  5820. break;
  5821. case Instruction::UIToFP:
  5822. CastedTo = ConstantExpr::getFPToUI(C, SrcTy, true);
  5823. break;
  5824. case Instruction::SIToFP:
  5825. CastedTo = ConstantExpr::getFPToSI(C, SrcTy, true);
  5826. break;
  5827. default:
  5828. break;
  5829. }
  5830. if (!CastedTo)
  5831. return nullptr;
  5832. // Make sure the cast doesn't lose any information.
  5833. Constant *CastedBack =
  5834. ConstantExpr::getCast(*CastOp, CastedTo, C->getType(), true);
  5835. if (CastedBack != C)
  5836. return nullptr;
  5837. return CastedTo;
  5838. }
  5839. SelectPatternResult llvm::matchSelectPattern(Value *V, Value *&LHS, Value *&RHS,
  5840. Instruction::CastOps *CastOp,
  5841. unsigned Depth) {
  5842. if (Depth >= MaxAnalysisRecursionDepth)
  5843. return {SPF_UNKNOWN, SPNB_NA, false};
  5844. SelectInst *SI = dyn_cast<SelectInst>(V);
  5845. if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
  5846. CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
  5847. if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
  5848. Value *TrueVal = SI->getTrueValue();
  5849. Value *FalseVal = SI->getFalseValue();
  5850. return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
  5851. CastOp, Depth);
  5852. }
  5853. SelectPatternResult llvm::matchDecomposedSelectPattern(
  5854. CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
  5855. Instruction::CastOps *CastOp, unsigned Depth) {
  5856. CmpInst::Predicate Pred = CmpI->getPredicate();
  5857. Value *CmpLHS = CmpI->getOperand(0);
  5858. Value *CmpRHS = CmpI->getOperand(1);
  5859. FastMathFlags FMF;
  5860. if (isa<FPMathOperator>(CmpI))
  5861. FMF = CmpI->getFastMathFlags();
  5862. // Bail out early.
  5863. if (CmpI->isEquality())
  5864. return {SPF_UNKNOWN, SPNB_NA, false};
  5865. // Deal with type mismatches.
  5866. if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
  5867. if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
  5868. // If this is a potential fmin/fmax with a cast to integer, then ignore
  5869. // -0.0 because there is no corresponding integer value.
  5870. if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
  5871. FMF.setNoSignedZeros();
  5872. return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
  5873. cast<CastInst>(TrueVal)->getOperand(0), C,
  5874. LHS, RHS, Depth);
  5875. }
  5876. if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
  5877. // If this is a potential fmin/fmax with a cast to integer, then ignore
  5878. // -0.0 because there is no corresponding integer value.
  5879. if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
  5880. FMF.setNoSignedZeros();
  5881. return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
  5882. C, cast<CastInst>(FalseVal)->getOperand(0),
  5883. LHS, RHS, Depth);
  5884. }
  5885. }
  5886. return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
  5887. LHS, RHS, Depth);
  5888. }
  5889. CmpInst::Predicate llvm::getMinMaxPred(SelectPatternFlavor SPF, bool Ordered) {
  5890. if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
  5891. if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
  5892. if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
  5893. if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
  5894. if (SPF == SPF_FMINNUM)
  5895. return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
  5896. if (SPF == SPF_FMAXNUM)
  5897. return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
  5898. llvm_unreachable("unhandled!");
  5899. }
  5900. SelectPatternFlavor llvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {
  5901. if (SPF == SPF_SMIN) return SPF_SMAX;
  5902. if (SPF == SPF_UMIN) return SPF_UMAX;
  5903. if (SPF == SPF_SMAX) return SPF_SMIN;
  5904. if (SPF == SPF_UMAX) return SPF_UMIN;
  5905. llvm_unreachable("unhandled!");
  5906. }
  5907. Intrinsic::ID llvm::getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID) {
  5908. switch (MinMaxID) {
  5909. case Intrinsic::smax: return Intrinsic::smin;
  5910. case Intrinsic::smin: return Intrinsic::smax;
  5911. case Intrinsic::umax: return Intrinsic::umin;
  5912. case Intrinsic::umin: return Intrinsic::umax;
  5913. default: llvm_unreachable("Unexpected intrinsic");
  5914. }
  5915. }
  5916. APInt llvm::getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth) {
  5917. switch (SPF) {
  5918. case SPF_SMAX: return APInt::getSignedMaxValue(BitWidth);
  5919. case SPF_SMIN: return APInt::getSignedMinValue(BitWidth);
  5920. case SPF_UMAX: return APInt::getMaxValue(BitWidth);
  5921. case SPF_UMIN: return APInt::getMinValue(BitWidth);
  5922. default: llvm_unreachable("Unexpected flavor");
  5923. }
  5924. }
  5925. std::pair<Intrinsic::ID, bool>
  5926. llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
  5927. // Check if VL contains select instructions that can be folded into a min/max
  5928. // vector intrinsic and return the intrinsic if it is possible.
  5929. // TODO: Support floating point min/max.
  5930. bool AllCmpSingleUse = true;
  5931. SelectPatternResult SelectPattern;
  5932. SelectPattern.Flavor = SPF_UNKNOWN;
  5933. if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
  5934. Value *LHS, *RHS;
  5935. auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
  5936. if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) ||
  5937. CurrentPattern.Flavor == SPF_FMINNUM ||
  5938. CurrentPattern.Flavor == SPF_FMAXNUM ||
  5939. !I->getType()->isIntOrIntVectorTy())
  5940. return false;
  5941. if (SelectPattern.Flavor != SPF_UNKNOWN &&
  5942. SelectPattern.Flavor != CurrentPattern.Flavor)
  5943. return false;
  5944. SelectPattern = CurrentPattern;
  5945. AllCmpSingleUse &=
  5946. match(I, m_Select(m_OneUse(m_Value()), m_Value(), m_Value()));
  5947. return true;
  5948. })) {
  5949. switch (SelectPattern.Flavor) {
  5950. case SPF_SMIN:
  5951. return {Intrinsic::smin, AllCmpSingleUse};
  5952. case SPF_UMIN:
  5953. return {Intrinsic::umin, AllCmpSingleUse};
  5954. case SPF_SMAX:
  5955. return {Intrinsic::smax, AllCmpSingleUse};
  5956. case SPF_UMAX:
  5957. return {Intrinsic::umax, AllCmpSingleUse};
  5958. default:
  5959. llvm_unreachable("unexpected select pattern flavor");
  5960. }
  5961. }
  5962. return {Intrinsic::not_intrinsic, false};
  5963. }
  5964. bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
  5965. Value *&Start, Value *&Step) {
  5966. // Handle the case of a simple two-predecessor recurrence PHI.
  5967. // There's a lot more that could theoretically be done here, but
  5968. // this is sufficient to catch some interesting cases.
  5969. if (P->getNumIncomingValues() != 2)
  5970. return false;
  5971. for (unsigned i = 0; i != 2; ++i) {
  5972. Value *L = P->getIncomingValue(i);
  5973. Value *R = P->getIncomingValue(!i);
  5974. Operator *LU = dyn_cast<Operator>(L);
  5975. if (!LU)
  5976. continue;
  5977. unsigned Opcode = LU->getOpcode();
  5978. switch (Opcode) {
  5979. default:
  5980. continue;
  5981. // TODO: Expand list -- xor, div, gep, uaddo, etc..
  5982. case Instruction::LShr:
  5983. case Instruction::AShr:
  5984. case Instruction::Shl:
  5985. case Instruction::Add:
  5986. case Instruction::Sub:
  5987. case Instruction::And:
  5988. case Instruction::Or:
  5989. case Instruction::Mul:
  5990. case Instruction::FMul: {
  5991. Value *LL = LU->getOperand(0);
  5992. Value *LR = LU->getOperand(1);
  5993. // Find a recurrence.
  5994. if (LL == P)
  5995. L = LR;
  5996. else if (LR == P)
  5997. L = LL;
  5998. else
  5999. continue; // Check for recurrence with L and R flipped.
  6000. break; // Match!
  6001. }
  6002. };
  6003. // We have matched a recurrence of the form:
  6004. // %iv = [R, %entry], [%iv.next, %backedge]
  6005. // %iv.next = binop %iv, L
  6006. // OR
  6007. // %iv = [R, %entry], [%iv.next, %backedge]
  6008. // %iv.next = binop L, %iv
  6009. BO = cast<BinaryOperator>(LU);
  6010. Start = R;
  6011. Step = L;
  6012. return true;
  6013. }
  6014. return false;
  6015. }
  6016. bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
  6017. Value *&Start, Value *&Step) {
  6018. BinaryOperator *BO = nullptr;
  6019. P = dyn_cast<PHINode>(I->getOperand(0));
  6020. if (!P)
  6021. P = dyn_cast<PHINode>(I->getOperand(1));
  6022. return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
  6023. }
  6024. /// Return true if "icmp Pred LHS RHS" is always true.
  6025. static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
  6026. const Value *RHS, const DataLayout &DL,
  6027. unsigned Depth) {
  6028. if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
  6029. return true;
  6030. switch (Pred) {
  6031. default:
  6032. return false;
  6033. case CmpInst::ICMP_SLE: {
  6034. const APInt *C;
  6035. // LHS s<= LHS +_{nsw} C if C >= 0
  6036. if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))))
  6037. return !C->isNegative();
  6038. return false;
  6039. }
  6040. case CmpInst::ICMP_ULE: {
  6041. const APInt *C;
  6042. // LHS u<= LHS +_{nuw} C for any C
  6043. if (match(RHS, m_NUWAdd(m_Specific(LHS), m_APInt(C))))
  6044. return true;
  6045. // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
  6046. auto MatchNUWAddsToSameValue = [&](const Value *A, const Value *B,
  6047. const Value *&X,
  6048. const APInt *&CA, const APInt *&CB) {
  6049. if (match(A, m_NUWAdd(m_Value(X), m_APInt(CA))) &&
  6050. match(B, m_NUWAdd(m_Specific(X), m_APInt(CB))))
  6051. return true;
  6052. // If X & C == 0 then (X | C) == X +_{nuw} C
  6053. if (match(A, m_Or(m_Value(X), m_APInt(CA))) &&
  6054. match(B, m_Or(m_Specific(X), m_APInt(CB)))) {
  6055. KnownBits Known(CA->getBitWidth());
  6056. computeKnownBits(X, Known, DL, Depth + 1, /*AC*/ nullptr,
  6057. /*CxtI*/ nullptr, /*DT*/ nullptr);
  6058. if (CA->isSubsetOf(Known.Zero) && CB->isSubsetOf(Known.Zero))
  6059. return true;
  6060. }
  6061. return false;
  6062. };
  6063. const Value *X;
  6064. const APInt *CLHS, *CRHS;
  6065. if (MatchNUWAddsToSameValue(LHS, RHS, X, CLHS, CRHS))
  6066. return CLHS->ule(*CRHS);
  6067. return false;
  6068. }
  6069. }
  6070. }
  6071. /// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
  6072. /// ALHS ARHS" is true. Otherwise, return std::nullopt.
  6073. static std::optional<bool>
  6074. isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS,
  6075. const Value *ARHS, const Value *BLHS, const Value *BRHS,
  6076. const DataLayout &DL, unsigned Depth) {
  6077. switch (Pred) {
  6078. default:
  6079. return std::nullopt;
  6080. case CmpInst::ICMP_SLT:
  6081. case CmpInst::ICMP_SLE:
  6082. if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS, DL, Depth) &&
  6083. isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS, DL, Depth))
  6084. return true;
  6085. return std::nullopt;
  6086. case CmpInst::ICMP_ULT:
  6087. case CmpInst::ICMP_ULE:
  6088. if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS, DL, Depth) &&
  6089. isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS, DL, Depth))
  6090. return true;
  6091. return std::nullopt;
  6092. }
  6093. }
  6094. /// Return true if the operands of two compares (expanded as "L0 pred L1" and
  6095. /// "R0 pred R1") match. IsSwappedOps is true when the operands match, but are
  6096. /// swapped.
  6097. static bool areMatchingOperands(const Value *L0, const Value *L1, const Value *R0,
  6098. const Value *R1, bool &AreSwappedOps) {
  6099. bool AreMatchingOps = (L0 == R0 && L1 == R1);
  6100. AreSwappedOps = (L0 == R1 && L1 == R0);
  6101. return AreMatchingOps || AreSwappedOps;
  6102. }
  6103. /// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
  6104. /// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
  6105. /// Otherwise, return std::nullopt if we can't infer anything.
  6106. static std::optional<bool>
  6107. isImpliedCondMatchingOperands(CmpInst::Predicate LPred,
  6108. CmpInst::Predicate RPred, bool AreSwappedOps) {
  6109. // Canonicalize the predicate as if the operands were not commuted.
  6110. if (AreSwappedOps)
  6111. RPred = ICmpInst::getSwappedPredicate(RPred);
  6112. if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
  6113. return true;
  6114. if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
  6115. return false;
  6116. return std::nullopt;
  6117. }
  6118. /// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
  6119. /// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
  6120. /// Otherwise, return std::nullopt if we can't infer anything.
  6121. static std::optional<bool> isImpliedCondCommonOperandWithConstants(
  6122. CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
  6123. const APInt &RC) {
  6124. ConstantRange DomCR = ConstantRange::makeExactICmpRegion(LPred, LC);
  6125. ConstantRange CR = ConstantRange::makeExactICmpRegion(RPred, RC);
  6126. ConstantRange Intersection = DomCR.intersectWith(CR);
  6127. ConstantRange Difference = DomCR.difference(CR);
  6128. if (Intersection.isEmptySet())
  6129. return false;
  6130. if (Difference.isEmptySet())
  6131. return true;
  6132. return std::nullopt;
  6133. }
  6134. /// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
  6135. /// is true. Return false if LHS implies RHS is false. Otherwise, return
  6136. /// std::nullopt if we can't infer anything.
  6137. static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
  6138. CmpInst::Predicate RPred,
  6139. const Value *R0, const Value *R1,
  6140. const DataLayout &DL,
  6141. bool LHSIsTrue, unsigned Depth) {
  6142. Value *L0 = LHS->getOperand(0);
  6143. Value *L1 = LHS->getOperand(1);
  6144. // The rest of the logic assumes the LHS condition is true. If that's not the
  6145. // case, invert the predicate to make it so.
  6146. CmpInst::Predicate LPred =
  6147. LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
  6148. // Can we infer anything when the two compares have matching operands?
  6149. bool AreSwappedOps;
  6150. if (areMatchingOperands(L0, L1, R0, R1, AreSwappedOps))
  6151. return isImpliedCondMatchingOperands(LPred, RPred, AreSwappedOps);
  6152. // Can we infer anything when the 0-operands match and the 1-operands are
  6153. // constants (not necessarily matching)?
  6154. const APInt *LC, *RC;
  6155. if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
  6156. return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
  6157. if (LPred == RPred)
  6158. return isImpliedCondOperands(LPred, L0, L1, R0, R1, DL, Depth);
  6159. return std::nullopt;
  6160. }
  6161. /// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
  6162. /// false. Otherwise, return std::nullopt if we can't infer anything. We
  6163. /// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
  6164. /// instruction.
  6165. static std::optional<bool>
  6166. isImpliedCondAndOr(const Instruction *LHS, CmpInst::Predicate RHSPred,
  6167. const Value *RHSOp0, const Value *RHSOp1,
  6168. const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
  6169. // The LHS must be an 'or', 'and', or a 'select' instruction.
  6170. assert((LHS->getOpcode() == Instruction::And ||
  6171. LHS->getOpcode() == Instruction::Or ||
  6172. LHS->getOpcode() == Instruction::Select) &&
  6173. "Expected LHS to be 'and', 'or', or 'select'.");
  6174. assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
  6175. // If the result of an 'or' is false, then we know both legs of the 'or' are
  6176. // false. Similarly, if the result of an 'and' is true, then we know both
  6177. // legs of the 'and' are true.
  6178. const Value *ALHS, *ARHS;
  6179. if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
  6180. (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
  6181. // FIXME: Make this non-recursion.
  6182. if (std::optional<bool> Implication = isImpliedCondition(
  6183. ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
  6184. return Implication;
  6185. if (std::optional<bool> Implication = isImpliedCondition(
  6186. ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
  6187. return Implication;
  6188. return std::nullopt;
  6189. }
  6190. return std::nullopt;
  6191. }
  6192. std::optional<bool>
  6193. llvm::isImpliedCondition(const Value *LHS, CmpInst::Predicate RHSPred,
  6194. const Value *RHSOp0, const Value *RHSOp1,
  6195. const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
  6196. // Bail out when we hit the limit.
  6197. if (Depth == MaxAnalysisRecursionDepth)
  6198. return std::nullopt;
  6199. // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
  6200. // example.
  6201. if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
  6202. return std::nullopt;
  6203. assert(LHS->getType()->isIntOrIntVectorTy(1) &&
  6204. "Expected integer type only!");
  6205. // Both LHS and RHS are icmps.
  6206. const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
  6207. if (LHSCmp)
  6208. return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
  6209. Depth);
  6210. /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
  6211. /// the RHS to be an icmp.
  6212. /// FIXME: Add support for and/or/select on the RHS.
  6213. if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
  6214. if ((LHSI->getOpcode() == Instruction::And ||
  6215. LHSI->getOpcode() == Instruction::Or ||
  6216. LHSI->getOpcode() == Instruction::Select))
  6217. return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
  6218. Depth);
  6219. }
  6220. return std::nullopt;
  6221. }
  6222. std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
  6223. const DataLayout &DL,
  6224. bool LHSIsTrue, unsigned Depth) {
  6225. // LHS ==> RHS by definition
  6226. if (LHS == RHS)
  6227. return LHSIsTrue;
  6228. if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS))
  6229. return isImpliedCondition(LHS, RHSCmp->getPredicate(),
  6230. RHSCmp->getOperand(0), RHSCmp->getOperand(1), DL,
  6231. LHSIsTrue, Depth);
  6232. if (Depth == MaxAnalysisRecursionDepth)
  6233. return std::nullopt;
  6234. // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
  6235. // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
  6236. const Value *RHS1, *RHS2;
  6237. if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
  6238. if (std::optional<bool> Imp =
  6239. isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
  6240. if (*Imp == true)
  6241. return true;
  6242. if (std::optional<bool> Imp =
  6243. isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
  6244. if (*Imp == true)
  6245. return true;
  6246. }
  6247. if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
  6248. if (std::optional<bool> Imp =
  6249. isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
  6250. if (*Imp == false)
  6251. return false;
  6252. if (std::optional<bool> Imp =
  6253. isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
  6254. if (*Imp == false)
  6255. return false;
  6256. }
  6257. return std::nullopt;
  6258. }
  6259. // Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
  6260. // condition dominating ContextI or nullptr, if no condition is found.
  6261. static std::pair<Value *, bool>
  6262. getDomPredecessorCondition(const Instruction *ContextI) {
  6263. if (!ContextI || !ContextI->getParent())
  6264. return {nullptr, false};
  6265. // TODO: This is a poor/cheap way to determine dominance. Should we use a
  6266. // dominator tree (eg, from a SimplifyQuery) instead?
  6267. const BasicBlock *ContextBB = ContextI->getParent();
  6268. const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
  6269. if (!PredBB)
  6270. return {nullptr, false};
  6271. // We need a conditional branch in the predecessor.
  6272. Value *PredCond;
  6273. BasicBlock *TrueBB, *FalseBB;
  6274. if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
  6275. return {nullptr, false};
  6276. // The branch should get simplified. Don't bother simplifying this condition.
  6277. if (TrueBB == FalseBB)
  6278. return {nullptr, false};
  6279. assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
  6280. "Predecessor block does not point to successor?");
  6281. // Is this condition implied by the predecessor condition?
  6282. return {PredCond, TrueBB == ContextBB};
  6283. }
  6284. std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
  6285. const Instruction *ContextI,
  6286. const DataLayout &DL) {
  6287. assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
  6288. auto PredCond = getDomPredecessorCondition(ContextI);
  6289. if (PredCond.first)
  6290. return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
  6291. return std::nullopt;
  6292. }
  6293. std::optional<bool> llvm::isImpliedByDomCondition(CmpInst::Predicate Pred,
  6294. const Value *LHS,
  6295. const Value *RHS,
  6296. const Instruction *ContextI,
  6297. const DataLayout &DL) {
  6298. auto PredCond = getDomPredecessorCondition(ContextI);
  6299. if (PredCond.first)
  6300. return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
  6301. PredCond.second);
  6302. return std::nullopt;
  6303. }
  6304. static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
  6305. APInt &Upper, const InstrInfoQuery &IIQ,
  6306. bool PreferSignedRange) {
  6307. unsigned Width = Lower.getBitWidth();
  6308. const APInt *C;
  6309. switch (BO.getOpcode()) {
  6310. case Instruction::Add:
  6311. if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
  6312. bool HasNSW = IIQ.hasNoSignedWrap(&BO);
  6313. bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
  6314. // If the caller expects a signed compare, then try to use a signed range.
  6315. // Otherwise if both no-wraps are set, use the unsigned range because it
  6316. // is never larger than the signed range. Example:
  6317. // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
  6318. if (PreferSignedRange && HasNSW && HasNUW)
  6319. HasNUW = false;
  6320. if (HasNUW) {
  6321. // 'add nuw x, C' produces [C, UINT_MAX].
  6322. Lower = *C;
  6323. } else if (HasNSW) {
  6324. if (C->isNegative()) {
  6325. // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
  6326. Lower = APInt::getSignedMinValue(Width);
  6327. Upper = APInt::getSignedMaxValue(Width) + *C + 1;
  6328. } else {
  6329. // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
  6330. Lower = APInt::getSignedMinValue(Width) + *C;
  6331. Upper = APInt::getSignedMaxValue(Width) + 1;
  6332. }
  6333. }
  6334. }
  6335. break;
  6336. case Instruction::And:
  6337. if (match(BO.getOperand(1), m_APInt(C)))
  6338. // 'and x, C' produces [0, C].
  6339. Upper = *C + 1;
  6340. break;
  6341. case Instruction::Or:
  6342. if (match(BO.getOperand(1), m_APInt(C)))
  6343. // 'or x, C' produces [C, UINT_MAX].
  6344. Lower = *C;
  6345. break;
  6346. case Instruction::AShr:
  6347. if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
  6348. // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
  6349. Lower = APInt::getSignedMinValue(Width).ashr(*C);
  6350. Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
  6351. } else if (match(BO.getOperand(0), m_APInt(C))) {
  6352. unsigned ShiftAmount = Width - 1;
  6353. if (!C->isZero() && IIQ.isExact(&BO))
  6354. ShiftAmount = C->countTrailingZeros();
  6355. if (C->isNegative()) {
  6356. // 'ashr C, x' produces [C, C >> (Width-1)]
  6357. Lower = *C;
  6358. Upper = C->ashr(ShiftAmount) + 1;
  6359. } else {
  6360. // 'ashr C, x' produces [C >> (Width-1), C]
  6361. Lower = C->ashr(ShiftAmount);
  6362. Upper = *C + 1;
  6363. }
  6364. }
  6365. break;
  6366. case Instruction::LShr:
  6367. if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
  6368. // 'lshr x, C' produces [0, UINT_MAX >> C].
  6369. Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
  6370. } else if (match(BO.getOperand(0), m_APInt(C))) {
  6371. // 'lshr C, x' produces [C >> (Width-1), C].
  6372. unsigned ShiftAmount = Width - 1;
  6373. if (!C->isZero() && IIQ.isExact(&BO))
  6374. ShiftAmount = C->countTrailingZeros();
  6375. Lower = C->lshr(ShiftAmount);
  6376. Upper = *C + 1;
  6377. }
  6378. break;
  6379. case Instruction::Shl:
  6380. if (match(BO.getOperand(0), m_APInt(C))) {
  6381. if (IIQ.hasNoUnsignedWrap(&BO)) {
  6382. // 'shl nuw C, x' produces [C, C << CLZ(C)]
  6383. Lower = *C;
  6384. Upper = Lower.shl(Lower.countLeadingZeros()) + 1;
  6385. } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
  6386. if (C->isNegative()) {
  6387. // 'shl nsw C, x' produces [C << CLO(C)-1, C]
  6388. unsigned ShiftAmount = C->countLeadingOnes() - 1;
  6389. Lower = C->shl(ShiftAmount);
  6390. Upper = *C + 1;
  6391. } else {
  6392. // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
  6393. unsigned ShiftAmount = C->countLeadingZeros() - 1;
  6394. Lower = *C;
  6395. Upper = C->shl(ShiftAmount) + 1;
  6396. }
  6397. }
  6398. }
  6399. break;
  6400. case Instruction::SDiv:
  6401. if (match(BO.getOperand(1), m_APInt(C))) {
  6402. APInt IntMin = APInt::getSignedMinValue(Width);
  6403. APInt IntMax = APInt::getSignedMaxValue(Width);
  6404. if (C->isAllOnes()) {
  6405. // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
  6406. // where C != -1 and C != 0 and C != 1
  6407. Lower = IntMin + 1;
  6408. Upper = IntMax + 1;
  6409. } else if (C->countLeadingZeros() < Width - 1) {
  6410. // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
  6411. // where C != -1 and C != 0 and C != 1
  6412. Lower = IntMin.sdiv(*C);
  6413. Upper = IntMax.sdiv(*C);
  6414. if (Lower.sgt(Upper))
  6415. std::swap(Lower, Upper);
  6416. Upper = Upper + 1;
  6417. assert(Upper != Lower && "Upper part of range has wrapped!");
  6418. }
  6419. } else if (match(BO.getOperand(0), m_APInt(C))) {
  6420. if (C->isMinSignedValue()) {
  6421. // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
  6422. Lower = *C;
  6423. Upper = Lower.lshr(1) + 1;
  6424. } else {
  6425. // 'sdiv C, x' produces [-|C|, |C|].
  6426. Upper = C->abs() + 1;
  6427. Lower = (-Upper) + 1;
  6428. }
  6429. }
  6430. break;
  6431. case Instruction::UDiv:
  6432. if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
  6433. // 'udiv x, C' produces [0, UINT_MAX / C].
  6434. Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
  6435. } else if (match(BO.getOperand(0), m_APInt(C))) {
  6436. // 'udiv C, x' produces [0, C].
  6437. Upper = *C + 1;
  6438. }
  6439. break;
  6440. case Instruction::SRem:
  6441. if (match(BO.getOperand(1), m_APInt(C))) {
  6442. // 'srem x, C' produces (-|C|, |C|).
  6443. Upper = C->abs();
  6444. Lower = (-Upper) + 1;
  6445. }
  6446. break;
  6447. case Instruction::URem:
  6448. if (match(BO.getOperand(1), m_APInt(C)))
  6449. // 'urem x, C' produces [0, C).
  6450. Upper = *C;
  6451. break;
  6452. default:
  6453. break;
  6454. }
  6455. }
  6456. static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
  6457. APInt &Upper) {
  6458. unsigned Width = Lower.getBitWidth();
  6459. const APInt *C;
  6460. switch (II.getIntrinsicID()) {
  6461. case Intrinsic::ctpop:
  6462. case Intrinsic::ctlz:
  6463. case Intrinsic::cttz:
  6464. // Maximum of set/clear bits is the bit width.
  6465. assert(Lower == 0 && "Expected lower bound to be zero");
  6466. Upper = Width + 1;
  6467. break;
  6468. case Intrinsic::uadd_sat:
  6469. // uadd.sat(x, C) produces [C, UINT_MAX].
  6470. if (match(II.getOperand(0), m_APInt(C)) ||
  6471. match(II.getOperand(1), m_APInt(C)))
  6472. Lower = *C;
  6473. break;
  6474. case Intrinsic::sadd_sat:
  6475. if (match(II.getOperand(0), m_APInt(C)) ||
  6476. match(II.getOperand(1), m_APInt(C))) {
  6477. if (C->isNegative()) {
  6478. // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
  6479. Lower = APInt::getSignedMinValue(Width);
  6480. Upper = APInt::getSignedMaxValue(Width) + *C + 1;
  6481. } else {
  6482. // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
  6483. Lower = APInt::getSignedMinValue(Width) + *C;
  6484. Upper = APInt::getSignedMaxValue(Width) + 1;
  6485. }
  6486. }
  6487. break;
  6488. case Intrinsic::usub_sat:
  6489. // usub.sat(C, x) produces [0, C].
  6490. if (match(II.getOperand(0), m_APInt(C)))
  6491. Upper = *C + 1;
  6492. // usub.sat(x, C) produces [0, UINT_MAX - C].
  6493. else if (match(II.getOperand(1), m_APInt(C)))
  6494. Upper = APInt::getMaxValue(Width) - *C + 1;
  6495. break;
  6496. case Intrinsic::ssub_sat:
  6497. if (match(II.getOperand(0), m_APInt(C))) {
  6498. if (C->isNegative()) {
  6499. // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
  6500. Lower = APInt::getSignedMinValue(Width);
  6501. Upper = *C - APInt::getSignedMinValue(Width) + 1;
  6502. } else {
  6503. // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
  6504. Lower = *C - APInt::getSignedMaxValue(Width);
  6505. Upper = APInt::getSignedMaxValue(Width) + 1;
  6506. }
  6507. } else if (match(II.getOperand(1), m_APInt(C))) {
  6508. if (C->isNegative()) {
  6509. // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
  6510. Lower = APInt::getSignedMinValue(Width) - *C;
  6511. Upper = APInt::getSignedMaxValue(Width) + 1;
  6512. } else {
  6513. // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
  6514. Lower = APInt::getSignedMinValue(Width);
  6515. Upper = APInt::getSignedMaxValue(Width) - *C + 1;
  6516. }
  6517. }
  6518. break;
  6519. case Intrinsic::umin:
  6520. case Intrinsic::umax:
  6521. case Intrinsic::smin:
  6522. case Intrinsic::smax:
  6523. if (!match(II.getOperand(0), m_APInt(C)) &&
  6524. !match(II.getOperand(1), m_APInt(C)))
  6525. break;
  6526. switch (II.getIntrinsicID()) {
  6527. case Intrinsic::umin:
  6528. Upper = *C + 1;
  6529. break;
  6530. case Intrinsic::umax:
  6531. Lower = *C;
  6532. break;
  6533. case Intrinsic::smin:
  6534. Lower = APInt::getSignedMinValue(Width);
  6535. Upper = *C + 1;
  6536. break;
  6537. case Intrinsic::smax:
  6538. Lower = *C;
  6539. Upper = APInt::getSignedMaxValue(Width) + 1;
  6540. break;
  6541. default:
  6542. llvm_unreachable("Must be min/max intrinsic");
  6543. }
  6544. break;
  6545. case Intrinsic::abs:
  6546. // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
  6547. // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
  6548. if (match(II.getOperand(1), m_One()))
  6549. Upper = APInt::getSignedMaxValue(Width) + 1;
  6550. else
  6551. Upper = APInt::getSignedMinValue(Width) + 1;
  6552. break;
  6553. default:
  6554. break;
  6555. }
  6556. }
  6557. static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
  6558. APInt &Upper, const InstrInfoQuery &IIQ) {
  6559. const Value *LHS = nullptr, *RHS = nullptr;
  6560. SelectPatternResult R = matchSelectPattern(&SI, LHS, RHS);
  6561. if (R.Flavor == SPF_UNKNOWN)
  6562. return;
  6563. unsigned BitWidth = SI.getType()->getScalarSizeInBits();
  6564. if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
  6565. // If the negation part of the abs (in RHS) has the NSW flag,
  6566. // then the result of abs(X) is [0..SIGNED_MAX],
  6567. // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
  6568. Lower = APInt::getZero(BitWidth);
  6569. if (match(RHS, m_Neg(m_Specific(LHS))) &&
  6570. IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
  6571. Upper = APInt::getSignedMaxValue(BitWidth) + 1;
  6572. else
  6573. Upper = APInt::getSignedMinValue(BitWidth) + 1;
  6574. return;
  6575. }
  6576. if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
  6577. // The result of -abs(X) is <= 0.
  6578. Lower = APInt::getSignedMinValue(BitWidth);
  6579. Upper = APInt(BitWidth, 1);
  6580. return;
  6581. }
  6582. const APInt *C;
  6583. if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
  6584. return;
  6585. switch (R.Flavor) {
  6586. case SPF_UMIN:
  6587. Upper = *C + 1;
  6588. break;
  6589. case SPF_UMAX:
  6590. Lower = *C;
  6591. break;
  6592. case SPF_SMIN:
  6593. Lower = APInt::getSignedMinValue(BitWidth);
  6594. Upper = *C + 1;
  6595. break;
  6596. case SPF_SMAX:
  6597. Lower = *C;
  6598. Upper = APInt::getSignedMaxValue(BitWidth) + 1;
  6599. break;
  6600. default:
  6601. break;
  6602. }
  6603. }
  6604. static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper) {
  6605. // The maximum representable value of a half is 65504. For floats the maximum
  6606. // value is 3.4e38 which requires roughly 129 bits.
  6607. unsigned BitWidth = I->getType()->getScalarSizeInBits();
  6608. if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
  6609. return;
  6610. if (isa<FPToSIInst>(I) && BitWidth >= 17) {
  6611. Lower = APInt(BitWidth, -65504);
  6612. Upper = APInt(BitWidth, 65505);
  6613. }
  6614. if (isa<FPToUIInst>(I) && BitWidth >= 16) {
  6615. // For a fptoui the lower limit is left as 0.
  6616. Upper = APInt(BitWidth, 65505);
  6617. }
  6618. }
  6619. ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
  6620. bool UseInstrInfo, AssumptionCache *AC,
  6621. const Instruction *CtxI,
  6622. const DominatorTree *DT,
  6623. unsigned Depth) {
  6624. assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
  6625. if (Depth == MaxAnalysisRecursionDepth)
  6626. return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
  6627. const APInt *C;
  6628. if (match(V, m_APInt(C)))
  6629. return ConstantRange(*C);
  6630. InstrInfoQuery IIQ(UseInstrInfo);
  6631. unsigned BitWidth = V->getType()->getScalarSizeInBits();
  6632. APInt Lower = APInt(BitWidth, 0);
  6633. APInt Upper = APInt(BitWidth, 0);
  6634. if (auto *BO = dyn_cast<BinaryOperator>(V))
  6635. setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
  6636. else if (auto *II = dyn_cast<IntrinsicInst>(V))
  6637. setLimitsForIntrinsic(*II, Lower, Upper);
  6638. else if (auto *SI = dyn_cast<SelectInst>(V))
  6639. setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
  6640. else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V))
  6641. setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
  6642. ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper);
  6643. if (auto *I = dyn_cast<Instruction>(V))
  6644. if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
  6645. CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));
  6646. if (CtxI && AC) {
  6647. // Try to restrict the range based on information from assumptions.
  6648. for (auto &AssumeVH : AC->assumptionsFor(V)) {
  6649. if (!AssumeVH)
  6650. continue;
  6651. IntrinsicInst *I = cast<IntrinsicInst>(AssumeVH);
  6652. assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
  6653. "Got assumption for the wrong function!");
  6654. if (!isValidAssumeForContext(I, CtxI, DT))
  6655. continue;
  6656. Value *Arg = I->getArgOperand(0);
  6657. ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
  6658. // Currently we just use information from comparisons.
  6659. if (!Cmp || Cmp->getOperand(0) != V)
  6660. continue;
  6661. // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
  6662. ConstantRange RHS =
  6663. computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
  6664. UseInstrInfo, AC, I, DT, Depth + 1);
  6665. CR = CR.intersectWith(
  6666. ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
  6667. }
  6668. }
  6669. return CR;
  6670. }
  6671. static std::optional<int64_t>
  6672. getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {
  6673. // Skip over the first indices.
  6674. gep_type_iterator GTI = gep_type_begin(GEP);
  6675. for (unsigned i = 1; i != Idx; ++i, ++GTI)
  6676. /*skip along*/;
  6677. // Compute the offset implied by the rest of the indices.
  6678. int64_t Offset = 0;
  6679. for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
  6680. ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));
  6681. if (!OpC)
  6682. return std::nullopt;
  6683. if (OpC->isZero())
  6684. continue; // No offset.
  6685. // Handle struct indices, which add their field offset to the pointer.
  6686. if (StructType *STy = GTI.getStructTypeOrNull()) {
  6687. Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());
  6688. continue;
  6689. }
  6690. // Otherwise, we have a sequential type like an array or fixed-length
  6691. // vector. Multiply the index by the ElementSize.
  6692. TypeSize Size = DL.getTypeAllocSize(GTI.getIndexedType());
  6693. if (Size.isScalable())
  6694. return std::nullopt;
  6695. Offset += Size.getFixedValue() * OpC->getSExtValue();
  6696. }
  6697. return Offset;
  6698. }
  6699. std::optional<int64_t> llvm::isPointerOffset(const Value *Ptr1,
  6700. const Value *Ptr2,
  6701. const DataLayout &DL) {
  6702. APInt Offset1(DL.getIndexTypeSizeInBits(Ptr1->getType()), 0);
  6703. APInt Offset2(DL.getIndexTypeSizeInBits(Ptr2->getType()), 0);
  6704. Ptr1 = Ptr1->stripAndAccumulateConstantOffsets(DL, Offset1, true);
  6705. Ptr2 = Ptr2->stripAndAccumulateConstantOffsets(DL, Offset2, true);
  6706. // Handle the trivial case first.
  6707. if (Ptr1 == Ptr2)
  6708. return Offset2.getSExtValue() - Offset1.getSExtValue();
  6709. const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);
  6710. const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);
  6711. // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical
  6712. // base. After that base, they may have some number of common (and
  6713. // potentially variable) indices. After that they handle some constant
  6714. // offset, which determines their offset from each other. At this point, we
  6715. // handle no other case.
  6716. if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||
  6717. GEP1->getSourceElementType() != GEP2->getSourceElementType())
  6718. return std::nullopt;
  6719. // Skip any common indices and track the GEP types.
  6720. unsigned Idx = 1;
  6721. for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)
  6722. if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))
  6723. break;
  6724. auto IOffset1 = getOffsetFromIndex(GEP1, Idx, DL);
  6725. auto IOffset2 = getOffsetFromIndex(GEP2, Idx, DL);
  6726. if (!IOffset1 || !IOffset2)
  6727. return std::nullopt;
  6728. return *IOffset2 - *IOffset1 + Offset2.getSExtValue() -
  6729. Offset1.getSExtValue();
  6730. }