PatternMatch.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file provides a simple and efficient mechanism for performing general
  11. // tree-based pattern matches on the LLVM IR. The power of these routines is
  12. // that it allows you to write concise patterns that are expressive and easy to
  13. // understand. The other major advantage of this is that it allows you to
  14. // trivially capture/bind elements in the pattern to variables. For example,
  15. // you can do something like this:
  16. //
  17. // Value *Exp = ...
  18. // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
  19. // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
  20. // m_And(m_Value(Y), m_ConstantInt(C2))))) {
  21. // ... Pattern is matched and variables are bound ...
  22. // }
  23. //
  24. // This is primarily useful to things like the instruction combiner, but can
  25. // also be useful for static analysis tools or code generators.
  26. //
  27. //===----------------------------------------------------------------------===//
  28. #ifndef LLVM_IR_PATTERNMATCH_H
  29. #define LLVM_IR_PATTERNMATCH_H
  30. #include "llvm/IR/CallSite.h"
  31. #include "llvm/IR/Constants.h"
  32. #include "llvm/IR/Instructions.h"
  33. #include "llvm/IR/Intrinsics.h"
  34. #include "llvm/IR/Operator.h"
  35. namespace llvm {
  36. namespace PatternMatch {
  37. template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
  38. return const_cast<Pattern &>(P).match(V);
  39. }
  40. template <typename SubPattern_t> struct OneUse_match {
  41. SubPattern_t SubPattern;
  42. OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
  43. template <typename OpTy> bool match(OpTy *V) {
  44. return V->hasOneUse() && SubPattern.match(V);
  45. }
  46. };
  47. template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
  48. return SubPattern;
  49. }
  50. template <typename Class> struct class_match {
  51. template <typename ITy> bool match(ITy *V) { return isa<Class>(V); }
  52. };
  53. /// \brief Match an arbitrary value and ignore it.
  54. inline class_match<Value> m_Value() { return class_match<Value>(); }
  55. /// \brief Match an arbitrary binary operation and ignore it.
  56. inline class_match<BinaryOperator> m_BinOp() {
  57. return class_match<BinaryOperator>();
  58. }
  59. /// \brief Matches any compare instruction and ignore it.
  60. inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
  61. /// \brief Match an arbitrary ConstantInt and ignore it.
  62. inline class_match<ConstantInt> m_ConstantInt() {
  63. return class_match<ConstantInt>();
  64. }
  65. /// \brief Match an arbitrary undef constant.
  66. inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
  67. /// \brief Match an arbitrary Constant and ignore it.
  68. inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
  69. /// Matching combinators
  70. template <typename LTy, typename RTy> struct match_combine_or {
  71. LTy L;
  72. RTy R;
  73. match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
  74. template <typename ITy> bool match(ITy *V) {
  75. if (L.match(V))
  76. return true;
  77. if (R.match(V))
  78. return true;
  79. return false;
  80. }
  81. };
  82. template <typename LTy, typename RTy> struct match_combine_and {
  83. LTy L;
  84. RTy R;
  85. match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
  86. template <typename ITy> bool match(ITy *V) {
  87. if (L.match(V))
  88. if (R.match(V))
  89. return true;
  90. return false;
  91. }
  92. };
  93. /// Combine two pattern matchers matching L || R
  94. template <typename LTy, typename RTy>
  95. inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
  96. return match_combine_or<LTy, RTy>(L, R);
  97. }
  98. /// Combine two pattern matchers matching L && R
  99. template <typename LTy, typename RTy>
  100. inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
  101. return match_combine_and<LTy, RTy>(L, R);
  102. }
  103. struct match_zero {
  104. template <typename ITy> bool match(ITy *V) {
  105. if (const auto *C = dyn_cast<Constant>(V))
  106. return C->isNullValue();
  107. return false;
  108. }
  109. };
  110. /// \brief Match an arbitrary zero/null constant. This includes
  111. /// zero_initializer for vectors and ConstantPointerNull for pointers.
  112. inline match_zero m_Zero() { return match_zero(); }
  113. struct match_neg_zero {
  114. template <typename ITy> bool match(ITy *V) {
  115. if (const auto *C = dyn_cast<Constant>(V))
  116. return C->isNegativeZeroValue();
  117. return false;
  118. }
  119. };
  120. /// \brief Match an arbitrary zero/null constant. This includes
  121. /// zero_initializer for vectors and ConstantPointerNull for pointers. For
  122. /// floating point constants, this will match negative zero but not positive
  123. /// zero
  124. inline match_neg_zero m_NegZero() { return match_neg_zero(); }
  125. /// \brief - Match an arbitrary zero/null constant. This includes
  126. /// zero_initializer for vectors and ConstantPointerNull for pointers. For
  127. /// floating point constants, this will match negative zero and positive zero
  128. inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
  129. return m_CombineOr(m_Zero(), m_NegZero());
  130. }
  131. struct apint_match {
  132. const APInt *&Res;
  133. apint_match(const APInt *&R) : Res(R) {}
  134. template <typename ITy> bool match(ITy *V) {
  135. if (auto *CI = dyn_cast<ConstantInt>(V)) {
  136. Res = &CI->getValue();
  137. return true;
  138. }
  139. if (V->getType()->isVectorTy())
  140. if (const auto *C = dyn_cast<Constant>(V))
  141. if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
  142. Res = &CI->getValue();
  143. return true;
  144. }
  145. return false;
  146. }
  147. };
  148. /// \brief Match a ConstantInt or splatted ConstantVector, binding the
  149. /// specified pointer to the contained APInt.
  150. inline apint_match m_APInt(const APInt *&Res) { return Res; }
  151. template <int64_t Val> struct constantint_match {
  152. template <typename ITy> bool match(ITy *V) {
  153. if (const auto *CI = dyn_cast<ConstantInt>(V)) {
  154. const APInt &CIV = CI->getValue();
  155. if (Val >= 0)
  156. return CIV == static_cast<uint64_t>(Val);
  157. // If Val is negative, and CI is shorter than it, truncate to the right
  158. // number of bits. If it is larger, then we have to sign extend. Just
  159. // compare their negated values.
  160. return -CIV == -Val;
  161. }
  162. return false;
  163. }
  164. };
  165. /// \brief Match a ConstantInt with a specific value.
  166. template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
  167. return constantint_match<Val>();
  168. }
  169. /// \brief This helper class is used to match scalar and vector constants that
  170. /// satisfy a specified predicate.
  171. template <typename Predicate> struct cst_pred_ty : public Predicate {
  172. template <typename ITy> bool match(ITy *V) {
  173. if (const auto *CI = dyn_cast<ConstantInt>(V))
  174. return this->isValue(CI->getValue());
  175. if (V->getType()->isVectorTy())
  176. if (const auto *C = dyn_cast<Constant>(V))
  177. if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
  178. return this->isValue(CI->getValue());
  179. return false;
  180. }
  181. };
  182. /// \brief This helper class is used to match scalar and vector constants that
  183. /// satisfy a specified predicate, and bind them to an APInt.
  184. template <typename Predicate> struct api_pred_ty : public Predicate {
  185. const APInt *&Res;
  186. api_pred_ty(const APInt *&R) : Res(R) {}
  187. template <typename ITy> bool match(ITy *V) {
  188. if (const auto *CI = dyn_cast<ConstantInt>(V))
  189. if (this->isValue(CI->getValue())) {
  190. Res = &CI->getValue();
  191. return true;
  192. }
  193. if (V->getType()->isVectorTy())
  194. if (const auto *C = dyn_cast<Constant>(V))
  195. if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
  196. if (this->isValue(CI->getValue())) {
  197. Res = &CI->getValue();
  198. return true;
  199. }
  200. return false;
  201. }
  202. };
  203. struct is_one {
  204. bool isValue(const APInt &C) { return C == 1; }
  205. };
  206. /// \brief Match an integer 1 or a vector with all elements equal to 1.
  207. inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
  208. inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
  209. struct is_all_ones {
  210. bool isValue(const APInt &C) { return C.isAllOnesValue(); }
  211. };
  212. /// \brief Match an integer or vector with all bits set to true.
  213. inline cst_pred_ty<is_all_ones> m_AllOnes() {
  214. return cst_pred_ty<is_all_ones>();
  215. }
  216. inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
  217. struct is_sign_bit {
  218. bool isValue(const APInt &C) { return C.isSignBit(); }
  219. };
  220. /// \brief Match an integer or vector with only the sign bit(s) set.
  221. inline cst_pred_ty<is_sign_bit> m_SignBit() {
  222. return cst_pred_ty<is_sign_bit>();
  223. }
  224. inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
  225. struct is_power2 {
  226. bool isValue(const APInt &C) { return C.isPowerOf2(); }
  227. };
  228. /// \brief Match an integer or vector power of 2.
  229. inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
  230. inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
  231. struct is_maxsignedvalue {
  232. bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
  233. };
  234. inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { return cst_pred_ty<is_maxsignedvalue>(); }
  235. inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { return V; }
  236. template <typename Class> struct bind_ty {
  237. Class *&VR;
  238. bind_ty(Class *&V) : VR(V) {}
  239. template <typename ITy> bool match(ITy *V) {
  240. if (auto *CV = dyn_cast<Class>(V)) {
  241. VR = CV;
  242. return true;
  243. }
  244. return false;
  245. }
  246. };
  247. /// \brief Match a value, capturing it if we match.
  248. inline bind_ty<Value> m_Value(Value *&V) { return V; }
  249. /// \brief Match an instruction, capturing it if we match.
  250. inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
  251. /// \brief Match a binary operator, capturing it if we match.
  252. inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
  253. /// \brief Match a ConstantInt, capturing the value if we match.
  254. inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
  255. /// \brief Match a Constant, capturing the value if we match.
  256. inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
  257. /// \brief Match a ConstantFP, capturing the value if we match.
  258. inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
  259. /// \brief Match a specified Value*.
  260. struct specificval_ty {
  261. const Value *Val;
  262. specificval_ty(const Value *V) : Val(V) {}
  263. template <typename ITy> bool match(ITy *V) { return V == Val; }
  264. };
  265. /// \brief Match if we have a specific specified value.
  266. inline specificval_ty m_Specific(const Value *V) { return V; }
  267. /// \brief Match a specified floating point value or vector of all elements of
  268. /// that value.
  269. struct specific_fpval {
  270. double Val;
  271. specific_fpval(double V) : Val(V) {}
  272. template <typename ITy> bool match(ITy *V) {
  273. if (const auto *CFP = dyn_cast<ConstantFP>(V))
  274. return CFP->isExactlyValue(Val);
  275. if (V->getType()->isVectorTy())
  276. if (const auto *C = dyn_cast<Constant>(V))
  277. if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
  278. return CFP->isExactlyValue(Val);
  279. return false;
  280. }
  281. };
  282. /// \brief Match a specific floating point value or vector with all elements
  283. /// equal to the value.
  284. inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
  285. /// \brief Match a float 1.0 or vector with all elements equal to 1.0.
  286. inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
  287. struct bind_const_intval_ty {
  288. uint64_t &VR;
  289. bind_const_intval_ty(uint64_t &V) : VR(V) {}
  290. template <typename ITy> bool match(ITy *V) {
  291. if (const auto *CV = dyn_cast<ConstantInt>(V))
  292. if (CV->getBitWidth() <= 64) {
  293. VR = CV->getZExtValue();
  294. return true;
  295. }
  296. return false;
  297. }
  298. };
  299. /// \brief Match a specified integer value or vector of all elements of that
  300. // value.
  301. struct specific_intval {
  302. uint64_t Val;
  303. specific_intval(uint64_t V) : Val(V) {}
  304. template <typename ITy> bool match(ITy *V) {
  305. const auto *CI = dyn_cast<ConstantInt>(V);
  306. if (!CI && V->getType()->isVectorTy())
  307. if (const auto *C = dyn_cast<Constant>(V))
  308. CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue());
  309. if (CI && CI->getBitWidth() <= 64)
  310. return CI->getZExtValue() == Val;
  311. return false;
  312. }
  313. };
  314. /// \brief Match a specific integer value or vector with all elements equal to
  315. /// the value.
  316. inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); }
  317. /// \brief Match a ConstantInt and bind to its value. This does not match
  318. /// ConstantInts wider than 64-bits.
  319. inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
  320. //===----------------------------------------------------------------------===//
  321. // Matcher for any binary operator.
  322. //
  323. template <typename LHS_t, typename RHS_t> struct AnyBinaryOp_match {
  324. LHS_t L;
  325. RHS_t R;
  326. AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  327. template <typename OpTy> bool match(OpTy *V) {
  328. if (auto *I = dyn_cast<BinaryOperator>(V))
  329. return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
  330. return false;
  331. }
  332. };
  333. template <typename LHS, typename RHS>
  334. inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
  335. return AnyBinaryOp_match<LHS, RHS>(L, R);
  336. }
  337. //===----------------------------------------------------------------------===//
  338. // Matchers for specific binary operators.
  339. //
  340. template <typename LHS_t, typename RHS_t, unsigned Opcode>
  341. struct BinaryOp_match {
  342. LHS_t L;
  343. RHS_t R;
  344. BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  345. template <typename OpTy> bool match(OpTy *V) {
  346. if (V->getValueID() == Value::InstructionVal + Opcode) {
  347. auto *I = cast<BinaryOperator>(V);
  348. return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
  349. }
  350. if (auto *CE = dyn_cast<ConstantExpr>(V))
  351. return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
  352. R.match(CE->getOperand(1));
  353. return false;
  354. }
  355. };
  356. template <typename LHS, typename RHS>
  357. inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
  358. const RHS &R) {
  359. return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
  360. }
  361. template <typename LHS, typename RHS>
  362. inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
  363. const RHS &R) {
  364. return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
  365. }
  366. template <typename LHS, typename RHS>
  367. inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
  368. const RHS &R) {
  369. return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
  370. }
  371. template <typename LHS, typename RHS>
  372. inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
  373. const RHS &R) {
  374. return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
  375. }
  376. template <typename LHS, typename RHS>
  377. inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
  378. const RHS &R) {
  379. return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
  380. }
  381. template <typename LHS, typename RHS>
  382. inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
  383. const RHS &R) {
  384. return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
  385. }
  386. template <typename LHS, typename RHS>
  387. inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
  388. const RHS &R) {
  389. return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
  390. }
  391. template <typename LHS, typename RHS>
  392. inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
  393. const RHS &R) {
  394. return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
  395. }
  396. template <typename LHS, typename RHS>
  397. inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
  398. const RHS &R) {
  399. return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
  400. }
  401. template <typename LHS, typename RHS>
  402. inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
  403. const RHS &R) {
  404. return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
  405. }
  406. template <typename LHS, typename RHS>
  407. inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
  408. const RHS &R) {
  409. return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
  410. }
  411. template <typename LHS, typename RHS>
  412. inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
  413. const RHS &R) {
  414. return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
  415. }
  416. template <typename LHS, typename RHS>
  417. inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
  418. const RHS &R) {
  419. return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
  420. }
  421. template <typename LHS, typename RHS>
  422. inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
  423. const RHS &R) {
  424. return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
  425. }
  426. template <typename LHS, typename RHS>
  427. inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
  428. const RHS &R) {
  429. return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
  430. }
  431. template <typename LHS, typename RHS>
  432. inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
  433. const RHS &R) {
  434. return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
  435. }
  436. template <typename LHS, typename RHS>
  437. inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
  438. const RHS &R) {
  439. return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
  440. }
  441. template <typename LHS, typename RHS>
  442. inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
  443. const RHS &R) {
  444. return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
  445. }
  446. template <typename LHS_t, typename RHS_t, unsigned Opcode,
  447. unsigned WrapFlags = 0>
  448. struct OverflowingBinaryOp_match {
  449. LHS_t L;
  450. RHS_t R;
  451. OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
  452. : L(LHS), R(RHS) {}
  453. template <typename OpTy> bool match(OpTy *V) {
  454. if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
  455. if (Op->getOpcode() != Opcode)
  456. return false;
  457. if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
  458. !Op->hasNoUnsignedWrap())
  459. return false;
  460. if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
  461. !Op->hasNoSignedWrap())
  462. return false;
  463. return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
  464. }
  465. return false;
  466. }
  467. };
  468. template <typename LHS, typename RHS>
  469. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  470. OverflowingBinaryOperator::NoSignedWrap>
  471. m_NSWAdd(const LHS &L, const RHS &R) {
  472. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  473. OverflowingBinaryOperator::NoSignedWrap>(
  474. L, R);
  475. }
  476. template <typename LHS, typename RHS>
  477. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  478. OverflowingBinaryOperator::NoSignedWrap>
  479. m_NSWSub(const LHS &L, const RHS &R) {
  480. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  481. OverflowingBinaryOperator::NoSignedWrap>(
  482. L, R);
  483. }
  484. template <typename LHS, typename RHS>
  485. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  486. OverflowingBinaryOperator::NoSignedWrap>
  487. m_NSWMul(const LHS &L, const RHS &R) {
  488. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  489. OverflowingBinaryOperator::NoSignedWrap>(
  490. L, R);
  491. }
  492. template <typename LHS, typename RHS>
  493. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  494. OverflowingBinaryOperator::NoSignedWrap>
  495. m_NSWShl(const LHS &L, const RHS &R) {
  496. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  497. OverflowingBinaryOperator::NoSignedWrap>(
  498. L, R);
  499. }
  500. template <typename LHS, typename RHS>
  501. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  502. OverflowingBinaryOperator::NoUnsignedWrap>
  503. m_NUWAdd(const LHS &L, const RHS &R) {
  504. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
  505. OverflowingBinaryOperator::NoUnsignedWrap>(
  506. L, R);
  507. }
  508. template <typename LHS, typename RHS>
  509. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  510. OverflowingBinaryOperator::NoUnsignedWrap>
  511. m_NUWSub(const LHS &L, const RHS &R) {
  512. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
  513. OverflowingBinaryOperator::NoUnsignedWrap>(
  514. L, R);
  515. }
  516. template <typename LHS, typename RHS>
  517. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  518. OverflowingBinaryOperator::NoUnsignedWrap>
  519. m_NUWMul(const LHS &L, const RHS &R) {
  520. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
  521. OverflowingBinaryOperator::NoUnsignedWrap>(
  522. L, R);
  523. }
  524. template <typename LHS, typename RHS>
  525. inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  526. OverflowingBinaryOperator::NoUnsignedWrap>
  527. m_NUWShl(const LHS &L, const RHS &R) {
  528. return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
  529. OverflowingBinaryOperator::NoUnsignedWrap>(
  530. L, R);
  531. }
  532. //===----------------------------------------------------------------------===//
  533. // Class that matches two different binary ops.
  534. //
  535. template <typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
  536. struct BinOp2_match {
  537. LHS_t L;
  538. RHS_t R;
  539. BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  540. template <typename OpTy> bool match(OpTy *V) {
  541. if (V->getValueID() == Value::InstructionVal + Opc1 ||
  542. V->getValueID() == Value::InstructionVal + Opc2) {
  543. auto *I = cast<BinaryOperator>(V);
  544. return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
  545. }
  546. if (auto *CE = dyn_cast<ConstantExpr>(V))
  547. return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
  548. L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
  549. return false;
  550. }
  551. };
  552. /// \brief Matches LShr or AShr.
  553. template <typename LHS, typename RHS>
  554. inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
  555. m_Shr(const LHS &L, const RHS &R) {
  556. return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
  557. }
  558. /// \brief Matches LShr or Shl.
  559. template <typename LHS, typename RHS>
  560. inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
  561. m_LogicalShift(const LHS &L, const RHS &R) {
  562. return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
  563. }
  564. /// \brief Matches UDiv and SDiv.
  565. template <typename LHS, typename RHS>
  566. inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
  567. m_IDiv(const LHS &L, const RHS &R) {
  568. return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
  569. }
  570. //===----------------------------------------------------------------------===//
  571. // Class that matches exact binary ops.
  572. //
  573. template <typename SubPattern_t> struct Exact_match {
  574. SubPattern_t SubPattern;
  575. Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
  576. template <typename OpTy> bool match(OpTy *V) {
  577. if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
  578. return PEO->isExact() && SubPattern.match(V);
  579. return false;
  580. }
  581. };
  582. template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
  583. return SubPattern;
  584. }
  585. //===----------------------------------------------------------------------===//
  586. // Matchers for CmpInst classes
  587. //
  588. template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
  589. struct CmpClass_match {
  590. PredicateTy &Predicate;
  591. LHS_t L;
  592. RHS_t R;
  593. CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
  594. : Predicate(Pred), L(LHS), R(RHS) {}
  595. template <typename OpTy> bool match(OpTy *V) {
  596. if (Class *I = dyn_cast<Class>(V))
  597. if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
  598. Predicate = I->getPredicate();
  599. return true;
  600. }
  601. return false;
  602. }
  603. };
  604. template <typename LHS, typename RHS>
  605. inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
  606. m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  607. return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
  608. }
  609. template <typename LHS, typename RHS>
  610. inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
  611. m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  612. return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
  613. }
  614. template <typename LHS, typename RHS>
  615. inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
  616. m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  617. return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
  618. }
  619. //===----------------------------------------------------------------------===//
  620. // Matchers for SelectInst classes
  621. //
  622. template <typename Cond_t, typename LHS_t, typename RHS_t>
  623. struct SelectClass_match {
  624. Cond_t C;
  625. LHS_t L;
  626. RHS_t R;
  627. SelectClass_match(const Cond_t &Cond, const LHS_t &LHS, const RHS_t &RHS)
  628. : C(Cond), L(LHS), R(RHS) {}
  629. template <typename OpTy> bool match(OpTy *V) {
  630. if (auto *I = dyn_cast<SelectInst>(V))
  631. return C.match(I->getOperand(0)) && L.match(I->getOperand(1)) &&
  632. R.match(I->getOperand(2));
  633. return false;
  634. }
  635. };
  636. template <typename Cond, typename LHS, typename RHS>
  637. inline SelectClass_match<Cond, LHS, RHS> m_Select(const Cond &C, const LHS &L,
  638. const RHS &R) {
  639. return SelectClass_match<Cond, LHS, RHS>(C, L, R);
  640. }
  641. /// \brief This matches a select of two constants, e.g.:
  642. /// m_SelectCst<-1, 0>(m_Value(V))
  643. template <int64_t L, int64_t R, typename Cond>
  644. inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R>>
  645. m_SelectCst(const Cond &C) {
  646. return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
  647. }
  648. //===----------------------------------------------------------------------===//
  649. // Matchers for CastInst classes
  650. //
  651. template <typename Op_t, unsigned Opcode> struct CastClass_match {
  652. Op_t Op;
  653. CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
  654. template <typename OpTy> bool match(OpTy *V) {
  655. if (auto *O = dyn_cast<Operator>(V))
  656. return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
  657. return false;
  658. }
  659. };
  660. /// \brief Matches BitCast.
  661. template <typename OpTy>
  662. inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
  663. return CastClass_match<OpTy, Instruction::BitCast>(Op);
  664. }
  665. /// \brief Matches PtrToInt.
  666. template <typename OpTy>
  667. inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
  668. return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
  669. }
  670. /// \brief Matches Trunc.
  671. template <typename OpTy>
  672. inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
  673. return CastClass_match<OpTy, Instruction::Trunc>(Op);
  674. }
  675. /// \brief Matches SExt.
  676. template <typename OpTy>
  677. inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
  678. return CastClass_match<OpTy, Instruction::SExt>(Op);
  679. }
  680. /// \brief Matches ZExt.
  681. template <typename OpTy>
  682. inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
  683. return CastClass_match<OpTy, Instruction::ZExt>(Op);
  684. }
  685. /// \brief Matches UIToFP.
  686. template <typename OpTy>
  687. inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
  688. return CastClass_match<OpTy, Instruction::UIToFP>(Op);
  689. }
  690. /// \brief Matches SIToFP.
  691. template <typename OpTy>
  692. inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
  693. return CastClass_match<OpTy, Instruction::SIToFP>(Op);
  694. }
  695. //===----------------------------------------------------------------------===//
  696. // Matchers for unary operators
  697. //
  698. template <typename LHS_t> struct not_match {
  699. LHS_t L;
  700. not_match(const LHS_t &LHS) : L(LHS) {}
  701. template <typename OpTy> bool match(OpTy *V) {
  702. if (auto *O = dyn_cast<Operator>(V))
  703. if (O->getOpcode() == Instruction::Xor)
  704. return matchIfNot(O->getOperand(0), O->getOperand(1));
  705. return false;
  706. }
  707. private:
  708. bool matchIfNot(Value *LHS, Value *RHS) {
  709. return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
  710. // FIXME: Remove CV.
  711. isa<ConstantVector>(RHS)) &&
  712. cast<Constant>(RHS)->isAllOnesValue() && L.match(LHS);
  713. }
  714. };
  715. template <typename LHS> inline not_match<LHS> m_Not(const LHS &L) { return L; }
  716. template <typename LHS_t> struct neg_match {
  717. LHS_t L;
  718. neg_match(const LHS_t &LHS) : L(LHS) {}
  719. template <typename OpTy> bool match(OpTy *V) {
  720. if (auto *O = dyn_cast<Operator>(V))
  721. if (O->getOpcode() == Instruction::Sub)
  722. return matchIfNeg(O->getOperand(0), O->getOperand(1));
  723. return false;
  724. }
  725. private:
  726. bool matchIfNeg(Value *LHS, Value *RHS) {
  727. return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
  728. isa<ConstantAggregateZero>(LHS)) &&
  729. L.match(RHS);
  730. }
  731. };
  732. /// \brief Match an integer negate.
  733. template <typename LHS> inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
  734. template <typename LHS_t> struct fneg_match {
  735. LHS_t L;
  736. fneg_match(const LHS_t &LHS) : L(LHS) {}
  737. template <typename OpTy> bool match(OpTy *V) {
  738. if (auto *O = dyn_cast<Operator>(V))
  739. if (O->getOpcode() == Instruction::FSub)
  740. return matchIfFNeg(O->getOperand(0), O->getOperand(1));
  741. return false;
  742. }
  743. private:
  744. bool matchIfFNeg(Value *LHS, Value *RHS) {
  745. if (const auto *C = dyn_cast<ConstantFP>(LHS))
  746. return C->isNegativeZeroValue() && L.match(RHS);
  747. return false;
  748. }
  749. };
  750. /// \brief Match a floating point negate.
  751. template <typename LHS> inline fneg_match<LHS> m_FNeg(const LHS &L) {
  752. return L;
  753. }
  754. //===----------------------------------------------------------------------===//
  755. // Matchers for control flow.
  756. //
  757. struct br_match {
  758. BasicBlock *&Succ;
  759. br_match(BasicBlock *&Succ) : Succ(Succ) {}
  760. template <typename OpTy> bool match(OpTy *V) {
  761. if (auto *BI = dyn_cast<BranchInst>(V))
  762. if (BI->isUnconditional()) {
  763. Succ = BI->getSuccessor(0);
  764. return true;
  765. }
  766. return false;
  767. }
  768. };
  769. inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
  770. template <typename Cond_t> struct brc_match {
  771. Cond_t Cond;
  772. BasicBlock *&T, *&F;
  773. brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
  774. : Cond(C), T(t), F(f) {}
  775. template <typename OpTy> bool match(OpTy *V) {
  776. if (auto *BI = dyn_cast<BranchInst>(V))
  777. if (BI->isConditional() && Cond.match(BI->getCondition())) {
  778. T = BI->getSuccessor(0);
  779. F = BI->getSuccessor(1);
  780. return true;
  781. }
  782. return false;
  783. }
  784. };
  785. template <typename Cond_t>
  786. inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
  787. return brc_match<Cond_t>(C, T, F);
  788. }
  789. //===----------------------------------------------------------------------===//
  790. // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
  791. //
  792. template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t>
  793. struct MaxMin_match {
  794. LHS_t L;
  795. RHS_t R;
  796. MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  797. template <typename OpTy> bool match(OpTy *V) {
  798. // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
  799. auto *SI = dyn_cast<SelectInst>(V);
  800. if (!SI)
  801. return false;
  802. auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
  803. if (!Cmp)
  804. return false;
  805. // At this point we have a select conditioned on a comparison. Check that
  806. // it is the values returned by the select that are being compared.
  807. Value *TrueVal = SI->getTrueValue();
  808. Value *FalseVal = SI->getFalseValue();
  809. Value *LHS = Cmp->getOperand(0);
  810. Value *RHS = Cmp->getOperand(1);
  811. if ((TrueVal != LHS || FalseVal != RHS) &&
  812. (TrueVal != RHS || FalseVal != LHS))
  813. return false;
  814. typename CmpInst_t::Predicate Pred =
  815. LHS == TrueVal ? Cmp->getPredicate() : Cmp->getSwappedPredicate();
  816. // Does "(x pred y) ? x : y" represent the desired max/min operation?
  817. if (!Pred_t::match(Pred))
  818. return false;
  819. // It does! Bind the operands.
  820. return L.match(LHS) && R.match(RHS);
  821. }
  822. };
  823. /// \brief Helper class for identifying signed max predicates.
  824. struct smax_pred_ty {
  825. static bool match(ICmpInst::Predicate Pred) {
  826. return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
  827. }
  828. };
  829. /// \brief Helper class for identifying signed min predicates.
  830. struct smin_pred_ty {
  831. static bool match(ICmpInst::Predicate Pred) {
  832. return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
  833. }
  834. };
  835. /// \brief Helper class for identifying unsigned max predicates.
  836. struct umax_pred_ty {
  837. static bool match(ICmpInst::Predicate Pred) {
  838. return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
  839. }
  840. };
  841. /// \brief Helper class for identifying unsigned min predicates.
  842. struct umin_pred_ty {
  843. static bool match(ICmpInst::Predicate Pred) {
  844. return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
  845. }
  846. };
  847. /// \brief Helper class for identifying ordered max predicates.
  848. struct ofmax_pred_ty {
  849. static bool match(FCmpInst::Predicate Pred) {
  850. return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
  851. }
  852. };
  853. /// \brief Helper class for identifying ordered min predicates.
  854. struct ofmin_pred_ty {
  855. static bool match(FCmpInst::Predicate Pred) {
  856. return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
  857. }
  858. };
  859. /// \brief Helper class for identifying unordered max predicates.
  860. struct ufmax_pred_ty {
  861. static bool match(FCmpInst::Predicate Pred) {
  862. return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
  863. }
  864. };
  865. /// \brief Helper class for identifying unordered min predicates.
  866. struct ufmin_pred_ty {
  867. static bool match(FCmpInst::Predicate Pred) {
  868. return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
  869. }
  870. };
  871. template <typename LHS, typename RHS>
  872. inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
  873. const RHS &R) {
  874. return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
  875. }
  876. template <typename LHS, typename RHS>
  877. inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
  878. const RHS &R) {
  879. return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
  880. }
  881. template <typename LHS, typename RHS>
  882. inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
  883. const RHS &R) {
  884. return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
  885. }
  886. template <typename LHS, typename RHS>
  887. inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
  888. const RHS &R) {
  889. return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
  890. }
  891. /// \brief Match an 'ordered' floating point maximum function.
  892. /// Floating point has one special value 'NaN'. Therefore, there is no total
  893. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  894. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
  895. /// semantics. In the presence of 'NaN' we have to preserve the original
  896. /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
  897. ///
  898. /// max(L, R) iff L and R are not NaN
  899. /// m_OrdFMax(L, R) = R iff L or R are NaN
  900. template <typename LHS, typename RHS>
  901. inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
  902. const RHS &R) {
  903. return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
  904. }
  905. /// \brief Match an 'ordered' floating point minimum function.
  906. /// Floating point has one special value 'NaN'. Therefore, there is no total
  907. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  908. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
  909. /// semantics. In the presence of 'NaN' we have to preserve the original
  910. /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
  911. ///
  912. /// max(L, R) iff L and R are not NaN
  913. /// m_OrdFMin(L, R) = R iff L or R are NaN
  914. template <typename LHS, typename RHS>
  915. inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
  916. const RHS &R) {
  917. return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
  918. }
  919. /// \brief Match an 'unordered' floating point maximum function.
  920. /// Floating point has one special value 'NaN'. Therefore, there is no total
  921. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  922. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
  923. /// semantics. In the presence of 'NaN' we have to preserve the original
  924. /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
  925. ///
  926. /// max(L, R) iff L and R are not NaN
  927. /// m_UnordFMin(L, R) = L iff L or R are NaN
  928. template <typename LHS, typename RHS>
  929. inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
  930. m_UnordFMax(const LHS &L, const RHS &R) {
  931. return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
  932. }
  933. // //
  934. ///////////////////////////////////////////////////////////////////////////////
  935. // Matchers for overflow check patterns: e.g. (a + b) u< a
  936. //
  937. template <typename LHS_t, typename RHS_t, typename Sum_t>
  938. struct UAddWithOverflow_match {
  939. LHS_t L;
  940. RHS_t R;
  941. Sum_t S;
  942. UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
  943. : L(L), R(R), S(S) {}
  944. template <typename OpTy> bool match(OpTy *V) {
  945. Value *ICmpLHS, *ICmpRHS;
  946. ICmpInst::Predicate Pred;
  947. if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
  948. return false;
  949. Value *AddLHS, *AddRHS;
  950. auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
  951. // (a + b) u< a, (a + b) u< b
  952. if (Pred == ICmpInst::ICMP_ULT)
  953. if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
  954. return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
  955. // a >u (a + b), b >u (a + b)
  956. if (Pred == ICmpInst::ICMP_UGT)
  957. if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
  958. return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
  959. return false;
  960. }
  961. };
  962. /// \brief Match an icmp instruction checking for unsigned overflow on addition.
  963. ///
  964. /// S is matched to the addition whose result is being checked for overflow, and
  965. /// L and R are matched to the LHS and RHS of S.
  966. template <typename LHS_t, typename RHS_t, typename Sum_t>
  967. UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
  968. m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
  969. return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
  970. }
  971. /// \brief Match an 'unordered' floating point minimum function.
  972. /// Floating point has one special value 'NaN'. Therefore, there is no total
  973. /// order. However, if we can ignore the 'NaN' value (for example, because of a
  974. /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
  975. /// semantics. In the presence of 'NaN' we have to preserve the original
  976. /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
  977. ///
  978. /// max(L, R) iff L and R are not NaN
  979. /// m_UnordFMin(L, R) = L iff L or R are NaN
  980. template <typename LHS, typename RHS>
  981. inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
  982. m_UnordFMin(const LHS &L, const RHS &R) {
  983. return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
  984. }
  985. template <typename Opnd_t> struct Argument_match {
  986. unsigned OpI;
  987. Opnd_t Val;
  988. Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
  989. template <typename OpTy> bool match(OpTy *V) {
  990. CallSite CS(V);
  991. return CS.isCall() && Val.match(CS.getArgument(OpI));
  992. }
  993. };
  994. /// \brief Match an argument.
  995. template <unsigned OpI, typename Opnd_t>
  996. inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
  997. return Argument_match<Opnd_t>(OpI, Op);
  998. }
  999. /// \brief Intrinsic matchers.
  1000. struct IntrinsicID_match {
  1001. unsigned ID;
  1002. IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
  1003. template <typename OpTy> bool match(OpTy *V) {
  1004. if (const auto *CI = dyn_cast<CallInst>(V))
  1005. if (const auto *F = CI->getCalledFunction())
  1006. return F->getIntrinsicID() == ID;
  1007. return false;
  1008. }
  1009. };
  1010. /// Intrinsic matches are combinations of ID matchers, and argument
  1011. /// matchers. Higher arity matcher are defined recursively in terms of and-ing
  1012. /// them with lower arity matchers. Here's some convenient typedefs for up to
  1013. /// several arguments, and more can be added as needed
  1014. template <typename T0 = void, typename T1 = void, typename T2 = void,
  1015. typename T3 = void, typename T4 = void, typename T5 = void,
  1016. typename T6 = void, typename T7 = void, typename T8 = void,
  1017. typename T9 = void, typename T10 = void>
  1018. struct m_Intrinsic_Ty;
  1019. template <typename T0> struct m_Intrinsic_Ty<T0> {
  1020. typedef match_combine_and<IntrinsicID_match, Argument_match<T0>> Ty;
  1021. };
  1022. template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
  1023. typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>
  1024. Ty;
  1025. };
  1026. template <typename T0, typename T1, typename T2>
  1027. struct m_Intrinsic_Ty<T0, T1, T2> {
  1028. typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
  1029. Argument_match<T2>> Ty;
  1030. };
  1031. template <typename T0, typename T1, typename T2, typename T3>
  1032. struct m_Intrinsic_Ty<T0, T1, T2, T3> {
  1033. typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
  1034. Argument_match<T3>> Ty;
  1035. };
  1036. /// \brief Match intrinsic calls like this:
  1037. /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
  1038. template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
  1039. return IntrinsicID_match(IntrID);
  1040. }
  1041. template <Intrinsic::ID IntrID, typename T0>
  1042. inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
  1043. return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
  1044. }
  1045. template <Intrinsic::ID IntrID, typename T0, typename T1>
  1046. inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
  1047. const T1 &Op1) {
  1048. return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
  1049. }
  1050. template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
  1051. inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
  1052. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
  1053. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
  1054. }
  1055. template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
  1056. typename T3>
  1057. inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
  1058. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
  1059. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
  1060. }
  1061. // Helper intrinsic matching specializations.
  1062. template <typename Opnd0>
  1063. inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
  1064. return m_Intrinsic<Intrinsic::bswap>(Op0);
  1065. }
  1066. template <typename Opnd0, typename Opnd1>
  1067. inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
  1068. const Opnd1 &Op1) {
  1069. return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
  1070. }
  1071. template <typename Opnd0, typename Opnd1>
  1072. inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
  1073. const Opnd1 &Op1) {
  1074. return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
  1075. }
  1076. } // end namespace PatternMatch
  1077. } // end namespace llvm
  1078. #endif