PatternMatch.h 87 KB

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