ValueTracking.cpp 264 KB

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