InstCombineSelect.cpp 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360
  1. //===- InstCombineSelect.cpp ----------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the visitSelect function.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "InstCombineInternal.h"
  13. #include "llvm/ADT/APInt.h"
  14. #include "llvm/ADT/Optional.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Analysis/AssumptionCache.h"
  18. #include "llvm/Analysis/CmpInstAnalysis.h"
  19. #include "llvm/Analysis/InstructionSimplify.h"
  20. #include "llvm/Analysis/OverflowInstAnalysis.h"
  21. #include "llvm/Analysis/ValueTracking.h"
  22. #include "llvm/IR/BasicBlock.h"
  23. #include "llvm/IR/Constant.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/DerivedTypes.h"
  26. #include "llvm/IR/IRBuilder.h"
  27. #include "llvm/IR/InstrTypes.h"
  28. #include "llvm/IR/Instruction.h"
  29. #include "llvm/IR/Instructions.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/Intrinsics.h"
  32. #include "llvm/IR/Operator.h"
  33. #include "llvm/IR/PatternMatch.h"
  34. #include "llvm/IR/Type.h"
  35. #include "llvm/IR/User.h"
  36. #include "llvm/IR/Value.h"
  37. #include "llvm/Support/Casting.h"
  38. #include "llvm/Support/ErrorHandling.h"
  39. #include "llvm/Support/KnownBits.h"
  40. #include "llvm/Transforms/InstCombine/InstCombiner.h"
  41. #include <cassert>
  42. #include <utility>
  43. #define DEBUG_TYPE "instcombine"
  44. #include "llvm/Transforms/Utils/InstructionWorklist.h"
  45. using namespace llvm;
  46. using namespace PatternMatch;
  47. static Value *createMinMax(InstCombiner::BuilderTy &Builder,
  48. SelectPatternFlavor SPF, Value *A, Value *B) {
  49. CmpInst::Predicate Pred = getMinMaxPred(SPF);
  50. assert(CmpInst::isIntPredicate(Pred) && "Expected integer predicate");
  51. return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B);
  52. }
  53. /// Replace a select operand based on an equality comparison with the identity
  54. /// constant of a binop.
  55. static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
  56. const TargetLibraryInfo &TLI,
  57. InstCombinerImpl &IC) {
  58. // The select condition must be an equality compare with a constant operand.
  59. Value *X;
  60. Constant *C;
  61. CmpInst::Predicate Pred;
  62. if (!match(Sel.getCondition(), m_Cmp(Pred, m_Value(X), m_Constant(C))))
  63. return nullptr;
  64. bool IsEq;
  65. if (ICmpInst::isEquality(Pred))
  66. IsEq = Pred == ICmpInst::ICMP_EQ;
  67. else if (Pred == FCmpInst::FCMP_OEQ)
  68. IsEq = true;
  69. else if (Pred == FCmpInst::FCMP_UNE)
  70. IsEq = false;
  71. else
  72. return nullptr;
  73. // A select operand must be a binop.
  74. BinaryOperator *BO;
  75. if (!match(Sel.getOperand(IsEq ? 1 : 2), m_BinOp(BO)))
  76. return nullptr;
  77. // The compare constant must be the identity constant for that binop.
  78. // If this a floating-point compare with 0.0, any zero constant will do.
  79. Type *Ty = BO->getType();
  80. Constant *IdC = ConstantExpr::getBinOpIdentity(BO->getOpcode(), Ty, true);
  81. if (IdC != C) {
  82. if (!IdC || !CmpInst::isFPPredicate(Pred))
  83. return nullptr;
  84. if (!match(IdC, m_AnyZeroFP()) || !match(C, m_AnyZeroFP()))
  85. return nullptr;
  86. }
  87. // Last, match the compare variable operand with a binop operand.
  88. Value *Y;
  89. if (!BO->isCommutative() && !match(BO, m_BinOp(m_Value(Y), m_Specific(X))))
  90. return nullptr;
  91. if (!match(BO, m_c_BinOp(m_Value(Y), m_Specific(X))))
  92. return nullptr;
  93. // +0.0 compares equal to -0.0, and so it does not behave as required for this
  94. // transform. Bail out if we can not exclude that possibility.
  95. if (isa<FPMathOperator>(BO))
  96. if (!BO->hasNoSignedZeros() && !CannotBeNegativeZero(Y, &TLI))
  97. return nullptr;
  98. // BO = binop Y, X
  99. // S = { select (cmp eq X, C), BO, ? } or { select (cmp ne X, C), ?, BO }
  100. // =>
  101. // S = { select (cmp eq X, C), Y, ? } or { select (cmp ne X, C), ?, Y }
  102. return IC.replaceOperand(Sel, IsEq ? 1 : 2, Y);
  103. }
  104. /// This folds:
  105. /// select (icmp eq (and X, C1)), TC, FC
  106. /// iff C1 is a power 2 and the difference between TC and FC is a power-of-2.
  107. /// To something like:
  108. /// (shr (and (X, C1)), (log2(C1) - log2(TC-FC))) + FC
  109. /// Or:
  110. /// (shl (and (X, C1)), (log2(TC-FC) - log2(C1))) + FC
  111. /// With some variations depending if FC is larger than TC, or the shift
  112. /// isn't needed, or the bit widths don't match.
  113. static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
  114. InstCombiner::BuilderTy &Builder) {
  115. const APInt *SelTC, *SelFC;
  116. if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
  117. !match(Sel.getFalseValue(), m_APInt(SelFC)))
  118. return nullptr;
  119. // If this is a vector select, we need a vector compare.
  120. Type *SelType = Sel.getType();
  121. if (SelType->isVectorTy() != Cmp->getType()->isVectorTy())
  122. return nullptr;
  123. Value *V;
  124. APInt AndMask;
  125. bool CreateAnd = false;
  126. ICmpInst::Predicate Pred = Cmp->getPredicate();
  127. if (ICmpInst::isEquality(Pred)) {
  128. if (!match(Cmp->getOperand(1), m_Zero()))
  129. return nullptr;
  130. V = Cmp->getOperand(0);
  131. const APInt *AndRHS;
  132. if (!match(V, m_And(m_Value(), m_Power2(AndRHS))))
  133. return nullptr;
  134. AndMask = *AndRHS;
  135. } else if (decomposeBitTestICmp(Cmp->getOperand(0), Cmp->getOperand(1),
  136. Pred, V, AndMask)) {
  137. assert(ICmpInst::isEquality(Pred) && "Not equality test?");
  138. if (!AndMask.isPowerOf2())
  139. return nullptr;
  140. CreateAnd = true;
  141. } else {
  142. return nullptr;
  143. }
  144. // In general, when both constants are non-zero, we would need an offset to
  145. // replace the select. This would require more instructions than we started
  146. // with. But there's one special-case that we handle here because it can
  147. // simplify/reduce the instructions.
  148. APInt TC = *SelTC;
  149. APInt FC = *SelFC;
  150. if (!TC.isZero() && !FC.isZero()) {
  151. // If the select constants differ by exactly one bit and that's the same
  152. // bit that is masked and checked by the select condition, the select can
  153. // be replaced by bitwise logic to set/clear one bit of the constant result.
  154. if (TC.getBitWidth() != AndMask.getBitWidth() || (TC ^ FC) != AndMask)
  155. return nullptr;
  156. if (CreateAnd) {
  157. // If we have to create an 'and', then we must kill the cmp to not
  158. // increase the instruction count.
  159. if (!Cmp->hasOneUse())
  160. return nullptr;
  161. V = Builder.CreateAnd(V, ConstantInt::get(SelType, AndMask));
  162. }
  163. bool ExtraBitInTC = TC.ugt(FC);
  164. if (Pred == ICmpInst::ICMP_EQ) {
  165. // If the masked bit in V is clear, clear or set the bit in the result:
  166. // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) ^ TC
  167. // (V & AndMaskC) == 0 ? TC : FC --> (V & AndMaskC) | TC
  168. Constant *C = ConstantInt::get(SelType, TC);
  169. return ExtraBitInTC ? Builder.CreateXor(V, C) : Builder.CreateOr(V, C);
  170. }
  171. if (Pred == ICmpInst::ICMP_NE) {
  172. // If the masked bit in V is set, set or clear the bit in the result:
  173. // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) | FC
  174. // (V & AndMaskC) != 0 ? TC : FC --> (V & AndMaskC) ^ FC
  175. Constant *C = ConstantInt::get(SelType, FC);
  176. return ExtraBitInTC ? Builder.CreateOr(V, C) : Builder.CreateXor(V, C);
  177. }
  178. llvm_unreachable("Only expecting equality predicates");
  179. }
  180. // Make sure one of the select arms is a power-of-2.
  181. if (!TC.isPowerOf2() && !FC.isPowerOf2())
  182. return nullptr;
  183. // Determine which shift is needed to transform result of the 'and' into the
  184. // desired result.
  185. const APInt &ValC = !TC.isZero() ? TC : FC;
  186. unsigned ValZeros = ValC.logBase2();
  187. unsigned AndZeros = AndMask.logBase2();
  188. // Insert the 'and' instruction on the input to the truncate.
  189. if (CreateAnd)
  190. V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask));
  191. // If types don't match, we can still convert the select by introducing a zext
  192. // or a trunc of the 'and'.
  193. if (ValZeros > AndZeros) {
  194. V = Builder.CreateZExtOrTrunc(V, SelType);
  195. V = Builder.CreateShl(V, ValZeros - AndZeros);
  196. } else if (ValZeros < AndZeros) {
  197. V = Builder.CreateLShr(V, AndZeros - ValZeros);
  198. V = Builder.CreateZExtOrTrunc(V, SelType);
  199. } else {
  200. V = Builder.CreateZExtOrTrunc(V, SelType);
  201. }
  202. // Okay, now we know that everything is set up, we just don't know whether we
  203. // have a icmp_ne or icmp_eq and whether the true or false val is the zero.
  204. bool ShouldNotVal = !TC.isZero();
  205. ShouldNotVal ^= Pred == ICmpInst::ICMP_NE;
  206. if (ShouldNotVal)
  207. V = Builder.CreateXor(V, ValC);
  208. return V;
  209. }
  210. /// We want to turn code that looks like this:
  211. /// %C = or %A, %B
  212. /// %D = select %cond, %C, %A
  213. /// into:
  214. /// %C = select %cond, %B, 0
  215. /// %D = or %A, %C
  216. ///
  217. /// Assuming that the specified instruction is an operand to the select, return
  218. /// a bitmask indicating which operands of this instruction are foldable if they
  219. /// equal the other incoming value of the select.
  220. static unsigned getSelectFoldableOperands(BinaryOperator *I) {
  221. switch (I->getOpcode()) {
  222. case Instruction::Add:
  223. case Instruction::FAdd:
  224. case Instruction::Mul:
  225. case Instruction::FMul:
  226. case Instruction::And:
  227. case Instruction::Or:
  228. case Instruction::Xor:
  229. return 3; // Can fold through either operand.
  230. case Instruction::Sub: // Can only fold on the amount subtracted.
  231. case Instruction::FSub:
  232. case Instruction::FDiv: // Can only fold on the divisor amount.
  233. case Instruction::Shl: // Can only fold on the shift amount.
  234. case Instruction::LShr:
  235. case Instruction::AShr:
  236. return 1;
  237. default:
  238. return 0; // Cannot fold
  239. }
  240. }
  241. /// We have (select c, TI, FI), and we know that TI and FI have the same opcode.
  242. Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
  243. Instruction *FI) {
  244. // Don't break up min/max patterns. The hasOneUse checks below prevent that
  245. // for most cases, but vector min/max with bitcasts can be transformed. If the
  246. // one-use restrictions are eased for other patterns, we still don't want to
  247. // obfuscate min/max.
  248. if ((match(&SI, m_SMin(m_Value(), m_Value())) ||
  249. match(&SI, m_SMax(m_Value(), m_Value())) ||
  250. match(&SI, m_UMin(m_Value(), m_Value())) ||
  251. match(&SI, m_UMax(m_Value(), m_Value()))))
  252. return nullptr;
  253. // If this is a cast from the same type, merge.
  254. Value *Cond = SI.getCondition();
  255. Type *CondTy = Cond->getType();
  256. if (TI->getNumOperands() == 1 && TI->isCast()) {
  257. Type *FIOpndTy = FI->getOperand(0)->getType();
  258. if (TI->getOperand(0)->getType() != FIOpndTy)
  259. return nullptr;
  260. // The select condition may be a vector. We may only change the operand
  261. // type if the vector width remains the same (and matches the condition).
  262. if (auto *CondVTy = dyn_cast<VectorType>(CondTy)) {
  263. if (!FIOpndTy->isVectorTy() ||
  264. CondVTy->getElementCount() !=
  265. cast<VectorType>(FIOpndTy)->getElementCount())
  266. return nullptr;
  267. // TODO: If the backend knew how to deal with casts better, we could
  268. // remove this limitation. For now, there's too much potential to create
  269. // worse codegen by promoting the select ahead of size-altering casts
  270. // (PR28160).
  271. //
  272. // Note that ValueTracking's matchSelectPattern() looks through casts
  273. // without checking 'hasOneUse' when it matches min/max patterns, so this
  274. // transform may end up happening anyway.
  275. if (TI->getOpcode() != Instruction::BitCast &&
  276. (!TI->hasOneUse() || !FI->hasOneUse()))
  277. return nullptr;
  278. } else if (!TI->hasOneUse() || !FI->hasOneUse()) {
  279. // TODO: The one-use restrictions for a scalar select could be eased if
  280. // the fold of a select in visitLoadInst() was enhanced to match a pattern
  281. // that includes a cast.
  282. return nullptr;
  283. }
  284. // Fold this by inserting a select from the input values.
  285. Value *NewSI =
  286. Builder.CreateSelect(Cond, TI->getOperand(0), FI->getOperand(0),
  287. SI.getName() + ".v", &SI);
  288. return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI,
  289. TI->getType());
  290. }
  291. // Cond ? -X : -Y --> -(Cond ? X : Y)
  292. Value *X, *Y;
  293. if (match(TI, m_FNeg(m_Value(X))) && match(FI, m_FNeg(m_Value(Y))) &&
  294. (TI->hasOneUse() || FI->hasOneUse())) {
  295. // Intersect FMF from the fneg instructions and union those with the select.
  296. FastMathFlags FMF = TI->getFastMathFlags();
  297. FMF &= FI->getFastMathFlags();
  298. FMF |= SI.getFastMathFlags();
  299. Value *NewSel = Builder.CreateSelect(Cond, X, Y, SI.getName() + ".v", &SI);
  300. if (auto *NewSelI = dyn_cast<Instruction>(NewSel))
  301. NewSelI->setFastMathFlags(FMF);
  302. Instruction *NewFNeg = UnaryOperator::CreateFNeg(NewSel);
  303. NewFNeg->setFastMathFlags(FMF);
  304. return NewFNeg;
  305. }
  306. // Min/max intrinsic with a common operand can have the common operand pulled
  307. // after the select. This is the same transform as below for binops, but
  308. // specialized for intrinsic matching and without the restrictive uses clause.
  309. auto *TII = dyn_cast<IntrinsicInst>(TI);
  310. auto *FII = dyn_cast<IntrinsicInst>(FI);
  311. if (TII && FII && TII->getIntrinsicID() == FII->getIntrinsicID() &&
  312. (TII->hasOneUse() || FII->hasOneUse())) {
  313. Value *T0, *T1, *F0, *F1;
  314. if (match(TII, m_MaxOrMin(m_Value(T0), m_Value(T1))) &&
  315. match(FII, m_MaxOrMin(m_Value(F0), m_Value(F1)))) {
  316. if (T0 == F0) {
  317. Value *NewSel = Builder.CreateSelect(Cond, T1, F1, "minmaxop", &SI);
  318. return CallInst::Create(TII->getCalledFunction(), {NewSel, T0});
  319. }
  320. if (T0 == F1) {
  321. Value *NewSel = Builder.CreateSelect(Cond, T1, F0, "minmaxop", &SI);
  322. return CallInst::Create(TII->getCalledFunction(), {NewSel, T0});
  323. }
  324. if (T1 == F0) {
  325. Value *NewSel = Builder.CreateSelect(Cond, T0, F1, "minmaxop", &SI);
  326. return CallInst::Create(TII->getCalledFunction(), {NewSel, T1});
  327. }
  328. if (T1 == F1) {
  329. Value *NewSel = Builder.CreateSelect(Cond, T0, F0, "minmaxop", &SI);
  330. return CallInst::Create(TII->getCalledFunction(), {NewSel, T1});
  331. }
  332. }
  333. }
  334. // Only handle binary operators (including two-operand getelementptr) with
  335. // one-use here. As with the cast case above, it may be possible to relax the
  336. // one-use constraint, but that needs be examined carefully since it may not
  337. // reduce the total number of instructions.
  338. if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 ||
  339. (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) ||
  340. !TI->hasOneUse() || !FI->hasOneUse())
  341. return nullptr;
  342. // Figure out if the operations have any operands in common.
  343. Value *MatchOp, *OtherOpT, *OtherOpF;
  344. bool MatchIsOpZero;
  345. if (TI->getOperand(0) == FI->getOperand(0)) {
  346. MatchOp = TI->getOperand(0);
  347. OtherOpT = TI->getOperand(1);
  348. OtherOpF = FI->getOperand(1);
  349. MatchIsOpZero = true;
  350. } else if (TI->getOperand(1) == FI->getOperand(1)) {
  351. MatchOp = TI->getOperand(1);
  352. OtherOpT = TI->getOperand(0);
  353. OtherOpF = FI->getOperand(0);
  354. MatchIsOpZero = false;
  355. } else if (!TI->isCommutative()) {
  356. return nullptr;
  357. } else if (TI->getOperand(0) == FI->getOperand(1)) {
  358. MatchOp = TI->getOperand(0);
  359. OtherOpT = TI->getOperand(1);
  360. OtherOpF = FI->getOperand(0);
  361. MatchIsOpZero = true;
  362. } else if (TI->getOperand(1) == FI->getOperand(0)) {
  363. MatchOp = TI->getOperand(1);
  364. OtherOpT = TI->getOperand(0);
  365. OtherOpF = FI->getOperand(1);
  366. MatchIsOpZero = true;
  367. } else {
  368. return nullptr;
  369. }
  370. // If the select condition is a vector, the operands of the original select's
  371. // operands also must be vectors. This may not be the case for getelementptr
  372. // for example.
  373. if (CondTy->isVectorTy() && (!OtherOpT->getType()->isVectorTy() ||
  374. !OtherOpF->getType()->isVectorTy()))
  375. return nullptr;
  376. // If we reach here, they do have operations in common.
  377. Value *NewSI = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
  378. SI.getName() + ".v", &SI);
  379. Value *Op0 = MatchIsOpZero ? MatchOp : NewSI;
  380. Value *Op1 = MatchIsOpZero ? NewSI : MatchOp;
  381. if (auto *BO = dyn_cast<BinaryOperator>(TI)) {
  382. BinaryOperator *NewBO = BinaryOperator::Create(BO->getOpcode(), Op0, Op1);
  383. NewBO->copyIRFlags(TI);
  384. NewBO->andIRFlags(FI);
  385. return NewBO;
  386. }
  387. if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) {
  388. auto *FGEP = cast<GetElementPtrInst>(FI);
  389. Type *ElementType = TGEP->getResultElementType();
  390. return TGEP->isInBounds() && FGEP->isInBounds()
  391. ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1})
  392. : GetElementPtrInst::Create(ElementType, Op0, {Op1});
  393. }
  394. llvm_unreachable("Expected BinaryOperator or GEP");
  395. return nullptr;
  396. }
  397. static bool isSelect01(const APInt &C1I, const APInt &C2I) {
  398. if (!C1I.isZero() && !C2I.isZero()) // One side must be zero.
  399. return false;
  400. return C1I.isOne() || C1I.isAllOnes() || C2I.isOne() || C2I.isAllOnes();
  401. }
  402. /// Try to fold the select into one of the operands to allow further
  403. /// optimization.
  404. Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal,
  405. Value *FalseVal) {
  406. // See the comment above GetSelectFoldableOperands for a description of the
  407. // transformation we are doing here.
  408. if (auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) {
  409. if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) {
  410. if (unsigned SFO = getSelectFoldableOperands(TVI)) {
  411. unsigned OpToFold = 0;
  412. if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
  413. OpToFold = 1;
  414. } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
  415. OpToFold = 2;
  416. }
  417. if (OpToFold) {
  418. Constant *C = ConstantExpr::getBinOpIdentity(TVI->getOpcode(),
  419. TVI->getType(), true);
  420. Value *OOp = TVI->getOperand(2-OpToFold);
  421. // Avoid creating select between 2 constants unless it's selecting
  422. // between 0, 1 and -1.
  423. const APInt *OOpC;
  424. bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
  425. if (!isa<Constant>(OOp) ||
  426. (OOpIsAPInt && isSelect01(C->getUniqueInteger(), *OOpC))) {
  427. Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C);
  428. NewSel->takeName(TVI);
  429. BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(),
  430. FalseVal, NewSel);
  431. BO->copyIRFlags(TVI);
  432. return BO;
  433. }
  434. }
  435. }
  436. }
  437. }
  438. if (auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) {
  439. if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) {
  440. if (unsigned SFO = getSelectFoldableOperands(FVI)) {
  441. unsigned OpToFold = 0;
  442. if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
  443. OpToFold = 1;
  444. } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
  445. OpToFold = 2;
  446. }
  447. if (OpToFold) {
  448. Constant *C = ConstantExpr::getBinOpIdentity(FVI->getOpcode(),
  449. FVI->getType(), true);
  450. Value *OOp = FVI->getOperand(2-OpToFold);
  451. // Avoid creating select between 2 constants unless it's selecting
  452. // between 0, 1 and -1.
  453. const APInt *OOpC;
  454. bool OOpIsAPInt = match(OOp, m_APInt(OOpC));
  455. if (!isa<Constant>(OOp) ||
  456. (OOpIsAPInt && isSelect01(C->getUniqueInteger(), *OOpC))) {
  457. Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp);
  458. NewSel->takeName(FVI);
  459. BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(),
  460. TrueVal, NewSel);
  461. BO->copyIRFlags(FVI);
  462. return BO;
  463. }
  464. }
  465. }
  466. }
  467. }
  468. return nullptr;
  469. }
  470. /// We want to turn:
  471. /// (select (icmp eq (and X, Y), 0), (and (lshr X, Z), 1), 1)
  472. /// into:
  473. /// zext (icmp ne i32 (and X, (or Y, (shl 1, Z))), 0)
  474. /// Note:
  475. /// Z may be 0 if lshr is missing.
  476. /// Worst-case scenario is that we will replace 5 instructions with 5 different
  477. /// instructions, but we got rid of select.
  478. static Instruction *foldSelectICmpAndAnd(Type *SelType, const ICmpInst *Cmp,
  479. Value *TVal, Value *FVal,
  480. InstCombiner::BuilderTy &Builder) {
  481. if (!(Cmp->hasOneUse() && Cmp->getOperand(0)->hasOneUse() &&
  482. Cmp->getPredicate() == ICmpInst::ICMP_EQ &&
  483. match(Cmp->getOperand(1), m_Zero()) && match(FVal, m_One())))
  484. return nullptr;
  485. // The TrueVal has general form of: and %B, 1
  486. Value *B;
  487. if (!match(TVal, m_OneUse(m_And(m_Value(B), m_One()))))
  488. return nullptr;
  489. // Where %B may be optionally shifted: lshr %X, %Z.
  490. Value *X, *Z;
  491. const bool HasShift = match(B, m_OneUse(m_LShr(m_Value(X), m_Value(Z))));
  492. if (!HasShift)
  493. X = B;
  494. Value *Y;
  495. if (!match(Cmp->getOperand(0), m_c_And(m_Specific(X), m_Value(Y))))
  496. return nullptr;
  497. // ((X & Y) == 0) ? ((X >> Z) & 1) : 1 --> (X & (Y | (1 << Z))) != 0
  498. // ((X & Y) == 0) ? (X & 1) : 1 --> (X & (Y | 1)) != 0
  499. Constant *One = ConstantInt::get(SelType, 1);
  500. Value *MaskB = HasShift ? Builder.CreateShl(One, Z) : One;
  501. Value *FullMask = Builder.CreateOr(Y, MaskB);
  502. Value *MaskedX = Builder.CreateAnd(X, FullMask);
  503. Value *ICmpNeZero = Builder.CreateIsNotNull(MaskedX);
  504. return new ZExtInst(ICmpNeZero, SelType);
  505. }
  506. /// We want to turn:
  507. /// (select (icmp sgt x, C), lshr (X, Y), ashr (X, Y)); iff C s>= -1
  508. /// (select (icmp slt x, C), ashr (X, Y), lshr (X, Y)); iff C s>= 0
  509. /// into:
  510. /// ashr (X, Y)
  511. static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
  512. Value *FalseVal,
  513. InstCombiner::BuilderTy &Builder) {
  514. ICmpInst::Predicate Pred = IC->getPredicate();
  515. Value *CmpLHS = IC->getOperand(0);
  516. Value *CmpRHS = IC->getOperand(1);
  517. if (!CmpRHS->getType()->isIntOrIntVectorTy())
  518. return nullptr;
  519. Value *X, *Y;
  520. unsigned Bitwidth = CmpRHS->getType()->getScalarSizeInBits();
  521. if ((Pred != ICmpInst::ICMP_SGT ||
  522. !match(CmpRHS,
  523. m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, -1)))) &&
  524. (Pred != ICmpInst::ICMP_SLT ||
  525. !match(CmpRHS,
  526. m_SpecificInt_ICMP(ICmpInst::ICMP_SGE, APInt(Bitwidth, 0)))))
  527. return nullptr;
  528. // Canonicalize so that ashr is in FalseVal.
  529. if (Pred == ICmpInst::ICMP_SLT)
  530. std::swap(TrueVal, FalseVal);
  531. if (match(TrueVal, m_LShr(m_Value(X), m_Value(Y))) &&
  532. match(FalseVal, m_AShr(m_Specific(X), m_Specific(Y))) &&
  533. match(CmpLHS, m_Specific(X))) {
  534. const auto *Ashr = cast<Instruction>(FalseVal);
  535. // if lshr is not exact and ashr is, this new ashr must not be exact.
  536. bool IsExact = Ashr->isExact() && cast<Instruction>(TrueVal)->isExact();
  537. return Builder.CreateAShr(X, Y, IC->getName(), IsExact);
  538. }
  539. return nullptr;
  540. }
  541. /// We want to turn:
  542. /// (select (icmp eq (and X, C1), 0), Y, (or Y, C2))
  543. /// into:
  544. /// (or (shl (and X, C1), C3), Y)
  545. /// iff:
  546. /// C1 and C2 are both powers of 2
  547. /// where:
  548. /// C3 = Log(C2) - Log(C1)
  549. ///
  550. /// This transform handles cases where:
  551. /// 1. The icmp predicate is inverted
  552. /// 2. The select operands are reversed
  553. /// 3. The magnitude of C2 and C1 are flipped
  554. static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
  555. Value *FalseVal,
  556. InstCombiner::BuilderTy &Builder) {
  557. // Only handle integer compares. Also, if this is a vector select, we need a
  558. // vector compare.
  559. if (!TrueVal->getType()->isIntOrIntVectorTy() ||
  560. TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy())
  561. return nullptr;
  562. Value *CmpLHS = IC->getOperand(0);
  563. Value *CmpRHS = IC->getOperand(1);
  564. Value *V;
  565. unsigned C1Log;
  566. bool IsEqualZero;
  567. bool NeedAnd = false;
  568. if (IC->isEquality()) {
  569. if (!match(CmpRHS, m_Zero()))
  570. return nullptr;
  571. const APInt *C1;
  572. if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1))))
  573. return nullptr;
  574. V = CmpLHS;
  575. C1Log = C1->logBase2();
  576. IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ;
  577. } else if (IC->getPredicate() == ICmpInst::ICMP_SLT ||
  578. IC->getPredicate() == ICmpInst::ICMP_SGT) {
  579. // We also need to recognize (icmp slt (trunc (X)), 0) and
  580. // (icmp sgt (trunc (X)), -1).
  581. IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_SGT;
  582. if ((IsEqualZero && !match(CmpRHS, m_AllOnes())) ||
  583. (!IsEqualZero && !match(CmpRHS, m_Zero())))
  584. return nullptr;
  585. if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V)))))
  586. return nullptr;
  587. C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1;
  588. NeedAnd = true;
  589. } else {
  590. return nullptr;
  591. }
  592. const APInt *C2;
  593. bool OrOnTrueVal = false;
  594. bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)));
  595. if (!OrOnFalseVal)
  596. OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)));
  597. if (!OrOnFalseVal && !OrOnTrueVal)
  598. return nullptr;
  599. Value *Y = OrOnFalseVal ? TrueVal : FalseVal;
  600. unsigned C2Log = C2->logBase2();
  601. bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal);
  602. bool NeedShift = C1Log != C2Log;
  603. bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() !=
  604. V->getType()->getScalarSizeInBits();
  605. // Make sure we don't create more instructions than we save.
  606. Value *Or = OrOnFalseVal ? FalseVal : TrueVal;
  607. if ((NeedShift + NeedXor + NeedZExtTrunc) >
  608. (IC->hasOneUse() + Or->hasOneUse()))
  609. return nullptr;
  610. if (NeedAnd) {
  611. // Insert the AND instruction on the input to the truncate.
  612. APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log);
  613. V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1));
  614. }
  615. if (C2Log > C1Log) {
  616. V = Builder.CreateZExtOrTrunc(V, Y->getType());
  617. V = Builder.CreateShl(V, C2Log - C1Log);
  618. } else if (C1Log > C2Log) {
  619. V = Builder.CreateLShr(V, C1Log - C2Log);
  620. V = Builder.CreateZExtOrTrunc(V, Y->getType());
  621. } else
  622. V = Builder.CreateZExtOrTrunc(V, Y->getType());
  623. if (NeedXor)
  624. V = Builder.CreateXor(V, *C2);
  625. return Builder.CreateOr(V, Y);
  626. }
  627. /// Canonicalize a set or clear of a masked set of constant bits to
  628. /// select-of-constants form.
  629. static Instruction *foldSetClearBits(SelectInst &Sel,
  630. InstCombiner::BuilderTy &Builder) {
  631. Value *Cond = Sel.getCondition();
  632. Value *T = Sel.getTrueValue();
  633. Value *F = Sel.getFalseValue();
  634. Type *Ty = Sel.getType();
  635. Value *X;
  636. const APInt *NotC, *C;
  637. // Cond ? (X & ~C) : (X | C) --> (X & ~C) | (Cond ? 0 : C)
  638. if (match(T, m_And(m_Value(X), m_APInt(NotC))) &&
  639. match(F, m_OneUse(m_Or(m_Specific(X), m_APInt(C)))) && *NotC == ~(*C)) {
  640. Constant *Zero = ConstantInt::getNullValue(Ty);
  641. Constant *OrC = ConstantInt::get(Ty, *C);
  642. Value *NewSel = Builder.CreateSelect(Cond, Zero, OrC, "masksel", &Sel);
  643. return BinaryOperator::CreateOr(T, NewSel);
  644. }
  645. // Cond ? (X | C) : (X & ~C) --> (X & ~C) | (Cond ? C : 0)
  646. if (match(F, m_And(m_Value(X), m_APInt(NotC))) &&
  647. match(T, m_OneUse(m_Or(m_Specific(X), m_APInt(C)))) && *NotC == ~(*C)) {
  648. Constant *Zero = ConstantInt::getNullValue(Ty);
  649. Constant *OrC = ConstantInt::get(Ty, *C);
  650. Value *NewSel = Builder.CreateSelect(Cond, OrC, Zero, "masksel", &Sel);
  651. return BinaryOperator::CreateOr(F, NewSel);
  652. }
  653. return nullptr;
  654. }
  655. // select (x == 0), 0, x * y --> freeze(y) * x
  656. // select (y == 0), 0, x * y --> freeze(x) * y
  657. // select (x == 0), undef, x * y --> freeze(y) * x
  658. // select (x == undef), 0, x * y --> freeze(y) * x
  659. // Usage of mul instead of 0 will make the result more poisonous,
  660. // so the operand that was not checked in the condition should be frozen.
  661. // The latter folding is applied only when a constant compared with x is
  662. // is a vector consisting of 0 and undefs. If a constant compared with x
  663. // is a scalar undefined value or undefined vector then an expression
  664. // should be already folded into a constant.
  665. static Instruction *foldSelectZeroOrMul(SelectInst &SI, InstCombinerImpl &IC) {
  666. auto *CondVal = SI.getCondition();
  667. auto *TrueVal = SI.getTrueValue();
  668. auto *FalseVal = SI.getFalseValue();
  669. Value *X, *Y;
  670. ICmpInst::Predicate Predicate;
  671. // Assuming that constant compared with zero is not undef (but it may be
  672. // a vector with some undef elements). Otherwise (when a constant is undef)
  673. // the select expression should be already simplified.
  674. if (!match(CondVal, m_ICmp(Predicate, m_Value(X), m_Zero())) ||
  675. !ICmpInst::isEquality(Predicate))
  676. return nullptr;
  677. if (Predicate == ICmpInst::ICMP_NE)
  678. std::swap(TrueVal, FalseVal);
  679. // Check that TrueVal is a constant instead of matching it with m_Zero()
  680. // to handle the case when it is a scalar undef value or a vector containing
  681. // non-zero elements that are masked by undef elements in the compare
  682. // constant.
  683. auto *TrueValC = dyn_cast<Constant>(TrueVal);
  684. if (TrueValC == nullptr ||
  685. !match(FalseVal, m_c_Mul(m_Specific(X), m_Value(Y))) ||
  686. !isa<Instruction>(FalseVal))
  687. return nullptr;
  688. auto *ZeroC = cast<Constant>(cast<Instruction>(CondVal)->getOperand(1));
  689. auto *MergedC = Constant::mergeUndefsWith(TrueValC, ZeroC);
  690. // If X is compared with 0 then TrueVal could be either zero or undef.
  691. // m_Zero match vectors containing some undef elements, but for scalars
  692. // m_Undef should be used explicitly.
  693. if (!match(MergedC, m_Zero()) && !match(MergedC, m_Undef()))
  694. return nullptr;
  695. auto *FalseValI = cast<Instruction>(FalseVal);
  696. auto *FrY = IC.InsertNewInstBefore(new FreezeInst(Y, Y->getName() + ".fr"),
  697. *FalseValI);
  698. IC.replaceOperand(*FalseValI, FalseValI->getOperand(0) == Y ? 0 : 1, FrY);
  699. return IC.replaceInstUsesWith(SI, FalseValI);
  700. }
  701. /// Transform patterns such as (a > b) ? a - b : 0 into usub.sat(a, b).
  702. /// There are 8 commuted/swapped variants of this pattern.
  703. /// TODO: Also support a - UMIN(a,b) patterns.
  704. static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI,
  705. const Value *TrueVal,
  706. const Value *FalseVal,
  707. InstCombiner::BuilderTy &Builder) {
  708. ICmpInst::Predicate Pred = ICI->getPredicate();
  709. if (!ICmpInst::isUnsigned(Pred))
  710. return nullptr;
  711. // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0
  712. if (match(TrueVal, m_Zero())) {
  713. Pred = ICmpInst::getInversePredicate(Pred);
  714. std::swap(TrueVal, FalseVal);
  715. }
  716. if (!match(FalseVal, m_Zero()))
  717. return nullptr;
  718. Value *A = ICI->getOperand(0);
  719. Value *B = ICI->getOperand(1);
  720. if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) {
  721. // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0
  722. std::swap(A, B);
  723. Pred = ICmpInst::getSwappedPredicate(Pred);
  724. }
  725. assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) &&
  726. "Unexpected isUnsigned predicate!");
  727. // Ensure the sub is of the form:
  728. // (a > b) ? a - b : 0 -> usub.sat(a, b)
  729. // (a > b) ? b - a : 0 -> -usub.sat(a, b)
  730. // Checking for both a-b and a+(-b) as a constant.
  731. bool IsNegative = false;
  732. const APInt *C;
  733. if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A))) ||
  734. (match(A, m_APInt(C)) &&
  735. match(TrueVal, m_Add(m_Specific(B), m_SpecificInt(-*C)))))
  736. IsNegative = true;
  737. else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B))) &&
  738. !(match(B, m_APInt(C)) &&
  739. match(TrueVal, m_Add(m_Specific(A), m_SpecificInt(-*C)))))
  740. return nullptr;
  741. // If we are adding a negate and the sub and icmp are used anywhere else, we
  742. // would end up with more instructions.
  743. if (IsNegative && !TrueVal->hasOneUse() && !ICI->hasOneUse())
  744. return nullptr;
  745. // (a > b) ? a - b : 0 -> usub.sat(a, b)
  746. // (a > b) ? b - a : 0 -> -usub.sat(a, b)
  747. Value *Result = Builder.CreateBinaryIntrinsic(Intrinsic::usub_sat, A, B);
  748. if (IsNegative)
  749. Result = Builder.CreateNeg(Result);
  750. return Result;
  751. }
  752. static Value *canonicalizeSaturatedAdd(ICmpInst *Cmp, Value *TVal, Value *FVal,
  753. InstCombiner::BuilderTy &Builder) {
  754. if (!Cmp->hasOneUse())
  755. return nullptr;
  756. // Match unsigned saturated add with constant.
  757. Value *Cmp0 = Cmp->getOperand(0);
  758. Value *Cmp1 = Cmp->getOperand(1);
  759. ICmpInst::Predicate Pred = Cmp->getPredicate();
  760. Value *X;
  761. const APInt *C, *CmpC;
  762. if (Pred == ICmpInst::ICMP_ULT &&
  763. match(TVal, m_Add(m_Value(X), m_APInt(C))) && X == Cmp0 &&
  764. match(FVal, m_AllOnes()) && match(Cmp1, m_APInt(CmpC)) && *CmpC == ~*C) {
  765. // (X u< ~C) ? (X + C) : -1 --> uadd.sat(X, C)
  766. return Builder.CreateBinaryIntrinsic(
  767. Intrinsic::uadd_sat, X, ConstantInt::get(X->getType(), *C));
  768. }
  769. // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
  770. // There are 8 commuted variants.
  771. // Canonicalize -1 (saturated result) to true value of the select.
  772. if (match(FVal, m_AllOnes())) {
  773. std::swap(TVal, FVal);
  774. Pred = CmpInst::getInversePredicate(Pred);
  775. }
  776. if (!match(TVal, m_AllOnes()))
  777. return nullptr;
  778. // Canonicalize predicate to less-than or less-or-equal-than.
  779. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
  780. std::swap(Cmp0, Cmp1);
  781. Pred = CmpInst::getSwappedPredicate(Pred);
  782. }
  783. if (Pred != ICmpInst::ICMP_ULT && Pred != ICmpInst::ICMP_ULE)
  784. return nullptr;
  785. // Match unsigned saturated add of 2 variables with an unnecessary 'not'.
  786. // Strictness of the comparison is irrelevant.
  787. Value *Y;
  788. if (match(Cmp0, m_Not(m_Value(X))) &&
  789. match(FVal, m_c_Add(m_Specific(X), m_Value(Y))) && Y == Cmp1) {
  790. // (~X u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
  791. // (~X u< Y) ? -1 : (Y + X) --> uadd.sat(X, Y)
  792. return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, X, Y);
  793. }
  794. // The 'not' op may be included in the sum but not the compare.
  795. // Strictness of the comparison is irrelevant.
  796. X = Cmp0;
  797. Y = Cmp1;
  798. if (match(FVal, m_c_Add(m_Not(m_Specific(X)), m_Specific(Y)))) {
  799. // (X u< Y) ? -1 : (~X + Y) --> uadd.sat(~X, Y)
  800. // (X u< Y) ? -1 : (Y + ~X) --> uadd.sat(Y, ~X)
  801. BinaryOperator *BO = cast<BinaryOperator>(FVal);
  802. return Builder.CreateBinaryIntrinsic(
  803. Intrinsic::uadd_sat, BO->getOperand(0), BO->getOperand(1));
  804. }
  805. // The overflow may be detected via the add wrapping round.
  806. // This is only valid for strict comparison!
  807. if (Pred == ICmpInst::ICMP_ULT &&
  808. match(Cmp0, m_c_Add(m_Specific(Cmp1), m_Value(Y))) &&
  809. match(FVal, m_c_Add(m_Specific(Cmp1), m_Specific(Y)))) {
  810. // ((X + Y) u< X) ? -1 : (X + Y) --> uadd.sat(X, Y)
  811. // ((X + Y) u< Y) ? -1 : (X + Y) --> uadd.sat(X, Y)
  812. return Builder.CreateBinaryIntrinsic(Intrinsic::uadd_sat, Cmp1, Y);
  813. }
  814. return nullptr;
  815. }
  816. /// Fold the following code sequence:
  817. /// \code
  818. /// int a = ctlz(x & -x);
  819. // x ? 31 - a : a;
  820. /// \code
  821. ///
  822. /// into:
  823. /// cttz(x)
  824. static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
  825. Value *FalseVal,
  826. InstCombiner::BuilderTy &Builder) {
  827. unsigned BitWidth = TrueVal->getType()->getScalarSizeInBits();
  828. if (!ICI->isEquality() || !match(ICI->getOperand(1), m_Zero()))
  829. return nullptr;
  830. if (ICI->getPredicate() == ICmpInst::ICMP_NE)
  831. std::swap(TrueVal, FalseVal);
  832. if (!match(FalseVal,
  833. m_Xor(m_Deferred(TrueVal), m_SpecificInt(BitWidth - 1))))
  834. return nullptr;
  835. if (!match(TrueVal, m_Intrinsic<Intrinsic::ctlz>()))
  836. return nullptr;
  837. Value *X = ICI->getOperand(0);
  838. auto *II = cast<IntrinsicInst>(TrueVal);
  839. if (!match(II->getOperand(0), m_c_And(m_Specific(X), m_Neg(m_Specific(X)))))
  840. return nullptr;
  841. Function *F = Intrinsic::getDeclaration(II->getModule(), Intrinsic::cttz,
  842. II->getType());
  843. return CallInst::Create(F, {X, II->getArgOperand(1)});
  844. }
  845. /// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single
  846. /// call to cttz/ctlz with flag 'is_zero_poison' cleared.
  847. ///
  848. /// For example, we can fold the following code sequence:
  849. /// \code
  850. /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true)
  851. /// %1 = icmp ne i32 %x, 0
  852. /// %2 = select i1 %1, i32 %0, i32 32
  853. /// \code
  854. ///
  855. /// into:
  856. /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
  857. static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
  858. InstCombiner::BuilderTy &Builder) {
  859. ICmpInst::Predicate Pred = ICI->getPredicate();
  860. Value *CmpLHS = ICI->getOperand(0);
  861. Value *CmpRHS = ICI->getOperand(1);
  862. // Check if the condition value compares a value for equality against zero.
  863. if (!ICI->isEquality() || !match(CmpRHS, m_Zero()))
  864. return nullptr;
  865. Value *SelectArg = FalseVal;
  866. Value *ValueOnZero = TrueVal;
  867. if (Pred == ICmpInst::ICMP_NE)
  868. std::swap(SelectArg, ValueOnZero);
  869. // Skip zero extend/truncate.
  870. Value *Count = nullptr;
  871. if (!match(SelectArg, m_ZExt(m_Value(Count))) &&
  872. !match(SelectArg, m_Trunc(m_Value(Count))))
  873. Count = SelectArg;
  874. // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the
  875. // input to the cttz/ctlz is used as LHS for the compare instruction.
  876. if (!match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) &&
  877. !match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS))))
  878. return nullptr;
  879. IntrinsicInst *II = cast<IntrinsicInst>(Count);
  880. // Check if the value propagated on zero is a constant number equal to the
  881. // sizeof in bits of 'Count'.
  882. unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits();
  883. if (match(ValueOnZero, m_SpecificInt(SizeOfInBits))) {
  884. // Explicitly clear the 'is_zero_poison' flag. It's always valid to go from
  885. // true to false on this flag, so we can replace it for all users.
  886. II->setArgOperand(1, ConstantInt::getFalse(II->getContext()));
  887. return SelectArg;
  888. }
  889. // The ValueOnZero is not the bitwidth. But if the cttz/ctlz (and optional
  890. // zext/trunc) have one use (ending at the select), the cttz/ctlz result will
  891. // not be used if the input is zero. Relax to 'zero is poison' for that case.
  892. if (II->hasOneUse() && SelectArg->hasOneUse() &&
  893. !match(II->getArgOperand(1), m_One()))
  894. II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
  895. return nullptr;
  896. }
  897. /// Return true if we find and adjust an icmp+select pattern where the compare
  898. /// is with a constant that can be incremented or decremented to match the
  899. /// minimum or maximum idiom.
  900. static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) {
  901. ICmpInst::Predicate Pred = Cmp.getPredicate();
  902. Value *CmpLHS = Cmp.getOperand(0);
  903. Value *CmpRHS = Cmp.getOperand(1);
  904. Value *TrueVal = Sel.getTrueValue();
  905. Value *FalseVal = Sel.getFalseValue();
  906. // We may move or edit the compare, so make sure the select is the only user.
  907. const APInt *CmpC;
  908. if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC)))
  909. return false;
  910. // These transforms only work for selects of integers or vector selects of
  911. // integer vectors.
  912. Type *SelTy = Sel.getType();
  913. auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType());
  914. if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy())
  915. return false;
  916. Constant *AdjustedRHS;
  917. if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT)
  918. AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1);
  919. else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT)
  920. AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1);
  921. else
  922. return false;
  923. // X > C ? X : C+1 --> X < C+1 ? C+1 : X
  924. // X < C ? X : C-1 --> X > C-1 ? C-1 : X
  925. if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
  926. (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) {
  927. ; // Nothing to do here. Values match without any sign/zero extension.
  928. }
  929. // Types do not match. Instead of calculating this with mixed types, promote
  930. // all to the larger type. This enables scalar evolution to analyze this
  931. // expression.
  932. else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) {
  933. Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy);
  934. // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X
  935. // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X
  936. // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X
  937. // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X
  938. if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) {
  939. CmpLHS = TrueVal;
  940. AdjustedRHS = SextRHS;
  941. } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) &&
  942. SextRHS == TrueVal) {
  943. CmpLHS = FalseVal;
  944. AdjustedRHS = SextRHS;
  945. } else if (Cmp.isUnsigned()) {
  946. Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy);
  947. // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X
  948. // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X
  949. // zext + signed compare cannot be changed:
  950. // 0xff <s 0x00, but 0x00ff >s 0x0000
  951. if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) {
  952. CmpLHS = TrueVal;
  953. AdjustedRHS = ZextRHS;
  954. } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) &&
  955. ZextRHS == TrueVal) {
  956. CmpLHS = FalseVal;
  957. AdjustedRHS = ZextRHS;
  958. } else {
  959. return false;
  960. }
  961. } else {
  962. return false;
  963. }
  964. } else {
  965. return false;
  966. }
  967. Pred = ICmpInst::getSwappedPredicate(Pred);
  968. CmpRHS = AdjustedRHS;
  969. std::swap(FalseVal, TrueVal);
  970. Cmp.setPredicate(Pred);
  971. Cmp.setOperand(0, CmpLHS);
  972. Cmp.setOperand(1, CmpRHS);
  973. Sel.setOperand(1, TrueVal);
  974. Sel.setOperand(2, FalseVal);
  975. Sel.swapProfMetadata();
  976. // Move the compare instruction right before the select instruction. Otherwise
  977. // the sext/zext value may be defined after the compare instruction uses it.
  978. Cmp.moveBefore(&Sel);
  979. return true;
  980. }
  981. /// If this is an integer min/max (icmp + select) with a constant operand,
  982. /// create the canonical icmp for the min/max operation and canonicalize the
  983. /// constant to the 'false' operand of the select:
  984. /// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2
  985. /// Note: if C1 != C2, this will change the icmp constant to the existing
  986. /// constant operand of the select.
  987. static Instruction *canonicalizeMinMaxWithConstant(SelectInst &Sel,
  988. ICmpInst &Cmp,
  989. InstCombinerImpl &IC) {
  990. if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1)))
  991. return nullptr;
  992. // Canonicalize the compare predicate based on whether we have min or max.
  993. Value *LHS, *RHS;
  994. SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS);
  995. if (!SelectPatternResult::isMinOrMax(SPR.Flavor))
  996. return nullptr;
  997. // Is this already canonical?
  998. ICmpInst::Predicate CanonicalPred = getMinMaxPred(SPR.Flavor);
  999. if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS &&
  1000. Cmp.getPredicate() == CanonicalPred)
  1001. return nullptr;
  1002. // Bail out on unsimplified X-0 operand (due to some worklist management bug),
  1003. // as this may cause an infinite combine loop. Let the sub be folded first.
  1004. if (match(LHS, m_Sub(m_Value(), m_Zero())) ||
  1005. match(RHS, m_Sub(m_Value(), m_Zero())))
  1006. return nullptr;
  1007. // Create the canonical compare and plug it into the select.
  1008. IC.replaceOperand(Sel, 0, IC.Builder.CreateICmp(CanonicalPred, LHS, RHS));
  1009. // If the select operands did not change, we're done.
  1010. if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS)
  1011. return &Sel;
  1012. // If we are swapping the select operands, swap the metadata too.
  1013. assert(Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS &&
  1014. "Unexpected results from matchSelectPattern");
  1015. Sel.swapValues();
  1016. Sel.swapProfMetadata();
  1017. return &Sel;
  1018. }
  1019. static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp,
  1020. InstCombinerImpl &IC) {
  1021. if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1)))
  1022. return nullptr;
  1023. Value *LHS, *RHS;
  1024. SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor;
  1025. if (SPF != SelectPatternFlavor::SPF_ABS &&
  1026. SPF != SelectPatternFlavor::SPF_NABS)
  1027. return nullptr;
  1028. // Note that NSW flag can only be propagated for normal, non-negated abs!
  1029. bool IntMinIsPoison = SPF == SelectPatternFlavor::SPF_ABS &&
  1030. match(RHS, m_NSWNeg(m_Specific(LHS)));
  1031. Constant *IntMinIsPoisonC =
  1032. ConstantInt::get(Type::getInt1Ty(Sel.getContext()), IntMinIsPoison);
  1033. Instruction *Abs =
  1034. IC.Builder.CreateBinaryIntrinsic(Intrinsic::abs, LHS, IntMinIsPoisonC);
  1035. if (SPF == SelectPatternFlavor::SPF_NABS)
  1036. return BinaryOperator::CreateNeg(Abs); // Always without NSW flag!
  1037. return IC.replaceInstUsesWith(Sel, Abs);
  1038. }
  1039. /// If we have a select with an equality comparison, then we know the value in
  1040. /// one of the arms of the select. See if substituting this value into an arm
  1041. /// and simplifying the result yields the same value as the other arm.
  1042. ///
  1043. /// To make this transform safe, we must drop poison-generating flags
  1044. /// (nsw, etc) if we simplified to a binop because the select may be guarding
  1045. /// that poison from propagating. If the existing binop already had no
  1046. /// poison-generating flags, then this transform can be done by instsimplify.
  1047. ///
  1048. /// Consider:
  1049. /// %cmp = icmp eq i32 %x, 2147483647
  1050. /// %add = add nsw i32 %x, 1
  1051. /// %sel = select i1 %cmp, i32 -2147483648, i32 %add
  1052. ///
  1053. /// We can't replace %sel with %add unless we strip away the flags.
  1054. /// TODO: Wrapping flags could be preserved in some cases with better analysis.
  1055. Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
  1056. ICmpInst &Cmp) {
  1057. // Value equivalence substitution requires an all-or-nothing replacement.
  1058. // It does not make sense for a vector compare where each lane is chosen
  1059. // independently.
  1060. if (!Cmp.isEquality() || Cmp.getType()->isVectorTy())
  1061. return nullptr;
  1062. // Canonicalize the pattern to ICMP_EQ by swapping the select operands.
  1063. Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
  1064. bool Swapped = false;
  1065. if (Cmp.getPredicate() == ICmpInst::ICMP_NE) {
  1066. std::swap(TrueVal, FalseVal);
  1067. Swapped = true;
  1068. }
  1069. // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
  1070. // Make sure Y cannot be undef though, as we might pick different values for
  1071. // undef in the icmp and in f(Y). Additionally, take care to avoid replacing
  1072. // X == Y ? X : Z with X == Y ? Y : Z, as that would lead to an infinite
  1073. // replacement cycle.
  1074. Value *CmpLHS = Cmp.getOperand(0), *CmpRHS = Cmp.getOperand(1);
  1075. if (TrueVal != CmpLHS &&
  1076. isGuaranteedNotToBeUndefOrPoison(CmpRHS, SQ.AC, &Sel, &DT)) {
  1077. if (Value *V = simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, SQ,
  1078. /* AllowRefinement */ true))
  1079. return replaceOperand(Sel, Swapped ? 2 : 1, V);
  1080. // Even if TrueVal does not simplify, we can directly replace a use of
  1081. // CmpLHS with CmpRHS, as long as the instruction is not used anywhere
  1082. // else and is safe to speculatively execute (we may end up executing it
  1083. // with different operands, which should not cause side-effects or trigger
  1084. // undefined behavior). Only do this if CmpRHS is a constant, as
  1085. // profitability is not clear for other cases.
  1086. // FIXME: The replacement could be performed recursively.
  1087. if (match(CmpRHS, m_ImmConstant()) && !match(CmpLHS, m_ImmConstant()))
  1088. if (auto *I = dyn_cast<Instruction>(TrueVal))
  1089. if (I->hasOneUse() && isSafeToSpeculativelyExecute(I))
  1090. for (Use &U : I->operands())
  1091. if (U == CmpLHS) {
  1092. replaceUse(U, CmpRHS);
  1093. return &Sel;
  1094. }
  1095. }
  1096. if (TrueVal != CmpRHS &&
  1097. isGuaranteedNotToBeUndefOrPoison(CmpLHS, SQ.AC, &Sel, &DT))
  1098. if (Value *V = simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, SQ,
  1099. /* AllowRefinement */ true))
  1100. return replaceOperand(Sel, Swapped ? 2 : 1, V);
  1101. auto *FalseInst = dyn_cast<Instruction>(FalseVal);
  1102. if (!FalseInst)
  1103. return nullptr;
  1104. // InstSimplify already performed this fold if it was possible subject to
  1105. // current poison-generating flags. Try the transform again with
  1106. // poison-generating flags temporarily dropped.
  1107. bool WasNUW = false, WasNSW = false, WasExact = false, WasInBounds = false;
  1108. if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(FalseVal)) {
  1109. WasNUW = OBO->hasNoUnsignedWrap();
  1110. WasNSW = OBO->hasNoSignedWrap();
  1111. FalseInst->setHasNoUnsignedWrap(false);
  1112. FalseInst->setHasNoSignedWrap(false);
  1113. }
  1114. if (auto *PEO = dyn_cast<PossiblyExactOperator>(FalseVal)) {
  1115. WasExact = PEO->isExact();
  1116. FalseInst->setIsExact(false);
  1117. }
  1118. if (auto *GEP = dyn_cast<GetElementPtrInst>(FalseVal)) {
  1119. WasInBounds = GEP->isInBounds();
  1120. GEP->setIsInBounds(false);
  1121. }
  1122. // Try each equivalence substitution possibility.
  1123. // We have an 'EQ' comparison, so the select's false value will propagate.
  1124. // Example:
  1125. // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
  1126. if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, SQ,
  1127. /* AllowRefinement */ false) == TrueVal ||
  1128. simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, SQ,
  1129. /* AllowRefinement */ false) == TrueVal) {
  1130. return replaceInstUsesWith(Sel, FalseVal);
  1131. }
  1132. // Restore poison-generating flags if the transform did not apply.
  1133. if (WasNUW)
  1134. FalseInst->setHasNoUnsignedWrap();
  1135. if (WasNSW)
  1136. FalseInst->setHasNoSignedWrap();
  1137. if (WasExact)
  1138. FalseInst->setIsExact();
  1139. if (WasInBounds)
  1140. cast<GetElementPtrInst>(FalseInst)->setIsInBounds();
  1141. return nullptr;
  1142. }
  1143. // See if this is a pattern like:
  1144. // %old_cmp1 = icmp slt i32 %x, C2
  1145. // %old_replacement = select i1 %old_cmp1, i32 %target_low, i32 %target_high
  1146. // %old_x_offseted = add i32 %x, C1
  1147. // %old_cmp0 = icmp ult i32 %old_x_offseted, C0
  1148. // %r = select i1 %old_cmp0, i32 %x, i32 %old_replacement
  1149. // This can be rewritten as more canonical pattern:
  1150. // %new_cmp1 = icmp slt i32 %x, -C1
  1151. // %new_cmp2 = icmp sge i32 %x, C0-C1
  1152. // %new_clamped_low = select i1 %new_cmp1, i32 %target_low, i32 %x
  1153. // %r = select i1 %new_cmp2, i32 %target_high, i32 %new_clamped_low
  1154. // Iff -C1 s<= C2 s<= C0-C1
  1155. // Also ULT predicate can also be UGT iff C0 != -1 (+invert result)
  1156. // SLT predicate can also be SGT iff C2 != INT_MAX (+invert res.)
  1157. static Value *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
  1158. InstCombiner::BuilderTy &Builder) {
  1159. Value *X = Sel0.getTrueValue();
  1160. Value *Sel1 = Sel0.getFalseValue();
  1161. // First match the condition of the outermost select.
  1162. // Said condition must be one-use.
  1163. if (!Cmp0.hasOneUse())
  1164. return nullptr;
  1165. ICmpInst::Predicate Pred0 = Cmp0.getPredicate();
  1166. Value *Cmp00 = Cmp0.getOperand(0);
  1167. Constant *C0;
  1168. if (!match(Cmp0.getOperand(1),
  1169. m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0))))
  1170. return nullptr;
  1171. if (!isa<SelectInst>(Sel1)) {
  1172. Pred0 = ICmpInst::getInversePredicate(Pred0);
  1173. std::swap(X, Sel1);
  1174. }
  1175. // Canonicalize Cmp0 into ult or uge.
  1176. // FIXME: we shouldn't care about lanes that are 'undef' in the end?
  1177. switch (Pred0) {
  1178. case ICmpInst::Predicate::ICMP_ULT:
  1179. case ICmpInst::Predicate::ICMP_UGE:
  1180. // Although icmp ult %x, 0 is an unusual thing to try and should generally
  1181. // have been simplified, it does not verify with undef inputs so ensure we
  1182. // are not in a strange state.
  1183. if (!match(C0, m_SpecificInt_ICMP(
  1184. ICmpInst::Predicate::ICMP_NE,
  1185. APInt::getZero(C0->getType()->getScalarSizeInBits()))))
  1186. return nullptr;
  1187. break; // Great!
  1188. case ICmpInst::Predicate::ICMP_ULE:
  1189. case ICmpInst::Predicate::ICMP_UGT:
  1190. // We want to canonicalize it to 'ult' or 'uge', so we'll need to increment
  1191. // C0, which again means it must not have any all-ones elements.
  1192. if (!match(C0,
  1193. m_SpecificInt_ICMP(
  1194. ICmpInst::Predicate::ICMP_NE,
  1195. APInt::getAllOnes(C0->getType()->getScalarSizeInBits()))))
  1196. return nullptr; // Can't do, have all-ones element[s].
  1197. C0 = InstCombiner::AddOne(C0);
  1198. break;
  1199. default:
  1200. return nullptr; // Unknown predicate.
  1201. }
  1202. // Now that we've canonicalized the ICmp, we know the X we expect;
  1203. // the select in other hand should be one-use.
  1204. if (!Sel1->hasOneUse())
  1205. return nullptr;
  1206. // If the types do not match, look through any truncs to the underlying
  1207. // instruction.
  1208. if (Cmp00->getType() != X->getType() && X->hasOneUse())
  1209. match(X, m_TruncOrSelf(m_Value(X)));
  1210. // We now can finish matching the condition of the outermost select:
  1211. // it should either be the X itself, or an addition of some constant to X.
  1212. Constant *C1;
  1213. if (Cmp00 == X)
  1214. C1 = ConstantInt::getNullValue(X->getType());
  1215. else if (!match(Cmp00,
  1216. m_Add(m_Specific(X),
  1217. m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C1)))))
  1218. return nullptr;
  1219. Value *Cmp1;
  1220. ICmpInst::Predicate Pred1;
  1221. Constant *C2;
  1222. Value *ReplacementLow, *ReplacementHigh;
  1223. if (!match(Sel1, m_Select(m_Value(Cmp1), m_Value(ReplacementLow),
  1224. m_Value(ReplacementHigh))) ||
  1225. !match(Cmp1,
  1226. m_ICmp(Pred1, m_Specific(X),
  1227. m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C2)))))
  1228. return nullptr;
  1229. if (!Cmp1->hasOneUse() && (Cmp00 == X || !Cmp00->hasOneUse()))
  1230. return nullptr; // Not enough one-use instructions for the fold.
  1231. // FIXME: this restriction could be relaxed if Cmp1 can be reused as one of
  1232. // two comparisons we'll need to build.
  1233. // Canonicalize Cmp1 into the form we expect.
  1234. // FIXME: we shouldn't care about lanes that are 'undef' in the end?
  1235. switch (Pred1) {
  1236. case ICmpInst::Predicate::ICMP_SLT:
  1237. break;
  1238. case ICmpInst::Predicate::ICMP_SLE:
  1239. // We'd have to increment C2 by one, and for that it must not have signed
  1240. // max element, but then it would have been canonicalized to 'slt' before
  1241. // we get here. So we can't do anything useful with 'sle'.
  1242. return nullptr;
  1243. case ICmpInst::Predicate::ICMP_SGT:
  1244. // We want to canonicalize it to 'slt', so we'll need to increment C2,
  1245. // which again means it must not have any signed max elements.
  1246. if (!match(C2,
  1247. m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_NE,
  1248. APInt::getSignedMaxValue(
  1249. C2->getType()->getScalarSizeInBits()))))
  1250. return nullptr; // Can't do, have signed max element[s].
  1251. C2 = InstCombiner::AddOne(C2);
  1252. LLVM_FALLTHROUGH;
  1253. case ICmpInst::Predicate::ICMP_SGE:
  1254. // Also non-canonical, but here we don't need to change C2,
  1255. // so we don't have any restrictions on C2, so we can just handle it.
  1256. std::swap(ReplacementLow, ReplacementHigh);
  1257. break;
  1258. default:
  1259. return nullptr; // Unknown predicate.
  1260. }
  1261. // The thresholds of this clamp-like pattern.
  1262. auto *ThresholdLowIncl = ConstantExpr::getNeg(C1);
  1263. auto *ThresholdHighExcl = ConstantExpr::getSub(C0, C1);
  1264. if (Pred0 == ICmpInst::Predicate::ICMP_UGE)
  1265. std::swap(ThresholdLowIncl, ThresholdHighExcl);
  1266. // The fold has a precondition 1: C2 s>= ThresholdLow
  1267. auto *Precond1 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SGE, C2,
  1268. ThresholdLowIncl);
  1269. if (!match(Precond1, m_One()))
  1270. return nullptr;
  1271. // The fold has a precondition 2: C2 s<= ThresholdHigh
  1272. auto *Precond2 = ConstantExpr::getICmp(ICmpInst::Predicate::ICMP_SLE, C2,
  1273. ThresholdHighExcl);
  1274. if (!match(Precond2, m_One()))
  1275. return nullptr;
  1276. // If we are matching from a truncated input, we need to sext the
  1277. // ReplacementLow and ReplacementHigh values. Only do the transform if they
  1278. // are free to extend due to being constants.
  1279. if (X->getType() != Sel0.getType()) {
  1280. Constant *LowC, *HighC;
  1281. if (!match(ReplacementLow, m_ImmConstant(LowC)) ||
  1282. !match(ReplacementHigh, m_ImmConstant(HighC)))
  1283. return nullptr;
  1284. ReplacementLow = ConstantExpr::getSExt(LowC, X->getType());
  1285. ReplacementHigh = ConstantExpr::getSExt(HighC, X->getType());
  1286. }
  1287. // All good, finally emit the new pattern.
  1288. Value *ShouldReplaceLow = Builder.CreateICmpSLT(X, ThresholdLowIncl);
  1289. Value *ShouldReplaceHigh = Builder.CreateICmpSGE(X, ThresholdHighExcl);
  1290. Value *MaybeReplacedLow =
  1291. Builder.CreateSelect(ShouldReplaceLow, ReplacementLow, X);
  1292. // Create the final select. If we looked through a truncate above, we will
  1293. // need to retruncate the result.
  1294. Value *MaybeReplacedHigh = Builder.CreateSelect(
  1295. ShouldReplaceHigh, ReplacementHigh, MaybeReplacedLow);
  1296. return Builder.CreateTrunc(MaybeReplacedHigh, Sel0.getType());
  1297. }
  1298. // If we have
  1299. // %cmp = icmp [canonical predicate] i32 %x, C0
  1300. // %r = select i1 %cmp, i32 %y, i32 C1
  1301. // Where C0 != C1 and %x may be different from %y, see if the constant that we
  1302. // will have if we flip the strictness of the predicate (i.e. without changing
  1303. // the result) is identical to the C1 in select. If it matches we can change
  1304. // original comparison to one with swapped predicate, reuse the constant,
  1305. // and swap the hands of select.
  1306. static Instruction *
  1307. tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
  1308. InstCombinerImpl &IC) {
  1309. ICmpInst::Predicate Pred;
  1310. Value *X;
  1311. Constant *C0;
  1312. if (!match(&Cmp, m_OneUse(m_ICmp(
  1313. Pred, m_Value(X),
  1314. m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C0))))))
  1315. return nullptr;
  1316. // If comparison predicate is non-relational, we won't be able to do anything.
  1317. if (ICmpInst::isEquality(Pred))
  1318. return nullptr;
  1319. // If comparison predicate is non-canonical, then we certainly won't be able
  1320. // to make it canonical; canonicalizeCmpWithConstant() already tried.
  1321. if (!InstCombiner::isCanonicalPredicate(Pred))
  1322. return nullptr;
  1323. // If the [input] type of comparison and select type are different, lets abort
  1324. // for now. We could try to compare constants with trunc/[zs]ext though.
  1325. if (C0->getType() != Sel.getType())
  1326. return nullptr;
  1327. // ULT with 'add' of a constant is canonical. See foldICmpAddConstant().
  1328. // FIXME: Are there more magic icmp predicate+constant pairs we must avoid?
  1329. // Or should we just abandon this transform entirely?
  1330. if (Pred == CmpInst::ICMP_ULT && match(X, m_Add(m_Value(), m_Constant())))
  1331. return nullptr;
  1332. Value *SelVal0, *SelVal1; // We do not care which one is from where.
  1333. match(&Sel, m_Select(m_Value(), m_Value(SelVal0), m_Value(SelVal1)));
  1334. // At least one of these values we are selecting between must be a constant
  1335. // else we'll never succeed.
  1336. if (!match(SelVal0, m_AnyIntegralConstant()) &&
  1337. !match(SelVal1, m_AnyIntegralConstant()))
  1338. return nullptr;
  1339. // Does this constant C match any of the `select` values?
  1340. auto MatchesSelectValue = [SelVal0, SelVal1](Constant *C) {
  1341. return C->isElementWiseEqual(SelVal0) || C->isElementWiseEqual(SelVal1);
  1342. };
  1343. // If C0 *already* matches true/false value of select, we are done.
  1344. if (MatchesSelectValue(C0))
  1345. return nullptr;
  1346. // Check the constant we'd have with flipped-strictness predicate.
  1347. auto FlippedStrictness =
  1348. InstCombiner::getFlippedStrictnessPredicateAndConstant(Pred, C0);
  1349. if (!FlippedStrictness)
  1350. return nullptr;
  1351. // If said constant doesn't match either, then there is no hope,
  1352. if (!MatchesSelectValue(FlippedStrictness->second))
  1353. return nullptr;
  1354. // It matched! Lets insert the new comparison just before select.
  1355. InstCombiner::BuilderTy::InsertPointGuard Guard(IC.Builder);
  1356. IC.Builder.SetInsertPoint(&Sel);
  1357. Pred = ICmpInst::getSwappedPredicate(Pred); // Yes, swapped.
  1358. Value *NewCmp = IC.Builder.CreateICmp(Pred, X, FlippedStrictness->second,
  1359. Cmp.getName() + ".inv");
  1360. IC.replaceOperand(Sel, 0, NewCmp);
  1361. Sel.swapValues();
  1362. Sel.swapProfMetadata();
  1363. return &Sel;
  1364. }
  1365. /// Visit a SelectInst that has an ICmpInst as its first operand.
  1366. Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
  1367. ICmpInst *ICI) {
  1368. if (Instruction *NewSel = foldSelectValueEquivalence(SI, *ICI))
  1369. return NewSel;
  1370. if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, *this))
  1371. return NewSel;
  1372. if (Instruction *NewAbs = canonicalizeAbsNabs(SI, *ICI, *this))
  1373. return NewAbs;
  1374. if (Value *V = canonicalizeClampLike(SI, *ICI, Builder))
  1375. return replaceInstUsesWith(SI, V);
  1376. if (Instruction *NewSel =
  1377. tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
  1378. return NewSel;
  1379. bool Changed = adjustMinMax(SI, *ICI);
  1380. if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
  1381. return replaceInstUsesWith(SI, V);
  1382. // NOTE: if we wanted to, this is where to detect integer MIN/MAX
  1383. Value *TrueVal = SI.getTrueValue();
  1384. Value *FalseVal = SI.getFalseValue();
  1385. ICmpInst::Predicate Pred = ICI->getPredicate();
  1386. Value *CmpLHS = ICI->getOperand(0);
  1387. Value *CmpRHS = ICI->getOperand(1);
  1388. if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
  1389. if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
  1390. // Transform (X == C) ? X : Y -> (X == C) ? C : Y
  1391. SI.setOperand(1, CmpRHS);
  1392. Changed = true;
  1393. } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
  1394. // Transform (X != C) ? Y : X -> (X != C) ? Y : C
  1395. SI.setOperand(2, CmpRHS);
  1396. Changed = true;
  1397. }
  1398. }
  1399. // Canonicalize a signbit condition to use zero constant by swapping:
  1400. // (CmpLHS > -1) ? TV : FV --> (CmpLHS < 0) ? FV : TV
  1401. // To avoid conflicts (infinite loops) with other canonicalizations, this is
  1402. // not applied with any constant select arm.
  1403. if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes()) &&
  1404. !match(TrueVal, m_Constant()) && !match(FalseVal, m_Constant()) &&
  1405. ICI->hasOneUse()) {
  1406. InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);
  1407. Builder.SetInsertPoint(&SI);
  1408. Value *IsNeg = Builder.CreateICmpSLT(
  1409. CmpLHS, ConstantInt::getNullValue(CmpLHS->getType()), ICI->getName());
  1410. replaceOperand(SI, 0, IsNeg);
  1411. SI.swapValues();
  1412. SI.swapProfMetadata();
  1413. return &SI;
  1414. }
  1415. // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring
  1416. // decomposeBitTestICmp() might help.
  1417. {
  1418. unsigned BitWidth =
  1419. DL.getTypeSizeInBits(TrueVal->getType()->getScalarType());
  1420. APInt MinSignedValue = APInt::getSignedMinValue(BitWidth);
  1421. Value *X;
  1422. const APInt *Y, *C;
  1423. bool TrueWhenUnset;
  1424. bool IsBitTest = false;
  1425. if (ICmpInst::isEquality(Pred) &&
  1426. match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) &&
  1427. match(CmpRHS, m_Zero())) {
  1428. IsBitTest = true;
  1429. TrueWhenUnset = Pred == ICmpInst::ICMP_EQ;
  1430. } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) {
  1431. X = CmpLHS;
  1432. Y = &MinSignedValue;
  1433. IsBitTest = true;
  1434. TrueWhenUnset = false;
  1435. } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) {
  1436. X = CmpLHS;
  1437. Y = &MinSignedValue;
  1438. IsBitTest = true;
  1439. TrueWhenUnset = true;
  1440. }
  1441. if (IsBitTest) {
  1442. Value *V = nullptr;
  1443. // (X & Y) == 0 ? X : X ^ Y --> X & ~Y
  1444. if (TrueWhenUnset && TrueVal == X &&
  1445. match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
  1446. V = Builder.CreateAnd(X, ~(*Y));
  1447. // (X & Y) != 0 ? X ^ Y : X --> X & ~Y
  1448. else if (!TrueWhenUnset && FalseVal == X &&
  1449. match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
  1450. V = Builder.CreateAnd(X, ~(*Y));
  1451. // (X & Y) == 0 ? X ^ Y : X --> X | Y
  1452. else if (TrueWhenUnset && FalseVal == X &&
  1453. match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
  1454. V = Builder.CreateOr(X, *Y);
  1455. // (X & Y) != 0 ? X : X ^ Y --> X | Y
  1456. else if (!TrueWhenUnset && TrueVal == X &&
  1457. match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C)
  1458. V = Builder.CreateOr(X, *Y);
  1459. if (V)
  1460. return replaceInstUsesWith(SI, V);
  1461. }
  1462. }
  1463. if (Instruction *V =
  1464. foldSelectICmpAndAnd(SI.getType(), ICI, TrueVal, FalseVal, Builder))
  1465. return V;
  1466. if (Instruction *V = foldSelectCtlzToCttz(ICI, TrueVal, FalseVal, Builder))
  1467. return V;
  1468. if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder))
  1469. return replaceInstUsesWith(SI, V);
  1470. if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))
  1471. return replaceInstUsesWith(SI, V);
  1472. if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
  1473. return replaceInstUsesWith(SI, V);
  1474. if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder))
  1475. return replaceInstUsesWith(SI, V);
  1476. if (Value *V = canonicalizeSaturatedAdd(ICI, TrueVal, FalseVal, Builder))
  1477. return replaceInstUsesWith(SI, V);
  1478. return Changed ? &SI : nullptr;
  1479. }
  1480. /// SI is a select whose condition is a PHI node (but the two may be in
  1481. /// different blocks). See if the true/false values (V) are live in all of the
  1482. /// predecessor blocks of the PHI. For example, cases like this can't be mapped:
  1483. ///
  1484. /// X = phi [ C1, BB1], [C2, BB2]
  1485. /// Y = add
  1486. /// Z = select X, Y, 0
  1487. ///
  1488. /// because Y is not live in BB1/BB2.
  1489. static bool canSelectOperandBeMappingIntoPredBlock(const Value *V,
  1490. const SelectInst &SI) {
  1491. // If the value is a non-instruction value like a constant or argument, it
  1492. // can always be mapped.
  1493. const Instruction *I = dyn_cast<Instruction>(V);
  1494. if (!I) return true;
  1495. // If V is a PHI node defined in the same block as the condition PHI, we can
  1496. // map the arguments.
  1497. const PHINode *CondPHI = cast<PHINode>(SI.getCondition());
  1498. if (const PHINode *VP = dyn_cast<PHINode>(I))
  1499. if (VP->getParent() == CondPHI->getParent())
  1500. return true;
  1501. // Otherwise, if the PHI and select are defined in the same block and if V is
  1502. // defined in a different block, then we can transform it.
  1503. if (SI.getParent() == CondPHI->getParent() &&
  1504. I->getParent() != CondPHI->getParent())
  1505. return true;
  1506. // Otherwise we have a 'hard' case and we can't tell without doing more
  1507. // detailed dominator based analysis, punt.
  1508. return false;
  1509. }
  1510. /// We have an SPF (e.g. a min or max) of an SPF of the form:
  1511. /// SPF2(SPF1(A, B), C)
  1512. Instruction *InstCombinerImpl::foldSPFofSPF(Instruction *Inner,
  1513. SelectPatternFlavor SPF1, Value *A,
  1514. Value *B, Instruction &Outer,
  1515. SelectPatternFlavor SPF2,
  1516. Value *C) {
  1517. if (Outer.getType() != Inner->getType())
  1518. return nullptr;
  1519. if (C == A || C == B) {
  1520. // MAX(MAX(A, B), B) -> MAX(A, B)
  1521. // MIN(MIN(a, b), a) -> MIN(a, b)
  1522. // TODO: This could be done in instsimplify.
  1523. if (SPF1 == SPF2 && SelectPatternResult::isMinOrMax(SPF1))
  1524. return replaceInstUsesWith(Outer, Inner);
  1525. // MAX(MIN(a, b), a) -> a
  1526. // MIN(MAX(a, b), a) -> a
  1527. // TODO: This could be done in instsimplify.
  1528. if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) ||
  1529. (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) ||
  1530. (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) ||
  1531. (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN))
  1532. return replaceInstUsesWith(Outer, C);
  1533. }
  1534. if (SPF1 == SPF2) {
  1535. const APInt *CB, *CC;
  1536. if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) {
  1537. // MIN(MIN(A, 23), 97) -> MIN(A, 23)
  1538. // MAX(MAX(A, 97), 23) -> MAX(A, 97)
  1539. // TODO: This could be done in instsimplify.
  1540. if ((SPF1 == SPF_UMIN && CB->ule(*CC)) ||
  1541. (SPF1 == SPF_SMIN && CB->sle(*CC)) ||
  1542. (SPF1 == SPF_UMAX && CB->uge(*CC)) ||
  1543. (SPF1 == SPF_SMAX && CB->sge(*CC)))
  1544. return replaceInstUsesWith(Outer, Inner);
  1545. // MIN(MIN(A, 97), 23) -> MIN(A, 23)
  1546. // MAX(MAX(A, 23), 97) -> MAX(A, 97)
  1547. if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) ||
  1548. (SPF1 == SPF_SMIN && CB->sgt(*CC)) ||
  1549. (SPF1 == SPF_UMAX && CB->ult(*CC)) ||
  1550. (SPF1 == SPF_SMAX && CB->slt(*CC))) {
  1551. Outer.replaceUsesOfWith(Inner, A);
  1552. return &Outer;
  1553. }
  1554. }
  1555. }
  1556. // max(max(A, B), min(A, B)) --> max(A, B)
  1557. // min(min(A, B), max(A, B)) --> min(A, B)
  1558. // TODO: This could be done in instsimplify.
  1559. if (SPF1 == SPF2 &&
  1560. ((SPF1 == SPF_UMIN && match(C, m_c_UMax(m_Specific(A), m_Specific(B)))) ||
  1561. (SPF1 == SPF_SMIN && match(C, m_c_SMax(m_Specific(A), m_Specific(B)))) ||
  1562. (SPF1 == SPF_UMAX && match(C, m_c_UMin(m_Specific(A), m_Specific(B)))) ||
  1563. (SPF1 == SPF_SMAX && match(C, m_c_SMin(m_Specific(A), m_Specific(B))))))
  1564. return replaceInstUsesWith(Outer, Inner);
  1565. // ABS(ABS(X)) -> ABS(X)
  1566. // NABS(NABS(X)) -> NABS(X)
  1567. // TODO: This could be done in instsimplify.
  1568. if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) {
  1569. return replaceInstUsesWith(Outer, Inner);
  1570. }
  1571. // ABS(NABS(X)) -> ABS(X)
  1572. // NABS(ABS(X)) -> NABS(X)
  1573. if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) ||
  1574. (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) {
  1575. SelectInst *SI = cast<SelectInst>(Inner);
  1576. Value *NewSI =
  1577. Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(),
  1578. SI->getTrueValue(), SI->getName(), SI);
  1579. return replaceInstUsesWith(Outer, NewSI);
  1580. }
  1581. auto IsFreeOrProfitableToInvert =
  1582. [&](Value *V, Value *&NotV, bool &ElidesXor) {
  1583. if (match(V, m_Not(m_Value(NotV)))) {
  1584. // If V has at most 2 uses then we can get rid of the xor operation
  1585. // entirely.
  1586. ElidesXor |= !V->hasNUsesOrMore(3);
  1587. return true;
  1588. }
  1589. if (isFreeToInvert(V, !V->hasNUsesOrMore(3))) {
  1590. NotV = nullptr;
  1591. return true;
  1592. }
  1593. return false;
  1594. };
  1595. Value *NotA, *NotB, *NotC;
  1596. bool ElidesXor = false;
  1597. // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C)
  1598. // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C)
  1599. // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C)
  1600. // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C)
  1601. //
  1602. // This transform is performance neutral if we can elide at least one xor from
  1603. // the set of three operands, since we'll be tacking on an xor at the very
  1604. // end.
  1605. if (SelectPatternResult::isMinOrMax(SPF1) &&
  1606. SelectPatternResult::isMinOrMax(SPF2) &&
  1607. IsFreeOrProfitableToInvert(A, NotA, ElidesXor) &&
  1608. IsFreeOrProfitableToInvert(B, NotB, ElidesXor) &&
  1609. IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) {
  1610. if (!NotA)
  1611. NotA = Builder.CreateNot(A);
  1612. if (!NotB)
  1613. NotB = Builder.CreateNot(B);
  1614. if (!NotC)
  1615. NotC = Builder.CreateNot(C);
  1616. Value *NewInner = createMinMax(Builder, getInverseMinMaxFlavor(SPF1), NotA,
  1617. NotB);
  1618. Value *NewOuter = Builder.CreateNot(
  1619. createMinMax(Builder, getInverseMinMaxFlavor(SPF2), NewInner, NotC));
  1620. return replaceInstUsesWith(Outer, NewOuter);
  1621. }
  1622. return nullptr;
  1623. }
  1624. /// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))).
  1625. /// This is even legal for FP.
  1626. static Instruction *foldAddSubSelect(SelectInst &SI,
  1627. InstCombiner::BuilderTy &Builder) {
  1628. Value *CondVal = SI.getCondition();
  1629. Value *TrueVal = SI.getTrueValue();
  1630. Value *FalseVal = SI.getFalseValue();
  1631. auto *TI = dyn_cast<Instruction>(TrueVal);
  1632. auto *FI = dyn_cast<Instruction>(FalseVal);
  1633. if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse())
  1634. return nullptr;
  1635. Instruction *AddOp = nullptr, *SubOp = nullptr;
  1636. if ((TI->getOpcode() == Instruction::Sub &&
  1637. FI->getOpcode() == Instruction::Add) ||
  1638. (TI->getOpcode() == Instruction::FSub &&
  1639. FI->getOpcode() == Instruction::FAdd)) {
  1640. AddOp = FI;
  1641. SubOp = TI;
  1642. } else if ((FI->getOpcode() == Instruction::Sub &&
  1643. TI->getOpcode() == Instruction::Add) ||
  1644. (FI->getOpcode() == Instruction::FSub &&
  1645. TI->getOpcode() == Instruction::FAdd)) {
  1646. AddOp = TI;
  1647. SubOp = FI;
  1648. }
  1649. if (AddOp) {
  1650. Value *OtherAddOp = nullptr;
  1651. if (SubOp->getOperand(0) == AddOp->getOperand(0)) {
  1652. OtherAddOp = AddOp->getOperand(1);
  1653. } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) {
  1654. OtherAddOp = AddOp->getOperand(0);
  1655. }
  1656. if (OtherAddOp) {
  1657. // So at this point we know we have (Y -> OtherAddOp):
  1658. // select C, (add X, Y), (sub X, Z)
  1659. Value *NegVal; // Compute -Z
  1660. if (SI.getType()->isFPOrFPVectorTy()) {
  1661. NegVal = Builder.CreateFNeg(SubOp->getOperand(1));
  1662. if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) {
  1663. FastMathFlags Flags = AddOp->getFastMathFlags();
  1664. Flags &= SubOp->getFastMathFlags();
  1665. NegInst->setFastMathFlags(Flags);
  1666. }
  1667. } else {
  1668. NegVal = Builder.CreateNeg(SubOp->getOperand(1));
  1669. }
  1670. Value *NewTrueOp = OtherAddOp;
  1671. Value *NewFalseOp = NegVal;
  1672. if (AddOp != TI)
  1673. std::swap(NewTrueOp, NewFalseOp);
  1674. Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp,
  1675. SI.getName() + ".p", &SI);
  1676. if (SI.getType()->isFPOrFPVectorTy()) {
  1677. Instruction *RI =
  1678. BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
  1679. FastMathFlags Flags = AddOp->getFastMathFlags();
  1680. Flags &= SubOp->getFastMathFlags();
  1681. RI->setFastMathFlags(Flags);
  1682. return RI;
  1683. } else
  1684. return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
  1685. }
  1686. }
  1687. return nullptr;
  1688. }
  1689. /// Turn X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
  1690. /// And X - Y overflows ? 0 : X - Y -> usub_sat X, Y
  1691. /// Along with a number of patterns similar to:
  1692. /// X + Y overflows ? (X < 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1693. /// X - Y overflows ? (X > 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1694. static Instruction *
  1695. foldOverflowingAddSubSelect(SelectInst &SI, InstCombiner::BuilderTy &Builder) {
  1696. Value *CondVal = SI.getCondition();
  1697. Value *TrueVal = SI.getTrueValue();
  1698. Value *FalseVal = SI.getFalseValue();
  1699. WithOverflowInst *II;
  1700. if (!match(CondVal, m_ExtractValue<1>(m_WithOverflowInst(II))) ||
  1701. !match(FalseVal, m_ExtractValue<0>(m_Specific(II))))
  1702. return nullptr;
  1703. Value *X = II->getLHS();
  1704. Value *Y = II->getRHS();
  1705. auto IsSignedSaturateLimit = [&](Value *Limit, bool IsAdd) {
  1706. Type *Ty = Limit->getType();
  1707. ICmpInst::Predicate Pred;
  1708. Value *TrueVal, *FalseVal, *Op;
  1709. const APInt *C;
  1710. if (!match(Limit, m_Select(m_ICmp(Pred, m_Value(Op), m_APInt(C)),
  1711. m_Value(TrueVal), m_Value(FalseVal))))
  1712. return false;
  1713. auto IsZeroOrOne = [](const APInt &C) { return C.isZero() || C.isOne(); };
  1714. auto IsMinMax = [&](Value *Min, Value *Max) {
  1715. APInt MinVal = APInt::getSignedMinValue(Ty->getScalarSizeInBits());
  1716. APInt MaxVal = APInt::getSignedMaxValue(Ty->getScalarSizeInBits());
  1717. return match(Min, m_SpecificInt(MinVal)) &&
  1718. match(Max, m_SpecificInt(MaxVal));
  1719. };
  1720. if (Op != X && Op != Y)
  1721. return false;
  1722. if (IsAdd) {
  1723. // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1724. // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1725. // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1726. // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1727. if (Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
  1728. IsMinMax(TrueVal, FalseVal))
  1729. return true;
  1730. // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1731. // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1732. // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1733. // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1734. if (Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
  1735. IsMinMax(FalseVal, TrueVal))
  1736. return true;
  1737. } else {
  1738. // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1739. // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1740. if (Op == X && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C + 1) &&
  1741. IsMinMax(TrueVal, FalseVal))
  1742. return true;
  1743. // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1744. // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1745. if (Op == X && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 2) &&
  1746. IsMinMax(FalseVal, TrueVal))
  1747. return true;
  1748. // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1749. // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1750. if (Op == Y && Pred == ICmpInst::ICMP_SLT && IsZeroOrOne(*C) &&
  1751. IsMinMax(FalseVal, TrueVal))
  1752. return true;
  1753. // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1754. // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1755. if (Op == Y && Pred == ICmpInst::ICMP_SGT && IsZeroOrOne(*C + 1) &&
  1756. IsMinMax(TrueVal, FalseVal))
  1757. return true;
  1758. }
  1759. return false;
  1760. };
  1761. Intrinsic::ID NewIntrinsicID;
  1762. if (II->getIntrinsicID() == Intrinsic::uadd_with_overflow &&
  1763. match(TrueVal, m_AllOnes()))
  1764. // X + Y overflows ? -1 : X + Y -> uadd_sat X, Y
  1765. NewIntrinsicID = Intrinsic::uadd_sat;
  1766. else if (II->getIntrinsicID() == Intrinsic::usub_with_overflow &&
  1767. match(TrueVal, m_Zero()))
  1768. // X - Y overflows ? 0 : X - Y -> usub_sat X, Y
  1769. NewIntrinsicID = Intrinsic::usub_sat;
  1770. else if (II->getIntrinsicID() == Intrinsic::sadd_with_overflow &&
  1771. IsSignedSaturateLimit(TrueVal, /*IsAdd=*/true))
  1772. // X + Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1773. // X + Y overflows ? (X <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1774. // X + Y overflows ? (X >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1775. // X + Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1776. // X + Y overflows ? (Y <s 0 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1777. // X + Y overflows ? (Y <s 1 ? INTMIN : INTMAX) : X + Y --> sadd_sat X, Y
  1778. // X + Y overflows ? (Y >s 0 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1779. // X + Y overflows ? (Y >s -1 ? INTMAX : INTMIN) : X + Y --> sadd_sat X, Y
  1780. NewIntrinsicID = Intrinsic::sadd_sat;
  1781. else if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow &&
  1782. IsSignedSaturateLimit(TrueVal, /*IsAdd=*/false))
  1783. // X - Y overflows ? (X <s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1784. // X - Y overflows ? (X <s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1785. // X - Y overflows ? (X >s -1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1786. // X - Y overflows ? (X >s -2 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1787. // X - Y overflows ? (Y <s 0 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1788. // X - Y overflows ? (Y <s 1 ? INTMAX : INTMIN) : X - Y --> ssub_sat X, Y
  1789. // X - Y overflows ? (Y >s 0 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1790. // X - Y overflows ? (Y >s -1 ? INTMIN : INTMAX) : X - Y --> ssub_sat X, Y
  1791. NewIntrinsicID = Intrinsic::ssub_sat;
  1792. else
  1793. return nullptr;
  1794. Function *F =
  1795. Intrinsic::getDeclaration(SI.getModule(), NewIntrinsicID, SI.getType());
  1796. return CallInst::Create(F, {X, Y});
  1797. }
  1798. Instruction *InstCombinerImpl::foldSelectExtConst(SelectInst &Sel) {
  1799. Constant *C;
  1800. if (!match(Sel.getTrueValue(), m_Constant(C)) &&
  1801. !match(Sel.getFalseValue(), m_Constant(C)))
  1802. return nullptr;
  1803. Instruction *ExtInst;
  1804. if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) &&
  1805. !match(Sel.getFalseValue(), m_Instruction(ExtInst)))
  1806. return nullptr;
  1807. auto ExtOpcode = ExtInst->getOpcode();
  1808. if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt)
  1809. return nullptr;
  1810. // If we are extending from a boolean type or if we can create a select that
  1811. // has the same size operands as its condition, try to narrow the select.
  1812. Value *X = ExtInst->getOperand(0);
  1813. Type *SmallType = X->getType();
  1814. Value *Cond = Sel.getCondition();
  1815. auto *Cmp = dyn_cast<CmpInst>(Cond);
  1816. if (!SmallType->isIntOrIntVectorTy(1) &&
  1817. (!Cmp || Cmp->getOperand(0)->getType() != SmallType))
  1818. return nullptr;
  1819. // If the constant is the same after truncation to the smaller type and
  1820. // extension to the original type, we can narrow the select.
  1821. Type *SelType = Sel.getType();
  1822. Constant *TruncC = ConstantExpr::getTrunc(C, SmallType);
  1823. Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType);
  1824. if (ExtC == C && ExtInst->hasOneUse()) {
  1825. Value *TruncCVal = cast<Value>(TruncC);
  1826. if (ExtInst == Sel.getFalseValue())
  1827. std::swap(X, TruncCVal);
  1828. // select Cond, (ext X), C --> ext(select Cond, X, C')
  1829. // select Cond, C, (ext X) --> ext(select Cond, C', X)
  1830. Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel);
  1831. return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType);
  1832. }
  1833. // If one arm of the select is the extend of the condition, replace that arm
  1834. // with the extension of the appropriate known bool value.
  1835. if (Cond == X) {
  1836. if (ExtInst == Sel.getTrueValue()) {
  1837. // select X, (sext X), C --> select X, -1, C
  1838. // select X, (zext X), C --> select X, 1, C
  1839. Constant *One = ConstantInt::getTrue(SmallType);
  1840. Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType);
  1841. return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel);
  1842. } else {
  1843. // select X, C, (sext X) --> select X, C, 0
  1844. // select X, C, (zext X) --> select X, C, 0
  1845. Constant *Zero = ConstantInt::getNullValue(SelType);
  1846. return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel);
  1847. }
  1848. }
  1849. return nullptr;
  1850. }
  1851. /// Try to transform a vector select with a constant condition vector into a
  1852. /// shuffle for easier combining with other shuffles and insert/extract.
  1853. static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) {
  1854. Value *CondVal = SI.getCondition();
  1855. Constant *CondC;
  1856. auto *CondValTy = dyn_cast<FixedVectorType>(CondVal->getType());
  1857. if (!CondValTy || !match(CondVal, m_Constant(CondC)))
  1858. return nullptr;
  1859. unsigned NumElts = CondValTy->getNumElements();
  1860. SmallVector<int, 16> Mask;
  1861. Mask.reserve(NumElts);
  1862. for (unsigned i = 0; i != NumElts; ++i) {
  1863. Constant *Elt = CondC->getAggregateElement(i);
  1864. if (!Elt)
  1865. return nullptr;
  1866. if (Elt->isOneValue()) {
  1867. // If the select condition element is true, choose from the 1st vector.
  1868. Mask.push_back(i);
  1869. } else if (Elt->isNullValue()) {
  1870. // If the select condition element is false, choose from the 2nd vector.
  1871. Mask.push_back(i + NumElts);
  1872. } else if (isa<UndefValue>(Elt)) {
  1873. // Undef in a select condition (choose one of the operands) does not mean
  1874. // the same thing as undef in a shuffle mask (any value is acceptable), so
  1875. // give up.
  1876. return nullptr;
  1877. } else {
  1878. // Bail out on a constant expression.
  1879. return nullptr;
  1880. }
  1881. }
  1882. return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), Mask);
  1883. }
  1884. /// If we have a select of vectors with a scalar condition, try to convert that
  1885. /// to a vector select by splatting the condition. A splat may get folded with
  1886. /// other operations in IR and having all operands of a select be vector types
  1887. /// is likely better for vector codegen.
  1888. static Instruction *canonicalizeScalarSelectOfVecs(SelectInst &Sel,
  1889. InstCombinerImpl &IC) {
  1890. auto *Ty = dyn_cast<VectorType>(Sel.getType());
  1891. if (!Ty)
  1892. return nullptr;
  1893. // We can replace a single-use extract with constant index.
  1894. Value *Cond = Sel.getCondition();
  1895. if (!match(Cond, m_OneUse(m_ExtractElt(m_Value(), m_ConstantInt()))))
  1896. return nullptr;
  1897. // select (extelt V, Index), T, F --> select (splat V, Index), T, F
  1898. // Splatting the extracted condition reduces code (we could directly create a
  1899. // splat shuffle of the source vector to eliminate the intermediate step).
  1900. return IC.replaceOperand(
  1901. Sel, 0, IC.Builder.CreateVectorSplat(Ty->getElementCount(), Cond));
  1902. }
  1903. /// Reuse bitcasted operands between a compare and select:
  1904. /// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
  1905. /// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D))
  1906. static Instruction *foldSelectCmpBitcasts(SelectInst &Sel,
  1907. InstCombiner::BuilderTy &Builder) {
  1908. Value *Cond = Sel.getCondition();
  1909. Value *TVal = Sel.getTrueValue();
  1910. Value *FVal = Sel.getFalseValue();
  1911. CmpInst::Predicate Pred;
  1912. Value *A, *B;
  1913. if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B))))
  1914. return nullptr;
  1915. // The select condition is a compare instruction. If the select's true/false
  1916. // values are already the same as the compare operands, there's nothing to do.
  1917. if (TVal == A || TVal == B || FVal == A || FVal == B)
  1918. return nullptr;
  1919. Value *C, *D;
  1920. if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D))))
  1921. return nullptr;
  1922. // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc)
  1923. Value *TSrc, *FSrc;
  1924. if (!match(TVal, m_BitCast(m_Value(TSrc))) ||
  1925. !match(FVal, m_BitCast(m_Value(FSrc))))
  1926. return nullptr;
  1927. // If the select true/false values are *different bitcasts* of the same source
  1928. // operands, make the select operands the same as the compare operands and
  1929. // cast the result. This is the canonical select form for min/max.
  1930. Value *NewSel;
  1931. if (TSrc == C && FSrc == D) {
  1932. // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) -->
  1933. // bitcast (select (cmp A, B), A, B)
  1934. NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel);
  1935. } else if (TSrc == D && FSrc == C) {
  1936. // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) -->
  1937. // bitcast (select (cmp A, B), B, A)
  1938. NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel);
  1939. } else {
  1940. return nullptr;
  1941. }
  1942. return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType());
  1943. }
  1944. /// Try to eliminate select instructions that test the returned flag of cmpxchg
  1945. /// instructions.
  1946. ///
  1947. /// If a select instruction tests the returned flag of a cmpxchg instruction and
  1948. /// selects between the returned value of the cmpxchg instruction its compare
  1949. /// operand, the result of the select will always be equal to its false value.
  1950. /// For example:
  1951. ///
  1952. /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
  1953. /// %1 = extractvalue { i64, i1 } %0, 1
  1954. /// %2 = extractvalue { i64, i1 } %0, 0
  1955. /// %3 = select i1 %1, i64 %compare, i64 %2
  1956. /// ret i64 %3
  1957. ///
  1958. /// The returned value of the cmpxchg instruction (%2) is the original value
  1959. /// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2
  1960. /// must have been equal to %compare. Thus, the result of the select is always
  1961. /// equal to %2, and the code can be simplified to:
  1962. ///
  1963. /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst
  1964. /// %1 = extractvalue { i64, i1 } %0, 0
  1965. /// ret i64 %1
  1966. ///
  1967. static Value *foldSelectCmpXchg(SelectInst &SI) {
  1968. // A helper that determines if V is an extractvalue instruction whose
  1969. // aggregate operand is a cmpxchg instruction and whose single index is equal
  1970. // to I. If such conditions are true, the helper returns the cmpxchg
  1971. // instruction; otherwise, a nullptr is returned.
  1972. auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * {
  1973. auto *Extract = dyn_cast<ExtractValueInst>(V);
  1974. if (!Extract)
  1975. return nullptr;
  1976. if (Extract->getIndices()[0] != I)
  1977. return nullptr;
  1978. return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand());
  1979. };
  1980. // If the select has a single user, and this user is a select instruction that
  1981. // we can simplify, skip the cmpxchg simplification for now.
  1982. if (SI.hasOneUse())
  1983. if (auto *Select = dyn_cast<SelectInst>(SI.user_back()))
  1984. if (Select->getCondition() == SI.getCondition())
  1985. if (Select->getFalseValue() == SI.getTrueValue() ||
  1986. Select->getTrueValue() == SI.getFalseValue())
  1987. return nullptr;
  1988. // Ensure the select condition is the returned flag of a cmpxchg instruction.
  1989. auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1);
  1990. if (!CmpXchg)
  1991. return nullptr;
  1992. // Check the true value case: The true value of the select is the returned
  1993. // value of the same cmpxchg used by the condition, and the false value is the
  1994. // cmpxchg instruction's compare operand.
  1995. if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0))
  1996. if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue())
  1997. return SI.getFalseValue();
  1998. // Check the false value case: The false value of the select is the returned
  1999. // value of the same cmpxchg used by the condition, and the true value is the
  2000. // cmpxchg instruction's compare operand.
  2001. if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0))
  2002. if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue())
  2003. return SI.getFalseValue();
  2004. return nullptr;
  2005. }
  2006. static Instruction *moveAddAfterMinMax(SelectPatternFlavor SPF, Value *X,
  2007. Value *Y,
  2008. InstCombiner::BuilderTy &Builder) {
  2009. assert(SelectPatternResult::isMinOrMax(SPF) && "Expected min/max pattern");
  2010. bool IsUnsigned = SPF == SelectPatternFlavor::SPF_UMIN ||
  2011. SPF == SelectPatternFlavor::SPF_UMAX;
  2012. // TODO: If InstSimplify could fold all cases where C2 <= C1, we could change
  2013. // the constant value check to an assert.
  2014. Value *A;
  2015. const APInt *C1, *C2;
  2016. if (IsUnsigned && match(X, m_NUWAdd(m_Value(A), m_APInt(C1))) &&
  2017. match(Y, m_APInt(C2)) && C2->uge(*C1) && X->hasNUses(2)) {
  2018. // umin (add nuw A, C1), C2 --> add nuw (umin A, C2 - C1), C1
  2019. // umax (add nuw A, C1), C2 --> add nuw (umax A, C2 - C1), C1
  2020. Value *NewMinMax = createMinMax(Builder, SPF, A,
  2021. ConstantInt::get(X->getType(), *C2 - *C1));
  2022. return BinaryOperator::CreateNUW(BinaryOperator::Add, NewMinMax,
  2023. ConstantInt::get(X->getType(), *C1));
  2024. }
  2025. if (!IsUnsigned && match(X, m_NSWAdd(m_Value(A), m_APInt(C1))) &&
  2026. match(Y, m_APInt(C2)) && X->hasNUses(2)) {
  2027. bool Overflow;
  2028. APInt Diff = C2->ssub_ov(*C1, Overflow);
  2029. if (!Overflow) {
  2030. // smin (add nsw A, C1), C2 --> add nsw (smin A, C2 - C1), C1
  2031. // smax (add nsw A, C1), C2 --> add nsw (smax A, C2 - C1), C1
  2032. Value *NewMinMax = createMinMax(Builder, SPF, A,
  2033. ConstantInt::get(X->getType(), Diff));
  2034. return BinaryOperator::CreateNSW(BinaryOperator::Add, NewMinMax,
  2035. ConstantInt::get(X->getType(), *C1));
  2036. }
  2037. }
  2038. return nullptr;
  2039. }
  2040. /// Match a sadd_sat or ssub_sat which is using min/max to clamp the value.
  2041. Instruction *InstCombinerImpl::matchSAddSubSat(Instruction &MinMax1) {
  2042. Type *Ty = MinMax1.getType();
  2043. // We are looking for a tree of:
  2044. // max(INT_MIN, min(INT_MAX, add(sext(A), sext(B))))
  2045. // Where the min and max could be reversed
  2046. Instruction *MinMax2;
  2047. BinaryOperator *AddSub;
  2048. const APInt *MinValue, *MaxValue;
  2049. if (match(&MinMax1, m_SMin(m_Instruction(MinMax2), m_APInt(MaxValue)))) {
  2050. if (!match(MinMax2, m_SMax(m_BinOp(AddSub), m_APInt(MinValue))))
  2051. return nullptr;
  2052. } else if (match(&MinMax1,
  2053. m_SMax(m_Instruction(MinMax2), m_APInt(MinValue)))) {
  2054. if (!match(MinMax2, m_SMin(m_BinOp(AddSub), m_APInt(MaxValue))))
  2055. return nullptr;
  2056. } else
  2057. return nullptr;
  2058. // Check that the constants clamp a saturate, and that the new type would be
  2059. // sensible to convert to.
  2060. if (!(*MaxValue + 1).isPowerOf2() || -*MinValue != *MaxValue + 1)
  2061. return nullptr;
  2062. // In what bitwidth can this be treated as saturating arithmetics?
  2063. unsigned NewBitWidth = (*MaxValue + 1).logBase2() + 1;
  2064. // FIXME: This isn't quite right for vectors, but using the scalar type is a
  2065. // good first approximation for what should be done there.
  2066. if (!shouldChangeType(Ty->getScalarType()->getIntegerBitWidth(), NewBitWidth))
  2067. return nullptr;
  2068. // Also make sure that the number of uses is as expected. The 3 is for the
  2069. // the two items of the compare and the select, or 2 from a min/max.
  2070. unsigned ExpUses = isa<IntrinsicInst>(MinMax1) ? 2 : 3;
  2071. if (MinMax2->hasNUsesOrMore(ExpUses) || AddSub->hasNUsesOrMore(ExpUses))
  2072. return nullptr;
  2073. // Create the new type (which can be a vector type)
  2074. Type *NewTy = Ty->getWithNewBitWidth(NewBitWidth);
  2075. Intrinsic::ID IntrinsicID;
  2076. if (AddSub->getOpcode() == Instruction::Add)
  2077. IntrinsicID = Intrinsic::sadd_sat;
  2078. else if (AddSub->getOpcode() == Instruction::Sub)
  2079. IntrinsicID = Intrinsic::ssub_sat;
  2080. else
  2081. return nullptr;
  2082. // The two operands of the add/sub must be nsw-truncatable to the NewTy. This
  2083. // is usually achieved via a sext from a smaller type.
  2084. if (ComputeMaxSignificantBits(AddSub->getOperand(0), 0, AddSub) >
  2085. NewBitWidth ||
  2086. ComputeMaxSignificantBits(AddSub->getOperand(1), 0, AddSub) > NewBitWidth)
  2087. return nullptr;
  2088. // Finally create and return the sat intrinsic, truncated to the new type
  2089. Function *F = Intrinsic::getDeclaration(MinMax1.getModule(), IntrinsicID, NewTy);
  2090. Value *AT = Builder.CreateTrunc(AddSub->getOperand(0), NewTy);
  2091. Value *BT = Builder.CreateTrunc(AddSub->getOperand(1), NewTy);
  2092. Value *Sat = Builder.CreateCall(F, {AT, BT});
  2093. return CastInst::Create(Instruction::SExt, Sat, Ty);
  2094. }
  2095. /// Reduce a sequence of min/max with a common operand.
  2096. static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS,
  2097. Value *RHS,
  2098. InstCombiner::BuilderTy &Builder) {
  2099. assert(SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max");
  2100. // TODO: Allow FP min/max with nnan/nsz.
  2101. if (!LHS->getType()->isIntOrIntVectorTy())
  2102. return nullptr;
  2103. // Match 3 of the same min/max ops. Example: umin(umin(), umin()).
  2104. Value *A, *B, *C, *D;
  2105. SelectPatternResult L = matchSelectPattern(LHS, A, B);
  2106. SelectPatternResult R = matchSelectPattern(RHS, C, D);
  2107. if (SPF != L.Flavor || L.Flavor != R.Flavor)
  2108. return nullptr;
  2109. // Look for a common operand. The use checks are different than usual because
  2110. // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by
  2111. // the select.
  2112. Value *MinMaxOp = nullptr;
  2113. Value *ThirdOp = nullptr;
  2114. if (!LHS->hasNUsesOrMore(3) && RHS->hasNUsesOrMore(3)) {
  2115. // If the LHS is only used in this chain and the RHS is used outside of it,
  2116. // reuse the RHS min/max because that will eliminate the LHS.
  2117. if (D == A || C == A) {
  2118. // min(min(a, b), min(c, a)) --> min(min(c, a), b)
  2119. // min(min(a, b), min(a, d)) --> min(min(a, d), b)
  2120. MinMaxOp = RHS;
  2121. ThirdOp = B;
  2122. } else if (D == B || C == B) {
  2123. // min(min(a, b), min(c, b)) --> min(min(c, b), a)
  2124. // min(min(a, b), min(b, d)) --> min(min(b, d), a)
  2125. MinMaxOp = RHS;
  2126. ThirdOp = A;
  2127. }
  2128. } else if (!RHS->hasNUsesOrMore(3)) {
  2129. // Reuse the LHS. This will eliminate the RHS.
  2130. if (D == A || D == B) {
  2131. // min(min(a, b), min(c, a)) --> min(min(a, b), c)
  2132. // min(min(a, b), min(c, b)) --> min(min(a, b), c)
  2133. MinMaxOp = LHS;
  2134. ThirdOp = C;
  2135. } else if (C == A || C == B) {
  2136. // min(min(a, b), min(b, d)) --> min(min(a, b), d)
  2137. // min(min(a, b), min(c, b)) --> min(min(a, b), d)
  2138. MinMaxOp = LHS;
  2139. ThirdOp = D;
  2140. }
  2141. }
  2142. if (!MinMaxOp || !ThirdOp)
  2143. return nullptr;
  2144. CmpInst::Predicate P = getMinMaxPred(SPF);
  2145. Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp);
  2146. return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp);
  2147. }
  2148. /// Try to reduce a funnel/rotate pattern that includes a compare and select
  2149. /// into a funnel shift intrinsic. Example:
  2150. /// rotl32(a, b) --> (b == 0 ? a : ((a >> (32 - b)) | (a << b)))
  2151. /// --> call llvm.fshl.i32(a, a, b)
  2152. /// fshl32(a, b, c) --> (c == 0 ? a : ((b >> (32 - c)) | (a << c)))
  2153. /// --> call llvm.fshl.i32(a, b, c)
  2154. /// fshr32(a, b, c) --> (c == 0 ? b : ((a >> (32 - c)) | (b << c)))
  2155. /// --> call llvm.fshr.i32(a, b, c)
  2156. static Instruction *foldSelectFunnelShift(SelectInst &Sel,
  2157. InstCombiner::BuilderTy &Builder) {
  2158. // This must be a power-of-2 type for a bitmasking transform to be valid.
  2159. unsigned Width = Sel.getType()->getScalarSizeInBits();
  2160. if (!isPowerOf2_32(Width))
  2161. return nullptr;
  2162. BinaryOperator *Or0, *Or1;
  2163. if (!match(Sel.getFalseValue(), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1)))))
  2164. return nullptr;
  2165. Value *SV0, *SV1, *SA0, *SA1;
  2166. if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(SV0),
  2167. m_ZExtOrSelf(m_Value(SA0))))) ||
  2168. !match(Or1, m_OneUse(m_LogicalShift(m_Value(SV1),
  2169. m_ZExtOrSelf(m_Value(SA1))))) ||
  2170. Or0->getOpcode() == Or1->getOpcode())
  2171. return nullptr;
  2172. // Canonicalize to or(shl(SV0, SA0), lshr(SV1, SA1)).
  2173. if (Or0->getOpcode() == BinaryOperator::LShr) {
  2174. std::swap(Or0, Or1);
  2175. std::swap(SV0, SV1);
  2176. std::swap(SA0, SA1);
  2177. }
  2178. assert(Or0->getOpcode() == BinaryOperator::Shl &&
  2179. Or1->getOpcode() == BinaryOperator::LShr &&
  2180. "Illegal or(shift,shift) pair");
  2181. // Check the shift amounts to see if they are an opposite pair.
  2182. Value *ShAmt;
  2183. if (match(SA1, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA0)))))
  2184. ShAmt = SA0;
  2185. else if (match(SA0, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(SA1)))))
  2186. ShAmt = SA1;
  2187. else
  2188. return nullptr;
  2189. // We should now have this pattern:
  2190. // select ?, TVal, (or (shl SV0, SA0), (lshr SV1, SA1))
  2191. // The false value of the select must be a funnel-shift of the true value:
  2192. // IsFShl -> TVal must be SV0 else TVal must be SV1.
  2193. bool IsFshl = (ShAmt == SA0);
  2194. Value *TVal = Sel.getTrueValue();
  2195. if ((IsFshl && TVal != SV0) || (!IsFshl && TVal != SV1))
  2196. return nullptr;
  2197. // Finally, see if the select is filtering out a shift-by-zero.
  2198. Value *Cond = Sel.getCondition();
  2199. ICmpInst::Predicate Pred;
  2200. if (!match(Cond, m_OneUse(m_ICmp(Pred, m_Specific(ShAmt), m_ZeroInt()))) ||
  2201. Pred != ICmpInst::ICMP_EQ)
  2202. return nullptr;
  2203. // If this is not a rotate then the select was blocking poison from the
  2204. // 'shift-by-zero' non-TVal, but a funnel shift won't - so freeze it.
  2205. if (SV0 != SV1) {
  2206. if (IsFshl && !llvm::isGuaranteedNotToBePoison(SV1))
  2207. SV1 = Builder.CreateFreeze(SV1);
  2208. else if (!IsFshl && !llvm::isGuaranteedNotToBePoison(SV0))
  2209. SV0 = Builder.CreateFreeze(SV0);
  2210. }
  2211. // This is a funnel/rotate that avoids shift-by-bitwidth UB in a suboptimal way.
  2212. // Convert to funnel shift intrinsic.
  2213. Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr;
  2214. Function *F = Intrinsic::getDeclaration(Sel.getModule(), IID, Sel.getType());
  2215. ShAmt = Builder.CreateZExt(ShAmt, Sel.getType());
  2216. return CallInst::Create(F, { SV0, SV1, ShAmt });
  2217. }
  2218. static Instruction *foldSelectToCopysign(SelectInst &Sel,
  2219. InstCombiner::BuilderTy &Builder) {
  2220. Value *Cond = Sel.getCondition();
  2221. Value *TVal = Sel.getTrueValue();
  2222. Value *FVal = Sel.getFalseValue();
  2223. Type *SelType = Sel.getType();
  2224. // Match select ?, TC, FC where the constants are equal but negated.
  2225. // TODO: Generalize to handle a negated variable operand?
  2226. const APFloat *TC, *FC;
  2227. if (!match(TVal, m_APFloat(TC)) || !match(FVal, m_APFloat(FC)) ||
  2228. !abs(*TC).bitwiseIsEqual(abs(*FC)))
  2229. return nullptr;
  2230. assert(TC != FC && "Expected equal select arms to simplify");
  2231. Value *X;
  2232. const APInt *C;
  2233. bool IsTrueIfSignSet;
  2234. ICmpInst::Predicate Pred;
  2235. if (!match(Cond, m_OneUse(m_ICmp(Pred, m_BitCast(m_Value(X)), m_APInt(C)))) ||
  2236. !InstCombiner::isSignBitCheck(Pred, *C, IsTrueIfSignSet) ||
  2237. X->getType() != SelType)
  2238. return nullptr;
  2239. // If needed, negate the value that will be the sign argument of the copysign:
  2240. // (bitcast X) < 0 ? -TC : TC --> copysign(TC, X)
  2241. // (bitcast X) < 0 ? TC : -TC --> copysign(TC, -X)
  2242. // (bitcast X) >= 0 ? -TC : TC --> copysign(TC, -X)
  2243. // (bitcast X) >= 0 ? TC : -TC --> copysign(TC, X)
  2244. if (IsTrueIfSignSet ^ TC->isNegative())
  2245. X = Builder.CreateFNegFMF(X, &Sel);
  2246. // Canonicalize the magnitude argument as the positive constant since we do
  2247. // not care about its sign.
  2248. Value *MagArg = TC->isNegative() ? FVal : TVal;
  2249. Function *F = Intrinsic::getDeclaration(Sel.getModule(), Intrinsic::copysign,
  2250. Sel.getType());
  2251. Instruction *CopySign = CallInst::Create(F, { MagArg, X });
  2252. CopySign->setFastMathFlags(Sel.getFastMathFlags());
  2253. return CopySign;
  2254. }
  2255. Instruction *InstCombinerImpl::foldVectorSelect(SelectInst &Sel) {
  2256. auto *VecTy = dyn_cast<FixedVectorType>(Sel.getType());
  2257. if (!VecTy)
  2258. return nullptr;
  2259. unsigned NumElts = VecTy->getNumElements();
  2260. APInt UndefElts(NumElts, 0);
  2261. APInt AllOnesEltMask(APInt::getAllOnes(NumElts));
  2262. if (Value *V = SimplifyDemandedVectorElts(&Sel, AllOnesEltMask, UndefElts)) {
  2263. if (V != &Sel)
  2264. return replaceInstUsesWith(Sel, V);
  2265. return &Sel;
  2266. }
  2267. // A select of a "select shuffle" with a common operand can be rearranged
  2268. // to select followed by "select shuffle". Because of poison, this only works
  2269. // in the case of a shuffle with no undefined mask elements.
  2270. Value *Cond = Sel.getCondition();
  2271. Value *TVal = Sel.getTrueValue();
  2272. Value *FVal = Sel.getFalseValue();
  2273. Value *X, *Y;
  2274. ArrayRef<int> Mask;
  2275. if (match(TVal, m_OneUse(m_Shuffle(m_Value(X), m_Value(Y), m_Mask(Mask)))) &&
  2276. !is_contained(Mask, UndefMaskElem) &&
  2277. cast<ShuffleVectorInst>(TVal)->isSelect()) {
  2278. if (X == FVal) {
  2279. // select Cond, (shuf_sel X, Y), X --> shuf_sel X, (select Cond, Y, X)
  2280. Value *NewSel = Builder.CreateSelect(Cond, Y, X, "sel", &Sel);
  2281. return new ShuffleVectorInst(X, NewSel, Mask);
  2282. }
  2283. if (Y == FVal) {
  2284. // select Cond, (shuf_sel X, Y), Y --> shuf_sel (select Cond, X, Y), Y
  2285. Value *NewSel = Builder.CreateSelect(Cond, X, Y, "sel", &Sel);
  2286. return new ShuffleVectorInst(NewSel, Y, Mask);
  2287. }
  2288. }
  2289. if (match(FVal, m_OneUse(m_Shuffle(m_Value(X), m_Value(Y), m_Mask(Mask)))) &&
  2290. !is_contained(Mask, UndefMaskElem) &&
  2291. cast<ShuffleVectorInst>(FVal)->isSelect()) {
  2292. if (X == TVal) {
  2293. // select Cond, X, (shuf_sel X, Y) --> shuf_sel X, (select Cond, X, Y)
  2294. Value *NewSel = Builder.CreateSelect(Cond, X, Y, "sel", &Sel);
  2295. return new ShuffleVectorInst(X, NewSel, Mask);
  2296. }
  2297. if (Y == TVal) {
  2298. // select Cond, Y, (shuf_sel X, Y) --> shuf_sel (select Cond, Y, X), Y
  2299. Value *NewSel = Builder.CreateSelect(Cond, Y, X, "sel", &Sel);
  2300. return new ShuffleVectorInst(NewSel, Y, Mask);
  2301. }
  2302. }
  2303. return nullptr;
  2304. }
  2305. static Instruction *foldSelectToPhiImpl(SelectInst &Sel, BasicBlock *BB,
  2306. const DominatorTree &DT,
  2307. InstCombiner::BuilderTy &Builder) {
  2308. // Find the block's immediate dominator that ends with a conditional branch
  2309. // that matches select's condition (maybe inverted).
  2310. auto *IDomNode = DT[BB]->getIDom();
  2311. if (!IDomNode)
  2312. return nullptr;
  2313. BasicBlock *IDom = IDomNode->getBlock();
  2314. Value *Cond = Sel.getCondition();
  2315. Value *IfTrue, *IfFalse;
  2316. BasicBlock *TrueSucc, *FalseSucc;
  2317. if (match(IDom->getTerminator(),
  2318. m_Br(m_Specific(Cond), m_BasicBlock(TrueSucc),
  2319. m_BasicBlock(FalseSucc)))) {
  2320. IfTrue = Sel.getTrueValue();
  2321. IfFalse = Sel.getFalseValue();
  2322. } else if (match(IDom->getTerminator(),
  2323. m_Br(m_Not(m_Specific(Cond)), m_BasicBlock(TrueSucc),
  2324. m_BasicBlock(FalseSucc)))) {
  2325. IfTrue = Sel.getFalseValue();
  2326. IfFalse = Sel.getTrueValue();
  2327. } else
  2328. return nullptr;
  2329. // Make sure the branches are actually different.
  2330. if (TrueSucc == FalseSucc)
  2331. return nullptr;
  2332. // We want to replace select %cond, %a, %b with a phi that takes value %a
  2333. // for all incoming edges that are dominated by condition `%cond == true`,
  2334. // and value %b for edges dominated by condition `%cond == false`. If %a
  2335. // or %b are also phis from the same basic block, we can go further and take
  2336. // their incoming values from the corresponding blocks.
  2337. BasicBlockEdge TrueEdge(IDom, TrueSucc);
  2338. BasicBlockEdge FalseEdge(IDom, FalseSucc);
  2339. DenseMap<BasicBlock *, Value *> Inputs;
  2340. for (auto *Pred : predecessors(BB)) {
  2341. // Check implication.
  2342. BasicBlockEdge Incoming(Pred, BB);
  2343. if (DT.dominates(TrueEdge, Incoming))
  2344. Inputs[Pred] = IfTrue->DoPHITranslation(BB, Pred);
  2345. else if (DT.dominates(FalseEdge, Incoming))
  2346. Inputs[Pred] = IfFalse->DoPHITranslation(BB, Pred);
  2347. else
  2348. return nullptr;
  2349. // Check availability.
  2350. if (auto *Insn = dyn_cast<Instruction>(Inputs[Pred]))
  2351. if (!DT.dominates(Insn, Pred->getTerminator()))
  2352. return nullptr;
  2353. }
  2354. Builder.SetInsertPoint(&*BB->begin());
  2355. auto *PN = Builder.CreatePHI(Sel.getType(), Inputs.size());
  2356. for (auto *Pred : predecessors(BB))
  2357. PN->addIncoming(Inputs[Pred], Pred);
  2358. PN->takeName(&Sel);
  2359. return PN;
  2360. }
  2361. static Instruction *foldSelectToPhi(SelectInst &Sel, const DominatorTree &DT,
  2362. InstCombiner::BuilderTy &Builder) {
  2363. // Try to replace this select with Phi in one of these blocks.
  2364. SmallSetVector<BasicBlock *, 4> CandidateBlocks;
  2365. CandidateBlocks.insert(Sel.getParent());
  2366. for (Value *V : Sel.operands())
  2367. if (auto *I = dyn_cast<Instruction>(V))
  2368. CandidateBlocks.insert(I->getParent());
  2369. for (BasicBlock *BB : CandidateBlocks)
  2370. if (auto *PN = foldSelectToPhiImpl(Sel, BB, DT, Builder))
  2371. return PN;
  2372. return nullptr;
  2373. }
  2374. static Value *foldSelectWithFrozenICmp(SelectInst &Sel, InstCombiner::BuilderTy &Builder) {
  2375. FreezeInst *FI = dyn_cast<FreezeInst>(Sel.getCondition());
  2376. if (!FI)
  2377. return nullptr;
  2378. Value *Cond = FI->getOperand(0);
  2379. Value *TrueVal = Sel.getTrueValue(), *FalseVal = Sel.getFalseValue();
  2380. // select (freeze(x == y)), x, y --> y
  2381. // select (freeze(x != y)), x, y --> x
  2382. // The freeze should be only used by this select. Otherwise, remaining uses of
  2383. // the freeze can observe a contradictory value.
  2384. // c = freeze(x == y) ; Let's assume that y = poison & x = 42; c is 0 or 1
  2385. // a = select c, x, y ;
  2386. // f(a, c) ; f(poison, 1) cannot happen, but if a is folded
  2387. // ; to y, this can happen.
  2388. CmpInst::Predicate Pred;
  2389. if (FI->hasOneUse() &&
  2390. match(Cond, m_c_ICmp(Pred, m_Specific(TrueVal), m_Specific(FalseVal))) &&
  2391. (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE)) {
  2392. return Pred == ICmpInst::ICMP_EQ ? FalseVal : TrueVal;
  2393. }
  2394. return nullptr;
  2395. }
  2396. Instruction *InstCombinerImpl::foldAndOrOfSelectUsingImpliedCond(Value *Op,
  2397. SelectInst &SI,
  2398. bool IsAnd) {
  2399. Value *CondVal = SI.getCondition();
  2400. Value *A = SI.getTrueValue();
  2401. Value *B = SI.getFalseValue();
  2402. assert(Op->getType()->isIntOrIntVectorTy(1) &&
  2403. "Op must be either i1 or vector of i1.");
  2404. Optional<bool> Res = isImpliedCondition(Op, CondVal, DL, IsAnd);
  2405. if (!Res)
  2406. return nullptr;
  2407. Value *Zero = Constant::getNullValue(A->getType());
  2408. Value *One = Constant::getAllOnesValue(A->getType());
  2409. if (*Res == true) {
  2410. if (IsAnd)
  2411. // select op, (select cond, A, B), false => select op, A, false
  2412. // and op, (select cond, A, B) => select op, A, false
  2413. // if op = true implies condval = true.
  2414. return SelectInst::Create(Op, A, Zero);
  2415. else
  2416. // select op, true, (select cond, A, B) => select op, true, A
  2417. // or op, (select cond, A, B) => select op, true, A
  2418. // if op = false implies condval = true.
  2419. return SelectInst::Create(Op, One, A);
  2420. } else {
  2421. if (IsAnd)
  2422. // select op, (select cond, A, B), false => select op, B, false
  2423. // and op, (select cond, A, B) => select op, B, false
  2424. // if op = true implies condval = false.
  2425. return SelectInst::Create(Op, B, Zero);
  2426. else
  2427. // select op, true, (select cond, A, B) => select op, true, B
  2428. // or op, (select cond, A, B) => select op, true, B
  2429. // if op = false implies condval = false.
  2430. return SelectInst::Create(Op, One, B);
  2431. }
  2432. }
  2433. Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
  2434. Value *CondVal = SI.getCondition();
  2435. Value *TrueVal = SI.getTrueValue();
  2436. Value *FalseVal = SI.getFalseValue();
  2437. Type *SelType = SI.getType();
  2438. // FIXME: Remove this workaround when freeze related patches are done.
  2439. // For select with undef operand which feeds into an equality comparison,
  2440. // don't simplify it so loop unswitch can know the equality comparison
  2441. // may have an undef operand. This is a workaround for PR31652 caused by
  2442. // descrepancy about branch on undef between LoopUnswitch and GVN.
  2443. if (match(TrueVal, m_Undef()) || match(FalseVal, m_Undef())) {
  2444. if (llvm::any_of(SI.users(), [&](User *U) {
  2445. ICmpInst *CI = dyn_cast<ICmpInst>(U);
  2446. if (CI && CI->isEquality())
  2447. return true;
  2448. return false;
  2449. })) {
  2450. return nullptr;
  2451. }
  2452. }
  2453. if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal,
  2454. SQ.getWithInstruction(&SI)))
  2455. return replaceInstUsesWith(SI, V);
  2456. if (Instruction *I = canonicalizeSelectToShuffle(SI))
  2457. return I;
  2458. if (Instruction *I = canonicalizeScalarSelectOfVecs(SI, *this))
  2459. return I;
  2460. CmpInst::Predicate Pred;
  2461. // Avoid potential infinite loops by checking for non-constant condition.
  2462. // TODO: Can we assert instead by improving canonicalizeSelectToShuffle()?
  2463. // Scalar select must have simplified?
  2464. if (SelType->isIntOrIntVectorTy(1) && !isa<Constant>(CondVal) &&
  2465. TrueVal->getType() == CondVal->getType()) {
  2466. // Folding select to and/or i1 isn't poison safe in general. impliesPoison
  2467. // checks whether folding it does not convert a well-defined value into
  2468. // poison.
  2469. if (match(TrueVal, m_One()) && impliesPoison(FalseVal, CondVal)) {
  2470. // Change: A = select B, true, C --> A = or B, C
  2471. return BinaryOperator::CreateOr(CondVal, FalseVal);
  2472. }
  2473. if (match(FalseVal, m_Zero()) && impliesPoison(TrueVal, CondVal)) {
  2474. // Change: A = select B, C, false --> A = and B, C
  2475. return BinaryOperator::CreateAnd(CondVal, TrueVal);
  2476. }
  2477. auto *One = ConstantInt::getTrue(SelType);
  2478. auto *Zero = ConstantInt::getFalse(SelType);
  2479. // We match the "full" 0 or 1 constant here to avoid a potential infinite
  2480. // loop with vectors that may have undefined/poison elements.
  2481. // select a, false, b -> select !a, b, false
  2482. if (match(TrueVal, m_Specific(Zero))) {
  2483. Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
  2484. return SelectInst::Create(NotCond, FalseVal, Zero);
  2485. }
  2486. // select a, b, true -> select !a, true, b
  2487. if (match(FalseVal, m_Specific(One))) {
  2488. Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
  2489. return SelectInst::Create(NotCond, One, TrueVal);
  2490. }
  2491. // select a, a, b -> select a, true, b
  2492. if (CondVal == TrueVal)
  2493. return replaceOperand(SI, 1, One);
  2494. // select a, b, a -> select a, b, false
  2495. if (CondVal == FalseVal)
  2496. return replaceOperand(SI, 2, Zero);
  2497. // select a, !a, b -> select !a, b, false
  2498. if (match(TrueVal, m_Not(m_Specific(CondVal))))
  2499. return SelectInst::Create(TrueVal, FalseVal, Zero);
  2500. // select a, b, !a -> select !a, true, b
  2501. if (match(FalseVal, m_Not(m_Specific(CondVal))))
  2502. return SelectInst::Create(FalseVal, One, TrueVal);
  2503. Value *A, *B;
  2504. // DeMorgan in select form: !a && !b --> !(a || b)
  2505. // select !a, !b, false --> not (select a, true, b)
  2506. if (match(&SI, m_LogicalAnd(m_Not(m_Value(A)), m_Not(m_Value(B)))) &&
  2507. (CondVal->hasOneUse() || TrueVal->hasOneUse()) &&
  2508. !match(A, m_ConstantExpr()) && !match(B, m_ConstantExpr()))
  2509. return BinaryOperator::CreateNot(Builder.CreateSelect(A, One, B));
  2510. // DeMorgan in select form: !a || !b --> !(a && b)
  2511. // select !a, true, !b --> not (select a, b, false)
  2512. if (match(&SI, m_LogicalOr(m_Not(m_Value(A)), m_Not(m_Value(B)))) &&
  2513. (CondVal->hasOneUse() || FalseVal->hasOneUse()) &&
  2514. !match(A, m_ConstantExpr()) && !match(B, m_ConstantExpr()))
  2515. return BinaryOperator::CreateNot(Builder.CreateSelect(A, B, Zero));
  2516. // select (select a, true, b), true, b -> select a, true, b
  2517. if (match(CondVal, m_Select(m_Value(A), m_One(), m_Value(B))) &&
  2518. match(TrueVal, m_One()) && match(FalseVal, m_Specific(B)))
  2519. return replaceOperand(SI, 0, A);
  2520. // select (select a, b, false), b, false -> select a, b, false
  2521. if (match(CondVal, m_Select(m_Value(A), m_Value(B), m_Zero())) &&
  2522. match(TrueVal, m_Specific(B)) && match(FalseVal, m_Zero()))
  2523. return replaceOperand(SI, 0, A);
  2524. if (!SelType->isVectorTy()) {
  2525. if (Value *S = simplifyWithOpReplaced(TrueVal, CondVal, One, SQ,
  2526. /* AllowRefinement */ true))
  2527. return replaceOperand(SI, 1, S);
  2528. if (Value *S = simplifyWithOpReplaced(FalseVal, CondVal, Zero, SQ,
  2529. /* AllowRefinement */ true))
  2530. return replaceOperand(SI, 2, S);
  2531. }
  2532. if (match(FalseVal, m_Zero()) || match(TrueVal, m_One())) {
  2533. Use *Y = nullptr;
  2534. bool IsAnd = match(FalseVal, m_Zero()) ? true : false;
  2535. Value *Op1 = IsAnd ? TrueVal : FalseVal;
  2536. if (isCheckForZeroAndMulWithOverflow(CondVal, Op1, IsAnd, Y)) {
  2537. auto *FI = new FreezeInst(*Y, (*Y)->getName() + ".fr");
  2538. InsertNewInstBefore(FI, *cast<Instruction>(Y->getUser()));
  2539. replaceUse(*Y, FI);
  2540. return replaceInstUsesWith(SI, Op1);
  2541. }
  2542. if (auto *Op1SI = dyn_cast<SelectInst>(Op1))
  2543. if (auto *I = foldAndOrOfSelectUsingImpliedCond(CondVal, *Op1SI,
  2544. /* IsAnd */ IsAnd))
  2545. return I;
  2546. if (auto *ICmp0 = dyn_cast<ICmpInst>(CondVal)) {
  2547. if (auto *ICmp1 = dyn_cast<ICmpInst>(Op1)) {
  2548. if (auto *V = foldAndOrOfICmpsOfAndWithPow2(ICmp0, ICmp1, &SI, IsAnd,
  2549. /* IsLogical */ true))
  2550. return replaceInstUsesWith(SI, V);
  2551. if (auto *V = foldEqOfParts(ICmp0, ICmp1, IsAnd))
  2552. return replaceInstUsesWith(SI, V);
  2553. }
  2554. }
  2555. }
  2556. // select (select a, true, b), c, false -> select a, c, false
  2557. // select c, (select a, true, b), false -> select c, a, false
  2558. // if c implies that b is false.
  2559. if (match(CondVal, m_Select(m_Value(A), m_One(), m_Value(B))) &&
  2560. match(FalseVal, m_Zero())) {
  2561. Optional<bool> Res = isImpliedCondition(TrueVal, B, DL);
  2562. if (Res && *Res == false)
  2563. return replaceOperand(SI, 0, A);
  2564. }
  2565. if (match(TrueVal, m_Select(m_Value(A), m_One(), m_Value(B))) &&
  2566. match(FalseVal, m_Zero())) {
  2567. Optional<bool> Res = isImpliedCondition(CondVal, B, DL);
  2568. if (Res && *Res == false)
  2569. return replaceOperand(SI, 1, A);
  2570. }
  2571. // select c, true, (select a, b, false) -> select c, true, a
  2572. // select (select a, b, false), true, c -> select a, true, c
  2573. // if c = false implies that b = true
  2574. if (match(TrueVal, m_One()) &&
  2575. match(FalseVal, m_Select(m_Value(A), m_Value(B), m_Zero()))) {
  2576. Optional<bool> Res = isImpliedCondition(CondVal, B, DL, false);
  2577. if (Res && *Res == true)
  2578. return replaceOperand(SI, 2, A);
  2579. }
  2580. if (match(CondVal, m_Select(m_Value(A), m_Value(B), m_Zero())) &&
  2581. match(TrueVal, m_One())) {
  2582. Optional<bool> Res = isImpliedCondition(FalseVal, B, DL, false);
  2583. if (Res && *Res == true)
  2584. return replaceOperand(SI, 0, A);
  2585. }
  2586. // sel (sel c, a, false), true, (sel !c, b, false) -> sel c, a, b
  2587. // sel (sel !c, a, false), true, (sel c, b, false) -> sel c, b, a
  2588. Value *C1, *C2;
  2589. if (match(CondVal, m_Select(m_Value(C1), m_Value(A), m_Zero())) &&
  2590. match(TrueVal, m_One()) &&
  2591. match(FalseVal, m_Select(m_Value(C2), m_Value(B), m_Zero()))) {
  2592. if (match(C2, m_Not(m_Specific(C1)))) // first case
  2593. return SelectInst::Create(C1, A, B);
  2594. else if (match(C1, m_Not(m_Specific(C2)))) // second case
  2595. return SelectInst::Create(C2, B, A);
  2596. }
  2597. }
  2598. // Selecting between two integer or vector splat integer constants?
  2599. //
  2600. // Note that we don't handle a scalar select of vectors:
  2601. // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0>
  2602. // because that may need 3 instructions to splat the condition value:
  2603. // extend, insertelement, shufflevector.
  2604. //
  2605. // Do not handle i1 TrueVal and FalseVal otherwise would result in
  2606. // zext/sext i1 to i1.
  2607. if (SelType->isIntOrIntVectorTy() && !SelType->isIntOrIntVectorTy(1) &&
  2608. CondVal->getType()->isVectorTy() == SelType->isVectorTy()) {
  2609. // select C, 1, 0 -> zext C to int
  2610. if (match(TrueVal, m_One()) && match(FalseVal, m_Zero()))
  2611. return new ZExtInst(CondVal, SelType);
  2612. // select C, -1, 0 -> sext C to int
  2613. if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero()))
  2614. return new SExtInst(CondVal, SelType);
  2615. // select C, 0, 1 -> zext !C to int
  2616. if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) {
  2617. Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
  2618. return new ZExtInst(NotCond, SelType);
  2619. }
  2620. // select C, 0, -1 -> sext !C to int
  2621. if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) {
  2622. Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName());
  2623. return new SExtInst(NotCond, SelType);
  2624. }
  2625. }
  2626. if (auto *FCmp = dyn_cast<FCmpInst>(CondVal)) {
  2627. Value *Cmp0 = FCmp->getOperand(0), *Cmp1 = FCmp->getOperand(1);
  2628. // Are we selecting a value based on a comparison of the two values?
  2629. if ((Cmp0 == TrueVal && Cmp1 == FalseVal) ||
  2630. (Cmp0 == FalseVal && Cmp1 == TrueVal)) {
  2631. // Canonicalize to use ordered comparisons by swapping the select
  2632. // operands.
  2633. //
  2634. // e.g.
  2635. // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X
  2636. if (FCmp->hasOneUse() && FCmpInst::isUnordered(FCmp->getPredicate())) {
  2637. FCmpInst::Predicate InvPred = FCmp->getInversePredicate();
  2638. IRBuilder<>::FastMathFlagGuard FMFG(Builder);
  2639. // FIXME: The FMF should propagate from the select, not the fcmp.
  2640. Builder.setFastMathFlags(FCmp->getFastMathFlags());
  2641. Value *NewCond = Builder.CreateFCmp(InvPred, Cmp0, Cmp1,
  2642. FCmp->getName() + ".inv");
  2643. Value *NewSel = Builder.CreateSelect(NewCond, FalseVal, TrueVal);
  2644. return replaceInstUsesWith(SI, NewSel);
  2645. }
  2646. // NOTE: if we wanted to, this is where to detect MIN/MAX
  2647. }
  2648. }
  2649. // Canonicalize select with fcmp to fabs(). -0.0 makes this tricky. We need
  2650. // fast-math-flags (nsz) or fsub with +0.0 (not fneg) for this to work.
  2651. // (X <= +/-0.0) ? (0.0 - X) : X --> fabs(X)
  2652. if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
  2653. match(TrueVal, m_FSub(m_PosZeroFP(), m_Specific(FalseVal))) &&
  2654. (Pred == FCmpInst::FCMP_OLE || Pred == FCmpInst::FCMP_ULE)) {
  2655. Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
  2656. return replaceInstUsesWith(SI, Fabs);
  2657. }
  2658. // (X > +/-0.0) ? X : (0.0 - X) --> fabs(X)
  2659. if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
  2660. match(FalseVal, m_FSub(m_PosZeroFP(), m_Specific(TrueVal))) &&
  2661. (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_UGT)) {
  2662. Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
  2663. return replaceInstUsesWith(SI, Fabs);
  2664. }
  2665. // With nnan and nsz:
  2666. // (X < +/-0.0) ? -X : X --> fabs(X)
  2667. // (X <= +/-0.0) ? -X : X --> fabs(X)
  2668. if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
  2669. match(TrueVal, m_FNeg(m_Specific(FalseVal))) && SI.hasNoSignedZeros() &&
  2670. (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
  2671. Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
  2672. Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
  2673. return replaceInstUsesWith(SI, Fabs);
  2674. }
  2675. // With nnan and nsz:
  2676. // (X > +/-0.0) ? X : -X --> fabs(X)
  2677. // (X >= +/-0.0) ? X : -X --> fabs(X)
  2678. if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
  2679. match(FalseVal, m_FNeg(m_Specific(TrueVal))) && SI.hasNoSignedZeros() &&
  2680. (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
  2681. Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
  2682. Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
  2683. return replaceInstUsesWith(SI, Fabs);
  2684. }
  2685. // See if we are selecting two values based on a comparison of the two values.
  2686. if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
  2687. if (Instruction *Result = foldSelectInstWithICmp(SI, ICI))
  2688. return Result;
  2689. if (Instruction *Add = foldAddSubSelect(SI, Builder))
  2690. return Add;
  2691. if (Instruction *Add = foldOverflowingAddSubSelect(SI, Builder))
  2692. return Add;
  2693. if (Instruction *Or = foldSetClearBits(SI, Builder))
  2694. return Or;
  2695. if (Instruction *Mul = foldSelectZeroOrMul(SI, *this))
  2696. return Mul;
  2697. // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z))
  2698. auto *TI = dyn_cast<Instruction>(TrueVal);
  2699. auto *FI = dyn_cast<Instruction>(FalseVal);
  2700. if (TI && FI && TI->getOpcode() == FI->getOpcode())
  2701. if (Instruction *IV = foldSelectOpOp(SI, TI, FI))
  2702. return IV;
  2703. if (Instruction *I = foldSelectExtConst(SI))
  2704. return I;
  2705. // Fold (select C, (gep Ptr, Idx), Ptr) -> (gep Ptr, (select C, Idx, 0))
  2706. // Fold (select C, Ptr, (gep Ptr, Idx)) -> (gep Ptr, (select C, 0, Idx))
  2707. auto SelectGepWithBase = [&](GetElementPtrInst *Gep, Value *Base,
  2708. bool Swap) -> GetElementPtrInst * {
  2709. Value *Ptr = Gep->getPointerOperand();
  2710. if (Gep->getNumOperands() != 2 || Gep->getPointerOperand() != Base ||
  2711. !Gep->hasOneUse())
  2712. return nullptr;
  2713. Value *Idx = Gep->getOperand(1);
  2714. if (isa<VectorType>(CondVal->getType()) && !isa<VectorType>(Idx->getType()))
  2715. return nullptr;
  2716. Type *ElementType = Gep->getResultElementType();
  2717. Value *NewT = Idx;
  2718. Value *NewF = Constant::getNullValue(Idx->getType());
  2719. if (Swap)
  2720. std::swap(NewT, NewF);
  2721. Value *NewSI =
  2722. Builder.CreateSelect(CondVal, NewT, NewF, SI.getName() + ".idx", &SI);
  2723. return GetElementPtrInst::Create(ElementType, Ptr, {NewSI});
  2724. };
  2725. if (auto *TrueGep = dyn_cast<GetElementPtrInst>(TrueVal))
  2726. if (auto *NewGep = SelectGepWithBase(TrueGep, FalseVal, false))
  2727. return NewGep;
  2728. if (auto *FalseGep = dyn_cast<GetElementPtrInst>(FalseVal))
  2729. if (auto *NewGep = SelectGepWithBase(FalseGep, TrueVal, true))
  2730. return NewGep;
  2731. // See if we can fold the select into one of our operands.
  2732. if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) {
  2733. if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal))
  2734. return FoldI;
  2735. Value *LHS, *RHS;
  2736. Instruction::CastOps CastOp;
  2737. SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp);
  2738. auto SPF = SPR.Flavor;
  2739. if (SPF) {
  2740. Value *LHS2, *RHS2;
  2741. if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor)
  2742. if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS), SPF2, LHS2,
  2743. RHS2, SI, SPF, RHS))
  2744. return R;
  2745. if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor)
  2746. if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS), SPF2, LHS2,
  2747. RHS2, SI, SPF, LHS))
  2748. return R;
  2749. // TODO.
  2750. // ABS(-X) -> ABS(X)
  2751. }
  2752. if (SelectPatternResult::isMinOrMax(SPF)) {
  2753. // Canonicalize so that
  2754. // - type casts are outside select patterns.
  2755. // - float clamp is transformed to min/max pattern
  2756. bool IsCastNeeded = LHS->getType() != SelType;
  2757. Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0);
  2758. Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1);
  2759. if (IsCastNeeded ||
  2760. (LHS->getType()->isFPOrFPVectorTy() &&
  2761. ((CmpLHS != LHS && CmpLHS != RHS) ||
  2762. (CmpRHS != LHS && CmpRHS != RHS)))) {
  2763. CmpInst::Predicate MinMaxPred = getMinMaxPred(SPF, SPR.Ordered);
  2764. Value *Cmp;
  2765. if (CmpInst::isIntPredicate(MinMaxPred)) {
  2766. Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
  2767. } else {
  2768. IRBuilder<>::FastMathFlagGuard FMFG(Builder);
  2769. auto FMF =
  2770. cast<FPMathOperator>(SI.getCondition())->getFastMathFlags();
  2771. Builder.setFastMathFlags(FMF);
  2772. Cmp = Builder.CreateFCmp(MinMaxPred, LHS, RHS);
  2773. }
  2774. Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
  2775. if (!IsCastNeeded)
  2776. return replaceInstUsesWith(SI, NewSI);
  2777. Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType);
  2778. return replaceInstUsesWith(SI, NewCast);
  2779. }
  2780. // MAX(~a, ~b) -> ~MIN(a, b)
  2781. // MAX(~a, C) -> ~MIN(a, ~C)
  2782. // MIN(~a, ~b) -> ~MAX(a, b)
  2783. // MIN(~a, C) -> ~MAX(a, ~C)
  2784. auto moveNotAfterMinMax = [&](Value *X, Value *Y) -> Instruction * {
  2785. Value *A;
  2786. if (match(X, m_Not(m_Value(A))) && !X->hasNUsesOrMore(3) &&
  2787. !isFreeToInvert(A, A->hasOneUse()) &&
  2788. // Passing false to only consider m_Not and constants.
  2789. isFreeToInvert(Y, false)) {
  2790. Value *B = Builder.CreateNot(Y);
  2791. Value *NewMinMax = createMinMax(Builder, getInverseMinMaxFlavor(SPF),
  2792. A, B);
  2793. // Copy the profile metadata.
  2794. if (MDNode *MD = SI.getMetadata(LLVMContext::MD_prof)) {
  2795. cast<SelectInst>(NewMinMax)->setMetadata(LLVMContext::MD_prof, MD);
  2796. // Swap the metadata if the operands are swapped.
  2797. if (X == SI.getFalseValue() && Y == SI.getTrueValue())
  2798. cast<SelectInst>(NewMinMax)->swapProfMetadata();
  2799. }
  2800. return BinaryOperator::CreateNot(NewMinMax);
  2801. }
  2802. return nullptr;
  2803. };
  2804. if (Instruction *I = moveNotAfterMinMax(LHS, RHS))
  2805. return I;
  2806. if (Instruction *I = moveNotAfterMinMax(RHS, LHS))
  2807. return I;
  2808. if (Instruction *I = moveAddAfterMinMax(SPF, LHS, RHS, Builder))
  2809. return I;
  2810. if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder))
  2811. return I;
  2812. if (Instruction *I = matchSAddSubSat(SI))
  2813. return I;
  2814. }
  2815. }
  2816. // Canonicalize select of FP values where NaN and -0.0 are not valid as
  2817. // minnum/maxnum intrinsics.
  2818. if (isa<FPMathOperator>(SI) && SI.hasNoNaNs() && SI.hasNoSignedZeros()) {
  2819. Value *X, *Y;
  2820. if (match(&SI, m_OrdFMax(m_Value(X), m_Value(Y))))
  2821. return replaceInstUsesWith(
  2822. SI, Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, X, Y, &SI));
  2823. if (match(&SI, m_OrdFMin(m_Value(X), m_Value(Y))))
  2824. return replaceInstUsesWith(
  2825. SI, Builder.CreateBinaryIntrinsic(Intrinsic::minnum, X, Y, &SI));
  2826. }
  2827. // See if we can fold the select into a phi node if the condition is a select.
  2828. if (auto *PN = dyn_cast<PHINode>(SI.getCondition()))
  2829. // The true/false values have to be live in the PHI predecessor's blocks.
  2830. if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) &&
  2831. canSelectOperandBeMappingIntoPredBlock(FalseVal, SI))
  2832. if (Instruction *NV = foldOpIntoPhi(SI, PN))
  2833. return NV;
  2834. if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
  2835. if (TrueSI->getCondition()->getType() == CondVal->getType()) {
  2836. // select(C, select(C, a, b), c) -> select(C, a, c)
  2837. if (TrueSI->getCondition() == CondVal) {
  2838. if (SI.getTrueValue() == TrueSI->getTrueValue())
  2839. return nullptr;
  2840. return replaceOperand(SI, 1, TrueSI->getTrueValue());
  2841. }
  2842. // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b)
  2843. // We choose this as normal form to enable folding on the And and
  2844. // shortening paths for the values (this helps getUnderlyingObjects() for
  2845. // example).
  2846. if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) {
  2847. Value *And = Builder.CreateLogicalAnd(CondVal, TrueSI->getCondition());
  2848. replaceOperand(SI, 0, And);
  2849. replaceOperand(SI, 1, TrueSI->getTrueValue());
  2850. return &SI;
  2851. }
  2852. }
  2853. }
  2854. if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
  2855. if (FalseSI->getCondition()->getType() == CondVal->getType()) {
  2856. // select(C, a, select(C, b, c)) -> select(C, a, c)
  2857. if (FalseSI->getCondition() == CondVal) {
  2858. if (SI.getFalseValue() == FalseSI->getFalseValue())
  2859. return nullptr;
  2860. return replaceOperand(SI, 2, FalseSI->getFalseValue());
  2861. }
  2862. // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b)
  2863. if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) {
  2864. Value *Or = Builder.CreateLogicalOr(CondVal, FalseSI->getCondition());
  2865. replaceOperand(SI, 0, Or);
  2866. replaceOperand(SI, 2, FalseSI->getFalseValue());
  2867. return &SI;
  2868. }
  2869. }
  2870. }
  2871. auto canMergeSelectThroughBinop = [](BinaryOperator *BO) {
  2872. // The select might be preventing a division by 0.
  2873. switch (BO->getOpcode()) {
  2874. default:
  2875. return true;
  2876. case Instruction::SRem:
  2877. case Instruction::URem:
  2878. case Instruction::SDiv:
  2879. case Instruction::UDiv:
  2880. return false;
  2881. }
  2882. };
  2883. // Try to simplify a binop sandwiched between 2 selects with the same
  2884. // condition.
  2885. // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z)
  2886. BinaryOperator *TrueBO;
  2887. if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) &&
  2888. canMergeSelectThroughBinop(TrueBO)) {
  2889. if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) {
  2890. if (TrueBOSI->getCondition() == CondVal) {
  2891. replaceOperand(*TrueBO, 0, TrueBOSI->getTrueValue());
  2892. Worklist.push(TrueBO);
  2893. return &SI;
  2894. }
  2895. }
  2896. if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) {
  2897. if (TrueBOSI->getCondition() == CondVal) {
  2898. replaceOperand(*TrueBO, 1, TrueBOSI->getTrueValue());
  2899. Worklist.push(TrueBO);
  2900. return &SI;
  2901. }
  2902. }
  2903. }
  2904. // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W))
  2905. BinaryOperator *FalseBO;
  2906. if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) &&
  2907. canMergeSelectThroughBinop(FalseBO)) {
  2908. if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) {
  2909. if (FalseBOSI->getCondition() == CondVal) {
  2910. replaceOperand(*FalseBO, 0, FalseBOSI->getFalseValue());
  2911. Worklist.push(FalseBO);
  2912. return &SI;
  2913. }
  2914. }
  2915. if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) {
  2916. if (FalseBOSI->getCondition() == CondVal) {
  2917. replaceOperand(*FalseBO, 1, FalseBOSI->getFalseValue());
  2918. Worklist.push(FalseBO);
  2919. return &SI;
  2920. }
  2921. }
  2922. }
  2923. Value *NotCond;
  2924. if (match(CondVal, m_Not(m_Value(NotCond))) &&
  2925. !InstCombiner::shouldAvoidAbsorbingNotIntoSelect(SI)) {
  2926. replaceOperand(SI, 0, NotCond);
  2927. SI.swapValues();
  2928. SI.swapProfMetadata();
  2929. return &SI;
  2930. }
  2931. if (Instruction *I = foldVectorSelect(SI))
  2932. return I;
  2933. // If we can compute the condition, there's no need for a select.
  2934. // Like the above fold, we are attempting to reduce compile-time cost by
  2935. // putting this fold here with limitations rather than in InstSimplify.
  2936. // The motivation for this call into value tracking is to take advantage of
  2937. // the assumption cache, so make sure that is populated.
  2938. if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) {
  2939. KnownBits Known(1);
  2940. computeKnownBits(CondVal, Known, 0, &SI);
  2941. if (Known.One.isOne())
  2942. return replaceInstUsesWith(SI, TrueVal);
  2943. if (Known.Zero.isOne())
  2944. return replaceInstUsesWith(SI, FalseVal);
  2945. }
  2946. if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder))
  2947. return BitCastSel;
  2948. // Simplify selects that test the returned flag of cmpxchg instructions.
  2949. if (Value *V = foldSelectCmpXchg(SI))
  2950. return replaceInstUsesWith(SI, V);
  2951. if (Instruction *Select = foldSelectBinOpIdentity(SI, TLI, *this))
  2952. return Select;
  2953. if (Instruction *Funnel = foldSelectFunnelShift(SI, Builder))
  2954. return Funnel;
  2955. if (Instruction *Copysign = foldSelectToCopysign(SI, Builder))
  2956. return Copysign;
  2957. if (Instruction *PN = foldSelectToPhi(SI, DT, Builder))
  2958. return replaceInstUsesWith(SI, PN);
  2959. if (Value *Fr = foldSelectWithFrozenICmp(SI, Builder))
  2960. return replaceInstUsesWith(SI, Fr);
  2961. // select(mask, mload(,,mask,0), 0) -> mload(,,mask,0)
  2962. // Load inst is intentionally not checked for hasOneUse()
  2963. if (match(FalseVal, m_Zero()) &&
  2964. match(TrueVal, m_MaskedLoad(m_Value(), m_Value(), m_Specific(CondVal),
  2965. m_CombineOr(m_Undef(), m_Zero())))) {
  2966. auto *MaskedLoad = cast<IntrinsicInst>(TrueVal);
  2967. if (isa<UndefValue>(MaskedLoad->getArgOperand(3)))
  2968. MaskedLoad->setArgOperand(3, FalseVal /* Zero */);
  2969. return replaceInstUsesWith(SI, MaskedLoad);
  2970. }
  2971. Value *Mask;
  2972. if (match(TrueVal, m_Zero()) &&
  2973. match(FalseVal, m_MaskedLoad(m_Value(), m_Value(), m_Value(Mask),
  2974. m_CombineOr(m_Undef(), m_Zero()))) &&
  2975. (CondVal->getType() == Mask->getType())) {
  2976. // We can remove the select by ensuring the load zeros all lanes the
  2977. // select would have. We determine this by proving there is no overlap
  2978. // between the load and select masks.
  2979. // (i.e (load_mask & select_mask) == 0 == no overlap)
  2980. bool CanMergeSelectIntoLoad = false;
  2981. if (Value *V = SimplifyAndInst(CondVal, Mask, SQ.getWithInstruction(&SI)))
  2982. CanMergeSelectIntoLoad = match(V, m_Zero());
  2983. if (CanMergeSelectIntoLoad) {
  2984. auto *MaskedLoad = cast<IntrinsicInst>(FalseVal);
  2985. if (isa<UndefValue>(MaskedLoad->getArgOperand(3)))
  2986. MaskedLoad->setArgOperand(3, TrueVal /* Zero */);
  2987. return replaceInstUsesWith(SI, MaskedLoad);
  2988. }
  2989. }
  2990. return nullptr;
  2991. }