PatternMatch.h 82 KB

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