PatternMatch.h 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file provides a simple and efficient mechanism for performing general
  15. // tree-based pattern matches on the LLVM IR. The power of these routines is
  16. // that it allows you to write concise patterns that are expressive and easy to
  17. // understand. The other major advantage of this is that it allows you to
  18. // trivially capture/bind elements in the pattern to variables. For example,
  19. // you can do something like this:
  20. //
  21. // Value *Exp = ...
  22. // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
  23. // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
  24. // m_And(m_Value(Y), m_ConstantInt(C2))))) {
  25. // ... Pattern is matched and variables are bound ...
  26. // }
  27. //
  28. // This is primarily useful to things like the instruction combiner, but can
  29. // also be useful for static analysis tools or code generators.
  30. //
  31. //===----------------------------------------------------------------------===//
  32. #ifndef LLVM_IR_PATTERNMATCH_H
  33. #define LLVM_IR_PATTERNMATCH_H
  34. #include "llvm/ADT/APFloat.h"
  35. #include "llvm/ADT/APInt.h"
  36. #include "llvm/IR/Constant.h"
  37. #include "llvm/IR/Constants.h"
  38. #include "llvm/IR/DataLayout.h"
  39. #include "llvm/IR/InstrTypes.h"
  40. #include "llvm/IR/Instruction.h"
  41. #include "llvm/IR/Instructions.h"
  42. #include "llvm/IR/IntrinsicInst.h"
  43. #include "llvm/IR/Intrinsics.h"
  44. #include "llvm/IR/Operator.h"
  45. #include "llvm/IR/Value.h"
  46. #include "llvm/Support/Casting.h"
  47. #include <cstdint>
  48. namespace llvm {
  49. namespace PatternMatch {
  50. template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
  51. return const_cast<Pattern &>(P).match(V);
  52. }
  53. template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
  54. return const_cast<Pattern &>(P).match(Mask);
  55. }
  56. template <typename SubPattern_t> struct OneUse_match {
  57. SubPattern_t SubPattern;
  58. OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
  59. template <typename OpTy> bool match(OpTy *V) {
  60. return V->hasOneUse() && SubPattern.match(V);
  61. }
  62. };
  63. template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
  64. return SubPattern;
  65. }
  66. template <typename Class> struct class_match {
  67. template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
  68. };
  69. /// Match an arbitrary value and ignore it.
  70. inline class_match<Value> m_Value() { return class_match<Value>(); }
  71. /// Match an arbitrary unary operation and ignore it.
  72. inline class_match<UnaryOperator> m_UnOp() {
  73. return class_match<UnaryOperator>();
  74. }
  75. /// Match an arbitrary binary operation and ignore it.
  76. inline class_match<BinaryOperator> m_BinOp() {
  77. return class_match<BinaryOperator>();
  78. }
  79. /// Matches any compare instruction and ignore it.
  80. inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
  81. struct undef_match {
  82. static bool check(const Value *V) {
  83. if (isa<UndefValue>(V))
  84. return true;
  85. const auto *CA = dyn_cast<ConstantAggregate>(V);
  86. if (!CA)
  87. return false;
  88. SmallPtrSet<const ConstantAggregate *, 8> Seen;
  89. SmallVector<const ConstantAggregate *, 8> Worklist;
  90. // Either UndefValue, PoisonValue, or an aggregate that only contains
  91. // these is accepted by matcher.
  92. // CheckValue returns false if CA cannot satisfy this constraint.
  93. auto CheckValue = [&](const ConstantAggregate *CA) {
  94. for (const Value *Op : CA->operand_values()) {
  95. if (isa<UndefValue>(Op))
  96. continue;
  97. const auto *CA = dyn_cast<ConstantAggregate>(Op);
  98. if (!CA)
  99. return false;
  100. if (Seen.insert(CA).second)
  101. Worklist.emplace_back(CA);
  102. }
  103. return true;
  104. };
  105. if (!CheckValue(CA))
  106. return false;
  107. while (!Worklist.empty()) {
  108. if (!CheckValue(Worklist.pop_back_val()))
  109. return false;
  110. }
  111. return true;
  112. }
  113. template <typename ITy> bool match(ITy *V) { return check(V); }
  114. };
  115. /// Match an arbitrary undef constant. This matches poison as well.
  116. /// If this is an aggregate and contains a non-aggregate element that is
  117. /// neither undef nor poison, the aggregate is not matched.
  118. inline auto m_Undef() { return undef_match(); }
  119. /// Match an arbitrary poison constant.
  120. inline class_match<PoisonValue> m_Poison() {
  121. return class_match<PoisonValue>();
  122. }
  123. /// Match an arbitrary Constant and ignore it.
  124. inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
  125. /// Match an arbitrary ConstantInt and ignore it.
  126. inline class_match<ConstantInt> m_ConstantInt() {
  127. return class_match<ConstantInt>();
  128. }
  129. /// Match an arbitrary ConstantFP and ignore it.
  130. inline class_match<ConstantFP> m_ConstantFP() {
  131. return class_match<ConstantFP>();
  132. }
  133. struct constantexpr_match {
  134. template <typename ITy> bool match(ITy *V) {
  135. auto *C = dyn_cast<Constant>(V);
  136. return C && (isa<ConstantExpr>(C) || C->containsConstantExpression());
  137. }
  138. };
  139. /// Match a constant expression or a constant that contains a constant
  140. /// expression.
  141. inline constantexpr_match m_ConstantExpr() { return constantexpr_match(); }
  142. /// Match an arbitrary basic block value and ignore it.
  143. inline class_match<BasicBlock> m_BasicBlock() {
  144. return class_match<BasicBlock>();
  145. }
  146. /// Inverting matcher
  147. template <typename Ty> struct match_unless {
  148. Ty M;
  149. match_unless(const Ty &Matcher) : M(Matcher) {}
  150. template <typename ITy> bool match(ITy *V) { return !M.match(V); }
  151. };
  152. /// Match if the inner matcher does *NOT* match.
  153. template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
  154. return match_unless<Ty>(M);
  155. }
  156. /// Matching combinators
  157. template <typename LTy, typename RTy> struct match_combine_or {
  158. LTy L;
  159. RTy R;
  160. match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
  161. template <typename ITy> bool match(ITy *V) {
  162. if (L.match(V))
  163. return true;
  164. if (R.match(V))
  165. return true;
  166. return false;
  167. }
  168. };
  169. template <typename LTy, typename RTy> struct match_combine_and {
  170. LTy L;
  171. RTy R;
  172. match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
  173. template <typename ITy> bool match(ITy *V) {
  174. if (L.match(V))
  175. if (R.match(V))
  176. return true;
  177. return false;
  178. }
  179. };
  180. /// Combine two pattern matchers matching L || R
  181. template <typename LTy, typename RTy>
  182. inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
  183. return match_combine_or<LTy, RTy>(L, R);
  184. }
  185. /// Combine two pattern matchers matching L && R
  186. template <typename LTy, typename RTy>
  187. inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
  188. return match_combine_and<LTy, RTy>(L, R);
  189. }
  190. struct apint_match {
  191. const APInt *&Res;
  192. bool AllowUndef;
  193. apint_match(const APInt *&Res, bool AllowUndef)
  194. : Res(Res), AllowUndef(AllowUndef) {}
  195. template <typename ITy> bool match(ITy *V) {
  196. if (auto *CI = dyn_cast<ConstantInt>(V)) {
  197. Res = &CI->getValue();
  198. return true;
  199. }
  200. if (V->getType()->isVectorTy())
  201. if (const auto *C = dyn_cast<Constant>(V))
  202. if (auto *CI =
  203. dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndef))) {
  204. Res = &CI->getValue();
  205. return true;
  206. }
  207. return false;
  208. }
  209. };
  210. // Either constexpr if or renaming ConstantFP::getValueAPF to
  211. // ConstantFP::getValue is needed to do it via single template
  212. // function for both apint/apfloat.
  213. struct apfloat_match {
  214. const APFloat *&Res;
  215. bool AllowUndef;
  216. apfloat_match(const APFloat *&Res, bool AllowUndef)
  217. : Res(Res), AllowUndef(AllowUndef) {}
  218. template <typename ITy> bool match(ITy *V) {
  219. if (auto *CI = dyn_cast<ConstantFP>(V)) {
  220. Res = &CI->getValueAPF();
  221. return true;
  222. }
  223. if (V->getType()->isVectorTy())
  224. if (const auto *C = dyn_cast<Constant>(V))
  225. if (auto *CI =
  226. dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowUndef))) {
  227. Res = &CI->getValueAPF();
  228. return true;
  229. }
  230. return false;
  231. }
  232. };
  233. /// Match a ConstantInt or splatted ConstantVector, binding the
  234. /// specified pointer to the contained APInt.
  235. inline apint_match m_APInt(const APInt *&Res) {
  236. // Forbid undefs by default to maintain previous behavior.
  237. return apint_match(Res, /* AllowUndef */ false);
  238. }
  239. /// Match APInt while allowing undefs in splat vector constants.
  240. inline apint_match m_APIntAllowUndef(const APInt *&Res) {
  241. return apint_match(Res, /* AllowUndef */ true);
  242. }
  243. /// Match APInt while forbidding undefs in splat vector constants.
  244. inline apint_match m_APIntForbidUndef(const APInt *&Res) {
  245. return apint_match(Res, /* AllowUndef */ false);
  246. }
  247. /// Match a ConstantFP or splatted ConstantVector, binding the
  248. /// specified pointer to the contained APFloat.
  249. inline apfloat_match m_APFloat(const APFloat *&Res) {
  250. // Forbid undefs by default to maintain previous behavior.
  251. return apfloat_match(Res, /* AllowUndef */ false);
  252. }
  253. /// Match APFloat while allowing undefs in splat vector constants.
  254. inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) {
  255. return apfloat_match(Res, /* AllowUndef */ true);
  256. }
  257. /// Match APFloat while forbidding undefs in splat vector constants.
  258. inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) {
  259. return apfloat_match(Res, /* AllowUndef */ false);
  260. }
  261. template <int64_t Val> struct constantint_match {
  262. template <typename ITy> bool match(ITy *V) {
  263. if (const auto *CI = dyn_cast<ConstantInt>(V)) {
  264. const APInt &CIV = CI->getValue();
  265. if (Val >= 0)
  266. return CIV == static_cast<uint64_t>(Val);
  267. // If Val is negative, and CI is shorter than it, truncate to the right
  268. // number of bits. If it is larger, then we have to sign extend. Just
  269. // compare their negated values.
  270. return -CIV == -Val;
  271. }
  272. return false;
  273. }
  274. };
  275. /// Match a ConstantInt with a specific value.
  276. template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
  277. return constantint_match<Val>();
  278. }
  279. /// This helper class is used to match constant scalars, vector splats,
  280. /// and fixed width vectors that satisfy a specified predicate.
  281. /// For fixed width vector constants, undefined elements are ignored.
  282. template <typename Predicate, typename ConstantVal>
  283. struct cstval_pred_ty : public Predicate {
  284. template <typename ITy> bool match(ITy *V) {
  285. if (const auto *CV = dyn_cast<ConstantVal>(V))
  286. return this->isValue(CV->getValue());
  287. if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
  288. if (const auto *C = dyn_cast<Constant>(V)) {
  289. if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
  290. return this->isValue(CV->getValue());
  291. // Number of elements of a scalable vector unknown at compile time
  292. auto *FVTy = dyn_cast<FixedVectorType>(VTy);
  293. if (!FVTy)
  294. return false;
  295. // Non-splat vector constant: check each element for a match.
  296. unsigned NumElts = FVTy->getNumElements();
  297. assert(NumElts != 0 && "Constant vector with no elements?");
  298. bool HasNonUndefElements = false;
  299. for (unsigned i = 0; i != NumElts; ++i) {
  300. Constant *Elt = C->getAggregateElement(i);
  301. if (!Elt)
  302. return false;
  303. if (isa<UndefValue>(Elt))
  304. continue;
  305. auto *CV = dyn_cast<ConstantVal>(Elt);
  306. if (!CV || !this->isValue(CV->getValue()))
  307. return false;
  308. HasNonUndefElements = true;
  309. }
  310. return HasNonUndefElements;
  311. }
  312. }
  313. return false;
  314. }
  315. };
  316. /// specialization of cstval_pred_ty for ConstantInt
  317. template <typename Predicate>
  318. using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>;
  319. /// specialization of cstval_pred_ty for ConstantFP
  320. template <typename Predicate>
  321. using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>;
  322. /// This helper class is used to match scalar and vector constants that
  323. /// satisfy a specified predicate, and bind them to an APInt.
  324. template <typename Predicate> struct api_pred_ty : public Predicate {
  325. const APInt *&Res;
  326. api_pred_ty(const APInt *&R) : Res(R) {}
  327. template <typename ITy> bool match(ITy *V) {
  328. if (const auto *CI = dyn_cast<ConstantInt>(V))
  329. if (this->isValue(CI->getValue())) {
  330. Res = &CI->getValue();
  331. return true;
  332. }
  333. if (V->getType()->isVectorTy())
  334. if (const auto *C = dyn_cast<Constant>(V))
  335. if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
  336. if (this->isValue(CI->getValue())) {
  337. Res = &CI->getValue();
  338. return true;
  339. }
  340. return false;
  341. }
  342. };
  343. /// This helper class is used to match scalar and vector constants that
  344. /// satisfy a specified predicate, and bind them to an APFloat.
  345. /// Undefs are allowed in splat vector constants.
  346. template <typename Predicate> struct apf_pred_ty : public Predicate {
  347. const APFloat *&Res;
  348. apf_pred_ty(const APFloat *&R) : Res(R) {}
  349. template <typename ITy> bool match(ITy *V) {
  350. if (const auto *CI = dyn_cast<ConstantFP>(V))
  351. if (this->isValue(CI->getValue())) {
  352. Res = &CI->getValue();
  353. return true;
  354. }
  355. if (V->getType()->isVectorTy())
  356. if (const auto *C = dyn_cast<Constant>(V))
  357. if (auto *CI = dyn_cast_or_null<ConstantFP>(
  358. C->getSplatValue(/* AllowUndef */ true)))
  359. if (this->isValue(CI->getValue())) {
  360. Res = &CI->getValue();
  361. return true;
  362. }
  363. return false;
  364. }
  365. };
  366. ///////////////////////////////////////////////////////////////////////////////
  367. //
  368. // Encapsulate constant value queries for use in templated predicate matchers.
  369. // This allows checking if constants match using compound predicates and works
  370. // with vector constants, possibly with relaxed constraints. For example, ignore
  371. // undef values.
  372. //
  373. ///////////////////////////////////////////////////////////////////////////////
  374. struct is_any_apint {
  375. bool isValue(const APInt &C) { return true; }
  376. };
  377. /// Match an integer or vector with any integral constant.
  378. /// For vectors, this includes constants with undefined elements.
  379. inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
  380. return cst_pred_ty<is_any_apint>();
  381. }
  382. struct is_all_ones {
  383. bool isValue(const APInt &C) { return C.isAllOnes(); }
  384. };
  385. /// Match an integer or vector with all bits set.
  386. /// For vectors, this includes constants with undefined elements.
  387. inline cst_pred_ty<is_all_ones> m_AllOnes() {
  388. return cst_pred_ty<is_all_ones>();
  389. }
  390. struct is_maxsignedvalue {
  391. bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
  392. };
  393. /// Match an integer or vector with values having all bits except for the high
  394. /// bit set (0x7f...).
  395. /// For vectors, this includes constants with undefined elements.
  396. inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
  397. return cst_pred_ty<is_maxsignedvalue>();
  398. }
  399. inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
  400. return V;
  401. }
  402. struct is_negative {
  403. bool isValue(const APInt &C) { return C.isNegative(); }
  404. };
  405. /// Match an integer or vector of negative values.
  406. /// For vectors, this includes constants with undefined elements.
  407. inline cst_pred_ty<is_negative> m_Negative() {
  408. return cst_pred_ty<is_negative>();
  409. }
  410. inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; }
  411. struct is_nonnegative {
  412. bool isValue(const APInt &C) { return C.isNonNegative(); }
  413. };
  414. /// Match an integer or vector of non-negative values.
  415. /// For vectors, this includes constants with undefined elements.
  416. inline cst_pred_ty<is_nonnegative> m_NonNegative() {
  417. return cst_pred_ty<is_nonnegative>();
  418. }
  419. inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; }
  420. struct is_strictlypositive {
  421. bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
  422. };
  423. /// Match an integer or vector of strictly positive values.
  424. /// For vectors, this includes constants with undefined elements.
  425. inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
  426. return cst_pred_ty<is_strictlypositive>();
  427. }
  428. inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
  429. return V;
  430. }
  431. struct is_nonpositive {
  432. bool isValue(const APInt &C) { return C.isNonPositive(); }
  433. };
  434. /// Match an integer or vector of non-positive values.
  435. /// For vectors, this includes constants with undefined elements.
  436. inline cst_pred_ty<is_nonpositive> m_NonPositive() {
  437. return cst_pred_ty<is_nonpositive>();
  438. }
  439. inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
  440. struct is_one {
  441. bool isValue(const APInt &C) { return C.isOne(); }
  442. };
  443. /// Match an integer 1 or a vector with all elements equal to 1.
  444. /// For vectors, this includes constants with undefined elements.
  445. inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
  446. struct is_zero_int {
  447. bool isValue(const APInt &C) { return C.isZero(); }
  448. };
  449. /// Match an integer 0 or a vector with all elements equal to 0.
  450. /// For vectors, this includes constants with undefined elements.
  451. inline cst_pred_ty<is_zero_int> m_ZeroInt() {
  452. return cst_pred_ty<is_zero_int>();
  453. }
  454. struct is_zero {
  455. template <typename ITy> bool match(ITy *V) {
  456. auto *C = dyn_cast<Constant>(V);
  457. // FIXME: this should be able to do something for scalable vectors
  458. return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
  459. }
  460. };
  461. /// Match any null constant or a vector with all elements equal to 0.
  462. /// For vectors, this includes constants with undefined elements.
  463. inline is_zero m_Zero() { return is_zero(); }
  464. struct is_power2 {
  465. bool isValue(const APInt &C) { return C.isPowerOf2(); }
  466. };
  467. /// Match an integer or vector power-of-2.
  468. /// For vectors, this includes constants with undefined elements.
  469. inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
  470. inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
  471. struct is_negated_power2 {
  472. bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); }
  473. };
  474. /// Match a integer or vector negated power-of-2.
  475. /// For vectors, this includes constants with undefined elements.
  476. inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
  477. return cst_pred_ty<is_negated_power2>();
  478. }
  479. inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
  480. return V;
  481. }
  482. struct is_power2_or_zero {
  483. bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
  484. };
  485. /// Match an integer or vector of 0 or power-of-2 values.
  486. /// For vectors, this includes constants with undefined elements.
  487. inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
  488. return cst_pred_ty<is_power2_or_zero>();
  489. }
  490. inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
  491. return V;
  492. }
  493. struct is_sign_mask {
  494. bool isValue(const APInt &C) { return C.isSignMask(); }
  495. };
  496. /// Match an integer or vector with only the sign bit(s) set.
  497. /// For vectors, this includes constants with undefined elements.
  498. inline cst_pred_ty<is_sign_mask> m_SignMask() {
  499. return cst_pred_ty<is_sign_mask>();
  500. }
  501. struct is_lowbit_mask {
  502. bool isValue(const APInt &C) { return C.isMask(); }
  503. };
  504. /// Match an integer or vector with only the low bit(s) set.
  505. /// For vectors, this includes constants with undefined elements.
  506. inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
  507. return cst_pred_ty<is_lowbit_mask>();
  508. }
  509. inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; }
  510. struct icmp_pred_with_threshold {
  511. ICmpInst::Predicate Pred;
  512. const APInt *Thr;
  513. bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); }
  514. };
  515. /// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
  516. /// to Threshold. For vectors, this includes constants with undefined elements.
  517. inline cst_pred_ty<icmp_pred_with_threshold>
  518. m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
  519. cst_pred_ty<icmp_pred_with_threshold> P;
  520. P.Pred = Predicate;
  521. P.Thr = &Threshold;
  522. return P;
  523. }
  524. struct is_nan {
  525. bool isValue(const APFloat &C) { return C.isNaN(); }
  526. };
  527. /// Match an arbitrary NaN constant. This includes quiet and signalling nans.
  528. /// For vectors, this includes constants with undefined elements.
  529. inline cstfp_pred_ty<is_nan> m_NaN() { return cstfp_pred_ty<is_nan>(); }
  530. struct is_nonnan {
  531. bool isValue(const APFloat &C) { return !C.isNaN(); }
  532. };
  533. /// Match a non-NaN FP constant.
  534. /// For vectors, this includes constants with undefined elements.
  535. inline cstfp_pred_ty<is_nonnan> m_NonNaN() {
  536. return cstfp_pred_ty<is_nonnan>();
  537. }
  538. struct is_inf {
  539. bool isValue(const APFloat &C) { return C.isInfinity(); }
  540. };
  541. /// Match a positive or negative infinity FP constant.
  542. /// For vectors, this includes constants with undefined elements.
  543. inline cstfp_pred_ty<is_inf> m_Inf() { return cstfp_pred_ty<is_inf>(); }
  544. struct is_noninf {
  545. bool isValue(const APFloat &C) { return !C.isInfinity(); }
  546. };
  547. /// Match a non-infinity FP constant, i.e. finite or NaN.
  548. /// For vectors, this includes constants with undefined elements.
  549. inline cstfp_pred_ty<is_noninf> m_NonInf() {
  550. return cstfp_pred_ty<is_noninf>();
  551. }
  552. struct is_finite {
  553. bool isValue(const APFloat &C) { return C.isFinite(); }
  554. };
  555. /// Match a finite FP constant, i.e. not infinity or NaN.
  556. /// For vectors, this includes constants with undefined elements.
  557. inline cstfp_pred_ty<is_finite> m_Finite() {
  558. return cstfp_pred_ty<is_finite>();
  559. }
  560. inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
  561. struct is_finitenonzero {
  562. bool isValue(const APFloat &C) { return C.isFiniteNonZero(); }
  563. };
  564. /// Match a finite non-zero FP constant.
  565. /// For vectors, this includes constants with undefined elements.
  566. inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() {
  567. return cstfp_pred_ty<is_finitenonzero>();
  568. }
  569. inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) {
  570. return V;
  571. }
  572. struct is_any_zero_fp {
  573. bool isValue(const APFloat &C) { return C.isZero(); }
  574. };
  575. /// Match a floating-point negative zero or positive zero.
  576. /// For vectors, this includes constants with undefined elements.
  577. inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
  578. return cstfp_pred_ty<is_any_zero_fp>();
  579. }
  580. struct is_pos_zero_fp {
  581. bool isValue(const APFloat &C) { return C.isPosZero(); }
  582. };
  583. /// Match a floating-point positive zero.
  584. /// For vectors, this includes constants with undefined elements.
  585. inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
  586. return cstfp_pred_ty<is_pos_zero_fp>();
  587. }
  588. struct is_neg_zero_fp {
  589. bool isValue(const APFloat &C) { return C.isNegZero(); }
  590. };
  591. /// Match a floating-point negative zero.
  592. /// For vectors, this includes constants with undefined elements.
  593. inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
  594. return cstfp_pred_ty<is_neg_zero_fp>();
  595. }
  596. struct is_non_zero_fp {
  597. bool isValue(const APFloat &C) { return C.isNonZero(); }
  598. };
  599. /// Match a floating-point non-zero.
  600. /// For vectors, this includes constants with undefined elements.
  601. inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() {
  602. return cstfp_pred_ty<is_non_zero_fp>();
  603. }
  604. ///////////////////////////////////////////////////////////////////////////////
  605. template <typename Class> struct bind_ty {
  606. Class *&VR;
  607. bind_ty(Class *&V) : VR(V) {}
  608. template <typename ITy> bool match(ITy *V) {
  609. if (auto *CV = dyn_cast<Class>(V)) {
  610. VR = CV;
  611. return true;
  612. }
  613. return false;
  614. }
  615. };
  616. /// Match a value, capturing it if we match.
  617. inline bind_ty<Value> m_Value(Value *&V) { return V; }
  618. inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
  619. /// Match an instruction, capturing it if we match.
  620. inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
  621. /// Match a unary operator, capturing it if we match.
  622. inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; }
  623. /// Match a binary operator, capturing it if we match.
  624. inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
  625. /// Match a with overflow intrinsic, capturing it if we match.
  626. inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) {
  627. return I;
  628. }
  629. inline bind_ty<const WithOverflowInst>
  630. m_WithOverflowInst(const WithOverflowInst *&I) {
  631. return I;
  632. }
  633. /// Match a Constant, capturing the value if we match.
  634. inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
  635. /// Match a ConstantInt, capturing the value if we match.
  636. inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
  637. /// Match a ConstantFP, capturing the value if we match.
  638. inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
  639. /// Match a ConstantExpr, capturing the value if we match.
  640. inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; }
  641. /// Match a basic block value, capturing it if we match.
  642. inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; }
  643. inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
  644. return V;
  645. }
  646. /// Match an arbitrary immediate Constant and ignore it.
  647. inline match_combine_and<class_match<Constant>,
  648. match_unless<constantexpr_match>>
  649. m_ImmConstant() {
  650. return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr()));
  651. }
  652. /// Match an immediate Constant, capturing the value if we match.
  653. inline match_combine_and<bind_ty<Constant>,
  654. match_unless<constantexpr_match>>
  655. m_ImmConstant(Constant *&C) {
  656. return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr()));
  657. }
  658. /// Match a specified Value*.
  659. struct specificval_ty {
  660. const Value *Val;
  661. specificval_ty(const Value *V) : Val(V) {}
  662. template <typename ITy> bool match(ITy *V) { return V == Val; }
  663. };
  664. /// Match if we have a specific specified value.
  665. inline specificval_ty m_Specific(const Value *V) { return V; }
  666. /// Stores a reference to the Value *, not the Value * itself,
  667. /// thus can be used in commutative matchers.
  668. template <typename Class> struct deferredval_ty {
  669. Class *const &Val;
  670. deferredval_ty(Class *const &V) : Val(V) {}
  671. template <typename ITy> bool match(ITy *const V) { return V == Val; }
  672. };
  673. /// Like m_Specific(), but works if the specific value to match is determined
  674. /// as part of the same match() expression. For example:
  675. /// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will
  676. /// bind X before the pattern match starts.
  677. /// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against
  678. /// whichever value m_Value(X) populated.
  679. inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
  680. inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
  681. return V;
  682. }
  683. /// Match a specified floating point value or vector of all elements of
  684. /// that value.
  685. struct specific_fpval {
  686. double Val;
  687. specific_fpval(double V) : Val(V) {}
  688. template <typename ITy> bool match(ITy *V) {
  689. if (const auto *CFP = dyn_cast<ConstantFP>(V))
  690. return CFP->isExactlyValue(Val);
  691. if (V->getType()->isVectorTy())
  692. if (const auto *C = dyn_cast<Constant>(V))
  693. if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
  694. return CFP->isExactlyValue(Val);
  695. return false;
  696. }
  697. };
  698. /// Match a specific floating point value or vector with all elements
  699. /// equal to the value.
  700. inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
  701. /// Match a float 1.0 or vector with all elements equal to 1.0.
  702. inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
  703. struct bind_const_intval_ty {
  704. uint64_t &VR;
  705. bind_const_intval_ty(uint64_t &V) : VR(V) {}
  706. template <typename ITy> bool match(ITy *V) {
  707. if (const auto *CV = dyn_cast<ConstantInt>(V))
  708. if (CV->getValue().ule(UINT64_MAX)) {
  709. VR = CV->getZExtValue();
  710. return true;
  711. }
  712. return false;
  713. }
  714. };
  715. /// Match a specified integer value or vector of all elements of that
  716. /// value.
  717. template <bool AllowUndefs> struct specific_intval {
  718. APInt Val;
  719. specific_intval(APInt V) : Val(std::move(V)) {}
  720. template <typename ITy> bool match(ITy *V) {
  721. const auto *CI = dyn_cast<ConstantInt>(V);
  722. if (!CI && V->getType()->isVectorTy())
  723. if (const auto *C = dyn_cast<Constant>(V))
  724. CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs));
  725. return CI && APInt::isSameValue(CI->getValue(), Val);
  726. }
  727. };
  728. /// Match a specific integer value or vector with all elements equal to
  729. /// the value.
  730. inline specific_intval<false> m_SpecificInt(APInt V) {
  731. return specific_intval<false>(std::move(V));
  732. }
  733. inline specific_intval<false> m_SpecificInt(uint64_t V) {
  734. return m_SpecificInt(APInt(64, V));
  735. }
  736. inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) {
  737. return specific_intval<true>(std::move(V));
  738. }
  739. inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) {
  740. return m_SpecificIntAllowUndef(APInt(64, V));
  741. }
  742. /// Match a ConstantInt and bind to its value. This does not match
  743. /// ConstantInts wider than 64-bits.
  744. inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
  745. /// Match a specified basic block value.
  746. struct specific_bbval {
  747. BasicBlock *Val;
  748. specific_bbval(BasicBlock *Val) : Val(Val) {}
  749. template <typename ITy> bool match(ITy *V) {
  750. const auto *BB = dyn_cast<BasicBlock>(V);
  751. return BB && BB == Val;
  752. }
  753. };
  754. /// Match a specific basic block value.
  755. inline specific_bbval m_SpecificBB(BasicBlock *BB) {
  756. return specific_bbval(BB);
  757. }
  758. /// A commutative-friendly version of m_Specific().
  759. inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) {
  760. return BB;
  761. }
  762. inline deferredval_ty<const BasicBlock>
  763. m_Deferred(const BasicBlock *const &BB) {
  764. return BB;
  765. }
  766. //===----------------------------------------------------------------------===//
  767. // Matcher for any binary operator.
  768. //
  769. template <typename LHS_t, typename RHS_t, bool Commutable = false>
  770. struct AnyBinaryOp_match {
  771. LHS_t L;
  772. RHS_t R;
  773. // The evaluation order is always stable, regardless of Commutability.
  774. // The LHS is always matched first.
  775. AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  776. template <typename OpTy> bool match(OpTy *V) {
  777. if (auto *I = dyn_cast<BinaryOperator>(V))
  778. return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
  779. (Commutable && L.match(I->getOperand(1)) &&
  780. R.match(I->getOperand(0)));
  781. return false;
  782. }
  783. };
  784. template <typename LHS, typename RHS>
  785. inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
  786. return AnyBinaryOp_match<LHS, RHS>(L, R);
  787. }
  788. //===----------------------------------------------------------------------===//
  789. // Matcher for any unary operator.
  790. // TODO fuse unary, binary matcher into n-ary matcher
  791. //
  792. template <typename OP_t> struct AnyUnaryOp_match {
  793. OP_t X;
  794. AnyUnaryOp_match(const OP_t &X) : X(X) {}
  795. template <typename OpTy> bool match(OpTy *V) {
  796. if (auto *I = dyn_cast<UnaryOperator>(V))
  797. return X.match(I->getOperand(0));
  798. return false;
  799. }
  800. };
  801. template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
  802. return AnyUnaryOp_match<OP_t>(X);
  803. }
  804. //===----------------------------------------------------------------------===//
  805. // Matchers for specific binary operators.
  806. //
  807. template <typename LHS_t, typename RHS_t, unsigned Opcode,
  808. bool Commutable = false>
  809. struct BinaryOp_match {
  810. LHS_t L;
  811. RHS_t R;
  812. // The evaluation order is always stable, regardless of Commutability.
  813. // The LHS is always matched first.
  814. BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  815. template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) {
  816. if (V->getValueID() == Value::InstructionVal + Opc) {
  817. auto *I = cast<BinaryOperator>(V);
  818. return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
  819. (Commutable && L.match(I->getOperand(1)) &&
  820. R.match(I->getOperand(0)));
  821. }
  822. if (auto *CE = dyn_cast<ConstantExpr>(V))
  823. return CE->getOpcode() == Opc &&
  824. ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
  825. (Commutable && L.match(CE->getOperand(1)) &&
  826. R.match(CE->getOperand(0))));
  827. return false;
  828. }
  829. template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); }
  830. };
  831. template <typename LHS, typename RHS>
  832. inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
  833. const RHS &R) {
  834. return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
  835. }
  836. template <typename LHS, typename RHS>
  837. inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
  838. const RHS &R) {
  839. return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
  840. }
  841. template <typename LHS, typename RHS>
  842. inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
  843. const RHS &R) {
  844. return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
  845. }
  846. template <typename LHS, typename RHS>
  847. inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
  848. const RHS &R) {
  849. return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
  850. }
  851. template <typename Op_t> struct FNeg_match {
  852. Op_t X;
  853. FNeg_match(const Op_t &Op) : X(Op) {}
  854. template <typename OpTy> bool match(OpTy *V) {
  855. auto *FPMO = dyn_cast<FPMathOperator>(V);
  856. if (!FPMO)
  857. return false;
  858. if (FPMO->getOpcode() == Instruction::FNeg)
  859. return X.match(FPMO->getOperand(0));
  860. if (FPMO->getOpcode() == Instruction::FSub) {
  861. if (FPMO->hasNoSignedZeros()) {
  862. // With 'nsz', any zero goes.
  863. if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
  864. return false;
  865. } else {
  866. // Without 'nsz', we need fsub -0.0, X exactly.
  867. if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
  868. return false;
  869. }
  870. return X.match(FPMO->getOperand(1));
  871. }
  872. return false;
  873. }
  874. };
  875. /// Match 'fneg X' as 'fsub -0.0, X'.
  876. template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) {
  877. return FNeg_match<OpTy>(X);
  878. }
  879. /// Match 'fneg X' as 'fsub +-0.0, X'.
  880. template <typename RHS>
  881. inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
  882. m_FNegNSZ(const RHS &X) {
  883. return m_FSub(m_AnyZeroFP(), X);
  884. }
  885. template <typename LHS, typename RHS>
  886. inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
  887. const RHS &R) {
  888. return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
  889. }
  890. template <typename LHS, typename RHS>
  891. inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
  892. const RHS &R) {
  893. return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
  894. }
  895. template <typename LHS, typename RHS>
  896. inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
  897. const RHS &R) {
  898. return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
  899. }
  900. template <typename LHS, typename RHS>
  901. inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
  902. const RHS &R) {
  903. return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
  904. }
  905. template <typename LHS, typename RHS>
  906. inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
  907. const RHS &R) {
  908. return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
  909. }
  910. template <typename LHS, typename RHS>
  911. inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
  912. const RHS &R) {
  913. return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
  914. }
  915. template <typename LHS, typename RHS>
  916. inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
  917. const RHS &R) {
  918. return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
  919. }
  920. template <typename LHS, typename RHS>
  921. inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
  922. const RHS &R) {
  923. return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
  924. }
  925. template <typename LHS, typename RHS>
  926. inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
  927. const RHS &R) {
  928. return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
  929. }
  930. template <typename LHS, typename RHS>
  931. inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
  932. const RHS &R) {
  933. return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
  934. }
  935. template <typename LHS, typename RHS>
  936. inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
  937. const RHS &R) {
  938. return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
  939. }
  940. template <typename LHS, typename RHS>
  941. inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
  942. const RHS &R) {
  943. return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
  944. }
  945. template <typename LHS, typename RHS>
  946. inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
  947. const RHS &R) {
  948. return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
  949. }
  950. template <typename LHS, typename RHS>
  951. inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
  952. const RHS &R) {
  953. return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
  954. }
  955. template <typename LHS_t, typename RHS_t, unsigned Opcode,
  956. unsigned WrapFlags = 0>
  957. struct OverflowingBinaryOp_match {
  958. LHS_t L;
  959. RHS_t R;
  960. OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
  961. : L(LHS), R(RHS) {}
  962. template <typename OpTy> bool match(OpTy *V) {
  963. if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
  964. if (Op->getOpcode() != Opcode)
  965. return false;
  966. if ((WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap) &&
  967. !Op->hasNoUnsignedWrap())
  968. return false;
  969. if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) &&
  970. !Op->hasNoSignedWrap())
  971. return false;
  972. return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
  973. }
  974. return false;
  975. }
  976. };
  977. template <typename LHS, typename RHS>
  978. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  979. OverflowingBinaryOperator::NoSignedWrap>
  980. m_NSWAdd(const LHS &L, const RHS &R) {
  981. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  982. OverflowingBinaryOperator::NoSignedWrap>(L,
  983. R);
  984. }
  985. template <typename LHS, typename RHS>
  986. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  987. OverflowingBinaryOperator::NoSignedWrap>
  988. m_NSWSub(const LHS &L, const RHS &R) {
  989. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  990. OverflowingBinaryOperator::NoSignedWrap>(L,
  991. R);
  992. }
  993. template <typename LHS, typename RHS>
  994. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  995. OverflowingBinaryOperator::NoSignedWrap>
  996. m_NSWMul(const LHS &L, const RHS &R) {
  997. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  998. OverflowingBinaryOperator::NoSignedWrap>(L,
  999. R);
  1000. }
  1001. template <typename LHS, typename RHS>
  1002. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  1003. OverflowingBinaryOperator::NoSignedWrap>
  1004. m_NSWShl(const LHS &L, const RHS &R) {
  1005. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  1006. OverflowingBinaryOperator::NoSignedWrap>(L,
  1007. R);
  1008. }
  1009. template <typename LHS, typename RHS>
  1010. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  1011. OverflowingBinaryOperator::NoUnsignedWrap>
  1012. m_NUWAdd(const LHS &L, const RHS &R) {
  1013. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  1014. OverflowingBinaryOperator::NoUnsignedWrap>(
  1015. L, R);
  1016. }
  1017. template <typename LHS, typename RHS>
  1018. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  1019. OverflowingBinaryOperator::NoUnsignedWrap>
  1020. m_NUWSub(const LHS &L, const RHS &R) {
  1021. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  1022. OverflowingBinaryOperator::NoUnsignedWrap>(
  1023. L, R);
  1024. }
  1025. template <typename LHS, typename RHS>
  1026. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  1027. OverflowingBinaryOperator::NoUnsignedWrap>
  1028. m_NUWMul(const LHS &L, const RHS &R) {
  1029. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  1030. OverflowingBinaryOperator::NoUnsignedWrap>(
  1031. L, R);
  1032. }
  1033. template <typename LHS, typename RHS>
  1034. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  1035. OverflowingBinaryOperator::NoUnsignedWrap>
  1036. m_NUWShl(const LHS &L, const RHS &R) {
  1037. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  1038. OverflowingBinaryOperator::NoUnsignedWrap>(
  1039. L, R);
  1040. }
  1041. template <typename LHS_t, typename RHS_t, bool Commutable = false>
  1042. struct SpecificBinaryOp_match
  1043. : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> {
  1044. unsigned Opcode;
  1045. SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS)
  1046. : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {}
  1047. template <typename OpTy> bool match(OpTy *V) {
  1048. return BinaryOp_match<LHS_t, RHS_t, 0, Commutable>::match(Opcode, V);
  1049. }
  1050. };
  1051. /// Matches a specific opcode.
  1052. template <typename LHS, typename RHS>
  1053. inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L,
  1054. const RHS &R) {
  1055. return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R);
  1056. }
  1057. //===----------------------------------------------------------------------===//
  1058. // Class that matches a group of binary opcodes.
  1059. //
  1060. template <typename LHS_t, typename RHS_t, typename Predicate>
  1061. struct BinOpPred_match : Predicate {
  1062. LHS_t L;
  1063. RHS_t R;
  1064. BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  1065. template <typename OpTy> bool match(OpTy *V) {
  1066. if (auto *I = dyn_cast<Instruction>(V))
  1067. return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
  1068. R.match(I->getOperand(1));
  1069. if (auto *CE = dyn_cast<ConstantExpr>(V))
  1070. return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
  1071. R.match(CE->getOperand(1));
  1072. return false;
  1073. }
  1074. };
  1075. struct is_shift_op {
  1076. bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
  1077. };
  1078. struct is_right_shift_op {
  1079. bool isOpType(unsigned Opcode) {
  1080. return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
  1081. }
  1082. };
  1083. struct is_logical_shift_op {
  1084. bool isOpType(unsigned Opcode) {
  1085. return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
  1086. }
  1087. };
  1088. struct is_bitwiselogic_op {
  1089. bool isOpType(unsigned Opcode) {
  1090. return Instruction::isBitwiseLogicOp(Opcode);
  1091. }
  1092. };
  1093. struct is_idiv_op {
  1094. bool isOpType(unsigned Opcode) {
  1095. return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
  1096. }
  1097. };
  1098. struct is_irem_op {
  1099. bool isOpType(unsigned Opcode) {
  1100. return Opcode == Instruction::SRem || Opcode == Instruction::URem;
  1101. }
  1102. };
  1103. /// Matches shift operations.
  1104. template <typename LHS, typename RHS>
  1105. inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
  1106. const RHS &R) {
  1107. return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
  1108. }
  1109. /// Matches logical shift operations.
  1110. template <typename LHS, typename RHS>
  1111. inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
  1112. const RHS &R) {
  1113. return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
  1114. }
  1115. /// Matches logical shift operations.
  1116. template <typename LHS, typename RHS>
  1117. inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
  1118. m_LogicalShift(const LHS &L, const RHS &R) {
  1119. return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
  1120. }
  1121. /// Matches bitwise logic operations.
  1122. template <typename LHS, typename RHS>
  1123. inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
  1124. m_BitwiseLogic(const LHS &L, const RHS &R) {
  1125. return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
  1126. }
  1127. /// Matches integer division operations.
  1128. template <typename LHS, typename RHS>
  1129. inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
  1130. const RHS &R) {
  1131. return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
  1132. }
  1133. /// Matches integer remainder operations.
  1134. template <typename LHS, typename RHS>
  1135. inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L,
  1136. const RHS &R) {
  1137. return BinOpPred_match<LHS, RHS, is_irem_op>(L, R);
  1138. }
  1139. //===----------------------------------------------------------------------===//
  1140. // Class that matches exact binary ops.
  1141. //
  1142. template <typename SubPattern_t> struct Exact_match {
  1143. SubPattern_t SubPattern;
  1144. Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
  1145. template <typename OpTy> bool match(OpTy *V) {
  1146. if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
  1147. return PEO->isExact() && SubPattern.match(V);
  1148. return false;
  1149. }
  1150. };
  1151. template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
  1152. return SubPattern;
  1153. }
  1154. //===----------------------------------------------------------------------===//
  1155. // Matchers for CmpInst classes
  1156. //
  1157. template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
  1158. bool Commutable = false>
  1159. struct CmpClass_match {
  1160. PredicateTy &Predicate;
  1161. LHS_t L;
  1162. RHS_t R;
  1163. // The evaluation order is always stable, regardless of Commutability.
  1164. // The LHS is always matched first.
  1165. CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
  1166. : Predicate(Pred), L(LHS), R(RHS) {}
  1167. template <typename OpTy> bool match(OpTy *V) {
  1168. if (auto *I = dyn_cast<Class>(V)) {
  1169. if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
  1170. Predicate = I->getPredicate();
  1171. return true;
  1172. } else if (Commutable && L.match(I->getOperand(1)) &&
  1173. R.match(I->getOperand(0))) {
  1174. Predicate = I->getSwappedPredicate();
  1175. return true;
  1176. }
  1177. }
  1178. return false;
  1179. }
  1180. };
  1181. template <typename LHS, typename RHS>
  1182. inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
  1183. m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  1184. return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
  1185. }
  1186. template <typename LHS, typename RHS>
  1187. inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
  1188. m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  1189. return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
  1190. }
  1191. template <typename LHS, typename RHS>
  1192. inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
  1193. m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  1194. return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
  1195. }
  1196. //===----------------------------------------------------------------------===//
  1197. // Matchers for instructions with a given opcode and number of operands.
  1198. //
  1199. /// Matches instructions with Opcode and three operands.
  1200. template <typename T0, unsigned Opcode> struct OneOps_match {
  1201. T0 Op1;
  1202. OneOps_match(const T0 &Op1) : Op1(Op1) {}
  1203. template <typename OpTy> bool match(OpTy *V) {
  1204. if (V->getValueID() == Value::InstructionVal + Opcode) {
  1205. auto *I = cast<Instruction>(V);
  1206. return Op1.match(I->getOperand(0));
  1207. }
  1208. return false;
  1209. }
  1210. };
  1211. /// Matches instructions with Opcode and three operands.
  1212. template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
  1213. T0 Op1;
  1214. T1 Op2;
  1215. TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
  1216. template <typename OpTy> bool match(OpTy *V) {
  1217. if (V->getValueID() == Value::InstructionVal + Opcode) {
  1218. auto *I = cast<Instruction>(V);
  1219. return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
  1220. }
  1221. return false;
  1222. }
  1223. };
  1224. /// Matches instructions with Opcode and three operands.
  1225. template <typename T0, typename T1, typename T2, unsigned Opcode>
  1226. struct ThreeOps_match {
  1227. T0 Op1;
  1228. T1 Op2;
  1229. T2 Op3;
  1230. ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
  1231. : Op1(Op1), Op2(Op2), Op3(Op3) {}
  1232. template <typename OpTy> bool match(OpTy *V) {
  1233. if (V->getValueID() == Value::InstructionVal + Opcode) {
  1234. auto *I = cast<Instruction>(V);
  1235. return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
  1236. Op3.match(I->getOperand(2));
  1237. }
  1238. return false;
  1239. }
  1240. };
  1241. /// Matches SelectInst.
  1242. template <typename Cond, typename LHS, typename RHS>
  1243. inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
  1244. m_Select(const Cond &C, const LHS &L, const RHS &R) {
  1245. return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
  1246. }
  1247. /// This matches a select of two constants, e.g.:
  1248. /// m_SelectCst<-1, 0>(m_Value(V))
  1249. template <int64_t L, int64_t R, typename Cond>
  1250. inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
  1251. Instruction::Select>
  1252. m_SelectCst(const Cond &C) {
  1253. return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
  1254. }
  1255. /// Matches FreezeInst.
  1256. template <typename OpTy>
  1257. inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
  1258. return OneOps_match<OpTy, Instruction::Freeze>(Op);
  1259. }
  1260. /// Matches InsertElementInst.
  1261. template <typename Val_t, typename Elt_t, typename Idx_t>
  1262. inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
  1263. m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
  1264. return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
  1265. Val, Elt, Idx);
  1266. }
  1267. /// Matches ExtractElementInst.
  1268. template <typename Val_t, typename Idx_t>
  1269. inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
  1270. m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
  1271. return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
  1272. }
  1273. /// Matches shuffle.
  1274. template <typename T0, typename T1, typename T2> struct Shuffle_match {
  1275. T0 Op1;
  1276. T1 Op2;
  1277. T2 Mask;
  1278. Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
  1279. : Op1(Op1), Op2(Op2), Mask(Mask) {}
  1280. template <typename OpTy> bool match(OpTy *V) {
  1281. if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
  1282. return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
  1283. Mask.match(I->getShuffleMask());
  1284. }
  1285. return false;
  1286. }
  1287. };
  1288. struct m_Mask {
  1289. ArrayRef<int> &MaskRef;
  1290. m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
  1291. bool match(ArrayRef<int> Mask) {
  1292. MaskRef = Mask;
  1293. return true;
  1294. }
  1295. };
  1296. struct m_ZeroMask {
  1297. bool match(ArrayRef<int> Mask) {
  1298. return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
  1299. }
  1300. };
  1301. struct m_SpecificMask {
  1302. ArrayRef<int> &MaskRef;
  1303. m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
  1304. bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
  1305. };
  1306. struct m_SplatOrUndefMask {
  1307. int &SplatIndex;
  1308. m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {}
  1309. bool match(ArrayRef<int> Mask) {
  1310. auto First = find_if(Mask, [](int Elem) { return Elem != -1; });
  1311. if (First == Mask.end())
  1312. return false;
  1313. SplatIndex = *First;
  1314. return all_of(Mask,
  1315. [First](int Elem) { return Elem == *First || Elem == -1; });
  1316. }
  1317. };
  1318. /// Matches ShuffleVectorInst independently of mask value.
  1319. template <typename V1_t, typename V2_t>
  1320. inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>
  1321. m_Shuffle(const V1_t &v1, const V2_t &v2) {
  1322. return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2);
  1323. }
  1324. template <typename V1_t, typename V2_t, typename Mask_t>
  1325. inline Shuffle_match<V1_t, V2_t, Mask_t>
  1326. m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
  1327. return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
  1328. }
  1329. /// Matches LoadInst.
  1330. template <typename OpTy>
  1331. inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
  1332. return OneOps_match<OpTy, Instruction::Load>(Op);
  1333. }
  1334. /// Matches StoreInst.
  1335. template <typename ValueOpTy, typename PointerOpTy>
  1336. inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
  1337. m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
  1338. return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
  1339. PointerOp);
  1340. }
  1341. //===----------------------------------------------------------------------===//
  1342. // Matchers for CastInst classes
  1343. //
  1344. template <typename Op_t, unsigned Opcode> struct CastClass_match {
  1345. Op_t Op;
  1346. CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
  1347. template <typename OpTy> bool match(OpTy *V) {
  1348. if (auto *O = dyn_cast<Operator>(V))
  1349. return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
  1350. return false;
  1351. }
  1352. };
  1353. /// Matches BitCast.
  1354. template <typename OpTy>
  1355. inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
  1356. return CastClass_match<OpTy, Instruction::BitCast>(Op);
  1357. }
  1358. /// Matches PtrToInt.
  1359. template <typename OpTy>
  1360. inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
  1361. return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
  1362. }
  1363. /// Matches IntToPtr.
  1364. template <typename OpTy>
  1365. inline CastClass_match<OpTy, Instruction::IntToPtr> m_IntToPtr(const OpTy &Op) {
  1366. return CastClass_match<OpTy, Instruction::IntToPtr>(Op);
  1367. }
  1368. /// Matches Trunc.
  1369. template <typename OpTy>
  1370. inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
  1371. return CastClass_match<OpTy, Instruction::Trunc>(Op);
  1372. }
  1373. template <typename OpTy>
  1374. inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy>
  1375. m_TruncOrSelf(const OpTy &Op) {
  1376. return m_CombineOr(m_Trunc(Op), Op);
  1377. }
  1378. /// Matches SExt.
  1379. template <typename OpTy>
  1380. inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
  1381. return CastClass_match<OpTy, Instruction::SExt>(Op);
  1382. }
  1383. /// Matches ZExt.
  1384. template <typename OpTy>
  1385. inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
  1386. return CastClass_match<OpTy, Instruction::ZExt>(Op);
  1387. }
  1388. template <typename OpTy>
  1389. inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy>
  1390. m_ZExtOrSelf(const OpTy &Op) {
  1391. return m_CombineOr(m_ZExt(Op), Op);
  1392. }
  1393. template <typename OpTy>
  1394. inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy>
  1395. m_SExtOrSelf(const OpTy &Op) {
  1396. return m_CombineOr(m_SExt(Op), Op);
  1397. }
  1398. template <typename OpTy>
  1399. inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
  1400. CastClass_match<OpTy, Instruction::SExt>>
  1401. m_ZExtOrSExt(const OpTy &Op) {
  1402. return m_CombineOr(m_ZExt(Op), m_SExt(Op));
  1403. }
  1404. template <typename OpTy>
  1405. inline match_combine_or<
  1406. match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
  1407. CastClass_match<OpTy, Instruction::SExt>>,
  1408. OpTy>
  1409. m_ZExtOrSExtOrSelf(const OpTy &Op) {
  1410. return m_CombineOr(m_ZExtOrSExt(Op), Op);
  1411. }
  1412. template <typename OpTy>
  1413. inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
  1414. return CastClass_match<OpTy, Instruction::UIToFP>(Op);
  1415. }
  1416. template <typename OpTy>
  1417. inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
  1418. return CastClass_match<OpTy, Instruction::SIToFP>(Op);
  1419. }
  1420. template <typename OpTy>
  1421. inline CastClass_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) {
  1422. return CastClass_match<OpTy, Instruction::FPToUI>(Op);
  1423. }
  1424. template <typename OpTy>
  1425. inline CastClass_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) {
  1426. return CastClass_match<OpTy, Instruction::FPToSI>(Op);
  1427. }
  1428. template <typename OpTy>
  1429. inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
  1430. return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
  1431. }
  1432. template <typename OpTy>
  1433. inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
  1434. return CastClass_match<OpTy, Instruction::FPExt>(Op);
  1435. }
  1436. //===----------------------------------------------------------------------===//
  1437. // Matchers for control flow.
  1438. //
  1439. struct br_match {
  1440. BasicBlock *&Succ;
  1441. br_match(BasicBlock *&Succ) : Succ(Succ) {}
  1442. template <typename OpTy> bool match(OpTy *V) {
  1443. if (auto *BI = dyn_cast<BranchInst>(V))
  1444. if (BI->isUnconditional()) {
  1445. Succ = BI->getSuccessor(0);
  1446. return true;
  1447. }
  1448. return false;
  1449. }
  1450. };
  1451. inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
  1452. template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
  1453. struct brc_match {
  1454. Cond_t Cond;
  1455. TrueBlock_t T;
  1456. FalseBlock_t F;
  1457. brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
  1458. : Cond(C), T(t), F(f) {}
  1459. template <typename OpTy> bool match(OpTy *V) {
  1460. if (auto *BI = dyn_cast<BranchInst>(V))
  1461. if (BI->isConditional() && Cond.match(BI->getCondition()))
  1462. return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
  1463. return false;
  1464. }
  1465. };
  1466. template <typename Cond_t>
  1467. inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>
  1468. m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
  1469. return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>(
  1470. C, m_BasicBlock(T), m_BasicBlock(F));
  1471. }
  1472. template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
  1473. inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t>
  1474. m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
  1475. return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F);
  1476. }
  1477. //===----------------------------------------------------------------------===//
  1478. // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
  1479. //
  1480. template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
  1481. bool Commutable = false>
  1482. struct MaxMin_match {
  1483. using PredType = Pred_t;
  1484. LHS_t L;
  1485. RHS_t R;
  1486. // The evaluation order is always stable, regardless of Commutability.
  1487. // The LHS is always matched first.
  1488. MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  1489. template <typename OpTy> bool match(OpTy *V) {
  1490. if (auto *II = dyn_cast<IntrinsicInst>(V)) {
  1491. Intrinsic::ID IID = II->getIntrinsicID();
  1492. if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
  1493. (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
  1494. (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
  1495. (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
  1496. Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
  1497. return (L.match(LHS) && R.match(RHS)) ||
  1498. (Commutable && L.match(RHS) && R.match(LHS));
  1499. }
  1500. }
  1501. // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
  1502. auto *SI = dyn_cast<SelectInst>(V);
  1503. if (!SI)
  1504. return false;
  1505. auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
  1506. if (!Cmp)
  1507. return false;
  1508. // At this point we have a select conditioned on a comparison. Check that
  1509. // it is the values returned by the select that are being compared.
  1510. auto *TrueVal = SI->getTrueValue();
  1511. auto *FalseVal = SI->getFalseValue();
  1512. auto *LHS = Cmp->getOperand(0);
  1513. auto *RHS = Cmp->getOperand(1);
  1514. if ((TrueVal != LHS || FalseVal != RHS) &&
  1515. (TrueVal != RHS || FalseVal != LHS))
  1516. return false;
  1517. typename CmpInst_t::Predicate Pred =
  1518. LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
  1519. // Does "(x pred y) ? x : y" represent the desired max/min operation?
  1520. if (!Pred_t::match(Pred))
  1521. return false;
  1522. // It does! Bind the operands.
  1523. return (L.match(LHS) && R.match(RHS)) ||
  1524. (Commutable && L.match(RHS) && R.match(LHS));
  1525. }
  1526. };
  1527. /// Helper class for identifying signed max predicates.
  1528. struct smax_pred_ty {
  1529. static bool match(ICmpInst::Predicate Pred) {
  1530. return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
  1531. }
  1532. };
  1533. /// Helper class for identifying signed min predicates.
  1534. struct smin_pred_ty {
  1535. static bool match(ICmpInst::Predicate Pred) {
  1536. return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
  1537. }
  1538. };
  1539. /// Helper class for identifying unsigned max predicates.
  1540. struct umax_pred_ty {
  1541. static bool match(ICmpInst::Predicate Pred) {
  1542. return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
  1543. }
  1544. };
  1545. /// Helper class for identifying unsigned min predicates.
  1546. struct umin_pred_ty {
  1547. static bool match(ICmpInst::Predicate Pred) {
  1548. return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
  1549. }
  1550. };
  1551. /// Helper class for identifying ordered max predicates.
  1552. struct ofmax_pred_ty {
  1553. static bool match(FCmpInst::Predicate Pred) {
  1554. return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
  1555. }
  1556. };
  1557. /// Helper class for identifying ordered min predicates.
  1558. struct ofmin_pred_ty {
  1559. static bool match(FCmpInst::Predicate Pred) {
  1560. return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
  1561. }
  1562. };
  1563. /// Helper class for identifying unordered max predicates.
  1564. struct ufmax_pred_ty {
  1565. static bool match(FCmpInst::Predicate Pred) {
  1566. return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
  1567. }
  1568. };
  1569. /// Helper class for identifying unordered min predicates.
  1570. struct ufmin_pred_ty {
  1571. static bool match(FCmpInst::Predicate Pred) {
  1572. return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
  1573. }
  1574. };
  1575. template <typename LHS, typename RHS>
  1576. inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
  1577. const RHS &R) {
  1578. return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
  1579. }
  1580. template <typename LHS, typename RHS>
  1581. inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
  1582. const RHS &R) {
  1583. return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
  1584. }
  1585. template <typename LHS, typename RHS>
  1586. inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
  1587. const RHS &R) {
  1588. return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
  1589. }
  1590. template <typename LHS, typename RHS>
  1591. inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
  1592. const RHS &R) {
  1593. return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
  1594. }
  1595. template <typename LHS, typename RHS>
  1596. inline match_combine_or<
  1597. match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>,
  1598. MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>,
  1599. match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>,
  1600. MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>>
  1601. m_MaxOrMin(const LHS &L, const RHS &R) {
  1602. return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
  1603. m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
  1604. }
  1605. /// Match an 'ordered' floating point maximum function.
  1606. /// Floating point has one special value 'NaN'. Therefore, there is no total
  1607. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  1608. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
  1609. /// semantics. In the presence of 'NaN' we have to preserve the original
  1610. /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
  1611. ///
  1612. /// max(L, R) iff L and R are not NaN
  1613. /// m_OrdFMax(L, R) = R iff L or R are NaN
  1614. template <typename LHS, typename RHS>
  1615. inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
  1616. const RHS &R) {
  1617. return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
  1618. }
  1619. /// Match an 'ordered' floating point minimum function.
  1620. /// Floating point has one special value 'NaN'. Therefore, there is no total
  1621. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  1622. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
  1623. /// semantics. In the presence of 'NaN' we have to preserve the original
  1624. /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
  1625. ///
  1626. /// min(L, R) iff L and R are not NaN
  1627. /// m_OrdFMin(L, R) = R iff L or R are NaN
  1628. template <typename LHS, typename RHS>
  1629. inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
  1630. const RHS &R) {
  1631. return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
  1632. }
  1633. /// Match an 'unordered' floating point maximum function.
  1634. /// Floating point has one special value 'NaN'. Therefore, there is no total
  1635. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  1636. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
  1637. /// semantics. In the presence of 'NaN' we have to preserve the original
  1638. /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
  1639. ///
  1640. /// max(L, R) iff L and R are not NaN
  1641. /// m_UnordFMax(L, R) = L iff L or R are NaN
  1642. template <typename LHS, typename RHS>
  1643. inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
  1644. m_UnordFMax(const LHS &L, const RHS &R) {
  1645. return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
  1646. }
  1647. /// Match an 'unordered' floating point minimum function.
  1648. /// Floating point has one special value 'NaN'. Therefore, there is no total
  1649. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  1650. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
  1651. /// semantics. In the presence of 'NaN' we have to preserve the original
  1652. /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
  1653. ///
  1654. /// min(L, R) iff L and R are not NaN
  1655. /// m_UnordFMin(L, R) = L iff L or R are NaN
  1656. template <typename LHS, typename RHS>
  1657. inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
  1658. m_UnordFMin(const LHS &L, const RHS &R) {
  1659. return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
  1660. }
  1661. //===----------------------------------------------------------------------===//
  1662. // Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
  1663. // Note that S might be matched to other instructions than AddInst.
  1664. //
  1665. template <typename LHS_t, typename RHS_t, typename Sum_t>
  1666. struct UAddWithOverflow_match {
  1667. LHS_t L;
  1668. RHS_t R;
  1669. Sum_t S;
  1670. UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
  1671. : L(L), R(R), S(S) {}
  1672. template <typename OpTy> bool match(OpTy *V) {
  1673. Value *ICmpLHS, *ICmpRHS;
  1674. ICmpInst::Predicate Pred;
  1675. if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
  1676. return false;
  1677. Value *AddLHS, *AddRHS;
  1678. auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
  1679. // (a + b) u< a, (a + b) u< b
  1680. if (Pred == ICmpInst::ICMP_ULT)
  1681. if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
  1682. return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
  1683. // a >u (a + b), b >u (a + b)
  1684. if (Pred == ICmpInst::ICMP_UGT)
  1685. if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
  1686. return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
  1687. Value *Op1;
  1688. auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
  1689. // (a ^ -1) <u b
  1690. if (Pred == ICmpInst::ICMP_ULT) {
  1691. if (XorExpr.match(ICmpLHS))
  1692. return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
  1693. }
  1694. // b > u (a ^ -1)
  1695. if (Pred == ICmpInst::ICMP_UGT) {
  1696. if (XorExpr.match(ICmpRHS))
  1697. return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
  1698. }
  1699. // Match special-case for increment-by-1.
  1700. if (Pred == ICmpInst::ICMP_EQ) {
  1701. // (a + 1) == 0
  1702. // (1 + a) == 0
  1703. if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
  1704. (m_One().match(AddLHS) || m_One().match(AddRHS)))
  1705. return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
  1706. // 0 == (a + 1)
  1707. // 0 == (1 + a)
  1708. if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
  1709. (m_One().match(AddLHS) || m_One().match(AddRHS)))
  1710. return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
  1711. }
  1712. return false;
  1713. }
  1714. };
  1715. /// Match an icmp instruction checking for unsigned overflow on addition.
  1716. ///
  1717. /// S is matched to the addition whose result is being checked for overflow, and
  1718. /// L and R are matched to the LHS and RHS of S.
  1719. template <typename LHS_t, typename RHS_t, typename Sum_t>
  1720. UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
  1721. m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
  1722. return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
  1723. }
  1724. template <typename Opnd_t> struct Argument_match {
  1725. unsigned OpI;
  1726. Opnd_t Val;
  1727. Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
  1728. template <typename OpTy> bool match(OpTy *V) {
  1729. // FIXME: Should likely be switched to use `CallBase`.
  1730. if (const auto *CI = dyn_cast<CallInst>(V))
  1731. return Val.match(CI->getArgOperand(OpI));
  1732. return false;
  1733. }
  1734. };
  1735. /// Match an argument.
  1736. template <unsigned OpI, typename Opnd_t>
  1737. inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
  1738. return Argument_match<Opnd_t>(OpI, Op);
  1739. }
  1740. /// Intrinsic matchers.
  1741. struct IntrinsicID_match {
  1742. unsigned ID;
  1743. IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
  1744. template <typename OpTy> bool match(OpTy *V) {
  1745. if (const auto *CI = dyn_cast<CallInst>(V))
  1746. if (const auto *F = CI->getCalledFunction())
  1747. return F->getIntrinsicID() == ID;
  1748. return false;
  1749. }
  1750. };
  1751. /// Intrinsic matches are combinations of ID matchers, and argument
  1752. /// matchers. Higher arity matcher are defined recursively in terms of and-ing
  1753. /// them with lower arity matchers. Here's some convenient typedefs for up to
  1754. /// several arguments, and more can be added as needed
  1755. template <typename T0 = void, typename T1 = void, typename T2 = void,
  1756. typename T3 = void, typename T4 = void, typename T5 = void,
  1757. typename T6 = void, typename T7 = void, typename T8 = void,
  1758. typename T9 = void, typename T10 = void>
  1759. struct m_Intrinsic_Ty;
  1760. template <typename T0> struct m_Intrinsic_Ty<T0> {
  1761. using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
  1762. };
  1763. template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
  1764. using Ty =
  1765. match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
  1766. };
  1767. template <typename T0, typename T1, typename T2>
  1768. struct m_Intrinsic_Ty<T0, T1, T2> {
  1769. using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
  1770. Argument_match<T2>>;
  1771. };
  1772. template <typename T0, typename T1, typename T2, typename T3>
  1773. struct m_Intrinsic_Ty<T0, T1, T2, T3> {
  1774. using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
  1775. Argument_match<T3>>;
  1776. };
  1777. template <typename T0, typename T1, typename T2, typename T3, typename T4>
  1778. struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
  1779. using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty,
  1780. Argument_match<T4>>;
  1781. };
  1782. template <typename T0, typename T1, typename T2, typename T3, typename T4,
  1783. typename T5>
  1784. struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> {
  1785. using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty,
  1786. Argument_match<T5>>;
  1787. };
  1788. /// Match intrinsic calls like this:
  1789. /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
  1790. template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
  1791. return IntrinsicID_match(IntrID);
  1792. }
  1793. /// Matches MaskedLoad Intrinsic.
  1794. template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
  1795. inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty
  1796. m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
  1797. const Opnd3 &Op3) {
  1798. return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3);
  1799. }
  1800. /// Matches MaskedGather Intrinsic.
  1801. template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3>
  1802. inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty
  1803. m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2,
  1804. const Opnd3 &Op3) {
  1805. return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3);
  1806. }
  1807. template <Intrinsic::ID IntrID, typename T0>
  1808. inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
  1809. return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
  1810. }
  1811. template <Intrinsic::ID IntrID, typename T0, typename T1>
  1812. inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
  1813. const T1 &Op1) {
  1814. return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
  1815. }
  1816. template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
  1817. inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
  1818. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
  1819. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
  1820. }
  1821. template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
  1822. typename T3>
  1823. inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
  1824. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
  1825. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
  1826. }
  1827. template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
  1828. typename T3, typename T4>
  1829. inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty
  1830. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
  1831. const T4 &Op4) {
  1832. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
  1833. m_Argument<4>(Op4));
  1834. }
  1835. template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
  1836. typename T3, typename T4, typename T5>
  1837. inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty
  1838. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
  1839. const T4 &Op4, const T5 &Op5) {
  1840. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
  1841. m_Argument<5>(Op5));
  1842. }
  1843. // Helper intrinsic matching specializations.
  1844. template <typename Opnd0>
  1845. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
  1846. return m_Intrinsic<Intrinsic::bitreverse>(Op0);
  1847. }
  1848. template <typename Opnd0>
  1849. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
  1850. return m_Intrinsic<Intrinsic::bswap>(Op0);
  1851. }
  1852. template <typename Opnd0>
  1853. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
  1854. return m_Intrinsic<Intrinsic::fabs>(Op0);
  1855. }
  1856. template <typename Opnd0>
  1857. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
  1858. return m_Intrinsic<Intrinsic::canonicalize>(Op0);
  1859. }
  1860. template <typename Opnd0, typename Opnd1>
  1861. inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
  1862. const Opnd1 &Op1) {
  1863. return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
  1864. }
  1865. template <typename Opnd0, typename Opnd1>
  1866. inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
  1867. const Opnd1 &Op1) {
  1868. return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
  1869. }
  1870. template <typename Opnd0, typename Opnd1, typename Opnd2>
  1871. inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
  1872. m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
  1873. return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
  1874. }
  1875. template <typename Opnd0, typename Opnd1, typename Opnd2>
  1876. inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
  1877. m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
  1878. return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
  1879. }
  1880. template <typename Opnd0>
  1881. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) {
  1882. return m_Intrinsic<Intrinsic::sqrt>(Op0);
  1883. }
  1884. template <typename Opnd0, typename Opnd1>
  1885. inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0,
  1886. const Opnd1 &Op1) {
  1887. return m_Intrinsic<Intrinsic::copysign>(Op0, Op1);
  1888. }
  1889. template <typename Opnd0>
  1890. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) {
  1891. return m_Intrinsic<Intrinsic::experimental_vector_reverse>(Op0);
  1892. }
  1893. //===----------------------------------------------------------------------===//
  1894. // Matchers for two-operands operators with the operators in either order
  1895. //
  1896. /// Matches a BinaryOperator with LHS and RHS in either order.
  1897. template <typename LHS, typename RHS>
  1898. inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
  1899. return AnyBinaryOp_match<LHS, RHS, true>(L, R);
  1900. }
  1901. /// Matches an ICmp with a predicate over LHS and RHS in either order.
  1902. /// Swaps the predicate if operands are commuted.
  1903. template <typename LHS, typename RHS>
  1904. inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
  1905. m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  1906. return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
  1907. R);
  1908. }
  1909. /// Matches a specific opcode with LHS and RHS in either order.
  1910. template <typename LHS, typename RHS>
  1911. inline SpecificBinaryOp_match<LHS, RHS, true>
  1912. m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) {
  1913. return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R);
  1914. }
  1915. /// Matches a Add with LHS and RHS in either order.
  1916. template <typename LHS, typename RHS>
  1917. inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
  1918. const RHS &R) {
  1919. return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
  1920. }
  1921. /// Matches a Mul with LHS and RHS in either order.
  1922. template <typename LHS, typename RHS>
  1923. inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
  1924. const RHS &R) {
  1925. return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
  1926. }
  1927. /// Matches an And with LHS and RHS in either order.
  1928. template <typename LHS, typename RHS>
  1929. inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
  1930. const RHS &R) {
  1931. return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
  1932. }
  1933. /// Matches an Or with LHS and RHS in either order.
  1934. template <typename LHS, typename RHS>
  1935. inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
  1936. const RHS &R) {
  1937. return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
  1938. }
  1939. /// Matches an Xor with LHS and RHS in either order.
  1940. template <typename LHS, typename RHS>
  1941. inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
  1942. const RHS &R) {
  1943. return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
  1944. }
  1945. /// Matches a 'Neg' as 'sub 0, V'.
  1946. template <typename ValTy>
  1947. inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
  1948. m_Neg(const ValTy &V) {
  1949. return m_Sub(m_ZeroInt(), V);
  1950. }
  1951. /// Matches a 'Neg' as 'sub nsw 0, V'.
  1952. template <typename ValTy>
  1953. inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy,
  1954. Instruction::Sub,
  1955. OverflowingBinaryOperator::NoSignedWrap>
  1956. m_NSWNeg(const ValTy &V) {
  1957. return m_NSWSub(m_ZeroInt(), V);
  1958. }
  1959. /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
  1960. /// NOTE: we first match the 'Not' (by matching '-1'),
  1961. /// and only then match the inner matcher!
  1962. template <typename ValTy>
  1963. inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true>
  1964. m_Not(const ValTy &V) {
  1965. return m_c_Xor(m_AllOnes(), V);
  1966. }
  1967. template <typename ValTy> struct NotForbidUndef_match {
  1968. ValTy Val;
  1969. NotForbidUndef_match(const ValTy &V) : Val(V) {}
  1970. template <typename OpTy> bool match(OpTy *V) {
  1971. // We do not use m_c_Xor because that could match an arbitrary APInt that is
  1972. // not -1 as C and then fail to match the other operand if it is -1.
  1973. // This code should still work even when both operands are constants.
  1974. Value *X;
  1975. const APInt *C;
  1976. if (m_Xor(m_Value(X), m_APIntForbidUndef(C)).match(V) && C->isAllOnes())
  1977. return Val.match(X);
  1978. if (m_Xor(m_APIntForbidUndef(C), m_Value(X)).match(V) && C->isAllOnes())
  1979. return Val.match(X);
  1980. return false;
  1981. }
  1982. };
  1983. /// Matches a bitwise 'not' as 'xor V, -1' or 'xor -1, V'. For vectors, the
  1984. /// constant value must be composed of only -1 scalar elements.
  1985. template <typename ValTy>
  1986. inline NotForbidUndef_match<ValTy> m_NotForbidUndef(const ValTy &V) {
  1987. return NotForbidUndef_match<ValTy>(V);
  1988. }
  1989. /// Matches an SMin with LHS and RHS in either order.
  1990. template <typename LHS, typename RHS>
  1991. inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
  1992. m_c_SMin(const LHS &L, const RHS &R) {
  1993. return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
  1994. }
  1995. /// Matches an SMax with LHS and RHS in either order.
  1996. template <typename LHS, typename RHS>
  1997. inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
  1998. m_c_SMax(const LHS &L, const RHS &R) {
  1999. return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
  2000. }
  2001. /// Matches a UMin with LHS and RHS in either order.
  2002. template <typename LHS, typename RHS>
  2003. inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
  2004. m_c_UMin(const LHS &L, const RHS &R) {
  2005. return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
  2006. }
  2007. /// Matches a UMax with LHS and RHS in either order.
  2008. template <typename LHS, typename RHS>
  2009. inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
  2010. m_c_UMax(const LHS &L, const RHS &R) {
  2011. return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
  2012. }
  2013. template <typename LHS, typename RHS>
  2014. inline match_combine_or<
  2015. match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>,
  2016. MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>,
  2017. match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>,
  2018. MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>>
  2019. m_c_MaxOrMin(const LHS &L, const RHS &R) {
  2020. return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
  2021. m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
  2022. }
  2023. /// Matches FAdd with LHS and RHS in either order.
  2024. template <typename LHS, typename RHS>
  2025. inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
  2026. m_c_FAdd(const LHS &L, const RHS &R) {
  2027. return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
  2028. }
  2029. /// Matches FMul with LHS and RHS in either order.
  2030. template <typename LHS, typename RHS>
  2031. inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
  2032. m_c_FMul(const LHS &L, const RHS &R) {
  2033. return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
  2034. }
  2035. template <typename Opnd_t> struct Signum_match {
  2036. Opnd_t Val;
  2037. Signum_match(const Opnd_t &V) : Val(V) {}
  2038. template <typename OpTy> bool match(OpTy *V) {
  2039. unsigned TypeSize = V->getType()->getScalarSizeInBits();
  2040. if (TypeSize == 0)
  2041. return false;
  2042. unsigned ShiftWidth = TypeSize - 1;
  2043. Value *OpL = nullptr, *OpR = nullptr;
  2044. // This is the representation of signum we match:
  2045. //
  2046. // signum(x) == (x >> 63) | (-x >>u 63)
  2047. //
  2048. // An i1 value is its own signum, so it's correct to match
  2049. //
  2050. // signum(x) == (x >> 0) | (-x >>u 0)
  2051. //
  2052. // for i1 values.
  2053. auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
  2054. auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
  2055. auto Signum = m_Or(LHS, RHS);
  2056. return Signum.match(V) && OpL == OpR && Val.match(OpL);
  2057. }
  2058. };
  2059. /// Matches a signum pattern.
  2060. ///
  2061. /// signum(x) =
  2062. /// x > 0 -> 1
  2063. /// x == 0 -> 0
  2064. /// x < 0 -> -1
  2065. template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
  2066. return Signum_match<Val_t>(V);
  2067. }
  2068. template <int Ind, typename Opnd_t> struct ExtractValue_match {
  2069. Opnd_t Val;
  2070. ExtractValue_match(const Opnd_t &V) : Val(V) {}
  2071. template <typename OpTy> bool match(OpTy *V) {
  2072. if (auto *I = dyn_cast<ExtractValueInst>(V)) {
  2073. // If Ind is -1, don't inspect indices
  2074. if (Ind != -1 &&
  2075. !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
  2076. return false;
  2077. return Val.match(I->getAggregateOperand());
  2078. }
  2079. return false;
  2080. }
  2081. };
  2082. /// Match a single index ExtractValue instruction.
  2083. /// For example m_ExtractValue<1>(...)
  2084. template <int Ind, typename Val_t>
  2085. inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) {
  2086. return ExtractValue_match<Ind, Val_t>(V);
  2087. }
  2088. /// Match an ExtractValue instruction with any index.
  2089. /// For example m_ExtractValue(...)
  2090. template <typename Val_t>
  2091. inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
  2092. return ExtractValue_match<-1, Val_t>(V);
  2093. }
  2094. /// Matcher for a single index InsertValue instruction.
  2095. template <int Ind, typename T0, typename T1> struct InsertValue_match {
  2096. T0 Op0;
  2097. T1 Op1;
  2098. InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
  2099. template <typename OpTy> bool match(OpTy *V) {
  2100. if (auto *I = dyn_cast<InsertValueInst>(V)) {
  2101. return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
  2102. I->getNumIndices() == 1 && Ind == I->getIndices()[0];
  2103. }
  2104. return false;
  2105. }
  2106. };
  2107. /// Matches a single index InsertValue instruction.
  2108. template <int Ind, typename Val_t, typename Elt_t>
  2109. inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
  2110. const Elt_t &Elt) {
  2111. return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
  2112. }
  2113. /// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
  2114. /// the constant expression
  2115. /// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
  2116. /// under the right conditions determined by DataLayout.
  2117. struct VScaleVal_match {
  2118. const DataLayout &DL;
  2119. VScaleVal_match(const DataLayout &DL) : DL(DL) {}
  2120. template <typename ITy> bool match(ITy *V) {
  2121. if (m_Intrinsic<Intrinsic::vscale>().match(V))
  2122. return true;
  2123. Value *Ptr;
  2124. if (m_PtrToInt(m_Value(Ptr)).match(V)) {
  2125. if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
  2126. auto *DerefTy = GEP->getSourceElementType();
  2127. if (GEP->getNumIndices() == 1 && isa<ScalableVectorType>(DerefTy) &&
  2128. m_Zero().match(GEP->getPointerOperand()) &&
  2129. m_SpecificInt(1).match(GEP->idx_begin()->get()) &&
  2130. DL.getTypeAllocSizeInBits(DerefTy).getKnownMinValue() == 8)
  2131. return true;
  2132. }
  2133. }
  2134. return false;
  2135. }
  2136. };
  2137. inline VScaleVal_match m_VScale(const DataLayout &DL) {
  2138. return VScaleVal_match(DL);
  2139. }
  2140. template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false>
  2141. struct LogicalOp_match {
  2142. LHS L;
  2143. RHS R;
  2144. LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
  2145. template <typename T> bool match(T *V) {
  2146. auto *I = dyn_cast<Instruction>(V);
  2147. if (!I || !I->getType()->isIntOrIntVectorTy(1))
  2148. return false;
  2149. if (I->getOpcode() == Opcode) {
  2150. auto *Op0 = I->getOperand(0);
  2151. auto *Op1 = I->getOperand(1);
  2152. return (L.match(Op0) && R.match(Op1)) ||
  2153. (Commutable && L.match(Op1) && R.match(Op0));
  2154. }
  2155. if (auto *Select = dyn_cast<SelectInst>(I)) {
  2156. auto *Cond = Select->getCondition();
  2157. auto *TVal = Select->getTrueValue();
  2158. auto *FVal = Select->getFalseValue();
  2159. // Don't match a scalar select of bool vectors.
  2160. // Transforms expect a single type for operands if this matches.
  2161. if (Cond->getType() != Select->getType())
  2162. return false;
  2163. if (Opcode == Instruction::And) {
  2164. auto *C = dyn_cast<Constant>(FVal);
  2165. if (C && C->isNullValue())
  2166. return (L.match(Cond) && R.match(TVal)) ||
  2167. (Commutable && L.match(TVal) && R.match(Cond));
  2168. } else {
  2169. assert(Opcode == Instruction::Or);
  2170. auto *C = dyn_cast<Constant>(TVal);
  2171. if (C && C->isOneValue())
  2172. return (L.match(Cond) && R.match(FVal)) ||
  2173. (Commutable && L.match(FVal) && R.match(Cond));
  2174. }
  2175. }
  2176. return false;
  2177. }
  2178. };
  2179. /// Matches L && R either in the form of L & R or L ? R : false.
  2180. /// Note that the latter form is poison-blocking.
  2181. template <typename LHS, typename RHS>
  2182. inline LogicalOp_match<LHS, RHS, Instruction::And> m_LogicalAnd(const LHS &L,
  2183. const RHS &R) {
  2184. return LogicalOp_match<LHS, RHS, Instruction::And>(L, R);
  2185. }
  2186. /// Matches L && R where L and R are arbitrary values.
  2187. inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
  2188. /// Matches L && R with LHS and RHS in either order.
  2189. template <typename LHS, typename RHS>
  2190. inline LogicalOp_match<LHS, RHS, Instruction::And, true>
  2191. m_c_LogicalAnd(const LHS &L, const RHS &R) {
  2192. return LogicalOp_match<LHS, RHS, Instruction::And, true>(L, R);
  2193. }
  2194. /// Matches L || R either in the form of L | R or L ? true : R.
  2195. /// Note that the latter form is poison-blocking.
  2196. template <typename LHS, typename RHS>
  2197. inline LogicalOp_match<LHS, RHS, Instruction::Or> m_LogicalOr(const LHS &L,
  2198. const RHS &R) {
  2199. return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R);
  2200. }
  2201. /// Matches L || R where L and R are arbitrary values.
  2202. inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); }
  2203. /// Matches L || R with LHS and RHS in either order.
  2204. template <typename LHS, typename RHS>
  2205. inline LogicalOp_match<LHS, RHS, Instruction::Or, true>
  2206. m_c_LogicalOr(const LHS &L, const RHS &R) {
  2207. return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R);
  2208. }
  2209. /// Matches either L && R or L || R,
  2210. /// either one being in the either binary or logical form.
  2211. /// Note that the latter form is poison-blocking.
  2212. template <typename LHS, typename RHS, bool Commutable = false>
  2213. inline auto m_LogicalOp(const LHS &L, const RHS &R) {
  2214. return m_CombineOr(
  2215. LogicalOp_match<LHS, RHS, Instruction::And, Commutable>(L, R),
  2216. LogicalOp_match<LHS, RHS, Instruction::Or, Commutable>(L, R));
  2217. }
  2218. /// Matches either L && R or L || R where L and R are arbitrary values.
  2219. inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); }
  2220. /// Matches either L && R or L || R with LHS and RHS in either order.
  2221. template <typename LHS, typename RHS>
  2222. inline auto m_c_LogicalOp(const LHS &L, const RHS &R) {
  2223. return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R);
  2224. }
  2225. } // end namespace PatternMatch
  2226. } // end namespace llvm
  2227. #endif // LLVM_IR_PATTERNMATCH_H
  2228. #ifdef __GNUC__
  2229. #pragma GCC diagnostic pop
  2230. #endif