InstructionSimplify.cpp 241 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478
  1. //===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
  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 implements routines for folding instructions into simpler forms
  10. // that do not require creating new instructions. This does constant folding
  11. // ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
  12. // returning a constant ("and i32 %x, 0" -> "0") or an already existing value
  13. // ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been
  14. // simplified: This is usually true and assuming it simplifies the logic (if
  15. // they have not been simplified then results are correct but maybe suboptimal).
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "llvm/Analysis/InstructionSimplify.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/SetVector.h"
  21. #include "llvm/ADT/SmallPtrSet.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include "llvm/Analysis/AliasAnalysis.h"
  24. #include "llvm/Analysis/AssumptionCache.h"
  25. #include "llvm/Analysis/CaptureTracking.h"
  26. #include "llvm/Analysis/CmpInstAnalysis.h"
  27. #include "llvm/Analysis/ConstantFolding.h"
  28. #include "llvm/Analysis/InstSimplifyFolder.h"
  29. #include "llvm/Analysis/LoopAnalysisManager.h"
  30. #include "llvm/Analysis/MemoryBuiltins.h"
  31. #include "llvm/Analysis/OverflowInstAnalysis.h"
  32. #include "llvm/Analysis/ValueTracking.h"
  33. #include "llvm/Analysis/VectorUtils.h"
  34. #include "llvm/IR/ConstantRange.h"
  35. #include "llvm/IR/DataLayout.h"
  36. #include "llvm/IR/Dominators.h"
  37. #include "llvm/IR/GetElementPtrTypeIterator.h"
  38. #include "llvm/IR/GlobalAlias.h"
  39. #include "llvm/IR/InstrTypes.h"
  40. #include "llvm/IR/Instructions.h"
  41. #include "llvm/IR/Operator.h"
  42. #include "llvm/IR/PatternMatch.h"
  43. #include "llvm/IR/ValueHandle.h"
  44. #include "llvm/Support/KnownBits.h"
  45. #include <algorithm>
  46. using namespace llvm;
  47. using namespace llvm::PatternMatch;
  48. #define DEBUG_TYPE "instsimplify"
  49. enum { RecursionLimit = 3 };
  50. STATISTIC(NumExpand, "Number of expansions");
  51. STATISTIC(NumReassoc, "Number of reassociations");
  52. static Value *SimplifyAndInst(Value *, Value *, const SimplifyQuery &, unsigned);
  53. static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned);
  54. static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &,
  55. const SimplifyQuery &, unsigned);
  56. static Value *SimplifyBinOp(unsigned, Value *, Value *, const SimplifyQuery &,
  57. unsigned);
  58. static Value *SimplifyBinOp(unsigned, Value *, Value *, const FastMathFlags &,
  59. const SimplifyQuery &, unsigned);
  60. static Value *SimplifyCmpInst(unsigned, Value *, Value *, const SimplifyQuery &,
  61. unsigned);
  62. static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  63. const SimplifyQuery &Q, unsigned MaxRecurse);
  64. static Value *SimplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned);
  65. static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned);
  66. static Value *SimplifyCastInst(unsigned, Value *, Type *,
  67. const SimplifyQuery &, unsigned);
  68. static Value *SimplifyGEPInst(Type *, Value *, ArrayRef<Value *>, bool,
  69. const SimplifyQuery &, unsigned);
  70. static Value *SimplifySelectInst(Value *, Value *, Value *,
  71. const SimplifyQuery &, unsigned);
  72. static Value *foldSelectWithBinaryOp(Value *Cond, Value *TrueVal,
  73. Value *FalseVal) {
  74. BinaryOperator::BinaryOps BinOpCode;
  75. if (auto *BO = dyn_cast<BinaryOperator>(Cond))
  76. BinOpCode = BO->getOpcode();
  77. else
  78. return nullptr;
  79. CmpInst::Predicate ExpectedPred, Pred1, Pred2;
  80. if (BinOpCode == BinaryOperator::Or) {
  81. ExpectedPred = ICmpInst::ICMP_NE;
  82. } else if (BinOpCode == BinaryOperator::And) {
  83. ExpectedPred = ICmpInst::ICMP_EQ;
  84. } else
  85. return nullptr;
  86. // %A = icmp eq %TV, %FV
  87. // %B = icmp eq %X, %Y (and one of these is a select operand)
  88. // %C = and %A, %B
  89. // %D = select %C, %TV, %FV
  90. // -->
  91. // %FV
  92. // %A = icmp ne %TV, %FV
  93. // %B = icmp ne %X, %Y (and one of these is a select operand)
  94. // %C = or %A, %B
  95. // %D = select %C, %TV, %FV
  96. // -->
  97. // %TV
  98. Value *X, *Y;
  99. if (!match(Cond, m_c_BinOp(m_c_ICmp(Pred1, m_Specific(TrueVal),
  100. m_Specific(FalseVal)),
  101. m_ICmp(Pred2, m_Value(X), m_Value(Y)))) ||
  102. Pred1 != Pred2 || Pred1 != ExpectedPred)
  103. return nullptr;
  104. if (X == TrueVal || X == FalseVal || Y == TrueVal || Y == FalseVal)
  105. return BinOpCode == BinaryOperator::Or ? TrueVal : FalseVal;
  106. return nullptr;
  107. }
  108. /// For a boolean type or a vector of boolean type, return false or a vector
  109. /// with every element false.
  110. static Constant *getFalse(Type *Ty) {
  111. return ConstantInt::getFalse(Ty);
  112. }
  113. /// For a boolean type or a vector of boolean type, return true or a vector
  114. /// with every element true.
  115. static Constant *getTrue(Type *Ty) {
  116. return ConstantInt::getTrue(Ty);
  117. }
  118. /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
  119. static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
  120. Value *RHS) {
  121. CmpInst *Cmp = dyn_cast<CmpInst>(V);
  122. if (!Cmp)
  123. return false;
  124. CmpInst::Predicate CPred = Cmp->getPredicate();
  125. Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
  126. if (CPred == Pred && CLHS == LHS && CRHS == RHS)
  127. return true;
  128. return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
  129. CRHS == LHS;
  130. }
  131. /// Simplify comparison with true or false branch of select:
  132. /// %sel = select i1 %cond, i32 %tv, i32 %fv
  133. /// %cmp = icmp sle i32 %sel, %rhs
  134. /// Compose new comparison by substituting %sel with either %tv or %fv
  135. /// and see if it simplifies.
  136. static Value *simplifyCmpSelCase(CmpInst::Predicate Pred, Value *LHS,
  137. Value *RHS, Value *Cond,
  138. const SimplifyQuery &Q, unsigned MaxRecurse,
  139. Constant *TrueOrFalse) {
  140. Value *SimplifiedCmp = SimplifyCmpInst(Pred, LHS, RHS, Q, MaxRecurse);
  141. if (SimplifiedCmp == Cond) {
  142. // %cmp simplified to the select condition (%cond).
  143. return TrueOrFalse;
  144. } else if (!SimplifiedCmp && isSameCompare(Cond, Pred, LHS, RHS)) {
  145. // It didn't simplify. However, if composed comparison is equivalent
  146. // to the select condition (%cond) then we can replace it.
  147. return TrueOrFalse;
  148. }
  149. return SimplifiedCmp;
  150. }
  151. /// Simplify comparison with true branch of select
  152. static Value *simplifyCmpSelTrueCase(CmpInst::Predicate Pred, Value *LHS,
  153. Value *RHS, Value *Cond,
  154. const SimplifyQuery &Q,
  155. unsigned MaxRecurse) {
  156. return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
  157. getTrue(Cond->getType()));
  158. }
  159. /// Simplify comparison with false branch of select
  160. static Value *simplifyCmpSelFalseCase(CmpInst::Predicate Pred, Value *LHS,
  161. Value *RHS, Value *Cond,
  162. const SimplifyQuery &Q,
  163. unsigned MaxRecurse) {
  164. return simplifyCmpSelCase(Pred, LHS, RHS, Cond, Q, MaxRecurse,
  165. getFalse(Cond->getType()));
  166. }
  167. /// We know comparison with both branches of select can be simplified, but they
  168. /// are not equal. This routine handles some logical simplifications.
  169. static Value *handleOtherCmpSelSimplifications(Value *TCmp, Value *FCmp,
  170. Value *Cond,
  171. const SimplifyQuery &Q,
  172. unsigned MaxRecurse) {
  173. // If the false value simplified to false, then the result of the compare
  174. // is equal to "Cond && TCmp". This also catches the case when the false
  175. // value simplified to false and the true value to true, returning "Cond".
  176. // Folding select to and/or isn't poison-safe in general; impliesPoison
  177. // checks whether folding it does not convert a well-defined value into
  178. // poison.
  179. if (match(FCmp, m_Zero()) && impliesPoison(TCmp, Cond))
  180. if (Value *V = SimplifyAndInst(Cond, TCmp, Q, MaxRecurse))
  181. return V;
  182. // If the true value simplified to true, then the result of the compare
  183. // is equal to "Cond || FCmp".
  184. if (match(TCmp, m_One()) && impliesPoison(FCmp, Cond))
  185. if (Value *V = SimplifyOrInst(Cond, FCmp, Q, MaxRecurse))
  186. return V;
  187. // Finally, if the false value simplified to true and the true value to
  188. // false, then the result of the compare is equal to "!Cond".
  189. if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
  190. if (Value *V = SimplifyXorInst(
  191. Cond, Constant::getAllOnesValue(Cond->getType()), Q, MaxRecurse))
  192. return V;
  193. return nullptr;
  194. }
  195. /// Does the given value dominate the specified phi node?
  196. static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
  197. Instruction *I = dyn_cast<Instruction>(V);
  198. if (!I)
  199. // Arguments and constants dominate all instructions.
  200. return true;
  201. // If we are processing instructions (and/or basic blocks) that have not been
  202. // fully added to a function, the parent nodes may still be null. Simply
  203. // return the conservative answer in these cases.
  204. if (!I->getParent() || !P->getParent() || !I->getFunction())
  205. return false;
  206. // If we have a DominatorTree then do a precise test.
  207. if (DT)
  208. return DT->dominates(I, P);
  209. // Otherwise, if the instruction is in the entry block and is not an invoke,
  210. // then it obviously dominates all phi nodes.
  211. if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
  212. !isa<CallBrInst>(I))
  213. return true;
  214. return false;
  215. }
  216. /// Try to simplify a binary operator of form "V op OtherOp" where V is
  217. /// "(B0 opex B1)" by distributing 'op' across 'opex' as
  218. /// "(B0 op OtherOp) opex (B1 op OtherOp)".
  219. static Value *expandBinOp(Instruction::BinaryOps Opcode, Value *V,
  220. Value *OtherOp, Instruction::BinaryOps OpcodeToExpand,
  221. const SimplifyQuery &Q, unsigned MaxRecurse) {
  222. auto *B = dyn_cast<BinaryOperator>(V);
  223. if (!B || B->getOpcode() != OpcodeToExpand)
  224. return nullptr;
  225. Value *B0 = B->getOperand(0), *B1 = B->getOperand(1);
  226. Value *L = SimplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(),
  227. MaxRecurse);
  228. if (!L)
  229. return nullptr;
  230. Value *R = SimplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(),
  231. MaxRecurse);
  232. if (!R)
  233. return nullptr;
  234. // Does the expanded pair of binops simplify to the existing binop?
  235. if ((L == B0 && R == B1) ||
  236. (Instruction::isCommutative(OpcodeToExpand) && L == B1 && R == B0)) {
  237. ++NumExpand;
  238. return B;
  239. }
  240. // Otherwise, return "L op' R" if it simplifies.
  241. Value *S = SimplifyBinOp(OpcodeToExpand, L, R, Q, MaxRecurse);
  242. if (!S)
  243. return nullptr;
  244. ++NumExpand;
  245. return S;
  246. }
  247. /// Try to simplify binops of form "A op (B op' C)" or the commuted variant by
  248. /// distributing op over op'.
  249. static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode,
  250. Value *L, Value *R,
  251. Instruction::BinaryOps OpcodeToExpand,
  252. const SimplifyQuery &Q,
  253. unsigned MaxRecurse) {
  254. // Recursion is always used, so bail out at once if we already hit the limit.
  255. if (!MaxRecurse--)
  256. return nullptr;
  257. if (Value *V = expandBinOp(Opcode, L, R, OpcodeToExpand, Q, MaxRecurse))
  258. return V;
  259. if (Value *V = expandBinOp(Opcode, R, L, OpcodeToExpand, Q, MaxRecurse))
  260. return V;
  261. return nullptr;
  262. }
  263. /// Generic simplifications for associative binary operations.
  264. /// Returns the simpler value, or null if none was found.
  265. static Value *SimplifyAssociativeBinOp(Instruction::BinaryOps Opcode,
  266. Value *LHS, Value *RHS,
  267. const SimplifyQuery &Q,
  268. unsigned MaxRecurse) {
  269. assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
  270. // Recursion is always used, so bail out at once if we already hit the limit.
  271. if (!MaxRecurse--)
  272. return nullptr;
  273. BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
  274. BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
  275. // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
  276. if (Op0 && Op0->getOpcode() == Opcode) {
  277. Value *A = Op0->getOperand(0);
  278. Value *B = Op0->getOperand(1);
  279. Value *C = RHS;
  280. // Does "B op C" simplify?
  281. if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) {
  282. // It does! Return "A op V" if it simplifies or is already available.
  283. // If V equals B then "A op V" is just the LHS.
  284. if (V == B) return LHS;
  285. // Otherwise return "A op V" if it simplifies.
  286. if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) {
  287. ++NumReassoc;
  288. return W;
  289. }
  290. }
  291. }
  292. // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
  293. if (Op1 && Op1->getOpcode() == Opcode) {
  294. Value *A = LHS;
  295. Value *B = Op1->getOperand(0);
  296. Value *C = Op1->getOperand(1);
  297. // Does "A op B" simplify?
  298. if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) {
  299. // It does! Return "V op C" if it simplifies or is already available.
  300. // If V equals B then "V op C" is just the RHS.
  301. if (V == B) return RHS;
  302. // Otherwise return "V op C" if it simplifies.
  303. if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) {
  304. ++NumReassoc;
  305. return W;
  306. }
  307. }
  308. }
  309. // The remaining transforms require commutativity as well as associativity.
  310. if (!Instruction::isCommutative(Opcode))
  311. return nullptr;
  312. // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
  313. if (Op0 && Op0->getOpcode() == Opcode) {
  314. Value *A = Op0->getOperand(0);
  315. Value *B = Op0->getOperand(1);
  316. Value *C = RHS;
  317. // Does "C op A" simplify?
  318. if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
  319. // It does! Return "V op B" if it simplifies or is already available.
  320. // If V equals A then "V op B" is just the LHS.
  321. if (V == A) return LHS;
  322. // Otherwise return "V op B" if it simplifies.
  323. if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) {
  324. ++NumReassoc;
  325. return W;
  326. }
  327. }
  328. }
  329. // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
  330. if (Op1 && Op1->getOpcode() == Opcode) {
  331. Value *A = LHS;
  332. Value *B = Op1->getOperand(0);
  333. Value *C = Op1->getOperand(1);
  334. // Does "C op A" simplify?
  335. if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) {
  336. // It does! Return "B op V" if it simplifies or is already available.
  337. // If V equals C then "B op V" is just the RHS.
  338. if (V == C) return RHS;
  339. // Otherwise return "B op V" if it simplifies.
  340. if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) {
  341. ++NumReassoc;
  342. return W;
  343. }
  344. }
  345. }
  346. return nullptr;
  347. }
  348. /// In the case of a binary operation with a select instruction as an operand,
  349. /// try to simplify the binop by seeing whether evaluating it on both branches
  350. /// of the select results in the same value. Returns the common value if so,
  351. /// otherwise returns null.
  352. static Value *ThreadBinOpOverSelect(Instruction::BinaryOps Opcode, Value *LHS,
  353. Value *RHS, const SimplifyQuery &Q,
  354. unsigned MaxRecurse) {
  355. // Recursion is always used, so bail out at once if we already hit the limit.
  356. if (!MaxRecurse--)
  357. return nullptr;
  358. SelectInst *SI;
  359. if (isa<SelectInst>(LHS)) {
  360. SI = cast<SelectInst>(LHS);
  361. } else {
  362. assert(isa<SelectInst>(RHS) && "No select instruction operand!");
  363. SI = cast<SelectInst>(RHS);
  364. }
  365. // Evaluate the BinOp on the true and false branches of the select.
  366. Value *TV;
  367. Value *FV;
  368. if (SI == LHS) {
  369. TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, Q, MaxRecurse);
  370. FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, Q, MaxRecurse);
  371. } else {
  372. TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), Q, MaxRecurse);
  373. FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), Q, MaxRecurse);
  374. }
  375. // If they simplified to the same value, then return the common value.
  376. // If they both failed to simplify then return null.
  377. if (TV == FV)
  378. return TV;
  379. // If one branch simplified to undef, return the other one.
  380. if (TV && Q.isUndefValue(TV))
  381. return FV;
  382. if (FV && Q.isUndefValue(FV))
  383. return TV;
  384. // If applying the operation did not change the true and false select values,
  385. // then the result of the binop is the select itself.
  386. if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
  387. return SI;
  388. // If one branch simplified and the other did not, and the simplified
  389. // value is equal to the unsimplified one, return the simplified value.
  390. // For example, select (cond, X, X & Z) & Z -> X & Z.
  391. if ((FV && !TV) || (TV && !FV)) {
  392. // Check that the simplified value has the form "X op Y" where "op" is the
  393. // same as the original operation.
  394. Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
  395. if (Simplified && Simplified->getOpcode() == unsigned(Opcode)) {
  396. // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
  397. // We already know that "op" is the same as for the simplified value. See
  398. // if the operands match too. If so, return the simplified value.
  399. Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
  400. Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
  401. Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
  402. if (Simplified->getOperand(0) == UnsimplifiedLHS &&
  403. Simplified->getOperand(1) == UnsimplifiedRHS)
  404. return Simplified;
  405. if (Simplified->isCommutative() &&
  406. Simplified->getOperand(1) == UnsimplifiedLHS &&
  407. Simplified->getOperand(0) == UnsimplifiedRHS)
  408. return Simplified;
  409. }
  410. }
  411. return nullptr;
  412. }
  413. /// In the case of a comparison with a select instruction, try to simplify the
  414. /// comparison by seeing whether both branches of the select result in the same
  415. /// value. Returns the common value if so, otherwise returns null.
  416. /// For example, if we have:
  417. /// %tmp = select i1 %cmp, i32 1, i32 2
  418. /// %cmp1 = icmp sle i32 %tmp, 3
  419. /// We can simplify %cmp1 to true, because both branches of select are
  420. /// less than 3. We compose new comparison by substituting %tmp with both
  421. /// branches of select and see if it can be simplified.
  422. static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
  423. Value *RHS, const SimplifyQuery &Q,
  424. unsigned MaxRecurse) {
  425. // Recursion is always used, so bail out at once if we already hit the limit.
  426. if (!MaxRecurse--)
  427. return nullptr;
  428. // Make sure the select is on the LHS.
  429. if (!isa<SelectInst>(LHS)) {
  430. std::swap(LHS, RHS);
  431. Pred = CmpInst::getSwappedPredicate(Pred);
  432. }
  433. assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
  434. SelectInst *SI = cast<SelectInst>(LHS);
  435. Value *Cond = SI->getCondition();
  436. Value *TV = SI->getTrueValue();
  437. Value *FV = SI->getFalseValue();
  438. // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
  439. // Does "cmp TV, RHS" simplify?
  440. Value *TCmp = simplifyCmpSelTrueCase(Pred, TV, RHS, Cond, Q, MaxRecurse);
  441. if (!TCmp)
  442. return nullptr;
  443. // Does "cmp FV, RHS" simplify?
  444. Value *FCmp = simplifyCmpSelFalseCase(Pred, FV, RHS, Cond, Q, MaxRecurse);
  445. if (!FCmp)
  446. return nullptr;
  447. // If both sides simplified to the same value, then use it as the result of
  448. // the original comparison.
  449. if (TCmp == FCmp)
  450. return TCmp;
  451. // The remaining cases only make sense if the select condition has the same
  452. // type as the result of the comparison, so bail out if this is not so.
  453. if (Cond->getType()->isVectorTy() == RHS->getType()->isVectorTy())
  454. return handleOtherCmpSelSimplifications(TCmp, FCmp, Cond, Q, MaxRecurse);
  455. return nullptr;
  456. }
  457. /// In the case of a binary operation with an operand that is a PHI instruction,
  458. /// try to simplify the binop by seeing whether evaluating it on the incoming
  459. /// phi values yields the same result for every value. If so returns the common
  460. /// value, otherwise returns null.
  461. static Value *ThreadBinOpOverPHI(Instruction::BinaryOps Opcode, Value *LHS,
  462. Value *RHS, const SimplifyQuery &Q,
  463. unsigned MaxRecurse) {
  464. // Recursion is always used, so bail out at once if we already hit the limit.
  465. if (!MaxRecurse--)
  466. return nullptr;
  467. PHINode *PI;
  468. if (isa<PHINode>(LHS)) {
  469. PI = cast<PHINode>(LHS);
  470. // Bail out if RHS and the phi may be mutually interdependent due to a loop.
  471. if (!valueDominatesPHI(RHS, PI, Q.DT))
  472. return nullptr;
  473. } else {
  474. assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
  475. PI = cast<PHINode>(RHS);
  476. // Bail out if LHS and the phi may be mutually interdependent due to a loop.
  477. if (!valueDominatesPHI(LHS, PI, Q.DT))
  478. return nullptr;
  479. }
  480. // Evaluate the BinOp on the incoming phi values.
  481. Value *CommonValue = nullptr;
  482. for (Value *Incoming : PI->incoming_values()) {
  483. // If the incoming value is the phi node itself, it can safely be skipped.
  484. if (Incoming == PI) continue;
  485. Value *V = PI == LHS ?
  486. SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) :
  487. SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse);
  488. // If the operation failed to simplify, or simplified to a different value
  489. // to previously, then give up.
  490. if (!V || (CommonValue && V != CommonValue))
  491. return nullptr;
  492. CommonValue = V;
  493. }
  494. return CommonValue;
  495. }
  496. /// In the case of a comparison with a PHI instruction, try to simplify the
  497. /// comparison by seeing whether comparing with all of the incoming phi values
  498. /// yields the same result every time. If so returns the common result,
  499. /// otherwise returns null.
  500. static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
  501. const SimplifyQuery &Q, unsigned MaxRecurse) {
  502. // Recursion is always used, so bail out at once if we already hit the limit.
  503. if (!MaxRecurse--)
  504. return nullptr;
  505. // Make sure the phi is on the LHS.
  506. if (!isa<PHINode>(LHS)) {
  507. std::swap(LHS, RHS);
  508. Pred = CmpInst::getSwappedPredicate(Pred);
  509. }
  510. assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
  511. PHINode *PI = cast<PHINode>(LHS);
  512. // Bail out if RHS and the phi may be mutually interdependent due to a loop.
  513. if (!valueDominatesPHI(RHS, PI, Q.DT))
  514. return nullptr;
  515. // Evaluate the BinOp on the incoming phi values.
  516. Value *CommonValue = nullptr;
  517. for (unsigned u = 0, e = PI->getNumIncomingValues(); u < e; ++u) {
  518. Value *Incoming = PI->getIncomingValue(u);
  519. Instruction *InTI = PI->getIncomingBlock(u)->getTerminator();
  520. // If the incoming value is the phi node itself, it can safely be skipped.
  521. if (Incoming == PI) continue;
  522. // Change the context instruction to the "edge" that flows into the phi.
  523. // This is important because that is where incoming is actually "evaluated"
  524. // even though it is used later somewhere else.
  525. Value *V = SimplifyCmpInst(Pred, Incoming, RHS, Q.getWithInstruction(InTI),
  526. MaxRecurse);
  527. // If the operation failed to simplify, or simplified to a different value
  528. // to previously, then give up.
  529. if (!V || (CommonValue && V != CommonValue))
  530. return nullptr;
  531. CommonValue = V;
  532. }
  533. return CommonValue;
  534. }
  535. static Constant *foldOrCommuteConstant(Instruction::BinaryOps Opcode,
  536. Value *&Op0, Value *&Op1,
  537. const SimplifyQuery &Q) {
  538. if (auto *CLHS = dyn_cast<Constant>(Op0)) {
  539. if (auto *CRHS = dyn_cast<Constant>(Op1))
  540. return ConstantFoldBinaryOpOperands(Opcode, CLHS, CRHS, Q.DL);
  541. // Canonicalize the constant to the RHS if this is a commutative operation.
  542. if (Instruction::isCommutative(Opcode))
  543. std::swap(Op0, Op1);
  544. }
  545. return nullptr;
  546. }
  547. /// Given operands for an Add, see if we can fold the result.
  548. /// If not, this returns null.
  549. static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
  550. const SimplifyQuery &Q, unsigned MaxRecurse) {
  551. if (Constant *C = foldOrCommuteConstant(Instruction::Add, Op0, Op1, Q))
  552. return C;
  553. // X + poison -> poison
  554. if (isa<PoisonValue>(Op1))
  555. return Op1;
  556. // X + undef -> undef
  557. if (Q.isUndefValue(Op1))
  558. return Op1;
  559. // X + 0 -> X
  560. if (match(Op1, m_Zero()))
  561. return Op0;
  562. // If two operands are negative, return 0.
  563. if (isKnownNegation(Op0, Op1))
  564. return Constant::getNullValue(Op0->getType());
  565. // X + (Y - X) -> Y
  566. // (Y - X) + X -> Y
  567. // Eg: X + -X -> 0
  568. Value *Y = nullptr;
  569. if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
  570. match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
  571. return Y;
  572. // X + ~X -> -1 since ~X = -X-1
  573. Type *Ty = Op0->getType();
  574. if (match(Op0, m_Not(m_Specific(Op1))) ||
  575. match(Op1, m_Not(m_Specific(Op0))))
  576. return Constant::getAllOnesValue(Ty);
  577. // add nsw/nuw (xor Y, signmask), signmask --> Y
  578. // The no-wrapping add guarantees that the top bit will be set by the add.
  579. // Therefore, the xor must be clearing the already set sign bit of Y.
  580. if ((IsNSW || IsNUW) && match(Op1, m_SignMask()) &&
  581. match(Op0, m_Xor(m_Value(Y), m_SignMask())))
  582. return Y;
  583. // add nuw %x, -1 -> -1, because %x can only be 0.
  584. if (IsNUW && match(Op1, m_AllOnes()))
  585. return Op1; // Which is -1.
  586. /// i1 add -> xor.
  587. if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
  588. if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
  589. return V;
  590. // Try some generic simplifications for associative operations.
  591. if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q,
  592. MaxRecurse))
  593. return V;
  594. // Threading Add over selects and phi nodes is pointless, so don't bother.
  595. // Threading over the select in "A + select(cond, B, C)" means evaluating
  596. // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
  597. // only if B and C are equal. If B and C are equal then (since we assume
  598. // that operands have already been simplified) "select(cond, B, C)" should
  599. // have been simplified to the common value of B and C already. Analysing
  600. // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
  601. // for threading over phi nodes.
  602. return nullptr;
  603. }
  604. Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
  605. const SimplifyQuery &Query) {
  606. return ::SimplifyAddInst(Op0, Op1, IsNSW, IsNUW, Query, RecursionLimit);
  607. }
  608. /// Compute the base pointer and cumulative constant offsets for V.
  609. ///
  610. /// This strips all constant offsets off of V, leaving it the base pointer, and
  611. /// accumulates the total constant offset applied in the returned constant. It
  612. /// returns 0 if V is not a pointer, and returns the constant '0' if there are
  613. /// no constant offsets applied.
  614. ///
  615. /// This is very similar to GetPointerBaseWithConstantOffset except it doesn't
  616. /// follow non-inbounds geps. This allows it to remain usable for icmp ult/etc.
  617. /// folding.
  618. static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
  619. bool AllowNonInbounds = false) {
  620. assert(V->getType()->isPtrOrPtrVectorTy());
  621. APInt Offset = APInt::getZero(DL.getIndexTypeSizeInBits(V->getType()));
  622. V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
  623. // As that strip may trace through `addrspacecast`, need to sext or trunc
  624. // the offset calculated.
  625. Type *IntIdxTy = DL.getIndexType(V->getType())->getScalarType();
  626. Offset = Offset.sextOrTrunc(IntIdxTy->getIntegerBitWidth());
  627. Constant *OffsetIntPtr = ConstantInt::get(IntIdxTy, Offset);
  628. if (VectorType *VecTy = dyn_cast<VectorType>(V->getType()))
  629. return ConstantVector::getSplat(VecTy->getElementCount(), OffsetIntPtr);
  630. return OffsetIntPtr;
  631. }
  632. /// Compute the constant difference between two pointer values.
  633. /// If the difference is not a constant, returns zero.
  634. static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
  635. Value *RHS) {
  636. Constant *LHSOffset = stripAndComputeConstantOffsets(DL, LHS);
  637. Constant *RHSOffset = stripAndComputeConstantOffsets(DL, RHS);
  638. // If LHS and RHS are not related via constant offsets to the same base
  639. // value, there is nothing we can do here.
  640. if (LHS != RHS)
  641. return nullptr;
  642. // Otherwise, the difference of LHS - RHS can be computed as:
  643. // LHS - RHS
  644. // = (LHSOffset + Base) - (RHSOffset + Base)
  645. // = LHSOffset - RHSOffset
  646. return ConstantExpr::getSub(LHSOffset, RHSOffset);
  647. }
  648. /// Given operands for a Sub, see if we can fold the result.
  649. /// If not, this returns null.
  650. static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  651. const SimplifyQuery &Q, unsigned MaxRecurse) {
  652. if (Constant *C = foldOrCommuteConstant(Instruction::Sub, Op0, Op1, Q))
  653. return C;
  654. // X - poison -> poison
  655. // poison - X -> poison
  656. if (isa<PoisonValue>(Op0) || isa<PoisonValue>(Op1))
  657. return PoisonValue::get(Op0->getType());
  658. // X - undef -> undef
  659. // undef - X -> undef
  660. if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
  661. return UndefValue::get(Op0->getType());
  662. // X - 0 -> X
  663. if (match(Op1, m_Zero()))
  664. return Op0;
  665. // X - X -> 0
  666. if (Op0 == Op1)
  667. return Constant::getNullValue(Op0->getType());
  668. // Is this a negation?
  669. if (match(Op0, m_Zero())) {
  670. // 0 - X -> 0 if the sub is NUW.
  671. if (isNUW)
  672. return Constant::getNullValue(Op0->getType());
  673. KnownBits Known = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  674. if (Known.Zero.isMaxSignedValue()) {
  675. // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
  676. // Op1 must be 0 because negating the minimum signed value is undefined.
  677. if (isNSW)
  678. return Constant::getNullValue(Op0->getType());
  679. // 0 - X -> X if X is 0 or the minimum signed value.
  680. return Op1;
  681. }
  682. }
  683. // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
  684. // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
  685. Value *X = nullptr, *Y = nullptr, *Z = Op1;
  686. if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
  687. // See if "V === Y - Z" simplifies.
  688. if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1))
  689. // It does! Now see if "X + V" simplifies.
  690. if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) {
  691. // It does, we successfully reassociated!
  692. ++NumReassoc;
  693. return W;
  694. }
  695. // See if "V === X - Z" simplifies.
  696. if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
  697. // It does! Now see if "Y + V" simplifies.
  698. if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) {
  699. // It does, we successfully reassociated!
  700. ++NumReassoc;
  701. return W;
  702. }
  703. }
  704. // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
  705. // For example, X - (X + 1) -> -1
  706. X = Op0;
  707. if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
  708. // See if "V === X - Y" simplifies.
  709. if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
  710. // It does! Now see if "V - Z" simplifies.
  711. if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) {
  712. // It does, we successfully reassociated!
  713. ++NumReassoc;
  714. return W;
  715. }
  716. // See if "V === X - Z" simplifies.
  717. if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1))
  718. // It does! Now see if "V - Y" simplifies.
  719. if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) {
  720. // It does, we successfully reassociated!
  721. ++NumReassoc;
  722. return W;
  723. }
  724. }
  725. // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
  726. // For example, X - (X - Y) -> Y.
  727. Z = Op0;
  728. if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
  729. // See if "V === Z - X" simplifies.
  730. if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1))
  731. // It does! Now see if "V + Y" simplifies.
  732. if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) {
  733. // It does, we successfully reassociated!
  734. ++NumReassoc;
  735. return W;
  736. }
  737. // trunc(X) - trunc(Y) -> trunc(X - Y) if everything simplifies.
  738. if (MaxRecurse && match(Op0, m_Trunc(m_Value(X))) &&
  739. match(Op1, m_Trunc(m_Value(Y))))
  740. if (X->getType() == Y->getType())
  741. // See if "V === X - Y" simplifies.
  742. if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1))
  743. // It does! Now see if "trunc V" simplifies.
  744. if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(),
  745. Q, MaxRecurse - 1))
  746. // It does, return the simplified "trunc V".
  747. return W;
  748. // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...).
  749. if (match(Op0, m_PtrToInt(m_Value(X))) &&
  750. match(Op1, m_PtrToInt(m_Value(Y))))
  751. if (Constant *Result = computePointerDifference(Q.DL, X, Y))
  752. return ConstantExpr::getIntegerCast(Result, Op0->getType(), true);
  753. // i1 sub -> xor.
  754. if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
  755. if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1))
  756. return V;
  757. // Threading Sub over selects and phi nodes is pointless, so don't bother.
  758. // Threading over the select in "A - select(cond, B, C)" means evaluating
  759. // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
  760. // only if B and C are equal. If B and C are equal then (since we assume
  761. // that operands have already been simplified) "select(cond, B, C)" should
  762. // have been simplified to the common value of B and C already. Analysing
  763. // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
  764. // for threading over phi nodes.
  765. return nullptr;
  766. }
  767. Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  768. const SimplifyQuery &Q) {
  769. return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
  770. }
  771. /// Given operands for a Mul, see if we can fold the result.
  772. /// If not, this returns null.
  773. static Value *SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  774. unsigned MaxRecurse) {
  775. if (Constant *C = foldOrCommuteConstant(Instruction::Mul, Op0, Op1, Q))
  776. return C;
  777. // X * poison -> poison
  778. if (isa<PoisonValue>(Op1))
  779. return Op1;
  780. // X * undef -> 0
  781. // X * 0 -> 0
  782. if (Q.isUndefValue(Op1) || match(Op1, m_Zero()))
  783. return Constant::getNullValue(Op0->getType());
  784. // X * 1 -> X
  785. if (match(Op1, m_One()))
  786. return Op0;
  787. // (X / Y) * Y -> X if the division is exact.
  788. Value *X = nullptr;
  789. if (Q.IIQ.UseInstrInfo &&
  790. (match(Op0,
  791. m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
  792. match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
  793. return X;
  794. // i1 mul -> and.
  795. if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
  796. if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1))
  797. return V;
  798. // Try some generic simplifications for associative operations.
  799. if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q,
  800. MaxRecurse))
  801. return V;
  802. // Mul distributes over Add. Try some generic simplifications based on this.
  803. if (Value *V = expandCommutativeBinOp(Instruction::Mul, Op0, Op1,
  804. Instruction::Add, Q, MaxRecurse))
  805. return V;
  806. // If the operation is with the result of a select instruction, check whether
  807. // operating on either branch of the select always yields the same value.
  808. if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
  809. if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q,
  810. MaxRecurse))
  811. return V;
  812. // If the operation is with the result of a phi instruction, check whether
  813. // operating on all incoming values of the phi always yields the same value.
  814. if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
  815. if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q,
  816. MaxRecurse))
  817. return V;
  818. return nullptr;
  819. }
  820. Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  821. return ::SimplifyMulInst(Op0, Op1, Q, RecursionLimit);
  822. }
  823. /// Check for common or similar folds of integer division or integer remainder.
  824. /// This applies to all 4 opcodes (sdiv/udiv/srem/urem).
  825. static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0,
  826. Value *Op1, const SimplifyQuery &Q) {
  827. bool IsDiv = (Opcode == Instruction::SDiv || Opcode == Instruction::UDiv);
  828. bool IsSigned = (Opcode == Instruction::SDiv || Opcode == Instruction::SRem);
  829. Type *Ty = Op0->getType();
  830. // X / undef -> poison
  831. // X % undef -> poison
  832. if (Q.isUndefValue(Op1) || isa<PoisonValue>(Op1))
  833. return PoisonValue::get(Ty);
  834. // X / 0 -> poison
  835. // X % 0 -> poison
  836. // We don't need to preserve faults!
  837. if (match(Op1, m_Zero()))
  838. return PoisonValue::get(Ty);
  839. // If any element of a constant divisor fixed width vector is zero or undef
  840. // the behavior is undefined and we can fold the whole op to poison.
  841. auto *Op1C = dyn_cast<Constant>(Op1);
  842. auto *VTy = dyn_cast<FixedVectorType>(Ty);
  843. if (Op1C && VTy) {
  844. unsigned NumElts = VTy->getNumElements();
  845. for (unsigned i = 0; i != NumElts; ++i) {
  846. Constant *Elt = Op1C->getAggregateElement(i);
  847. if (Elt && (Elt->isNullValue() || Q.isUndefValue(Elt)))
  848. return PoisonValue::get(Ty);
  849. }
  850. }
  851. // poison / X -> poison
  852. // poison % X -> poison
  853. if (isa<PoisonValue>(Op0))
  854. return Op0;
  855. // undef / X -> 0
  856. // undef % X -> 0
  857. if (Q.isUndefValue(Op0))
  858. return Constant::getNullValue(Ty);
  859. // 0 / X -> 0
  860. // 0 % X -> 0
  861. if (match(Op0, m_Zero()))
  862. return Constant::getNullValue(Op0->getType());
  863. // X / X -> 1
  864. // X % X -> 0
  865. if (Op0 == Op1)
  866. return IsDiv ? ConstantInt::get(Ty, 1) : Constant::getNullValue(Ty);
  867. // X / 1 -> X
  868. // X % 1 -> 0
  869. // If this is a boolean op (single-bit element type), we can't have
  870. // division-by-zero or remainder-by-zero, so assume the divisor is 1.
  871. // Similarly, if we're zero-extending a boolean divisor, then assume it's a 1.
  872. Value *X;
  873. if (match(Op1, m_One()) || Ty->isIntOrIntVectorTy(1) ||
  874. (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
  875. return IsDiv ? Op0 : Constant::getNullValue(Ty);
  876. // If X * Y does not overflow, then:
  877. // X * Y / Y -> X
  878. // X * Y % Y -> 0
  879. if (match(Op0, m_c_Mul(m_Value(X), m_Specific(Op1)))) {
  880. auto *Mul = cast<OverflowingBinaryOperator>(Op0);
  881. // The multiplication can't overflow if it is defined not to, or if
  882. // X == A / Y for some A.
  883. if ((IsSigned && Q.IIQ.hasNoSignedWrap(Mul)) ||
  884. (!IsSigned && Q.IIQ.hasNoUnsignedWrap(Mul)) ||
  885. (IsSigned && match(X, m_SDiv(m_Value(), m_Specific(Op1)))) ||
  886. (!IsSigned && match(X, m_UDiv(m_Value(), m_Specific(Op1))))) {
  887. return IsDiv ? X : Constant::getNullValue(Op0->getType());
  888. }
  889. }
  890. return nullptr;
  891. }
  892. /// Given a predicate and two operands, return true if the comparison is true.
  893. /// This is a helper for div/rem simplification where we return some other value
  894. /// when we can prove a relationship between the operands.
  895. static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS,
  896. const SimplifyQuery &Q, unsigned MaxRecurse) {
  897. Value *V = SimplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse);
  898. Constant *C = dyn_cast_or_null<Constant>(V);
  899. return (C && C->isAllOnesValue());
  900. }
  901. /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer
  902. /// to simplify X % Y to X.
  903. static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q,
  904. unsigned MaxRecurse, bool IsSigned) {
  905. // Recursion is always used, so bail out at once if we already hit the limit.
  906. if (!MaxRecurse--)
  907. return false;
  908. if (IsSigned) {
  909. // |X| / |Y| --> 0
  910. //
  911. // We require that 1 operand is a simple constant. That could be extended to
  912. // 2 variables if we computed the sign bit for each.
  913. //
  914. // Make sure that a constant is not the minimum signed value because taking
  915. // the abs() of that is undefined.
  916. Type *Ty = X->getType();
  917. const APInt *C;
  918. if (match(X, m_APInt(C)) && !C->isMinSignedValue()) {
  919. // Is the variable divisor magnitude always greater than the constant
  920. // dividend magnitude?
  921. // |Y| > |C| --> Y < -abs(C) or Y > abs(C)
  922. Constant *PosDividendC = ConstantInt::get(Ty, C->abs());
  923. Constant *NegDividendC = ConstantInt::get(Ty, -C->abs());
  924. if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) ||
  925. isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse))
  926. return true;
  927. }
  928. if (match(Y, m_APInt(C))) {
  929. // Special-case: we can't take the abs() of a minimum signed value. If
  930. // that's the divisor, then all we have to do is prove that the dividend
  931. // is also not the minimum signed value.
  932. if (C->isMinSignedValue())
  933. return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse);
  934. // Is the variable dividend magnitude always less than the constant
  935. // divisor magnitude?
  936. // |X| < |C| --> X > -abs(C) and X < abs(C)
  937. Constant *PosDivisorC = ConstantInt::get(Ty, C->abs());
  938. Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs());
  939. if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) &&
  940. isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse))
  941. return true;
  942. }
  943. return false;
  944. }
  945. // IsSigned == false.
  946. // Is the unsigned dividend known to be less than a constant divisor?
  947. // TODO: Convert this (and above) to range analysis
  948. // ("computeConstantRangeIncludingKnownBits")?
  949. const APInt *C;
  950. if (match(Y, m_APInt(C)) &&
  951. computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT).getMaxValue().ult(*C))
  952. return true;
  953. // Try again for any divisor:
  954. // Is the dividend unsigned less than the divisor?
  955. return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse);
  956. }
  957. /// These are simplifications common to SDiv and UDiv.
  958. static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
  959. const SimplifyQuery &Q, unsigned MaxRecurse) {
  960. if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
  961. return C;
  962. if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q))
  963. return V;
  964. bool IsSigned = Opcode == Instruction::SDiv;
  965. // (X rem Y) / Y -> 0
  966. if ((IsSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
  967. (!IsSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
  968. return Constant::getNullValue(Op0->getType());
  969. // (X /u C1) /u C2 -> 0 if C1 * C2 overflow
  970. ConstantInt *C1, *C2;
  971. if (!IsSigned && match(Op0, m_UDiv(m_Value(), m_ConstantInt(C1))) &&
  972. match(Op1, m_ConstantInt(C2))) {
  973. bool Overflow;
  974. (void)C1->getValue().umul_ov(C2->getValue(), Overflow);
  975. if (Overflow)
  976. return Constant::getNullValue(Op0->getType());
  977. }
  978. // If the operation is with the result of a select instruction, check whether
  979. // operating on either branch of the select always yields the same value.
  980. if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
  981. if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
  982. return V;
  983. // If the operation is with the result of a phi instruction, check whether
  984. // operating on all incoming values of the phi always yields the same value.
  985. if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
  986. if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
  987. return V;
  988. if (isDivZero(Op0, Op1, Q, MaxRecurse, IsSigned))
  989. return Constant::getNullValue(Op0->getType());
  990. return nullptr;
  991. }
  992. /// These are simplifications common to SRem and URem.
  993. static Value *simplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
  994. const SimplifyQuery &Q, unsigned MaxRecurse) {
  995. if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
  996. return C;
  997. if (Value *V = simplifyDivRem(Opcode, Op0, Op1, Q))
  998. return V;
  999. // (X % Y) % Y -> X % Y
  1000. if ((Opcode == Instruction::SRem &&
  1001. match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
  1002. (Opcode == Instruction::URem &&
  1003. match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
  1004. return Op0;
  1005. // (X << Y) % X -> 0
  1006. if (Q.IIQ.UseInstrInfo &&
  1007. ((Opcode == Instruction::SRem &&
  1008. match(Op0, m_NSWShl(m_Specific(Op1), m_Value()))) ||
  1009. (Opcode == Instruction::URem &&
  1010. match(Op0, m_NUWShl(m_Specific(Op1), m_Value())))))
  1011. return Constant::getNullValue(Op0->getType());
  1012. // If the operation is with the result of a select instruction, check whether
  1013. // operating on either branch of the select always yields the same value.
  1014. if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
  1015. if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
  1016. return V;
  1017. // If the operation is with the result of a phi instruction, check whether
  1018. // operating on all incoming values of the phi always yields the same value.
  1019. if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
  1020. if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
  1021. return V;
  1022. // If X / Y == 0, then X % Y == X.
  1023. if (isDivZero(Op0, Op1, Q, MaxRecurse, Opcode == Instruction::SRem))
  1024. return Op0;
  1025. return nullptr;
  1026. }
  1027. /// Given operands for an SDiv, see if we can fold the result.
  1028. /// If not, this returns null.
  1029. static Value *SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  1030. unsigned MaxRecurse) {
  1031. // If two operands are negated and no signed overflow, return -1.
  1032. if (isKnownNegation(Op0, Op1, /*NeedNSW=*/true))
  1033. return Constant::getAllOnesValue(Op0->getType());
  1034. return simplifyDiv(Instruction::SDiv, Op0, Op1, Q, MaxRecurse);
  1035. }
  1036. Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  1037. return ::SimplifySDivInst(Op0, Op1, Q, RecursionLimit);
  1038. }
  1039. /// Given operands for a UDiv, see if we can fold the result.
  1040. /// If not, this returns null.
  1041. static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  1042. unsigned MaxRecurse) {
  1043. return simplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse);
  1044. }
  1045. Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  1046. return ::SimplifyUDivInst(Op0, Op1, Q, RecursionLimit);
  1047. }
  1048. /// Given operands for an SRem, see if we can fold the result.
  1049. /// If not, this returns null.
  1050. static Value *SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  1051. unsigned MaxRecurse) {
  1052. // If the divisor is 0, the result is undefined, so assume the divisor is -1.
  1053. // srem Op0, (sext i1 X) --> srem Op0, -1 --> 0
  1054. Value *X;
  1055. if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
  1056. return ConstantInt::getNullValue(Op0->getType());
  1057. // If the two operands are negated, return 0.
  1058. if (isKnownNegation(Op0, Op1))
  1059. return ConstantInt::getNullValue(Op0->getType());
  1060. return simplifyRem(Instruction::SRem, Op0, Op1, Q, MaxRecurse);
  1061. }
  1062. Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  1063. return ::SimplifySRemInst(Op0, Op1, Q, RecursionLimit);
  1064. }
  1065. /// Given operands for a URem, see if we can fold the result.
  1066. /// If not, this returns null.
  1067. static Value *SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  1068. unsigned MaxRecurse) {
  1069. return simplifyRem(Instruction::URem, Op0, Op1, Q, MaxRecurse);
  1070. }
  1071. Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  1072. return ::SimplifyURemInst(Op0, Op1, Q, RecursionLimit);
  1073. }
  1074. /// Returns true if a shift by \c Amount always yields poison.
  1075. static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) {
  1076. Constant *C = dyn_cast<Constant>(Amount);
  1077. if (!C)
  1078. return false;
  1079. // X shift by undef -> poison because it may shift by the bitwidth.
  1080. if (Q.isUndefValue(C))
  1081. return true;
  1082. // Shifting by the bitwidth or more is undefined.
  1083. if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
  1084. if (CI->getValue().uge(CI->getType()->getScalarSizeInBits()))
  1085. return true;
  1086. // If all lanes of a vector shift are undefined the whole shift is.
  1087. if (isa<ConstantVector>(C) || isa<ConstantDataVector>(C)) {
  1088. for (unsigned I = 0,
  1089. E = cast<FixedVectorType>(C->getType())->getNumElements();
  1090. I != E; ++I)
  1091. if (!isPoisonShift(C->getAggregateElement(I), Q))
  1092. return false;
  1093. return true;
  1094. }
  1095. return false;
  1096. }
  1097. /// Given operands for an Shl, LShr or AShr, see if we can fold the result.
  1098. /// If not, this returns null.
  1099. static Value *SimplifyShift(Instruction::BinaryOps Opcode, Value *Op0,
  1100. Value *Op1, bool IsNSW, const SimplifyQuery &Q,
  1101. unsigned MaxRecurse) {
  1102. if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q))
  1103. return C;
  1104. // poison shift by X -> poison
  1105. if (isa<PoisonValue>(Op0))
  1106. return Op0;
  1107. // 0 shift by X -> 0
  1108. if (match(Op0, m_Zero()))
  1109. return Constant::getNullValue(Op0->getType());
  1110. // X shift by 0 -> X
  1111. // Shift-by-sign-extended bool must be shift-by-0 because shift-by-all-ones
  1112. // would be poison.
  1113. Value *X;
  1114. if (match(Op1, m_Zero()) ||
  1115. (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
  1116. return Op0;
  1117. // Fold undefined shifts.
  1118. if (isPoisonShift(Op1, Q))
  1119. return PoisonValue::get(Op0->getType());
  1120. // If the operation is with the result of a select instruction, check whether
  1121. // operating on either branch of the select always yields the same value.
  1122. if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
  1123. if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, Q, MaxRecurse))
  1124. return V;
  1125. // If the operation is with the result of a phi instruction, check whether
  1126. // operating on all incoming values of the phi always yields the same value.
  1127. if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
  1128. if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, Q, MaxRecurse))
  1129. return V;
  1130. // If any bits in the shift amount make that value greater than or equal to
  1131. // the number of bits in the type, the shift is undefined.
  1132. KnownBits KnownAmt = computeKnownBits(Op1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  1133. if (KnownAmt.getMinValue().uge(KnownAmt.getBitWidth()))
  1134. return PoisonValue::get(Op0->getType());
  1135. // If all valid bits in the shift amount are known zero, the first operand is
  1136. // unchanged.
  1137. unsigned NumValidShiftBits = Log2_32_Ceil(KnownAmt.getBitWidth());
  1138. if (KnownAmt.countMinTrailingZeros() >= NumValidShiftBits)
  1139. return Op0;
  1140. // Check for nsw shl leading to a poison value.
  1141. if (IsNSW) {
  1142. assert(Opcode == Instruction::Shl && "Expected shl for nsw instruction");
  1143. KnownBits KnownVal = computeKnownBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  1144. KnownBits KnownShl = KnownBits::shl(KnownVal, KnownAmt);
  1145. if (KnownVal.Zero.isSignBitSet())
  1146. KnownShl.Zero.setSignBit();
  1147. if (KnownVal.One.isSignBitSet())
  1148. KnownShl.One.setSignBit();
  1149. if (KnownShl.hasConflict())
  1150. return PoisonValue::get(Op0->getType());
  1151. }
  1152. return nullptr;
  1153. }
  1154. /// Given operands for an Shl, LShr or AShr, see if we can
  1155. /// fold the result. If not, this returns null.
  1156. static Value *SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0,
  1157. Value *Op1, bool isExact, const SimplifyQuery &Q,
  1158. unsigned MaxRecurse) {
  1159. if (Value *V =
  1160. SimplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse))
  1161. return V;
  1162. // X >> X -> 0
  1163. if (Op0 == Op1)
  1164. return Constant::getNullValue(Op0->getType());
  1165. // undef >> X -> 0
  1166. // undef >> X -> undef (if it's exact)
  1167. if (Q.isUndefValue(Op0))
  1168. return isExact ? Op0 : Constant::getNullValue(Op0->getType());
  1169. // The low bit cannot be shifted out of an exact shift if it is set.
  1170. if (isExact) {
  1171. KnownBits Op0Known = computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT);
  1172. if (Op0Known.One[0])
  1173. return Op0;
  1174. }
  1175. return nullptr;
  1176. }
  1177. /// Given operands for an Shl, see if we can fold the result.
  1178. /// If not, this returns null.
  1179. static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  1180. const SimplifyQuery &Q, unsigned MaxRecurse) {
  1181. if (Value *V =
  1182. SimplifyShift(Instruction::Shl, Op0, Op1, isNSW, Q, MaxRecurse))
  1183. return V;
  1184. // undef << X -> 0
  1185. // undef << X -> undef if (if it's NSW/NUW)
  1186. if (Q.isUndefValue(Op0))
  1187. return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType());
  1188. // (X >> A) << A -> X
  1189. Value *X;
  1190. if (Q.IIQ.UseInstrInfo &&
  1191. match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
  1192. return X;
  1193. // shl nuw i8 C, %x -> C iff C has sign bit set.
  1194. if (isNUW && match(Op0, m_Negative()))
  1195. return Op0;
  1196. // NOTE: could use computeKnownBits() / LazyValueInfo,
  1197. // but the cost-benefit analysis suggests it isn't worth it.
  1198. return nullptr;
  1199. }
  1200. Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
  1201. const SimplifyQuery &Q) {
  1202. return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Q, RecursionLimit);
  1203. }
  1204. /// Given operands for an LShr, see if we can fold the result.
  1205. /// If not, this returns null.
  1206. static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
  1207. const SimplifyQuery &Q, unsigned MaxRecurse) {
  1208. if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q,
  1209. MaxRecurse))
  1210. return V;
  1211. // (X << A) >> A -> X
  1212. Value *X;
  1213. if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
  1214. return X;
  1215. // ((X << A) | Y) >> A -> X if effective width of Y is not larger than A.
  1216. // We can return X as we do in the above case since OR alters no bits in X.
  1217. // SimplifyDemandedBits in InstCombine can do more general optimization for
  1218. // bit manipulation. This pattern aims to provide opportunities for other
  1219. // optimizers by supporting a simple but common case in InstSimplify.
  1220. Value *Y;
  1221. const APInt *ShRAmt, *ShLAmt;
  1222. if (match(Op1, m_APInt(ShRAmt)) &&
  1223. match(Op0, m_c_Or(m_NUWShl(m_Value(X), m_APInt(ShLAmt)), m_Value(Y))) &&
  1224. *ShRAmt == *ShLAmt) {
  1225. const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  1226. const unsigned EffWidthY = YKnown.countMaxActiveBits();
  1227. if (ShRAmt->uge(EffWidthY))
  1228. return X;
  1229. }
  1230. return nullptr;
  1231. }
  1232. Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
  1233. const SimplifyQuery &Q) {
  1234. return ::SimplifyLShrInst(Op0, Op1, isExact, Q, RecursionLimit);
  1235. }
  1236. /// Given operands for an AShr, see if we can fold the result.
  1237. /// If not, this returns null.
  1238. static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
  1239. const SimplifyQuery &Q, unsigned MaxRecurse) {
  1240. if (Value *V = SimplifyRightShift(Instruction::AShr, Op0, Op1, isExact, Q,
  1241. MaxRecurse))
  1242. return V;
  1243. // -1 >>a X --> -1
  1244. // (-1 << X) a>> X --> -1
  1245. // Do not return Op0 because it may contain undef elements if it's a vector.
  1246. if (match(Op0, m_AllOnes()) ||
  1247. match(Op0, m_Shl(m_AllOnes(), m_Specific(Op1))))
  1248. return Constant::getAllOnesValue(Op0->getType());
  1249. // (X << A) >> A -> X
  1250. Value *X;
  1251. if (Q.IIQ.UseInstrInfo && match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))
  1252. return X;
  1253. // Arithmetic shifting an all-sign-bit value is a no-op.
  1254. unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  1255. if (NumSignBits == Op0->getType()->getScalarSizeInBits())
  1256. return Op0;
  1257. return nullptr;
  1258. }
  1259. Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
  1260. const SimplifyQuery &Q) {
  1261. return ::SimplifyAShrInst(Op0, Op1, isExact, Q, RecursionLimit);
  1262. }
  1263. /// Commuted variants are assumed to be handled by calling this function again
  1264. /// with the parameters swapped.
  1265. static Value *simplifyUnsignedRangeCheck(ICmpInst *ZeroICmp,
  1266. ICmpInst *UnsignedICmp, bool IsAnd,
  1267. const SimplifyQuery &Q) {
  1268. Value *X, *Y;
  1269. ICmpInst::Predicate EqPred;
  1270. if (!match(ZeroICmp, m_ICmp(EqPred, m_Value(Y), m_Zero())) ||
  1271. !ICmpInst::isEquality(EqPred))
  1272. return nullptr;
  1273. ICmpInst::Predicate UnsignedPred;
  1274. Value *A, *B;
  1275. // Y = (A - B);
  1276. if (match(Y, m_Sub(m_Value(A), m_Value(B)))) {
  1277. if (match(UnsignedICmp,
  1278. m_c_ICmp(UnsignedPred, m_Specific(A), m_Specific(B))) &&
  1279. ICmpInst::isUnsigned(UnsignedPred)) {
  1280. // A >=/<= B || (A - B) != 0 <--> true
  1281. if ((UnsignedPred == ICmpInst::ICMP_UGE ||
  1282. UnsignedPred == ICmpInst::ICMP_ULE) &&
  1283. EqPred == ICmpInst::ICMP_NE && !IsAnd)
  1284. return ConstantInt::getTrue(UnsignedICmp->getType());
  1285. // A </> B && (A - B) == 0 <--> false
  1286. if ((UnsignedPred == ICmpInst::ICMP_ULT ||
  1287. UnsignedPred == ICmpInst::ICMP_UGT) &&
  1288. EqPred == ICmpInst::ICMP_EQ && IsAnd)
  1289. return ConstantInt::getFalse(UnsignedICmp->getType());
  1290. // A </> B && (A - B) != 0 <--> A </> B
  1291. // A </> B || (A - B) != 0 <--> (A - B) != 0
  1292. if (EqPred == ICmpInst::ICMP_NE && (UnsignedPred == ICmpInst::ICMP_ULT ||
  1293. UnsignedPred == ICmpInst::ICMP_UGT))
  1294. return IsAnd ? UnsignedICmp : ZeroICmp;
  1295. // A <=/>= B && (A - B) == 0 <--> (A - B) == 0
  1296. // A <=/>= B || (A - B) == 0 <--> A <=/>= B
  1297. if (EqPred == ICmpInst::ICMP_EQ && (UnsignedPred == ICmpInst::ICMP_ULE ||
  1298. UnsignedPred == ICmpInst::ICMP_UGE))
  1299. return IsAnd ? ZeroICmp : UnsignedICmp;
  1300. }
  1301. // Given Y = (A - B)
  1302. // Y >= A && Y != 0 --> Y >= A iff B != 0
  1303. // Y < A || Y == 0 --> Y < A iff B != 0
  1304. if (match(UnsignedICmp,
  1305. m_c_ICmp(UnsignedPred, m_Specific(Y), m_Specific(A)))) {
  1306. if (UnsignedPred == ICmpInst::ICMP_UGE && IsAnd &&
  1307. EqPred == ICmpInst::ICMP_NE &&
  1308. isKnownNonZero(B, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
  1309. return UnsignedICmp;
  1310. if (UnsignedPred == ICmpInst::ICMP_ULT && !IsAnd &&
  1311. EqPred == ICmpInst::ICMP_EQ &&
  1312. isKnownNonZero(B, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
  1313. return UnsignedICmp;
  1314. }
  1315. }
  1316. if (match(UnsignedICmp, m_ICmp(UnsignedPred, m_Value(X), m_Specific(Y))) &&
  1317. ICmpInst::isUnsigned(UnsignedPred))
  1318. ;
  1319. else if (match(UnsignedICmp,
  1320. m_ICmp(UnsignedPred, m_Specific(Y), m_Value(X))) &&
  1321. ICmpInst::isUnsigned(UnsignedPred))
  1322. UnsignedPred = ICmpInst::getSwappedPredicate(UnsignedPred);
  1323. else
  1324. return nullptr;
  1325. // X > Y && Y == 0 --> Y == 0 iff X != 0
  1326. // X > Y || Y == 0 --> X > Y iff X != 0
  1327. if (UnsignedPred == ICmpInst::ICMP_UGT && EqPred == ICmpInst::ICMP_EQ &&
  1328. isKnownNonZero(X, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
  1329. return IsAnd ? ZeroICmp : UnsignedICmp;
  1330. // X <= Y && Y != 0 --> X <= Y iff X != 0
  1331. // X <= Y || Y != 0 --> Y != 0 iff X != 0
  1332. if (UnsignedPred == ICmpInst::ICMP_ULE && EqPred == ICmpInst::ICMP_NE &&
  1333. isKnownNonZero(X, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT))
  1334. return IsAnd ? UnsignedICmp : ZeroICmp;
  1335. // The transforms below here are expected to be handled more generally with
  1336. // simplifyAndOrOfICmpsWithLimitConst() or in InstCombine's
  1337. // foldAndOrOfICmpsWithConstEq(). If we are looking to trim optimizer overlap,
  1338. // these are candidates for removal.
  1339. // X < Y && Y != 0 --> X < Y
  1340. // X < Y || Y != 0 --> Y != 0
  1341. if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_NE)
  1342. return IsAnd ? UnsignedICmp : ZeroICmp;
  1343. // X >= Y && Y == 0 --> Y == 0
  1344. // X >= Y || Y == 0 --> X >= Y
  1345. if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_EQ)
  1346. return IsAnd ? ZeroICmp : UnsignedICmp;
  1347. // X < Y && Y == 0 --> false
  1348. if (UnsignedPred == ICmpInst::ICMP_ULT && EqPred == ICmpInst::ICMP_EQ &&
  1349. IsAnd)
  1350. return getFalse(UnsignedICmp->getType());
  1351. // X >= Y || Y != 0 --> true
  1352. if (UnsignedPred == ICmpInst::ICMP_UGE && EqPred == ICmpInst::ICMP_NE &&
  1353. !IsAnd)
  1354. return getTrue(UnsignedICmp->getType());
  1355. return nullptr;
  1356. }
  1357. /// Commuted variants are assumed to be handled by calling this function again
  1358. /// with the parameters swapped.
  1359. static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) {
  1360. ICmpInst::Predicate Pred0, Pred1;
  1361. Value *A ,*B;
  1362. if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) ||
  1363. !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B))))
  1364. return nullptr;
  1365. // We have (icmp Pred0, A, B) & (icmp Pred1, A, B).
  1366. // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we
  1367. // can eliminate Op1 from this 'and'.
  1368. if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1))
  1369. return Op0;
  1370. // Check for any combination of predicates that are guaranteed to be disjoint.
  1371. if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) ||
  1372. (Pred0 == ICmpInst::ICMP_EQ && ICmpInst::isFalseWhenEqual(Pred1)) ||
  1373. (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT) ||
  1374. (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT))
  1375. return getFalse(Op0->getType());
  1376. return nullptr;
  1377. }
  1378. /// Commuted variants are assumed to be handled by calling this function again
  1379. /// with the parameters swapped.
  1380. static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) {
  1381. ICmpInst::Predicate Pred0, Pred1;
  1382. Value *A ,*B;
  1383. if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) ||
  1384. !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B))))
  1385. return nullptr;
  1386. // We have (icmp Pred0, A, B) | (icmp Pred1, A, B).
  1387. // If Op1 is always implied true by Op0, then Op0 is a subset of Op1, and we
  1388. // can eliminate Op0 from this 'or'.
  1389. if (ICmpInst::isImpliedTrueByMatchingCmp(Pred0, Pred1))
  1390. return Op1;
  1391. // Check for any combination of predicates that cover the entire range of
  1392. // possibilities.
  1393. if ((Pred0 == ICmpInst::getInversePredicate(Pred1)) ||
  1394. (Pred0 == ICmpInst::ICMP_NE && ICmpInst::isTrueWhenEqual(Pred1)) ||
  1395. (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGE) ||
  1396. (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGE))
  1397. return getTrue(Op0->getType());
  1398. return nullptr;
  1399. }
  1400. /// Test if a pair of compares with a shared operand and 2 constants has an
  1401. /// empty set intersection, full set union, or if one compare is a superset of
  1402. /// the other.
  1403. static Value *simplifyAndOrOfICmpsWithConstants(ICmpInst *Cmp0, ICmpInst *Cmp1,
  1404. bool IsAnd) {
  1405. // Look for this pattern: {and/or} (icmp X, C0), (icmp X, C1)).
  1406. if (Cmp0->getOperand(0) != Cmp1->getOperand(0))
  1407. return nullptr;
  1408. const APInt *C0, *C1;
  1409. if (!match(Cmp0->getOperand(1), m_APInt(C0)) ||
  1410. !match(Cmp1->getOperand(1), m_APInt(C1)))
  1411. return nullptr;
  1412. auto Range0 = ConstantRange::makeExactICmpRegion(Cmp0->getPredicate(), *C0);
  1413. auto Range1 = ConstantRange::makeExactICmpRegion(Cmp1->getPredicate(), *C1);
  1414. // For and-of-compares, check if the intersection is empty:
  1415. // (icmp X, C0) && (icmp X, C1) --> empty set --> false
  1416. if (IsAnd && Range0.intersectWith(Range1).isEmptySet())
  1417. return getFalse(Cmp0->getType());
  1418. // For or-of-compares, check if the union is full:
  1419. // (icmp X, C0) || (icmp X, C1) --> full set --> true
  1420. if (!IsAnd && Range0.unionWith(Range1).isFullSet())
  1421. return getTrue(Cmp0->getType());
  1422. // Is one range a superset of the other?
  1423. // If this is and-of-compares, take the smaller set:
  1424. // (icmp sgt X, 4) && (icmp sgt X, 42) --> icmp sgt X, 42
  1425. // If this is or-of-compares, take the larger set:
  1426. // (icmp sgt X, 4) || (icmp sgt X, 42) --> icmp sgt X, 4
  1427. if (Range0.contains(Range1))
  1428. return IsAnd ? Cmp1 : Cmp0;
  1429. if (Range1.contains(Range0))
  1430. return IsAnd ? Cmp0 : Cmp1;
  1431. return nullptr;
  1432. }
  1433. static Value *simplifyAndOrOfICmpsWithZero(ICmpInst *Cmp0, ICmpInst *Cmp1,
  1434. bool IsAnd) {
  1435. ICmpInst::Predicate P0 = Cmp0->getPredicate(), P1 = Cmp1->getPredicate();
  1436. if (!match(Cmp0->getOperand(1), m_Zero()) ||
  1437. !match(Cmp1->getOperand(1), m_Zero()) || P0 != P1)
  1438. return nullptr;
  1439. if ((IsAnd && P0 != ICmpInst::ICMP_NE) || (!IsAnd && P1 != ICmpInst::ICMP_EQ))
  1440. return nullptr;
  1441. // We have either "(X == 0 || Y == 0)" or "(X != 0 && Y != 0)".
  1442. Value *X = Cmp0->getOperand(0);
  1443. Value *Y = Cmp1->getOperand(0);
  1444. // If one of the compares is a masked version of a (not) null check, then
  1445. // that compare implies the other, so we eliminate the other. Optionally, look
  1446. // through a pointer-to-int cast to match a null check of a pointer type.
  1447. // (X == 0) || (([ptrtoint] X & ?) == 0) --> ([ptrtoint] X & ?) == 0
  1448. // (X == 0) || ((? & [ptrtoint] X) == 0) --> (? & [ptrtoint] X) == 0
  1449. // (X != 0) && (([ptrtoint] X & ?) != 0) --> ([ptrtoint] X & ?) != 0
  1450. // (X != 0) && ((? & [ptrtoint] X) != 0) --> (? & [ptrtoint] X) != 0
  1451. if (match(Y, m_c_And(m_Specific(X), m_Value())) ||
  1452. match(Y, m_c_And(m_PtrToInt(m_Specific(X)), m_Value())))
  1453. return Cmp1;
  1454. // (([ptrtoint] Y & ?) == 0) || (Y == 0) --> ([ptrtoint] Y & ?) == 0
  1455. // ((? & [ptrtoint] Y) == 0) || (Y == 0) --> (? & [ptrtoint] Y) == 0
  1456. // (([ptrtoint] Y & ?) != 0) && (Y != 0) --> ([ptrtoint] Y & ?) != 0
  1457. // ((? & [ptrtoint] Y) != 0) && (Y != 0) --> (? & [ptrtoint] Y) != 0
  1458. if (match(X, m_c_And(m_Specific(Y), m_Value())) ||
  1459. match(X, m_c_And(m_PtrToInt(m_Specific(Y)), m_Value())))
  1460. return Cmp0;
  1461. return nullptr;
  1462. }
  1463. static Value *simplifyAndOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
  1464. const InstrInfoQuery &IIQ) {
  1465. // (icmp (add V, C0), C1) & (icmp V, C0)
  1466. ICmpInst::Predicate Pred0, Pred1;
  1467. const APInt *C0, *C1;
  1468. Value *V;
  1469. if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
  1470. return nullptr;
  1471. if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
  1472. return nullptr;
  1473. auto *AddInst = cast<OverflowingBinaryOperator>(Op0->getOperand(0));
  1474. if (AddInst->getOperand(1) != Op1->getOperand(1))
  1475. return nullptr;
  1476. Type *ITy = Op0->getType();
  1477. bool isNSW = IIQ.hasNoSignedWrap(AddInst);
  1478. bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
  1479. const APInt Delta = *C1 - *C0;
  1480. if (C0->isStrictlyPositive()) {
  1481. if (Delta == 2) {
  1482. if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_SGT)
  1483. return getFalse(ITy);
  1484. if (Pred0 == ICmpInst::ICMP_SLT && Pred1 == ICmpInst::ICMP_SGT && isNSW)
  1485. return getFalse(ITy);
  1486. }
  1487. if (Delta == 1) {
  1488. if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_SGT)
  1489. return getFalse(ITy);
  1490. if (Pred0 == ICmpInst::ICMP_SLE && Pred1 == ICmpInst::ICMP_SGT && isNSW)
  1491. return getFalse(ITy);
  1492. }
  1493. }
  1494. if (C0->getBoolValue() && isNUW) {
  1495. if (Delta == 2)
  1496. if (Pred0 == ICmpInst::ICMP_ULT && Pred1 == ICmpInst::ICMP_UGT)
  1497. return getFalse(ITy);
  1498. if (Delta == 1)
  1499. if (Pred0 == ICmpInst::ICMP_ULE && Pred1 == ICmpInst::ICMP_UGT)
  1500. return getFalse(ITy);
  1501. }
  1502. return nullptr;
  1503. }
  1504. /// Try to eliminate compares with signed or unsigned min/max constants.
  1505. static Value *simplifyAndOrOfICmpsWithLimitConst(ICmpInst *Cmp0, ICmpInst *Cmp1,
  1506. bool IsAnd) {
  1507. // Canonicalize an equality compare as Cmp0.
  1508. if (Cmp1->isEquality())
  1509. std::swap(Cmp0, Cmp1);
  1510. if (!Cmp0->isEquality())
  1511. return nullptr;
  1512. // The non-equality compare must include a common operand (X). Canonicalize
  1513. // the common operand as operand 0 (the predicate is swapped if the common
  1514. // operand was operand 1).
  1515. ICmpInst::Predicate Pred0 = Cmp0->getPredicate();
  1516. Value *X = Cmp0->getOperand(0);
  1517. ICmpInst::Predicate Pred1;
  1518. bool HasNotOp = match(Cmp1, m_c_ICmp(Pred1, m_Not(m_Specific(X)), m_Value()));
  1519. if (!HasNotOp && !match(Cmp1, m_c_ICmp(Pred1, m_Specific(X), m_Value())))
  1520. return nullptr;
  1521. if (ICmpInst::isEquality(Pred1))
  1522. return nullptr;
  1523. // The equality compare must be against a constant. Flip bits if we matched
  1524. // a bitwise not. Convert a null pointer constant to an integer zero value.
  1525. APInt MinMaxC;
  1526. const APInt *C;
  1527. if (match(Cmp0->getOperand(1), m_APInt(C)))
  1528. MinMaxC = HasNotOp ? ~*C : *C;
  1529. else if (isa<ConstantPointerNull>(Cmp0->getOperand(1)))
  1530. MinMaxC = APInt::getZero(8);
  1531. else
  1532. return nullptr;
  1533. // DeMorganize if this is 'or': P0 || P1 --> !P0 && !P1.
  1534. if (!IsAnd) {
  1535. Pred0 = ICmpInst::getInversePredicate(Pred0);
  1536. Pred1 = ICmpInst::getInversePredicate(Pred1);
  1537. }
  1538. // Normalize to unsigned compare and unsigned min/max value.
  1539. // Example for 8-bit: -128 + 128 -> 0; 127 + 128 -> 255
  1540. if (ICmpInst::isSigned(Pred1)) {
  1541. Pred1 = ICmpInst::getUnsignedPredicate(Pred1);
  1542. MinMaxC += APInt::getSignedMinValue(MinMaxC.getBitWidth());
  1543. }
  1544. // (X != MAX) && (X < Y) --> X < Y
  1545. // (X == MAX) || (X >= Y) --> X >= Y
  1546. if (MinMaxC.isMaxValue())
  1547. if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT)
  1548. return Cmp1;
  1549. // (X != MIN) && (X > Y) --> X > Y
  1550. // (X == MIN) || (X <= Y) --> X <= Y
  1551. if (MinMaxC.isMinValue())
  1552. if (Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_UGT)
  1553. return Cmp1;
  1554. return nullptr;
  1555. }
  1556. static Value *simplifyAndOfICmps(ICmpInst *Op0, ICmpInst *Op1,
  1557. const SimplifyQuery &Q) {
  1558. if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/true, Q))
  1559. return X;
  1560. if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/true, Q))
  1561. return X;
  1562. if (Value *X = simplifyAndOfICmpsWithSameOperands(Op0, Op1))
  1563. return X;
  1564. if (Value *X = simplifyAndOfICmpsWithSameOperands(Op1, Op0))
  1565. return X;
  1566. if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, true))
  1567. return X;
  1568. if (Value *X = simplifyAndOrOfICmpsWithLimitConst(Op0, Op1, true))
  1569. return X;
  1570. if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, true))
  1571. return X;
  1572. if (Value *X = simplifyAndOfICmpsWithAdd(Op0, Op1, Q.IIQ))
  1573. return X;
  1574. if (Value *X = simplifyAndOfICmpsWithAdd(Op1, Op0, Q.IIQ))
  1575. return X;
  1576. return nullptr;
  1577. }
  1578. static Value *simplifyOrOfICmpsWithAdd(ICmpInst *Op0, ICmpInst *Op1,
  1579. const InstrInfoQuery &IIQ) {
  1580. // (icmp (add V, C0), C1) | (icmp V, C0)
  1581. ICmpInst::Predicate Pred0, Pred1;
  1582. const APInt *C0, *C1;
  1583. Value *V;
  1584. if (!match(Op0, m_ICmp(Pred0, m_Add(m_Value(V), m_APInt(C0)), m_APInt(C1))))
  1585. return nullptr;
  1586. if (!match(Op1, m_ICmp(Pred1, m_Specific(V), m_Value())))
  1587. return nullptr;
  1588. auto *AddInst = cast<BinaryOperator>(Op0->getOperand(0));
  1589. if (AddInst->getOperand(1) != Op1->getOperand(1))
  1590. return nullptr;
  1591. Type *ITy = Op0->getType();
  1592. bool isNSW = IIQ.hasNoSignedWrap(AddInst);
  1593. bool isNUW = IIQ.hasNoUnsignedWrap(AddInst);
  1594. const APInt Delta = *C1 - *C0;
  1595. if (C0->isStrictlyPositive()) {
  1596. if (Delta == 2) {
  1597. if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_SLE)
  1598. return getTrue(ITy);
  1599. if (Pred0 == ICmpInst::ICMP_SGE && Pred1 == ICmpInst::ICMP_SLE && isNSW)
  1600. return getTrue(ITy);
  1601. }
  1602. if (Delta == 1) {
  1603. if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_SLE)
  1604. return getTrue(ITy);
  1605. if (Pred0 == ICmpInst::ICMP_SGT && Pred1 == ICmpInst::ICMP_SLE && isNSW)
  1606. return getTrue(ITy);
  1607. }
  1608. }
  1609. if (C0->getBoolValue() && isNUW) {
  1610. if (Delta == 2)
  1611. if (Pred0 == ICmpInst::ICMP_UGE && Pred1 == ICmpInst::ICMP_ULE)
  1612. return getTrue(ITy);
  1613. if (Delta == 1)
  1614. if (Pred0 == ICmpInst::ICMP_UGT && Pred1 == ICmpInst::ICMP_ULE)
  1615. return getTrue(ITy);
  1616. }
  1617. return nullptr;
  1618. }
  1619. static Value *simplifyOrOfICmps(ICmpInst *Op0, ICmpInst *Op1,
  1620. const SimplifyQuery &Q) {
  1621. if (Value *X = simplifyUnsignedRangeCheck(Op0, Op1, /*IsAnd=*/false, Q))
  1622. return X;
  1623. if (Value *X = simplifyUnsignedRangeCheck(Op1, Op0, /*IsAnd=*/false, Q))
  1624. return X;
  1625. if (Value *X = simplifyOrOfICmpsWithSameOperands(Op0, Op1))
  1626. return X;
  1627. if (Value *X = simplifyOrOfICmpsWithSameOperands(Op1, Op0))
  1628. return X;
  1629. if (Value *X = simplifyAndOrOfICmpsWithConstants(Op0, Op1, false))
  1630. return X;
  1631. if (Value *X = simplifyAndOrOfICmpsWithLimitConst(Op0, Op1, false))
  1632. return X;
  1633. if (Value *X = simplifyAndOrOfICmpsWithZero(Op0, Op1, false))
  1634. return X;
  1635. if (Value *X = simplifyOrOfICmpsWithAdd(Op0, Op1, Q.IIQ))
  1636. return X;
  1637. if (Value *X = simplifyOrOfICmpsWithAdd(Op1, Op0, Q.IIQ))
  1638. return X;
  1639. return nullptr;
  1640. }
  1641. static Value *simplifyAndOrOfFCmps(const TargetLibraryInfo *TLI,
  1642. FCmpInst *LHS, FCmpInst *RHS, bool IsAnd) {
  1643. Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1);
  1644. Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1);
  1645. if (LHS0->getType() != RHS0->getType())
  1646. return nullptr;
  1647. FCmpInst::Predicate PredL = LHS->getPredicate(), PredR = RHS->getPredicate();
  1648. if ((PredL == FCmpInst::FCMP_ORD && PredR == FCmpInst::FCMP_ORD && IsAnd) ||
  1649. (PredL == FCmpInst::FCMP_UNO && PredR == FCmpInst::FCMP_UNO && !IsAnd)) {
  1650. // (fcmp ord NNAN, X) & (fcmp ord X, Y) --> fcmp ord X, Y
  1651. // (fcmp ord NNAN, X) & (fcmp ord Y, X) --> fcmp ord Y, X
  1652. // (fcmp ord X, NNAN) & (fcmp ord X, Y) --> fcmp ord X, Y
  1653. // (fcmp ord X, NNAN) & (fcmp ord Y, X) --> fcmp ord Y, X
  1654. // (fcmp uno NNAN, X) | (fcmp uno X, Y) --> fcmp uno X, Y
  1655. // (fcmp uno NNAN, X) | (fcmp uno Y, X) --> fcmp uno Y, X
  1656. // (fcmp uno X, NNAN) | (fcmp uno X, Y) --> fcmp uno X, Y
  1657. // (fcmp uno X, NNAN) | (fcmp uno Y, X) --> fcmp uno Y, X
  1658. if ((isKnownNeverNaN(LHS0, TLI) && (LHS1 == RHS0 || LHS1 == RHS1)) ||
  1659. (isKnownNeverNaN(LHS1, TLI) && (LHS0 == RHS0 || LHS0 == RHS1)))
  1660. return RHS;
  1661. // (fcmp ord X, Y) & (fcmp ord NNAN, X) --> fcmp ord X, Y
  1662. // (fcmp ord Y, X) & (fcmp ord NNAN, X) --> fcmp ord Y, X
  1663. // (fcmp ord X, Y) & (fcmp ord X, NNAN) --> fcmp ord X, Y
  1664. // (fcmp ord Y, X) & (fcmp ord X, NNAN) --> fcmp ord Y, X
  1665. // (fcmp uno X, Y) | (fcmp uno NNAN, X) --> fcmp uno X, Y
  1666. // (fcmp uno Y, X) | (fcmp uno NNAN, X) --> fcmp uno Y, X
  1667. // (fcmp uno X, Y) | (fcmp uno X, NNAN) --> fcmp uno X, Y
  1668. // (fcmp uno Y, X) | (fcmp uno X, NNAN) --> fcmp uno Y, X
  1669. if ((isKnownNeverNaN(RHS0, TLI) && (RHS1 == LHS0 || RHS1 == LHS1)) ||
  1670. (isKnownNeverNaN(RHS1, TLI) && (RHS0 == LHS0 || RHS0 == LHS1)))
  1671. return LHS;
  1672. }
  1673. return nullptr;
  1674. }
  1675. static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q,
  1676. Value *Op0, Value *Op1, bool IsAnd) {
  1677. // Look through casts of the 'and' operands to find compares.
  1678. auto *Cast0 = dyn_cast<CastInst>(Op0);
  1679. auto *Cast1 = dyn_cast<CastInst>(Op1);
  1680. if (Cast0 && Cast1 && Cast0->getOpcode() == Cast1->getOpcode() &&
  1681. Cast0->getSrcTy() == Cast1->getSrcTy()) {
  1682. Op0 = Cast0->getOperand(0);
  1683. Op1 = Cast1->getOperand(0);
  1684. }
  1685. Value *V = nullptr;
  1686. auto *ICmp0 = dyn_cast<ICmpInst>(Op0);
  1687. auto *ICmp1 = dyn_cast<ICmpInst>(Op1);
  1688. if (ICmp0 && ICmp1)
  1689. V = IsAnd ? simplifyAndOfICmps(ICmp0, ICmp1, Q)
  1690. : simplifyOrOfICmps(ICmp0, ICmp1, Q);
  1691. auto *FCmp0 = dyn_cast<FCmpInst>(Op0);
  1692. auto *FCmp1 = dyn_cast<FCmpInst>(Op1);
  1693. if (FCmp0 && FCmp1)
  1694. V = simplifyAndOrOfFCmps(Q.TLI, FCmp0, FCmp1, IsAnd);
  1695. if (!V)
  1696. return nullptr;
  1697. if (!Cast0)
  1698. return V;
  1699. // If we looked through casts, we can only handle a constant simplification
  1700. // because we are not allowed to create a cast instruction here.
  1701. if (auto *C = dyn_cast<Constant>(V))
  1702. return ConstantExpr::getCast(Cast0->getOpcode(), C, Cast0->getType());
  1703. return nullptr;
  1704. }
  1705. /// Given a bitwise logic op, check if the operands are add/sub with a common
  1706. /// source value and inverted constant (identity: C - X -> ~(X + ~C)).
  1707. static Value *simplifyLogicOfAddSub(Value *Op0, Value *Op1,
  1708. Instruction::BinaryOps Opcode) {
  1709. assert(Op0->getType() == Op1->getType() && "Mismatched binop types");
  1710. assert(BinaryOperator::isBitwiseLogicOp(Opcode) && "Expected logic op");
  1711. Value *X;
  1712. Constant *C1, *C2;
  1713. if ((match(Op0, m_Add(m_Value(X), m_Constant(C1))) &&
  1714. match(Op1, m_Sub(m_Constant(C2), m_Specific(X)))) ||
  1715. (match(Op1, m_Add(m_Value(X), m_Constant(C1))) &&
  1716. match(Op0, m_Sub(m_Constant(C2), m_Specific(X))))) {
  1717. if (ConstantExpr::getNot(C1) == C2) {
  1718. // (X + C) & (~C - X) --> (X + C) & ~(X + C) --> 0
  1719. // (X + C) | (~C - X) --> (X + C) | ~(X + C) --> -1
  1720. // (X + C) ^ (~C - X) --> (X + C) ^ ~(X + C) --> -1
  1721. Type *Ty = Op0->getType();
  1722. return Opcode == Instruction::And ? ConstantInt::getNullValue(Ty)
  1723. : ConstantInt::getAllOnesValue(Ty);
  1724. }
  1725. }
  1726. return nullptr;
  1727. }
  1728. /// Given operands for an And, see if we can fold the result.
  1729. /// If not, this returns null.
  1730. static Value *SimplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  1731. unsigned MaxRecurse) {
  1732. if (Constant *C = foldOrCommuteConstant(Instruction::And, Op0, Op1, Q))
  1733. return C;
  1734. // X & poison -> poison
  1735. if (isa<PoisonValue>(Op1))
  1736. return Op1;
  1737. // X & undef -> 0
  1738. if (Q.isUndefValue(Op1))
  1739. return Constant::getNullValue(Op0->getType());
  1740. // X & X = X
  1741. if (Op0 == Op1)
  1742. return Op0;
  1743. // X & 0 = 0
  1744. if (match(Op1, m_Zero()))
  1745. return Constant::getNullValue(Op0->getType());
  1746. // X & -1 = X
  1747. if (match(Op1, m_AllOnes()))
  1748. return Op0;
  1749. // A & ~A = ~A & A = 0
  1750. if (match(Op0, m_Not(m_Specific(Op1))) ||
  1751. match(Op1, m_Not(m_Specific(Op0))))
  1752. return Constant::getNullValue(Op0->getType());
  1753. // (A | ?) & A = A
  1754. if (match(Op0, m_c_Or(m_Specific(Op1), m_Value())))
  1755. return Op1;
  1756. // A & (A | ?) = A
  1757. if (match(Op1, m_c_Or(m_Specific(Op0), m_Value())))
  1758. return Op0;
  1759. // (X | Y) & (X | ~Y) --> X (commuted 8 ways)
  1760. Value *X, *Y;
  1761. if (match(Op0, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
  1762. match(Op1, m_c_Or(m_Deferred(X), m_Deferred(Y))))
  1763. return X;
  1764. if (match(Op1, m_c_Or(m_Value(X), m_Not(m_Value(Y)))) &&
  1765. match(Op0, m_c_Or(m_Deferred(X), m_Deferred(Y))))
  1766. return X;
  1767. if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::And))
  1768. return V;
  1769. // A mask that only clears known zeros of a shifted value is a no-op.
  1770. const APInt *Mask;
  1771. const APInt *ShAmt;
  1772. if (match(Op1, m_APInt(Mask))) {
  1773. // If all bits in the inverted and shifted mask are clear:
  1774. // and (shl X, ShAmt), Mask --> shl X, ShAmt
  1775. if (match(Op0, m_Shl(m_Value(X), m_APInt(ShAmt))) &&
  1776. (~(*Mask)).lshr(*ShAmt).isZero())
  1777. return Op0;
  1778. // If all bits in the inverted and shifted mask are clear:
  1779. // and (lshr X, ShAmt), Mask --> lshr X, ShAmt
  1780. if (match(Op0, m_LShr(m_Value(X), m_APInt(ShAmt))) &&
  1781. (~(*Mask)).shl(*ShAmt).isZero())
  1782. return Op0;
  1783. }
  1784. // If we have a multiplication overflow check that is being 'and'ed with a
  1785. // check that one of the multipliers is not zero, we can omit the 'and', and
  1786. // only keep the overflow check.
  1787. if (isCheckForZeroAndMulWithOverflow(Op0, Op1, true))
  1788. return Op1;
  1789. if (isCheckForZeroAndMulWithOverflow(Op1, Op0, true))
  1790. return Op0;
  1791. // A & (-A) = A if A is a power of two or zero.
  1792. if (match(Op0, m_Neg(m_Specific(Op1))) ||
  1793. match(Op1, m_Neg(m_Specific(Op0)))) {
  1794. if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
  1795. Q.DT))
  1796. return Op0;
  1797. if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
  1798. Q.DT))
  1799. return Op1;
  1800. }
  1801. // This is a similar pattern used for checking if a value is a power-of-2:
  1802. // (A - 1) & A --> 0 (if A is a power-of-2 or 0)
  1803. // A & (A - 1) --> 0 (if A is a power-of-2 or 0)
  1804. if (match(Op0, m_Add(m_Specific(Op1), m_AllOnes())) &&
  1805. isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
  1806. return Constant::getNullValue(Op1->getType());
  1807. if (match(Op1, m_Add(m_Specific(Op0), m_AllOnes())) &&
  1808. isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI, Q.DT))
  1809. return Constant::getNullValue(Op0->getType());
  1810. if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, true))
  1811. return V;
  1812. // Try some generic simplifications for associative operations.
  1813. if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q,
  1814. MaxRecurse))
  1815. return V;
  1816. // And distributes over Or. Try some generic simplifications based on this.
  1817. if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
  1818. Instruction::Or, Q, MaxRecurse))
  1819. return V;
  1820. // And distributes over Xor. Try some generic simplifications based on this.
  1821. if (Value *V = expandCommutativeBinOp(Instruction::And, Op0, Op1,
  1822. Instruction::Xor, Q, MaxRecurse))
  1823. return V;
  1824. if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
  1825. if (Op0->getType()->isIntOrIntVectorTy(1)) {
  1826. // A & (A && B) -> A && B
  1827. if (match(Op1, m_Select(m_Specific(Op0), m_Value(), m_Zero())))
  1828. return Op1;
  1829. else if (match(Op0, m_Select(m_Specific(Op1), m_Value(), m_Zero())))
  1830. return Op0;
  1831. }
  1832. // If the operation is with the result of a select instruction, check
  1833. // whether operating on either branch of the select always yields the same
  1834. // value.
  1835. if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q,
  1836. MaxRecurse))
  1837. return V;
  1838. }
  1839. // If the operation is with the result of a phi instruction, check whether
  1840. // operating on all incoming values of the phi always yields the same value.
  1841. if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
  1842. if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q,
  1843. MaxRecurse))
  1844. return V;
  1845. // Assuming the effective width of Y is not larger than A, i.e. all bits
  1846. // from X and Y are disjoint in (X << A) | Y,
  1847. // if the mask of this AND op covers all bits of X or Y, while it covers
  1848. // no bits from the other, we can bypass this AND op. E.g.,
  1849. // ((X << A) | Y) & Mask -> Y,
  1850. // if Mask = ((1 << effective_width_of(Y)) - 1)
  1851. // ((X << A) | Y) & Mask -> X << A,
  1852. // if Mask = ((1 << effective_width_of(X)) - 1) << A
  1853. // SimplifyDemandedBits in InstCombine can optimize the general case.
  1854. // This pattern aims to help other passes for a common case.
  1855. Value *XShifted;
  1856. if (match(Op1, m_APInt(Mask)) &&
  1857. match(Op0, m_c_Or(m_CombineAnd(m_NUWShl(m_Value(X), m_APInt(ShAmt)),
  1858. m_Value(XShifted)),
  1859. m_Value(Y)))) {
  1860. const unsigned Width = Op0->getType()->getScalarSizeInBits();
  1861. const unsigned ShftCnt = ShAmt->getLimitedValue(Width);
  1862. const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  1863. const unsigned EffWidthY = YKnown.countMaxActiveBits();
  1864. if (EffWidthY <= ShftCnt) {
  1865. const KnownBits XKnown = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI,
  1866. Q.DT);
  1867. const unsigned EffWidthX = XKnown.countMaxActiveBits();
  1868. const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY);
  1869. const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt;
  1870. // If the mask is extracting all bits from X or Y as is, we can skip
  1871. // this AND op.
  1872. if (EffBitsY.isSubsetOf(*Mask) && !EffBitsX.intersects(*Mask))
  1873. return Y;
  1874. if (EffBitsX.isSubsetOf(*Mask) && !EffBitsY.intersects(*Mask))
  1875. return XShifted;
  1876. }
  1877. }
  1878. // ((X | Y) ^ X ) & ((X | Y) ^ Y) --> 0
  1879. // ((X | Y) ^ Y ) & ((X | Y) ^ X) --> 0
  1880. BinaryOperator *Or;
  1881. if (match(Op0, m_c_Xor(m_Value(X),
  1882. m_CombineAnd(m_BinOp(Or),
  1883. m_c_Or(m_Deferred(X), m_Value(Y))))) &&
  1884. match(Op1, m_c_Xor(m_Specific(Or), m_Specific(Y))))
  1885. return Constant::getNullValue(Op0->getType());
  1886. return nullptr;
  1887. }
  1888. Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  1889. return ::SimplifyAndInst(Op0, Op1, Q, RecursionLimit);
  1890. }
  1891. static Value *simplifyOrLogic(Value *X, Value *Y) {
  1892. assert(X->getType() == Y->getType() && "Expected same type for 'or' ops");
  1893. Type *Ty = X->getType();
  1894. // X | ~X --> -1
  1895. if (match(Y, m_Not(m_Specific(X))))
  1896. return ConstantInt::getAllOnesValue(Ty);
  1897. // X | ~(X & ?) = -1
  1898. if (match(Y, m_Not(m_c_And(m_Specific(X), m_Value()))))
  1899. return ConstantInt::getAllOnesValue(Ty);
  1900. // X | (X & ?) --> X
  1901. if (match(Y, m_c_And(m_Specific(X), m_Value())))
  1902. return X;
  1903. Value *A, *B;
  1904. // (A ^ B) | (A | B) --> A | B
  1905. // (A ^ B) | (B | A) --> B | A
  1906. if (match(X, m_Xor(m_Value(A), m_Value(B))) &&
  1907. match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
  1908. return Y;
  1909. // ~(A ^ B) | (A | B) --> -1
  1910. // ~(A ^ B) | (B | A) --> -1
  1911. if (match(X, m_Not(m_Xor(m_Value(A), m_Value(B)))) &&
  1912. match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
  1913. return ConstantInt::getAllOnesValue(Ty);
  1914. // (A & ~B) | (A ^ B) --> A ^ B
  1915. // (~B & A) | (A ^ B) --> A ^ B
  1916. // (A & ~B) | (B ^ A) --> B ^ A
  1917. // (~B & A) | (B ^ A) --> B ^ A
  1918. if (match(X, m_c_And(m_Value(A), m_Not(m_Value(B)))) &&
  1919. match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
  1920. return Y;
  1921. // (~A ^ B) | (A & B) --> ~A ^ B
  1922. // (B ^ ~A) | (A & B) --> B ^ ~A
  1923. // (~A ^ B) | (B & A) --> ~A ^ B
  1924. // (B ^ ~A) | (B & A) --> B ^ ~A
  1925. if (match(X, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
  1926. match(Y, m_c_And(m_Specific(A), m_Specific(B))))
  1927. return X;
  1928. // (~A | B) | (A ^ B) --> -1
  1929. // (~A | B) | (B ^ A) --> -1
  1930. // (B | ~A) | (A ^ B) --> -1
  1931. // (B | ~A) | (B ^ A) --> -1
  1932. if (match(X, m_c_Or(m_Not(m_Value(A)), m_Value(B))) &&
  1933. match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
  1934. return ConstantInt::getAllOnesValue(Ty);
  1935. // (~A & B) | ~(A | B) --> ~A
  1936. // (~A & B) | ~(B | A) --> ~A
  1937. // (B & ~A) | ~(A | B) --> ~A
  1938. // (B & ~A) | ~(B | A) --> ~A
  1939. Value *NotA;
  1940. if (match(X,
  1941. m_c_And(m_CombineAnd(m_Value(NotA), m_NotForbidUndef(m_Value(A))),
  1942. m_Value(B))) &&
  1943. match(Y, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
  1944. return NotA;
  1945. // ~(A ^ B) | (A & B) --> ~(A ^ B)
  1946. // ~(A ^ B) | (B & A) --> ~(A ^ B)
  1947. Value *NotAB;
  1948. if (match(X, m_CombineAnd(m_NotForbidUndef(m_Xor(m_Value(A), m_Value(B))),
  1949. m_Value(NotAB))) &&
  1950. match(Y, m_c_And(m_Specific(A), m_Specific(B))))
  1951. return NotAB;
  1952. // ~(A & B) | (A ^ B) --> ~(A & B)
  1953. // ~(A & B) | (B ^ A) --> ~(A & B)
  1954. if (match(X, m_CombineAnd(m_NotForbidUndef(m_And(m_Value(A), m_Value(B))),
  1955. m_Value(NotAB))) &&
  1956. match(Y, m_c_Xor(m_Specific(A), m_Specific(B))))
  1957. return NotAB;
  1958. return nullptr;
  1959. }
  1960. /// Given operands for an Or, see if we can fold the result.
  1961. /// If not, this returns null.
  1962. static Value *SimplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  1963. unsigned MaxRecurse) {
  1964. if (Constant *C = foldOrCommuteConstant(Instruction::Or, Op0, Op1, Q))
  1965. return C;
  1966. // X | poison -> poison
  1967. if (isa<PoisonValue>(Op1))
  1968. return Op1;
  1969. // X | undef -> -1
  1970. // X | -1 = -1
  1971. // Do not return Op1 because it may contain undef elements if it's a vector.
  1972. if (Q.isUndefValue(Op1) || match(Op1, m_AllOnes()))
  1973. return Constant::getAllOnesValue(Op0->getType());
  1974. // X | X = X
  1975. // X | 0 = X
  1976. if (Op0 == Op1 || match(Op1, m_Zero()))
  1977. return Op0;
  1978. if (Value *R = simplifyOrLogic(Op0, Op1))
  1979. return R;
  1980. if (Value *R = simplifyOrLogic(Op1, Op0))
  1981. return R;
  1982. if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Or))
  1983. return V;
  1984. // Rotated -1 is still -1:
  1985. // (-1 << X) | (-1 >> (C - X)) --> -1
  1986. // (-1 >> X) | (-1 << (C - X)) --> -1
  1987. // ...with C <= bitwidth (and commuted variants).
  1988. Value *X, *Y;
  1989. if ((match(Op0, m_Shl(m_AllOnes(), m_Value(X))) &&
  1990. match(Op1, m_LShr(m_AllOnes(), m_Value(Y)))) ||
  1991. (match(Op1, m_Shl(m_AllOnes(), m_Value(X))) &&
  1992. match(Op0, m_LShr(m_AllOnes(), m_Value(Y))))) {
  1993. const APInt *C;
  1994. if ((match(X, m_Sub(m_APInt(C), m_Specific(Y))) ||
  1995. match(Y, m_Sub(m_APInt(C), m_Specific(X)))) &&
  1996. C->ule(X->getType()->getScalarSizeInBits())) {
  1997. return ConstantInt::getAllOnesValue(X->getType());
  1998. }
  1999. }
  2000. if (Value *V = simplifyAndOrOfCmps(Q, Op0, Op1, false))
  2001. return V;
  2002. // If we have a multiplication overflow check that is being 'and'ed with a
  2003. // check that one of the multipliers is not zero, we can omit the 'and', and
  2004. // only keep the overflow check.
  2005. if (isCheckForZeroAndMulWithOverflow(Op0, Op1, false))
  2006. return Op1;
  2007. if (isCheckForZeroAndMulWithOverflow(Op1, Op0, false))
  2008. return Op0;
  2009. // Try some generic simplifications for associative operations.
  2010. if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q,
  2011. MaxRecurse))
  2012. return V;
  2013. // Or distributes over And. Try some generic simplifications based on this.
  2014. if (Value *V = expandCommutativeBinOp(Instruction::Or, Op0, Op1,
  2015. Instruction::And, Q, MaxRecurse))
  2016. return V;
  2017. if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) {
  2018. if (Op0->getType()->isIntOrIntVectorTy(1)) {
  2019. // A | (A || B) -> A || B
  2020. if (match(Op1, m_Select(m_Specific(Op0), m_One(), m_Value())))
  2021. return Op1;
  2022. else if (match(Op0, m_Select(m_Specific(Op1), m_One(), m_Value())))
  2023. return Op0;
  2024. }
  2025. // If the operation is with the result of a select instruction, check
  2026. // whether operating on either branch of the select always yields the same
  2027. // value.
  2028. if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q,
  2029. MaxRecurse))
  2030. return V;
  2031. }
  2032. // (A & C1)|(B & C2)
  2033. Value *A, *B;
  2034. const APInt *C1, *C2;
  2035. if (match(Op0, m_And(m_Value(A), m_APInt(C1))) &&
  2036. match(Op1, m_And(m_Value(B), m_APInt(C2)))) {
  2037. if (*C1 == ~*C2) {
  2038. // (A & C1)|(B & C2)
  2039. // If we have: ((V + N) & C1) | (V & C2)
  2040. // .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
  2041. // replace with V+N.
  2042. Value *N;
  2043. if (C2->isMask() && // C2 == 0+1+
  2044. match(A, m_c_Add(m_Specific(B), m_Value(N)))) {
  2045. // Add commutes, try both ways.
  2046. if (MaskedValueIsZero(N, *C2, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
  2047. return A;
  2048. }
  2049. // Or commutes, try both ways.
  2050. if (C1->isMask() &&
  2051. match(B, m_c_Add(m_Specific(A), m_Value(N)))) {
  2052. // Add commutes, try both ways.
  2053. if (MaskedValueIsZero(N, *C1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
  2054. return B;
  2055. }
  2056. }
  2057. }
  2058. // If the operation is with the result of a phi instruction, check whether
  2059. // operating on all incoming values of the phi always yields the same value.
  2060. if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
  2061. if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, Q, MaxRecurse))
  2062. return V;
  2063. return nullptr;
  2064. }
  2065. Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  2066. return ::SimplifyOrInst(Op0, Op1, Q, RecursionLimit);
  2067. }
  2068. /// Given operands for a Xor, see if we can fold the result.
  2069. /// If not, this returns null.
  2070. static Value *SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q,
  2071. unsigned MaxRecurse) {
  2072. if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q))
  2073. return C;
  2074. // X ^ poison -> poison
  2075. if (isa<PoisonValue>(Op1))
  2076. return Op1;
  2077. // A ^ undef -> undef
  2078. if (Q.isUndefValue(Op1))
  2079. return Op1;
  2080. // A ^ 0 = A
  2081. if (match(Op1, m_Zero()))
  2082. return Op0;
  2083. // A ^ A = 0
  2084. if (Op0 == Op1)
  2085. return Constant::getNullValue(Op0->getType());
  2086. // A ^ ~A = ~A ^ A = -1
  2087. if (match(Op0, m_Not(m_Specific(Op1))) ||
  2088. match(Op1, m_Not(m_Specific(Op0))))
  2089. return Constant::getAllOnesValue(Op0->getType());
  2090. auto foldAndOrNot = [](Value *X, Value *Y) -> Value * {
  2091. Value *A, *B;
  2092. // (~A & B) ^ (A | B) --> A -- There are 8 commuted variants.
  2093. if (match(X, m_c_And(m_Not(m_Value(A)), m_Value(B))) &&
  2094. match(Y, m_c_Or(m_Specific(A), m_Specific(B))))
  2095. return A;
  2096. // (~A | B) ^ (A & B) --> ~A -- There are 8 commuted variants.
  2097. // The 'not' op must contain a complete -1 operand (no undef elements for
  2098. // vector) for the transform to be safe.
  2099. Value *NotA;
  2100. if (match(X,
  2101. m_c_Or(m_CombineAnd(m_NotForbidUndef(m_Value(A)), m_Value(NotA)),
  2102. m_Value(B))) &&
  2103. match(Y, m_c_And(m_Specific(A), m_Specific(B))))
  2104. return NotA;
  2105. return nullptr;
  2106. };
  2107. if (Value *R = foldAndOrNot(Op0, Op1))
  2108. return R;
  2109. if (Value *R = foldAndOrNot(Op1, Op0))
  2110. return R;
  2111. if (Value *V = simplifyLogicOfAddSub(Op0, Op1, Instruction::Xor))
  2112. return V;
  2113. // Try some generic simplifications for associative operations.
  2114. if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q,
  2115. MaxRecurse))
  2116. return V;
  2117. // Threading Xor over selects and phi nodes is pointless, so don't bother.
  2118. // Threading over the select in "A ^ select(cond, B, C)" means evaluating
  2119. // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
  2120. // only if B and C are equal. If B and C are equal then (since we assume
  2121. // that operands have already been simplified) "select(cond, B, C)" should
  2122. // have been simplified to the common value of B and C already. Analysing
  2123. // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
  2124. // for threading over phi nodes.
  2125. return nullptr;
  2126. }
  2127. Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q) {
  2128. return ::SimplifyXorInst(Op0, Op1, Q, RecursionLimit);
  2129. }
  2130. static Type *GetCompareTy(Value *Op) {
  2131. return CmpInst::makeCmpResultType(Op->getType());
  2132. }
  2133. /// Rummage around inside V looking for something equivalent to the comparison
  2134. /// "LHS Pred RHS". Return such a value if found, otherwise return null.
  2135. /// Helper function for analyzing max/min idioms.
  2136. static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred,
  2137. Value *LHS, Value *RHS) {
  2138. SelectInst *SI = dyn_cast<SelectInst>(V);
  2139. if (!SI)
  2140. return nullptr;
  2141. CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
  2142. if (!Cmp)
  2143. return nullptr;
  2144. Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
  2145. if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
  2146. return Cmp;
  2147. if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
  2148. LHS == CmpRHS && RHS == CmpLHS)
  2149. return Cmp;
  2150. return nullptr;
  2151. }
  2152. // A significant optimization not implemented here is assuming that alloca
  2153. // addresses are not equal to incoming argument values. They don't *alias*,
  2154. // as we say, but that doesn't mean they aren't equal, so we take a
  2155. // conservative approach.
  2156. //
  2157. // This is inspired in part by C++11 5.10p1:
  2158. // "Two pointers of the same type compare equal if and only if they are both
  2159. // null, both point to the same function, or both represent the same
  2160. // address."
  2161. //
  2162. // This is pretty permissive.
  2163. //
  2164. // It's also partly due to C11 6.5.9p6:
  2165. // "Two pointers compare equal if and only if both are null pointers, both are
  2166. // pointers to the same object (including a pointer to an object and a
  2167. // subobject at its beginning) or function, both are pointers to one past the
  2168. // last element of the same array object, or one is a pointer to one past the
  2169. // end of one array object and the other is a pointer to the start of a
  2170. // different array object that happens to immediately follow the first array
  2171. // object in the address space.)
  2172. //
  2173. // C11's version is more restrictive, however there's no reason why an argument
  2174. // couldn't be a one-past-the-end value for a stack object in the caller and be
  2175. // equal to the beginning of a stack object in the callee.
  2176. //
  2177. // If the C and C++ standards are ever made sufficiently restrictive in this
  2178. // area, it may be possible to update LLVM's semantics accordingly and reinstate
  2179. // this optimization.
  2180. static Constant *
  2181. computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
  2182. const SimplifyQuery &Q) {
  2183. const DataLayout &DL = Q.DL;
  2184. const TargetLibraryInfo *TLI = Q.TLI;
  2185. const DominatorTree *DT = Q.DT;
  2186. const Instruction *CxtI = Q.CxtI;
  2187. const InstrInfoQuery &IIQ = Q.IIQ;
  2188. // First, skip past any trivial no-ops.
  2189. LHS = LHS->stripPointerCasts();
  2190. RHS = RHS->stripPointerCasts();
  2191. // A non-null pointer is not equal to a null pointer.
  2192. if (isa<ConstantPointerNull>(RHS) && ICmpInst::isEquality(Pred) &&
  2193. llvm::isKnownNonZero(LHS, DL, 0, nullptr, nullptr, nullptr,
  2194. IIQ.UseInstrInfo))
  2195. return ConstantInt::get(GetCompareTy(LHS),
  2196. !CmpInst::isTrueWhenEqual(Pred));
  2197. // We can only fold certain predicates on pointer comparisons.
  2198. switch (Pred) {
  2199. default:
  2200. return nullptr;
  2201. // Equality comaprisons are easy to fold.
  2202. case CmpInst::ICMP_EQ:
  2203. case CmpInst::ICMP_NE:
  2204. break;
  2205. // We can only handle unsigned relational comparisons because 'inbounds' on
  2206. // a GEP only protects against unsigned wrapping.
  2207. case CmpInst::ICMP_UGT:
  2208. case CmpInst::ICMP_UGE:
  2209. case CmpInst::ICMP_ULT:
  2210. case CmpInst::ICMP_ULE:
  2211. // However, we have to switch them to their signed variants to handle
  2212. // negative indices from the base pointer.
  2213. Pred = ICmpInst::getSignedPredicate(Pred);
  2214. break;
  2215. }
  2216. // Strip off any constant offsets so that we can reason about them.
  2217. // It's tempting to use getUnderlyingObject or even just stripInBoundsOffsets
  2218. // here and compare base addresses like AliasAnalysis does, however there are
  2219. // numerous hazards. AliasAnalysis and its utilities rely on special rules
  2220. // governing loads and stores which don't apply to icmps. Also, AliasAnalysis
  2221. // doesn't need to guarantee pointer inequality when it says NoAlias.
  2222. // Even if an non-inbounds GEP occurs along the path we can still optimize
  2223. // equality comparisons concerning the result.
  2224. bool AllowNonInbounds = ICmpInst::isEquality(Pred);
  2225. Constant *LHSOffset =
  2226. stripAndComputeConstantOffsets(DL, LHS, AllowNonInbounds);
  2227. Constant *RHSOffset =
  2228. stripAndComputeConstantOffsets(DL, RHS, AllowNonInbounds);
  2229. // If LHS and RHS are related via constant offsets to the same base
  2230. // value, we can replace it with an icmp which just compares the offsets.
  2231. if (LHS == RHS)
  2232. return ConstantExpr::getICmp(Pred, LHSOffset, RHSOffset);
  2233. // Various optimizations for (in)equality comparisons.
  2234. if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
  2235. // Different non-empty allocations that exist at the same time have
  2236. // different addresses (if the program can tell). Global variables always
  2237. // exist, so they always exist during the lifetime of each other and all
  2238. // allocas. Two different allocas usually have different addresses...
  2239. //
  2240. // However, if there's an @llvm.stackrestore dynamically in between two
  2241. // allocas, they may have the same address. It's tempting to reduce the
  2242. // scope of the problem by only looking at *static* allocas here. That would
  2243. // cover the majority of allocas while significantly reducing the likelihood
  2244. // of having an @llvm.stackrestore pop up in the middle. However, it's not
  2245. // actually impossible for an @llvm.stackrestore to pop up in the middle of
  2246. // an entry block. Also, if we have a block that's not attached to a
  2247. // function, we can't tell if it's "static" under the current definition.
  2248. // Theoretically, this problem could be fixed by creating a new kind of
  2249. // instruction kind specifically for static allocas. Such a new instruction
  2250. // could be required to be at the top of the entry block, thus preventing it
  2251. // from being subject to a @llvm.stackrestore. Instcombine could even
  2252. // convert regular allocas into these special allocas. It'd be nifty.
  2253. // However, until then, this problem remains open.
  2254. //
  2255. // So, we'll assume that two non-empty allocas have different addresses
  2256. // for now.
  2257. //
  2258. // With all that, if the offsets are within the bounds of their allocations
  2259. // (and not one-past-the-end! so we can't use inbounds!), and their
  2260. // allocations aren't the same, the pointers are not equal.
  2261. //
  2262. // Note that it's not necessary to check for LHS being a global variable
  2263. // address, due to canonicalization and constant folding.
  2264. if (isa<AllocaInst>(LHS) &&
  2265. (isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
  2266. ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset);
  2267. ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset);
  2268. uint64_t LHSSize, RHSSize;
  2269. ObjectSizeOpts Opts;
  2270. Opts.NullIsUnknownSize =
  2271. NullPointerIsDefined(cast<AllocaInst>(LHS)->getFunction());
  2272. if (LHSOffsetCI && RHSOffsetCI &&
  2273. getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
  2274. getObjectSize(RHS, RHSSize, DL, TLI, Opts)) {
  2275. const APInt &LHSOffsetValue = LHSOffsetCI->getValue();
  2276. const APInt &RHSOffsetValue = RHSOffsetCI->getValue();
  2277. if (!LHSOffsetValue.isNegative() &&
  2278. !RHSOffsetValue.isNegative() &&
  2279. LHSOffsetValue.ult(LHSSize) &&
  2280. RHSOffsetValue.ult(RHSSize)) {
  2281. return ConstantInt::get(GetCompareTy(LHS),
  2282. !CmpInst::isTrueWhenEqual(Pred));
  2283. }
  2284. }
  2285. // Repeat the above check but this time without depending on DataLayout
  2286. // or being able to compute a precise size.
  2287. if (!cast<PointerType>(LHS->getType())->isEmptyTy() &&
  2288. !cast<PointerType>(RHS->getType())->isEmptyTy() &&
  2289. LHSOffset->isNullValue() &&
  2290. RHSOffset->isNullValue())
  2291. return ConstantInt::get(GetCompareTy(LHS),
  2292. !CmpInst::isTrueWhenEqual(Pred));
  2293. }
  2294. // If one side of the equality comparison must come from a noalias call
  2295. // (meaning a system memory allocation function), and the other side must
  2296. // come from a pointer that cannot overlap with dynamically-allocated
  2297. // memory within the lifetime of the current function (allocas, byval
  2298. // arguments, globals), then determine the comparison result here.
  2299. SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
  2300. getUnderlyingObjects(LHS, LHSUObjs);
  2301. getUnderlyingObjects(RHS, RHSUObjs);
  2302. // Is the set of underlying objects all noalias calls?
  2303. auto IsNAC = [](ArrayRef<const Value *> Objects) {
  2304. return all_of(Objects, isNoAliasCall);
  2305. };
  2306. // Is the set of underlying objects all things which must be disjoint from
  2307. // noalias calls. For allocas, we consider only static ones (dynamic
  2308. // allocas might be transformed into calls to malloc not simultaneously
  2309. // live with the compared-to allocation). For globals, we exclude symbols
  2310. // that might be resolve lazily to symbols in another dynamically-loaded
  2311. // library (and, thus, could be malloc'ed by the implementation).
  2312. auto IsAllocDisjoint = [](ArrayRef<const Value *> Objects) {
  2313. return all_of(Objects, [](const Value *V) {
  2314. if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
  2315. return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
  2316. if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
  2317. return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() ||
  2318. GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) &&
  2319. !GV->isThreadLocal();
  2320. if (const Argument *A = dyn_cast<Argument>(V))
  2321. return A->hasByValAttr();
  2322. return false;
  2323. });
  2324. };
  2325. if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) ||
  2326. (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs)))
  2327. return ConstantInt::get(GetCompareTy(LHS),
  2328. !CmpInst::isTrueWhenEqual(Pred));
  2329. // Fold comparisons for non-escaping pointer even if the allocation call
  2330. // cannot be elided. We cannot fold malloc comparison to null. Also, the
  2331. // dynamic allocation call could be either of the operands. Note that
  2332. // the other operand can not be based on the alloc - if it were, then
  2333. // the cmp itself would be a capture.
  2334. Value *MI = nullptr;
  2335. if (isAllocLikeFn(LHS, TLI) &&
  2336. llvm::isKnownNonZero(RHS, DL, 0, nullptr, CxtI, DT))
  2337. MI = LHS;
  2338. else if (isAllocLikeFn(RHS, TLI) &&
  2339. llvm::isKnownNonZero(LHS, DL, 0, nullptr, CxtI, DT))
  2340. MI = RHS;
  2341. // FIXME: We should also fold the compare when the pointer escapes, but the
  2342. // compare dominates the pointer escape
  2343. if (MI && !PointerMayBeCaptured(MI, true, true))
  2344. return ConstantInt::get(GetCompareTy(LHS),
  2345. CmpInst::isFalseWhenEqual(Pred));
  2346. }
  2347. // Otherwise, fail.
  2348. return nullptr;
  2349. }
  2350. /// Fold an icmp when its operands have i1 scalar type.
  2351. static Value *simplifyICmpOfBools(CmpInst::Predicate Pred, Value *LHS,
  2352. Value *RHS, const SimplifyQuery &Q) {
  2353. Type *ITy = GetCompareTy(LHS); // The return type.
  2354. Type *OpTy = LHS->getType(); // The operand type.
  2355. if (!OpTy->isIntOrIntVectorTy(1))
  2356. return nullptr;
  2357. // A boolean compared to true/false can be reduced in 14 out of the 20
  2358. // (10 predicates * 2 constants) possible combinations. The other
  2359. // 6 cases require a 'not' of the LHS.
  2360. auto ExtractNotLHS = [](Value *V) -> Value * {
  2361. Value *X;
  2362. if (match(V, m_Not(m_Value(X))))
  2363. return X;
  2364. return nullptr;
  2365. };
  2366. if (match(RHS, m_Zero())) {
  2367. switch (Pred) {
  2368. case CmpInst::ICMP_NE: // X != 0 -> X
  2369. case CmpInst::ICMP_UGT: // X >u 0 -> X
  2370. case CmpInst::ICMP_SLT: // X <s 0 -> X
  2371. return LHS;
  2372. case CmpInst::ICMP_EQ: // not(X) == 0 -> X != 0 -> X
  2373. case CmpInst::ICMP_ULE: // not(X) <=u 0 -> X >u 0 -> X
  2374. case CmpInst::ICMP_SGE: // not(X) >=s 0 -> X <s 0 -> X
  2375. if (Value *X = ExtractNotLHS(LHS))
  2376. return X;
  2377. break;
  2378. case CmpInst::ICMP_ULT: // X <u 0 -> false
  2379. case CmpInst::ICMP_SGT: // X >s 0 -> false
  2380. return getFalse(ITy);
  2381. case CmpInst::ICMP_UGE: // X >=u 0 -> true
  2382. case CmpInst::ICMP_SLE: // X <=s 0 -> true
  2383. return getTrue(ITy);
  2384. default: break;
  2385. }
  2386. } else if (match(RHS, m_One())) {
  2387. switch (Pred) {
  2388. case CmpInst::ICMP_EQ: // X == 1 -> X
  2389. case CmpInst::ICMP_UGE: // X >=u 1 -> X
  2390. case CmpInst::ICMP_SLE: // X <=s -1 -> X
  2391. return LHS;
  2392. case CmpInst::ICMP_NE: // not(X) != 1 -> X == 1 -> X
  2393. case CmpInst::ICMP_ULT: // not(X) <=u 1 -> X >=u 1 -> X
  2394. case CmpInst::ICMP_SGT: // not(X) >s 1 -> X <=s -1 -> X
  2395. if (Value *X = ExtractNotLHS(LHS))
  2396. return X;
  2397. break;
  2398. case CmpInst::ICMP_UGT: // X >u 1 -> false
  2399. case CmpInst::ICMP_SLT: // X <s -1 -> false
  2400. return getFalse(ITy);
  2401. case CmpInst::ICMP_ULE: // X <=u 1 -> true
  2402. case CmpInst::ICMP_SGE: // X >=s -1 -> true
  2403. return getTrue(ITy);
  2404. default: break;
  2405. }
  2406. }
  2407. switch (Pred) {
  2408. default:
  2409. break;
  2410. case ICmpInst::ICMP_UGE:
  2411. if (isImpliedCondition(RHS, LHS, Q.DL).getValueOr(false))
  2412. return getTrue(ITy);
  2413. break;
  2414. case ICmpInst::ICMP_SGE:
  2415. /// For signed comparison, the values for an i1 are 0 and -1
  2416. /// respectively. This maps into a truth table of:
  2417. /// LHS | RHS | LHS >=s RHS | LHS implies RHS
  2418. /// 0 | 0 | 1 (0 >= 0) | 1
  2419. /// 0 | 1 | 1 (0 >= -1) | 1
  2420. /// 1 | 0 | 0 (-1 >= 0) | 0
  2421. /// 1 | 1 | 1 (-1 >= -1) | 1
  2422. if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false))
  2423. return getTrue(ITy);
  2424. break;
  2425. case ICmpInst::ICMP_ULE:
  2426. if (isImpliedCondition(LHS, RHS, Q.DL).getValueOr(false))
  2427. return getTrue(ITy);
  2428. break;
  2429. }
  2430. return nullptr;
  2431. }
  2432. /// Try hard to fold icmp with zero RHS because this is a common case.
  2433. static Value *simplifyICmpWithZero(CmpInst::Predicate Pred, Value *LHS,
  2434. Value *RHS, const SimplifyQuery &Q) {
  2435. if (!match(RHS, m_Zero()))
  2436. return nullptr;
  2437. Type *ITy = GetCompareTy(LHS); // The return type.
  2438. switch (Pred) {
  2439. default:
  2440. llvm_unreachable("Unknown ICmp predicate!");
  2441. case ICmpInst::ICMP_ULT:
  2442. return getFalse(ITy);
  2443. case ICmpInst::ICMP_UGE:
  2444. return getTrue(ITy);
  2445. case ICmpInst::ICMP_EQ:
  2446. case ICmpInst::ICMP_ULE:
  2447. if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
  2448. return getFalse(ITy);
  2449. break;
  2450. case ICmpInst::ICMP_NE:
  2451. case ICmpInst::ICMP_UGT:
  2452. if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo))
  2453. return getTrue(ITy);
  2454. break;
  2455. case ICmpInst::ICMP_SLT: {
  2456. KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2457. if (LHSKnown.isNegative())
  2458. return getTrue(ITy);
  2459. if (LHSKnown.isNonNegative())
  2460. return getFalse(ITy);
  2461. break;
  2462. }
  2463. case ICmpInst::ICMP_SLE: {
  2464. KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2465. if (LHSKnown.isNegative())
  2466. return getTrue(ITy);
  2467. if (LHSKnown.isNonNegative() &&
  2468. isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
  2469. return getFalse(ITy);
  2470. break;
  2471. }
  2472. case ICmpInst::ICMP_SGE: {
  2473. KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2474. if (LHSKnown.isNegative())
  2475. return getFalse(ITy);
  2476. if (LHSKnown.isNonNegative())
  2477. return getTrue(ITy);
  2478. break;
  2479. }
  2480. case ICmpInst::ICMP_SGT: {
  2481. KnownBits LHSKnown = computeKnownBits(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2482. if (LHSKnown.isNegative())
  2483. return getFalse(ITy);
  2484. if (LHSKnown.isNonNegative() &&
  2485. isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
  2486. return getTrue(ITy);
  2487. break;
  2488. }
  2489. }
  2490. return nullptr;
  2491. }
  2492. static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
  2493. Value *RHS, const InstrInfoQuery &IIQ) {
  2494. Type *ITy = GetCompareTy(RHS); // The return type.
  2495. Value *X;
  2496. // Sign-bit checks can be optimized to true/false after unsigned
  2497. // floating-point casts:
  2498. // icmp slt (bitcast (uitofp X)), 0 --> false
  2499. // icmp sgt (bitcast (uitofp X)), -1 --> true
  2500. if (match(LHS, m_BitCast(m_UIToFP(m_Value(X))))) {
  2501. if (Pred == ICmpInst::ICMP_SLT && match(RHS, m_Zero()))
  2502. return ConstantInt::getFalse(ITy);
  2503. if (Pred == ICmpInst::ICMP_SGT && match(RHS, m_AllOnes()))
  2504. return ConstantInt::getTrue(ITy);
  2505. }
  2506. const APInt *C;
  2507. if (!match(RHS, m_APIntAllowUndef(C)))
  2508. return nullptr;
  2509. // Rule out tautological comparisons (eg., ult 0 or uge 0).
  2510. ConstantRange RHS_CR = ConstantRange::makeExactICmpRegion(Pred, *C);
  2511. if (RHS_CR.isEmptySet())
  2512. return ConstantInt::getFalse(ITy);
  2513. if (RHS_CR.isFullSet())
  2514. return ConstantInt::getTrue(ITy);
  2515. ConstantRange LHS_CR =
  2516. computeConstantRange(LHS, CmpInst::isSigned(Pred), IIQ.UseInstrInfo);
  2517. if (!LHS_CR.isFullSet()) {
  2518. if (RHS_CR.contains(LHS_CR))
  2519. return ConstantInt::getTrue(ITy);
  2520. if (RHS_CR.inverse().contains(LHS_CR))
  2521. return ConstantInt::getFalse(ITy);
  2522. }
  2523. // (mul nuw/nsw X, MulC) != C --> true (if C is not a multiple of MulC)
  2524. // (mul nuw/nsw X, MulC) == C --> false (if C is not a multiple of MulC)
  2525. const APInt *MulC;
  2526. if (ICmpInst::isEquality(Pred) &&
  2527. ((match(LHS, m_NUWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
  2528. *MulC != 0 && C->urem(*MulC) != 0) ||
  2529. (match(LHS, m_NSWMul(m_Value(), m_APIntAllowUndef(MulC))) &&
  2530. *MulC != 0 && C->srem(*MulC) != 0)))
  2531. return ConstantInt::get(ITy, Pred == ICmpInst::ICMP_NE);
  2532. return nullptr;
  2533. }
  2534. static Value *simplifyICmpWithBinOpOnLHS(
  2535. CmpInst::Predicate Pred, BinaryOperator *LBO, Value *RHS,
  2536. const SimplifyQuery &Q, unsigned MaxRecurse) {
  2537. Type *ITy = GetCompareTy(RHS); // The return type.
  2538. Value *Y = nullptr;
  2539. // icmp pred (or X, Y), X
  2540. if (match(LBO, m_c_Or(m_Value(Y), m_Specific(RHS)))) {
  2541. if (Pred == ICmpInst::ICMP_ULT)
  2542. return getFalse(ITy);
  2543. if (Pred == ICmpInst::ICMP_UGE)
  2544. return getTrue(ITy);
  2545. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGE) {
  2546. KnownBits RHSKnown = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2547. KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2548. if (RHSKnown.isNonNegative() && YKnown.isNegative())
  2549. return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
  2550. if (RHSKnown.isNegative() || YKnown.isNonNegative())
  2551. return Pred == ICmpInst::ICMP_SLT ? getFalse(ITy) : getTrue(ITy);
  2552. }
  2553. }
  2554. // icmp pred (and X, Y), X
  2555. if (match(LBO, m_c_And(m_Value(), m_Specific(RHS)))) {
  2556. if (Pred == ICmpInst::ICMP_UGT)
  2557. return getFalse(ITy);
  2558. if (Pred == ICmpInst::ICMP_ULE)
  2559. return getTrue(ITy);
  2560. }
  2561. // icmp pred (urem X, Y), Y
  2562. if (match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
  2563. switch (Pred) {
  2564. default:
  2565. break;
  2566. case ICmpInst::ICMP_SGT:
  2567. case ICmpInst::ICMP_SGE: {
  2568. KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2569. if (!Known.isNonNegative())
  2570. break;
  2571. LLVM_FALLTHROUGH;
  2572. }
  2573. case ICmpInst::ICMP_EQ:
  2574. case ICmpInst::ICMP_UGT:
  2575. case ICmpInst::ICMP_UGE:
  2576. return getFalse(ITy);
  2577. case ICmpInst::ICMP_SLT:
  2578. case ICmpInst::ICMP_SLE: {
  2579. KnownBits Known = computeKnownBits(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
  2580. if (!Known.isNonNegative())
  2581. break;
  2582. LLVM_FALLTHROUGH;
  2583. }
  2584. case ICmpInst::ICMP_NE:
  2585. case ICmpInst::ICMP_ULT:
  2586. case ICmpInst::ICMP_ULE:
  2587. return getTrue(ITy);
  2588. }
  2589. }
  2590. // icmp pred (urem X, Y), X
  2591. if (match(LBO, m_URem(m_Specific(RHS), m_Value()))) {
  2592. if (Pred == ICmpInst::ICMP_ULE)
  2593. return getTrue(ITy);
  2594. if (Pred == ICmpInst::ICMP_UGT)
  2595. return getFalse(ITy);
  2596. }
  2597. // x >>u y <=u x --> true.
  2598. // x >>u y >u x --> false.
  2599. // x udiv y <=u x --> true.
  2600. // x udiv y >u x --> false.
  2601. if (match(LBO, m_LShr(m_Specific(RHS), m_Value())) ||
  2602. match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) {
  2603. // icmp pred (X op Y), X
  2604. if (Pred == ICmpInst::ICMP_UGT)
  2605. return getFalse(ITy);
  2606. if (Pred == ICmpInst::ICMP_ULE)
  2607. return getTrue(ITy);
  2608. }
  2609. // If x is nonzero:
  2610. // x >>u C <u x --> true for C != 0.
  2611. // x >>u C != x --> true for C != 0.
  2612. // x >>u C >=u x --> false for C != 0.
  2613. // x >>u C == x --> false for C != 0.
  2614. // x udiv C <u x --> true for C != 1.
  2615. // x udiv C != x --> true for C != 1.
  2616. // x udiv C >=u x --> false for C != 1.
  2617. // x udiv C == x --> false for C != 1.
  2618. // TODO: allow non-constant shift amount/divisor
  2619. const APInt *C;
  2620. if ((match(LBO, m_LShr(m_Specific(RHS), m_APInt(C))) && *C != 0) ||
  2621. (match(LBO, m_UDiv(m_Specific(RHS), m_APInt(C))) && *C != 1)) {
  2622. if (isKnownNonZero(RHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) {
  2623. switch (Pred) {
  2624. default:
  2625. break;
  2626. case ICmpInst::ICMP_EQ:
  2627. case ICmpInst::ICMP_UGE:
  2628. return getFalse(ITy);
  2629. case ICmpInst::ICMP_NE:
  2630. case ICmpInst::ICMP_ULT:
  2631. return getTrue(ITy);
  2632. case ICmpInst::ICMP_UGT:
  2633. case ICmpInst::ICMP_ULE:
  2634. // UGT/ULE are handled by the more general case just above
  2635. llvm_unreachable("Unexpected UGT/ULE, should have been handled");
  2636. }
  2637. }
  2638. }
  2639. // (x*C1)/C2 <= x for C1 <= C2.
  2640. // This holds even if the multiplication overflows: Assume that x != 0 and
  2641. // arithmetic is modulo M. For overflow to occur we must have C1 >= M/x and
  2642. // thus C2 >= M/x. It follows that (x*C1)/C2 <= (M-1)/C2 <= ((M-1)*x)/M < x.
  2643. //
  2644. // Additionally, either the multiplication and division might be represented
  2645. // as shifts:
  2646. // (x*C1)>>C2 <= x for C1 < 2**C2.
  2647. // (x<<C1)/C2 <= x for 2**C1 < C2.
  2648. const APInt *C1, *C2;
  2649. if ((match(LBO, m_UDiv(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
  2650. C1->ule(*C2)) ||
  2651. (match(LBO, m_LShr(m_Mul(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
  2652. C1->ule(APInt(C2->getBitWidth(), 1) << *C2)) ||
  2653. (match(LBO, m_UDiv(m_Shl(m_Specific(RHS), m_APInt(C1)), m_APInt(C2))) &&
  2654. (APInt(C1->getBitWidth(), 1) << *C1).ule(*C2))) {
  2655. if (Pred == ICmpInst::ICMP_UGT)
  2656. return getFalse(ITy);
  2657. if (Pred == ICmpInst::ICMP_ULE)
  2658. return getTrue(ITy);
  2659. }
  2660. return nullptr;
  2661. }
  2662. // If only one of the icmp's operands has NSW flags, try to prove that:
  2663. //
  2664. // icmp slt (x + C1), (x +nsw C2)
  2665. //
  2666. // is equivalent to:
  2667. //
  2668. // icmp slt C1, C2
  2669. //
  2670. // which is true if x + C2 has the NSW flags set and:
  2671. // *) C1 < C2 && C1 >= 0, or
  2672. // *) C2 < C1 && C1 <= 0.
  2673. //
  2674. static bool trySimplifyICmpWithAdds(CmpInst::Predicate Pred, Value *LHS,
  2675. Value *RHS) {
  2676. // TODO: only support icmp slt for now.
  2677. if (Pred != CmpInst::ICMP_SLT)
  2678. return false;
  2679. // Canonicalize nsw add as RHS.
  2680. if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
  2681. std::swap(LHS, RHS);
  2682. if (!match(RHS, m_NSWAdd(m_Value(), m_Value())))
  2683. return false;
  2684. Value *X;
  2685. const APInt *C1, *C2;
  2686. if (!match(LHS, m_c_Add(m_Value(X), m_APInt(C1))) ||
  2687. !match(RHS, m_c_Add(m_Specific(X), m_APInt(C2))))
  2688. return false;
  2689. return (C1->slt(*C2) && C1->isNonNegative()) ||
  2690. (C2->slt(*C1) && C1->isNonPositive());
  2691. }
  2692. /// TODO: A large part of this logic is duplicated in InstCombine's
  2693. /// foldICmpBinOp(). We should be able to share that and avoid the code
  2694. /// duplication.
  2695. static Value *simplifyICmpWithBinOp(CmpInst::Predicate Pred, Value *LHS,
  2696. Value *RHS, const SimplifyQuery &Q,
  2697. unsigned MaxRecurse) {
  2698. BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
  2699. BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
  2700. if (MaxRecurse && (LBO || RBO)) {
  2701. // Analyze the case when either LHS or RHS is an add instruction.
  2702. Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
  2703. // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
  2704. bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
  2705. if (LBO && LBO->getOpcode() == Instruction::Add) {
  2706. A = LBO->getOperand(0);
  2707. B = LBO->getOperand(1);
  2708. NoLHSWrapProblem =
  2709. ICmpInst::isEquality(Pred) ||
  2710. (CmpInst::isUnsigned(Pred) &&
  2711. Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO))) ||
  2712. (CmpInst::isSigned(Pred) &&
  2713. Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)));
  2714. }
  2715. if (RBO && RBO->getOpcode() == Instruction::Add) {
  2716. C = RBO->getOperand(0);
  2717. D = RBO->getOperand(1);
  2718. NoRHSWrapProblem =
  2719. ICmpInst::isEquality(Pred) ||
  2720. (CmpInst::isUnsigned(Pred) &&
  2721. Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(RBO))) ||
  2722. (CmpInst::isSigned(Pred) &&
  2723. Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RBO)));
  2724. }
  2725. // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
  2726. if ((A == RHS || B == RHS) && NoLHSWrapProblem)
  2727. if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A,
  2728. Constant::getNullValue(RHS->getType()), Q,
  2729. MaxRecurse - 1))
  2730. return V;
  2731. // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
  2732. if ((C == LHS || D == LHS) && NoRHSWrapProblem)
  2733. if (Value *V =
  2734. SimplifyICmpInst(Pred, Constant::getNullValue(LHS->getType()),
  2735. C == LHS ? D : C, Q, MaxRecurse - 1))
  2736. return V;
  2737. // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
  2738. bool CanSimplify = (NoLHSWrapProblem && NoRHSWrapProblem) ||
  2739. trySimplifyICmpWithAdds(Pred, LHS, RHS);
  2740. if (A && C && (A == C || A == D || B == C || B == D) && CanSimplify) {
  2741. // Determine Y and Z in the form icmp (X+Y), (X+Z).
  2742. Value *Y, *Z;
  2743. if (A == C) {
  2744. // C + B == C + D -> B == D
  2745. Y = B;
  2746. Z = D;
  2747. } else if (A == D) {
  2748. // D + B == C + D -> B == C
  2749. Y = B;
  2750. Z = C;
  2751. } else if (B == C) {
  2752. // A + C == C + D -> A == D
  2753. Y = A;
  2754. Z = D;
  2755. } else {
  2756. assert(B == D);
  2757. // A + D == C + D -> A == C
  2758. Y = A;
  2759. Z = C;
  2760. }
  2761. if (Value *V = SimplifyICmpInst(Pred, Y, Z, Q, MaxRecurse - 1))
  2762. return V;
  2763. }
  2764. }
  2765. if (LBO)
  2766. if (Value *V = simplifyICmpWithBinOpOnLHS(Pred, LBO, RHS, Q, MaxRecurse))
  2767. return V;
  2768. if (RBO)
  2769. if (Value *V = simplifyICmpWithBinOpOnLHS(
  2770. ICmpInst::getSwappedPredicate(Pred), RBO, LHS, Q, MaxRecurse))
  2771. return V;
  2772. // 0 - (zext X) pred C
  2773. if (!CmpInst::isUnsigned(Pred) && match(LHS, m_Neg(m_ZExt(m_Value())))) {
  2774. const APInt *C;
  2775. if (match(RHS, m_APInt(C))) {
  2776. if (C->isStrictlyPositive()) {
  2777. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_NE)
  2778. return ConstantInt::getTrue(GetCompareTy(RHS));
  2779. if (Pred == ICmpInst::ICMP_SGE || Pred == ICmpInst::ICMP_EQ)
  2780. return ConstantInt::getFalse(GetCompareTy(RHS));
  2781. }
  2782. if (C->isNonNegative()) {
  2783. if (Pred == ICmpInst::ICMP_SLE)
  2784. return ConstantInt::getTrue(GetCompareTy(RHS));
  2785. if (Pred == ICmpInst::ICMP_SGT)
  2786. return ConstantInt::getFalse(GetCompareTy(RHS));
  2787. }
  2788. }
  2789. }
  2790. // If C2 is a power-of-2 and C is not:
  2791. // (C2 << X) == C --> false
  2792. // (C2 << X) != C --> true
  2793. const APInt *C;
  2794. if (match(LHS, m_Shl(m_Power2(), m_Value())) &&
  2795. match(RHS, m_APIntAllowUndef(C)) && !C->isPowerOf2()) {
  2796. // C2 << X can equal zero in some circumstances.
  2797. // This simplification might be unsafe if C is zero.
  2798. //
  2799. // We know it is safe if:
  2800. // - The shift is nsw. We can't shift out the one bit.
  2801. // - The shift is nuw. We can't shift out the one bit.
  2802. // - C2 is one.
  2803. // - C isn't zero.
  2804. if (Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
  2805. Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(LBO)) ||
  2806. match(LHS, m_Shl(m_One(), m_Value())) || !C->isZero()) {
  2807. if (Pred == ICmpInst::ICMP_EQ)
  2808. return ConstantInt::getFalse(GetCompareTy(RHS));
  2809. if (Pred == ICmpInst::ICMP_NE)
  2810. return ConstantInt::getTrue(GetCompareTy(RHS));
  2811. }
  2812. }
  2813. // TODO: This is overly constrained. LHS can be any power-of-2.
  2814. // (1 << X) >u 0x8000 --> false
  2815. // (1 << X) <=u 0x8000 --> true
  2816. if (match(LHS, m_Shl(m_One(), m_Value())) && match(RHS, m_SignMask())) {
  2817. if (Pred == ICmpInst::ICMP_UGT)
  2818. return ConstantInt::getFalse(GetCompareTy(RHS));
  2819. if (Pred == ICmpInst::ICMP_ULE)
  2820. return ConstantInt::getTrue(GetCompareTy(RHS));
  2821. }
  2822. if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() &&
  2823. LBO->getOperand(1) == RBO->getOperand(1)) {
  2824. switch (LBO->getOpcode()) {
  2825. default:
  2826. break;
  2827. case Instruction::UDiv:
  2828. case Instruction::LShr:
  2829. if (ICmpInst::isSigned(Pred) || !Q.IIQ.isExact(LBO) ||
  2830. !Q.IIQ.isExact(RBO))
  2831. break;
  2832. if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
  2833. RBO->getOperand(0), Q, MaxRecurse - 1))
  2834. return V;
  2835. break;
  2836. case Instruction::SDiv:
  2837. if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) ||
  2838. !Q.IIQ.isExact(RBO))
  2839. break;
  2840. if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
  2841. RBO->getOperand(0), Q, MaxRecurse - 1))
  2842. return V;
  2843. break;
  2844. case Instruction::AShr:
  2845. if (!Q.IIQ.isExact(LBO) || !Q.IIQ.isExact(RBO))
  2846. break;
  2847. if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
  2848. RBO->getOperand(0), Q, MaxRecurse - 1))
  2849. return V;
  2850. break;
  2851. case Instruction::Shl: {
  2852. bool NUW = Q.IIQ.hasNoUnsignedWrap(LBO) && Q.IIQ.hasNoUnsignedWrap(RBO);
  2853. bool NSW = Q.IIQ.hasNoSignedWrap(LBO) && Q.IIQ.hasNoSignedWrap(RBO);
  2854. if (!NUW && !NSW)
  2855. break;
  2856. if (!NSW && ICmpInst::isSigned(Pred))
  2857. break;
  2858. if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
  2859. RBO->getOperand(0), Q, MaxRecurse - 1))
  2860. return V;
  2861. break;
  2862. }
  2863. }
  2864. }
  2865. return nullptr;
  2866. }
  2867. /// Simplify integer comparisons where at least one operand of the compare
  2868. /// matches an integer min/max idiom.
  2869. static Value *simplifyICmpWithMinMax(CmpInst::Predicate Pred, Value *LHS,
  2870. Value *RHS, const SimplifyQuery &Q,
  2871. unsigned MaxRecurse) {
  2872. Type *ITy = GetCompareTy(LHS); // The return type.
  2873. Value *A, *B;
  2874. CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
  2875. CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
  2876. // Signed variants on "max(a,b)>=a -> true".
  2877. if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
  2878. if (A != RHS)
  2879. std::swap(A, B); // smax(A, B) pred A.
  2880. EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
  2881. // We analyze this as smax(A, B) pred A.
  2882. P = Pred;
  2883. } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
  2884. (A == LHS || B == LHS)) {
  2885. if (A != LHS)
  2886. std::swap(A, B); // A pred smax(A, B).
  2887. EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
  2888. // We analyze this as smax(A, B) swapped-pred A.
  2889. P = CmpInst::getSwappedPredicate(Pred);
  2890. } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
  2891. (A == RHS || B == RHS)) {
  2892. if (A != RHS)
  2893. std::swap(A, B); // smin(A, B) pred A.
  2894. EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
  2895. // We analyze this as smax(-A, -B) swapped-pred -A.
  2896. // Note that we do not need to actually form -A or -B thanks to EqP.
  2897. P = CmpInst::getSwappedPredicate(Pred);
  2898. } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
  2899. (A == LHS || B == LHS)) {
  2900. if (A != LHS)
  2901. std::swap(A, B); // A pred smin(A, B).
  2902. EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
  2903. // We analyze this as smax(-A, -B) pred -A.
  2904. // Note that we do not need to actually form -A or -B thanks to EqP.
  2905. P = Pred;
  2906. }
  2907. if (P != CmpInst::BAD_ICMP_PREDICATE) {
  2908. // Cases correspond to "max(A, B) p A".
  2909. switch (P) {
  2910. default:
  2911. break;
  2912. case CmpInst::ICMP_EQ:
  2913. case CmpInst::ICMP_SLE:
  2914. // Equivalent to "A EqP B". This may be the same as the condition tested
  2915. // in the max/min; if so, we can just return that.
  2916. if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
  2917. return V;
  2918. if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
  2919. return V;
  2920. // Otherwise, see if "A EqP B" simplifies.
  2921. if (MaxRecurse)
  2922. if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
  2923. return V;
  2924. break;
  2925. case CmpInst::ICMP_NE:
  2926. case CmpInst::ICMP_SGT: {
  2927. CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
  2928. // Equivalent to "A InvEqP B". This may be the same as the condition
  2929. // tested in the max/min; if so, we can just return that.
  2930. if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
  2931. return V;
  2932. if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
  2933. return V;
  2934. // Otherwise, see if "A InvEqP B" simplifies.
  2935. if (MaxRecurse)
  2936. if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
  2937. return V;
  2938. break;
  2939. }
  2940. case CmpInst::ICMP_SGE:
  2941. // Always true.
  2942. return getTrue(ITy);
  2943. case CmpInst::ICMP_SLT:
  2944. // Always false.
  2945. return getFalse(ITy);
  2946. }
  2947. }
  2948. // Unsigned variants on "max(a,b)>=a -> true".
  2949. P = CmpInst::BAD_ICMP_PREDICATE;
  2950. if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
  2951. if (A != RHS)
  2952. std::swap(A, B); // umax(A, B) pred A.
  2953. EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
  2954. // We analyze this as umax(A, B) pred A.
  2955. P = Pred;
  2956. } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
  2957. (A == LHS || B == LHS)) {
  2958. if (A != LHS)
  2959. std::swap(A, B); // A pred umax(A, B).
  2960. EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
  2961. // We analyze this as umax(A, B) swapped-pred A.
  2962. P = CmpInst::getSwappedPredicate(Pred);
  2963. } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
  2964. (A == RHS || B == RHS)) {
  2965. if (A != RHS)
  2966. std::swap(A, B); // umin(A, B) pred A.
  2967. EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
  2968. // We analyze this as umax(-A, -B) swapped-pred -A.
  2969. // Note that we do not need to actually form -A or -B thanks to EqP.
  2970. P = CmpInst::getSwappedPredicate(Pred);
  2971. } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
  2972. (A == LHS || B == LHS)) {
  2973. if (A != LHS)
  2974. std::swap(A, B); // A pred umin(A, B).
  2975. EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
  2976. // We analyze this as umax(-A, -B) pred -A.
  2977. // Note that we do not need to actually form -A or -B thanks to EqP.
  2978. P = Pred;
  2979. }
  2980. if (P != CmpInst::BAD_ICMP_PREDICATE) {
  2981. // Cases correspond to "max(A, B) p A".
  2982. switch (P) {
  2983. default:
  2984. break;
  2985. case CmpInst::ICMP_EQ:
  2986. case CmpInst::ICMP_ULE:
  2987. // Equivalent to "A EqP B". This may be the same as the condition tested
  2988. // in the max/min; if so, we can just return that.
  2989. if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
  2990. return V;
  2991. if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
  2992. return V;
  2993. // Otherwise, see if "A EqP B" simplifies.
  2994. if (MaxRecurse)
  2995. if (Value *V = SimplifyICmpInst(EqP, A, B, Q, MaxRecurse - 1))
  2996. return V;
  2997. break;
  2998. case CmpInst::ICMP_NE:
  2999. case CmpInst::ICMP_UGT: {
  3000. CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
  3001. // Equivalent to "A InvEqP B". This may be the same as the condition
  3002. // tested in the max/min; if so, we can just return that.
  3003. if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
  3004. return V;
  3005. if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
  3006. return V;
  3007. // Otherwise, see if "A InvEqP B" simplifies.
  3008. if (MaxRecurse)
  3009. if (Value *V = SimplifyICmpInst(InvEqP, A, B, Q, MaxRecurse - 1))
  3010. return V;
  3011. break;
  3012. }
  3013. case CmpInst::ICMP_UGE:
  3014. return getTrue(ITy);
  3015. case CmpInst::ICMP_ULT:
  3016. return getFalse(ITy);
  3017. }
  3018. }
  3019. // Comparing 1 each of min/max with a common operand?
  3020. // Canonicalize min operand to RHS.
  3021. if (match(LHS, m_UMin(m_Value(), m_Value())) ||
  3022. match(LHS, m_SMin(m_Value(), m_Value()))) {
  3023. std::swap(LHS, RHS);
  3024. Pred = ICmpInst::getSwappedPredicate(Pred);
  3025. }
  3026. Value *C, *D;
  3027. if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
  3028. match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
  3029. (A == C || A == D || B == C || B == D)) {
  3030. // smax(A, B) >=s smin(A, D) --> true
  3031. if (Pred == CmpInst::ICMP_SGE)
  3032. return getTrue(ITy);
  3033. // smax(A, B) <s smin(A, D) --> false
  3034. if (Pred == CmpInst::ICMP_SLT)
  3035. return getFalse(ITy);
  3036. } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
  3037. match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
  3038. (A == C || A == D || B == C || B == D)) {
  3039. // umax(A, B) >=u umin(A, D) --> true
  3040. if (Pred == CmpInst::ICMP_UGE)
  3041. return getTrue(ITy);
  3042. // umax(A, B) <u umin(A, D) --> false
  3043. if (Pred == CmpInst::ICMP_ULT)
  3044. return getFalse(ITy);
  3045. }
  3046. return nullptr;
  3047. }
  3048. static Value *simplifyICmpWithDominatingAssume(CmpInst::Predicate Predicate,
  3049. Value *LHS, Value *RHS,
  3050. const SimplifyQuery &Q) {
  3051. // Gracefully handle instructions that have not been inserted yet.
  3052. if (!Q.AC || !Q.CxtI || !Q.CxtI->getParent())
  3053. return nullptr;
  3054. for (Value *AssumeBaseOp : {LHS, RHS}) {
  3055. for (auto &AssumeVH : Q.AC->assumptionsFor(AssumeBaseOp)) {
  3056. if (!AssumeVH)
  3057. continue;
  3058. CallInst *Assume = cast<CallInst>(AssumeVH);
  3059. if (Optional<bool> Imp =
  3060. isImpliedCondition(Assume->getArgOperand(0), Predicate, LHS, RHS,
  3061. Q.DL))
  3062. if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT))
  3063. return ConstantInt::get(GetCompareTy(LHS), *Imp);
  3064. }
  3065. }
  3066. return nullptr;
  3067. }
  3068. /// Given operands for an ICmpInst, see if we can fold the result.
  3069. /// If not, this returns null.
  3070. static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  3071. const SimplifyQuery &Q, unsigned MaxRecurse) {
  3072. CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
  3073. assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
  3074. if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
  3075. if (Constant *CRHS = dyn_cast<Constant>(RHS))
  3076. return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
  3077. // If we have a constant, make sure it is on the RHS.
  3078. std::swap(LHS, RHS);
  3079. Pred = CmpInst::getSwappedPredicate(Pred);
  3080. }
  3081. assert(!isa<UndefValue>(LHS) && "Unexpected icmp undef,%X");
  3082. Type *ITy = GetCompareTy(LHS); // The return type.
  3083. // icmp poison, X -> poison
  3084. if (isa<PoisonValue>(RHS))
  3085. return PoisonValue::get(ITy);
  3086. // For EQ and NE, we can always pick a value for the undef to make the
  3087. // predicate pass or fail, so we can return undef.
  3088. // Matches behavior in llvm::ConstantFoldCompareInstruction.
  3089. if (Q.isUndefValue(RHS) && ICmpInst::isEquality(Pred))
  3090. return UndefValue::get(ITy);
  3091. // icmp X, X -> true/false
  3092. // icmp X, undef -> true/false because undef could be X.
  3093. if (LHS == RHS || Q.isUndefValue(RHS))
  3094. return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
  3095. if (Value *V = simplifyICmpOfBools(Pred, LHS, RHS, Q))
  3096. return V;
  3097. // TODO: Sink/common this with other potentially expensive calls that use
  3098. // ValueTracking? See comment below for isKnownNonEqual().
  3099. if (Value *V = simplifyICmpWithZero(Pred, LHS, RHS, Q))
  3100. return V;
  3101. if (Value *V = simplifyICmpWithConstant(Pred, LHS, RHS, Q.IIQ))
  3102. return V;
  3103. // If both operands have range metadata, use the metadata
  3104. // to simplify the comparison.
  3105. if (isa<Instruction>(RHS) && isa<Instruction>(LHS)) {
  3106. auto RHS_Instr = cast<Instruction>(RHS);
  3107. auto LHS_Instr = cast<Instruction>(LHS);
  3108. if (Q.IIQ.getMetadata(RHS_Instr, LLVMContext::MD_range) &&
  3109. Q.IIQ.getMetadata(LHS_Instr, LLVMContext::MD_range)) {
  3110. auto RHS_CR = getConstantRangeFromMetadata(
  3111. *RHS_Instr->getMetadata(LLVMContext::MD_range));
  3112. auto LHS_CR = getConstantRangeFromMetadata(
  3113. *LHS_Instr->getMetadata(LLVMContext::MD_range));
  3114. if (LHS_CR.icmp(Pred, RHS_CR))
  3115. return ConstantInt::getTrue(RHS->getContext());
  3116. if (LHS_CR.icmp(CmpInst::getInversePredicate(Pred), RHS_CR))
  3117. return ConstantInt::getFalse(RHS->getContext());
  3118. }
  3119. }
  3120. // Compare of cast, for example (zext X) != 0 -> X != 0
  3121. if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
  3122. Instruction *LI = cast<CastInst>(LHS);
  3123. Value *SrcOp = LI->getOperand(0);
  3124. Type *SrcTy = SrcOp->getType();
  3125. Type *DstTy = LI->getType();
  3126. // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
  3127. // if the integer type is the same size as the pointer type.
  3128. if (MaxRecurse && isa<PtrToIntInst>(LI) &&
  3129. Q.DL.getTypeSizeInBits(SrcTy) == DstTy->getPrimitiveSizeInBits()) {
  3130. if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
  3131. // Transfer the cast to the constant.
  3132. if (Value *V = SimplifyICmpInst(Pred, SrcOp,
  3133. ConstantExpr::getIntToPtr(RHSC, SrcTy),
  3134. Q, MaxRecurse-1))
  3135. return V;
  3136. } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
  3137. if (RI->getOperand(0)->getType() == SrcTy)
  3138. // Compare without the cast.
  3139. if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
  3140. Q, MaxRecurse-1))
  3141. return V;
  3142. }
  3143. }
  3144. if (isa<ZExtInst>(LHS)) {
  3145. // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
  3146. // same type.
  3147. if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
  3148. if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
  3149. // Compare X and Y. Note that signed predicates become unsigned.
  3150. if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
  3151. SrcOp, RI->getOperand(0), Q,
  3152. MaxRecurse-1))
  3153. return V;
  3154. }
  3155. // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true.
  3156. else if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
  3157. if (SrcOp == RI->getOperand(0)) {
  3158. if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_SGE)
  3159. return ConstantInt::getTrue(ITy);
  3160. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SLT)
  3161. return ConstantInt::getFalse(ITy);
  3162. }
  3163. }
  3164. // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
  3165. // too. If not, then try to deduce the result of the comparison.
  3166. else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
  3167. // Compute the constant that would happen if we truncated to SrcTy then
  3168. // reextended to DstTy.
  3169. Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
  3170. Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
  3171. // If the re-extended constant didn't change then this is effectively
  3172. // also a case of comparing two zero-extended values.
  3173. if (RExt == CI && MaxRecurse)
  3174. if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
  3175. SrcOp, Trunc, Q, MaxRecurse-1))
  3176. return V;
  3177. // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
  3178. // there. Use this to work out the result of the comparison.
  3179. if (RExt != CI) {
  3180. switch (Pred) {
  3181. default: llvm_unreachable("Unknown ICmp predicate!");
  3182. // LHS <u RHS.
  3183. case ICmpInst::ICMP_EQ:
  3184. case ICmpInst::ICMP_UGT:
  3185. case ICmpInst::ICMP_UGE:
  3186. return ConstantInt::getFalse(CI->getContext());
  3187. case ICmpInst::ICMP_NE:
  3188. case ICmpInst::ICMP_ULT:
  3189. case ICmpInst::ICMP_ULE:
  3190. return ConstantInt::getTrue(CI->getContext());
  3191. // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
  3192. // is non-negative then LHS <s RHS.
  3193. case ICmpInst::ICMP_SGT:
  3194. case ICmpInst::ICMP_SGE:
  3195. return CI->getValue().isNegative() ?
  3196. ConstantInt::getTrue(CI->getContext()) :
  3197. ConstantInt::getFalse(CI->getContext());
  3198. case ICmpInst::ICMP_SLT:
  3199. case ICmpInst::ICMP_SLE:
  3200. return CI->getValue().isNegative() ?
  3201. ConstantInt::getFalse(CI->getContext()) :
  3202. ConstantInt::getTrue(CI->getContext());
  3203. }
  3204. }
  3205. }
  3206. }
  3207. if (isa<SExtInst>(LHS)) {
  3208. // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
  3209. // same type.
  3210. if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
  3211. if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
  3212. // Compare X and Y. Note that the predicate does not change.
  3213. if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
  3214. Q, MaxRecurse-1))
  3215. return V;
  3216. }
  3217. // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true.
  3218. else if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
  3219. if (SrcOp == RI->getOperand(0)) {
  3220. if (Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_SLE)
  3221. return ConstantInt::getTrue(ITy);
  3222. if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SGT)
  3223. return ConstantInt::getFalse(ITy);
  3224. }
  3225. }
  3226. // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
  3227. // too. If not, then try to deduce the result of the comparison.
  3228. else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
  3229. // Compute the constant that would happen if we truncated to SrcTy then
  3230. // reextended to DstTy.
  3231. Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
  3232. Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
  3233. // If the re-extended constant didn't change then this is effectively
  3234. // also a case of comparing two sign-extended values.
  3235. if (RExt == CI && MaxRecurse)
  3236. if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1))
  3237. return V;
  3238. // Otherwise the upper bits of LHS are all equal, while RHS has varying
  3239. // bits there. Use this to work out the result of the comparison.
  3240. if (RExt != CI) {
  3241. switch (Pred) {
  3242. default: llvm_unreachable("Unknown ICmp predicate!");
  3243. case ICmpInst::ICMP_EQ:
  3244. return ConstantInt::getFalse(CI->getContext());
  3245. case ICmpInst::ICMP_NE:
  3246. return ConstantInt::getTrue(CI->getContext());
  3247. // If RHS is non-negative then LHS <s RHS. If RHS is negative then
  3248. // LHS >s RHS.
  3249. case ICmpInst::ICMP_SGT:
  3250. case ICmpInst::ICMP_SGE:
  3251. return CI->getValue().isNegative() ?
  3252. ConstantInt::getTrue(CI->getContext()) :
  3253. ConstantInt::getFalse(CI->getContext());
  3254. case ICmpInst::ICMP_SLT:
  3255. case ICmpInst::ICMP_SLE:
  3256. return CI->getValue().isNegative() ?
  3257. ConstantInt::getFalse(CI->getContext()) :
  3258. ConstantInt::getTrue(CI->getContext());
  3259. // If LHS is non-negative then LHS <u RHS. If LHS is negative then
  3260. // LHS >u RHS.
  3261. case ICmpInst::ICMP_UGT:
  3262. case ICmpInst::ICMP_UGE:
  3263. // Comparison is true iff the LHS <s 0.
  3264. if (MaxRecurse)
  3265. if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
  3266. Constant::getNullValue(SrcTy),
  3267. Q, MaxRecurse-1))
  3268. return V;
  3269. break;
  3270. case ICmpInst::ICMP_ULT:
  3271. case ICmpInst::ICMP_ULE:
  3272. // Comparison is true iff the LHS >=s 0.
  3273. if (MaxRecurse)
  3274. if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
  3275. Constant::getNullValue(SrcTy),
  3276. Q, MaxRecurse-1))
  3277. return V;
  3278. break;
  3279. }
  3280. }
  3281. }
  3282. }
  3283. }
  3284. // icmp eq|ne X, Y -> false|true if X != Y
  3285. // This is potentially expensive, and we have already computedKnownBits for
  3286. // compares with 0 above here, so only try this for a non-zero compare.
  3287. if (ICmpInst::isEquality(Pred) && !match(RHS, m_Zero()) &&
  3288. isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT, Q.IIQ.UseInstrInfo)) {
  3289. return Pred == ICmpInst::ICMP_NE ? getTrue(ITy) : getFalse(ITy);
  3290. }
  3291. if (Value *V = simplifyICmpWithBinOp(Pred, LHS, RHS, Q, MaxRecurse))
  3292. return V;
  3293. if (Value *V = simplifyICmpWithMinMax(Pred, LHS, RHS, Q, MaxRecurse))
  3294. return V;
  3295. if (Value *V = simplifyICmpWithDominatingAssume(Pred, LHS, RHS, Q))
  3296. return V;
  3297. // Simplify comparisons of related pointers using a powerful, recursive
  3298. // GEP-walk when we have target data available..
  3299. if (LHS->getType()->isPointerTy())
  3300. if (auto *C = computePointerICmp(Pred, LHS, RHS, Q))
  3301. return C;
  3302. if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
  3303. if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
  3304. if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
  3305. Q.DL.getTypeSizeInBits(CLHS->getType()) &&
  3306. Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
  3307. Q.DL.getTypeSizeInBits(CRHS->getType()))
  3308. if (auto *C = computePointerICmp(Pred, CLHS->getPointerOperand(),
  3309. CRHS->getPointerOperand(), Q))
  3310. return C;
  3311. // If the comparison is with the result of a select instruction, check whether
  3312. // comparing with either branch of the select always yields the same value.
  3313. if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
  3314. if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
  3315. return V;
  3316. // If the comparison is with the result of a phi instruction, check whether
  3317. // doing the compare with each incoming phi value yields a common result.
  3318. if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
  3319. if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
  3320. return V;
  3321. return nullptr;
  3322. }
  3323. Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  3324. const SimplifyQuery &Q) {
  3325. return ::SimplifyICmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
  3326. }
  3327. /// Given operands for an FCmpInst, see if we can fold the result.
  3328. /// If not, this returns null.
  3329. static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  3330. FastMathFlags FMF, const SimplifyQuery &Q,
  3331. unsigned MaxRecurse) {
  3332. CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
  3333. assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
  3334. if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
  3335. if (Constant *CRHS = dyn_cast<Constant>(RHS))
  3336. return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, Q.DL, Q.TLI);
  3337. // If we have a constant, make sure it is on the RHS.
  3338. std::swap(LHS, RHS);
  3339. Pred = CmpInst::getSwappedPredicate(Pred);
  3340. }
  3341. // Fold trivial predicates.
  3342. Type *RetTy = GetCompareTy(LHS);
  3343. if (Pred == FCmpInst::FCMP_FALSE)
  3344. return getFalse(RetTy);
  3345. if (Pred == FCmpInst::FCMP_TRUE)
  3346. return getTrue(RetTy);
  3347. // Fold (un)ordered comparison if we can determine there are no NaNs.
  3348. if (Pred == FCmpInst::FCMP_UNO || Pred == FCmpInst::FCMP_ORD)
  3349. if (FMF.noNaNs() ||
  3350. (isKnownNeverNaN(LHS, Q.TLI) && isKnownNeverNaN(RHS, Q.TLI)))
  3351. return ConstantInt::get(RetTy, Pred == FCmpInst::FCMP_ORD);
  3352. // NaN is unordered; NaN is not ordered.
  3353. assert((FCmpInst::isOrdered(Pred) || FCmpInst::isUnordered(Pred)) &&
  3354. "Comparison must be either ordered or unordered");
  3355. if (match(RHS, m_NaN()))
  3356. return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
  3357. // fcmp pred x, poison and fcmp pred poison, x
  3358. // fold to poison
  3359. if (isa<PoisonValue>(LHS) || isa<PoisonValue>(RHS))
  3360. return PoisonValue::get(RetTy);
  3361. // fcmp pred x, undef and fcmp pred undef, x
  3362. // fold to true if unordered, false if ordered
  3363. if (Q.isUndefValue(LHS) || Q.isUndefValue(RHS)) {
  3364. // Choosing NaN for the undef will always make unordered comparison succeed
  3365. // and ordered comparison fail.
  3366. return ConstantInt::get(RetTy, CmpInst::isUnordered(Pred));
  3367. }
  3368. // fcmp x,x -> true/false. Not all compares are foldable.
  3369. if (LHS == RHS) {
  3370. if (CmpInst::isTrueWhenEqual(Pred))
  3371. return getTrue(RetTy);
  3372. if (CmpInst::isFalseWhenEqual(Pred))
  3373. return getFalse(RetTy);
  3374. }
  3375. // Handle fcmp with constant RHS.
  3376. // TODO: Use match with a specific FP value, so these work with vectors with
  3377. // undef lanes.
  3378. const APFloat *C;
  3379. if (match(RHS, m_APFloat(C))) {
  3380. // Check whether the constant is an infinity.
  3381. if (C->isInfinity()) {
  3382. if (C->isNegative()) {
  3383. switch (Pred) {
  3384. case FCmpInst::FCMP_OLT:
  3385. // No value is ordered and less than negative infinity.
  3386. return getFalse(RetTy);
  3387. case FCmpInst::FCMP_UGE:
  3388. // All values are unordered with or at least negative infinity.
  3389. return getTrue(RetTy);
  3390. default:
  3391. break;
  3392. }
  3393. } else {
  3394. switch (Pred) {
  3395. case FCmpInst::FCMP_OGT:
  3396. // No value is ordered and greater than infinity.
  3397. return getFalse(RetTy);
  3398. case FCmpInst::FCMP_ULE:
  3399. // All values are unordered with and at most infinity.
  3400. return getTrue(RetTy);
  3401. default:
  3402. break;
  3403. }
  3404. }
  3405. // LHS == Inf
  3406. if (Pred == FCmpInst::FCMP_OEQ && isKnownNeverInfinity(LHS, Q.TLI))
  3407. return getFalse(RetTy);
  3408. // LHS != Inf
  3409. if (Pred == FCmpInst::FCMP_UNE && isKnownNeverInfinity(LHS, Q.TLI))
  3410. return getTrue(RetTy);
  3411. // LHS == Inf || LHS == NaN
  3412. if (Pred == FCmpInst::FCMP_UEQ && isKnownNeverInfinity(LHS, Q.TLI) &&
  3413. isKnownNeverNaN(LHS, Q.TLI))
  3414. return getFalse(RetTy);
  3415. // LHS != Inf && LHS != NaN
  3416. if (Pred == FCmpInst::FCMP_ONE && isKnownNeverInfinity(LHS, Q.TLI) &&
  3417. isKnownNeverNaN(LHS, Q.TLI))
  3418. return getTrue(RetTy);
  3419. }
  3420. if (C->isNegative() && !C->isNegZero()) {
  3421. assert(!C->isNaN() && "Unexpected NaN constant!");
  3422. // TODO: We can catch more cases by using a range check rather than
  3423. // relying on CannotBeOrderedLessThanZero.
  3424. switch (Pred) {
  3425. case FCmpInst::FCMP_UGE:
  3426. case FCmpInst::FCMP_UGT:
  3427. case FCmpInst::FCMP_UNE:
  3428. // (X >= 0) implies (X > C) when (C < 0)
  3429. if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
  3430. return getTrue(RetTy);
  3431. break;
  3432. case FCmpInst::FCMP_OEQ:
  3433. case FCmpInst::FCMP_OLE:
  3434. case FCmpInst::FCMP_OLT:
  3435. // (X >= 0) implies !(X < C) when (C < 0)
  3436. if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
  3437. return getFalse(RetTy);
  3438. break;
  3439. default:
  3440. break;
  3441. }
  3442. }
  3443. // Check comparison of [minnum/maxnum with constant] with other constant.
  3444. const APFloat *C2;
  3445. if ((match(LHS, m_Intrinsic<Intrinsic::minnum>(m_Value(), m_APFloat(C2))) &&
  3446. *C2 < *C) ||
  3447. (match(LHS, m_Intrinsic<Intrinsic::maxnum>(m_Value(), m_APFloat(C2))) &&
  3448. *C2 > *C)) {
  3449. bool IsMaxNum =
  3450. cast<IntrinsicInst>(LHS)->getIntrinsicID() == Intrinsic::maxnum;
  3451. // The ordered relationship and minnum/maxnum guarantee that we do not
  3452. // have NaN constants, so ordered/unordered preds are handled the same.
  3453. switch (Pred) {
  3454. case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_UEQ:
  3455. // minnum(X, LesserC) == C --> false
  3456. // maxnum(X, GreaterC) == C --> false
  3457. return getFalse(RetTy);
  3458. case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_UNE:
  3459. // minnum(X, LesserC) != C --> true
  3460. // maxnum(X, GreaterC) != C --> true
  3461. return getTrue(RetTy);
  3462. case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_UGE:
  3463. case FCmpInst::FCMP_OGT: case FCmpInst::FCMP_UGT:
  3464. // minnum(X, LesserC) >= C --> false
  3465. // minnum(X, LesserC) > C --> false
  3466. // maxnum(X, GreaterC) >= C --> true
  3467. // maxnum(X, GreaterC) > C --> true
  3468. return ConstantInt::get(RetTy, IsMaxNum);
  3469. case FCmpInst::FCMP_OLE: case FCmpInst::FCMP_ULE:
  3470. case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_ULT:
  3471. // minnum(X, LesserC) <= C --> true
  3472. // minnum(X, LesserC) < C --> true
  3473. // maxnum(X, GreaterC) <= C --> false
  3474. // maxnum(X, GreaterC) < C --> false
  3475. return ConstantInt::get(RetTy, !IsMaxNum);
  3476. default:
  3477. // TRUE/FALSE/ORD/UNO should be handled before this.
  3478. llvm_unreachable("Unexpected fcmp predicate");
  3479. }
  3480. }
  3481. }
  3482. if (match(RHS, m_AnyZeroFP())) {
  3483. switch (Pred) {
  3484. case FCmpInst::FCMP_OGE:
  3485. case FCmpInst::FCMP_ULT:
  3486. // Positive or zero X >= 0.0 --> true
  3487. // Positive or zero X < 0.0 --> false
  3488. if ((FMF.noNaNs() || isKnownNeverNaN(LHS, Q.TLI)) &&
  3489. CannotBeOrderedLessThanZero(LHS, Q.TLI))
  3490. return Pred == FCmpInst::FCMP_OGE ? getTrue(RetTy) : getFalse(RetTy);
  3491. break;
  3492. case FCmpInst::FCMP_UGE:
  3493. case FCmpInst::FCMP_OLT:
  3494. // Positive or zero or nan X >= 0.0 --> true
  3495. // Positive or zero or nan X < 0.0 --> false
  3496. if (CannotBeOrderedLessThanZero(LHS, Q.TLI))
  3497. return Pred == FCmpInst::FCMP_UGE ? getTrue(RetTy) : getFalse(RetTy);
  3498. break;
  3499. default:
  3500. break;
  3501. }
  3502. }
  3503. // If the comparison is with the result of a select instruction, check whether
  3504. // comparing with either branch of the select always yields the same value.
  3505. if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
  3506. if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, Q, MaxRecurse))
  3507. return V;
  3508. // If the comparison is with the result of a phi instruction, check whether
  3509. // doing the compare with each incoming phi value yields a common result.
  3510. if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
  3511. if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, Q, MaxRecurse))
  3512. return V;
  3513. return nullptr;
  3514. }
  3515. Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  3516. FastMathFlags FMF, const SimplifyQuery &Q) {
  3517. return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF, Q, RecursionLimit);
  3518. }
  3519. static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
  3520. const SimplifyQuery &Q,
  3521. bool AllowRefinement,
  3522. unsigned MaxRecurse) {
  3523. assert(!Op->getType()->isVectorTy() && "This is not safe for vectors");
  3524. // Trivial replacement.
  3525. if (V == Op)
  3526. return RepOp;
  3527. // We cannot replace a constant, and shouldn't even try.
  3528. if (isa<Constant>(Op))
  3529. return nullptr;
  3530. auto *I = dyn_cast<Instruction>(V);
  3531. if (!I || !is_contained(I->operands(), Op))
  3532. return nullptr;
  3533. // Replace Op with RepOp in instruction operands.
  3534. SmallVector<Value *, 8> NewOps(I->getNumOperands());
  3535. transform(I->operands(), NewOps.begin(),
  3536. [&](Value *V) { return V == Op ? RepOp : V; });
  3537. if (!AllowRefinement) {
  3538. // General InstSimplify functions may refine the result, e.g. by returning
  3539. // a constant for a potentially poison value. To avoid this, implement only
  3540. // a few non-refining but profitable transforms here.
  3541. if (auto *BO = dyn_cast<BinaryOperator>(I)) {
  3542. unsigned Opcode = BO->getOpcode();
  3543. // id op x -> x, x op id -> x
  3544. if (NewOps[0] == ConstantExpr::getBinOpIdentity(Opcode, I->getType()))
  3545. return NewOps[1];
  3546. if (NewOps[1] == ConstantExpr::getBinOpIdentity(Opcode, I->getType(),
  3547. /* RHS */ true))
  3548. return NewOps[0];
  3549. // x & x -> x, x | x -> x
  3550. if ((Opcode == Instruction::And || Opcode == Instruction::Or) &&
  3551. NewOps[0] == NewOps[1])
  3552. return NewOps[0];
  3553. }
  3554. if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
  3555. // getelementptr x, 0 -> x
  3556. if (NewOps.size() == 2 && match(NewOps[1], m_Zero()) &&
  3557. !GEP->isInBounds())
  3558. return NewOps[0];
  3559. }
  3560. } else if (MaxRecurse) {
  3561. // The simplification queries below may return the original value. Consider:
  3562. // %div = udiv i32 %arg, %arg2
  3563. // %mul = mul nsw i32 %div, %arg2
  3564. // %cmp = icmp eq i32 %mul, %arg
  3565. // %sel = select i1 %cmp, i32 %div, i32 undef
  3566. // Replacing %arg by %mul, %div becomes "udiv i32 %mul, %arg2", which
  3567. // simplifies back to %arg. This can only happen because %mul does not
  3568. // dominate %div. To ensure a consistent return value contract, we make sure
  3569. // that this case returns nullptr as well.
  3570. auto PreventSelfSimplify = [V](Value *Simplified) {
  3571. return Simplified != V ? Simplified : nullptr;
  3572. };
  3573. if (auto *B = dyn_cast<BinaryOperator>(I))
  3574. return PreventSelfSimplify(SimplifyBinOp(B->getOpcode(), NewOps[0],
  3575. NewOps[1], Q, MaxRecurse - 1));
  3576. if (CmpInst *C = dyn_cast<CmpInst>(I))
  3577. return PreventSelfSimplify(SimplifyCmpInst(C->getPredicate(), NewOps[0],
  3578. NewOps[1], Q, MaxRecurse - 1));
  3579. if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
  3580. return PreventSelfSimplify(SimplifyGEPInst(
  3581. GEP->getSourceElementType(), NewOps[0], makeArrayRef(NewOps).slice(1),
  3582. GEP->isInBounds(), Q, MaxRecurse - 1));
  3583. if (isa<SelectInst>(I))
  3584. return PreventSelfSimplify(
  3585. SimplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q,
  3586. MaxRecurse - 1));
  3587. // TODO: We could hand off more cases to instsimplify here.
  3588. }
  3589. // If all operands are constant after substituting Op for RepOp then we can
  3590. // constant fold the instruction.
  3591. SmallVector<Constant *, 8> ConstOps;
  3592. for (Value *NewOp : NewOps) {
  3593. if (Constant *ConstOp = dyn_cast<Constant>(NewOp))
  3594. ConstOps.push_back(ConstOp);
  3595. else
  3596. return nullptr;
  3597. }
  3598. // Consider:
  3599. // %cmp = icmp eq i32 %x, 2147483647
  3600. // %add = add nsw i32 %x, 1
  3601. // %sel = select i1 %cmp, i32 -2147483648, i32 %add
  3602. //
  3603. // We can't replace %sel with %add unless we strip away the flags (which
  3604. // will be done in InstCombine).
  3605. // TODO: This may be unsound, because it only catches some forms of
  3606. // refinement.
  3607. if (!AllowRefinement && canCreatePoison(cast<Operator>(I)))
  3608. return nullptr;
  3609. if (CmpInst *C = dyn_cast<CmpInst>(I))
  3610. return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0],
  3611. ConstOps[1], Q.DL, Q.TLI);
  3612. if (LoadInst *LI = dyn_cast<LoadInst>(I))
  3613. if (!LI->isVolatile())
  3614. return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL);
  3615. return ConstantFoldInstOperands(I, ConstOps, Q.DL, Q.TLI);
  3616. }
  3617. Value *llvm::simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
  3618. const SimplifyQuery &Q,
  3619. bool AllowRefinement) {
  3620. return ::simplifyWithOpReplaced(V, Op, RepOp, Q, AllowRefinement,
  3621. RecursionLimit);
  3622. }
  3623. /// Try to simplify a select instruction when its condition operand is an
  3624. /// integer comparison where one operand of the compare is a constant.
  3625. static Value *simplifySelectBitTest(Value *TrueVal, Value *FalseVal, Value *X,
  3626. const APInt *Y, bool TrueWhenUnset) {
  3627. const APInt *C;
  3628. // (X & Y) == 0 ? X & ~Y : X --> X
  3629. // (X & Y) != 0 ? X & ~Y : X --> X & ~Y
  3630. if (FalseVal == X && match(TrueVal, m_And(m_Specific(X), m_APInt(C))) &&
  3631. *Y == ~*C)
  3632. return TrueWhenUnset ? FalseVal : TrueVal;
  3633. // (X & Y) == 0 ? X : X & ~Y --> X & ~Y
  3634. // (X & Y) != 0 ? X : X & ~Y --> X
  3635. if (TrueVal == X && match(FalseVal, m_And(m_Specific(X), m_APInt(C))) &&
  3636. *Y == ~*C)
  3637. return TrueWhenUnset ? FalseVal : TrueVal;
  3638. if (Y->isPowerOf2()) {
  3639. // (X & Y) == 0 ? X | Y : X --> X | Y
  3640. // (X & Y) != 0 ? X | Y : X --> X
  3641. if (FalseVal == X && match(TrueVal, m_Or(m_Specific(X), m_APInt(C))) &&
  3642. *Y == *C)
  3643. return TrueWhenUnset ? TrueVal : FalseVal;
  3644. // (X & Y) == 0 ? X : X | Y --> X
  3645. // (X & Y) != 0 ? X : X | Y --> X | Y
  3646. if (TrueVal == X && match(FalseVal, m_Or(m_Specific(X), m_APInt(C))) &&
  3647. *Y == *C)
  3648. return TrueWhenUnset ? TrueVal : FalseVal;
  3649. }
  3650. return nullptr;
  3651. }
  3652. /// An alternative way to test if a bit is set or not uses sgt/slt instead of
  3653. /// eq/ne.
  3654. static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
  3655. ICmpInst::Predicate Pred,
  3656. Value *TrueVal, Value *FalseVal) {
  3657. Value *X;
  3658. APInt Mask;
  3659. if (!decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask))
  3660. return nullptr;
  3661. return simplifySelectBitTest(TrueVal, FalseVal, X, &Mask,
  3662. Pred == ICmpInst::ICMP_EQ);
  3663. }
  3664. /// Try to simplify a select instruction when its condition operand is an
  3665. /// integer comparison.
  3666. static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
  3667. Value *FalseVal, const SimplifyQuery &Q,
  3668. unsigned MaxRecurse) {
  3669. ICmpInst::Predicate Pred;
  3670. Value *CmpLHS, *CmpRHS;
  3671. if (!match(CondVal, m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS))))
  3672. return nullptr;
  3673. // Canonicalize ne to eq predicate.
  3674. if (Pred == ICmpInst::ICMP_NE) {
  3675. Pred = ICmpInst::ICMP_EQ;
  3676. std::swap(TrueVal, FalseVal);
  3677. }
  3678. // Check for integer min/max with a limit constant:
  3679. // X > MIN_INT ? X : MIN_INT --> X
  3680. // X < MAX_INT ? X : MAX_INT --> X
  3681. if (TrueVal->getType()->isIntOrIntVectorTy()) {
  3682. Value *X, *Y;
  3683. SelectPatternFlavor SPF =
  3684. matchDecomposedSelectPattern(cast<ICmpInst>(CondVal), TrueVal, FalseVal,
  3685. X, Y).Flavor;
  3686. if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) {
  3687. APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF),
  3688. X->getType()->getScalarSizeInBits());
  3689. if (match(Y, m_SpecificInt(LimitC)))
  3690. return X;
  3691. }
  3692. }
  3693. if (Pred == ICmpInst::ICMP_EQ && match(CmpRHS, m_Zero())) {
  3694. Value *X;
  3695. const APInt *Y;
  3696. if (match(CmpLHS, m_And(m_Value(X), m_APInt(Y))))
  3697. if (Value *V = simplifySelectBitTest(TrueVal, FalseVal, X, Y,
  3698. /*TrueWhenUnset=*/true))
  3699. return V;
  3700. // Test for a bogus zero-shift-guard-op around funnel-shift or rotate.
  3701. Value *ShAmt;
  3702. auto isFsh = m_CombineOr(m_FShl(m_Value(X), m_Value(), m_Value(ShAmt)),
  3703. m_FShr(m_Value(), m_Value(X), m_Value(ShAmt)));
  3704. // (ShAmt == 0) ? fshl(X, *, ShAmt) : X --> X
  3705. // (ShAmt == 0) ? fshr(*, X, ShAmt) : X --> X
  3706. if (match(TrueVal, isFsh) && FalseVal == X && CmpLHS == ShAmt)
  3707. return X;
  3708. // Test for a zero-shift-guard-op around rotates. These are used to
  3709. // avoid UB from oversized shifts in raw IR rotate patterns, but the
  3710. // intrinsics do not have that problem.
  3711. // We do not allow this transform for the general funnel shift case because
  3712. // that would not preserve the poison safety of the original code.
  3713. auto isRotate =
  3714. m_CombineOr(m_FShl(m_Value(X), m_Deferred(X), m_Value(ShAmt)),
  3715. m_FShr(m_Value(X), m_Deferred(X), m_Value(ShAmt)));
  3716. // (ShAmt == 0) ? X : fshl(X, X, ShAmt) --> fshl(X, X, ShAmt)
  3717. // (ShAmt == 0) ? X : fshr(X, X, ShAmt) --> fshr(X, X, ShAmt)
  3718. if (match(FalseVal, isRotate) && TrueVal == X && CmpLHS == ShAmt &&
  3719. Pred == ICmpInst::ICMP_EQ)
  3720. return FalseVal;
  3721. // X == 0 ? abs(X) : -abs(X) --> -abs(X)
  3722. // X == 0 ? -abs(X) : abs(X) --> abs(X)
  3723. if (match(TrueVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))) &&
  3724. match(FalseVal, m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))))
  3725. return FalseVal;
  3726. if (match(TrueVal,
  3727. m_Neg(m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS)))) &&
  3728. match(FalseVal, m_Intrinsic<Intrinsic::abs>(m_Specific(CmpLHS))))
  3729. return FalseVal;
  3730. }
  3731. // Check for other compares that behave like bit test.
  3732. if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred,
  3733. TrueVal, FalseVal))
  3734. return V;
  3735. // If we have a scalar equality comparison, then we know the value in one of
  3736. // the arms of the select. See if substituting this value into the arm and
  3737. // simplifying the result yields the same value as the other arm.
  3738. // Note that the equivalence/replacement opportunity does not hold for vectors
  3739. // because each element of a vector select is chosen independently.
  3740. if (Pred == ICmpInst::ICMP_EQ && !CondVal->getType()->isVectorTy()) {
  3741. if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q,
  3742. /* AllowRefinement */ false, MaxRecurse) ==
  3743. TrueVal ||
  3744. simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q,
  3745. /* AllowRefinement */ false, MaxRecurse) ==
  3746. TrueVal)
  3747. return FalseVal;
  3748. if (simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q,
  3749. /* AllowRefinement */ true, MaxRecurse) ==
  3750. FalseVal ||
  3751. simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q,
  3752. /* AllowRefinement */ true, MaxRecurse) ==
  3753. FalseVal)
  3754. return FalseVal;
  3755. }
  3756. return nullptr;
  3757. }
  3758. /// Try to simplify a select instruction when its condition operand is a
  3759. /// floating-point comparison.
  3760. static Value *simplifySelectWithFCmp(Value *Cond, Value *T, Value *F,
  3761. const SimplifyQuery &Q) {
  3762. FCmpInst::Predicate Pred;
  3763. if (!match(Cond, m_FCmp(Pred, m_Specific(T), m_Specific(F))) &&
  3764. !match(Cond, m_FCmp(Pred, m_Specific(F), m_Specific(T))))
  3765. return nullptr;
  3766. // This transform is safe if we do not have (do not care about) -0.0 or if
  3767. // at least one operand is known to not be -0.0. Otherwise, the select can
  3768. // change the sign of a zero operand.
  3769. bool HasNoSignedZeros = Q.CxtI && isa<FPMathOperator>(Q.CxtI) &&
  3770. Q.CxtI->hasNoSignedZeros();
  3771. const APFloat *C;
  3772. if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) ||
  3773. (match(F, m_APFloat(C)) && C->isNonZero())) {
  3774. // (T == F) ? T : F --> F
  3775. // (F == T) ? T : F --> F
  3776. if (Pred == FCmpInst::FCMP_OEQ)
  3777. return F;
  3778. // (T != F) ? T : F --> T
  3779. // (F != T) ? T : F --> T
  3780. if (Pred == FCmpInst::FCMP_UNE)
  3781. return T;
  3782. }
  3783. return nullptr;
  3784. }
  3785. /// Given operands for a SelectInst, see if we can fold the result.
  3786. /// If not, this returns null.
  3787. static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
  3788. const SimplifyQuery &Q, unsigned MaxRecurse) {
  3789. if (auto *CondC = dyn_cast<Constant>(Cond)) {
  3790. if (auto *TrueC = dyn_cast<Constant>(TrueVal))
  3791. if (auto *FalseC = dyn_cast<Constant>(FalseVal))
  3792. return ConstantFoldSelectInstruction(CondC, TrueC, FalseC);
  3793. // select poison, X, Y -> poison
  3794. if (isa<PoisonValue>(CondC))
  3795. return PoisonValue::get(TrueVal->getType());
  3796. // select undef, X, Y -> X or Y
  3797. if (Q.isUndefValue(CondC))
  3798. return isa<Constant>(FalseVal) ? FalseVal : TrueVal;
  3799. // select true, X, Y --> X
  3800. // select false, X, Y --> Y
  3801. // For vectors, allow undef/poison elements in the condition to match the
  3802. // defined elements, so we can eliminate the select.
  3803. if (match(CondC, m_One()))
  3804. return TrueVal;
  3805. if (match(CondC, m_Zero()))
  3806. return FalseVal;
  3807. }
  3808. assert(Cond->getType()->isIntOrIntVectorTy(1) &&
  3809. "Select must have bool or bool vector condition");
  3810. assert(TrueVal->getType() == FalseVal->getType() &&
  3811. "Select must have same types for true/false ops");
  3812. if (Cond->getType() == TrueVal->getType()) {
  3813. // select i1 Cond, i1 true, i1 false --> i1 Cond
  3814. if (match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
  3815. return Cond;
  3816. // (X || Y) && (X || !Y) --> X (commuted 8 ways)
  3817. Value *X, *Y;
  3818. if (match(FalseVal, m_ZeroInt())) {
  3819. if (match(Cond, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
  3820. match(TrueVal, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
  3821. return X;
  3822. if (match(TrueVal, m_c_LogicalOr(m_Value(X), m_Not(m_Value(Y)))) &&
  3823. match(Cond, m_c_LogicalOr(m_Specific(X), m_Specific(Y))))
  3824. return X;
  3825. }
  3826. }
  3827. // select ?, X, X -> X
  3828. if (TrueVal == FalseVal)
  3829. return TrueVal;
  3830. // If the true or false value is poison, we can fold to the other value.
  3831. // If the true or false value is undef, we can fold to the other value as
  3832. // long as the other value isn't poison.
  3833. // select ?, poison, X -> X
  3834. // select ?, undef, X -> X
  3835. if (isa<PoisonValue>(TrueVal) ||
  3836. (Q.isUndefValue(TrueVal) &&
  3837. isGuaranteedNotToBePoison(FalseVal, Q.AC, Q.CxtI, Q.DT)))
  3838. return FalseVal;
  3839. // select ?, X, poison -> X
  3840. // select ?, X, undef -> X
  3841. if (isa<PoisonValue>(FalseVal) ||
  3842. (Q.isUndefValue(FalseVal) &&
  3843. isGuaranteedNotToBePoison(TrueVal, Q.AC, Q.CxtI, Q.DT)))
  3844. return TrueVal;
  3845. // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
  3846. Constant *TrueC, *FalseC;
  3847. if (isa<FixedVectorType>(TrueVal->getType()) &&
  3848. match(TrueVal, m_Constant(TrueC)) &&
  3849. match(FalseVal, m_Constant(FalseC))) {
  3850. unsigned NumElts =
  3851. cast<FixedVectorType>(TrueC->getType())->getNumElements();
  3852. SmallVector<Constant *, 16> NewC;
  3853. for (unsigned i = 0; i != NumElts; ++i) {
  3854. // Bail out on incomplete vector constants.
  3855. Constant *TEltC = TrueC->getAggregateElement(i);
  3856. Constant *FEltC = FalseC->getAggregateElement(i);
  3857. if (!TEltC || !FEltC)
  3858. break;
  3859. // If the elements match (undef or not), that value is the result. If only
  3860. // one element is undef, choose the defined element as the safe result.
  3861. if (TEltC == FEltC)
  3862. NewC.push_back(TEltC);
  3863. else if (isa<PoisonValue>(TEltC) ||
  3864. (Q.isUndefValue(TEltC) && isGuaranteedNotToBePoison(FEltC)))
  3865. NewC.push_back(FEltC);
  3866. else if (isa<PoisonValue>(FEltC) ||
  3867. (Q.isUndefValue(FEltC) && isGuaranteedNotToBePoison(TEltC)))
  3868. NewC.push_back(TEltC);
  3869. else
  3870. break;
  3871. }
  3872. if (NewC.size() == NumElts)
  3873. return ConstantVector::get(NewC);
  3874. }
  3875. if (Value *V =
  3876. simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
  3877. return V;
  3878. if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q))
  3879. return V;
  3880. if (Value *V = foldSelectWithBinaryOp(Cond, TrueVal, FalseVal))
  3881. return V;
  3882. Optional<bool> Imp = isImpliedByDomCondition(Cond, Q.CxtI, Q.DL);
  3883. if (Imp)
  3884. return *Imp ? TrueVal : FalseVal;
  3885. return nullptr;
  3886. }
  3887. Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
  3888. const SimplifyQuery &Q) {
  3889. return ::SimplifySelectInst(Cond, TrueVal, FalseVal, Q, RecursionLimit);
  3890. }
  3891. /// Given operands for an GetElementPtrInst, see if we can fold the result.
  3892. /// If not, this returns null.
  3893. static Value *SimplifyGEPInst(Type *SrcTy, Value *Ptr,
  3894. ArrayRef<Value *> Indices, bool InBounds,
  3895. const SimplifyQuery &Q, unsigned) {
  3896. // The type of the GEP pointer operand.
  3897. unsigned AS =
  3898. cast<PointerType>(Ptr->getType()->getScalarType())->getAddressSpace();
  3899. // getelementptr P -> P.
  3900. if (Indices.empty())
  3901. return Ptr;
  3902. // Compute the (pointer) type returned by the GEP instruction.
  3903. Type *LastType = GetElementPtrInst::getIndexedType(SrcTy, Indices);
  3904. Type *GEPTy = PointerType::get(LastType, AS);
  3905. if (VectorType *VT = dyn_cast<VectorType>(Ptr->getType()))
  3906. GEPTy = VectorType::get(GEPTy, VT->getElementCount());
  3907. else {
  3908. for (Value *Op : Indices) {
  3909. // If one of the operands is a vector, the result type is a vector of
  3910. // pointers. All vector operands must have the same number of elements.
  3911. if (VectorType *VT = dyn_cast<VectorType>(Op->getType())) {
  3912. GEPTy = VectorType::get(GEPTy, VT->getElementCount());
  3913. break;
  3914. }
  3915. }
  3916. }
  3917. // getelementptr poison, idx -> poison
  3918. // getelementptr baseptr, poison -> poison
  3919. if (isa<PoisonValue>(Ptr) ||
  3920. any_of(Indices, [](const auto *V) { return isa<PoisonValue>(V); }))
  3921. return PoisonValue::get(GEPTy);
  3922. if (Q.isUndefValue(Ptr))
  3923. // If inbounds, we can choose an out-of-bounds pointer as a base pointer.
  3924. return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy);
  3925. bool IsScalableVec =
  3926. isa<ScalableVectorType>(SrcTy) || any_of(Indices, [](const Value *V) {
  3927. return isa<ScalableVectorType>(V->getType());
  3928. });
  3929. if (Indices.size() == 1) {
  3930. // getelementptr P, 0 -> P.
  3931. if (match(Indices[0], m_Zero()) && Ptr->getType() == GEPTy)
  3932. return Ptr;
  3933. Type *Ty = SrcTy;
  3934. if (!IsScalableVec && Ty->isSized()) {
  3935. Value *P;
  3936. uint64_t C;
  3937. uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty);
  3938. // getelementptr P, N -> P if P points to a type of zero size.
  3939. if (TyAllocSize == 0 && Ptr->getType() == GEPTy)
  3940. return Ptr;
  3941. // The following transforms are only safe if the ptrtoint cast
  3942. // doesn't truncate the pointers.
  3943. if (Indices[0]->getType()->getScalarSizeInBits() ==
  3944. Q.DL.getPointerSizeInBits(AS)) {
  3945. auto CanSimplify = [GEPTy, &P, Ptr]() -> bool {
  3946. return P->getType() == GEPTy &&
  3947. getUnderlyingObject(P) == getUnderlyingObject(Ptr);
  3948. };
  3949. // getelementptr V, (sub P, V) -> P if P points to a type of size 1.
  3950. if (TyAllocSize == 1 &&
  3951. match(Indices[0],
  3952. m_Sub(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Specific(Ptr)))) &&
  3953. CanSimplify())
  3954. return P;
  3955. // getelementptr V, (ashr (sub P, V), C) -> P if P points to a type of
  3956. // size 1 << C.
  3957. if (match(Indices[0], m_AShr(m_Sub(m_PtrToInt(m_Value(P)),
  3958. m_PtrToInt(m_Specific(Ptr))),
  3959. m_ConstantInt(C))) &&
  3960. TyAllocSize == 1ULL << C && CanSimplify())
  3961. return P;
  3962. // getelementptr V, (sdiv (sub P, V), C) -> P if P points to a type of
  3963. // size C.
  3964. if (match(Indices[0], m_SDiv(m_Sub(m_PtrToInt(m_Value(P)),
  3965. m_PtrToInt(m_Specific(Ptr))),
  3966. m_SpecificInt(TyAllocSize))) &&
  3967. CanSimplify())
  3968. return P;
  3969. }
  3970. }
  3971. }
  3972. if (!IsScalableVec && Q.DL.getTypeAllocSize(LastType) == 1 &&
  3973. all_of(Indices.drop_back(1),
  3974. [](Value *Idx) { return match(Idx, m_Zero()); })) {
  3975. unsigned IdxWidth =
  3976. Q.DL.getIndexSizeInBits(Ptr->getType()->getPointerAddressSpace());
  3977. if (Q.DL.getTypeSizeInBits(Indices.back()->getType()) == IdxWidth) {
  3978. APInt BasePtrOffset(IdxWidth, 0);
  3979. Value *StrippedBasePtr =
  3980. Ptr->stripAndAccumulateInBoundsConstantOffsets(Q.DL, BasePtrOffset);
  3981. // Avoid creating inttoptr of zero here: While LLVMs treatment of
  3982. // inttoptr is generally conservative, this particular case is folded to
  3983. // a null pointer, which will have incorrect provenance.
  3984. // gep (gep V, C), (sub 0, V) -> C
  3985. if (match(Indices.back(),
  3986. m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr)))) &&
  3987. !BasePtrOffset.isZero()) {
  3988. auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset);
  3989. return ConstantExpr::getIntToPtr(CI, GEPTy);
  3990. }
  3991. // gep (gep V, C), (xor V, -1) -> C-1
  3992. if (match(Indices.back(),
  3993. m_Xor(m_PtrToInt(m_Specific(StrippedBasePtr)), m_AllOnes())) &&
  3994. !BasePtrOffset.isOne()) {
  3995. auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset - 1);
  3996. return ConstantExpr::getIntToPtr(CI, GEPTy);
  3997. }
  3998. }
  3999. }
  4000. // Check to see if this is constant foldable.
  4001. if (!isa<Constant>(Ptr) ||
  4002. !all_of(Indices, [](Value *V) { return isa<Constant>(V); }))
  4003. return nullptr;
  4004. auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast<Constant>(Ptr), Indices,
  4005. InBounds);
  4006. return ConstantFoldConstant(CE, Q.DL);
  4007. }
  4008. Value *llvm::SimplifyGEPInst(Type *SrcTy, Value *Ptr, ArrayRef<Value *> Indices,
  4009. bool InBounds, const SimplifyQuery &Q) {
  4010. return ::SimplifyGEPInst(SrcTy, Ptr, Indices, InBounds, Q, RecursionLimit);
  4011. }
  4012. /// Given operands for an InsertValueInst, see if we can fold the result.
  4013. /// If not, this returns null.
  4014. static Value *SimplifyInsertValueInst(Value *Agg, Value *Val,
  4015. ArrayRef<unsigned> Idxs, const SimplifyQuery &Q,
  4016. unsigned) {
  4017. if (Constant *CAgg = dyn_cast<Constant>(Agg))
  4018. if (Constant *CVal = dyn_cast<Constant>(Val))
  4019. return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
  4020. // insertvalue x, undef, n -> x
  4021. if (Q.isUndefValue(Val))
  4022. return Agg;
  4023. // insertvalue x, (extractvalue y, n), n
  4024. if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
  4025. if (EV->getAggregateOperand()->getType() == Agg->getType() &&
  4026. EV->getIndices() == Idxs) {
  4027. // insertvalue undef, (extractvalue y, n), n -> y
  4028. if (Q.isUndefValue(Agg))
  4029. return EV->getAggregateOperand();
  4030. // insertvalue y, (extractvalue y, n), n -> y
  4031. if (Agg == EV->getAggregateOperand())
  4032. return Agg;
  4033. }
  4034. return nullptr;
  4035. }
  4036. Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
  4037. ArrayRef<unsigned> Idxs,
  4038. const SimplifyQuery &Q) {
  4039. return ::SimplifyInsertValueInst(Agg, Val, Idxs, Q, RecursionLimit);
  4040. }
  4041. Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
  4042. const SimplifyQuery &Q) {
  4043. // Try to constant fold.
  4044. auto *VecC = dyn_cast<Constant>(Vec);
  4045. auto *ValC = dyn_cast<Constant>(Val);
  4046. auto *IdxC = dyn_cast<Constant>(Idx);
  4047. if (VecC && ValC && IdxC)
  4048. return ConstantExpr::getInsertElement(VecC, ValC, IdxC);
  4049. // For fixed-length vector, fold into poison if index is out of bounds.
  4050. if (auto *CI = dyn_cast<ConstantInt>(Idx)) {
  4051. if (isa<FixedVectorType>(Vec->getType()) &&
  4052. CI->uge(cast<FixedVectorType>(Vec->getType())->getNumElements()))
  4053. return PoisonValue::get(Vec->getType());
  4054. }
  4055. // If index is undef, it might be out of bounds (see above case)
  4056. if (Q.isUndefValue(Idx))
  4057. return PoisonValue::get(Vec->getType());
  4058. // If the scalar is poison, or it is undef and there is no risk of
  4059. // propagating poison from the vector value, simplify to the vector value.
  4060. if (isa<PoisonValue>(Val) ||
  4061. (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
  4062. return Vec;
  4063. // If we are extracting a value from a vector, then inserting it into the same
  4064. // place, that's the input vector:
  4065. // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec
  4066. if (match(Val, m_ExtractElt(m_Specific(Vec), m_Specific(Idx))))
  4067. return Vec;
  4068. return nullptr;
  4069. }
  4070. /// Given operands for an ExtractValueInst, see if we can fold the result.
  4071. /// If not, this returns null.
  4072. static Value *SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
  4073. const SimplifyQuery &, unsigned) {
  4074. if (auto *CAgg = dyn_cast<Constant>(Agg))
  4075. return ConstantFoldExtractValueInstruction(CAgg, Idxs);
  4076. // extractvalue x, (insertvalue y, elt, n), n -> elt
  4077. unsigned NumIdxs = Idxs.size();
  4078. for (auto *IVI = dyn_cast<InsertValueInst>(Agg); IVI != nullptr;
  4079. IVI = dyn_cast<InsertValueInst>(IVI->getAggregateOperand())) {
  4080. ArrayRef<unsigned> InsertValueIdxs = IVI->getIndices();
  4081. unsigned NumInsertValueIdxs = InsertValueIdxs.size();
  4082. unsigned NumCommonIdxs = std::min(NumInsertValueIdxs, NumIdxs);
  4083. if (InsertValueIdxs.slice(0, NumCommonIdxs) ==
  4084. Idxs.slice(0, NumCommonIdxs)) {
  4085. if (NumIdxs == NumInsertValueIdxs)
  4086. return IVI->getInsertedValueOperand();
  4087. break;
  4088. }
  4089. }
  4090. return nullptr;
  4091. }
  4092. Value *llvm::SimplifyExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
  4093. const SimplifyQuery &Q) {
  4094. return ::SimplifyExtractValueInst(Agg, Idxs, Q, RecursionLimit);
  4095. }
  4096. /// Given operands for an ExtractElementInst, see if we can fold the result.
  4097. /// If not, this returns null.
  4098. static Value *SimplifyExtractElementInst(Value *Vec, Value *Idx,
  4099. const SimplifyQuery &Q, unsigned) {
  4100. auto *VecVTy = cast<VectorType>(Vec->getType());
  4101. if (auto *CVec = dyn_cast<Constant>(Vec)) {
  4102. if (auto *CIdx = dyn_cast<Constant>(Idx))
  4103. return ConstantExpr::getExtractElement(CVec, CIdx);
  4104. if (Q.isUndefValue(Vec))
  4105. return UndefValue::get(VecVTy->getElementType());
  4106. }
  4107. // An undef extract index can be arbitrarily chosen to be an out-of-range
  4108. // index value, which would result in the instruction being poison.
  4109. if (Q.isUndefValue(Idx))
  4110. return PoisonValue::get(VecVTy->getElementType());
  4111. // If extracting a specified index from the vector, see if we can recursively
  4112. // find a previously computed scalar that was inserted into the vector.
  4113. if (auto *IdxC = dyn_cast<ConstantInt>(Idx)) {
  4114. // For fixed-length vector, fold into undef if index is out of bounds.
  4115. unsigned MinNumElts = VecVTy->getElementCount().getKnownMinValue();
  4116. if (isa<FixedVectorType>(VecVTy) && IdxC->getValue().uge(MinNumElts))
  4117. return PoisonValue::get(VecVTy->getElementType());
  4118. // Handle case where an element is extracted from a splat.
  4119. if (IdxC->getValue().ult(MinNumElts))
  4120. if (auto *Splat = getSplatValue(Vec))
  4121. return Splat;
  4122. if (Value *Elt = findScalarElement(Vec, IdxC->getZExtValue()))
  4123. return Elt;
  4124. } else {
  4125. // The index is not relevant if our vector is a splat.
  4126. if (Value *Splat = getSplatValue(Vec))
  4127. return Splat;
  4128. }
  4129. return nullptr;
  4130. }
  4131. Value *llvm::SimplifyExtractElementInst(Value *Vec, Value *Idx,
  4132. const SimplifyQuery &Q) {
  4133. return ::SimplifyExtractElementInst(Vec, Idx, Q, RecursionLimit);
  4134. }
  4135. /// See if we can fold the given phi. If not, returns null.
  4136. static Value *SimplifyPHINode(PHINode *PN, ArrayRef<Value *> IncomingValues,
  4137. const SimplifyQuery &Q) {
  4138. // WARNING: no matter how worthwhile it may seem, we can not perform PHI CSE
  4139. // here, because the PHI we may succeed simplifying to was not
  4140. // def-reachable from the original PHI!
  4141. // If all of the PHI's incoming values are the same then replace the PHI node
  4142. // with the common value.
  4143. Value *CommonValue = nullptr;
  4144. bool HasUndefInput = false;
  4145. for (Value *Incoming : IncomingValues) {
  4146. // If the incoming value is the phi node itself, it can safely be skipped.
  4147. if (Incoming == PN) continue;
  4148. if (Q.isUndefValue(Incoming)) {
  4149. // Remember that we saw an undef value, but otherwise ignore them.
  4150. HasUndefInput = true;
  4151. continue;
  4152. }
  4153. if (CommonValue && Incoming != CommonValue)
  4154. return nullptr; // Not the same, bail out.
  4155. CommonValue = Incoming;
  4156. }
  4157. // If CommonValue is null then all of the incoming values were either undef or
  4158. // equal to the phi node itself.
  4159. if (!CommonValue)
  4160. return UndefValue::get(PN->getType());
  4161. // If we have a PHI node like phi(X, undef, X), where X is defined by some
  4162. // instruction, we cannot return X as the result of the PHI node unless it
  4163. // dominates the PHI block.
  4164. if (HasUndefInput)
  4165. return valueDominatesPHI(CommonValue, PN, Q.DT) ? CommonValue : nullptr;
  4166. return CommonValue;
  4167. }
  4168. static Value *SimplifyCastInst(unsigned CastOpc, Value *Op,
  4169. Type *Ty, const SimplifyQuery &Q, unsigned MaxRecurse) {
  4170. if (auto *C = dyn_cast<Constant>(Op))
  4171. return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL);
  4172. if (auto *CI = dyn_cast<CastInst>(Op)) {
  4173. auto *Src = CI->getOperand(0);
  4174. Type *SrcTy = Src->getType();
  4175. Type *MidTy = CI->getType();
  4176. Type *DstTy = Ty;
  4177. if (Src->getType() == Ty) {
  4178. auto FirstOp = static_cast<Instruction::CastOps>(CI->getOpcode());
  4179. auto SecondOp = static_cast<Instruction::CastOps>(CastOpc);
  4180. Type *SrcIntPtrTy =
  4181. SrcTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(SrcTy) : nullptr;
  4182. Type *MidIntPtrTy =
  4183. MidTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(MidTy) : nullptr;
  4184. Type *DstIntPtrTy =
  4185. DstTy->isPtrOrPtrVectorTy() ? Q.DL.getIntPtrType(DstTy) : nullptr;
  4186. if (CastInst::isEliminableCastPair(FirstOp, SecondOp, SrcTy, MidTy, DstTy,
  4187. SrcIntPtrTy, MidIntPtrTy,
  4188. DstIntPtrTy) == Instruction::BitCast)
  4189. return Src;
  4190. }
  4191. }
  4192. // bitcast x -> x
  4193. if (CastOpc == Instruction::BitCast)
  4194. if (Op->getType() == Ty)
  4195. return Op;
  4196. return nullptr;
  4197. }
  4198. Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
  4199. const SimplifyQuery &Q) {
  4200. return ::SimplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
  4201. }
  4202. /// For the given destination element of a shuffle, peek through shuffles to
  4203. /// match a root vector source operand that contains that element in the same
  4204. /// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
  4205. static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
  4206. int MaskVal, Value *RootVec,
  4207. unsigned MaxRecurse) {
  4208. if (!MaxRecurse--)
  4209. return nullptr;
  4210. // Bail out if any mask value is undefined. That kind of shuffle may be
  4211. // simplified further based on demanded bits or other folds.
  4212. if (MaskVal == -1)
  4213. return nullptr;
  4214. // The mask value chooses which source operand we need to look at next.
  4215. int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
  4216. int RootElt = MaskVal;
  4217. Value *SourceOp = Op0;
  4218. if (MaskVal >= InVecNumElts) {
  4219. RootElt = MaskVal - InVecNumElts;
  4220. SourceOp = Op1;
  4221. }
  4222. // If the source operand is a shuffle itself, look through it to find the
  4223. // matching root vector.
  4224. if (auto *SourceShuf = dyn_cast<ShuffleVectorInst>(SourceOp)) {
  4225. return foldIdentityShuffles(
  4226. DestElt, SourceShuf->getOperand(0), SourceShuf->getOperand(1),
  4227. SourceShuf->getMaskValue(RootElt), RootVec, MaxRecurse);
  4228. }
  4229. // TODO: Look through bitcasts? What if the bitcast changes the vector element
  4230. // size?
  4231. // The source operand is not a shuffle. Initialize the root vector value for
  4232. // this shuffle if that has not been done yet.
  4233. if (!RootVec)
  4234. RootVec = SourceOp;
  4235. // Give up as soon as a source operand does not match the existing root value.
  4236. if (RootVec != SourceOp)
  4237. return nullptr;
  4238. // The element must be coming from the same lane in the source vector
  4239. // (although it may have crossed lanes in intermediate shuffles).
  4240. if (RootElt != DestElt)
  4241. return nullptr;
  4242. return RootVec;
  4243. }
  4244. static Value *SimplifyShuffleVectorInst(Value *Op0, Value *Op1,
  4245. ArrayRef<int> Mask, Type *RetTy,
  4246. const SimplifyQuery &Q,
  4247. unsigned MaxRecurse) {
  4248. if (all_of(Mask, [](int Elem) { return Elem == UndefMaskElem; }))
  4249. return UndefValue::get(RetTy);
  4250. auto *InVecTy = cast<VectorType>(Op0->getType());
  4251. unsigned MaskNumElts = Mask.size();
  4252. ElementCount InVecEltCount = InVecTy->getElementCount();
  4253. bool Scalable = InVecEltCount.isScalable();
  4254. SmallVector<int, 32> Indices;
  4255. Indices.assign(Mask.begin(), Mask.end());
  4256. // Canonicalization: If mask does not select elements from an input vector,
  4257. // replace that input vector with poison.
  4258. if (!Scalable) {
  4259. bool MaskSelects0 = false, MaskSelects1 = false;
  4260. unsigned InVecNumElts = InVecEltCount.getKnownMinValue();
  4261. for (unsigned i = 0; i != MaskNumElts; ++i) {
  4262. if (Indices[i] == -1)
  4263. continue;
  4264. if ((unsigned)Indices[i] < InVecNumElts)
  4265. MaskSelects0 = true;
  4266. else
  4267. MaskSelects1 = true;
  4268. }
  4269. if (!MaskSelects0)
  4270. Op0 = PoisonValue::get(InVecTy);
  4271. if (!MaskSelects1)
  4272. Op1 = PoisonValue::get(InVecTy);
  4273. }
  4274. auto *Op0Const = dyn_cast<Constant>(Op0);
  4275. auto *Op1Const = dyn_cast<Constant>(Op1);
  4276. // If all operands are constant, constant fold the shuffle. This
  4277. // transformation depends on the value of the mask which is not known at
  4278. // compile time for scalable vectors
  4279. if (Op0Const && Op1Const)
  4280. return ConstantExpr::getShuffleVector(Op0Const, Op1Const, Mask);
  4281. // Canonicalization: if only one input vector is constant, it shall be the
  4282. // second one. This transformation depends on the value of the mask which
  4283. // is not known at compile time for scalable vectors
  4284. if (!Scalable && Op0Const && !Op1Const) {
  4285. std::swap(Op0, Op1);
  4286. ShuffleVectorInst::commuteShuffleMask(Indices,
  4287. InVecEltCount.getKnownMinValue());
  4288. }
  4289. // A splat of an inserted scalar constant becomes a vector constant:
  4290. // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
  4291. // NOTE: We may have commuted above, so analyze the updated Indices, not the
  4292. // original mask constant.
  4293. // NOTE: This transformation depends on the value of the mask which is not
  4294. // known at compile time for scalable vectors
  4295. Constant *C;
  4296. ConstantInt *IndexC;
  4297. if (!Scalable && match(Op0, m_InsertElt(m_Value(), m_Constant(C),
  4298. m_ConstantInt(IndexC)))) {
  4299. // Match a splat shuffle mask of the insert index allowing undef elements.
  4300. int InsertIndex = IndexC->getZExtValue();
  4301. if (all_of(Indices, [InsertIndex](int MaskElt) {
  4302. return MaskElt == InsertIndex || MaskElt == -1;
  4303. })) {
  4304. assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
  4305. // Shuffle mask undefs become undefined constant result elements.
  4306. SmallVector<Constant *, 16> VecC(MaskNumElts, C);
  4307. for (unsigned i = 0; i != MaskNumElts; ++i)
  4308. if (Indices[i] == -1)
  4309. VecC[i] = UndefValue::get(C->getType());
  4310. return ConstantVector::get(VecC);
  4311. }
  4312. }
  4313. // A shuffle of a splat is always the splat itself. Legal if the shuffle's
  4314. // value type is same as the input vectors' type.
  4315. if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))
  4316. if (Q.isUndefValue(Op1) && RetTy == InVecTy &&
  4317. is_splat(OpShuf->getShuffleMask()))
  4318. return Op0;
  4319. // All remaining transformation depend on the value of the mask, which is
  4320. // not known at compile time for scalable vectors.
  4321. if (Scalable)
  4322. return nullptr;
  4323. // Don't fold a shuffle with undef mask elements. This may get folded in a
  4324. // better way using demanded bits or other analysis.
  4325. // TODO: Should we allow this?
  4326. if (is_contained(Indices, -1))
  4327. return nullptr;
  4328. // Check if every element of this shuffle can be mapped back to the
  4329. // corresponding element of a single root vector. If so, we don't need this
  4330. // shuffle. This handles simple identity shuffles as well as chains of
  4331. // shuffles that may widen/narrow and/or move elements across lanes and back.
  4332. Value *RootVec = nullptr;
  4333. for (unsigned i = 0; i != MaskNumElts; ++i) {
  4334. // Note that recursion is limited for each vector element, so if any element
  4335. // exceeds the limit, this will fail to simplify.
  4336. RootVec =
  4337. foldIdentityShuffles(i, Op0, Op1, Indices[i], RootVec, MaxRecurse);
  4338. // We can't replace a widening/narrowing shuffle with one of its operands.
  4339. if (!RootVec || RootVec->getType() != RetTy)
  4340. return nullptr;
  4341. }
  4342. return RootVec;
  4343. }
  4344. /// Given operands for a ShuffleVectorInst, fold the result or return null.
  4345. Value *llvm::SimplifyShuffleVectorInst(Value *Op0, Value *Op1,
  4346. ArrayRef<int> Mask, Type *RetTy,
  4347. const SimplifyQuery &Q) {
  4348. return ::SimplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit);
  4349. }
  4350. static Constant *foldConstant(Instruction::UnaryOps Opcode,
  4351. Value *&Op, const SimplifyQuery &Q) {
  4352. if (auto *C = dyn_cast<Constant>(Op))
  4353. return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL);
  4354. return nullptr;
  4355. }
  4356. /// Given the operand for an FNeg, see if we can fold the result. If not, this
  4357. /// returns null.
  4358. static Value *simplifyFNegInst(Value *Op, FastMathFlags FMF,
  4359. const SimplifyQuery &Q, unsigned MaxRecurse) {
  4360. if (Constant *C = foldConstant(Instruction::FNeg, Op, Q))
  4361. return C;
  4362. Value *X;
  4363. // fneg (fneg X) ==> X
  4364. if (match(Op, m_FNeg(m_Value(X))))
  4365. return X;
  4366. return nullptr;
  4367. }
  4368. Value *llvm::SimplifyFNegInst(Value *Op, FastMathFlags FMF,
  4369. const SimplifyQuery &Q) {
  4370. return ::simplifyFNegInst(Op, FMF, Q, RecursionLimit);
  4371. }
  4372. static Constant *propagateNaN(Constant *In) {
  4373. // If the input is a vector with undef elements, just return a default NaN.
  4374. if (!In->isNaN())
  4375. return ConstantFP::getNaN(In->getType());
  4376. // Propagate the existing NaN constant when possible.
  4377. // TODO: Should we quiet a signaling NaN?
  4378. return In;
  4379. }
  4380. /// Perform folds that are common to any floating-point operation. This implies
  4381. /// transforms based on poison/undef/NaN because the operation itself makes no
  4382. /// difference to the result.
  4383. static Constant *simplifyFPOp(ArrayRef<Value *> Ops, FastMathFlags FMF,
  4384. const SimplifyQuery &Q,
  4385. fp::ExceptionBehavior ExBehavior,
  4386. RoundingMode Rounding) {
  4387. // Poison is independent of anything else. It always propagates from an
  4388. // operand to a math result.
  4389. if (any_of(Ops, [](Value *V) { return match(V, m_Poison()); }))
  4390. return PoisonValue::get(Ops[0]->getType());
  4391. for (Value *V : Ops) {
  4392. bool IsNan = match(V, m_NaN());
  4393. bool IsInf = match(V, m_Inf());
  4394. bool IsUndef = Q.isUndefValue(V);
  4395. // If this operation has 'nnan' or 'ninf' and at least 1 disallowed operand
  4396. // (an undef operand can be chosen to be Nan/Inf), then the result of
  4397. // this operation is poison.
  4398. if (FMF.noNaNs() && (IsNan || IsUndef))
  4399. return PoisonValue::get(V->getType());
  4400. if (FMF.noInfs() && (IsInf || IsUndef))
  4401. return PoisonValue::get(V->getType());
  4402. if (isDefaultFPEnvironment(ExBehavior, Rounding)) {
  4403. if (IsUndef || IsNan)
  4404. return propagateNaN(cast<Constant>(V));
  4405. } else if (ExBehavior != fp::ebStrict) {
  4406. if (IsNan)
  4407. return propagateNaN(cast<Constant>(V));
  4408. }
  4409. }
  4410. return nullptr;
  4411. }
  4412. // TODO: Move this out to a header file:
  4413. static inline bool canIgnoreSNaN(fp::ExceptionBehavior EB, FastMathFlags FMF) {
  4414. return (EB == fp::ebIgnore || FMF.noNaNs());
  4415. }
  4416. /// Given operands for an FAdd, see if we can fold the result. If not, this
  4417. /// returns null.
  4418. static Value *
  4419. SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4420. const SimplifyQuery &Q, unsigned MaxRecurse,
  4421. fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
  4422. RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
  4423. if (isDefaultFPEnvironment(ExBehavior, Rounding))
  4424. if (Constant *C = foldOrCommuteConstant(Instruction::FAdd, Op0, Op1, Q))
  4425. return C;
  4426. if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
  4427. return C;
  4428. // fadd X, -0 ==> X
  4429. // With strict/constrained FP, we have these possible edge cases that do
  4430. // not simplify to Op0:
  4431. // fadd SNaN, -0.0 --> QNaN
  4432. // fadd +0.0, -0.0 --> -0.0 (but only with round toward negative)
  4433. if (canIgnoreSNaN(ExBehavior, FMF) &&
  4434. (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
  4435. FMF.noSignedZeros()))
  4436. if (match(Op1, m_NegZeroFP()))
  4437. return Op0;
  4438. // fadd X, 0 ==> X, when we know X is not -0
  4439. if (canIgnoreSNaN(ExBehavior, FMF))
  4440. if (match(Op1, m_PosZeroFP()) &&
  4441. (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
  4442. return Op0;
  4443. if (!isDefaultFPEnvironment(ExBehavior, Rounding))
  4444. return nullptr;
  4445. // With nnan: -X + X --> 0.0 (and commuted variant)
  4446. // We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
  4447. // Negative zeros are allowed because we always end up with positive zero:
  4448. // X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
  4449. // X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
  4450. // X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
  4451. // X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
  4452. if (FMF.noNaNs()) {
  4453. if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
  4454. match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
  4455. return ConstantFP::getNullValue(Op0->getType());
  4456. if (match(Op0, m_FNeg(m_Specific(Op1))) ||
  4457. match(Op1, m_FNeg(m_Specific(Op0))))
  4458. return ConstantFP::getNullValue(Op0->getType());
  4459. }
  4460. // (X - Y) + Y --> X
  4461. // Y + (X - Y) --> X
  4462. Value *X;
  4463. if (FMF.noSignedZeros() && FMF.allowReassoc() &&
  4464. (match(Op0, m_FSub(m_Value(X), m_Specific(Op1))) ||
  4465. match(Op1, m_FSub(m_Value(X), m_Specific(Op0)))))
  4466. return X;
  4467. return nullptr;
  4468. }
  4469. /// Given operands for an FSub, see if we can fold the result. If not, this
  4470. /// returns null.
  4471. static Value *
  4472. SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4473. const SimplifyQuery &Q, unsigned MaxRecurse,
  4474. fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
  4475. RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
  4476. if (isDefaultFPEnvironment(ExBehavior, Rounding))
  4477. if (Constant *C = foldOrCommuteConstant(Instruction::FSub, Op0, Op1, Q))
  4478. return C;
  4479. if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
  4480. return C;
  4481. if (!isDefaultFPEnvironment(ExBehavior, Rounding))
  4482. return nullptr;
  4483. // fsub X, +0 ==> X
  4484. if (match(Op1, m_PosZeroFP()))
  4485. return Op0;
  4486. // fsub X, -0 ==> X, when we know X is not -0
  4487. if (match(Op1, m_NegZeroFP()) &&
  4488. (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
  4489. return Op0;
  4490. // fsub -0.0, (fsub -0.0, X) ==> X
  4491. // fsub -0.0, (fneg X) ==> X
  4492. Value *X;
  4493. if (match(Op0, m_NegZeroFP()) &&
  4494. match(Op1, m_FNeg(m_Value(X))))
  4495. return X;
  4496. // fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
  4497. // fsub 0.0, (fneg X) ==> X if signed zeros are ignored.
  4498. if (FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()) &&
  4499. (match(Op1, m_FSub(m_AnyZeroFP(), m_Value(X))) ||
  4500. match(Op1, m_FNeg(m_Value(X)))))
  4501. return X;
  4502. // fsub nnan x, x ==> 0.0
  4503. if (FMF.noNaNs() && Op0 == Op1)
  4504. return Constant::getNullValue(Op0->getType());
  4505. // Y - (Y - X) --> X
  4506. // (X + Y) - Y --> X
  4507. if (FMF.noSignedZeros() && FMF.allowReassoc() &&
  4508. (match(Op1, m_FSub(m_Specific(Op0), m_Value(X))) ||
  4509. match(Op0, m_c_FAdd(m_Specific(Op1), m_Value(X)))))
  4510. return X;
  4511. return nullptr;
  4512. }
  4513. static Value *SimplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
  4514. const SimplifyQuery &Q, unsigned MaxRecurse,
  4515. fp::ExceptionBehavior ExBehavior,
  4516. RoundingMode Rounding) {
  4517. if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
  4518. return C;
  4519. if (!isDefaultFPEnvironment(ExBehavior, Rounding))
  4520. return nullptr;
  4521. // fmul X, 1.0 ==> X
  4522. if (match(Op1, m_FPOne()))
  4523. return Op0;
  4524. // fmul 1.0, X ==> X
  4525. if (match(Op0, m_FPOne()))
  4526. return Op1;
  4527. // fmul nnan nsz X, 0 ==> 0
  4528. if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op1, m_AnyZeroFP()))
  4529. return ConstantFP::getNullValue(Op0->getType());
  4530. // fmul nnan nsz 0, X ==> 0
  4531. if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
  4532. return ConstantFP::getNullValue(Op1->getType());
  4533. // sqrt(X) * sqrt(X) --> X, if we can:
  4534. // 1. Remove the intermediate rounding (reassociate).
  4535. // 2. Ignore non-zero negative numbers because sqrt would produce NAN.
  4536. // 3. Ignore -0.0 because sqrt(-0.0) == -0.0, but -0.0 * -0.0 == 0.0.
  4537. Value *X;
  4538. if (Op0 == Op1 && match(Op0, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) &&
  4539. FMF.allowReassoc() && FMF.noNaNs() && FMF.noSignedZeros())
  4540. return X;
  4541. return nullptr;
  4542. }
  4543. /// Given the operands for an FMul, see if we can fold the result
  4544. static Value *
  4545. SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4546. const SimplifyQuery &Q, unsigned MaxRecurse,
  4547. fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
  4548. RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
  4549. if (isDefaultFPEnvironment(ExBehavior, Rounding))
  4550. if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
  4551. return C;
  4552. // Now apply simplifications that do not require rounding.
  4553. return SimplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse, ExBehavior, Rounding);
  4554. }
  4555. Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4556. const SimplifyQuery &Q,
  4557. fp::ExceptionBehavior ExBehavior,
  4558. RoundingMode Rounding) {
  4559. return ::SimplifyFAddInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
  4560. Rounding);
  4561. }
  4562. Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4563. const SimplifyQuery &Q,
  4564. fp::ExceptionBehavior ExBehavior,
  4565. RoundingMode Rounding) {
  4566. return ::SimplifyFSubInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
  4567. Rounding);
  4568. }
  4569. Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4570. const SimplifyQuery &Q,
  4571. fp::ExceptionBehavior ExBehavior,
  4572. RoundingMode Rounding) {
  4573. return ::SimplifyFMulInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
  4574. Rounding);
  4575. }
  4576. Value *llvm::SimplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
  4577. const SimplifyQuery &Q,
  4578. fp::ExceptionBehavior ExBehavior,
  4579. RoundingMode Rounding) {
  4580. return ::SimplifyFMAFMul(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
  4581. Rounding);
  4582. }
  4583. static Value *
  4584. SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4585. const SimplifyQuery &Q, unsigned,
  4586. fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
  4587. RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
  4588. if (isDefaultFPEnvironment(ExBehavior, Rounding))
  4589. if (Constant *C = foldOrCommuteConstant(Instruction::FDiv, Op0, Op1, Q))
  4590. return C;
  4591. if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
  4592. return C;
  4593. if (!isDefaultFPEnvironment(ExBehavior, Rounding))
  4594. return nullptr;
  4595. // X / 1.0 -> X
  4596. if (match(Op1, m_FPOne()))
  4597. return Op0;
  4598. // 0 / X -> 0
  4599. // Requires that NaNs are off (X could be zero) and signed zeroes are
  4600. // ignored (X could be positive or negative, so the output sign is unknown).
  4601. if (FMF.noNaNs() && FMF.noSignedZeros() && match(Op0, m_AnyZeroFP()))
  4602. return ConstantFP::getNullValue(Op0->getType());
  4603. if (FMF.noNaNs()) {
  4604. // X / X -> 1.0 is legal when NaNs are ignored.
  4605. // We can ignore infinities because INF/INF is NaN.
  4606. if (Op0 == Op1)
  4607. return ConstantFP::get(Op0->getType(), 1.0);
  4608. // (X * Y) / Y --> X if we can reassociate to the above form.
  4609. Value *X;
  4610. if (FMF.allowReassoc() && match(Op0, m_c_FMul(m_Value(X), m_Specific(Op1))))
  4611. return X;
  4612. // -X / X -> -1.0 and
  4613. // X / -X -> -1.0 are legal when NaNs are ignored.
  4614. // We can ignore signed zeros because +-0.0/+-0.0 is NaN and ignored.
  4615. if (match(Op0, m_FNegNSZ(m_Specific(Op1))) ||
  4616. match(Op1, m_FNegNSZ(m_Specific(Op0))))
  4617. return ConstantFP::get(Op0->getType(), -1.0);
  4618. }
  4619. return nullptr;
  4620. }
  4621. Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4622. const SimplifyQuery &Q,
  4623. fp::ExceptionBehavior ExBehavior,
  4624. RoundingMode Rounding) {
  4625. return ::SimplifyFDivInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
  4626. Rounding);
  4627. }
  4628. static Value *
  4629. SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4630. const SimplifyQuery &Q, unsigned,
  4631. fp::ExceptionBehavior ExBehavior = fp::ebIgnore,
  4632. RoundingMode Rounding = RoundingMode::NearestTiesToEven) {
  4633. if (isDefaultFPEnvironment(ExBehavior, Rounding))
  4634. if (Constant *C = foldOrCommuteConstant(Instruction::FRem, Op0, Op1, Q))
  4635. return C;
  4636. if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
  4637. return C;
  4638. if (!isDefaultFPEnvironment(ExBehavior, Rounding))
  4639. return nullptr;
  4640. // Unlike fdiv, the result of frem always matches the sign of the dividend.
  4641. // The constant match may include undef elements in a vector, so return a full
  4642. // zero constant as the result.
  4643. if (FMF.noNaNs()) {
  4644. // +0 % X -> 0
  4645. if (match(Op0, m_PosZeroFP()))
  4646. return ConstantFP::getNullValue(Op0->getType());
  4647. // -0 % X -> -0
  4648. if (match(Op0, m_NegZeroFP()))
  4649. return ConstantFP::getNegativeZero(Op0->getType());
  4650. }
  4651. return nullptr;
  4652. }
  4653. Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
  4654. const SimplifyQuery &Q,
  4655. fp::ExceptionBehavior ExBehavior,
  4656. RoundingMode Rounding) {
  4657. return ::SimplifyFRemInst(Op0, Op1, FMF, Q, RecursionLimit, ExBehavior,
  4658. Rounding);
  4659. }
  4660. //=== Helper functions for higher up the class hierarchy.
  4661. /// Given the operand for a UnaryOperator, see if we can fold the result.
  4662. /// If not, this returns null.
  4663. static Value *simplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q,
  4664. unsigned MaxRecurse) {
  4665. switch (Opcode) {
  4666. case Instruction::FNeg:
  4667. return simplifyFNegInst(Op, FastMathFlags(), Q, MaxRecurse);
  4668. default:
  4669. llvm_unreachable("Unexpected opcode");
  4670. }
  4671. }
  4672. /// Given the operand for a UnaryOperator, see if we can fold the result.
  4673. /// If not, this returns null.
  4674. /// Try to use FastMathFlags when folding the result.
  4675. static Value *simplifyFPUnOp(unsigned Opcode, Value *Op,
  4676. const FastMathFlags &FMF,
  4677. const SimplifyQuery &Q, unsigned MaxRecurse) {
  4678. switch (Opcode) {
  4679. case Instruction::FNeg:
  4680. return simplifyFNegInst(Op, FMF, Q, MaxRecurse);
  4681. default:
  4682. return simplifyUnOp(Opcode, Op, Q, MaxRecurse);
  4683. }
  4684. }
  4685. Value *llvm::SimplifyUnOp(unsigned Opcode, Value *Op, const SimplifyQuery &Q) {
  4686. return ::simplifyUnOp(Opcode, Op, Q, RecursionLimit);
  4687. }
  4688. Value *llvm::SimplifyUnOp(unsigned Opcode, Value *Op, FastMathFlags FMF,
  4689. const SimplifyQuery &Q) {
  4690. return ::simplifyFPUnOp(Opcode, Op, FMF, Q, RecursionLimit);
  4691. }
  4692. /// Given operands for a BinaryOperator, see if we can fold the result.
  4693. /// If not, this returns null.
  4694. static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  4695. const SimplifyQuery &Q, unsigned MaxRecurse) {
  4696. switch (Opcode) {
  4697. case Instruction::Add:
  4698. return SimplifyAddInst(LHS, RHS, false, false, Q, MaxRecurse);
  4699. case Instruction::Sub:
  4700. return SimplifySubInst(LHS, RHS, false, false, Q, MaxRecurse);
  4701. case Instruction::Mul:
  4702. return SimplifyMulInst(LHS, RHS, Q, MaxRecurse);
  4703. case Instruction::SDiv:
  4704. return SimplifySDivInst(LHS, RHS, Q, MaxRecurse);
  4705. case Instruction::UDiv:
  4706. return SimplifyUDivInst(LHS, RHS, Q, MaxRecurse);
  4707. case Instruction::SRem:
  4708. return SimplifySRemInst(LHS, RHS, Q, MaxRecurse);
  4709. case Instruction::URem:
  4710. return SimplifyURemInst(LHS, RHS, Q, MaxRecurse);
  4711. case Instruction::Shl:
  4712. return SimplifyShlInst(LHS, RHS, false, false, Q, MaxRecurse);
  4713. case Instruction::LShr:
  4714. return SimplifyLShrInst(LHS, RHS, false, Q, MaxRecurse);
  4715. case Instruction::AShr:
  4716. return SimplifyAShrInst(LHS, RHS, false, Q, MaxRecurse);
  4717. case Instruction::And:
  4718. return SimplifyAndInst(LHS, RHS, Q, MaxRecurse);
  4719. case Instruction::Or:
  4720. return SimplifyOrInst(LHS, RHS, Q, MaxRecurse);
  4721. case Instruction::Xor:
  4722. return SimplifyXorInst(LHS, RHS, Q, MaxRecurse);
  4723. case Instruction::FAdd:
  4724. return SimplifyFAddInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
  4725. case Instruction::FSub:
  4726. return SimplifyFSubInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
  4727. case Instruction::FMul:
  4728. return SimplifyFMulInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
  4729. case Instruction::FDiv:
  4730. return SimplifyFDivInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
  4731. case Instruction::FRem:
  4732. return SimplifyFRemInst(LHS, RHS, FastMathFlags(), Q, MaxRecurse);
  4733. default:
  4734. llvm_unreachable("Unexpected opcode");
  4735. }
  4736. }
  4737. /// Given operands for a BinaryOperator, see if we can fold the result.
  4738. /// If not, this returns null.
  4739. /// Try to use FastMathFlags when folding the result.
  4740. static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  4741. const FastMathFlags &FMF, const SimplifyQuery &Q,
  4742. unsigned MaxRecurse) {
  4743. switch (Opcode) {
  4744. case Instruction::FAdd:
  4745. return SimplifyFAddInst(LHS, RHS, FMF, Q, MaxRecurse);
  4746. case Instruction::FSub:
  4747. return SimplifyFSubInst(LHS, RHS, FMF, Q, MaxRecurse);
  4748. case Instruction::FMul:
  4749. return SimplifyFMulInst(LHS, RHS, FMF, Q, MaxRecurse);
  4750. case Instruction::FDiv:
  4751. return SimplifyFDivInst(LHS, RHS, FMF, Q, MaxRecurse);
  4752. default:
  4753. return SimplifyBinOp(Opcode, LHS, RHS, Q, MaxRecurse);
  4754. }
  4755. }
  4756. Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  4757. const SimplifyQuery &Q) {
  4758. return ::SimplifyBinOp(Opcode, LHS, RHS, Q, RecursionLimit);
  4759. }
  4760. Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
  4761. FastMathFlags FMF, const SimplifyQuery &Q) {
  4762. return ::SimplifyBinOp(Opcode, LHS, RHS, FMF, Q, RecursionLimit);
  4763. }
  4764. /// Given operands for a CmpInst, see if we can fold the result.
  4765. static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  4766. const SimplifyQuery &Q, unsigned MaxRecurse) {
  4767. if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
  4768. return SimplifyICmpInst(Predicate, LHS, RHS, Q, MaxRecurse);
  4769. return SimplifyFCmpInst(Predicate, LHS, RHS, FastMathFlags(), Q, MaxRecurse);
  4770. }
  4771. Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
  4772. const SimplifyQuery &Q) {
  4773. return ::SimplifyCmpInst(Predicate, LHS, RHS, Q, RecursionLimit);
  4774. }
  4775. static bool IsIdempotent(Intrinsic::ID ID) {
  4776. switch (ID) {
  4777. default: return false;
  4778. // Unary idempotent: f(f(x)) = f(x)
  4779. case Intrinsic::fabs:
  4780. case Intrinsic::floor:
  4781. case Intrinsic::ceil:
  4782. case Intrinsic::trunc:
  4783. case Intrinsic::rint:
  4784. case Intrinsic::nearbyint:
  4785. case Intrinsic::round:
  4786. case Intrinsic::roundeven:
  4787. case Intrinsic::canonicalize:
  4788. return true;
  4789. }
  4790. }
  4791. static Value *SimplifyRelativeLoad(Constant *Ptr, Constant *Offset,
  4792. const DataLayout &DL) {
  4793. GlobalValue *PtrSym;
  4794. APInt PtrOffset;
  4795. if (!IsConstantOffsetFromGlobal(Ptr, PtrSym, PtrOffset, DL))
  4796. return nullptr;
  4797. Type *Int8PtrTy = Type::getInt8PtrTy(Ptr->getContext());
  4798. Type *Int32Ty = Type::getInt32Ty(Ptr->getContext());
  4799. Type *Int32PtrTy = Int32Ty->getPointerTo();
  4800. Type *Int64Ty = Type::getInt64Ty(Ptr->getContext());
  4801. auto *OffsetConstInt = dyn_cast<ConstantInt>(Offset);
  4802. if (!OffsetConstInt || OffsetConstInt->getType()->getBitWidth() > 64)
  4803. return nullptr;
  4804. uint64_t OffsetInt = OffsetConstInt->getSExtValue();
  4805. if (OffsetInt % 4 != 0)
  4806. return nullptr;
  4807. Constant *C = ConstantExpr::getGetElementPtr(
  4808. Int32Ty, ConstantExpr::getBitCast(Ptr, Int32PtrTy),
  4809. ConstantInt::get(Int64Ty, OffsetInt / 4));
  4810. Constant *Loaded = ConstantFoldLoadFromConstPtr(C, Int32Ty, DL);
  4811. if (!Loaded)
  4812. return nullptr;
  4813. auto *LoadedCE = dyn_cast<ConstantExpr>(Loaded);
  4814. if (!LoadedCE)
  4815. return nullptr;
  4816. if (LoadedCE->getOpcode() == Instruction::Trunc) {
  4817. LoadedCE = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
  4818. if (!LoadedCE)
  4819. return nullptr;
  4820. }
  4821. if (LoadedCE->getOpcode() != Instruction::Sub)
  4822. return nullptr;
  4823. auto *LoadedLHS = dyn_cast<ConstantExpr>(LoadedCE->getOperand(0));
  4824. if (!LoadedLHS || LoadedLHS->getOpcode() != Instruction::PtrToInt)
  4825. return nullptr;
  4826. auto *LoadedLHSPtr = LoadedLHS->getOperand(0);
  4827. Constant *LoadedRHS = LoadedCE->getOperand(1);
  4828. GlobalValue *LoadedRHSSym;
  4829. APInt LoadedRHSOffset;
  4830. if (!IsConstantOffsetFromGlobal(LoadedRHS, LoadedRHSSym, LoadedRHSOffset,
  4831. DL) ||
  4832. PtrSym != LoadedRHSSym || PtrOffset != LoadedRHSOffset)
  4833. return nullptr;
  4834. return ConstantExpr::getBitCast(LoadedLHSPtr, Int8PtrTy);
  4835. }
  4836. static Value *simplifyUnaryIntrinsic(Function *F, Value *Op0,
  4837. const SimplifyQuery &Q) {
  4838. // Idempotent functions return the same result when called repeatedly.
  4839. Intrinsic::ID IID = F->getIntrinsicID();
  4840. if (IsIdempotent(IID))
  4841. if (auto *II = dyn_cast<IntrinsicInst>(Op0))
  4842. if (II->getIntrinsicID() == IID)
  4843. return II;
  4844. Value *X;
  4845. switch (IID) {
  4846. case Intrinsic::fabs:
  4847. if (SignBitMustBeZero(Op0, Q.TLI)) return Op0;
  4848. break;
  4849. case Intrinsic::bswap:
  4850. // bswap(bswap(x)) -> x
  4851. if (match(Op0, m_BSwap(m_Value(X)))) return X;
  4852. break;
  4853. case Intrinsic::bitreverse:
  4854. // bitreverse(bitreverse(x)) -> x
  4855. if (match(Op0, m_BitReverse(m_Value(X)))) return X;
  4856. break;
  4857. case Intrinsic::ctpop: {
  4858. // If everything but the lowest bit is zero, that bit is the pop-count. Ex:
  4859. // ctpop(and X, 1) --> and X, 1
  4860. unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
  4861. if (MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, BitWidth - 1),
  4862. Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
  4863. return Op0;
  4864. break;
  4865. }
  4866. case Intrinsic::exp:
  4867. // exp(log(x)) -> x
  4868. if (Q.CxtI->hasAllowReassoc() &&
  4869. match(Op0, m_Intrinsic<Intrinsic::log>(m_Value(X)))) return X;
  4870. break;
  4871. case Intrinsic::exp2:
  4872. // exp2(log2(x)) -> x
  4873. if (Q.CxtI->hasAllowReassoc() &&
  4874. match(Op0, m_Intrinsic<Intrinsic::log2>(m_Value(X)))) return X;
  4875. break;
  4876. case Intrinsic::log:
  4877. // log(exp(x)) -> x
  4878. if (Q.CxtI->hasAllowReassoc() &&
  4879. match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X)))) return X;
  4880. break;
  4881. case Intrinsic::log2:
  4882. // log2(exp2(x)) -> x
  4883. if (Q.CxtI->hasAllowReassoc() &&
  4884. (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) ||
  4885. match(Op0, m_Intrinsic<Intrinsic::pow>(m_SpecificFP(2.0),
  4886. m_Value(X))))) return X;
  4887. break;
  4888. case Intrinsic::log10:
  4889. // log10(pow(10.0, x)) -> x
  4890. if (Q.CxtI->hasAllowReassoc() &&
  4891. match(Op0, m_Intrinsic<Intrinsic::pow>(m_SpecificFP(10.0),
  4892. m_Value(X)))) return X;
  4893. break;
  4894. case Intrinsic::floor:
  4895. case Intrinsic::trunc:
  4896. case Intrinsic::ceil:
  4897. case Intrinsic::round:
  4898. case Intrinsic::roundeven:
  4899. case Intrinsic::nearbyint:
  4900. case Intrinsic::rint: {
  4901. // floor (sitofp x) -> sitofp x
  4902. // floor (uitofp x) -> uitofp x
  4903. //
  4904. // Converting from int always results in a finite integral number or
  4905. // infinity. For either of those inputs, these rounding functions always
  4906. // return the same value, so the rounding can be eliminated.
  4907. if (match(Op0, m_SIToFP(m_Value())) || match(Op0, m_UIToFP(m_Value())))
  4908. return Op0;
  4909. break;
  4910. }
  4911. case Intrinsic::experimental_vector_reverse:
  4912. // experimental.vector.reverse(experimental.vector.reverse(x)) -> x
  4913. if (match(Op0,
  4914. m_Intrinsic<Intrinsic::experimental_vector_reverse>(m_Value(X))))
  4915. return X;
  4916. // experimental.vector.reverse(splat(X)) -> splat(X)
  4917. if (isSplatValue(Op0))
  4918. return Op0;
  4919. break;
  4920. default:
  4921. break;
  4922. }
  4923. return nullptr;
  4924. }
  4925. /// Given a min/max intrinsic, see if it can be removed based on having an
  4926. /// operand that is another min/max intrinsic with shared operand(s). The caller
  4927. /// is expected to swap the operand arguments to handle commutation.
  4928. static Value *foldMinMaxSharedOp(Intrinsic::ID IID, Value *Op0, Value *Op1) {
  4929. Value *X, *Y;
  4930. if (!match(Op0, m_MaxOrMin(m_Value(X), m_Value(Y))))
  4931. return nullptr;
  4932. auto *MM0 = dyn_cast<IntrinsicInst>(Op0);
  4933. if (!MM0)
  4934. return nullptr;
  4935. Intrinsic::ID IID0 = MM0->getIntrinsicID();
  4936. if (Op1 == X || Op1 == Y ||
  4937. match(Op1, m_c_MaxOrMin(m_Specific(X), m_Specific(Y)))) {
  4938. // max (max X, Y), X --> max X, Y
  4939. if (IID0 == IID)
  4940. return MM0;
  4941. // max (min X, Y), X --> X
  4942. if (IID0 == getInverseMinMaxIntrinsic(IID))
  4943. return Op1;
  4944. }
  4945. return nullptr;
  4946. }
  4947. static Value *simplifyBinaryIntrinsic(Function *F, Value *Op0, Value *Op1,
  4948. const SimplifyQuery &Q) {
  4949. Intrinsic::ID IID = F->getIntrinsicID();
  4950. Type *ReturnType = F->getReturnType();
  4951. unsigned BitWidth = ReturnType->getScalarSizeInBits();
  4952. switch (IID) {
  4953. case Intrinsic::abs:
  4954. // abs(abs(x)) -> abs(x). We don't need to worry about the nsw arg here.
  4955. // It is always ok to pick the earlier abs. We'll just lose nsw if its only
  4956. // on the outer abs.
  4957. if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(), m_Value())))
  4958. return Op0;
  4959. break;
  4960. case Intrinsic::cttz: {
  4961. Value *X;
  4962. if (match(Op0, m_Shl(m_One(), m_Value(X))))
  4963. return X;
  4964. break;
  4965. }
  4966. case Intrinsic::ctlz: {
  4967. Value *X;
  4968. if (match(Op0, m_LShr(m_Negative(), m_Value(X))))
  4969. return X;
  4970. if (match(Op0, m_AShr(m_Negative(), m_Value())))
  4971. return Constant::getNullValue(ReturnType);
  4972. break;
  4973. }
  4974. case Intrinsic::smax:
  4975. case Intrinsic::smin:
  4976. case Intrinsic::umax:
  4977. case Intrinsic::umin: {
  4978. // If the arguments are the same, this is a no-op.
  4979. if (Op0 == Op1)
  4980. return Op0;
  4981. // Canonicalize constant operand as Op1.
  4982. if (isa<Constant>(Op0))
  4983. std::swap(Op0, Op1);
  4984. // Assume undef is the limit value.
  4985. if (Q.isUndefValue(Op1))
  4986. return ConstantInt::get(
  4987. ReturnType, MinMaxIntrinsic::getSaturationPoint(IID, BitWidth));
  4988. const APInt *C;
  4989. if (match(Op1, m_APIntAllowUndef(C))) {
  4990. // Clamp to limit value. For example:
  4991. // umax(i8 %x, i8 255) --> 255
  4992. if (*C == MinMaxIntrinsic::getSaturationPoint(IID, BitWidth))
  4993. return ConstantInt::get(ReturnType, *C);
  4994. // If the constant op is the opposite of the limit value, the other must
  4995. // be larger/smaller or equal. For example:
  4996. // umin(i8 %x, i8 255) --> %x
  4997. if (*C == MinMaxIntrinsic::getSaturationPoint(
  4998. getInverseMinMaxIntrinsic(IID), BitWidth))
  4999. return Op0;
  5000. // Remove nested call if constant operands allow it. Example:
  5001. // max (max X, 7), 5 -> max X, 7
  5002. auto *MinMax0 = dyn_cast<IntrinsicInst>(Op0);
  5003. if (MinMax0 && MinMax0->getIntrinsicID() == IID) {
  5004. // TODO: loosen undef/splat restrictions for vector constants.
  5005. Value *M00 = MinMax0->getOperand(0), *M01 = MinMax0->getOperand(1);
  5006. const APInt *InnerC;
  5007. if ((match(M00, m_APInt(InnerC)) || match(M01, m_APInt(InnerC))) &&
  5008. ICmpInst::compare(*InnerC, *C,
  5009. ICmpInst::getNonStrictPredicate(
  5010. MinMaxIntrinsic::getPredicate(IID))))
  5011. return Op0;
  5012. }
  5013. }
  5014. if (Value *V = foldMinMaxSharedOp(IID, Op0, Op1))
  5015. return V;
  5016. if (Value *V = foldMinMaxSharedOp(IID, Op1, Op0))
  5017. return V;
  5018. ICmpInst::Predicate Pred =
  5019. ICmpInst::getNonStrictPredicate(MinMaxIntrinsic::getPredicate(IID));
  5020. if (isICmpTrue(Pred, Op0, Op1, Q.getWithoutUndef(), RecursionLimit))
  5021. return Op0;
  5022. if (isICmpTrue(Pred, Op1, Op0, Q.getWithoutUndef(), RecursionLimit))
  5023. return Op1;
  5024. if (Optional<bool> Imp =
  5025. isImpliedByDomCondition(Pred, Op0, Op1, Q.CxtI, Q.DL))
  5026. return *Imp ? Op0 : Op1;
  5027. if (Optional<bool> Imp =
  5028. isImpliedByDomCondition(Pred, Op1, Op0, Q.CxtI, Q.DL))
  5029. return *Imp ? Op1 : Op0;
  5030. break;
  5031. }
  5032. case Intrinsic::usub_with_overflow:
  5033. case Intrinsic::ssub_with_overflow:
  5034. // X - X -> { 0, false }
  5035. // X - undef -> { 0, false }
  5036. // undef - X -> { 0, false }
  5037. if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
  5038. return Constant::getNullValue(ReturnType);
  5039. break;
  5040. case Intrinsic::uadd_with_overflow:
  5041. case Intrinsic::sadd_with_overflow:
  5042. // X + undef -> { -1, false }
  5043. // undef + x -> { -1, false }
  5044. if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1)) {
  5045. return ConstantStruct::get(
  5046. cast<StructType>(ReturnType),
  5047. {Constant::getAllOnesValue(ReturnType->getStructElementType(0)),
  5048. Constant::getNullValue(ReturnType->getStructElementType(1))});
  5049. }
  5050. break;
  5051. case Intrinsic::umul_with_overflow:
  5052. case Intrinsic::smul_with_overflow:
  5053. // 0 * X -> { 0, false }
  5054. // X * 0 -> { 0, false }
  5055. if (match(Op0, m_Zero()) || match(Op1, m_Zero()))
  5056. return Constant::getNullValue(ReturnType);
  5057. // undef * X -> { 0, false }
  5058. // X * undef -> { 0, false }
  5059. if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
  5060. return Constant::getNullValue(ReturnType);
  5061. break;
  5062. case Intrinsic::uadd_sat:
  5063. // sat(MAX + X) -> MAX
  5064. // sat(X + MAX) -> MAX
  5065. if (match(Op0, m_AllOnes()) || match(Op1, m_AllOnes()))
  5066. return Constant::getAllOnesValue(ReturnType);
  5067. LLVM_FALLTHROUGH;
  5068. case Intrinsic::sadd_sat:
  5069. // sat(X + undef) -> -1
  5070. // sat(undef + X) -> -1
  5071. // For unsigned: Assume undef is MAX, thus we saturate to MAX (-1).
  5072. // For signed: Assume undef is ~X, in which case X + ~X = -1.
  5073. if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
  5074. return Constant::getAllOnesValue(ReturnType);
  5075. // X + 0 -> X
  5076. if (match(Op1, m_Zero()))
  5077. return Op0;
  5078. // 0 + X -> X
  5079. if (match(Op0, m_Zero()))
  5080. return Op1;
  5081. break;
  5082. case Intrinsic::usub_sat:
  5083. // sat(0 - X) -> 0, sat(X - MAX) -> 0
  5084. if (match(Op0, m_Zero()) || match(Op1, m_AllOnes()))
  5085. return Constant::getNullValue(ReturnType);
  5086. LLVM_FALLTHROUGH;
  5087. case Intrinsic::ssub_sat:
  5088. // X - X -> 0, X - undef -> 0, undef - X -> 0
  5089. if (Op0 == Op1 || Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
  5090. return Constant::getNullValue(ReturnType);
  5091. // X - 0 -> X
  5092. if (match(Op1, m_Zero()))
  5093. return Op0;
  5094. break;
  5095. case Intrinsic::load_relative:
  5096. if (auto *C0 = dyn_cast<Constant>(Op0))
  5097. if (auto *C1 = dyn_cast<Constant>(Op1))
  5098. return SimplifyRelativeLoad(C0, C1, Q.DL);
  5099. break;
  5100. case Intrinsic::powi:
  5101. if (auto *Power = dyn_cast<ConstantInt>(Op1)) {
  5102. // powi(x, 0) -> 1.0
  5103. if (Power->isZero())
  5104. return ConstantFP::get(Op0->getType(), 1.0);
  5105. // powi(x, 1) -> x
  5106. if (Power->isOne())
  5107. return Op0;
  5108. }
  5109. break;
  5110. case Intrinsic::copysign:
  5111. // copysign X, X --> X
  5112. if (Op0 == Op1)
  5113. return Op0;
  5114. // copysign -X, X --> X
  5115. // copysign X, -X --> -X
  5116. if (match(Op0, m_FNeg(m_Specific(Op1))) ||
  5117. match(Op1, m_FNeg(m_Specific(Op0))))
  5118. return Op1;
  5119. break;
  5120. case Intrinsic::maxnum:
  5121. case Intrinsic::minnum:
  5122. case Intrinsic::maximum:
  5123. case Intrinsic::minimum: {
  5124. // If the arguments are the same, this is a no-op.
  5125. if (Op0 == Op1) return Op0;
  5126. // Canonicalize constant operand as Op1.
  5127. if (isa<Constant>(Op0))
  5128. std::swap(Op0, Op1);
  5129. // If an argument is undef, return the other argument.
  5130. if (Q.isUndefValue(Op1))
  5131. return Op0;
  5132. bool PropagateNaN = IID == Intrinsic::minimum || IID == Intrinsic::maximum;
  5133. bool IsMin = IID == Intrinsic::minimum || IID == Intrinsic::minnum;
  5134. // minnum(X, nan) -> X
  5135. // maxnum(X, nan) -> X
  5136. // minimum(X, nan) -> nan
  5137. // maximum(X, nan) -> nan
  5138. if (match(Op1, m_NaN()))
  5139. return PropagateNaN ? propagateNaN(cast<Constant>(Op1)) : Op0;
  5140. // In the following folds, inf can be replaced with the largest finite
  5141. // float, if the ninf flag is set.
  5142. const APFloat *C;
  5143. if (match(Op1, m_APFloat(C)) &&
  5144. (C->isInfinity() || (Q.CxtI->hasNoInfs() && C->isLargest()))) {
  5145. // minnum(X, -inf) -> -inf
  5146. // maxnum(X, +inf) -> +inf
  5147. // minimum(X, -inf) -> -inf if nnan
  5148. // maximum(X, +inf) -> +inf if nnan
  5149. if (C->isNegative() == IsMin && (!PropagateNaN || Q.CxtI->hasNoNaNs()))
  5150. return ConstantFP::get(ReturnType, *C);
  5151. // minnum(X, +inf) -> X if nnan
  5152. // maxnum(X, -inf) -> X if nnan
  5153. // minimum(X, +inf) -> X
  5154. // maximum(X, -inf) -> X
  5155. if (C->isNegative() != IsMin && (PropagateNaN || Q.CxtI->hasNoNaNs()))
  5156. return Op0;
  5157. }
  5158. // Min/max of the same operation with common operand:
  5159. // m(m(X, Y)), X --> m(X, Y) (4 commuted variants)
  5160. if (auto *M0 = dyn_cast<IntrinsicInst>(Op0))
  5161. if (M0->getIntrinsicID() == IID &&
  5162. (M0->getOperand(0) == Op1 || M0->getOperand(1) == Op1))
  5163. return Op0;
  5164. if (auto *M1 = dyn_cast<IntrinsicInst>(Op1))
  5165. if (M1->getIntrinsicID() == IID &&
  5166. (M1->getOperand(0) == Op0 || M1->getOperand(1) == Op0))
  5167. return Op1;
  5168. break;
  5169. }
  5170. case Intrinsic::experimental_vector_extract: {
  5171. Type *ReturnType = F->getReturnType();
  5172. // (extract_vector (insert_vector _, X, 0), 0) -> X
  5173. unsigned IdxN = cast<ConstantInt>(Op1)->getZExtValue();
  5174. Value *X = nullptr;
  5175. if (match(Op0, m_Intrinsic<Intrinsic::experimental_vector_insert>(
  5176. m_Value(), m_Value(X), m_Zero())) &&
  5177. IdxN == 0 && X->getType() == ReturnType)
  5178. return X;
  5179. break;
  5180. }
  5181. default:
  5182. break;
  5183. }
  5184. return nullptr;
  5185. }
  5186. static Value *simplifyIntrinsic(CallBase *Call, const SimplifyQuery &Q) {
  5187. unsigned NumOperands = Call->arg_size();
  5188. Function *F = cast<Function>(Call->getCalledFunction());
  5189. Intrinsic::ID IID = F->getIntrinsicID();
  5190. // Most of the intrinsics with no operands have some kind of side effect.
  5191. // Don't simplify.
  5192. if (!NumOperands) {
  5193. switch (IID) {
  5194. case Intrinsic::vscale: {
  5195. // Call may not be inserted into the IR yet at point of calling simplify.
  5196. if (!Call->getParent() || !Call->getParent()->getParent())
  5197. return nullptr;
  5198. auto Attr = Call->getFunction()->getFnAttribute(Attribute::VScaleRange);
  5199. if (!Attr.isValid())
  5200. return nullptr;
  5201. unsigned VScaleMin = Attr.getVScaleRangeMin();
  5202. Optional<unsigned> VScaleMax = Attr.getVScaleRangeMax();
  5203. if (VScaleMax && VScaleMin == VScaleMax)
  5204. return ConstantInt::get(F->getReturnType(), VScaleMin);
  5205. return nullptr;
  5206. }
  5207. default:
  5208. return nullptr;
  5209. }
  5210. }
  5211. if (NumOperands == 1)
  5212. return simplifyUnaryIntrinsic(F, Call->getArgOperand(0), Q);
  5213. if (NumOperands == 2)
  5214. return simplifyBinaryIntrinsic(F, Call->getArgOperand(0),
  5215. Call->getArgOperand(1), Q);
  5216. // Handle intrinsics with 3 or more arguments.
  5217. switch (IID) {
  5218. case Intrinsic::masked_load:
  5219. case Intrinsic::masked_gather: {
  5220. Value *MaskArg = Call->getArgOperand(2);
  5221. Value *PassthruArg = Call->getArgOperand(3);
  5222. // If the mask is all zeros or undef, the "passthru" argument is the result.
  5223. if (maskIsAllZeroOrUndef(MaskArg))
  5224. return PassthruArg;
  5225. return nullptr;
  5226. }
  5227. case Intrinsic::fshl:
  5228. case Intrinsic::fshr: {
  5229. Value *Op0 = Call->getArgOperand(0), *Op1 = Call->getArgOperand(1),
  5230. *ShAmtArg = Call->getArgOperand(2);
  5231. // If both operands are undef, the result is undef.
  5232. if (Q.isUndefValue(Op0) && Q.isUndefValue(Op1))
  5233. return UndefValue::get(F->getReturnType());
  5234. // If shift amount is undef, assume it is zero.
  5235. if (Q.isUndefValue(ShAmtArg))
  5236. return Call->getArgOperand(IID == Intrinsic::fshl ? 0 : 1);
  5237. const APInt *ShAmtC;
  5238. if (match(ShAmtArg, m_APInt(ShAmtC))) {
  5239. // If there's effectively no shift, return the 1st arg or 2nd arg.
  5240. APInt BitWidth = APInt(ShAmtC->getBitWidth(), ShAmtC->getBitWidth());
  5241. if (ShAmtC->urem(BitWidth).isZero())
  5242. return Call->getArgOperand(IID == Intrinsic::fshl ? 0 : 1);
  5243. }
  5244. // Rotating zero by anything is zero.
  5245. if (match(Op0, m_Zero()) && match(Op1, m_Zero()))
  5246. return ConstantInt::getNullValue(F->getReturnType());
  5247. // Rotating -1 by anything is -1.
  5248. if (match(Op0, m_AllOnes()) && match(Op1, m_AllOnes()))
  5249. return ConstantInt::getAllOnesValue(F->getReturnType());
  5250. return nullptr;
  5251. }
  5252. case Intrinsic::experimental_constrained_fma: {
  5253. Value *Op0 = Call->getArgOperand(0);
  5254. Value *Op1 = Call->getArgOperand(1);
  5255. Value *Op2 = Call->getArgOperand(2);
  5256. auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
  5257. if (Value *V = simplifyFPOp({Op0, Op1, Op2}, {}, Q,
  5258. FPI->getExceptionBehavior().getValue(),
  5259. FPI->getRoundingMode().getValue()))
  5260. return V;
  5261. return nullptr;
  5262. }
  5263. case Intrinsic::fma:
  5264. case Intrinsic::fmuladd: {
  5265. Value *Op0 = Call->getArgOperand(0);
  5266. Value *Op1 = Call->getArgOperand(1);
  5267. Value *Op2 = Call->getArgOperand(2);
  5268. if (Value *V = simplifyFPOp({Op0, Op1, Op2}, {}, Q, fp::ebIgnore,
  5269. RoundingMode::NearestTiesToEven))
  5270. return V;
  5271. return nullptr;
  5272. }
  5273. case Intrinsic::smul_fix:
  5274. case Intrinsic::smul_fix_sat: {
  5275. Value *Op0 = Call->getArgOperand(0);
  5276. Value *Op1 = Call->getArgOperand(1);
  5277. Value *Op2 = Call->getArgOperand(2);
  5278. Type *ReturnType = F->getReturnType();
  5279. // Canonicalize constant operand as Op1 (ConstantFolding handles the case
  5280. // when both Op0 and Op1 are constant so we do not care about that special
  5281. // case here).
  5282. if (isa<Constant>(Op0))
  5283. std::swap(Op0, Op1);
  5284. // X * 0 -> 0
  5285. if (match(Op1, m_Zero()))
  5286. return Constant::getNullValue(ReturnType);
  5287. // X * undef -> 0
  5288. if (Q.isUndefValue(Op1))
  5289. return Constant::getNullValue(ReturnType);
  5290. // X * (1 << Scale) -> X
  5291. APInt ScaledOne =
  5292. APInt::getOneBitSet(ReturnType->getScalarSizeInBits(),
  5293. cast<ConstantInt>(Op2)->getZExtValue());
  5294. if (ScaledOne.isNonNegative() && match(Op1, m_SpecificInt(ScaledOne)))
  5295. return Op0;
  5296. return nullptr;
  5297. }
  5298. case Intrinsic::experimental_vector_insert: {
  5299. Value *Vec = Call->getArgOperand(0);
  5300. Value *SubVec = Call->getArgOperand(1);
  5301. Value *Idx = Call->getArgOperand(2);
  5302. Type *ReturnType = F->getReturnType();
  5303. // (insert_vector Y, (extract_vector X, 0), 0) -> X
  5304. // where: Y is X, or Y is undef
  5305. unsigned IdxN = cast<ConstantInt>(Idx)->getZExtValue();
  5306. Value *X = nullptr;
  5307. if (match(SubVec, m_Intrinsic<Intrinsic::experimental_vector_extract>(
  5308. m_Value(X), m_Zero())) &&
  5309. (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 &&
  5310. X->getType() == ReturnType)
  5311. return X;
  5312. return nullptr;
  5313. }
  5314. case Intrinsic::experimental_constrained_fadd: {
  5315. auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
  5316. return SimplifyFAddInst(FPI->getArgOperand(0), FPI->getArgOperand(1),
  5317. FPI->getFastMathFlags(), Q,
  5318. FPI->getExceptionBehavior().getValue(),
  5319. FPI->getRoundingMode().getValue());
  5320. break;
  5321. }
  5322. case Intrinsic::experimental_constrained_fsub: {
  5323. auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
  5324. return SimplifyFSubInst(FPI->getArgOperand(0), FPI->getArgOperand(1),
  5325. FPI->getFastMathFlags(), Q,
  5326. FPI->getExceptionBehavior().getValue(),
  5327. FPI->getRoundingMode().getValue());
  5328. break;
  5329. }
  5330. case Intrinsic::experimental_constrained_fmul: {
  5331. auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
  5332. return SimplifyFMulInst(FPI->getArgOperand(0), FPI->getArgOperand(1),
  5333. FPI->getFastMathFlags(), Q,
  5334. FPI->getExceptionBehavior().getValue(),
  5335. FPI->getRoundingMode().getValue());
  5336. break;
  5337. }
  5338. case Intrinsic::experimental_constrained_fdiv: {
  5339. auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
  5340. return SimplifyFDivInst(FPI->getArgOperand(0), FPI->getArgOperand(1),
  5341. FPI->getFastMathFlags(), Q,
  5342. FPI->getExceptionBehavior().getValue(),
  5343. FPI->getRoundingMode().getValue());
  5344. break;
  5345. }
  5346. case Intrinsic::experimental_constrained_frem: {
  5347. auto *FPI = cast<ConstrainedFPIntrinsic>(Call);
  5348. return SimplifyFRemInst(FPI->getArgOperand(0), FPI->getArgOperand(1),
  5349. FPI->getFastMathFlags(), Q,
  5350. FPI->getExceptionBehavior().getValue(),
  5351. FPI->getRoundingMode().getValue());
  5352. break;
  5353. }
  5354. default:
  5355. return nullptr;
  5356. }
  5357. }
  5358. static Value *tryConstantFoldCall(CallBase *Call, const SimplifyQuery &Q) {
  5359. auto *F = dyn_cast<Function>(Call->getCalledOperand());
  5360. if (!F || !canConstantFoldCallTo(Call, F))
  5361. return nullptr;
  5362. SmallVector<Constant *, 4> ConstantArgs;
  5363. unsigned NumArgs = Call->arg_size();
  5364. ConstantArgs.reserve(NumArgs);
  5365. for (auto &Arg : Call->args()) {
  5366. Constant *C = dyn_cast<Constant>(&Arg);
  5367. if (!C) {
  5368. if (isa<MetadataAsValue>(Arg.get()))
  5369. continue;
  5370. return nullptr;
  5371. }
  5372. ConstantArgs.push_back(C);
  5373. }
  5374. return ConstantFoldCall(Call, F, ConstantArgs, Q.TLI);
  5375. }
  5376. Value *llvm::SimplifyCall(CallBase *Call, const SimplifyQuery &Q) {
  5377. // musttail calls can only be simplified if they are also DCEd.
  5378. // As we can't guarantee this here, don't simplify them.
  5379. if (Call->isMustTailCall())
  5380. return nullptr;
  5381. // call undef -> poison
  5382. // call null -> poison
  5383. Value *Callee = Call->getCalledOperand();
  5384. if (isa<UndefValue>(Callee) || isa<ConstantPointerNull>(Callee))
  5385. return PoisonValue::get(Call->getType());
  5386. if (Value *V = tryConstantFoldCall(Call, Q))
  5387. return V;
  5388. auto *F = dyn_cast<Function>(Callee);
  5389. if (F && F->isIntrinsic())
  5390. if (Value *Ret = simplifyIntrinsic(Call, Q))
  5391. return Ret;
  5392. return nullptr;
  5393. }
  5394. /// Given operands for a Freeze, see if we can fold the result.
  5395. static Value *SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
  5396. // Use a utility function defined in ValueTracking.
  5397. if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT))
  5398. return Op0;
  5399. // We have room for improvement.
  5400. return nullptr;
  5401. }
  5402. Value *llvm::SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
  5403. return ::SimplifyFreezeInst(Op0, Q);
  5404. }
  5405. static Value *SimplifyLoadInst(LoadInst *LI, Value *PtrOp,
  5406. const SimplifyQuery &Q) {
  5407. if (LI->isVolatile())
  5408. return nullptr;
  5409. APInt Offset(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()), 0);
  5410. auto *PtrOpC = dyn_cast<Constant>(PtrOp);
  5411. // Try to convert operand into a constant by stripping offsets while looking
  5412. // through invariant.group intrinsics. Don't bother if the underlying object
  5413. // is not constant, as calculating GEP offsets is expensive.
  5414. if (!PtrOpC && isa<Constant>(getUnderlyingObject(PtrOp))) {
  5415. PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
  5416. Q.DL, Offset, /* AllowNonInbounts */ true,
  5417. /* AllowInvariantGroup */ true);
  5418. // Index size may have changed due to address space casts.
  5419. Offset = Offset.sextOrTrunc(Q.DL.getIndexTypeSizeInBits(PtrOp->getType()));
  5420. PtrOpC = dyn_cast<Constant>(PtrOp);
  5421. }
  5422. if (PtrOpC)
  5423. return ConstantFoldLoadFromConstPtr(PtrOpC, LI->getType(), Offset, Q.DL);
  5424. return nullptr;
  5425. }
  5426. /// See if we can compute a simplified version of this instruction.
  5427. /// If not, this returns null.
  5428. static Value *simplifyInstructionWithOperands(Instruction *I,
  5429. ArrayRef<Value *> NewOps,
  5430. const SimplifyQuery &SQ,
  5431. OptimizationRemarkEmitter *ORE) {
  5432. const SimplifyQuery Q = SQ.CxtI ? SQ : SQ.getWithInstruction(I);
  5433. Value *Result = nullptr;
  5434. switch (I->getOpcode()) {
  5435. default:
  5436. if (llvm::all_of(NewOps, [](Value *V) { return isa<Constant>(V); })) {
  5437. SmallVector<Constant *, 8> NewConstOps(NewOps.size());
  5438. transform(NewOps, NewConstOps.begin(),
  5439. [](Value *V) { return cast<Constant>(V); });
  5440. Result = ConstantFoldInstOperands(I, NewConstOps, Q.DL, Q.TLI);
  5441. }
  5442. break;
  5443. case Instruction::FNeg:
  5444. Result = SimplifyFNegInst(NewOps[0], I->getFastMathFlags(), Q);
  5445. break;
  5446. case Instruction::FAdd:
  5447. Result = SimplifyFAddInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q);
  5448. break;
  5449. case Instruction::Add:
  5450. Result = SimplifyAddInst(
  5451. NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
  5452. Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q);
  5453. break;
  5454. case Instruction::FSub:
  5455. Result = SimplifyFSubInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q);
  5456. break;
  5457. case Instruction::Sub:
  5458. Result = SimplifySubInst(
  5459. NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
  5460. Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q);
  5461. break;
  5462. case Instruction::FMul:
  5463. Result = SimplifyFMulInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q);
  5464. break;
  5465. case Instruction::Mul:
  5466. Result = SimplifyMulInst(NewOps[0], NewOps[1], Q);
  5467. break;
  5468. case Instruction::SDiv:
  5469. Result = SimplifySDivInst(NewOps[0], NewOps[1], Q);
  5470. break;
  5471. case Instruction::UDiv:
  5472. Result = SimplifyUDivInst(NewOps[0], NewOps[1], Q);
  5473. break;
  5474. case Instruction::FDiv:
  5475. Result = SimplifyFDivInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q);
  5476. break;
  5477. case Instruction::SRem:
  5478. Result = SimplifySRemInst(NewOps[0], NewOps[1], Q);
  5479. break;
  5480. case Instruction::URem:
  5481. Result = SimplifyURemInst(NewOps[0], NewOps[1], Q);
  5482. break;
  5483. case Instruction::FRem:
  5484. Result = SimplifyFRemInst(NewOps[0], NewOps[1], I->getFastMathFlags(), Q);
  5485. break;
  5486. case Instruction::Shl:
  5487. Result = SimplifyShlInst(
  5488. NewOps[0], NewOps[1], Q.IIQ.hasNoSignedWrap(cast<BinaryOperator>(I)),
  5489. Q.IIQ.hasNoUnsignedWrap(cast<BinaryOperator>(I)), Q);
  5490. break;
  5491. case Instruction::LShr:
  5492. Result = SimplifyLShrInst(NewOps[0], NewOps[1],
  5493. Q.IIQ.isExact(cast<BinaryOperator>(I)), Q);
  5494. break;
  5495. case Instruction::AShr:
  5496. Result = SimplifyAShrInst(NewOps[0], NewOps[1],
  5497. Q.IIQ.isExact(cast<BinaryOperator>(I)), Q);
  5498. break;
  5499. case Instruction::And:
  5500. Result = SimplifyAndInst(NewOps[0], NewOps[1], Q);
  5501. break;
  5502. case Instruction::Or:
  5503. Result = SimplifyOrInst(NewOps[0], NewOps[1], Q);
  5504. break;
  5505. case Instruction::Xor:
  5506. Result = SimplifyXorInst(NewOps[0], NewOps[1], Q);
  5507. break;
  5508. case Instruction::ICmp:
  5509. Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), NewOps[0],
  5510. NewOps[1], Q);
  5511. break;
  5512. case Instruction::FCmp:
  5513. Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), NewOps[0],
  5514. NewOps[1], I->getFastMathFlags(), Q);
  5515. break;
  5516. case Instruction::Select:
  5517. Result = SimplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q);
  5518. break;
  5519. case Instruction::GetElementPtr: {
  5520. auto *GEPI = cast<GetElementPtrInst>(I);
  5521. Result =
  5522. SimplifyGEPInst(GEPI->getSourceElementType(), NewOps[0],
  5523. makeArrayRef(NewOps).slice(1), GEPI->isInBounds(), Q);
  5524. break;
  5525. }
  5526. case Instruction::InsertValue: {
  5527. InsertValueInst *IV = cast<InsertValueInst>(I);
  5528. Result = SimplifyInsertValueInst(NewOps[0], NewOps[1], IV->getIndices(), Q);
  5529. break;
  5530. }
  5531. case Instruction::InsertElement: {
  5532. Result = SimplifyInsertElementInst(NewOps[0], NewOps[1], NewOps[2], Q);
  5533. break;
  5534. }
  5535. case Instruction::ExtractValue: {
  5536. auto *EVI = cast<ExtractValueInst>(I);
  5537. Result = SimplifyExtractValueInst(NewOps[0], EVI->getIndices(), Q);
  5538. break;
  5539. }
  5540. case Instruction::ExtractElement: {
  5541. Result = SimplifyExtractElementInst(NewOps[0], NewOps[1], Q);
  5542. break;
  5543. }
  5544. case Instruction::ShuffleVector: {
  5545. auto *SVI = cast<ShuffleVectorInst>(I);
  5546. Result = SimplifyShuffleVectorInst(
  5547. NewOps[0], NewOps[1], SVI->getShuffleMask(), SVI->getType(), Q);
  5548. break;
  5549. }
  5550. case Instruction::PHI:
  5551. Result = SimplifyPHINode(cast<PHINode>(I), NewOps, Q);
  5552. break;
  5553. case Instruction::Call: {
  5554. // TODO: Use NewOps
  5555. Result = SimplifyCall(cast<CallInst>(I), Q);
  5556. break;
  5557. }
  5558. case Instruction::Freeze:
  5559. Result = llvm::SimplifyFreezeInst(NewOps[0], Q);
  5560. break;
  5561. #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
  5562. #include "llvm/IR/Instruction.def"
  5563. #undef HANDLE_CAST_INST
  5564. Result = SimplifyCastInst(I->getOpcode(), NewOps[0], I->getType(), Q);
  5565. break;
  5566. case Instruction::Alloca:
  5567. // No simplifications for Alloca and it can't be constant folded.
  5568. Result = nullptr;
  5569. break;
  5570. case Instruction::Load:
  5571. Result = SimplifyLoadInst(cast<LoadInst>(I), NewOps[0], Q);
  5572. break;
  5573. }
  5574. /// If called on unreachable code, the above logic may report that the
  5575. /// instruction simplified to itself. Make life easier for users by
  5576. /// detecting that case here, returning a safe value instead.
  5577. return Result == I ? UndefValue::get(I->getType()) : Result;
  5578. }
  5579. Value *llvm::SimplifyInstructionWithOperands(Instruction *I,
  5580. ArrayRef<Value *> NewOps,
  5581. const SimplifyQuery &SQ,
  5582. OptimizationRemarkEmitter *ORE) {
  5583. assert(NewOps.size() == I->getNumOperands() &&
  5584. "Number of operands should match the instruction!");
  5585. return ::simplifyInstructionWithOperands(I, NewOps, SQ, ORE);
  5586. }
  5587. Value *llvm::SimplifyInstruction(Instruction *I, const SimplifyQuery &SQ,
  5588. OptimizationRemarkEmitter *ORE) {
  5589. SmallVector<Value *, 8> Ops(I->operands());
  5590. return ::simplifyInstructionWithOperands(I, Ops, SQ, ORE);
  5591. }
  5592. /// Implementation of recursive simplification through an instruction's
  5593. /// uses.
  5594. ///
  5595. /// This is the common implementation of the recursive simplification routines.
  5596. /// If we have a pre-simplified value in 'SimpleV', that is forcibly used to
  5597. /// replace the instruction 'I'. Otherwise, we simply add 'I' to the list of
  5598. /// instructions to process and attempt to simplify it using
  5599. /// InstructionSimplify. Recursively visited users which could not be
  5600. /// simplified themselves are to the optional UnsimplifiedUsers set for
  5601. /// further processing by the caller.
  5602. ///
  5603. /// This routine returns 'true' only when *it* simplifies something. The passed
  5604. /// in simplified value does not count toward this.
  5605. static bool replaceAndRecursivelySimplifyImpl(
  5606. Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
  5607. const DominatorTree *DT, AssumptionCache *AC,
  5608. SmallSetVector<Instruction *, 8> *UnsimplifiedUsers = nullptr) {
  5609. bool Simplified = false;
  5610. SmallSetVector<Instruction *, 8> Worklist;
  5611. const DataLayout &DL = I->getModule()->getDataLayout();
  5612. // If we have an explicit value to collapse to, do that round of the
  5613. // simplification loop by hand initially.
  5614. if (SimpleV) {
  5615. for (User *U : I->users())
  5616. if (U != I)
  5617. Worklist.insert(cast<Instruction>(U));
  5618. // Replace the instruction with its simplified value.
  5619. I->replaceAllUsesWith(SimpleV);
  5620. // Gracefully handle edge cases where the instruction is not wired into any
  5621. // parent block.
  5622. if (I->getParent() && !I->isEHPad() && !I->isTerminator() &&
  5623. !I->mayHaveSideEffects())
  5624. I->eraseFromParent();
  5625. } else {
  5626. Worklist.insert(I);
  5627. }
  5628. // Note that we must test the size on each iteration, the worklist can grow.
  5629. for (unsigned Idx = 0; Idx != Worklist.size(); ++Idx) {
  5630. I = Worklist[Idx];
  5631. // See if this instruction simplifies.
  5632. SimpleV = SimplifyInstruction(I, {DL, TLI, DT, AC});
  5633. if (!SimpleV) {
  5634. if (UnsimplifiedUsers)
  5635. UnsimplifiedUsers->insert(I);
  5636. continue;
  5637. }
  5638. Simplified = true;
  5639. // Stash away all the uses of the old instruction so we can check them for
  5640. // recursive simplifications after a RAUW. This is cheaper than checking all
  5641. // uses of To on the recursive step in most cases.
  5642. for (User *U : I->users())
  5643. Worklist.insert(cast<Instruction>(U));
  5644. // Replace the instruction with its simplified value.
  5645. I->replaceAllUsesWith(SimpleV);
  5646. // Gracefully handle edge cases where the instruction is not wired into any
  5647. // parent block.
  5648. if (I->getParent() && !I->isEHPad() && !I->isTerminator() &&
  5649. !I->mayHaveSideEffects())
  5650. I->eraseFromParent();
  5651. }
  5652. return Simplified;
  5653. }
  5654. bool llvm::replaceAndRecursivelySimplify(
  5655. Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI,
  5656. const DominatorTree *DT, AssumptionCache *AC,
  5657. SmallSetVector<Instruction *, 8> *UnsimplifiedUsers) {
  5658. assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
  5659. assert(SimpleV && "Must provide a simplified value.");
  5660. return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC,
  5661. UnsimplifiedUsers);
  5662. }
  5663. namespace llvm {
  5664. const SimplifyQuery getBestSimplifyQuery(Pass &P, Function &F) {
  5665. auto *DTWP = P.getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  5666. auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
  5667. auto *TLIWP = P.getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
  5668. auto *TLI = TLIWP ? &TLIWP->getTLI(F) : nullptr;
  5669. auto *ACWP = P.getAnalysisIfAvailable<AssumptionCacheTracker>();
  5670. auto *AC = ACWP ? &ACWP->getAssumptionCache(F) : nullptr;
  5671. return {F.getParent()->getDataLayout(), TLI, DT, AC};
  5672. }
  5673. const SimplifyQuery getBestSimplifyQuery(LoopStandardAnalysisResults &AR,
  5674. const DataLayout &DL) {
  5675. return {DL, &AR.TLI, &AR.DT, &AR.AC};
  5676. }
  5677. template <class T, class... TArgs>
  5678. const SimplifyQuery getBestSimplifyQuery(AnalysisManager<T, TArgs...> &AM,
  5679. Function &F) {
  5680. auto *DT = AM.template getCachedResult<DominatorTreeAnalysis>(F);
  5681. auto *TLI = AM.template getCachedResult<TargetLibraryAnalysis>(F);
  5682. auto *AC = AM.template getCachedResult<AssumptionAnalysis>(F);
  5683. return {F.getParent()->getDataLayout(), TLI, DT, AC};
  5684. }
  5685. template const SimplifyQuery getBestSimplifyQuery(AnalysisManager<Function> &,
  5686. Function &);
  5687. }
  5688. void InstSimplifyFolder::anchor() {}