InstCombineCompares.cpp 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102
  1. //===- InstCombineCompares.cpp --------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the visitICmp and visitFCmp functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "InstCombineInternal.h"
  14. #include "llvm/ADT/APSInt.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/Analysis/ConstantFolding.h"
  17. #include "llvm/Analysis/InstructionSimplify.h"
  18. #include "llvm/Analysis/MemoryBuiltins.h"
  19. #include "llvm/IR/ConstantRange.h"
  20. #include "llvm/IR/DataLayout.h"
  21. #include "llvm/IR/GetElementPtrTypeIterator.h"
  22. #include "llvm/IR/IntrinsicInst.h"
  23. #include "llvm/IR/PatternMatch.h"
  24. #include "llvm/Support/CommandLine.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Analysis/TargetLibraryInfo.h"
  27. using namespace llvm;
  28. using namespace PatternMatch;
  29. #define DEBUG_TYPE "instcombine"
  30. // How many times is a select replaced by one of its operands?
  31. STATISTIC(NumSel, "Number of select opts");
  32. // Initialization Routines
  33. static ConstantInt *getOne(Constant *C) {
  34. return ConstantInt::get(cast<IntegerType>(C->getType()), 1);
  35. }
  36. static ConstantInt *ExtractElement(Constant *V, Constant *Idx) {
  37. return cast<ConstantInt>(ConstantExpr::getExtractElement(V, Idx));
  38. }
  39. static bool HasAddOverflow(ConstantInt *Result,
  40. ConstantInt *In1, ConstantInt *In2,
  41. bool IsSigned) {
  42. if (!IsSigned)
  43. return Result->getValue().ult(In1->getValue());
  44. if (In2->isNegative())
  45. return Result->getValue().sgt(In1->getValue());
  46. return Result->getValue().slt(In1->getValue());
  47. }
  48. /// AddWithOverflow - Compute Result = In1+In2, returning true if the result
  49. /// overflowed for this type.
  50. static bool AddWithOverflow(Constant *&Result, Constant *In1,
  51. Constant *In2, bool IsSigned = false) {
  52. Result = ConstantExpr::getAdd(In1, In2);
  53. if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
  54. for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
  55. Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
  56. if (HasAddOverflow(ExtractElement(Result, Idx),
  57. ExtractElement(In1, Idx),
  58. ExtractElement(In2, Idx),
  59. IsSigned))
  60. return true;
  61. }
  62. return false;
  63. }
  64. return HasAddOverflow(cast<ConstantInt>(Result),
  65. cast<ConstantInt>(In1), cast<ConstantInt>(In2),
  66. IsSigned);
  67. }
  68. static bool HasSubOverflow(ConstantInt *Result,
  69. ConstantInt *In1, ConstantInt *In2,
  70. bool IsSigned) {
  71. if (!IsSigned)
  72. return Result->getValue().ugt(In1->getValue());
  73. if (In2->isNegative())
  74. return Result->getValue().slt(In1->getValue());
  75. return Result->getValue().sgt(In1->getValue());
  76. }
  77. /// SubWithOverflow - Compute Result = In1-In2, returning true if the result
  78. /// overflowed for this type.
  79. static bool SubWithOverflow(Constant *&Result, Constant *In1,
  80. Constant *In2, bool IsSigned = false) {
  81. Result = ConstantExpr::getSub(In1, In2);
  82. if (VectorType *VTy = dyn_cast<VectorType>(In1->getType())) {
  83. for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
  84. Constant *Idx = ConstantInt::get(Type::getInt32Ty(In1->getContext()), i);
  85. if (HasSubOverflow(ExtractElement(Result, Idx),
  86. ExtractElement(In1, Idx),
  87. ExtractElement(In2, Idx),
  88. IsSigned))
  89. return true;
  90. }
  91. return false;
  92. }
  93. return HasSubOverflow(cast<ConstantInt>(Result),
  94. cast<ConstantInt>(In1), cast<ConstantInt>(In2),
  95. IsSigned);
  96. }
  97. /// isSignBitCheck - Given an exploded icmp instruction, return true if the
  98. /// comparison only checks the sign bit. If it only checks the sign bit, set
  99. /// TrueIfSigned if the result of the comparison is true when the input value is
  100. /// signed.
  101. static bool isSignBitCheck(ICmpInst::Predicate pred, ConstantInt *RHS,
  102. bool &TrueIfSigned) {
  103. switch (pred) {
  104. case ICmpInst::ICMP_SLT: // True if LHS s< 0
  105. TrueIfSigned = true;
  106. return RHS->isZero();
  107. case ICmpInst::ICMP_SLE: // True if LHS s<= RHS and RHS == -1
  108. TrueIfSigned = true;
  109. return RHS->isAllOnesValue();
  110. case ICmpInst::ICMP_SGT: // True if LHS s> -1
  111. TrueIfSigned = false;
  112. return RHS->isAllOnesValue();
  113. case ICmpInst::ICMP_UGT:
  114. // True if LHS u> RHS and RHS == high-bit-mask - 1
  115. TrueIfSigned = true;
  116. return RHS->isMaxValue(true);
  117. case ICmpInst::ICMP_UGE:
  118. // True if LHS u>= RHS and RHS == high-bit-mask (2^7, 2^15, 2^31, etc)
  119. TrueIfSigned = true;
  120. return RHS->getValue().isSignBit();
  121. default:
  122. return false;
  123. }
  124. }
  125. /// Returns true if the exploded icmp can be expressed as a signed comparison
  126. /// to zero and updates the predicate accordingly.
  127. /// The signedness of the comparison is preserved.
  128. static bool isSignTest(ICmpInst::Predicate &pred, const ConstantInt *RHS) {
  129. if (!ICmpInst::isSigned(pred))
  130. return false;
  131. if (RHS->isZero())
  132. return ICmpInst::isRelational(pred);
  133. if (RHS->isOne()) {
  134. if (pred == ICmpInst::ICMP_SLT) {
  135. pred = ICmpInst::ICMP_SLE;
  136. return true;
  137. }
  138. } else if (RHS->isAllOnesValue()) {
  139. if (pred == ICmpInst::ICMP_SGT) {
  140. pred = ICmpInst::ICMP_SGE;
  141. return true;
  142. }
  143. }
  144. return false;
  145. }
  146. // isHighOnes - Return true if the constant is of the form 1+0+.
  147. // This is the same as lowones(~X).
  148. static bool isHighOnes(const ConstantInt *CI) {
  149. return (~CI->getValue() + 1).isPowerOf2();
  150. }
  151. /// ComputeSignedMinMaxValuesFromKnownBits - Given a signed integer type and a
  152. /// set of known zero and one bits, compute the maximum and minimum values that
  153. /// could have the specified known zero and known one bits, returning them in
  154. /// min/max.
  155. static void ComputeSignedMinMaxValuesFromKnownBits(const APInt& KnownZero,
  156. const APInt& KnownOne,
  157. APInt& Min, APInt& Max) {
  158. assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
  159. KnownZero.getBitWidth() == Min.getBitWidth() &&
  160. KnownZero.getBitWidth() == Max.getBitWidth() &&
  161. "KnownZero, KnownOne and Min, Max must have equal bitwidth.");
  162. APInt UnknownBits = ~(KnownZero|KnownOne);
  163. // The minimum value is when all unknown bits are zeros, EXCEPT for the sign
  164. // bit if it is unknown.
  165. Min = KnownOne;
  166. Max = KnownOne|UnknownBits;
  167. if (UnknownBits.isNegative()) { // Sign bit is unknown
  168. Min.setBit(Min.getBitWidth()-1);
  169. Max.clearBit(Max.getBitWidth()-1);
  170. }
  171. }
  172. // ComputeUnsignedMinMaxValuesFromKnownBits - Given an unsigned integer type and
  173. // a set of known zero and one bits, compute the maximum and minimum values that
  174. // could have the specified known zero and known one bits, returning them in
  175. // min/max.
  176. static void ComputeUnsignedMinMaxValuesFromKnownBits(const APInt &KnownZero,
  177. const APInt &KnownOne,
  178. APInt &Min, APInt &Max) {
  179. assert(KnownZero.getBitWidth() == KnownOne.getBitWidth() &&
  180. KnownZero.getBitWidth() == Min.getBitWidth() &&
  181. KnownZero.getBitWidth() == Max.getBitWidth() &&
  182. "Ty, KnownZero, KnownOne and Min, Max must have equal bitwidth.");
  183. APInt UnknownBits = ~(KnownZero|KnownOne);
  184. // The minimum value is when the unknown bits are all zeros.
  185. Min = KnownOne;
  186. // The maximum value is when the unknown bits are all ones.
  187. Max = KnownOne|UnknownBits;
  188. }
  189. /// FoldCmpLoadFromIndexedGlobal - Called we see this pattern:
  190. /// cmp pred (load (gep GV, ...)), cmpcst
  191. /// where GV is a global variable with a constant initializer. Try to simplify
  192. /// this into some simple computation that does not need the load. For example
  193. /// we can optimize "icmp eq (load (gep "foo", 0, i)), 0" into "icmp eq i, 3".
  194. ///
  195. /// If AndCst is non-null, then the loaded value is masked with that constant
  196. /// before doing the comparison. This handles cases like "A[i]&4 == 0".
  197. Instruction *InstCombiner::
  198. FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP, GlobalVariable *GV,
  199. CmpInst &ICI, ConstantInt *AndCst) {
  200. Constant *Init = GV->getInitializer();
  201. if (!isa<ConstantArray>(Init) && !isa<ConstantDataArray>(Init))
  202. return nullptr;
  203. uint64_t ArrayElementCount = Init->getType()->getArrayNumElements();
  204. if (ArrayElementCount > 1024) return nullptr; // Don't blow up on huge arrays.
  205. // There are many forms of this optimization we can handle, for now, just do
  206. // the simple index into a single-dimensional array.
  207. //
  208. // Require: GEP GV, 0, i {{, constant indices}}
  209. if (GEP->getNumOperands() < 3 ||
  210. !isa<ConstantInt>(GEP->getOperand(1)) ||
  211. !cast<ConstantInt>(GEP->getOperand(1))->isZero() ||
  212. isa<Constant>(GEP->getOperand(2)))
  213. return nullptr;
  214. // Check that indices after the variable are constants and in-range for the
  215. // type they index. Collect the indices. This is typically for arrays of
  216. // structs.
  217. SmallVector<unsigned, 4> LaterIndices;
  218. Type *EltTy = Init->getType()->getArrayElementType();
  219. for (unsigned i = 3, e = GEP->getNumOperands(); i != e; ++i) {
  220. ConstantInt *Idx = dyn_cast<ConstantInt>(GEP->getOperand(i));
  221. if (!Idx) return nullptr; // Variable index.
  222. uint64_t IdxVal = Idx->getZExtValue();
  223. if ((unsigned)IdxVal != IdxVal) return nullptr; // Too large array index.
  224. if (StructType *STy = dyn_cast<StructType>(EltTy))
  225. EltTy = STy->getElementType(IdxVal);
  226. else if (ArrayType *ATy = dyn_cast<ArrayType>(EltTy)) {
  227. if (IdxVal >= ATy->getNumElements()) return nullptr;
  228. EltTy = ATy->getElementType();
  229. } else {
  230. return nullptr; // Unknown type.
  231. }
  232. LaterIndices.push_back(IdxVal);
  233. }
  234. enum { Overdefined = -3, Undefined = -2 };
  235. // Variables for our state machines.
  236. // FirstTrueElement/SecondTrueElement - Used to emit a comparison of the form
  237. // "i == 47 | i == 87", where 47 is the first index the condition is true for,
  238. // and 87 is the second (and last) index. FirstTrueElement is -2 when
  239. // undefined, otherwise set to the first true element. SecondTrueElement is
  240. // -2 when undefined, -3 when overdefined and >= 0 when that index is true.
  241. int FirstTrueElement = Undefined, SecondTrueElement = Undefined;
  242. // FirstFalseElement/SecondFalseElement - Used to emit a comparison of the
  243. // form "i != 47 & i != 87". Same state transitions as for true elements.
  244. int FirstFalseElement = Undefined, SecondFalseElement = Undefined;
  245. /// TrueRangeEnd/FalseRangeEnd - In conjunction with First*Element, these
  246. /// define a state machine that triggers for ranges of values that the index
  247. /// is true or false for. This triggers on things like "abbbbc"[i] == 'b'.
  248. /// This is -2 when undefined, -3 when overdefined, and otherwise the last
  249. /// index in the range (inclusive). We use -2 for undefined here because we
  250. /// use relative comparisons and don't want 0-1 to match -1.
  251. int TrueRangeEnd = Undefined, FalseRangeEnd = Undefined;
  252. // MagicBitvector - This is a magic bitvector where we set a bit if the
  253. // comparison is true for element 'i'. If there are 64 elements or less in
  254. // the array, this will fully represent all the comparison results.
  255. uint64_t MagicBitvector = 0;
  256. // Scan the array and see if one of our patterns matches.
  257. Constant *CompareRHS = cast<Constant>(ICI.getOperand(1));
  258. for (unsigned i = 0, e = ArrayElementCount; i != e; ++i) {
  259. Constant *Elt = Init->getAggregateElement(i);
  260. if (!Elt) return nullptr;
  261. // If this is indexing an array of structures, get the structure element.
  262. if (!LaterIndices.empty())
  263. Elt = ConstantExpr::getExtractValue(Elt, LaterIndices);
  264. // If the element is masked, handle it.
  265. if (AndCst) Elt = ConstantExpr::getAnd(Elt, AndCst);
  266. // Find out if the comparison would be true or false for the i'th element.
  267. Constant *C = ConstantFoldCompareInstOperands(ICI.getPredicate(), Elt,
  268. CompareRHS, DL, TLI);
  269. // If the result is undef for this element, ignore it.
  270. if (isa<UndefValue>(C)) {
  271. // Extend range state machines to cover this element in case there is an
  272. // undef in the middle of the range.
  273. if (TrueRangeEnd == (int)i-1)
  274. TrueRangeEnd = i;
  275. if (FalseRangeEnd == (int)i-1)
  276. FalseRangeEnd = i;
  277. continue;
  278. }
  279. // If we can't compute the result for any of the elements, we have to give
  280. // up evaluating the entire conditional.
  281. if (!isa<ConstantInt>(C)) return nullptr;
  282. // Otherwise, we know if the comparison is true or false for this element,
  283. // update our state machines.
  284. bool IsTrueForElt = !cast<ConstantInt>(C)->isZero();
  285. // State machine for single/double/range index comparison.
  286. if (IsTrueForElt) {
  287. // Update the TrueElement state machine.
  288. if (FirstTrueElement == Undefined)
  289. FirstTrueElement = TrueRangeEnd = i; // First true element.
  290. else {
  291. // Update double-compare state machine.
  292. if (SecondTrueElement == Undefined)
  293. SecondTrueElement = i;
  294. else
  295. SecondTrueElement = Overdefined;
  296. // Update range state machine.
  297. if (TrueRangeEnd == (int)i-1)
  298. TrueRangeEnd = i;
  299. else
  300. TrueRangeEnd = Overdefined;
  301. }
  302. } else {
  303. // Update the FalseElement state machine.
  304. if (FirstFalseElement == Undefined)
  305. FirstFalseElement = FalseRangeEnd = i; // First false element.
  306. else {
  307. // Update double-compare state machine.
  308. if (SecondFalseElement == Undefined)
  309. SecondFalseElement = i;
  310. else
  311. SecondFalseElement = Overdefined;
  312. // Update range state machine.
  313. if (FalseRangeEnd == (int)i-1)
  314. FalseRangeEnd = i;
  315. else
  316. FalseRangeEnd = Overdefined;
  317. }
  318. }
  319. // If this element is in range, update our magic bitvector.
  320. if (i < 64 && IsTrueForElt)
  321. MagicBitvector |= 1ULL << i;
  322. // If all of our states become overdefined, bail out early. Since the
  323. // predicate is expensive, only check it every 8 elements. This is only
  324. // really useful for really huge arrays.
  325. if ((i & 8) == 0 && i >= 64 && SecondTrueElement == Overdefined &&
  326. SecondFalseElement == Overdefined && TrueRangeEnd == Overdefined &&
  327. FalseRangeEnd == Overdefined)
  328. return nullptr;
  329. }
  330. // Now that we've scanned the entire array, emit our new comparison(s). We
  331. // order the state machines in complexity of the generated code.
  332. Value *Idx = GEP->getOperand(2);
  333. // If the index is larger than the pointer size of the target, truncate the
  334. // index down like the GEP would do implicitly. We don't have to do this for
  335. // an inbounds GEP because the index can't be out of range.
  336. if (!GEP->isInBounds()) {
  337. Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
  338. unsigned PtrSize = IntPtrTy->getIntegerBitWidth();
  339. if (Idx->getType()->getPrimitiveSizeInBits() > PtrSize)
  340. Idx = Builder->CreateTrunc(Idx, IntPtrTy);
  341. }
  342. // If the comparison is only true for one or two elements, emit direct
  343. // comparisons.
  344. if (SecondTrueElement != Overdefined) {
  345. // None true -> false.
  346. if (FirstTrueElement == Undefined)
  347. return ReplaceInstUsesWith(ICI, Builder->getFalse());
  348. Value *FirstTrueIdx = ConstantInt::get(Idx->getType(), FirstTrueElement);
  349. // True for one element -> 'i == 47'.
  350. if (SecondTrueElement == Undefined)
  351. return new ICmpInst(ICmpInst::ICMP_EQ, Idx, FirstTrueIdx);
  352. // True for two elements -> 'i == 47 | i == 72'.
  353. Value *C1 = Builder->CreateICmpEQ(Idx, FirstTrueIdx);
  354. Value *SecondTrueIdx = ConstantInt::get(Idx->getType(), SecondTrueElement);
  355. Value *C2 = Builder->CreateICmpEQ(Idx, SecondTrueIdx);
  356. return BinaryOperator::CreateOr(C1, C2);
  357. }
  358. // If the comparison is only false for one or two elements, emit direct
  359. // comparisons.
  360. if (SecondFalseElement != Overdefined) {
  361. // None false -> true.
  362. if (FirstFalseElement == Undefined)
  363. return ReplaceInstUsesWith(ICI, Builder->getTrue());
  364. Value *FirstFalseIdx = ConstantInt::get(Idx->getType(), FirstFalseElement);
  365. // False for one element -> 'i != 47'.
  366. if (SecondFalseElement == Undefined)
  367. return new ICmpInst(ICmpInst::ICMP_NE, Idx, FirstFalseIdx);
  368. // False for two elements -> 'i != 47 & i != 72'.
  369. Value *C1 = Builder->CreateICmpNE(Idx, FirstFalseIdx);
  370. Value *SecondFalseIdx = ConstantInt::get(Idx->getType(),SecondFalseElement);
  371. Value *C2 = Builder->CreateICmpNE(Idx, SecondFalseIdx);
  372. return BinaryOperator::CreateAnd(C1, C2);
  373. }
  374. // If the comparison can be replaced with a range comparison for the elements
  375. // where it is true, emit the range check.
  376. if (TrueRangeEnd != Overdefined) {
  377. assert(TrueRangeEnd != FirstTrueElement && "Should emit single compare");
  378. // Generate (i-FirstTrue) <u (TrueRangeEnd-FirstTrue+1).
  379. if (FirstTrueElement) {
  380. Value *Offs = ConstantInt::get(Idx->getType(), -FirstTrueElement);
  381. Idx = Builder->CreateAdd(Idx, Offs);
  382. }
  383. Value *End = ConstantInt::get(Idx->getType(),
  384. TrueRangeEnd-FirstTrueElement+1);
  385. return new ICmpInst(ICmpInst::ICMP_ULT, Idx, End);
  386. }
  387. // False range check.
  388. if (FalseRangeEnd != Overdefined) {
  389. assert(FalseRangeEnd != FirstFalseElement && "Should emit single compare");
  390. // Generate (i-FirstFalse) >u (FalseRangeEnd-FirstFalse).
  391. if (FirstFalseElement) {
  392. Value *Offs = ConstantInt::get(Idx->getType(), -FirstFalseElement);
  393. Idx = Builder->CreateAdd(Idx, Offs);
  394. }
  395. Value *End = ConstantInt::get(Idx->getType(),
  396. FalseRangeEnd-FirstFalseElement);
  397. return new ICmpInst(ICmpInst::ICMP_UGT, Idx, End);
  398. }
  399. // If a magic bitvector captures the entire comparison state
  400. // of this load, replace it with computation that does:
  401. // ((magic_cst >> i) & 1) != 0
  402. {
  403. Type *Ty = nullptr;
  404. // Look for an appropriate type:
  405. // - The type of Idx if the magic fits
  406. // - The smallest fitting legal type if we have a DataLayout
  407. // - Default to i32
  408. if (ArrayElementCount <= Idx->getType()->getIntegerBitWidth())
  409. Ty = Idx->getType();
  410. else
  411. Ty = DL.getSmallestLegalIntType(Init->getContext(), ArrayElementCount);
  412. if (Ty) {
  413. Value *V = Builder->CreateIntCast(Idx, Ty, false);
  414. V = Builder->CreateLShr(ConstantInt::get(Ty, MagicBitvector), V);
  415. V = Builder->CreateAnd(ConstantInt::get(Ty, 1), V);
  416. return new ICmpInst(ICmpInst::ICMP_NE, V, ConstantInt::get(Ty, 0));
  417. }
  418. }
  419. return nullptr;
  420. }
  421. /// EvaluateGEPOffsetExpression - Return a value that can be used to compare
  422. /// the *offset* implied by a GEP to zero. For example, if we have &A[i], we
  423. /// want to return 'i' for "icmp ne i, 0". Note that, in general, indices can
  424. /// be complex, and scales are involved. The above expression would also be
  425. /// legal to codegen as "icmp ne (i*4), 0" (assuming A is a pointer to i32).
  426. /// This later form is less amenable to optimization though, and we are allowed
  427. /// to generate the first by knowing that pointer arithmetic doesn't overflow.
  428. ///
  429. /// If we can't emit an optimized form for this expression, this returns null.
  430. ///
  431. static Value *EvaluateGEPOffsetExpression(User *GEP, InstCombiner &IC,
  432. const DataLayout &DL) {
  433. gep_type_iterator GTI = gep_type_begin(GEP);
  434. // Check to see if this gep only has a single variable index. If so, and if
  435. // any constant indices are a multiple of its scale, then we can compute this
  436. // in terms of the scale of the variable index. For example, if the GEP
  437. // implies an offset of "12 + i*4", then we can codegen this as "3 + i",
  438. // because the expression will cross zero at the same point.
  439. unsigned i, e = GEP->getNumOperands();
  440. int64_t Offset = 0;
  441. for (i = 1; i != e; ++i, ++GTI) {
  442. if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
  443. // Compute the aggregate offset of constant indices.
  444. if (CI->isZero()) continue;
  445. // Handle a struct index, which adds its field offset to the pointer.
  446. if (StructType *STy = dyn_cast<StructType>(*GTI)) {
  447. Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
  448. } else {
  449. uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
  450. Offset += Size*CI->getSExtValue();
  451. }
  452. } else {
  453. // Found our variable index.
  454. break;
  455. }
  456. }
  457. // If there are no variable indices, we must have a constant offset, just
  458. // evaluate it the general way.
  459. if (i == e) return nullptr;
  460. Value *VariableIdx = GEP->getOperand(i);
  461. // Determine the scale factor of the variable element. For example, this is
  462. // 4 if the variable index is into an array of i32.
  463. uint64_t VariableScale = DL.getTypeAllocSize(GTI.getIndexedType());
  464. // Verify that there are no other variable indices. If so, emit the hard way.
  465. for (++i, ++GTI; i != e; ++i, ++GTI) {
  466. ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i));
  467. if (!CI) return nullptr;
  468. // Compute the aggregate offset of constant indices.
  469. if (CI->isZero()) continue;
  470. // Handle a struct index, which adds its field offset to the pointer.
  471. if (StructType *STy = dyn_cast<StructType>(*GTI)) {
  472. Offset += DL.getStructLayout(STy)->getElementOffset(CI->getZExtValue());
  473. } else {
  474. uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType());
  475. Offset += Size*CI->getSExtValue();
  476. }
  477. }
  478. // Okay, we know we have a single variable index, which must be a
  479. // pointer/array/vector index. If there is no offset, life is simple, return
  480. // the index.
  481. Type *IntPtrTy = DL.getIntPtrType(GEP->getOperand(0)->getType());
  482. unsigned IntPtrWidth = IntPtrTy->getIntegerBitWidth();
  483. if (Offset == 0) {
  484. // Cast to intptrty in case a truncation occurs. If an extension is needed,
  485. // we don't need to bother extending: the extension won't affect where the
  486. // computation crosses zero.
  487. if (VariableIdx->getType()->getPrimitiveSizeInBits() > IntPtrWidth) {
  488. VariableIdx = IC.Builder->CreateTrunc(VariableIdx, IntPtrTy);
  489. }
  490. return VariableIdx;
  491. }
  492. // Otherwise, there is an index. The computation we will do will be modulo
  493. // the pointer size, so get it.
  494. uint64_t PtrSizeMask = ~0ULL >> (64-IntPtrWidth);
  495. Offset &= PtrSizeMask;
  496. VariableScale &= PtrSizeMask;
  497. // To do this transformation, any constant index must be a multiple of the
  498. // variable scale factor. For example, we can evaluate "12 + 4*i" as "3 + i",
  499. // but we can't evaluate "10 + 3*i" in terms of i. Check that the offset is a
  500. // multiple of the variable scale.
  501. int64_t NewOffs = Offset / (int64_t)VariableScale;
  502. if (Offset != NewOffs*(int64_t)VariableScale)
  503. return nullptr;
  504. // Okay, we can do this evaluation. Start by converting the index to intptr.
  505. if (VariableIdx->getType() != IntPtrTy)
  506. VariableIdx = IC.Builder->CreateIntCast(VariableIdx, IntPtrTy,
  507. true /*Signed*/);
  508. Constant *OffsetVal = ConstantInt::get(IntPtrTy, NewOffs);
  509. return IC.Builder->CreateAdd(VariableIdx, OffsetVal, "offset");
  510. }
  511. /// FoldGEPICmp - Fold comparisons between a GEP instruction and something
  512. /// else. At this point we know that the GEP is on the LHS of the comparison.
  513. Instruction *InstCombiner::FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
  514. ICmpInst::Predicate Cond,
  515. Instruction &I) {
  516. // Don't transform signed compares of GEPs into index compares. Even if the
  517. // GEP is inbounds, the final add of the base pointer can have signed overflow
  518. // and would change the result of the icmp.
  519. // e.g. "&foo[0] <s &foo[1]" can't be folded to "true" because "foo" could be
  520. // the maximum signed value for the pointer type.
  521. if (ICmpInst::isSigned(Cond))
  522. return nullptr;
  523. // Look through bitcasts and addrspacecasts. We do not however want to remove
  524. // 0 GEPs.
  525. if (!isa<GetElementPtrInst>(RHS))
  526. RHS = RHS->stripPointerCasts();
  527. Value *PtrBase = GEPLHS->getOperand(0);
  528. if (PtrBase == RHS && GEPLHS->isInBounds()) {
  529. // ((gep Ptr, OFFSET) cmp Ptr) ---> (OFFSET cmp 0).
  530. // This transformation (ignoring the base and scales) is valid because we
  531. // know pointers can't overflow since the gep is inbounds. See if we can
  532. // output an optimized form.
  533. Value *Offset = EvaluateGEPOffsetExpression(GEPLHS, *this, DL);
  534. // If not, synthesize the offset the hard way.
  535. if (!Offset)
  536. Offset = EmitGEPOffset(GEPLHS);
  537. return new ICmpInst(ICmpInst::getSignedPredicate(Cond), Offset,
  538. Constant::getNullValue(Offset->getType()));
  539. } else if (GEPOperator *GEPRHS = dyn_cast<GEPOperator>(RHS)) {
  540. // If the base pointers are different, but the indices are the same, just
  541. // compare the base pointer.
  542. if (PtrBase != GEPRHS->getOperand(0)) {
  543. bool IndicesTheSame = GEPLHS->getNumOperands()==GEPRHS->getNumOperands();
  544. IndicesTheSame &= GEPLHS->getOperand(0)->getType() ==
  545. GEPRHS->getOperand(0)->getType();
  546. if (IndicesTheSame)
  547. for (unsigned i = 1, e = GEPLHS->getNumOperands(); i != e; ++i)
  548. if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
  549. IndicesTheSame = false;
  550. break;
  551. }
  552. // If all indices are the same, just compare the base pointers.
  553. if (IndicesTheSame)
  554. return new ICmpInst(Cond, GEPLHS->getOperand(0), GEPRHS->getOperand(0));
  555. // If we're comparing GEPs with two base pointers that only differ in type
  556. // and both GEPs have only constant indices or just one use, then fold
  557. // the compare with the adjusted indices.
  558. if (GEPLHS->isInBounds() && GEPRHS->isInBounds() &&
  559. (GEPLHS->hasAllConstantIndices() || GEPLHS->hasOneUse()) &&
  560. (GEPRHS->hasAllConstantIndices() || GEPRHS->hasOneUse()) &&
  561. PtrBase->stripPointerCasts() ==
  562. GEPRHS->getOperand(0)->stripPointerCasts()) {
  563. Value *LOffset = EmitGEPOffset(GEPLHS);
  564. Value *ROffset = EmitGEPOffset(GEPRHS);
  565. // If we looked through an addrspacecast between different sized address
  566. // spaces, the LHS and RHS pointers are different sized
  567. // integers. Truncate to the smaller one.
  568. Type *LHSIndexTy = LOffset->getType();
  569. Type *RHSIndexTy = ROffset->getType();
  570. if (LHSIndexTy != RHSIndexTy) {
  571. if (LHSIndexTy->getPrimitiveSizeInBits() <
  572. RHSIndexTy->getPrimitiveSizeInBits()) {
  573. ROffset = Builder->CreateTrunc(ROffset, LHSIndexTy);
  574. } else
  575. LOffset = Builder->CreateTrunc(LOffset, RHSIndexTy);
  576. }
  577. Value *Cmp = Builder->CreateICmp(ICmpInst::getSignedPredicate(Cond),
  578. LOffset, ROffset);
  579. return ReplaceInstUsesWith(I, Cmp);
  580. }
  581. // Otherwise, the base pointers are different and the indices are
  582. // different, bail out.
  583. return nullptr;
  584. }
  585. // If one of the GEPs has all zero indices, recurse.
  586. if (GEPLHS->hasAllZeroIndices())
  587. return FoldGEPICmp(GEPRHS, GEPLHS->getOperand(0),
  588. ICmpInst::getSwappedPredicate(Cond), I);
  589. // If the other GEP has all zero indices, recurse.
  590. if (GEPRHS->hasAllZeroIndices())
  591. return FoldGEPICmp(GEPLHS, GEPRHS->getOperand(0), Cond, I);
  592. bool GEPsInBounds = GEPLHS->isInBounds() && GEPRHS->isInBounds();
  593. if (GEPLHS->getNumOperands() == GEPRHS->getNumOperands()) {
  594. // If the GEPs only differ by one index, compare it.
  595. unsigned NumDifferences = 0; // Keep track of # differences.
  596. unsigned DiffOperand = 0; // The operand that differs.
  597. for (unsigned i = 1, e = GEPRHS->getNumOperands(); i != e; ++i)
  598. if (GEPLHS->getOperand(i) != GEPRHS->getOperand(i)) {
  599. if (GEPLHS->getOperand(i)->getType()->getPrimitiveSizeInBits() !=
  600. GEPRHS->getOperand(i)->getType()->getPrimitiveSizeInBits()) {
  601. // Irreconcilable differences.
  602. NumDifferences = 2;
  603. break;
  604. } else {
  605. if (NumDifferences++) break;
  606. DiffOperand = i;
  607. }
  608. }
  609. if (NumDifferences == 0) // SAME GEP?
  610. return ReplaceInstUsesWith(I, // No comparison is needed here.
  611. Builder->getInt1(ICmpInst::isTrueWhenEqual(Cond)));
  612. else if (NumDifferences == 1 && GEPsInBounds) {
  613. Value *LHSV = GEPLHS->getOperand(DiffOperand);
  614. Value *RHSV = GEPRHS->getOperand(DiffOperand);
  615. // Make sure we do a signed comparison here.
  616. return new ICmpInst(ICmpInst::getSignedPredicate(Cond), LHSV, RHSV);
  617. }
  618. }
  619. // Only lower this if the icmp is the only user of the GEP or if we expect
  620. // the result to fold to a constant!
  621. if (GEPsInBounds && (isa<ConstantExpr>(GEPLHS) || GEPLHS->hasOneUse()) &&
  622. (isa<ConstantExpr>(GEPRHS) || GEPRHS->hasOneUse())) {
  623. // ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
  624. Value *L = EmitGEPOffset(GEPLHS);
  625. Value *R = EmitGEPOffset(GEPRHS);
  626. return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
  627. }
  628. }
  629. return nullptr;
  630. }
  631. /// FoldICmpAddOpCst - Fold "icmp pred (X+CI), X".
  632. Instruction *InstCombiner::FoldICmpAddOpCst(Instruction &ICI,
  633. Value *X, ConstantInt *CI,
  634. ICmpInst::Predicate Pred) {
  635. // From this point on, we know that (X+C <= X) --> (X+C < X) because C != 0,
  636. // so the values can never be equal. Similarly for all other "or equals"
  637. // operators.
  638. // (X+1) <u X --> X >u (MAXUINT-1) --> X == 255
  639. // (X+2) <u X --> X >u (MAXUINT-2) --> X > 253
  640. // (X+MAXUINT) <u X --> X >u (MAXUINT-MAXUINT) --> X != 0
  641. if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
  642. Value *R =
  643. ConstantExpr::getSub(ConstantInt::getAllOnesValue(CI->getType()), CI);
  644. return new ICmpInst(ICmpInst::ICMP_UGT, X, R);
  645. }
  646. // (X+1) >u X --> X <u (0-1) --> X != 255
  647. // (X+2) >u X --> X <u (0-2) --> X <u 254
  648. // (X+MAXUINT) >u X --> X <u (0-MAXUINT) --> X <u 1 --> X == 0
  649. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
  650. return new ICmpInst(ICmpInst::ICMP_ULT, X, ConstantExpr::getNeg(CI));
  651. unsigned BitWidth = CI->getType()->getPrimitiveSizeInBits();
  652. ConstantInt *SMax = ConstantInt::get(X->getContext(),
  653. APInt::getSignedMaxValue(BitWidth));
  654. // (X+ 1) <s X --> X >s (MAXSINT-1) --> X == 127
  655. // (X+ 2) <s X --> X >s (MAXSINT-2) --> X >s 125
  656. // (X+MAXSINT) <s X --> X >s (MAXSINT-MAXSINT) --> X >s 0
  657. // (X+MINSINT) <s X --> X >s (MAXSINT-MINSINT) --> X >s -1
  658. // (X+ -2) <s X --> X >s (MAXSINT- -2) --> X >s 126
  659. // (X+ -1) <s X --> X >s (MAXSINT- -1) --> X != 127
  660. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
  661. return new ICmpInst(ICmpInst::ICMP_SGT, X, ConstantExpr::getSub(SMax, CI));
  662. // (X+ 1) >s X --> X <s (MAXSINT-(1-1)) --> X != 127
  663. // (X+ 2) >s X --> X <s (MAXSINT-(2-1)) --> X <s 126
  664. // (X+MAXSINT) >s X --> X <s (MAXSINT-(MAXSINT-1)) --> X <s 1
  665. // (X+MINSINT) >s X --> X <s (MAXSINT-(MINSINT-1)) --> X <s -2
  666. // (X+ -2) >s X --> X <s (MAXSINT-(-2-1)) --> X <s -126
  667. // (X+ -1) >s X --> X <s (MAXSINT-(-1-1)) --> X == -128
  668. assert(Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE);
  669. Constant *C = Builder->getInt(CI->getValue()-1);
  670. return new ICmpInst(ICmpInst::ICMP_SLT, X, ConstantExpr::getSub(SMax, C));
  671. }
  672. /// FoldICmpDivCst - Fold "icmp pred, ([su]div X, DivRHS), CmpRHS" where DivRHS
  673. /// and CmpRHS are both known to be integer constants.
  674. Instruction *InstCombiner::FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
  675. ConstantInt *DivRHS) {
  676. ConstantInt *CmpRHS = cast<ConstantInt>(ICI.getOperand(1));
  677. const APInt &CmpRHSV = CmpRHS->getValue();
  678. // FIXME: If the operand types don't match the type of the divide
  679. // then don't attempt this transform. The code below doesn't have the
  680. // logic to deal with a signed divide and an unsigned compare (and
  681. // vice versa). This is because (x /s C1) <s C2 produces different
  682. // results than (x /s C1) <u C2 or (x /u C1) <s C2 or even
  683. // (x /u C1) <u C2. Simply casting the operands and result won't
  684. // work. :( The if statement below tests that condition and bails
  685. // if it finds it.
  686. bool DivIsSigned = DivI->getOpcode() == Instruction::SDiv;
  687. if (!ICI.isEquality() && DivIsSigned != ICI.isSigned())
  688. return nullptr;
  689. if (DivRHS->isZero())
  690. return nullptr; // The ProdOV computation fails on divide by zero.
  691. if (DivIsSigned && DivRHS->isAllOnesValue())
  692. return nullptr; // The overflow computation also screws up here
  693. if (DivRHS->isOne()) {
  694. // This eliminates some funny cases with INT_MIN.
  695. ICI.setOperand(0, DivI->getOperand(0)); // X/1 == X.
  696. return &ICI;
  697. }
  698. // Compute Prod = CI * DivRHS. We are essentially solving an equation
  699. // of form X/C1=C2. We solve for X by multiplying C1 (DivRHS) and
  700. // C2 (CI). By solving for X we can turn this into a range check
  701. // instead of computing a divide.
  702. Constant *Prod = ConstantExpr::getMul(CmpRHS, DivRHS);
  703. // Determine if the product overflows by seeing if the product is
  704. // not equal to the divide. Make sure we do the same kind of divide
  705. // as in the LHS instruction that we're folding.
  706. bool ProdOV = (DivIsSigned ? ConstantExpr::getSDiv(Prod, DivRHS) :
  707. ConstantExpr::getUDiv(Prod, DivRHS)) != CmpRHS;
  708. // Get the ICmp opcode
  709. ICmpInst::Predicate Pred = ICI.getPredicate();
  710. /// If the division is known to be exact, then there is no remainder from the
  711. /// divide, so the covered range size is unit, otherwise it is the divisor.
  712. ConstantInt *RangeSize = DivI->isExact() ? getOne(Prod) : DivRHS;
  713. // Figure out the interval that is being checked. For example, a comparison
  714. // like "X /u 5 == 0" is really checking that X is in the interval [0, 5).
  715. // Compute this interval based on the constants involved and the signedness of
  716. // the compare/divide. This computes a half-open interval, keeping track of
  717. // whether either value in the interval overflows. After analysis each
  718. // overflow variable is set to 0 if it's corresponding bound variable is valid
  719. // -1 if overflowed off the bottom end, or +1 if overflowed off the top end.
  720. int LoOverflow = 0, HiOverflow = 0;
  721. Constant *LoBound = nullptr, *HiBound = nullptr;
  722. if (!DivIsSigned) { // udiv
  723. // e.g. X/5 op 3 --> [15, 20)
  724. LoBound = Prod;
  725. HiOverflow = LoOverflow = ProdOV;
  726. if (!HiOverflow) {
  727. // If this is not an exact divide, then many values in the range collapse
  728. // to the same result value.
  729. HiOverflow = AddWithOverflow(HiBound, LoBound, RangeSize, false);
  730. }
  731. } else if (DivRHS->getValue().isStrictlyPositive()) { // Divisor is > 0.
  732. if (CmpRHSV == 0) { // (X / pos) op 0
  733. // Can't overflow. e.g. X/2 op 0 --> [-1, 2)
  734. LoBound = ConstantExpr::getNeg(SubOne(RangeSize));
  735. HiBound = RangeSize;
  736. } else if (CmpRHSV.isStrictlyPositive()) { // (X / pos) op pos
  737. LoBound = Prod; // e.g. X/5 op 3 --> [15, 20)
  738. HiOverflow = LoOverflow = ProdOV;
  739. if (!HiOverflow)
  740. HiOverflow = AddWithOverflow(HiBound, Prod, RangeSize, true);
  741. } else { // (X / pos) op neg
  742. // e.g. X/5 op -3 --> [-15-4, -15+1) --> [-19, -14)
  743. HiBound = AddOne(Prod);
  744. LoOverflow = HiOverflow = ProdOV ? -1 : 0;
  745. if (!LoOverflow) {
  746. ConstantInt *DivNeg =cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
  747. LoOverflow = AddWithOverflow(LoBound, HiBound, DivNeg, true) ? -1 : 0;
  748. }
  749. }
  750. } else if (DivRHS->isNegative()) { // Divisor is < 0.
  751. if (DivI->isExact())
  752. RangeSize = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
  753. if (CmpRHSV == 0) { // (X / neg) op 0
  754. // e.g. X/-5 op 0 --> [-4, 5)
  755. LoBound = AddOne(RangeSize);
  756. HiBound = cast<ConstantInt>(ConstantExpr::getNeg(RangeSize));
  757. if (HiBound == DivRHS) { // -INTMIN = INTMIN
  758. HiOverflow = 1; // [INTMIN+1, overflow)
  759. HiBound = nullptr; // e.g. X/INTMIN = 0 --> X > INTMIN
  760. }
  761. } else if (CmpRHSV.isStrictlyPositive()) { // (X / neg) op pos
  762. // e.g. X/-5 op 3 --> [-19, -14)
  763. HiBound = AddOne(Prod);
  764. HiOverflow = LoOverflow = ProdOV ? -1 : 0;
  765. if (!LoOverflow)
  766. LoOverflow = AddWithOverflow(LoBound, HiBound, RangeSize, true) ? -1:0;
  767. } else { // (X / neg) op neg
  768. LoBound = Prod; // e.g. X/-5 op -3 --> [15, 20)
  769. LoOverflow = HiOverflow = ProdOV;
  770. if (!HiOverflow)
  771. HiOverflow = SubWithOverflow(HiBound, Prod, RangeSize, true);
  772. }
  773. // Dividing by a negative swaps the condition. LT <-> GT
  774. Pred = ICmpInst::getSwappedPredicate(Pred);
  775. }
  776. Value *X = DivI->getOperand(0);
  777. switch (Pred) {
  778. default: llvm_unreachable("Unhandled icmp opcode!");
  779. case ICmpInst::ICMP_EQ:
  780. if (LoOverflow && HiOverflow)
  781. return ReplaceInstUsesWith(ICI, Builder->getFalse());
  782. if (HiOverflow)
  783. return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
  784. ICmpInst::ICMP_UGE, X, LoBound);
  785. if (LoOverflow)
  786. return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
  787. ICmpInst::ICMP_ULT, X, HiBound);
  788. return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
  789. DivIsSigned, true));
  790. case ICmpInst::ICMP_NE:
  791. if (LoOverflow && HiOverflow)
  792. return ReplaceInstUsesWith(ICI, Builder->getTrue());
  793. if (HiOverflow)
  794. return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SLT :
  795. ICmpInst::ICMP_ULT, X, LoBound);
  796. if (LoOverflow)
  797. return new ICmpInst(DivIsSigned ? ICmpInst::ICMP_SGE :
  798. ICmpInst::ICMP_UGE, X, HiBound);
  799. return ReplaceInstUsesWith(ICI, InsertRangeTest(X, LoBound, HiBound,
  800. DivIsSigned, false));
  801. case ICmpInst::ICMP_ULT:
  802. case ICmpInst::ICMP_SLT:
  803. if (LoOverflow == +1) // Low bound is greater than input range.
  804. return ReplaceInstUsesWith(ICI, Builder->getTrue());
  805. if (LoOverflow == -1) // Low bound is less than input range.
  806. return ReplaceInstUsesWith(ICI, Builder->getFalse());
  807. return new ICmpInst(Pred, X, LoBound);
  808. case ICmpInst::ICMP_UGT:
  809. case ICmpInst::ICMP_SGT:
  810. if (HiOverflow == +1) // High bound greater than input range.
  811. return ReplaceInstUsesWith(ICI, Builder->getFalse());
  812. if (HiOverflow == -1) // High bound less than input range.
  813. return ReplaceInstUsesWith(ICI, Builder->getTrue());
  814. if (Pred == ICmpInst::ICMP_UGT)
  815. return new ICmpInst(ICmpInst::ICMP_UGE, X, HiBound);
  816. return new ICmpInst(ICmpInst::ICMP_SGE, X, HiBound);
  817. }
  818. }
  819. /// FoldICmpShrCst - Handle "icmp(([al]shr X, cst1), cst2)".
  820. Instruction *InstCombiner::FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *Shr,
  821. ConstantInt *ShAmt) {
  822. const APInt &CmpRHSV = cast<ConstantInt>(ICI.getOperand(1))->getValue();
  823. // Check that the shift amount is in range. If not, don't perform
  824. // undefined shifts. When the shift is visited it will be
  825. // simplified.
  826. uint32_t TypeBits = CmpRHSV.getBitWidth();
  827. uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
  828. if (ShAmtVal >= TypeBits || ShAmtVal == 0)
  829. return nullptr;
  830. if (!ICI.isEquality()) {
  831. // If we have an unsigned comparison and an ashr, we can't simplify this.
  832. // Similarly for signed comparisons with lshr.
  833. if (ICI.isSigned() != (Shr->getOpcode() == Instruction::AShr))
  834. return nullptr;
  835. // Otherwise, all lshr and most exact ashr's are equivalent to a udiv/sdiv
  836. // by a power of 2. Since we already have logic to simplify these,
  837. // transform to div and then simplify the resultant comparison.
  838. if (Shr->getOpcode() == Instruction::AShr &&
  839. (!Shr->isExact() || ShAmtVal == TypeBits - 1))
  840. return nullptr;
  841. // Revisit the shift (to delete it).
  842. Worklist.Add(Shr);
  843. Constant *DivCst =
  844. ConstantInt::get(Shr->getType(), APInt::getOneBitSet(TypeBits, ShAmtVal));
  845. Value *Tmp =
  846. Shr->getOpcode() == Instruction::AShr ?
  847. Builder->CreateSDiv(Shr->getOperand(0), DivCst, "", Shr->isExact()) :
  848. Builder->CreateUDiv(Shr->getOperand(0), DivCst, "", Shr->isExact());
  849. ICI.setOperand(0, Tmp);
  850. // If the builder folded the binop, just return it.
  851. BinaryOperator *TheDiv = dyn_cast<BinaryOperator>(Tmp);
  852. if (!TheDiv)
  853. return &ICI;
  854. // Otherwise, fold this div/compare.
  855. assert(TheDiv->getOpcode() == Instruction::SDiv ||
  856. TheDiv->getOpcode() == Instruction::UDiv);
  857. Instruction *Res = FoldICmpDivCst(ICI, TheDiv, cast<ConstantInt>(DivCst));
  858. assert(Res && "This div/cst should have folded!");
  859. return Res;
  860. }
  861. // If we are comparing against bits always shifted out, the
  862. // comparison cannot succeed.
  863. APInt Comp = CmpRHSV << ShAmtVal;
  864. ConstantInt *ShiftedCmpRHS = Builder->getInt(Comp);
  865. if (Shr->getOpcode() == Instruction::LShr)
  866. Comp = Comp.lshr(ShAmtVal);
  867. else
  868. Comp = Comp.ashr(ShAmtVal);
  869. if (Comp != CmpRHSV) { // Comparing against a bit that we know is zero.
  870. bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
  871. Constant *Cst = Builder->getInt1(IsICMP_NE);
  872. return ReplaceInstUsesWith(ICI, Cst);
  873. }
  874. // Otherwise, check to see if the bits shifted out are known to be zero.
  875. // If so, we can compare against the unshifted value:
  876. // (X & 4) >> 1 == 2 --> (X & 4) == 4.
  877. if (Shr->hasOneUse() && Shr->isExact())
  878. return new ICmpInst(ICI.getPredicate(), Shr->getOperand(0), ShiftedCmpRHS);
  879. if (Shr->hasOneUse()) {
  880. // Otherwise strength reduce the shift into an and.
  881. APInt Val(APInt::getHighBitsSet(TypeBits, TypeBits - ShAmtVal));
  882. Constant *Mask = Builder->getInt(Val);
  883. Value *And = Builder->CreateAnd(Shr->getOperand(0),
  884. Mask, Shr->getName()+".mask");
  885. return new ICmpInst(ICI.getPredicate(), And, ShiftedCmpRHS);
  886. }
  887. return nullptr;
  888. }
  889. /// FoldICmpCstShrCst - Handle "(icmp eq/ne (ashr/lshr const2, A), const1)" ->
  890. /// (icmp eq/ne A, Log2(const2/const1)) ->
  891. /// (icmp eq/ne A, Log2(const2) - Log2(const1)).
  892. Instruction *InstCombiner::FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
  893. ConstantInt *CI1,
  894. ConstantInt *CI2) {
  895. assert(I.isEquality() && "Cannot fold icmp gt/lt");
  896. auto getConstant = [&I, this](bool IsTrue) {
  897. if (I.getPredicate() == I.ICMP_NE)
  898. IsTrue = !IsTrue;
  899. return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), IsTrue));
  900. };
  901. auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
  902. if (I.getPredicate() == I.ICMP_NE)
  903. Pred = CmpInst::getInversePredicate(Pred);
  904. return new ICmpInst(Pred, LHS, RHS);
  905. };
  906. APInt AP1 = CI1->getValue();
  907. APInt AP2 = CI2->getValue();
  908. // Don't bother doing any work for cases which InstSimplify handles.
  909. if (AP2 == 0)
  910. return nullptr;
  911. bool IsAShr = isa<AShrOperator>(Op);
  912. if (IsAShr) {
  913. if (AP2.isAllOnesValue())
  914. return nullptr;
  915. if (AP2.isNegative() != AP1.isNegative())
  916. return nullptr;
  917. if (AP2.sgt(AP1))
  918. return nullptr;
  919. }
  920. if (!AP1)
  921. // 'A' must be large enough to shift out the highest set bit.
  922. return getICmp(I.ICMP_UGT, A,
  923. ConstantInt::get(A->getType(), AP2.logBase2()));
  924. if (AP1 == AP2)
  925. return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
  926. // Get the distance between the highest bit that's set.
  927. int Shift;
  928. // Both the constants are negative, take their positive to calculate log.
  929. if (IsAShr && AP1.isNegative())
  930. // Get the ones' complement of AP2 and AP1 when computing the distance.
  931. Shift = (~AP2).logBase2() - (~AP1).logBase2();
  932. else
  933. Shift = AP2.logBase2() - AP1.logBase2();
  934. if (Shift > 0) {
  935. if (IsAShr ? AP1 == AP2.ashr(Shift) : AP1 == AP2.lshr(Shift))
  936. return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
  937. }
  938. // Shifting const2 will never be equal to const1.
  939. return getConstant(false);
  940. }
  941. /// FoldICmpCstShlCst - Handle "(icmp eq/ne (shl const2, A), const1)" ->
  942. /// (icmp eq/ne A, TrailingZeros(const1) - TrailingZeros(const2)).
  943. Instruction *InstCombiner::FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
  944. ConstantInt *CI1,
  945. ConstantInt *CI2) {
  946. assert(I.isEquality() && "Cannot fold icmp gt/lt");
  947. auto getConstant = [&I, this](bool IsTrue) {
  948. if (I.getPredicate() == I.ICMP_NE)
  949. IsTrue = !IsTrue;
  950. return ReplaceInstUsesWith(I, ConstantInt::get(I.getType(), IsTrue));
  951. };
  952. auto getICmp = [&I](CmpInst::Predicate Pred, Value *LHS, Value *RHS) {
  953. if (I.getPredicate() == I.ICMP_NE)
  954. Pred = CmpInst::getInversePredicate(Pred);
  955. return new ICmpInst(Pred, LHS, RHS);
  956. };
  957. APInt AP1 = CI1->getValue();
  958. APInt AP2 = CI2->getValue();
  959. // Don't bother doing any work for cases which InstSimplify handles.
  960. if (AP2 == 0)
  961. return nullptr;
  962. unsigned AP2TrailingZeros = AP2.countTrailingZeros();
  963. if (!AP1 && AP2TrailingZeros != 0)
  964. return getICmp(I.ICMP_UGE, A,
  965. ConstantInt::get(A->getType(), AP2.getBitWidth() - AP2TrailingZeros));
  966. if (AP1 == AP2)
  967. return getICmp(I.ICMP_EQ, A, ConstantInt::getNullValue(A->getType()));
  968. // Get the distance between the lowest bits that are set.
  969. int Shift = AP1.countTrailingZeros() - AP2TrailingZeros;
  970. if (Shift > 0 && AP2.shl(Shift) == AP1)
  971. return getICmp(I.ICMP_EQ, A, ConstantInt::get(A->getType(), Shift));
  972. // Shifting const2 will never be equal to const1.
  973. return getConstant(false);
  974. }
  975. /// visitICmpInstWithInstAndIntCst - Handle "icmp (instr, intcst)".
  976. ///
  977. Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
  978. Instruction *LHSI,
  979. ConstantInt *RHS) {
  980. const APInt &RHSV = RHS->getValue();
  981. switch (LHSI->getOpcode()) {
  982. case Instruction::Trunc:
  983. if (ICI.isEquality() && LHSI->hasOneUse()) {
  984. // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
  985. // of the high bits truncated out of x are known.
  986. unsigned DstBits = LHSI->getType()->getPrimitiveSizeInBits(),
  987. SrcBits = LHSI->getOperand(0)->getType()->getPrimitiveSizeInBits();
  988. APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
  989. computeKnownBits(LHSI->getOperand(0), KnownZero, KnownOne, 0, &ICI);
  990. // If all the high bits are known, we can do this xform.
  991. if ((KnownZero|KnownOne).countLeadingOnes() >= SrcBits-DstBits) {
  992. // Pull in the high bits from known-ones set.
  993. APInt NewRHS = RHS->getValue().zext(SrcBits);
  994. NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits-DstBits);
  995. return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
  996. Builder->getInt(NewRHS));
  997. }
  998. }
  999. break;
  1000. case Instruction::Xor: // (icmp pred (xor X, XorCst), CI)
  1001. if (ConstantInt *XorCst = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
  1002. // If this is a comparison that tests the signbit (X < 0) or (x > -1),
  1003. // fold the xor.
  1004. if ((ICI.getPredicate() == ICmpInst::ICMP_SLT && RHSV == 0) ||
  1005. (ICI.getPredicate() == ICmpInst::ICMP_SGT && RHSV.isAllOnesValue())) {
  1006. Value *CompareVal = LHSI->getOperand(0);
  1007. // If the sign bit of the XorCst is not set, there is no change to
  1008. // the operation, just stop using the Xor.
  1009. if (!XorCst->isNegative()) {
  1010. ICI.setOperand(0, CompareVal);
  1011. Worklist.Add(LHSI);
  1012. return &ICI;
  1013. }
  1014. // Was the old condition true if the operand is positive?
  1015. bool isTrueIfPositive = ICI.getPredicate() == ICmpInst::ICMP_SGT;
  1016. // If so, the new one isn't.
  1017. isTrueIfPositive ^= true;
  1018. if (isTrueIfPositive)
  1019. return new ICmpInst(ICmpInst::ICMP_SGT, CompareVal,
  1020. SubOne(RHS));
  1021. else
  1022. return new ICmpInst(ICmpInst::ICMP_SLT, CompareVal,
  1023. AddOne(RHS));
  1024. }
  1025. if (LHSI->hasOneUse()) {
  1026. // (icmp u/s (xor A SignBit), C) -> (icmp s/u A, (xor C SignBit))
  1027. if (!ICI.isEquality() && XorCst->getValue().isSignBit()) {
  1028. const APInt &SignBit = XorCst->getValue();
  1029. ICmpInst::Predicate Pred = ICI.isSigned()
  1030. ? ICI.getUnsignedPredicate()
  1031. : ICI.getSignedPredicate();
  1032. return new ICmpInst(Pred, LHSI->getOperand(0),
  1033. Builder->getInt(RHSV ^ SignBit));
  1034. }
  1035. // (icmp u/s (xor A ~SignBit), C) -> (icmp s/u (xor C ~SignBit), A)
  1036. if (!ICI.isEquality() && XorCst->isMaxValue(true)) {
  1037. const APInt &NotSignBit = XorCst->getValue();
  1038. ICmpInst::Predicate Pred = ICI.isSigned()
  1039. ? ICI.getUnsignedPredicate()
  1040. : ICI.getSignedPredicate();
  1041. Pred = ICI.getSwappedPredicate(Pred);
  1042. return new ICmpInst(Pred, LHSI->getOperand(0),
  1043. Builder->getInt(RHSV ^ NotSignBit));
  1044. }
  1045. }
  1046. // (icmp ugt (xor X, C), ~C) -> (icmp ult X, C)
  1047. // iff -C is a power of 2
  1048. if (ICI.getPredicate() == ICmpInst::ICMP_UGT &&
  1049. XorCst->getValue() == ~RHSV && (RHSV + 1).isPowerOf2())
  1050. return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0), XorCst);
  1051. // (icmp ult (xor X, C), -C) -> (icmp uge X, C)
  1052. // iff -C is a power of 2
  1053. if (ICI.getPredicate() == ICmpInst::ICMP_ULT &&
  1054. XorCst->getValue() == -RHSV && RHSV.isPowerOf2())
  1055. return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0), XorCst);
  1056. }
  1057. break;
  1058. case Instruction::And: // (icmp pred (and X, AndCst), RHS)
  1059. if (LHSI->hasOneUse() && isa<ConstantInt>(LHSI->getOperand(1)) &&
  1060. LHSI->getOperand(0)->hasOneUse()) {
  1061. ConstantInt *AndCst = cast<ConstantInt>(LHSI->getOperand(1));
  1062. // If the LHS is an AND of a truncating cast, we can widen the
  1063. // and/compare to be the input width without changing the value
  1064. // produced, eliminating a cast.
  1065. if (TruncInst *Cast = dyn_cast<TruncInst>(LHSI->getOperand(0))) {
  1066. // We can do this transformation if either the AND constant does not
  1067. // have its sign bit set or if it is an equality comparison.
  1068. // Extending a relational comparison when we're checking the sign
  1069. // bit would not work.
  1070. if (ICI.isEquality() ||
  1071. (!AndCst->isNegative() && RHSV.isNonNegative())) {
  1072. Value *NewAnd =
  1073. Builder->CreateAnd(Cast->getOperand(0),
  1074. ConstantExpr::getZExt(AndCst, Cast->getSrcTy()));
  1075. NewAnd->takeName(LHSI);
  1076. return new ICmpInst(ICI.getPredicate(), NewAnd,
  1077. ConstantExpr::getZExt(RHS, Cast->getSrcTy()));
  1078. }
  1079. }
  1080. // If the LHS is an AND of a zext, and we have an equality compare, we can
  1081. // shrink the and/compare to the smaller type, eliminating the cast.
  1082. if (ZExtInst *Cast = dyn_cast<ZExtInst>(LHSI->getOperand(0))) {
  1083. IntegerType *Ty = cast<IntegerType>(Cast->getSrcTy());
  1084. // Make sure we don't compare the upper bits, SimplifyDemandedBits
  1085. // should fold the icmp to true/false in that case.
  1086. if (ICI.isEquality() && RHSV.getActiveBits() <= Ty->getBitWidth()) {
  1087. Value *NewAnd =
  1088. Builder->CreateAnd(Cast->getOperand(0),
  1089. ConstantExpr::getTrunc(AndCst, Ty));
  1090. NewAnd->takeName(LHSI);
  1091. return new ICmpInst(ICI.getPredicate(), NewAnd,
  1092. ConstantExpr::getTrunc(RHS, Ty));
  1093. }
  1094. }
  1095. // If this is: (X >> C1) & C2 != C3 (where any shift and any compare
  1096. // could exist), turn it into (X & (C2 << C1)) != (C3 << C1). This
  1097. // happens a LOT in code produced by the C front-end, for bitfield
  1098. // access.
  1099. BinaryOperator *Shift = dyn_cast<BinaryOperator>(LHSI->getOperand(0));
  1100. if (Shift && !Shift->isShift())
  1101. Shift = nullptr;
  1102. ConstantInt *ShAmt;
  1103. ShAmt = Shift ? dyn_cast<ConstantInt>(Shift->getOperand(1)) : nullptr;
  1104. // This seemingly simple opportunity to fold away a shift turns out to
  1105. // be rather complicated. See PR17827
  1106. // ( http://llvm.org/bugs/show_bug.cgi?id=17827 ) for details.
  1107. if (ShAmt) {
  1108. bool CanFold = false;
  1109. unsigned ShiftOpcode = Shift->getOpcode();
  1110. if (ShiftOpcode == Instruction::AShr) {
  1111. // There may be some constraints that make this possible,
  1112. // but nothing simple has been discovered yet.
  1113. CanFold = false;
  1114. } else if (ShiftOpcode == Instruction::Shl) {
  1115. // For a left shift, we can fold if the comparison is not signed.
  1116. // We can also fold a signed comparison if the mask value and
  1117. // comparison value are not negative. These constraints may not be
  1118. // obvious, but we can prove that they are correct using an SMT
  1119. // solver.
  1120. if (!ICI.isSigned() || (!AndCst->isNegative() && !RHS->isNegative()))
  1121. CanFold = true;
  1122. } else if (ShiftOpcode == Instruction::LShr) {
  1123. // For a logical right shift, we can fold if the comparison is not
  1124. // signed. We can also fold a signed comparison if the shifted mask
  1125. // value and the shifted comparison value are not negative.
  1126. // These constraints may not be obvious, but we can prove that they
  1127. // are correct using an SMT solver.
  1128. if (!ICI.isSigned())
  1129. CanFold = true;
  1130. else {
  1131. ConstantInt *ShiftedAndCst =
  1132. cast<ConstantInt>(ConstantExpr::getShl(AndCst, ShAmt));
  1133. ConstantInt *ShiftedRHSCst =
  1134. cast<ConstantInt>(ConstantExpr::getShl(RHS, ShAmt));
  1135. if (!ShiftedAndCst->isNegative() && !ShiftedRHSCst->isNegative())
  1136. CanFold = true;
  1137. }
  1138. }
  1139. if (CanFold) {
  1140. Constant *NewCst;
  1141. if (ShiftOpcode == Instruction::Shl)
  1142. NewCst = ConstantExpr::getLShr(RHS, ShAmt);
  1143. else
  1144. NewCst = ConstantExpr::getShl(RHS, ShAmt);
  1145. // Check to see if we are shifting out any of the bits being
  1146. // compared.
  1147. if (ConstantExpr::get(ShiftOpcode, NewCst, ShAmt) != RHS) {
  1148. // If we shifted bits out, the fold is not going to work out.
  1149. // As a special case, check to see if this means that the
  1150. // result is always true or false now.
  1151. if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
  1152. return ReplaceInstUsesWith(ICI, Builder->getFalse());
  1153. if (ICI.getPredicate() == ICmpInst::ICMP_NE)
  1154. return ReplaceInstUsesWith(ICI, Builder->getTrue());
  1155. } else {
  1156. ICI.setOperand(1, NewCst);
  1157. Constant *NewAndCst;
  1158. if (ShiftOpcode == Instruction::Shl)
  1159. NewAndCst = ConstantExpr::getLShr(AndCst, ShAmt);
  1160. else
  1161. NewAndCst = ConstantExpr::getShl(AndCst, ShAmt);
  1162. LHSI->setOperand(1, NewAndCst);
  1163. LHSI->setOperand(0, Shift->getOperand(0));
  1164. Worklist.Add(Shift); // Shift is dead.
  1165. return &ICI;
  1166. }
  1167. }
  1168. }
  1169. // Turn ((X >> Y) & C) == 0 into (X & (C << Y)) == 0. The later is
  1170. // preferable because it allows the C<<Y expression to be hoisted out
  1171. // of a loop if Y is invariant and X is not.
  1172. if (Shift && Shift->hasOneUse() && RHSV == 0 &&
  1173. ICI.isEquality() && !Shift->isArithmeticShift() &&
  1174. !isa<Constant>(Shift->getOperand(0))) {
  1175. // Compute C << Y.
  1176. Value *NS;
  1177. if (Shift->getOpcode() == Instruction::LShr) {
  1178. NS = Builder->CreateShl(AndCst, Shift->getOperand(1));
  1179. } else {
  1180. // Insert a logical shift.
  1181. NS = Builder->CreateLShr(AndCst, Shift->getOperand(1));
  1182. }
  1183. // Compute X & (C << Y).
  1184. Value *NewAnd =
  1185. Builder->CreateAnd(Shift->getOperand(0), NS, LHSI->getName());
  1186. ICI.setOperand(0, NewAnd);
  1187. return &ICI;
  1188. }
  1189. // (icmp pred (and (or (lshr X, Y), X), 1), 0) -->
  1190. // (icmp pred (and X, (or (shl 1, Y), 1), 0))
  1191. //
  1192. // iff pred isn't signed
  1193. {
  1194. Value *X, *Y, *LShr;
  1195. if (!ICI.isSigned() && RHSV == 0) {
  1196. if (match(LHSI->getOperand(1), m_One())) {
  1197. Constant *One = cast<Constant>(LHSI->getOperand(1));
  1198. Value *Or = LHSI->getOperand(0);
  1199. if (match(Or, m_Or(m_Value(LShr), m_Value(X))) &&
  1200. match(LShr, m_LShr(m_Specific(X), m_Value(Y)))) {
  1201. unsigned UsesRemoved = 0;
  1202. if (LHSI->hasOneUse())
  1203. ++UsesRemoved;
  1204. if (Or->hasOneUse())
  1205. ++UsesRemoved;
  1206. if (LShr->hasOneUse())
  1207. ++UsesRemoved;
  1208. Value *NewOr = nullptr;
  1209. // Compute X & ((1 << Y) | 1)
  1210. if (auto *C = dyn_cast<Constant>(Y)) {
  1211. if (UsesRemoved >= 1)
  1212. NewOr =
  1213. ConstantExpr::getOr(ConstantExpr::getNUWShl(One, C), One);
  1214. } else {
  1215. if (UsesRemoved >= 3)
  1216. NewOr = Builder->CreateOr(Builder->CreateShl(One, Y,
  1217. LShr->getName(),
  1218. /*HasNUW=*/true),
  1219. One, Or->getName());
  1220. }
  1221. if (NewOr) {
  1222. Value *NewAnd = Builder->CreateAnd(X, NewOr, LHSI->getName());
  1223. ICI.setOperand(0, NewAnd);
  1224. return &ICI;
  1225. }
  1226. }
  1227. }
  1228. }
  1229. }
  1230. // Replace ((X & AndCst) > RHSV) with ((X & AndCst) != 0), if any
  1231. // bit set in (X & AndCst) will produce a result greater than RHSV.
  1232. if (ICI.getPredicate() == ICmpInst::ICMP_UGT) {
  1233. unsigned NTZ = AndCst->getValue().countTrailingZeros();
  1234. if ((NTZ < AndCst->getBitWidth()) &&
  1235. APInt::getOneBitSet(AndCst->getBitWidth(), NTZ).ugt(RHSV))
  1236. return new ICmpInst(ICmpInst::ICMP_NE, LHSI,
  1237. Constant::getNullValue(RHS->getType()));
  1238. }
  1239. }
  1240. // Try to optimize things like "A[i]&42 == 0" to index computations.
  1241. if (LoadInst *LI = dyn_cast<LoadInst>(LHSI->getOperand(0))) {
  1242. if (GetElementPtrInst *GEP =
  1243. dyn_cast<GetElementPtrInst>(LI->getOperand(0)))
  1244. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
  1245. if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
  1246. !LI->isVolatile() && isa<ConstantInt>(LHSI->getOperand(1))) {
  1247. ConstantInt *C = cast<ConstantInt>(LHSI->getOperand(1));
  1248. if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV,ICI, C))
  1249. return Res;
  1250. }
  1251. }
  1252. // X & -C == -C -> X > u ~C
  1253. // X & -C != -C -> X <= u ~C
  1254. // iff C is a power of 2
  1255. if (ICI.isEquality() && RHS == LHSI->getOperand(1) && (-RHSV).isPowerOf2())
  1256. return new ICmpInst(
  1257. ICI.getPredicate() == ICmpInst::ICMP_EQ ? ICmpInst::ICMP_UGT
  1258. : ICmpInst::ICMP_ULE,
  1259. LHSI->getOperand(0), SubOne(RHS));
  1260. break;
  1261. case Instruction::Or: {
  1262. if (!ICI.isEquality() || !RHS->isNullValue() || !LHSI->hasOneUse())
  1263. break;
  1264. Value *P, *Q;
  1265. if (match(LHSI, m_Or(m_PtrToInt(m_Value(P)), m_PtrToInt(m_Value(Q))))) {
  1266. // Simplify icmp eq (or (ptrtoint P), (ptrtoint Q)), 0
  1267. // -> and (icmp eq P, null), (icmp eq Q, null).
  1268. Value *ICIP = Builder->CreateICmp(ICI.getPredicate(), P,
  1269. Constant::getNullValue(P->getType()));
  1270. Value *ICIQ = Builder->CreateICmp(ICI.getPredicate(), Q,
  1271. Constant::getNullValue(Q->getType()));
  1272. Instruction *Op;
  1273. if (ICI.getPredicate() == ICmpInst::ICMP_EQ)
  1274. Op = BinaryOperator::CreateAnd(ICIP, ICIQ);
  1275. else
  1276. Op = BinaryOperator::CreateOr(ICIP, ICIQ);
  1277. return Op;
  1278. }
  1279. break;
  1280. }
  1281. case Instruction::Mul: { // (icmp pred (mul X, Val), CI)
  1282. ConstantInt *Val = dyn_cast<ConstantInt>(LHSI->getOperand(1));
  1283. if (!Val) break;
  1284. // If this is a signed comparison to 0 and the mul is sign preserving,
  1285. // use the mul LHS operand instead.
  1286. ICmpInst::Predicate pred = ICI.getPredicate();
  1287. if (isSignTest(pred, RHS) && !Val->isZero() &&
  1288. cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
  1289. return new ICmpInst(Val->isNegative() ?
  1290. ICmpInst::getSwappedPredicate(pred) : pred,
  1291. LHSI->getOperand(0),
  1292. Constant::getNullValue(RHS->getType()));
  1293. break;
  1294. }
  1295. case Instruction::Shl: { // (icmp pred (shl X, ShAmt), CI)
  1296. uint32_t TypeBits = RHSV.getBitWidth();
  1297. ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1));
  1298. if (!ShAmt) {
  1299. Value *X;
  1300. // (1 << X) pred P2 -> X pred Log2(P2)
  1301. if (match(LHSI, m_Shl(m_One(), m_Value(X)))) {
  1302. bool RHSVIsPowerOf2 = RHSV.isPowerOf2();
  1303. ICmpInst::Predicate Pred = ICI.getPredicate();
  1304. if (ICI.isUnsigned()) {
  1305. if (!RHSVIsPowerOf2) {
  1306. // (1 << X) < 30 -> X <= 4
  1307. // (1 << X) <= 30 -> X <= 4
  1308. // (1 << X) >= 30 -> X > 4
  1309. // (1 << X) > 30 -> X > 4
  1310. if (Pred == ICmpInst::ICMP_ULT)
  1311. Pred = ICmpInst::ICMP_ULE;
  1312. else if (Pred == ICmpInst::ICMP_UGE)
  1313. Pred = ICmpInst::ICMP_UGT;
  1314. }
  1315. unsigned RHSLog2 = RHSV.logBase2();
  1316. // (1 << X) >= 2147483648 -> X >= 31 -> X == 31
  1317. // (1 << X) < 2147483648 -> X < 31 -> X != 31
  1318. if (RHSLog2 == TypeBits-1) {
  1319. if (Pred == ICmpInst::ICMP_UGE)
  1320. Pred = ICmpInst::ICMP_EQ;
  1321. else if (Pred == ICmpInst::ICMP_ULT)
  1322. Pred = ICmpInst::ICMP_NE;
  1323. }
  1324. return new ICmpInst(Pred, X,
  1325. ConstantInt::get(RHS->getType(), RHSLog2));
  1326. } else if (ICI.isSigned()) {
  1327. if (RHSV.isAllOnesValue()) {
  1328. // (1 << X) <= -1 -> X == 31
  1329. if (Pred == ICmpInst::ICMP_SLE)
  1330. return new ICmpInst(ICmpInst::ICMP_EQ, X,
  1331. ConstantInt::get(RHS->getType(), TypeBits-1));
  1332. // (1 << X) > -1 -> X != 31
  1333. if (Pred == ICmpInst::ICMP_SGT)
  1334. return new ICmpInst(ICmpInst::ICMP_NE, X,
  1335. ConstantInt::get(RHS->getType(), TypeBits-1));
  1336. } else if (!RHSV) {
  1337. // (1 << X) < 0 -> X == 31
  1338. // (1 << X) <= 0 -> X == 31
  1339. if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
  1340. return new ICmpInst(ICmpInst::ICMP_EQ, X,
  1341. ConstantInt::get(RHS->getType(), TypeBits-1));
  1342. // (1 << X) >= 0 -> X != 31
  1343. // (1 << X) > 0 -> X != 31
  1344. if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
  1345. return new ICmpInst(ICmpInst::ICMP_NE, X,
  1346. ConstantInt::get(RHS->getType(), TypeBits-1));
  1347. }
  1348. } else if (ICI.isEquality()) {
  1349. if (RHSVIsPowerOf2)
  1350. return new ICmpInst(
  1351. Pred, X, ConstantInt::get(RHS->getType(), RHSV.logBase2()));
  1352. }
  1353. }
  1354. break;
  1355. }
  1356. // Check that the shift amount is in range. If not, don't perform
  1357. // undefined shifts. When the shift is visited it will be
  1358. // simplified.
  1359. if (ShAmt->uge(TypeBits))
  1360. break;
  1361. if (ICI.isEquality()) {
  1362. // If we are comparing against bits always shifted out, the
  1363. // comparison cannot succeed.
  1364. Constant *Comp =
  1365. ConstantExpr::getShl(ConstantExpr::getLShr(RHS, ShAmt),
  1366. ShAmt);
  1367. if (Comp != RHS) {// Comparing against a bit that we know is zero.
  1368. bool IsICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
  1369. Constant *Cst = Builder->getInt1(IsICMP_NE);
  1370. return ReplaceInstUsesWith(ICI, Cst);
  1371. }
  1372. // If the shift is NUW, then it is just shifting out zeros, no need for an
  1373. // AND.
  1374. if (cast<BinaryOperator>(LHSI)->hasNoUnsignedWrap())
  1375. return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
  1376. ConstantExpr::getLShr(RHS, ShAmt));
  1377. // If the shift is NSW and we compare to 0, then it is just shifting out
  1378. // sign bits, no need for an AND either.
  1379. if (cast<BinaryOperator>(LHSI)->hasNoSignedWrap() && RHSV == 0)
  1380. return new ICmpInst(ICI.getPredicate(), LHSI->getOperand(0),
  1381. ConstantExpr::getLShr(RHS, ShAmt));
  1382. if (LHSI->hasOneUse()) {
  1383. // Otherwise strength reduce the shift into an and.
  1384. uint32_t ShAmtVal = (uint32_t)ShAmt->getLimitedValue(TypeBits);
  1385. Constant *Mask = Builder->getInt(APInt::getLowBitsSet(TypeBits,
  1386. TypeBits - ShAmtVal));
  1387. Value *And =
  1388. Builder->CreateAnd(LHSI->getOperand(0),Mask, LHSI->getName()+".mask");
  1389. return new ICmpInst(ICI.getPredicate(), And,
  1390. ConstantExpr::getLShr(RHS, ShAmt));
  1391. }
  1392. }
  1393. // If this is a signed comparison to 0 and the shift is sign preserving,
  1394. // use the shift LHS operand instead.
  1395. ICmpInst::Predicate pred = ICI.getPredicate();
  1396. if (isSignTest(pred, RHS) &&
  1397. cast<BinaryOperator>(LHSI)->hasNoSignedWrap())
  1398. return new ICmpInst(pred,
  1399. LHSI->getOperand(0),
  1400. Constant::getNullValue(RHS->getType()));
  1401. // Otherwise, if this is a comparison of the sign bit, simplify to and/test.
  1402. bool TrueIfSigned = false;
  1403. if (LHSI->hasOneUse() &&
  1404. isSignBitCheck(ICI.getPredicate(), RHS, TrueIfSigned)) {
  1405. // (X << 31) <s 0 --> (X&1) != 0
  1406. Constant *Mask = ConstantInt::get(LHSI->getOperand(0)->getType(),
  1407. APInt::getOneBitSet(TypeBits,
  1408. TypeBits-ShAmt->getZExtValue()-1));
  1409. Value *And =
  1410. Builder->CreateAnd(LHSI->getOperand(0), Mask, LHSI->getName()+".mask");
  1411. return new ICmpInst(TrueIfSigned ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ,
  1412. And, Constant::getNullValue(And->getType()));
  1413. }
  1414. // Transform (icmp pred iM (shl iM %v, N), CI)
  1415. // -> (icmp pred i(M-N) (trunc %v iM to i(M-N)), (trunc (CI>>N))
  1416. // Transform the shl to a trunc if (trunc (CI>>N)) has no loss and M-N.
  1417. // This enables to get rid of the shift in favor of a trunc which can be
  1418. // free on the target. It has the additional benefit of comparing to a
  1419. // smaller constant, which will be target friendly.
  1420. unsigned Amt = ShAmt->getLimitedValue(TypeBits-1);
  1421. if (LHSI->hasOneUse() &&
  1422. Amt != 0 && RHSV.countTrailingZeros() >= Amt) {
  1423. Type *NTy = IntegerType::get(ICI.getContext(), TypeBits - Amt);
  1424. Constant *NCI = ConstantExpr::getTrunc(
  1425. ConstantExpr::getAShr(RHS,
  1426. ConstantInt::get(RHS->getType(), Amt)),
  1427. NTy);
  1428. return new ICmpInst(ICI.getPredicate(),
  1429. Builder->CreateTrunc(LHSI->getOperand(0), NTy),
  1430. NCI);
  1431. }
  1432. break;
  1433. }
  1434. case Instruction::LShr: // (icmp pred (shr X, ShAmt), CI)
  1435. case Instruction::AShr: {
  1436. // Handle equality comparisons of shift-by-constant.
  1437. BinaryOperator *BO = cast<BinaryOperator>(LHSI);
  1438. if (ConstantInt *ShAmt = dyn_cast<ConstantInt>(LHSI->getOperand(1))) {
  1439. if (Instruction *Res = FoldICmpShrCst(ICI, BO, ShAmt))
  1440. return Res;
  1441. }
  1442. // Handle exact shr's.
  1443. if (ICI.isEquality() && BO->isExact() && BO->hasOneUse()) {
  1444. if (RHSV.isMinValue())
  1445. return new ICmpInst(ICI.getPredicate(), BO->getOperand(0), RHS);
  1446. }
  1447. break;
  1448. }
  1449. case Instruction::SDiv:
  1450. case Instruction::UDiv:
  1451. // Fold: icmp pred ([us]div X, C1), C2 -> range test
  1452. // Fold this div into the comparison, producing a range check.
  1453. // Determine, based on the divide type, what the range is being
  1454. // checked. If there is an overflow on the low or high side, remember
  1455. // it, otherwise compute the range [low, hi) bounding the new value.
  1456. // See: InsertRangeTest above for the kinds of replacements possible.
  1457. if (ConstantInt *DivRHS = dyn_cast<ConstantInt>(LHSI->getOperand(1)))
  1458. if (Instruction *R = FoldICmpDivCst(ICI, cast<BinaryOperator>(LHSI),
  1459. DivRHS))
  1460. return R;
  1461. break;
  1462. case Instruction::Sub: {
  1463. ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(0));
  1464. if (!LHSC) break;
  1465. const APInt &LHSV = LHSC->getValue();
  1466. // C1-X <u C2 -> (X|(C2-1)) == C1
  1467. // iff C1 & (C2-1) == C2-1
  1468. // C2 is a power of 2
  1469. if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() &&
  1470. RHSV.isPowerOf2() && (LHSV & (RHSV - 1)) == (RHSV - 1))
  1471. return new ICmpInst(ICmpInst::ICMP_EQ,
  1472. Builder->CreateOr(LHSI->getOperand(1), RHSV - 1),
  1473. LHSC);
  1474. // C1-X >u C2 -> (X|C2) != C1
  1475. // iff C1 & C2 == C2
  1476. // C2+1 is a power of 2
  1477. if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() &&
  1478. (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == RHSV)
  1479. return new ICmpInst(ICmpInst::ICMP_NE,
  1480. Builder->CreateOr(LHSI->getOperand(1), RHSV), LHSC);
  1481. break;
  1482. }
  1483. case Instruction::Add:
  1484. // Fold: icmp pred (add X, C1), C2
  1485. if (!ICI.isEquality()) {
  1486. ConstantInt *LHSC = dyn_cast<ConstantInt>(LHSI->getOperand(1));
  1487. if (!LHSC) break;
  1488. const APInt &LHSV = LHSC->getValue();
  1489. ConstantRange CR = ICI.makeConstantRange(ICI.getPredicate(), RHSV)
  1490. .subtract(LHSV);
  1491. if (ICI.isSigned()) {
  1492. if (CR.getLower().isSignBit()) {
  1493. return new ICmpInst(ICmpInst::ICMP_SLT, LHSI->getOperand(0),
  1494. Builder->getInt(CR.getUpper()));
  1495. } else if (CR.getUpper().isSignBit()) {
  1496. return new ICmpInst(ICmpInst::ICMP_SGE, LHSI->getOperand(0),
  1497. Builder->getInt(CR.getLower()));
  1498. }
  1499. } else {
  1500. if (CR.getLower().isMinValue()) {
  1501. return new ICmpInst(ICmpInst::ICMP_ULT, LHSI->getOperand(0),
  1502. Builder->getInt(CR.getUpper()));
  1503. } else if (CR.getUpper().isMinValue()) {
  1504. return new ICmpInst(ICmpInst::ICMP_UGE, LHSI->getOperand(0),
  1505. Builder->getInt(CR.getLower()));
  1506. }
  1507. }
  1508. // X-C1 <u C2 -> (X & -C2) == C1
  1509. // iff C1 & (C2-1) == 0
  1510. // C2 is a power of 2
  1511. if (ICI.getPredicate() == ICmpInst::ICMP_ULT && LHSI->hasOneUse() &&
  1512. RHSV.isPowerOf2() && (LHSV & (RHSV - 1)) == 0)
  1513. return new ICmpInst(ICmpInst::ICMP_EQ,
  1514. Builder->CreateAnd(LHSI->getOperand(0), -RHSV),
  1515. ConstantExpr::getNeg(LHSC));
  1516. // X-C1 >u C2 -> (X & ~C2) != C1
  1517. // iff C1 & C2 == 0
  1518. // C2+1 is a power of 2
  1519. if (ICI.getPredicate() == ICmpInst::ICMP_UGT && LHSI->hasOneUse() &&
  1520. (RHSV + 1).isPowerOf2() && (LHSV & RHSV) == 0)
  1521. return new ICmpInst(ICmpInst::ICMP_NE,
  1522. Builder->CreateAnd(LHSI->getOperand(0), ~RHSV),
  1523. ConstantExpr::getNeg(LHSC));
  1524. }
  1525. break;
  1526. }
  1527. // Simplify icmp_eq and icmp_ne instructions with integer constant RHS.
  1528. if (ICI.isEquality()) {
  1529. bool isICMP_NE = ICI.getPredicate() == ICmpInst::ICMP_NE;
  1530. // If the first operand is (add|sub|and|or|xor|rem) with a constant, and
  1531. // the second operand is a constant, simplify a bit.
  1532. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(LHSI)) {
  1533. switch (BO->getOpcode()) {
  1534. case Instruction::SRem:
  1535. // If we have a signed (X % (2^c)) == 0, turn it into an unsigned one.
  1536. if (RHSV == 0 && isa<ConstantInt>(BO->getOperand(1)) &&BO->hasOneUse()){
  1537. const APInt &V = cast<ConstantInt>(BO->getOperand(1))->getValue();
  1538. if (V.sgt(1) && V.isPowerOf2()) {
  1539. Value *NewRem =
  1540. Builder->CreateURem(BO->getOperand(0), BO->getOperand(1),
  1541. BO->getName());
  1542. return new ICmpInst(ICI.getPredicate(), NewRem,
  1543. Constant::getNullValue(BO->getType()));
  1544. }
  1545. }
  1546. break;
  1547. case Instruction::Add:
  1548. // Replace ((add A, B) != C) with (A != C-B) if B & C are constants.
  1549. if (ConstantInt *BOp1C = dyn_cast<ConstantInt>(BO->getOperand(1))) {
  1550. if (BO->hasOneUse())
  1551. return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
  1552. ConstantExpr::getSub(RHS, BOp1C));
  1553. } else if (RHSV == 0) {
  1554. // Replace ((add A, B) != 0) with (A != -B) if A or B is
  1555. // efficiently invertible, or if the add has just this one use.
  1556. Value *BOp0 = BO->getOperand(0), *BOp1 = BO->getOperand(1);
  1557. if (Value *NegVal = dyn_castNegVal(BOp1))
  1558. return new ICmpInst(ICI.getPredicate(), BOp0, NegVal);
  1559. if (Value *NegVal = dyn_castNegVal(BOp0))
  1560. return new ICmpInst(ICI.getPredicate(), NegVal, BOp1);
  1561. if (BO->hasOneUse()) {
  1562. Value *Neg = Builder->CreateNeg(BOp1);
  1563. Neg->takeName(BO);
  1564. return new ICmpInst(ICI.getPredicate(), BOp0, Neg);
  1565. }
  1566. }
  1567. break;
  1568. case Instruction::Xor:
  1569. // For the xor case, we can xor two constants together, eliminating
  1570. // the explicit xor.
  1571. if (Constant *BOC = dyn_cast<Constant>(BO->getOperand(1))) {
  1572. return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
  1573. ConstantExpr::getXor(RHS, BOC));
  1574. } else if (RHSV == 0) {
  1575. // Replace ((xor A, B) != 0) with (A != B)
  1576. return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
  1577. BO->getOperand(1));
  1578. }
  1579. break;
  1580. case Instruction::Sub:
  1581. // Replace ((sub A, B) != C) with (B != A-C) if A & C are constants.
  1582. if (ConstantInt *BOp0C = dyn_cast<ConstantInt>(BO->getOperand(0))) {
  1583. if (BO->hasOneUse())
  1584. return new ICmpInst(ICI.getPredicate(), BO->getOperand(1),
  1585. ConstantExpr::getSub(BOp0C, RHS));
  1586. } else if (RHSV == 0) {
  1587. // Replace ((sub A, B) != 0) with (A != B)
  1588. return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
  1589. BO->getOperand(1));
  1590. }
  1591. break;
  1592. case Instruction::Or:
  1593. // If bits are being or'd in that are not present in the constant we
  1594. // are comparing against, then the comparison could never succeed!
  1595. if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
  1596. Constant *NotCI = ConstantExpr::getNot(RHS);
  1597. if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
  1598. return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
  1599. }
  1600. break;
  1601. case Instruction::And:
  1602. if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
  1603. // If bits are being compared against that are and'd out, then the
  1604. // comparison can never succeed!
  1605. if ((RHSV & ~BOC->getValue()) != 0)
  1606. return ReplaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
  1607. // If we have ((X & C) == C), turn it into ((X & C) != 0).
  1608. if (RHS == BOC && RHSV.isPowerOf2())
  1609. return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ :
  1610. ICmpInst::ICMP_NE, LHSI,
  1611. Constant::getNullValue(RHS->getType()));
  1612. // Don't perform the following transforms if the AND has multiple uses
  1613. if (!BO->hasOneUse())
  1614. break;
  1615. // Replace (and X, (1 << size(X)-1) != 0) with x s< 0
  1616. if (BOC->getValue().isSignBit()) {
  1617. Value *X = BO->getOperand(0);
  1618. Constant *Zero = Constant::getNullValue(X->getType());
  1619. ICmpInst::Predicate pred = isICMP_NE ?
  1620. ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGE;
  1621. return new ICmpInst(pred, X, Zero);
  1622. }
  1623. // ((X & ~7) == 0) --> X < 8
  1624. if (RHSV == 0 && isHighOnes(BOC)) {
  1625. Value *X = BO->getOperand(0);
  1626. Constant *NegX = ConstantExpr::getNeg(BOC);
  1627. ICmpInst::Predicate pred = isICMP_NE ?
  1628. ICmpInst::ICMP_UGE : ICmpInst::ICMP_ULT;
  1629. return new ICmpInst(pred, X, NegX);
  1630. }
  1631. }
  1632. break;
  1633. case Instruction::Mul:
  1634. if (RHSV == 0 && BO->hasNoSignedWrap()) {
  1635. if (ConstantInt *BOC = dyn_cast<ConstantInt>(BO->getOperand(1))) {
  1636. // The trivial case (mul X, 0) is handled by InstSimplify
  1637. // General case : (mul X, C) != 0 iff X != 0
  1638. // (mul X, C) == 0 iff X == 0
  1639. if (!BOC->isZero())
  1640. return new ICmpInst(ICI.getPredicate(), BO->getOperand(0),
  1641. Constant::getNullValue(RHS->getType()));
  1642. }
  1643. }
  1644. break;
  1645. default: break;
  1646. }
  1647. } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI)) {
  1648. // Handle icmp {eq|ne} <intrinsic>, intcst.
  1649. switch (II->getIntrinsicID()) {
  1650. case Intrinsic::bswap:
  1651. Worklist.Add(II);
  1652. ICI.setOperand(0, II->getArgOperand(0));
  1653. ICI.setOperand(1, Builder->getInt(RHSV.byteSwap()));
  1654. return &ICI;
  1655. case Intrinsic::ctlz:
  1656. case Intrinsic::cttz:
  1657. // ctz(A) == bitwidth(a) -> A == 0 and likewise for !=
  1658. if (RHSV == RHS->getType()->getBitWidth()) {
  1659. Worklist.Add(II);
  1660. ICI.setOperand(0, II->getArgOperand(0));
  1661. ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
  1662. return &ICI;
  1663. }
  1664. break;
  1665. case Intrinsic::ctpop:
  1666. // popcount(A) == 0 -> A == 0 and likewise for !=
  1667. if (RHS->isZero()) {
  1668. Worklist.Add(II);
  1669. ICI.setOperand(0, II->getArgOperand(0));
  1670. ICI.setOperand(1, RHS);
  1671. return &ICI;
  1672. }
  1673. break;
  1674. default:
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. return nullptr;
  1680. }
  1681. /// visitICmpInstWithCastAndCast - Handle icmp (cast x to y), (cast/cst).
  1682. /// We only handle extending casts so far.
  1683. ///
  1684. Instruction *InstCombiner::visitICmpInstWithCastAndCast(ICmpInst &ICI) {
  1685. const CastInst *LHSCI = cast<CastInst>(ICI.getOperand(0));
  1686. Value *LHSCIOp = LHSCI->getOperand(0);
  1687. Type *SrcTy = LHSCIOp->getType();
  1688. Type *DestTy = LHSCI->getType();
  1689. Value *RHSCIOp;
  1690. // Turn icmp (ptrtoint x), (ptrtoint/c) into a compare of the input if the
  1691. // integer type is the same size as the pointer type.
  1692. if (LHSCI->getOpcode() == Instruction::PtrToInt &&
  1693. DL.getPointerTypeSizeInBits(SrcTy) == DestTy->getIntegerBitWidth()) {
  1694. Value *RHSOp = nullptr;
  1695. if (PtrToIntOperator *RHSC = dyn_cast<PtrToIntOperator>(ICI.getOperand(1))) {
  1696. Value *RHSCIOp = RHSC->getOperand(0);
  1697. if (RHSCIOp->getType()->getPointerAddressSpace() ==
  1698. LHSCIOp->getType()->getPointerAddressSpace()) {
  1699. RHSOp = RHSC->getOperand(0);
  1700. // If the pointer types don't match, insert a bitcast.
  1701. if (LHSCIOp->getType() != RHSOp->getType())
  1702. RHSOp = Builder->CreateBitCast(RHSOp, LHSCIOp->getType());
  1703. }
  1704. } else if (Constant *RHSC = dyn_cast<Constant>(ICI.getOperand(1)))
  1705. RHSOp = ConstantExpr::getIntToPtr(RHSC, SrcTy);
  1706. if (RHSOp)
  1707. return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSOp);
  1708. }
  1709. // The code below only handles extension cast instructions, so far.
  1710. // Enforce this.
  1711. if (LHSCI->getOpcode() != Instruction::ZExt &&
  1712. LHSCI->getOpcode() != Instruction::SExt)
  1713. return nullptr;
  1714. bool isSignedExt = LHSCI->getOpcode() == Instruction::SExt;
  1715. bool isSignedCmp = ICI.isSigned();
  1716. if (CastInst *CI = dyn_cast<CastInst>(ICI.getOperand(1))) {
  1717. // Not an extension from the same type?
  1718. RHSCIOp = CI->getOperand(0);
  1719. if (RHSCIOp->getType() != LHSCIOp->getType())
  1720. return nullptr;
  1721. // If the signedness of the two casts doesn't agree (i.e. one is a sext
  1722. // and the other is a zext), then we can't handle this.
  1723. if (CI->getOpcode() != LHSCI->getOpcode())
  1724. return nullptr;
  1725. // Deal with equality cases early.
  1726. if (ICI.isEquality())
  1727. return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
  1728. // A signed comparison of sign extended values simplifies into a
  1729. // signed comparison.
  1730. if (isSignedCmp && isSignedExt)
  1731. return new ICmpInst(ICI.getPredicate(), LHSCIOp, RHSCIOp);
  1732. // The other three cases all fold into an unsigned comparison.
  1733. return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, RHSCIOp);
  1734. }
  1735. // If we aren't dealing with a constant on the RHS, exit early
  1736. ConstantInt *CI = dyn_cast<ConstantInt>(ICI.getOperand(1));
  1737. if (!CI)
  1738. return nullptr;
  1739. // Compute the constant that would happen if we truncated to SrcTy then
  1740. // reextended to DestTy.
  1741. Constant *Res1 = ConstantExpr::getTrunc(CI, SrcTy);
  1742. Constant *Res2 = ConstantExpr::getCast(LHSCI->getOpcode(),
  1743. Res1, DestTy);
  1744. // If the re-extended constant didn't change...
  1745. if (Res2 == CI) {
  1746. // Deal with equality cases early.
  1747. if (ICI.isEquality())
  1748. return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
  1749. // A signed comparison of sign extended values simplifies into a
  1750. // signed comparison.
  1751. if (isSignedExt && isSignedCmp)
  1752. return new ICmpInst(ICI.getPredicate(), LHSCIOp, Res1);
  1753. // The other three cases all fold into an unsigned comparison.
  1754. return new ICmpInst(ICI.getUnsignedPredicate(), LHSCIOp, Res1);
  1755. }
  1756. // The re-extended constant changed so the constant cannot be represented
  1757. // in the shorter type. Consequently, we cannot emit a simple comparison.
  1758. // All the cases that fold to true or false will have already been handled
  1759. // by SimplifyICmpInst, so only deal with the tricky case.
  1760. if (isSignedCmp || !isSignedExt)
  1761. return nullptr;
  1762. // Evaluate the comparison for LT (we invert for GT below). LE and GE cases
  1763. // should have been folded away previously and not enter in here.
  1764. // We're performing an unsigned comp with a sign extended value.
  1765. // This is true if the input is >= 0. [aka >s -1]
  1766. Constant *NegOne = Constant::getAllOnesValue(SrcTy);
  1767. Value *Result = Builder->CreateICmpSGT(LHSCIOp, NegOne, ICI.getName());
  1768. // Finally, return the value computed.
  1769. if (ICI.getPredicate() == ICmpInst::ICMP_ULT)
  1770. return ReplaceInstUsesWith(ICI, Result);
  1771. assert(ICI.getPredicate() == ICmpInst::ICMP_UGT && "ICmp should be folded!");
  1772. return BinaryOperator::CreateNot(Result);
  1773. }
  1774. /// ProcessUGT_ADDCST_ADD - The caller has matched a pattern of the form:
  1775. /// I = icmp ugt (add (add A, B), CI2), CI1
  1776. /// If this is of the form:
  1777. /// sum = a + b
  1778. /// if (sum+128 >u 255)
  1779. /// Then replace it with llvm.sadd.with.overflow.i8.
  1780. ///
  1781. static Instruction *ProcessUGT_ADDCST_ADD(ICmpInst &I, Value *A, Value *B,
  1782. ConstantInt *CI2, ConstantInt *CI1,
  1783. InstCombiner &IC) {
  1784. // The transformation we're trying to do here is to transform this into an
  1785. // llvm.sadd.with.overflow. To do this, we have to replace the original add
  1786. // with a narrower add, and discard the add-with-constant that is part of the
  1787. // range check (if we can't eliminate it, this isn't profitable).
  1788. // In order to eliminate the add-with-constant, the compare can be its only
  1789. // use.
  1790. Instruction *AddWithCst = cast<Instruction>(I.getOperand(0));
  1791. if (!AddWithCst->hasOneUse()) return nullptr;
  1792. // If CI2 is 2^7, 2^15, 2^31, then it might be an sadd.with.overflow.
  1793. if (!CI2->getValue().isPowerOf2()) return nullptr;
  1794. unsigned NewWidth = CI2->getValue().countTrailingZeros();
  1795. if (NewWidth != 7 && NewWidth != 15 && NewWidth != 31) return nullptr;
  1796. // The width of the new add formed is 1 more than the bias.
  1797. ++NewWidth;
  1798. // Check to see that CI1 is an all-ones value with NewWidth bits.
  1799. if (CI1->getBitWidth() == NewWidth ||
  1800. CI1->getValue() != APInt::getLowBitsSet(CI1->getBitWidth(), NewWidth))
  1801. return nullptr;
  1802. // This is only really a signed overflow check if the inputs have been
  1803. // sign-extended; check for that condition. For example, if CI2 is 2^31 and
  1804. // the operands of the add are 64 bits wide, we need at least 33 sign bits.
  1805. unsigned NeededSignBits = CI1->getBitWidth() - NewWidth + 1;
  1806. if (IC.ComputeNumSignBits(A, 0, &I) < NeededSignBits ||
  1807. IC.ComputeNumSignBits(B, 0, &I) < NeededSignBits)
  1808. return nullptr;
  1809. // In order to replace the original add with a narrower
  1810. // llvm.sadd.with.overflow, the only uses allowed are the add-with-constant
  1811. // and truncates that discard the high bits of the add. Verify that this is
  1812. // the case.
  1813. Instruction *OrigAdd = cast<Instruction>(AddWithCst->getOperand(0));
  1814. for (User *U : OrigAdd->users()) {
  1815. if (U == AddWithCst) continue;
  1816. // Only accept truncates for now. We would really like a nice recursive
  1817. // predicate like SimplifyDemandedBits, but which goes downwards the use-def
  1818. // chain to see which bits of a value are actually demanded. If the
  1819. // original add had another add which was then immediately truncated, we
  1820. // could still do the transformation.
  1821. TruncInst *TI = dyn_cast<TruncInst>(U);
  1822. if (!TI || TI->getType()->getPrimitiveSizeInBits() > NewWidth)
  1823. return nullptr;
  1824. }
  1825. // If the pattern matches, truncate the inputs to the narrower type and
  1826. // use the sadd_with_overflow intrinsic to efficiently compute both the
  1827. // result and the overflow bit.
  1828. Module *M = I.getParent()->getParent()->getParent();
  1829. Type *NewType = IntegerType::get(OrigAdd->getContext(), NewWidth);
  1830. Value *F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow,
  1831. NewType);
  1832. InstCombiner::BuilderTy *Builder = IC.Builder;
  1833. // Put the new code above the original add, in case there are any uses of the
  1834. // add between the add and the compare.
  1835. Builder->SetInsertPoint(OrigAdd);
  1836. Value *TruncA = Builder->CreateTrunc(A, NewType, A->getName()+".trunc");
  1837. Value *TruncB = Builder->CreateTrunc(B, NewType, B->getName()+".trunc");
  1838. CallInst *Call = Builder->CreateCall(F, {TruncA, TruncB}, "sadd");
  1839. Value *Add = Builder->CreateExtractValue(Call, 0, "sadd.result");
  1840. Value *ZExt = Builder->CreateZExt(Add, OrigAdd->getType());
  1841. // The inner add was the result of the narrow add, zero extended to the
  1842. // wider type. Replace it with the result computed by the intrinsic.
  1843. IC.ReplaceInstUsesWith(*OrigAdd, ZExt);
  1844. // The original icmp gets replaced with the overflow value.
  1845. return ExtractValueInst::Create(Call, 1, "sadd.overflow");
  1846. }
  1847. bool InstCombiner::OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS,
  1848. Value *RHS, Instruction &OrigI,
  1849. Value *&Result, Constant *&Overflow) {
  1850. if (OrigI.isCommutative() && isa<Constant>(LHS) && !isa<Constant>(RHS))
  1851. std::swap(LHS, RHS);
  1852. auto SetResult = [&](Value *OpResult, Constant *OverflowVal, bool ReuseName) {
  1853. Result = OpResult;
  1854. Overflow = OverflowVal;
  1855. if (ReuseName)
  1856. Result->takeName(&OrigI);
  1857. return true;
  1858. };
  1859. switch (OCF) {
  1860. case OCF_INVALID:
  1861. llvm_unreachable("bad overflow check kind!");
  1862. case OCF_UNSIGNED_ADD: {
  1863. OverflowResult OR = computeOverflowForUnsignedAdd(LHS, RHS, &OrigI);
  1864. if (OR == OverflowResult::NeverOverflows)
  1865. return SetResult(Builder->CreateNUWAdd(LHS, RHS), Builder->getFalse(),
  1866. true);
  1867. if (OR == OverflowResult::AlwaysOverflows)
  1868. return SetResult(Builder->CreateAdd(LHS, RHS), Builder->getTrue(), true);
  1869. }
  1870. // FALL THROUGH uadd into sadd
  1871. case OCF_SIGNED_ADD: {
  1872. // X + 0 -> {X, false}
  1873. if (match(RHS, m_Zero()))
  1874. return SetResult(LHS, Builder->getFalse(), false);
  1875. // We can strength reduce this signed add into a regular add if we can prove
  1876. // that it will never overflow.
  1877. if (OCF == OCF_SIGNED_ADD)
  1878. if (WillNotOverflowSignedAdd(LHS, RHS, OrigI))
  1879. return SetResult(Builder->CreateNSWAdd(LHS, RHS), Builder->getFalse(),
  1880. true);
  1881. break;
  1882. }
  1883. case OCF_UNSIGNED_SUB:
  1884. case OCF_SIGNED_SUB: {
  1885. // X - 0 -> {X, false}
  1886. if (match(RHS, m_Zero()))
  1887. return SetResult(LHS, Builder->getFalse(), false);
  1888. if (OCF == OCF_SIGNED_SUB) {
  1889. if (WillNotOverflowSignedSub(LHS, RHS, OrigI))
  1890. return SetResult(Builder->CreateNSWSub(LHS, RHS), Builder->getFalse(),
  1891. true);
  1892. } else {
  1893. if (WillNotOverflowUnsignedSub(LHS, RHS, OrigI))
  1894. return SetResult(Builder->CreateNUWSub(LHS, RHS), Builder->getFalse(),
  1895. true);
  1896. }
  1897. break;
  1898. }
  1899. case OCF_UNSIGNED_MUL: {
  1900. OverflowResult OR = computeOverflowForUnsignedMul(LHS, RHS, &OrigI);
  1901. if (OR == OverflowResult::NeverOverflows)
  1902. return SetResult(Builder->CreateNUWMul(LHS, RHS), Builder->getFalse(),
  1903. true);
  1904. if (OR == OverflowResult::AlwaysOverflows)
  1905. return SetResult(Builder->CreateMul(LHS, RHS), Builder->getTrue(), true);
  1906. } // FALL THROUGH
  1907. case OCF_SIGNED_MUL:
  1908. // X * undef -> undef
  1909. if (isa<UndefValue>(RHS))
  1910. return SetResult(RHS, UndefValue::get(Builder->getInt1Ty()), false);
  1911. // X * 0 -> {0, false}
  1912. if (match(RHS, m_Zero()))
  1913. return SetResult(RHS, Builder->getFalse(), false);
  1914. // X * 1 -> {X, false}
  1915. if (match(RHS, m_One()))
  1916. return SetResult(LHS, Builder->getFalse(), false);
  1917. if (OCF == OCF_SIGNED_MUL)
  1918. if (WillNotOverflowSignedMul(LHS, RHS, OrigI))
  1919. return SetResult(Builder->CreateNSWMul(LHS, RHS), Builder->getFalse(),
  1920. true);
  1921. break;
  1922. }
  1923. return false;
  1924. }
  1925. /// \brief Recognize and process idiom involving test for multiplication
  1926. /// overflow.
  1927. ///
  1928. /// The caller has matched a pattern of the form:
  1929. /// I = cmp u (mul(zext A, zext B), V
  1930. /// The function checks if this is a test for overflow and if so replaces
  1931. /// multiplication with call to 'mul.with.overflow' intrinsic.
  1932. ///
  1933. /// \param I Compare instruction.
  1934. /// \param MulVal Result of 'mult' instruction. It is one of the arguments of
  1935. /// the compare instruction. Must be of integer type.
  1936. /// \param OtherVal The other argument of compare instruction.
  1937. /// \returns Instruction which must replace the compare instruction, NULL if no
  1938. /// replacement required.
  1939. static Instruction *ProcessUMulZExtIdiom(ICmpInst &I, Value *MulVal,
  1940. Value *OtherVal, InstCombiner &IC) {
  1941. // Don't bother doing this transformation for pointers, don't do it for
  1942. // vectors.
  1943. if (!isa<IntegerType>(MulVal->getType()))
  1944. return nullptr;
  1945. assert(I.getOperand(0) == MulVal || I.getOperand(1) == MulVal);
  1946. assert(I.getOperand(0) == OtherVal || I.getOperand(1) == OtherVal);
  1947. Instruction *MulInstr = cast<Instruction>(MulVal);
  1948. assert(MulInstr->getOpcode() == Instruction::Mul);
  1949. auto *LHS = cast<ZExtOperator>(MulInstr->getOperand(0)),
  1950. *RHS = cast<ZExtOperator>(MulInstr->getOperand(1));
  1951. assert(LHS->getOpcode() == Instruction::ZExt);
  1952. assert(RHS->getOpcode() == Instruction::ZExt);
  1953. Value *A = LHS->getOperand(0), *B = RHS->getOperand(0);
  1954. // Calculate type and width of the result produced by mul.with.overflow.
  1955. Type *TyA = A->getType(), *TyB = B->getType();
  1956. unsigned WidthA = TyA->getPrimitiveSizeInBits(),
  1957. WidthB = TyB->getPrimitiveSizeInBits();
  1958. unsigned MulWidth;
  1959. Type *MulType;
  1960. if (WidthB > WidthA) {
  1961. MulWidth = WidthB;
  1962. MulType = TyB;
  1963. } else {
  1964. MulWidth = WidthA;
  1965. MulType = TyA;
  1966. }
  1967. // In order to replace the original mul with a narrower mul.with.overflow,
  1968. // all uses must ignore upper bits of the product. The number of used low
  1969. // bits must be not greater than the width of mul.with.overflow.
  1970. if (MulVal->hasNUsesOrMore(2))
  1971. for (User *U : MulVal->users()) {
  1972. if (U == &I)
  1973. continue;
  1974. if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
  1975. // Check if truncation ignores bits above MulWidth.
  1976. unsigned TruncWidth = TI->getType()->getPrimitiveSizeInBits();
  1977. if (TruncWidth > MulWidth)
  1978. return nullptr;
  1979. } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
  1980. // Check if AND ignores bits above MulWidth.
  1981. if (BO->getOpcode() != Instruction::And)
  1982. return nullptr;
  1983. if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
  1984. const APInt &CVal = CI->getValue();
  1985. if (CVal.getBitWidth() - CVal.countLeadingZeros() > MulWidth)
  1986. return nullptr;
  1987. }
  1988. } else {
  1989. // Other uses prohibit this transformation.
  1990. return nullptr;
  1991. }
  1992. }
  1993. // Recognize patterns
  1994. switch (I.getPredicate()) {
  1995. case ICmpInst::ICMP_EQ:
  1996. case ICmpInst::ICMP_NE:
  1997. // Recognize pattern:
  1998. // mulval = mul(zext A, zext B)
  1999. // cmp eq/neq mulval, zext trunc mulval
  2000. if (ZExtInst *Zext = dyn_cast<ZExtInst>(OtherVal))
  2001. if (Zext->hasOneUse()) {
  2002. Value *ZextArg = Zext->getOperand(0);
  2003. if (TruncInst *Trunc = dyn_cast<TruncInst>(ZextArg))
  2004. if (Trunc->getType()->getPrimitiveSizeInBits() == MulWidth)
  2005. break; //Recognized
  2006. }
  2007. // Recognize pattern:
  2008. // mulval = mul(zext A, zext B)
  2009. // cmp eq/neq mulval, and(mulval, mask), mask selects low MulWidth bits.
  2010. ConstantInt *CI;
  2011. Value *ValToMask;
  2012. if (match(OtherVal, m_And(m_Value(ValToMask), m_ConstantInt(CI)))) {
  2013. if (ValToMask != MulVal)
  2014. return nullptr;
  2015. const APInt &CVal = CI->getValue() + 1;
  2016. if (CVal.isPowerOf2()) {
  2017. unsigned MaskWidth = CVal.logBase2();
  2018. if (MaskWidth == MulWidth)
  2019. break; // Recognized
  2020. }
  2021. }
  2022. return nullptr;
  2023. case ICmpInst::ICMP_UGT:
  2024. // Recognize pattern:
  2025. // mulval = mul(zext A, zext B)
  2026. // cmp ugt mulval, max
  2027. if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
  2028. APInt MaxVal = APInt::getMaxValue(MulWidth);
  2029. MaxVal = MaxVal.zext(CI->getBitWidth());
  2030. if (MaxVal.eq(CI->getValue()))
  2031. break; // Recognized
  2032. }
  2033. return nullptr;
  2034. case ICmpInst::ICMP_UGE:
  2035. // Recognize pattern:
  2036. // mulval = mul(zext A, zext B)
  2037. // cmp uge mulval, max+1
  2038. if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
  2039. APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth);
  2040. if (MaxVal.eq(CI->getValue()))
  2041. break; // Recognized
  2042. }
  2043. return nullptr;
  2044. case ICmpInst::ICMP_ULE:
  2045. // Recognize pattern:
  2046. // mulval = mul(zext A, zext B)
  2047. // cmp ule mulval, max
  2048. if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
  2049. APInt MaxVal = APInt::getMaxValue(MulWidth);
  2050. MaxVal = MaxVal.zext(CI->getBitWidth());
  2051. if (MaxVal.eq(CI->getValue()))
  2052. break; // Recognized
  2053. }
  2054. return nullptr;
  2055. case ICmpInst::ICMP_ULT:
  2056. // Recognize pattern:
  2057. // mulval = mul(zext A, zext B)
  2058. // cmp ule mulval, max + 1
  2059. if (ConstantInt *CI = dyn_cast<ConstantInt>(OtherVal)) {
  2060. APInt MaxVal = APInt::getOneBitSet(CI->getBitWidth(), MulWidth);
  2061. if (MaxVal.eq(CI->getValue()))
  2062. break; // Recognized
  2063. }
  2064. return nullptr;
  2065. default:
  2066. return nullptr;
  2067. }
  2068. InstCombiner::BuilderTy *Builder = IC.Builder;
  2069. Builder->SetInsertPoint(MulInstr);
  2070. Module *M = I.getParent()->getParent()->getParent();
  2071. // Replace: mul(zext A, zext B) --> mul.with.overflow(A, B)
  2072. Value *MulA = A, *MulB = B;
  2073. if (WidthA < MulWidth)
  2074. MulA = Builder->CreateZExt(A, MulType);
  2075. if (WidthB < MulWidth)
  2076. MulB = Builder->CreateZExt(B, MulType);
  2077. Value *F =
  2078. Intrinsic::getDeclaration(M, Intrinsic::umul_with_overflow, MulType);
  2079. CallInst *Call = Builder->CreateCall(F, {MulA, MulB}, "umul");
  2080. IC.Worklist.Add(MulInstr);
  2081. // If there are uses of mul result other than the comparison, we know that
  2082. // they are truncation or binary AND. Change them to use result of
  2083. // mul.with.overflow and adjust properly mask/size.
  2084. if (MulVal->hasNUsesOrMore(2)) {
  2085. Value *Mul = Builder->CreateExtractValue(Call, 0, "umul.value");
  2086. for (User *U : MulVal->users()) {
  2087. if (U == &I || U == OtherVal)
  2088. continue;
  2089. if (TruncInst *TI = dyn_cast<TruncInst>(U)) {
  2090. if (TI->getType()->getPrimitiveSizeInBits() == MulWidth)
  2091. IC.ReplaceInstUsesWith(*TI, Mul);
  2092. else
  2093. TI->setOperand(0, Mul);
  2094. } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
  2095. assert(BO->getOpcode() == Instruction::And);
  2096. // Replace (mul & mask) --> zext (mul.with.overflow & short_mask)
  2097. ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1));
  2098. APInt ShortMask = CI->getValue().trunc(MulWidth);
  2099. Value *ShortAnd = Builder->CreateAnd(Mul, ShortMask);
  2100. Instruction *Zext =
  2101. cast<Instruction>(Builder->CreateZExt(ShortAnd, BO->getType()));
  2102. IC.Worklist.Add(Zext);
  2103. IC.ReplaceInstUsesWith(*BO, Zext);
  2104. } else {
  2105. llvm_unreachable("Unexpected Binary operation");
  2106. }
  2107. IC.Worklist.Add(cast<Instruction>(U));
  2108. }
  2109. }
  2110. if (isa<Instruction>(OtherVal))
  2111. IC.Worklist.Add(cast<Instruction>(OtherVal));
  2112. // The original icmp gets replaced with the overflow value, maybe inverted
  2113. // depending on predicate.
  2114. bool Inverse = false;
  2115. switch (I.getPredicate()) {
  2116. case ICmpInst::ICMP_NE:
  2117. break;
  2118. case ICmpInst::ICMP_EQ:
  2119. Inverse = true;
  2120. break;
  2121. case ICmpInst::ICMP_UGT:
  2122. case ICmpInst::ICMP_UGE:
  2123. if (I.getOperand(0) == MulVal)
  2124. break;
  2125. Inverse = true;
  2126. break;
  2127. case ICmpInst::ICMP_ULT:
  2128. case ICmpInst::ICMP_ULE:
  2129. if (I.getOperand(1) == MulVal)
  2130. break;
  2131. Inverse = true;
  2132. break;
  2133. default:
  2134. llvm_unreachable("Unexpected predicate");
  2135. }
  2136. if (Inverse) {
  2137. Value *Res = Builder->CreateExtractValue(Call, 1);
  2138. return BinaryOperator::CreateNot(Res);
  2139. }
  2140. return ExtractValueInst::Create(Call, 1);
  2141. }
  2142. // DemandedBitsLHSMask - When performing a comparison against a constant,
  2143. // it is possible that not all the bits in the LHS are demanded. This helper
  2144. // method computes the mask that IS demanded.
  2145. static APInt DemandedBitsLHSMask(ICmpInst &I,
  2146. unsigned BitWidth, bool isSignCheck) {
  2147. if (isSignCheck)
  2148. return APInt::getSignBit(BitWidth);
  2149. ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
  2150. if (!CI) return APInt::getAllOnesValue(BitWidth);
  2151. const APInt &RHS = CI->getValue();
  2152. switch (I.getPredicate()) {
  2153. // For a UGT comparison, we don't care about any bits that
  2154. // correspond to the trailing ones of the comparand. The value of these
  2155. // bits doesn't impact the outcome of the comparison, because any value
  2156. // greater than the RHS must differ in a bit higher than these due to carry.
  2157. case ICmpInst::ICMP_UGT: {
  2158. unsigned trailingOnes = RHS.countTrailingOnes();
  2159. APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingOnes);
  2160. return ~lowBitsSet;
  2161. }
  2162. // Similarly, for a ULT comparison, we don't care about the trailing zeros.
  2163. // Any value less than the RHS must differ in a higher bit because of carries.
  2164. case ICmpInst::ICMP_ULT: {
  2165. unsigned trailingZeros = RHS.countTrailingZeros();
  2166. APInt lowBitsSet = APInt::getLowBitsSet(BitWidth, trailingZeros);
  2167. return ~lowBitsSet;
  2168. }
  2169. default:
  2170. return APInt::getAllOnesValue(BitWidth);
  2171. }
  2172. }
  2173. /// \brief Check if the order of \p Op0 and \p Op1 as operand in an ICmpInst
  2174. /// should be swapped.
  2175. /// The decision is based on how many times these two operands are reused
  2176. /// as subtract operands and their positions in those instructions.
  2177. /// The rational is that several architectures use the same instruction for
  2178. /// both subtract and cmp, thus it is better if the order of those operands
  2179. /// match.
  2180. /// \return true if Op0 and Op1 should be swapped.
  2181. static bool swapMayExposeCSEOpportunities(const Value * Op0,
  2182. const Value * Op1) {
  2183. // Filter out pointer value as those cannot appears directly in subtract.
  2184. // FIXME: we may want to go through inttoptrs or bitcasts.
  2185. if (Op0->getType()->isPointerTy())
  2186. return false;
  2187. // Count every uses of both Op0 and Op1 in a subtract.
  2188. // Each time Op0 is the first operand, count -1: swapping is bad, the
  2189. // subtract has already the same layout as the compare.
  2190. // Each time Op0 is the second operand, count +1: swapping is good, the
  2191. // subtract has a different layout as the compare.
  2192. // At the end, if the benefit is greater than 0, Op0 should come second to
  2193. // expose more CSE opportunities.
  2194. int GlobalSwapBenefits = 0;
  2195. for (const User *U : Op0->users()) {
  2196. const BinaryOperator *BinOp = dyn_cast<BinaryOperator>(U);
  2197. if (!BinOp || BinOp->getOpcode() != Instruction::Sub)
  2198. continue;
  2199. // If Op0 is the first argument, this is not beneficial to swap the
  2200. // arguments.
  2201. int LocalSwapBenefits = -1;
  2202. unsigned Op1Idx = 1;
  2203. if (BinOp->getOperand(Op1Idx) == Op0) {
  2204. Op1Idx = 0;
  2205. LocalSwapBenefits = 1;
  2206. }
  2207. if (BinOp->getOperand(Op1Idx) != Op1)
  2208. continue;
  2209. GlobalSwapBenefits += LocalSwapBenefits;
  2210. }
  2211. return GlobalSwapBenefits > 0;
  2212. }
  2213. /// \brief Check that one use is in the same block as the definition and all
  2214. /// other uses are in blocks dominated by a given block
  2215. ///
  2216. /// \param DI Definition
  2217. /// \param UI Use
  2218. /// \param DB Block that must dominate all uses of \p DI outside
  2219. /// the parent block
  2220. /// \return true when \p UI is the only use of \p DI in the parent block
  2221. /// and all other uses of \p DI are in blocks dominated by \p DB.
  2222. ///
  2223. bool InstCombiner::dominatesAllUses(const Instruction *DI,
  2224. const Instruction *UI,
  2225. const BasicBlock *DB) const {
  2226. assert(DI && UI && "Instruction not defined\n");
  2227. // ignore incomplete definitions
  2228. if (!DI->getParent())
  2229. return false;
  2230. // DI and UI must be in the same block
  2231. if (DI->getParent() != UI->getParent())
  2232. return false;
  2233. // Protect from self-referencing blocks
  2234. if (DI->getParent() == DB)
  2235. return false;
  2236. // DominatorTree available?
  2237. if (!DT)
  2238. return false;
  2239. for (const User *U : DI->users()) {
  2240. auto *Usr = cast<Instruction>(U);
  2241. if (Usr != UI && !DT->dominates(DB, Usr->getParent()))
  2242. return false;
  2243. }
  2244. return true;
  2245. }
  2246. ///
  2247. /// true when the instruction sequence within a block is select-cmp-br.
  2248. ///
  2249. static bool isChainSelectCmpBranch(const SelectInst *SI) {
  2250. const BasicBlock *BB = SI->getParent();
  2251. if (!BB)
  2252. return false;
  2253. auto *BI = dyn_cast_or_null<BranchInst>(BB->getTerminator());
  2254. if (!BI || BI->getNumSuccessors() != 2)
  2255. return false;
  2256. auto *IC = dyn_cast<ICmpInst>(BI->getCondition());
  2257. if (!IC || (IC->getOperand(0) != SI && IC->getOperand(1) != SI))
  2258. return false;
  2259. return true;
  2260. }
  2261. ///
  2262. /// \brief True when a select result is replaced by one of its operands
  2263. /// in select-icmp sequence. This will eventually result in the elimination
  2264. /// of the select.
  2265. ///
  2266. /// \param SI Select instruction
  2267. /// \param Icmp Compare instruction
  2268. /// \param SIOpd Operand that replaces the select
  2269. ///
  2270. /// Notes:
  2271. /// - The replacement is global and requires dominator information
  2272. /// - The caller is responsible for the actual replacement
  2273. ///
  2274. /// Example:
  2275. ///
  2276. /// entry:
  2277. /// %4 = select i1 %3, %C* %0, %C* null
  2278. /// %5 = icmp eq %C* %4, null
  2279. /// br i1 %5, label %9, label %7
  2280. /// ...
  2281. /// ; <label>:7 ; preds = %entry
  2282. /// %8 = getelementptr inbounds %C* %4, i64 0, i32 0
  2283. /// ...
  2284. ///
  2285. /// can be transformed to
  2286. ///
  2287. /// %5 = icmp eq %C* %0, null
  2288. /// %6 = select i1 %3, i1 %5, i1 true
  2289. /// br i1 %6, label %9, label %7
  2290. /// ...
  2291. /// ; <label>:7 ; preds = %entry
  2292. /// %8 = getelementptr inbounds %C* %0, i64 0, i32 0 // replace by %0!
  2293. ///
  2294. /// Similar when the first operand of the select is a constant or/and
  2295. /// the compare is for not equal rather than equal.
  2296. ///
  2297. /// NOTE: The function is only called when the select and compare constants
  2298. /// are equal, the optimization can work only for EQ predicates. This is not a
  2299. /// major restriction since a NE compare should be 'normalized' to an equal
  2300. /// compare, which usually happens in the combiner and test case
  2301. /// select-cmp-br.ll
  2302. /// checks for it.
  2303. bool InstCombiner::replacedSelectWithOperand(SelectInst *SI,
  2304. const ICmpInst *Icmp,
  2305. const unsigned SIOpd) {
  2306. assert((SIOpd == 1 || SIOpd == 2) && "Invalid select operand!");
  2307. if (isChainSelectCmpBranch(SI) && Icmp->getPredicate() == ICmpInst::ICMP_EQ) {
  2308. BasicBlock *Succ = SI->getParent()->getTerminator()->getSuccessor(1);
  2309. // The check for the unique predecessor is not the best that can be
  2310. // done. But it protects efficiently against cases like when SI's
  2311. // home block has two successors, Succ and Succ1, and Succ1 predecessor
  2312. // of Succ. Then SI can't be replaced by SIOpd because the use that gets
  2313. // replaced can be reached on either path. So the uniqueness check
  2314. // guarantees that the path all uses of SI (outside SI's parent) are on
  2315. // is disjoint from all other paths out of SI. But that information
  2316. // is more expensive to compute, and the trade-off here is in favor
  2317. // of compile-time.
  2318. if (Succ->getUniquePredecessor() && dominatesAllUses(SI, Icmp, Succ)) {
  2319. NumSel++;
  2320. SI->replaceUsesOutsideBlock(SI->getOperand(SIOpd), SI->getParent());
  2321. return true;
  2322. }
  2323. }
  2324. return false;
  2325. }
  2326. Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
  2327. bool Changed = false;
  2328. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  2329. unsigned Op0Cplxity = getComplexity(Op0);
  2330. unsigned Op1Cplxity = getComplexity(Op1);
  2331. /// Orders the operands of the compare so that they are listed from most
  2332. /// complex to least complex. This puts constants before unary operators,
  2333. /// before binary operators.
  2334. if (Op0Cplxity < Op1Cplxity ||
  2335. (Op0Cplxity == Op1Cplxity &&
  2336. swapMayExposeCSEOpportunities(Op0, Op1))) {
  2337. I.swapOperands();
  2338. std::swap(Op0, Op1);
  2339. Changed = true;
  2340. }
  2341. if (Value *V =
  2342. SimplifyICmpInst(I.getPredicate(), Op0, Op1, DL, TLI, DT, AC, &I))
  2343. return ReplaceInstUsesWith(I, V);
  2344. // comparing -val or val with non-zero is the same as just comparing val
  2345. // ie, abs(val) != 0 -> val != 0
  2346. if (I.getPredicate() == ICmpInst::ICMP_NE && match(Op1, m_Zero()))
  2347. {
  2348. Value *Cond, *SelectTrue, *SelectFalse;
  2349. if (match(Op0, m_Select(m_Value(Cond), m_Value(SelectTrue),
  2350. m_Value(SelectFalse)))) {
  2351. if (Value *V = dyn_castNegVal(SelectTrue)) {
  2352. if (V == SelectFalse)
  2353. return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
  2354. }
  2355. else if (Value *V = dyn_castNegVal(SelectFalse)) {
  2356. if (V == SelectTrue)
  2357. return CmpInst::Create(Instruction::ICmp, I.getPredicate(), V, Op1);
  2358. }
  2359. }
  2360. }
  2361. Type *Ty = Op0->getType();
  2362. // icmp's with boolean values can always be turned into bitwise operations
  2363. if (Ty->isIntegerTy(1)) {
  2364. switch (I.getPredicate()) {
  2365. default: llvm_unreachable("Invalid icmp instruction!");
  2366. case ICmpInst::ICMP_EQ: { // icmp eq i1 A, B -> ~(A^B)
  2367. Value *Xor = Builder->CreateXor(Op0, Op1, I.getName()+"tmp");
  2368. return BinaryOperator::CreateNot(Xor);
  2369. }
  2370. case ICmpInst::ICMP_NE: // icmp eq i1 A, B -> A^B
  2371. return BinaryOperator::CreateXor(Op0, Op1);
  2372. case ICmpInst::ICMP_UGT:
  2373. std::swap(Op0, Op1); // Change icmp ugt -> icmp ult
  2374. // FALL THROUGH
  2375. case ICmpInst::ICMP_ULT:{ // icmp ult i1 A, B -> ~A & B
  2376. Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
  2377. return BinaryOperator::CreateAnd(Not, Op1);
  2378. }
  2379. case ICmpInst::ICMP_SGT:
  2380. std::swap(Op0, Op1); // Change icmp sgt -> icmp slt
  2381. // FALL THROUGH
  2382. case ICmpInst::ICMP_SLT: { // icmp slt i1 A, B -> A & ~B
  2383. Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
  2384. return BinaryOperator::CreateAnd(Not, Op0);
  2385. }
  2386. case ICmpInst::ICMP_UGE:
  2387. std::swap(Op0, Op1); // Change icmp uge -> icmp ule
  2388. // FALL THROUGH
  2389. case ICmpInst::ICMP_ULE: { // icmp ule i1 A, B -> ~A | B
  2390. Value *Not = Builder->CreateNot(Op0, I.getName()+"tmp");
  2391. return BinaryOperator::CreateOr(Not, Op1);
  2392. }
  2393. case ICmpInst::ICMP_SGE:
  2394. std::swap(Op0, Op1); // Change icmp sge -> icmp sle
  2395. // FALL THROUGH
  2396. case ICmpInst::ICMP_SLE: { // icmp sle i1 A, B -> A | ~B
  2397. Value *Not = Builder->CreateNot(Op1, I.getName()+"tmp");
  2398. return BinaryOperator::CreateOr(Not, Op0);
  2399. }
  2400. }
  2401. }
  2402. unsigned BitWidth = 0;
  2403. if (Ty->isIntOrIntVectorTy())
  2404. BitWidth = Ty->getScalarSizeInBits();
  2405. else // Get pointer size.
  2406. BitWidth = DL.getTypeSizeInBits(Ty->getScalarType());
  2407. bool isSignBit = false;
  2408. // See if we are doing a comparison with a constant.
  2409. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  2410. Value *A = nullptr, *B = nullptr;
  2411. // Match the following pattern, which is a common idiom when writing
  2412. // overflow-safe integer arithmetic function. The source performs an
  2413. // addition in wider type, and explicitly checks for overflow using
  2414. // comparisons against INT_MIN and INT_MAX. Simplify this by using the
  2415. // sadd_with_overflow intrinsic.
  2416. //
  2417. // TODO: This could probably be generalized to handle other overflow-safe
  2418. // operations if we worked out the formulas to compute the appropriate
  2419. // magic constants.
  2420. //
  2421. // sum = a + b
  2422. // if (sum+128 >u 255) ... -> llvm.sadd.with.overflow.i8
  2423. {
  2424. ConstantInt *CI2; // I = icmp ugt (add (add A, B), CI2), CI
  2425. if (I.getPredicate() == ICmpInst::ICMP_UGT &&
  2426. match(Op0, m_Add(m_Add(m_Value(A), m_Value(B)), m_ConstantInt(CI2))))
  2427. if (Instruction *Res = ProcessUGT_ADDCST_ADD(I, A, B, CI2, CI, *this))
  2428. return Res;
  2429. }
  2430. // The following transforms are only 'worth it' if the only user of the
  2431. // subtraction is the icmp.
  2432. if (Op0->hasOneUse()) {
  2433. // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B)
  2434. if (I.isEquality() && CI->isZero() &&
  2435. match(Op0, m_Sub(m_Value(A), m_Value(B))))
  2436. return new ICmpInst(I.getPredicate(), A, B);
  2437. // (icmp sgt (sub nsw A B), -1) -> (icmp sge A, B)
  2438. if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isAllOnesValue() &&
  2439. match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
  2440. return new ICmpInst(ICmpInst::ICMP_SGE, A, B);
  2441. // (icmp sgt (sub nsw A B), 0) -> (icmp sgt A, B)
  2442. if (I.getPredicate() == ICmpInst::ICMP_SGT && CI->isZero() &&
  2443. match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
  2444. return new ICmpInst(ICmpInst::ICMP_SGT, A, B);
  2445. // (icmp slt (sub nsw A B), 0) -> (icmp slt A, B)
  2446. if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isZero() &&
  2447. match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
  2448. return new ICmpInst(ICmpInst::ICMP_SLT, A, B);
  2449. // (icmp slt (sub nsw A B), 1) -> (icmp sle A, B)
  2450. if (I.getPredicate() == ICmpInst::ICMP_SLT && CI->isOne() &&
  2451. match(Op0, m_NSWSub(m_Value(A), m_Value(B))))
  2452. return new ICmpInst(ICmpInst::ICMP_SLE, A, B);
  2453. }
  2454. // If we have an icmp le or icmp ge instruction, turn it into the
  2455. // appropriate icmp lt or icmp gt instruction. This allows us to rely on
  2456. // them being folded in the code below. The SimplifyICmpInst code has
  2457. // already handled the edge cases for us, so we just assert on them.
  2458. switch (I.getPredicate()) {
  2459. default: break;
  2460. case ICmpInst::ICMP_ULE:
  2461. assert(!CI->isMaxValue(false)); // A <=u MAX -> TRUE
  2462. return new ICmpInst(ICmpInst::ICMP_ULT, Op0,
  2463. Builder->getInt(CI->getValue()+1));
  2464. case ICmpInst::ICMP_SLE:
  2465. assert(!CI->isMaxValue(true)); // A <=s MAX -> TRUE
  2466. return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
  2467. Builder->getInt(CI->getValue()+1));
  2468. case ICmpInst::ICMP_UGE:
  2469. assert(!CI->isMinValue(false)); // A >=u MIN -> TRUE
  2470. return new ICmpInst(ICmpInst::ICMP_UGT, Op0,
  2471. Builder->getInt(CI->getValue()-1));
  2472. case ICmpInst::ICMP_SGE:
  2473. assert(!CI->isMinValue(true)); // A >=s MIN -> TRUE
  2474. return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
  2475. Builder->getInt(CI->getValue()-1));
  2476. }
  2477. if (I.isEquality()) {
  2478. ConstantInt *CI2;
  2479. if (match(Op0, m_AShr(m_ConstantInt(CI2), m_Value(A))) ||
  2480. match(Op0, m_LShr(m_ConstantInt(CI2), m_Value(A)))) {
  2481. // (icmp eq/ne (ashr/lshr const2, A), const1)
  2482. if (Instruction *Inst = FoldICmpCstShrCst(I, Op0, A, CI, CI2))
  2483. return Inst;
  2484. }
  2485. if (match(Op0, m_Shl(m_ConstantInt(CI2), m_Value(A)))) {
  2486. // (icmp eq/ne (shl const2, A), const1)
  2487. if (Instruction *Inst = FoldICmpCstShlCst(I, Op0, A, CI, CI2))
  2488. return Inst;
  2489. }
  2490. }
  2491. // If this comparison is a normal comparison, it demands all
  2492. // bits, if it is a sign bit comparison, it only demands the sign bit.
  2493. bool UnusedBit;
  2494. isSignBit = isSignBitCheck(I.getPredicate(), CI, UnusedBit);
  2495. }
  2496. // See if we can fold the comparison based on range information we can get
  2497. // by checking whether bits are known to be zero or one in the input.
  2498. if (BitWidth != 0) {
  2499. APInt Op0KnownZero(BitWidth, 0), Op0KnownOne(BitWidth, 0);
  2500. APInt Op1KnownZero(BitWidth, 0), Op1KnownOne(BitWidth, 0);
  2501. if (SimplifyDemandedBits(I.getOperandUse(0),
  2502. DemandedBitsLHSMask(I, BitWidth, isSignBit),
  2503. Op0KnownZero, Op0KnownOne, 0))
  2504. return &I;
  2505. if (SimplifyDemandedBits(I.getOperandUse(1),
  2506. APInt::getAllOnesValue(BitWidth), Op1KnownZero,
  2507. Op1KnownOne, 0))
  2508. return &I;
  2509. // Given the known and unknown bits, compute a range that the LHS could be
  2510. // in. Compute the Min, Max and RHS values based on the known bits. For the
  2511. // EQ and NE we use unsigned values.
  2512. APInt Op0Min(BitWidth, 0), Op0Max(BitWidth, 0);
  2513. APInt Op1Min(BitWidth, 0), Op1Max(BitWidth, 0);
  2514. if (I.isSigned()) {
  2515. ComputeSignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
  2516. Op0Min, Op0Max);
  2517. ComputeSignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
  2518. Op1Min, Op1Max);
  2519. } else {
  2520. ComputeUnsignedMinMaxValuesFromKnownBits(Op0KnownZero, Op0KnownOne,
  2521. Op0Min, Op0Max);
  2522. ComputeUnsignedMinMaxValuesFromKnownBits(Op1KnownZero, Op1KnownOne,
  2523. Op1Min, Op1Max);
  2524. }
  2525. // If Min and Max are known to be the same, then SimplifyDemandedBits
  2526. // figured out that the LHS is a constant. Just constant fold this now so
  2527. // that code below can assume that Min != Max.
  2528. if (!isa<Constant>(Op0) && Op0Min == Op0Max)
  2529. return new ICmpInst(I.getPredicate(),
  2530. ConstantInt::get(Op0->getType(), Op0Min), Op1);
  2531. if (!isa<Constant>(Op1) && Op1Min == Op1Max)
  2532. return new ICmpInst(I.getPredicate(), Op0,
  2533. ConstantInt::get(Op1->getType(), Op1Min));
  2534. // Based on the range information we know about the LHS, see if we can
  2535. // simplify this comparison. For example, (x&4) < 8 is always true.
  2536. switch (I.getPredicate()) {
  2537. default: llvm_unreachable("Unknown icmp opcode!");
  2538. case ICmpInst::ICMP_EQ: {
  2539. if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
  2540. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2541. // If all bits are known zero except for one, then we know at most one
  2542. // bit is set. If the comparison is against zero, then this is a check
  2543. // to see if *that* bit is set.
  2544. APInt Op0KnownZeroInverted = ~Op0KnownZero;
  2545. if (~Op1KnownZero == 0) {
  2546. // If the LHS is an AND with the same constant, look through it.
  2547. Value *LHS = nullptr;
  2548. ConstantInt *LHSC = nullptr;
  2549. if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
  2550. LHSC->getValue() != Op0KnownZeroInverted)
  2551. LHS = Op0;
  2552. // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
  2553. // then turn "((1 << x)&8) == 0" into "x != 3".
  2554. // or turn "((1 << x)&7) == 0" into "x > 2".
  2555. Value *X = nullptr;
  2556. if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
  2557. APInt ValToCheck = Op0KnownZeroInverted;
  2558. if (ValToCheck.isPowerOf2()) {
  2559. unsigned CmpVal = ValToCheck.countTrailingZeros();
  2560. return new ICmpInst(ICmpInst::ICMP_NE, X,
  2561. ConstantInt::get(X->getType(), CmpVal));
  2562. } else if ((++ValToCheck).isPowerOf2()) {
  2563. unsigned CmpVal = ValToCheck.countTrailingZeros() - 1;
  2564. return new ICmpInst(ICmpInst::ICMP_UGT, X,
  2565. ConstantInt::get(X->getType(), CmpVal));
  2566. }
  2567. }
  2568. // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
  2569. // then turn "((8 >>u x)&1) == 0" into "x != 3".
  2570. const APInt *CI;
  2571. if (Op0KnownZeroInverted == 1 &&
  2572. match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
  2573. return new ICmpInst(ICmpInst::ICMP_NE, X,
  2574. ConstantInt::get(X->getType(),
  2575. CI->countTrailingZeros()));
  2576. }
  2577. break;
  2578. }
  2579. case ICmpInst::ICMP_NE: {
  2580. if (Op0Max.ult(Op1Min) || Op0Min.ugt(Op1Max))
  2581. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2582. // If all bits are known zero except for one, then we know at most one
  2583. // bit is set. If the comparison is against zero, then this is a check
  2584. // to see if *that* bit is set.
  2585. APInt Op0KnownZeroInverted = ~Op0KnownZero;
  2586. if (~Op1KnownZero == 0) {
  2587. // If the LHS is an AND with the same constant, look through it.
  2588. Value *LHS = nullptr;
  2589. ConstantInt *LHSC = nullptr;
  2590. if (!match(Op0, m_And(m_Value(LHS), m_ConstantInt(LHSC))) ||
  2591. LHSC->getValue() != Op0KnownZeroInverted)
  2592. LHS = Op0;
  2593. // If the LHS is 1 << x, and we know the result is a power of 2 like 8,
  2594. // then turn "((1 << x)&8) != 0" into "x == 3".
  2595. // or turn "((1 << x)&7) != 0" into "x < 3".
  2596. Value *X = nullptr;
  2597. if (match(LHS, m_Shl(m_One(), m_Value(X)))) {
  2598. APInt ValToCheck = Op0KnownZeroInverted;
  2599. if (ValToCheck.isPowerOf2()) {
  2600. unsigned CmpVal = ValToCheck.countTrailingZeros();
  2601. return new ICmpInst(ICmpInst::ICMP_EQ, X,
  2602. ConstantInt::get(X->getType(), CmpVal));
  2603. } else if ((++ValToCheck).isPowerOf2()) {
  2604. unsigned CmpVal = ValToCheck.countTrailingZeros();
  2605. return new ICmpInst(ICmpInst::ICMP_ULT, X,
  2606. ConstantInt::get(X->getType(), CmpVal));
  2607. }
  2608. }
  2609. // If the LHS is 8 >>u x, and we know the result is a power of 2 like 1,
  2610. // then turn "((8 >>u x)&1) != 0" into "x == 3".
  2611. const APInt *CI;
  2612. if (Op0KnownZeroInverted == 1 &&
  2613. match(LHS, m_LShr(m_Power2(CI), m_Value(X))))
  2614. return new ICmpInst(ICmpInst::ICMP_EQ, X,
  2615. ConstantInt::get(X->getType(),
  2616. CI->countTrailingZeros()));
  2617. }
  2618. break;
  2619. }
  2620. case ICmpInst::ICMP_ULT:
  2621. if (Op0Max.ult(Op1Min)) // A <u B -> true if max(A) < min(B)
  2622. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2623. if (Op0Min.uge(Op1Max)) // A <u B -> false if min(A) >= max(B)
  2624. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2625. if (Op1Min == Op0Max) // A <u B -> A != B if max(A) == min(B)
  2626. return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
  2627. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  2628. if (Op1Max == Op0Min+1) // A <u C -> A == C-1 if min(A)+1 == C
  2629. return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
  2630. Builder->getInt(CI->getValue()-1));
  2631. // (x <u 2147483648) -> (x >s -1) -> true if sign bit clear
  2632. if (CI->isMinValue(true))
  2633. return new ICmpInst(ICmpInst::ICMP_SGT, Op0,
  2634. Constant::getAllOnesValue(Op0->getType()));
  2635. }
  2636. break;
  2637. case ICmpInst::ICMP_UGT:
  2638. if (Op0Min.ugt(Op1Max)) // A >u B -> true if min(A) > max(B)
  2639. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2640. if (Op0Max.ule(Op1Min)) // A >u B -> false if max(A) <= max(B)
  2641. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2642. if (Op1Max == Op0Min) // A >u B -> A != B if min(A) == max(B)
  2643. return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
  2644. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  2645. if (Op1Min == Op0Max-1) // A >u C -> A == C+1 if max(a)-1 == C
  2646. return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
  2647. Builder->getInt(CI->getValue()+1));
  2648. // (x >u 2147483647) -> (x <s 0) -> true if sign bit set
  2649. if (CI->isMaxValue(true))
  2650. return new ICmpInst(ICmpInst::ICMP_SLT, Op0,
  2651. Constant::getNullValue(Op0->getType()));
  2652. }
  2653. break;
  2654. case ICmpInst::ICMP_SLT:
  2655. if (Op0Max.slt(Op1Min)) // A <s B -> true if max(A) < min(C)
  2656. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2657. if (Op0Min.sge(Op1Max)) // A <s B -> false if min(A) >= max(C)
  2658. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2659. if (Op1Min == Op0Max) // A <s B -> A != B if max(A) == min(B)
  2660. return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
  2661. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  2662. if (Op1Max == Op0Min+1) // A <s C -> A == C-1 if min(A)+1 == C
  2663. return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
  2664. Builder->getInt(CI->getValue()-1));
  2665. }
  2666. break;
  2667. case ICmpInst::ICMP_SGT:
  2668. if (Op0Min.sgt(Op1Max)) // A >s B -> true if min(A) > max(B)
  2669. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2670. if (Op0Max.sle(Op1Min)) // A >s B -> false if max(A) <= min(B)
  2671. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2672. if (Op1Max == Op0Min) // A >s B -> A != B if min(A) == max(B)
  2673. return new ICmpInst(ICmpInst::ICMP_NE, Op0, Op1);
  2674. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  2675. if (Op1Min == Op0Max-1) // A >s C -> A == C+1 if max(A)-1 == C
  2676. return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
  2677. Builder->getInt(CI->getValue()+1));
  2678. }
  2679. break;
  2680. case ICmpInst::ICMP_SGE:
  2681. assert(!isa<ConstantInt>(Op1) && "ICMP_SGE with ConstantInt not folded!");
  2682. if (Op0Min.sge(Op1Max)) // A >=s B -> true if min(A) >= max(B)
  2683. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2684. if (Op0Max.slt(Op1Min)) // A >=s B -> false if max(A) < min(B)
  2685. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2686. break;
  2687. case ICmpInst::ICMP_SLE:
  2688. assert(!isa<ConstantInt>(Op1) && "ICMP_SLE with ConstantInt not folded!");
  2689. if (Op0Max.sle(Op1Min)) // A <=s B -> true if max(A) <= min(B)
  2690. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2691. if (Op0Min.sgt(Op1Max)) // A <=s B -> false if min(A) > max(B)
  2692. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2693. break;
  2694. case ICmpInst::ICMP_UGE:
  2695. assert(!isa<ConstantInt>(Op1) && "ICMP_UGE with ConstantInt not folded!");
  2696. if (Op0Min.uge(Op1Max)) // A >=u B -> true if min(A) >= max(B)
  2697. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2698. if (Op0Max.ult(Op1Min)) // A >=u B -> false if max(A) < min(B)
  2699. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2700. break;
  2701. case ICmpInst::ICMP_ULE:
  2702. assert(!isa<ConstantInt>(Op1) && "ICMP_ULE with ConstantInt not folded!");
  2703. if (Op0Max.ule(Op1Min)) // A <=u B -> true if max(A) <= min(B)
  2704. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  2705. if (Op0Min.ugt(Op1Max)) // A <=u B -> false if min(A) > max(B)
  2706. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  2707. break;
  2708. }
  2709. // Turn a signed comparison into an unsigned one if both operands
  2710. // are known to have the same sign.
  2711. if (I.isSigned() &&
  2712. ((Op0KnownZero.isNegative() && Op1KnownZero.isNegative()) ||
  2713. (Op0KnownOne.isNegative() && Op1KnownOne.isNegative())))
  2714. return new ICmpInst(I.getUnsignedPredicate(), Op0, Op1);
  2715. }
  2716. // Test if the ICmpInst instruction is used exclusively by a select as
  2717. // part of a minimum or maximum operation. If so, refrain from doing
  2718. // any other folding. This helps out other analyses which understand
  2719. // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
  2720. // and CodeGen. And in this case, at least one of the comparison
  2721. // operands has at least one user besides the compare (the select),
  2722. // which would often largely negate the benefit of folding anyway.
  2723. if (I.hasOneUse())
  2724. if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
  2725. if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
  2726. (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
  2727. return nullptr;
  2728. // See if we are doing a comparison between a constant and an instruction that
  2729. // can be folded into the comparison.
  2730. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  2731. // Since the RHS is a ConstantInt (CI), if the left hand side is an
  2732. // instruction, see if that instruction also has constants so that the
  2733. // instruction can be folded into the icmp
  2734. if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
  2735. if (Instruction *Res = visitICmpInstWithInstAndIntCst(I, LHSI, CI))
  2736. return Res;
  2737. }
  2738. // Handle icmp with constant (but not simple integer constant) RHS
  2739. if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
  2740. if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
  2741. switch (LHSI->getOpcode()) {
  2742. case Instruction::GetElementPtr:
  2743. // icmp pred GEP (P, int 0, int 0, int 0), null -> icmp pred P, null
  2744. if (RHSC->isNullValue() &&
  2745. cast<GetElementPtrInst>(LHSI)->hasAllZeroIndices())
  2746. return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
  2747. Constant::getNullValue(LHSI->getOperand(0)->getType()));
  2748. break;
  2749. case Instruction::PHI:
  2750. // Only fold icmp into the PHI if the phi and icmp are in the same
  2751. // block. If in the same block, we're encouraging jump threading. If
  2752. // not, we are just pessimizing the code by making an i1 phi.
  2753. if (LHSI->getParent() == I.getParent())
  2754. if (Instruction *NV = FoldOpIntoPhi(I))
  2755. return NV;
  2756. break;
  2757. case Instruction::Select: {
  2758. // If either operand of the select is a constant, we can fold the
  2759. // comparison into the select arms, which will cause one to be
  2760. // constant folded and the select turned into a bitwise or.
  2761. Value *Op1 = nullptr, *Op2 = nullptr;
  2762. ConstantInt *CI = 0;
  2763. if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(1))) {
  2764. Op1 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
  2765. CI = dyn_cast<ConstantInt>(Op1);
  2766. }
  2767. if (Constant *C = dyn_cast<Constant>(LHSI->getOperand(2))) {
  2768. Op2 = ConstantExpr::getICmp(I.getPredicate(), C, RHSC);
  2769. CI = dyn_cast<ConstantInt>(Op2);
  2770. }
  2771. // We only want to perform this transformation if it will not lead to
  2772. // additional code. This is true if either both sides of the select
  2773. // fold to a constant (in which case the icmp is replaced with a select
  2774. // which will usually simplify) or this is the only user of the
  2775. // select (in which case we are trading a select+icmp for a simpler
  2776. // select+icmp) or all uses of the select can be replaced based on
  2777. // dominance information ("Global cases").
  2778. bool Transform = false;
  2779. if (Op1 && Op2)
  2780. Transform = true;
  2781. else if (Op1 || Op2) {
  2782. // Local case
  2783. if (LHSI->hasOneUse())
  2784. Transform = true;
  2785. // Global cases
  2786. else if (CI && !CI->isZero())
  2787. // When Op1 is constant try replacing select with second operand.
  2788. // Otherwise Op2 is constant and try replacing select with first
  2789. // operand.
  2790. Transform = replacedSelectWithOperand(cast<SelectInst>(LHSI), &I,
  2791. Op1 ? 2 : 1);
  2792. }
  2793. if (Transform) {
  2794. if (!Op1)
  2795. Op1 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(1),
  2796. RHSC, I.getName());
  2797. if (!Op2)
  2798. Op2 = Builder->CreateICmp(I.getPredicate(), LHSI->getOperand(2),
  2799. RHSC, I.getName());
  2800. return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
  2801. }
  2802. break;
  2803. }
  2804. case Instruction::IntToPtr:
  2805. // icmp pred inttoptr(X), null -> icmp pred X, 0
  2806. if (RHSC->isNullValue() &&
  2807. DL.getIntPtrType(RHSC->getType()) == LHSI->getOperand(0)->getType())
  2808. return new ICmpInst(I.getPredicate(), LHSI->getOperand(0),
  2809. Constant::getNullValue(LHSI->getOperand(0)->getType()));
  2810. break;
  2811. case Instruction::Load:
  2812. // Try to optimize things like "A[i] > 4" to index computations.
  2813. if (GetElementPtrInst *GEP =
  2814. dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
  2815. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
  2816. if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
  2817. !cast<LoadInst>(LHSI)->isVolatile())
  2818. if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
  2819. return Res;
  2820. }
  2821. break;
  2822. }
  2823. }
  2824. // If we can optimize a 'icmp GEP, P' or 'icmp P, GEP', do so now.
  2825. if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op0))
  2826. if (Instruction *NI = FoldGEPICmp(GEP, Op1, I.getPredicate(), I))
  2827. return NI;
  2828. if (GEPOperator *GEP = dyn_cast<GEPOperator>(Op1))
  2829. if (Instruction *NI = FoldGEPICmp(GEP, Op0,
  2830. ICmpInst::getSwappedPredicate(I.getPredicate()), I))
  2831. return NI;
  2832. // Test to see if the operands of the icmp are casted versions of other
  2833. // values. If the ptr->ptr cast can be stripped off both arguments, we do so
  2834. // now.
  2835. if (BitCastInst *CI = dyn_cast<BitCastInst>(Op0)) {
  2836. if (Op0->getType()->isPointerTy() &&
  2837. (isa<Constant>(Op1) || isa<BitCastInst>(Op1))) {
  2838. // We keep moving the cast from the left operand over to the right
  2839. // operand, where it can often be eliminated completely.
  2840. Op0 = CI->getOperand(0);
  2841. // If operand #1 is a bitcast instruction, it must also be a ptr->ptr cast
  2842. // so eliminate it as well.
  2843. if (BitCastInst *CI2 = dyn_cast<BitCastInst>(Op1))
  2844. Op1 = CI2->getOperand(0);
  2845. // If Op1 is a constant, we can fold the cast into the constant.
  2846. if (Op0->getType() != Op1->getType()) {
  2847. if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
  2848. Op1 = ConstantExpr::getBitCast(Op1C, Op0->getType());
  2849. } else {
  2850. // Otherwise, cast the RHS right before the icmp
  2851. Op1 = Builder->CreateBitCast(Op1, Op0->getType());
  2852. }
  2853. }
  2854. return new ICmpInst(I.getPredicate(), Op0, Op1);
  2855. }
  2856. }
  2857. if (isa<CastInst>(Op0)) {
  2858. // Handle the special case of: icmp (cast bool to X), <cst>
  2859. // This comes up when you have code like
  2860. // int X = A < B;
  2861. // if (X) ...
  2862. // For generality, we handle any zero-extension of any operand comparison
  2863. // with a constant or another cast from the same type.
  2864. if (isa<Constant>(Op1) || isa<CastInst>(Op1))
  2865. if (Instruction *R = visitICmpInstWithCastAndCast(I))
  2866. return R;
  2867. }
  2868. // Special logic for binary operators.
  2869. BinaryOperator *BO0 = dyn_cast<BinaryOperator>(Op0);
  2870. BinaryOperator *BO1 = dyn_cast<BinaryOperator>(Op1);
  2871. if (BO0 || BO1) {
  2872. CmpInst::Predicate Pred = I.getPredicate();
  2873. bool NoOp0WrapProblem = false, NoOp1WrapProblem = false;
  2874. if (BO0 && isa<OverflowingBinaryOperator>(BO0))
  2875. NoOp0WrapProblem = ICmpInst::isEquality(Pred) ||
  2876. (CmpInst::isUnsigned(Pred) && BO0->hasNoUnsignedWrap()) ||
  2877. (CmpInst::isSigned(Pred) && BO0->hasNoSignedWrap());
  2878. if (BO1 && isa<OverflowingBinaryOperator>(BO1))
  2879. NoOp1WrapProblem = ICmpInst::isEquality(Pred) ||
  2880. (CmpInst::isUnsigned(Pred) && BO1->hasNoUnsignedWrap()) ||
  2881. (CmpInst::isSigned(Pred) && BO1->hasNoSignedWrap());
  2882. // Analyze the case when either Op0 or Op1 is an add instruction.
  2883. // Op0 = A + B (or A and B are null); Op1 = C + D (or C and D are null).
  2884. Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
  2885. if (BO0 && BO0->getOpcode() == Instruction::Add)
  2886. A = BO0->getOperand(0), B = BO0->getOperand(1);
  2887. if (BO1 && BO1->getOpcode() == Instruction::Add)
  2888. C = BO1->getOperand(0), D = BO1->getOperand(1);
  2889. // icmp (X+cst) < 0 --> X < -cst
  2890. if (NoOp0WrapProblem && ICmpInst::isSigned(Pred) && match(Op1, m_Zero()))
  2891. if (ConstantInt *RHSC = dyn_cast_or_null<ConstantInt>(B))
  2892. if (!RHSC->isMinValue(/*isSigned=*/true))
  2893. return new ICmpInst(Pred, A, ConstantExpr::getNeg(RHSC));
  2894. // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
  2895. if ((A == Op1 || B == Op1) && NoOp0WrapProblem)
  2896. return new ICmpInst(Pred, A == Op1 ? B : A,
  2897. Constant::getNullValue(Op1->getType()));
  2898. // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
  2899. if ((C == Op0 || D == Op0) && NoOp1WrapProblem)
  2900. return new ICmpInst(Pred, Constant::getNullValue(Op0->getType()),
  2901. C == Op0 ? D : C);
  2902. // icmp (X+Y), (X+Z) -> icmp Y, Z for equalities or if there is no overflow.
  2903. if (A && C && (A == C || A == D || B == C || B == D) &&
  2904. NoOp0WrapProblem && NoOp1WrapProblem &&
  2905. // Try not to increase register pressure.
  2906. BO0->hasOneUse() && BO1->hasOneUse()) {
  2907. // Determine Y and Z in the form icmp (X+Y), (X+Z).
  2908. Value *Y, *Z;
  2909. if (A == C) {
  2910. // C + B == C + D -> B == D
  2911. Y = B;
  2912. Z = D;
  2913. } else if (A == D) {
  2914. // D + B == C + D -> B == C
  2915. Y = B;
  2916. Z = C;
  2917. } else if (B == C) {
  2918. // A + C == C + D -> A == D
  2919. Y = A;
  2920. Z = D;
  2921. } else {
  2922. assert(B == D);
  2923. // A + D == C + D -> A == C
  2924. Y = A;
  2925. Z = C;
  2926. }
  2927. return new ICmpInst(Pred, Y, Z);
  2928. }
  2929. // icmp slt (X + -1), Y -> icmp sle X, Y
  2930. if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLT &&
  2931. match(B, m_AllOnes()))
  2932. return new ICmpInst(CmpInst::ICMP_SLE, A, Op1);
  2933. // icmp sge (X + -1), Y -> icmp sgt X, Y
  2934. if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGE &&
  2935. match(B, m_AllOnes()))
  2936. return new ICmpInst(CmpInst::ICMP_SGT, A, Op1);
  2937. // icmp sle (X + 1), Y -> icmp slt X, Y
  2938. if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SLE &&
  2939. match(B, m_One()))
  2940. return new ICmpInst(CmpInst::ICMP_SLT, A, Op1);
  2941. // icmp sgt (X + 1), Y -> icmp sge X, Y
  2942. if (A && NoOp0WrapProblem && Pred == CmpInst::ICMP_SGT &&
  2943. match(B, m_One()))
  2944. return new ICmpInst(CmpInst::ICMP_SGE, A, Op1);
  2945. // if C1 has greater magnitude than C2:
  2946. // icmp (X + C1), (Y + C2) -> icmp (X + C3), Y
  2947. // s.t. C3 = C1 - C2
  2948. //
  2949. // if C2 has greater magnitude than C1:
  2950. // icmp (X + C1), (Y + C2) -> icmp X, (Y + C3)
  2951. // s.t. C3 = C2 - C1
  2952. if (A && C && NoOp0WrapProblem && NoOp1WrapProblem &&
  2953. (BO0->hasOneUse() || BO1->hasOneUse()) && !I.isUnsigned())
  2954. if (ConstantInt *C1 = dyn_cast<ConstantInt>(B))
  2955. if (ConstantInt *C2 = dyn_cast<ConstantInt>(D)) {
  2956. const APInt &AP1 = C1->getValue();
  2957. const APInt &AP2 = C2->getValue();
  2958. if (AP1.isNegative() == AP2.isNegative()) {
  2959. APInt AP1Abs = C1->getValue().abs();
  2960. APInt AP2Abs = C2->getValue().abs();
  2961. if (AP1Abs.uge(AP2Abs)) {
  2962. ConstantInt *C3 = Builder->getInt(AP1 - AP2);
  2963. Value *NewAdd = Builder->CreateNSWAdd(A, C3);
  2964. return new ICmpInst(Pred, NewAdd, C);
  2965. } else {
  2966. ConstantInt *C3 = Builder->getInt(AP2 - AP1);
  2967. Value *NewAdd = Builder->CreateNSWAdd(C, C3);
  2968. return new ICmpInst(Pred, A, NewAdd);
  2969. }
  2970. }
  2971. }
  2972. // Analyze the case when either Op0 or Op1 is a sub instruction.
  2973. // Op0 = A - B (or A and B are null); Op1 = C - D (or C and D are null).
  2974. A = nullptr; B = nullptr; C = nullptr; D = nullptr;
  2975. if (BO0 && BO0->getOpcode() == Instruction::Sub)
  2976. A = BO0->getOperand(0), B = BO0->getOperand(1);
  2977. if (BO1 && BO1->getOpcode() == Instruction::Sub)
  2978. C = BO1->getOperand(0), D = BO1->getOperand(1);
  2979. // icmp (X-Y), X -> icmp 0, Y for equalities or if there is no overflow.
  2980. if (A == Op1 && NoOp0WrapProblem)
  2981. return new ICmpInst(Pred, Constant::getNullValue(Op1->getType()), B);
  2982. // icmp X, (X-Y) -> icmp Y, 0 for equalities or if there is no overflow.
  2983. if (C == Op0 && NoOp1WrapProblem)
  2984. return new ICmpInst(Pred, D, Constant::getNullValue(Op0->getType()));
  2985. // icmp (Y-X), (Z-X) -> icmp Y, Z for equalities or if there is no overflow.
  2986. if (B && D && B == D && NoOp0WrapProblem && NoOp1WrapProblem &&
  2987. // Try not to increase register pressure.
  2988. BO0->hasOneUse() && BO1->hasOneUse())
  2989. return new ICmpInst(Pred, A, C);
  2990. // icmp (X-Y), (X-Z) -> icmp Z, Y for equalities or if there is no overflow.
  2991. if (A && C && A == C && NoOp0WrapProblem && NoOp1WrapProblem &&
  2992. // Try not to increase register pressure.
  2993. BO0->hasOneUse() && BO1->hasOneUse())
  2994. return new ICmpInst(Pred, D, B);
  2995. // icmp (0-X) < cst --> x > -cst
  2996. if (NoOp0WrapProblem && ICmpInst::isSigned(Pred)) {
  2997. Value *X;
  2998. if (match(BO0, m_Neg(m_Value(X))))
  2999. if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
  3000. if (!RHSC->isMinValue(/*isSigned=*/true))
  3001. return new ICmpInst(I.getSwappedPredicate(), X,
  3002. ConstantExpr::getNeg(RHSC));
  3003. }
  3004. BinaryOperator *SRem = nullptr;
  3005. // icmp (srem X, Y), Y
  3006. if (BO0 && BO0->getOpcode() == Instruction::SRem &&
  3007. Op1 == BO0->getOperand(1))
  3008. SRem = BO0;
  3009. // icmp Y, (srem X, Y)
  3010. else if (BO1 && BO1->getOpcode() == Instruction::SRem &&
  3011. Op0 == BO1->getOperand(1))
  3012. SRem = BO1;
  3013. if (SRem) {
  3014. // We don't check hasOneUse to avoid increasing register pressure because
  3015. // the value we use is the same value this instruction was already using.
  3016. switch (SRem == BO0 ? ICmpInst::getSwappedPredicate(Pred) : Pred) {
  3017. default: break;
  3018. case ICmpInst::ICMP_EQ:
  3019. return ReplaceInstUsesWith(I, ConstantInt::getFalse(I.getType()));
  3020. case ICmpInst::ICMP_NE:
  3021. return ReplaceInstUsesWith(I, ConstantInt::getTrue(I.getType()));
  3022. case ICmpInst::ICMP_SGT:
  3023. case ICmpInst::ICMP_SGE:
  3024. return new ICmpInst(ICmpInst::ICMP_SGT, SRem->getOperand(1),
  3025. Constant::getAllOnesValue(SRem->getType()));
  3026. case ICmpInst::ICMP_SLT:
  3027. case ICmpInst::ICMP_SLE:
  3028. return new ICmpInst(ICmpInst::ICMP_SLT, SRem->getOperand(1),
  3029. Constant::getNullValue(SRem->getType()));
  3030. }
  3031. }
  3032. if (BO0 && BO1 && BO0->getOpcode() == BO1->getOpcode() &&
  3033. BO0->hasOneUse() && BO1->hasOneUse() &&
  3034. BO0->getOperand(1) == BO1->getOperand(1)) {
  3035. switch (BO0->getOpcode()) {
  3036. default: break;
  3037. case Instruction::Add:
  3038. case Instruction::Sub:
  3039. case Instruction::Xor:
  3040. if (I.isEquality()) // a+x icmp eq/ne b+x --> a icmp b
  3041. return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
  3042. BO1->getOperand(0));
  3043. // icmp u/s (a ^ signbit), (b ^ signbit) --> icmp s/u a, b
  3044. if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
  3045. if (CI->getValue().isSignBit()) {
  3046. ICmpInst::Predicate Pred = I.isSigned()
  3047. ? I.getUnsignedPredicate()
  3048. : I.getSignedPredicate();
  3049. return new ICmpInst(Pred, BO0->getOperand(0),
  3050. BO1->getOperand(0));
  3051. }
  3052. if (CI->isMaxValue(true)) {
  3053. ICmpInst::Predicate Pred = I.isSigned()
  3054. ? I.getUnsignedPredicate()
  3055. : I.getSignedPredicate();
  3056. Pred = I.getSwappedPredicate(Pred);
  3057. return new ICmpInst(Pred, BO0->getOperand(0),
  3058. BO1->getOperand(0));
  3059. }
  3060. }
  3061. break;
  3062. case Instruction::Mul:
  3063. if (!I.isEquality())
  3064. break;
  3065. if (ConstantInt *CI = dyn_cast<ConstantInt>(BO0->getOperand(1))) {
  3066. // a * Cst icmp eq/ne b * Cst --> a & Mask icmp b & Mask
  3067. // Mask = -1 >> count-trailing-zeros(Cst).
  3068. if (!CI->isZero() && !CI->isOne()) {
  3069. const APInt &AP = CI->getValue();
  3070. ConstantInt *Mask = ConstantInt::get(I.getContext(),
  3071. APInt::getLowBitsSet(AP.getBitWidth(),
  3072. AP.getBitWidth() -
  3073. AP.countTrailingZeros()));
  3074. Value *And1 = Builder->CreateAnd(BO0->getOperand(0), Mask);
  3075. Value *And2 = Builder->CreateAnd(BO1->getOperand(0), Mask);
  3076. return new ICmpInst(I.getPredicate(), And1, And2);
  3077. }
  3078. }
  3079. break;
  3080. case Instruction::UDiv:
  3081. case Instruction::LShr:
  3082. if (I.isSigned())
  3083. break;
  3084. // fall-through
  3085. case Instruction::SDiv:
  3086. case Instruction::AShr:
  3087. if (!BO0->isExact() || !BO1->isExact())
  3088. break;
  3089. return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
  3090. BO1->getOperand(0));
  3091. case Instruction::Shl: {
  3092. bool NUW = BO0->hasNoUnsignedWrap() && BO1->hasNoUnsignedWrap();
  3093. bool NSW = BO0->hasNoSignedWrap() && BO1->hasNoSignedWrap();
  3094. if (!NUW && !NSW)
  3095. break;
  3096. if (!NSW && I.isSigned())
  3097. break;
  3098. return new ICmpInst(I.getPredicate(), BO0->getOperand(0),
  3099. BO1->getOperand(0));
  3100. }
  3101. }
  3102. }
  3103. }
  3104. { Value *A, *B;
  3105. // Transform (A & ~B) == 0 --> (A & B) != 0
  3106. // and (A & ~B) != 0 --> (A & B) == 0
  3107. // if A is a power of 2.
  3108. if (match(Op0, m_And(m_Value(A), m_Not(m_Value(B)))) &&
  3109. match(Op1, m_Zero()) &&
  3110. isKnownToBeAPowerOfTwo(A, DL, false, 0, AC, &I, DT) && I.isEquality())
  3111. return new ICmpInst(I.getInversePredicate(),
  3112. Builder->CreateAnd(A, B),
  3113. Op1);
  3114. // ~x < ~y --> y < x
  3115. // ~x < cst --> ~cst < x
  3116. if (match(Op0, m_Not(m_Value(A)))) {
  3117. if (match(Op1, m_Not(m_Value(B))))
  3118. return new ICmpInst(I.getPredicate(), B, A);
  3119. if (ConstantInt *RHSC = dyn_cast<ConstantInt>(Op1))
  3120. return new ICmpInst(I.getPredicate(), ConstantExpr::getNot(RHSC), A);
  3121. }
  3122. Instruction *AddI = nullptr;
  3123. if (match(&I, m_UAddWithOverflow(m_Value(A), m_Value(B),
  3124. m_Instruction(AddI))) &&
  3125. isa<IntegerType>(A->getType())) {
  3126. Value *Result;
  3127. Constant *Overflow;
  3128. if (OptimizeOverflowCheck(OCF_UNSIGNED_ADD, A, B, *AddI, Result,
  3129. Overflow)) {
  3130. ReplaceInstUsesWith(*AddI, Result);
  3131. return ReplaceInstUsesWith(I, Overflow);
  3132. }
  3133. }
  3134. // (zext a) * (zext b) --> llvm.umul.with.overflow.
  3135. if (match(Op0, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
  3136. if (Instruction *R = ProcessUMulZExtIdiom(I, Op0, Op1, *this))
  3137. return R;
  3138. }
  3139. if (match(Op1, m_Mul(m_ZExt(m_Value(A)), m_ZExt(m_Value(B))))) {
  3140. if (Instruction *R = ProcessUMulZExtIdiom(I, Op1, Op0, *this))
  3141. return R;
  3142. }
  3143. }
  3144. if (I.isEquality()) {
  3145. Value *A, *B, *C, *D;
  3146. if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) {
  3147. if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0
  3148. Value *OtherVal = A == Op1 ? B : A;
  3149. return new ICmpInst(I.getPredicate(), OtherVal,
  3150. Constant::getNullValue(A->getType()));
  3151. }
  3152. if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) {
  3153. // A^c1 == C^c2 --> A == C^(c1^c2)
  3154. ConstantInt *C1, *C2;
  3155. if (match(B, m_ConstantInt(C1)) &&
  3156. match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) {
  3157. Constant *NC = Builder->getInt(C1->getValue() ^ C2->getValue());
  3158. Value *Xor = Builder->CreateXor(C, NC);
  3159. return new ICmpInst(I.getPredicate(), A, Xor);
  3160. }
  3161. // A^B == A^D -> B == D
  3162. if (A == C) return new ICmpInst(I.getPredicate(), B, D);
  3163. if (A == D) return new ICmpInst(I.getPredicate(), B, C);
  3164. if (B == C) return new ICmpInst(I.getPredicate(), A, D);
  3165. if (B == D) return new ICmpInst(I.getPredicate(), A, C);
  3166. }
  3167. }
  3168. if (match(Op1, m_Xor(m_Value(A), m_Value(B))) &&
  3169. (A == Op0 || B == Op0)) {
  3170. // A == (A^B) -> B == 0
  3171. Value *OtherVal = A == Op0 ? B : A;
  3172. return new ICmpInst(I.getPredicate(), OtherVal,
  3173. Constant::getNullValue(A->getType()));
  3174. }
  3175. // (X&Z) == (Y&Z) -> (X^Y) & Z == 0
  3176. if (match(Op0, m_OneUse(m_And(m_Value(A), m_Value(B)))) &&
  3177. match(Op1, m_OneUse(m_And(m_Value(C), m_Value(D))))) {
  3178. Value *X = nullptr, *Y = nullptr, *Z = nullptr;
  3179. if (A == C) {
  3180. X = B; Y = D; Z = A;
  3181. } else if (A == D) {
  3182. X = B; Y = C; Z = A;
  3183. } else if (B == C) {
  3184. X = A; Y = D; Z = B;
  3185. } else if (B == D) {
  3186. X = A; Y = C; Z = B;
  3187. }
  3188. if (X) { // Build (X^Y) & Z
  3189. Op1 = Builder->CreateXor(X, Y);
  3190. Op1 = Builder->CreateAnd(Op1, Z);
  3191. I.setOperand(0, Op1);
  3192. I.setOperand(1, Constant::getNullValue(Op1->getType()));
  3193. return &I;
  3194. }
  3195. }
  3196. // Transform (zext A) == (B & (1<<X)-1) --> A == (trunc B)
  3197. // and (B & (1<<X)-1) == (zext A) --> A == (trunc B)
  3198. ConstantInt *Cst1;
  3199. if ((Op0->hasOneUse() &&
  3200. match(Op0, m_ZExt(m_Value(A))) &&
  3201. match(Op1, m_And(m_Value(B), m_ConstantInt(Cst1)))) ||
  3202. (Op1->hasOneUse() &&
  3203. match(Op0, m_And(m_Value(B), m_ConstantInt(Cst1))) &&
  3204. match(Op1, m_ZExt(m_Value(A))))) {
  3205. APInt Pow2 = Cst1->getValue() + 1;
  3206. if (Pow2.isPowerOf2() && isa<IntegerType>(A->getType()) &&
  3207. Pow2.logBase2() == cast<IntegerType>(A->getType())->getBitWidth())
  3208. return new ICmpInst(I.getPredicate(), A,
  3209. Builder->CreateTrunc(B, A->getType()));
  3210. }
  3211. // (A >> C) == (B >> C) --> (A^B) u< (1 << C)
  3212. // For lshr and ashr pairs.
  3213. if ((match(Op0, m_OneUse(m_LShr(m_Value(A), m_ConstantInt(Cst1)))) &&
  3214. match(Op1, m_OneUse(m_LShr(m_Value(B), m_Specific(Cst1))))) ||
  3215. (match(Op0, m_OneUse(m_AShr(m_Value(A), m_ConstantInt(Cst1)))) &&
  3216. match(Op1, m_OneUse(m_AShr(m_Value(B), m_Specific(Cst1)))))) {
  3217. unsigned TypeBits = Cst1->getBitWidth();
  3218. unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
  3219. if (ShAmt < TypeBits && ShAmt != 0) {
  3220. ICmpInst::Predicate Pred = I.getPredicate() == ICmpInst::ICMP_NE
  3221. ? ICmpInst::ICMP_UGE
  3222. : ICmpInst::ICMP_ULT;
  3223. Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
  3224. APInt CmpVal = APInt::getOneBitSet(TypeBits, ShAmt);
  3225. return new ICmpInst(Pred, Xor, Builder->getInt(CmpVal));
  3226. }
  3227. }
  3228. // (A << C) == (B << C) --> ((A^B) & (~0U >> C)) == 0
  3229. if (match(Op0, m_OneUse(m_Shl(m_Value(A), m_ConstantInt(Cst1)))) &&
  3230. match(Op1, m_OneUse(m_Shl(m_Value(B), m_Specific(Cst1))))) {
  3231. unsigned TypeBits = Cst1->getBitWidth();
  3232. unsigned ShAmt = (unsigned)Cst1->getLimitedValue(TypeBits);
  3233. if (ShAmt < TypeBits && ShAmt != 0) {
  3234. Value *Xor = Builder->CreateXor(A, B, I.getName() + ".unshifted");
  3235. APInt AndVal = APInt::getLowBitsSet(TypeBits, TypeBits - ShAmt);
  3236. Value *And = Builder->CreateAnd(Xor, Builder->getInt(AndVal),
  3237. I.getName() + ".mask");
  3238. return new ICmpInst(I.getPredicate(), And,
  3239. Constant::getNullValue(Cst1->getType()));
  3240. }
  3241. }
  3242. // Transform "icmp eq (trunc (lshr(X, cst1)), cst" to
  3243. // "icmp (and X, mask), cst"
  3244. uint64_t ShAmt = 0;
  3245. if (Op0->hasOneUse() &&
  3246. match(Op0, m_Trunc(m_OneUse(m_LShr(m_Value(A),
  3247. m_ConstantInt(ShAmt))))) &&
  3248. match(Op1, m_ConstantInt(Cst1)) &&
  3249. // Only do this when A has multiple uses. This is most important to do
  3250. // when it exposes other optimizations.
  3251. !A->hasOneUse()) {
  3252. unsigned ASize =cast<IntegerType>(A->getType())->getPrimitiveSizeInBits();
  3253. if (ShAmt < ASize) {
  3254. APInt MaskV =
  3255. APInt::getLowBitsSet(ASize, Op0->getType()->getPrimitiveSizeInBits());
  3256. MaskV <<= ShAmt;
  3257. APInt CmpV = Cst1->getValue().zext(ASize);
  3258. CmpV <<= ShAmt;
  3259. Value *Mask = Builder->CreateAnd(A, Builder->getInt(MaskV));
  3260. return new ICmpInst(I.getPredicate(), Mask, Builder->getInt(CmpV));
  3261. }
  3262. }
  3263. }
  3264. // The 'cmpxchg' instruction returns an aggregate containing the old value and
  3265. // an i1 which indicates whether or not we successfully did the swap.
  3266. //
  3267. // Replace comparisons between the old value and the expected value with the
  3268. // indicator that 'cmpxchg' returns.
  3269. //
  3270. // N.B. This transform is only valid when the 'cmpxchg' is not permitted to
  3271. // spuriously fail. In those cases, the old value may equal the expected
  3272. // value but it is possible for the swap to not occur.
  3273. if (I.getPredicate() == ICmpInst::ICMP_EQ)
  3274. if (auto *EVI = dyn_cast<ExtractValueInst>(Op0))
  3275. if (auto *ACXI = dyn_cast<AtomicCmpXchgInst>(EVI->getAggregateOperand()))
  3276. if (EVI->getIndices()[0] == 0 && ACXI->getCompareOperand() == Op1 &&
  3277. !ACXI->isWeak())
  3278. return ExtractValueInst::Create(ACXI, 1);
  3279. {
  3280. Value *X; ConstantInt *Cst;
  3281. // icmp X+Cst, X
  3282. if (match(Op0, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op1 == X)
  3283. return FoldICmpAddOpCst(I, X, Cst, I.getPredicate());
  3284. // icmp X, X+Cst
  3285. if (match(Op1, m_Add(m_Value(X), m_ConstantInt(Cst))) && Op0 == X)
  3286. return FoldICmpAddOpCst(I, X, Cst, I.getSwappedPredicate());
  3287. }
  3288. return Changed ? &I : nullptr;
  3289. }
  3290. /// FoldFCmp_IntToFP_Cst - Fold fcmp ([us]itofp x, cst) if possible.
  3291. Instruction *InstCombiner::FoldFCmp_IntToFP_Cst(FCmpInst &I,
  3292. Instruction *LHSI,
  3293. Constant *RHSC) {
  3294. if (!isa<ConstantFP>(RHSC)) return nullptr;
  3295. const APFloat &RHS = cast<ConstantFP>(RHSC)->getValueAPF();
  3296. // Get the width of the mantissa. We don't want to hack on conversions that
  3297. // might lose information from the integer, e.g. "i64 -> float"
  3298. int MantissaWidth = LHSI->getType()->getFPMantissaWidth();
  3299. if (MantissaWidth == -1) return nullptr; // Unknown.
  3300. IntegerType *IntTy = cast<IntegerType>(LHSI->getOperand(0)->getType());
  3301. // Check to see that the input is converted from an integer type that is small
  3302. // enough that preserves all bits. TODO: check here for "known" sign bits.
  3303. // This would allow us to handle (fptosi (x >>s 62) to float) if x is i64 f.e.
  3304. unsigned InputSize = IntTy->getScalarSizeInBits();
  3305. // If this is a uitofp instruction, we need an extra bit to hold the sign.
  3306. bool LHSUnsigned = isa<UIToFPInst>(LHSI);
  3307. if (LHSUnsigned)
  3308. ++InputSize;
  3309. if (I.isEquality()) {
  3310. FCmpInst::Predicate P = I.getPredicate();
  3311. bool IsExact = false;
  3312. APSInt RHSCvt(IntTy->getBitWidth(), LHSUnsigned);
  3313. RHS.convertToInteger(RHSCvt, APFloat::rmNearestTiesToEven, &IsExact);
  3314. // If the floating point constant isn't an integer value, we know if we will
  3315. // ever compare equal / not equal to it.
  3316. if (!IsExact) {
  3317. // TODO: Can never be -0.0 and other non-representable values
  3318. APFloat RHSRoundInt(RHS);
  3319. RHSRoundInt.roundToIntegral(APFloat::rmNearestTiesToEven);
  3320. if (RHS.compare(RHSRoundInt) != APFloat::cmpEqual) {
  3321. if (P == FCmpInst::FCMP_OEQ || P == FCmpInst::FCMP_UEQ)
  3322. return ReplaceInstUsesWith(I, Builder->getFalse());
  3323. assert(P == FCmpInst::FCMP_ONE || P == FCmpInst::FCMP_UNE);
  3324. return ReplaceInstUsesWith(I, Builder->getTrue());
  3325. }
  3326. }
  3327. // TODO: If the constant is exactly representable, is it always OK to do
  3328. // equality compares as integer?
  3329. }
  3330. // Comparisons with zero are a special case where we know we won't lose
  3331. // information.
  3332. bool IsCmpZero = RHS.isPosZero();
  3333. // If the conversion would lose info, don't hack on this.
  3334. if ((int)InputSize > MantissaWidth && !IsCmpZero)
  3335. return nullptr;
  3336. // Otherwise, we can potentially simplify the comparison. We know that it
  3337. // will always come through as an integer value and we know the constant is
  3338. // not a NAN (it would have been previously simplified).
  3339. assert(!RHS.isNaN() && "NaN comparison not already folded!");
  3340. ICmpInst::Predicate Pred;
  3341. switch (I.getPredicate()) {
  3342. default: llvm_unreachable("Unexpected predicate!");
  3343. case FCmpInst::FCMP_UEQ:
  3344. case FCmpInst::FCMP_OEQ:
  3345. Pred = ICmpInst::ICMP_EQ;
  3346. break;
  3347. case FCmpInst::FCMP_UGT:
  3348. case FCmpInst::FCMP_OGT:
  3349. Pred = LHSUnsigned ? ICmpInst::ICMP_UGT : ICmpInst::ICMP_SGT;
  3350. break;
  3351. case FCmpInst::FCMP_UGE:
  3352. case FCmpInst::FCMP_OGE:
  3353. Pred = LHSUnsigned ? ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
  3354. break;
  3355. case FCmpInst::FCMP_ULT:
  3356. case FCmpInst::FCMP_OLT:
  3357. Pred = LHSUnsigned ? ICmpInst::ICMP_ULT : ICmpInst::ICMP_SLT;
  3358. break;
  3359. case FCmpInst::FCMP_ULE:
  3360. case FCmpInst::FCMP_OLE:
  3361. Pred = LHSUnsigned ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_SLE;
  3362. break;
  3363. case FCmpInst::FCMP_UNE:
  3364. case FCmpInst::FCMP_ONE:
  3365. Pred = ICmpInst::ICMP_NE;
  3366. break;
  3367. case FCmpInst::FCMP_ORD:
  3368. return ReplaceInstUsesWith(I, Builder->getTrue());
  3369. case FCmpInst::FCMP_UNO:
  3370. return ReplaceInstUsesWith(I, Builder->getFalse());
  3371. }
  3372. // Now we know that the APFloat is a normal number, zero or inf.
  3373. // See if the FP constant is too large for the integer. For example,
  3374. // comparing an i8 to 300.0.
  3375. unsigned IntWidth = IntTy->getScalarSizeInBits();
  3376. if (!LHSUnsigned) {
  3377. // If the RHS value is > SignedMax, fold the comparison. This handles +INF
  3378. // and large values.
  3379. APFloat SMax(RHS.getSemantics());
  3380. SMax.convertFromAPInt(APInt::getSignedMaxValue(IntWidth), true,
  3381. APFloat::rmNearestTiesToEven);
  3382. if (SMax.compare(RHS) == APFloat::cmpLessThan) { // smax < 13123.0
  3383. if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SLT ||
  3384. Pred == ICmpInst::ICMP_SLE)
  3385. return ReplaceInstUsesWith(I, Builder->getTrue());
  3386. return ReplaceInstUsesWith(I, Builder->getFalse());
  3387. }
  3388. } else {
  3389. // If the RHS value is > UnsignedMax, fold the comparison. This handles
  3390. // +INF and large values.
  3391. APFloat UMax(RHS.getSemantics());
  3392. UMax.convertFromAPInt(APInt::getMaxValue(IntWidth), false,
  3393. APFloat::rmNearestTiesToEven);
  3394. if (UMax.compare(RHS) == APFloat::cmpLessThan) { // umax < 13123.0
  3395. if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_ULT ||
  3396. Pred == ICmpInst::ICMP_ULE)
  3397. return ReplaceInstUsesWith(I, Builder->getTrue());
  3398. return ReplaceInstUsesWith(I, Builder->getFalse());
  3399. }
  3400. }
  3401. if (!LHSUnsigned) {
  3402. // See if the RHS value is < SignedMin.
  3403. APFloat SMin(RHS.getSemantics());
  3404. SMin.convertFromAPInt(APInt::getSignedMinValue(IntWidth), true,
  3405. APFloat::rmNearestTiesToEven);
  3406. if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // smin > 12312.0
  3407. if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_SGT ||
  3408. Pred == ICmpInst::ICMP_SGE)
  3409. return ReplaceInstUsesWith(I, Builder->getTrue());
  3410. return ReplaceInstUsesWith(I, Builder->getFalse());
  3411. }
  3412. } else {
  3413. // See if the RHS value is < UnsignedMin.
  3414. APFloat SMin(RHS.getSemantics());
  3415. SMin.convertFromAPInt(APInt::getMinValue(IntWidth), true,
  3416. APFloat::rmNearestTiesToEven);
  3417. if (SMin.compare(RHS) == APFloat::cmpGreaterThan) { // umin > 12312.0
  3418. if (Pred == ICmpInst::ICMP_NE || Pred == ICmpInst::ICMP_UGT ||
  3419. Pred == ICmpInst::ICMP_UGE)
  3420. return ReplaceInstUsesWith(I, Builder->getTrue());
  3421. return ReplaceInstUsesWith(I, Builder->getFalse());
  3422. }
  3423. }
  3424. // Okay, now we know that the FP constant fits in the range [SMIN, SMAX] or
  3425. // [0, UMAX], but it may still be fractional. See if it is fractional by
  3426. // casting the FP value to the integer value and back, checking for equality.
  3427. // Don't do this for zero, because -0.0 is not fractional.
  3428. Constant *RHSInt = LHSUnsigned
  3429. ? ConstantExpr::getFPToUI(RHSC, IntTy)
  3430. : ConstantExpr::getFPToSI(RHSC, IntTy);
  3431. if (!RHS.isZero()) {
  3432. bool Equal = LHSUnsigned
  3433. ? ConstantExpr::getUIToFP(RHSInt, RHSC->getType()) == RHSC
  3434. : ConstantExpr::getSIToFP(RHSInt, RHSC->getType()) == RHSC;
  3435. if (!Equal) {
  3436. // If we had a comparison against a fractional value, we have to adjust
  3437. // the compare predicate and sometimes the value. RHSC is rounded towards
  3438. // zero at this point.
  3439. switch (Pred) {
  3440. default: llvm_unreachable("Unexpected integer comparison!");
  3441. case ICmpInst::ICMP_NE: // (float)int != 4.4 --> true
  3442. return ReplaceInstUsesWith(I, Builder->getTrue());
  3443. case ICmpInst::ICMP_EQ: // (float)int == 4.4 --> false
  3444. return ReplaceInstUsesWith(I, Builder->getFalse());
  3445. case ICmpInst::ICMP_ULE:
  3446. // (float)int <= 4.4 --> int <= 4
  3447. // (float)int <= -4.4 --> false
  3448. if (RHS.isNegative())
  3449. return ReplaceInstUsesWith(I, Builder->getFalse());
  3450. break;
  3451. case ICmpInst::ICMP_SLE:
  3452. // (float)int <= 4.4 --> int <= 4
  3453. // (float)int <= -4.4 --> int < -4
  3454. if (RHS.isNegative())
  3455. Pred = ICmpInst::ICMP_SLT;
  3456. break;
  3457. case ICmpInst::ICMP_ULT:
  3458. // (float)int < -4.4 --> false
  3459. // (float)int < 4.4 --> int <= 4
  3460. if (RHS.isNegative())
  3461. return ReplaceInstUsesWith(I, Builder->getFalse());
  3462. Pred = ICmpInst::ICMP_ULE;
  3463. break;
  3464. case ICmpInst::ICMP_SLT:
  3465. // (float)int < -4.4 --> int < -4
  3466. // (float)int < 4.4 --> int <= 4
  3467. if (!RHS.isNegative())
  3468. Pred = ICmpInst::ICMP_SLE;
  3469. break;
  3470. case ICmpInst::ICMP_UGT:
  3471. // (float)int > 4.4 --> int > 4
  3472. // (float)int > -4.4 --> true
  3473. if (RHS.isNegative())
  3474. return ReplaceInstUsesWith(I, Builder->getTrue());
  3475. break;
  3476. case ICmpInst::ICMP_SGT:
  3477. // (float)int > 4.4 --> int > 4
  3478. // (float)int > -4.4 --> int >= -4
  3479. if (RHS.isNegative())
  3480. Pred = ICmpInst::ICMP_SGE;
  3481. break;
  3482. case ICmpInst::ICMP_UGE:
  3483. // (float)int >= -4.4 --> true
  3484. // (float)int >= 4.4 --> int > 4
  3485. if (RHS.isNegative())
  3486. return ReplaceInstUsesWith(I, Builder->getTrue());
  3487. Pred = ICmpInst::ICMP_UGT;
  3488. break;
  3489. case ICmpInst::ICMP_SGE:
  3490. // (float)int >= -4.4 --> int >= -4
  3491. // (float)int >= 4.4 --> int > 4
  3492. if (!RHS.isNegative())
  3493. Pred = ICmpInst::ICMP_SGT;
  3494. break;
  3495. }
  3496. }
  3497. }
  3498. // Lower this FP comparison into an appropriate integer version of the
  3499. // comparison.
  3500. return new ICmpInst(Pred, LHSI->getOperand(0), RHSInt);
  3501. }
  3502. Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
  3503. bool Changed = false;
  3504. /// Orders the operands of the compare so that they are listed from most
  3505. /// complex to least complex. This puts constants before unary operators,
  3506. /// before binary operators.
  3507. if (getComplexity(I.getOperand(0)) < getComplexity(I.getOperand(1))) {
  3508. I.swapOperands();
  3509. Changed = true;
  3510. }
  3511. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  3512. if (Value *V = SimplifyFCmpInst(I.getPredicate(), Op0, Op1,
  3513. I.getFastMathFlags(), DL, TLI, DT, AC, &I))
  3514. return ReplaceInstUsesWith(I, V);
  3515. // Simplify 'fcmp pred X, X'
  3516. if (Op0 == Op1) {
  3517. switch (I.getPredicate()) {
  3518. default: llvm_unreachable("Unknown predicate!");
  3519. case FCmpInst::FCMP_UNO: // True if unordered: isnan(X) | isnan(Y)
  3520. case FCmpInst::FCMP_ULT: // True if unordered or less than
  3521. case FCmpInst::FCMP_UGT: // True if unordered or greater than
  3522. case FCmpInst::FCMP_UNE: // True if unordered or not equal
  3523. // Canonicalize these to be 'fcmp uno %X, 0.0'.
  3524. I.setPredicate(FCmpInst::FCMP_UNO);
  3525. I.setOperand(1, Constant::getNullValue(Op0->getType()));
  3526. return &I;
  3527. case FCmpInst::FCMP_ORD: // True if ordered (no nans)
  3528. case FCmpInst::FCMP_OEQ: // True if ordered and equal
  3529. case FCmpInst::FCMP_OGE: // True if ordered and greater than or equal
  3530. case FCmpInst::FCMP_OLE: // True if ordered and less than or equal
  3531. // Canonicalize these to be 'fcmp ord %X, 0.0'.
  3532. I.setPredicate(FCmpInst::FCMP_ORD);
  3533. I.setOperand(1, Constant::getNullValue(Op0->getType()));
  3534. return &I;
  3535. }
  3536. }
  3537. // Test if the FCmpInst instruction is used exclusively by a select as
  3538. // part of a minimum or maximum operation. If so, refrain from doing
  3539. // any other folding. This helps out other analyses which understand
  3540. // non-obfuscated minimum and maximum idioms, such as ScalarEvolution
  3541. // and CodeGen. And in this case, at least one of the comparison
  3542. // operands has at least one user besides the compare (the select),
  3543. // which would often largely negate the benefit of folding anyway.
  3544. if (I.hasOneUse())
  3545. if (SelectInst *SI = dyn_cast<SelectInst>(*I.user_begin()))
  3546. if ((SI->getOperand(1) == Op0 && SI->getOperand(2) == Op1) ||
  3547. (SI->getOperand(2) == Op0 && SI->getOperand(1) == Op1))
  3548. return nullptr;
  3549. // Handle fcmp with constant RHS
  3550. if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
  3551. if (Instruction *LHSI = dyn_cast<Instruction>(Op0))
  3552. switch (LHSI->getOpcode()) {
  3553. case Instruction::FPExt: {
  3554. // fcmp (fpext x), C -> fcmp x, (fptrunc C) if fptrunc is lossless
  3555. FPExtInst *LHSExt = cast<FPExtInst>(LHSI);
  3556. ConstantFP *RHSF = dyn_cast<ConstantFP>(RHSC);
  3557. if (!RHSF)
  3558. break;
  3559. const fltSemantics *Sem;
  3560. // FIXME: This shouldn't be here.
  3561. if (LHSExt->getSrcTy()->isHalfTy())
  3562. Sem = &APFloat::IEEEhalf;
  3563. else if (LHSExt->getSrcTy()->isFloatTy())
  3564. Sem = &APFloat::IEEEsingle;
  3565. else if (LHSExt->getSrcTy()->isDoubleTy())
  3566. Sem = &APFloat::IEEEdouble;
  3567. else if (LHSExt->getSrcTy()->isFP128Ty())
  3568. Sem = &APFloat::IEEEquad;
  3569. else if (LHSExt->getSrcTy()->isX86_FP80Ty())
  3570. Sem = &APFloat::x87DoubleExtended;
  3571. else if (LHSExt->getSrcTy()->isPPC_FP128Ty())
  3572. Sem = &APFloat::PPCDoubleDouble;
  3573. else
  3574. break;
  3575. bool Lossy;
  3576. APFloat F = RHSF->getValueAPF();
  3577. F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
  3578. // Avoid lossy conversions and denormals. Zero is a special case
  3579. // that's OK to convert.
  3580. APFloat Fabs = F;
  3581. Fabs.clearSign();
  3582. if (!Lossy &&
  3583. ((Fabs.compare(APFloat::getSmallestNormalized(*Sem)) !=
  3584. APFloat::cmpLessThan) || Fabs.isZero()))
  3585. return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
  3586. ConstantFP::get(RHSC->getContext(), F));
  3587. break;
  3588. }
  3589. case Instruction::PHI:
  3590. // Only fold fcmp into the PHI if the phi and fcmp are in the same
  3591. // block. If in the same block, we're encouraging jump threading. If
  3592. // not, we are just pessimizing the code by making an i1 phi.
  3593. if (LHSI->getParent() == I.getParent())
  3594. if (Instruction *NV = FoldOpIntoPhi(I))
  3595. return NV;
  3596. break;
  3597. case Instruction::SIToFP:
  3598. case Instruction::UIToFP:
  3599. if (Instruction *NV = FoldFCmp_IntToFP_Cst(I, LHSI, RHSC))
  3600. return NV;
  3601. break;
  3602. case Instruction::FSub: {
  3603. // fcmp pred (fneg x), C -> fcmp swap(pred) x, -C
  3604. Value *Op;
  3605. if (match(LHSI, m_FNeg(m_Value(Op))))
  3606. return new FCmpInst(I.getSwappedPredicate(), Op,
  3607. ConstantExpr::getFNeg(RHSC));
  3608. break;
  3609. }
  3610. case Instruction::Load:
  3611. if (GetElementPtrInst *GEP =
  3612. dyn_cast<GetElementPtrInst>(LHSI->getOperand(0))) {
  3613. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)))
  3614. if (GV->isConstant() && GV->hasDefinitiveInitializer() &&
  3615. !cast<LoadInst>(LHSI)->isVolatile())
  3616. if (Instruction *Res = FoldCmpLoadFromIndexedGlobal(GEP, GV, I))
  3617. return Res;
  3618. }
  3619. break;
  3620. case Instruction::Call: {
  3621. if (!RHSC->isNullValue())
  3622. break;
  3623. CallInst *CI = cast<CallInst>(LHSI);
  3624. const Function *F = CI->getCalledFunction();
  3625. if (!F)
  3626. break;
  3627. // Various optimization for fabs compared with zero.
  3628. LibFunc::Func Func;
  3629. if (F->getIntrinsicID() == Intrinsic::fabs ||
  3630. (TLI->getLibFunc(F->getName(), Func) && TLI->has(Func) &&
  3631. (Func == LibFunc::fabs || Func == LibFunc::fabsf ||
  3632. Func == LibFunc::fabsl))) {
  3633. switch (I.getPredicate()) {
  3634. default:
  3635. break;
  3636. // fabs(x) < 0 --> false
  3637. case FCmpInst::FCMP_OLT:
  3638. return ReplaceInstUsesWith(I, Builder->getFalse());
  3639. // fabs(x) > 0 --> x != 0
  3640. case FCmpInst::FCMP_OGT:
  3641. return new FCmpInst(FCmpInst::FCMP_ONE, CI->getArgOperand(0), RHSC);
  3642. // fabs(x) <= 0 --> x == 0
  3643. case FCmpInst::FCMP_OLE:
  3644. return new FCmpInst(FCmpInst::FCMP_OEQ, CI->getArgOperand(0), RHSC);
  3645. // fabs(x) >= 0 --> !isnan(x)
  3646. case FCmpInst::FCMP_OGE:
  3647. return new FCmpInst(FCmpInst::FCMP_ORD, CI->getArgOperand(0), RHSC);
  3648. // fabs(x) == 0 --> x == 0
  3649. // fabs(x) != 0 --> x != 0
  3650. case FCmpInst::FCMP_OEQ:
  3651. case FCmpInst::FCMP_UEQ:
  3652. case FCmpInst::FCMP_ONE:
  3653. case FCmpInst::FCMP_UNE:
  3654. return new FCmpInst(I.getPredicate(), CI->getArgOperand(0), RHSC);
  3655. }
  3656. }
  3657. }
  3658. }
  3659. }
  3660. // fcmp pred (fneg x), (fneg y) -> fcmp swap(pred) x, y
  3661. Value *X, *Y;
  3662. if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
  3663. return new FCmpInst(I.getSwappedPredicate(), X, Y);
  3664. // fcmp (fpext x), (fpext y) -> fcmp x, y
  3665. if (FPExtInst *LHSExt = dyn_cast<FPExtInst>(Op0))
  3666. if (FPExtInst *RHSExt = dyn_cast<FPExtInst>(Op1))
  3667. if (LHSExt->getSrcTy() == RHSExt->getSrcTy())
  3668. return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
  3669. RHSExt->getOperand(0));
  3670. return Changed ? &I : nullptr;
  3671. }