Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1020 lines
30 KiB

  1. //===-- llvm/Support/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_SUPPORT_PATTERNMATCH_H
  29. #define LLVM_SUPPORT_PATTERNMATCH_H
  30. #include "llvm/IR/Constants.h"
  31. #include "llvm/IR/Instructions.h"
  32. #include "llvm/IR/IntrinsicInst.h"
  33. #include "llvm/IR/Operator.h"
  34. #include "llvm/Support/CallSite.h"
  35. namespace llvm {
  36. namespace PatternMatch {
  37. template<typename Val, typename Pattern>
  38. bool match(Val *V, const Pattern &P) {
  39. return const_cast<Pattern&>(P).match(V);
  40. }
  41. template<typename SubPattern_t>
  42. struct OneUse_match {
  43. SubPattern_t SubPattern;
  44. OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
  45. template<typename OpTy>
  46. bool match(OpTy *V) {
  47. return V->hasOneUse() && SubPattern.match(V);
  48. }
  49. };
  50. template<typename T>
  51. inline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; }
  52. template<typename Class>
  53. struct class_match {
  54. template<typename ITy>
  55. bool match(ITy *V) { return isa<Class>(V); }
  56. };
  57. /// m_Value() - Match an arbitrary value and ignore it.
  58. inline class_match<Value> m_Value() { return class_match<Value>(); }
  59. /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
  60. inline class_match<ConstantInt> m_ConstantInt() {
  61. return class_match<ConstantInt>();
  62. }
  63. /// m_Undef() - Match an arbitrary undef constant.
  64. inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
  65. inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
  66. /// Matching combinators
  67. template<typename LTy, typename RTy>
  68. struct match_combine_or {
  69. LTy L;
  70. RTy R;
  71. match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
  72. template<typename ITy>
  73. bool match(ITy *V) {
  74. if (L.match(V))
  75. return true;
  76. if (R.match(V))
  77. return true;
  78. return false;
  79. }
  80. };
  81. template<typename LTy, typename RTy>
  82. 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>
  87. bool match(ITy *V) {
  88. if (L.match(V))
  89. if (R.match(V))
  90. return true;
  91. return false;
  92. }
  93. };
  94. /// Combine two pattern matchers matching L || R
  95. template<typename LTy, typename RTy>
  96. inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
  97. return match_combine_or<LTy, RTy>(L, R);
  98. }
  99. /// Combine two pattern matchers matching L && R
  100. template<typename LTy, typename RTy>
  101. inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
  102. return match_combine_and<LTy, RTy>(L, R);
  103. }
  104. struct match_zero {
  105. template<typename ITy>
  106. bool match(ITy *V) {
  107. if (const Constant *C = dyn_cast<Constant>(V))
  108. return C->isNullValue();
  109. return false;
  110. }
  111. };
  112. /// m_Zero() - Match an arbitrary zero/null constant. This includes
  113. /// zero_initializer for vectors and ConstantPointerNull for pointers.
  114. inline match_zero m_Zero() { return match_zero(); }
  115. struct match_neg_zero {
  116. template<typename ITy>
  117. bool match(ITy *V) {
  118. if (const Constant *C = dyn_cast<Constant>(V))
  119. return C->isNegativeZeroValue();
  120. return false;
  121. }
  122. };
  123. /// m_NegZero() - Match an arbitrary zero/null constant. This includes
  124. /// zero_initializer for vectors and ConstantPointerNull for pointers. For
  125. /// floating point constants, this will match negative zero but not positive
  126. /// zero
  127. inline match_neg_zero m_NegZero() { return match_neg_zero(); }
  128. /// m_AnyZero() - Match an arbitrary zero/null constant. This includes
  129. /// zero_initializer for vectors and ConstantPointerNull for pointers. For
  130. /// floating point constants, this will match negative zero and positive zero
  131. inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
  132. return m_CombineOr(m_Zero(), m_NegZero());
  133. }
  134. struct apint_match {
  135. const APInt *&Res;
  136. apint_match(const APInt *&R) : Res(R) {}
  137. template<typename ITy>
  138. bool match(ITy *V) {
  139. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  140. Res = &CI->getValue();
  141. return true;
  142. }
  143. if (V->getType()->isVectorTy())
  144. if (const Constant *C = dyn_cast<Constant>(V))
  145. if (ConstantInt *CI =
  146. dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
  147. Res = &CI->getValue();
  148. return true;
  149. }
  150. return false;
  151. }
  152. };
  153. /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
  154. /// specified pointer to the contained APInt.
  155. inline apint_match m_APInt(const APInt *&Res) { return Res; }
  156. template<int64_t Val>
  157. struct constantint_match {
  158. template<typename ITy>
  159. bool match(ITy *V) {
  160. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  161. const APInt &CIV = CI->getValue();
  162. if (Val >= 0)
  163. return CIV == static_cast<uint64_t>(Val);
  164. // If Val is negative, and CI is shorter than it, truncate to the right
  165. // number of bits. If it is larger, then we have to sign extend. Just
  166. // compare their negated values.
  167. return -CIV == -Val;
  168. }
  169. return false;
  170. }
  171. };
  172. /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
  173. template<int64_t Val>
  174. inline constantint_match<Val> m_ConstantInt() {
  175. return constantint_match<Val>();
  176. }
  177. /// cst_pred_ty - This helper class is used to match scalar and vector constants
  178. /// that satisfy a specified predicate.
  179. template<typename Predicate>
  180. struct cst_pred_ty : public Predicate {
  181. template<typename ITy>
  182. bool match(ITy *V) {
  183. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
  184. return this->isValue(CI->getValue());
  185. if (V->getType()->isVectorTy())
  186. if (const Constant *C = dyn_cast<Constant>(V))
  187. if (const ConstantInt *CI =
  188. dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
  189. return this->isValue(CI->getValue());
  190. return false;
  191. }
  192. };
  193. /// api_pred_ty - This helper class is used to match scalar and vector constants
  194. /// that satisfy a specified predicate, and bind them to an APInt.
  195. template<typename Predicate>
  196. struct api_pred_ty : public Predicate {
  197. const APInt *&Res;
  198. api_pred_ty(const APInt *&R) : Res(R) {}
  199. template<typename ITy>
  200. bool match(ITy *V) {
  201. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
  202. if (this->isValue(CI->getValue())) {
  203. Res = &CI->getValue();
  204. return true;
  205. }
  206. if (V->getType()->isVectorTy())
  207. if (const Constant *C = dyn_cast<Constant>(V))
  208. if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
  209. if (this->isValue(CI->getValue())) {
  210. Res = &CI->getValue();
  211. return true;
  212. }
  213. return false;
  214. }
  215. };
  216. struct is_one {
  217. bool isValue(const APInt &C) { return C == 1; }
  218. };
  219. /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
  220. inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
  221. inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
  222. struct is_all_ones {
  223. bool isValue(const APInt &C) { return C.isAllOnesValue(); }
  224. };
  225. /// m_AllOnes() - Match an integer or vector with all bits set to true.
  226. inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
  227. inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
  228. struct is_sign_bit {
  229. bool isValue(const APInt &C) { return C.isSignBit(); }
  230. };
  231. /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
  232. inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
  233. inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
  234. struct is_power2 {
  235. bool isValue(const APInt &C) { return C.isPowerOf2(); }
  236. };
  237. /// m_Power2() - Match an integer or vector power of 2.
  238. inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
  239. inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
  240. template<typename Class>
  241. struct bind_ty {
  242. Class *&VR;
  243. bind_ty(Class *&V) : VR(V) {}
  244. template<typename ITy>
  245. bool match(ITy *V) {
  246. if (Class *CV = dyn_cast<Class>(V)) {
  247. VR = CV;
  248. return true;
  249. }
  250. return false;
  251. }
  252. };
  253. /// m_Value - Match a value, capturing it if we match.
  254. inline bind_ty<Value> m_Value(Value *&V) { return V; }
  255. /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
  256. inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
  257. /// m_Constant - Match a Constant, capturing the value if we match.
  258. inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
  259. /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
  260. inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
  261. /// specificval_ty - Match a specified Value*.
  262. struct specificval_ty {
  263. const Value *Val;
  264. specificval_ty(const Value *V) : Val(V) {}
  265. template<typename ITy>
  266. bool match(ITy *V) {
  267. return V == Val;
  268. }
  269. };
  270. /// m_Specific - Match if we have a specific specified value.
  271. inline specificval_ty m_Specific(const Value *V) { return V; }
  272. /// Match a specified floating point value or vector of all elements of that
  273. /// value.
  274. struct specific_fpval {
  275. double Val;
  276. specific_fpval(double V) : Val(V) {}
  277. template<typename ITy>
  278. bool match(ITy *V) {
  279. if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
  280. return CFP->isExactlyValue(Val);
  281. if (V->getType()->isVectorTy())
  282. if (const Constant *C = dyn_cast<Constant>(V))
  283. if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
  284. return CFP->isExactlyValue(Val);
  285. return false;
  286. }
  287. };
  288. /// Match a specific floating point value or vector with all elements equal to
  289. /// the value.
  290. inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
  291. /// Match a float 1.0 or vector with all elements equal to 1.0.
  292. inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
  293. struct bind_const_intval_ty {
  294. uint64_t &VR;
  295. bind_const_intval_ty(uint64_t &V) : VR(V) {}
  296. template<typename ITy>
  297. bool match(ITy *V) {
  298. if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
  299. if (CV->getBitWidth() <= 64) {
  300. VR = CV->getZExtValue();
  301. return true;
  302. }
  303. return false;
  304. }
  305. };
  306. /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
  307. /// match ConstantInts wider than 64-bits.
  308. inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
  309. //===----------------------------------------------------------------------===//
  310. // Matchers for specific binary operators.
  311. //
  312. template<typename LHS_t, typename RHS_t, unsigned Opcode>
  313. struct BinaryOp_match {
  314. LHS_t L;
  315. RHS_t R;
  316. BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  317. template<typename OpTy>
  318. bool match(OpTy *V) {
  319. if (V->getValueID() == Value::InstructionVal + Opcode) {
  320. BinaryOperator *I = cast<BinaryOperator>(V);
  321. return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
  322. }
  323. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
  324. return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
  325. R.match(CE->getOperand(1));
  326. return false;
  327. }
  328. };
  329. template<typename LHS, typename RHS>
  330. inline BinaryOp_match<LHS, RHS, Instruction::Add>
  331. m_Add(const LHS &L, const RHS &R) {
  332. return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
  333. }
  334. template<typename LHS, typename RHS>
  335. inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
  336. m_FAdd(const LHS &L, const RHS &R) {
  337. return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
  338. }
  339. template<typename LHS, typename RHS>
  340. inline BinaryOp_match<LHS, RHS, Instruction::Sub>
  341. m_Sub(const LHS &L, const RHS &R) {
  342. return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
  343. }
  344. template<typename LHS, typename RHS>
  345. inline BinaryOp_match<LHS, RHS, Instruction::FSub>
  346. m_FSub(const LHS &L, const RHS &R) {
  347. return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
  348. }
  349. template<typename LHS, typename RHS>
  350. inline BinaryOp_match<LHS, RHS, Instruction::Mul>
  351. m_Mul(const LHS &L, const RHS &R) {
  352. return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
  353. }
  354. template<typename LHS, typename RHS>
  355. inline BinaryOp_match<LHS, RHS, Instruction::FMul>
  356. m_FMul(const LHS &L, const RHS &R) {
  357. return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
  358. }
  359. template<typename LHS, typename RHS>
  360. inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
  361. m_UDiv(const LHS &L, const RHS &R) {
  362. return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
  363. }
  364. template<typename LHS, typename RHS>
  365. inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
  366. m_SDiv(const LHS &L, const RHS &R) {
  367. return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
  368. }
  369. template<typename LHS, typename RHS>
  370. inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
  371. m_FDiv(const LHS &L, const RHS &R) {
  372. return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
  373. }
  374. template<typename LHS, typename RHS>
  375. inline BinaryOp_match<LHS, RHS, Instruction::URem>
  376. m_URem(const LHS &L, const RHS &R) {
  377. return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
  378. }
  379. template<typename LHS, typename RHS>
  380. inline BinaryOp_match<LHS, RHS, Instruction::SRem>
  381. m_SRem(const LHS &L, const RHS &R) {
  382. return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
  383. }
  384. template<typename LHS, typename RHS>
  385. inline BinaryOp_match<LHS, RHS, Instruction::FRem>
  386. m_FRem(const LHS &L, const RHS &R) {
  387. return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
  388. }
  389. template<typename LHS, typename RHS>
  390. inline BinaryOp_match<LHS, RHS, Instruction::And>
  391. m_And(const LHS &L, const RHS &R) {
  392. return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
  393. }
  394. template<typename LHS, typename RHS>
  395. inline BinaryOp_match<LHS, RHS, Instruction::Or>
  396. m_Or(const LHS &L, const RHS &R) {
  397. return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
  398. }
  399. template<typename LHS, typename RHS>
  400. inline BinaryOp_match<LHS, RHS, Instruction::Xor>
  401. m_Xor(const LHS &L, const RHS &R) {
  402. return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
  403. }
  404. template<typename LHS, typename RHS>
  405. inline BinaryOp_match<LHS, RHS, Instruction::Shl>
  406. m_Shl(const LHS &L, const RHS &R) {
  407. return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
  408. }
  409. template<typename LHS, typename RHS>
  410. inline BinaryOp_match<LHS, RHS, Instruction::LShr>
  411. m_LShr(const LHS &L, const RHS &R) {
  412. return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
  413. }
  414. template<typename LHS, typename RHS>
  415. inline BinaryOp_match<LHS, RHS, Instruction::AShr>
  416. m_AShr(const LHS &L, const RHS &R) {
  417. return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
  418. }
  419. //===----------------------------------------------------------------------===//
  420. // Class that matches two different binary ops.
  421. //
  422. template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
  423. struct BinOp2_match {
  424. LHS_t L;
  425. RHS_t R;
  426. BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
  427. template<typename OpTy>
  428. bool match(OpTy *V) {
  429. if (V->getValueID() == Value::InstructionVal + Opc1 ||
  430. V->getValueID() == Value::InstructionVal + Opc2) {
  431. BinaryOperator *I = cast<BinaryOperator>(V);
  432. return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
  433. }
  434. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
  435. return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
  436. L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
  437. return false;
  438. }
  439. };
  440. /// m_Shr - Matches LShr or AShr.
  441. template<typename LHS, typename RHS>
  442. inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
  443. m_Shr(const LHS &L, const RHS &R) {
  444. return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
  445. }
  446. /// m_LogicalShift - Matches LShr or Shl.
  447. template<typename LHS, typename RHS>
  448. inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
  449. m_LogicalShift(const LHS &L, const RHS &R) {
  450. return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
  451. }
  452. /// m_IDiv - Matches UDiv and SDiv.
  453. template<typename LHS, typename RHS>
  454. inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
  455. m_IDiv(const LHS &L, const RHS &R) {
  456. return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
  457. }
  458. //===----------------------------------------------------------------------===//
  459. // Class that matches exact binary ops.
  460. //
  461. template<typename SubPattern_t>
  462. struct Exact_match {
  463. SubPattern_t SubPattern;
  464. Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
  465. template<typename OpTy>
  466. bool match(OpTy *V) {
  467. if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
  468. return PEO->isExact() && SubPattern.match(V);
  469. return false;
  470. }
  471. };
  472. template<typename T>
  473. inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
  474. //===----------------------------------------------------------------------===//
  475. // Matchers for CmpInst classes
  476. //
  477. template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
  478. struct CmpClass_match {
  479. PredicateTy &Predicate;
  480. LHS_t L;
  481. RHS_t R;
  482. CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
  483. : Predicate(Pred), L(LHS), R(RHS) {}
  484. template<typename OpTy>
  485. bool match(OpTy *V) {
  486. if (Class *I = dyn_cast<Class>(V))
  487. if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
  488. Predicate = I->getPredicate();
  489. return true;
  490. }
  491. return false;
  492. }
  493. };
  494. template<typename LHS, typename RHS>
  495. inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
  496. m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  497. return CmpClass_match<LHS, RHS,
  498. ICmpInst, ICmpInst::Predicate>(Pred, L, R);
  499. }
  500. template<typename LHS, typename RHS>
  501. inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
  502. m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
  503. return CmpClass_match<LHS, RHS,
  504. FCmpInst, FCmpInst::Predicate>(Pred, L, R);
  505. }
  506. //===----------------------------------------------------------------------===//
  507. // Matchers for SelectInst classes
  508. //
  509. template<typename Cond_t, typename LHS_t, typename RHS_t>
  510. struct SelectClass_match {
  511. Cond_t C;
  512. LHS_t L;
  513. RHS_t R;
  514. SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
  515. const RHS_t &RHS)
  516. : C(Cond), L(LHS), R(RHS) {}
  517. template<typename OpTy>
  518. bool match(OpTy *V) {
  519. if (SelectInst *I = dyn_cast<SelectInst>(V))
  520. return C.match(I->getOperand(0)) &&
  521. L.match(I->getOperand(1)) &&
  522. R.match(I->getOperand(2));
  523. return false;
  524. }
  525. };
  526. template<typename Cond, typename LHS, typename RHS>
  527. inline SelectClass_match<Cond, LHS, RHS>
  528. m_Select(const Cond &C, const LHS &L, const RHS &R) {
  529. return SelectClass_match<Cond, LHS, RHS>(C, L, R);
  530. }
  531. /// m_SelectCst - This matches a select of two constants, e.g.:
  532. /// m_SelectCst<-1, 0>(m_Value(V))
  533. template<int64_t L, int64_t R, typename Cond>
  534. inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
  535. m_SelectCst(const Cond &C) {
  536. return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
  537. }
  538. //===----------------------------------------------------------------------===//
  539. // Matchers for CastInst classes
  540. //
  541. template<typename Op_t, unsigned Opcode>
  542. struct CastClass_match {
  543. Op_t Op;
  544. CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
  545. template<typename OpTy>
  546. bool match(OpTy *V) {
  547. if (Operator *O = dyn_cast<Operator>(V))
  548. return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
  549. return false;
  550. }
  551. };
  552. /// m_BitCast
  553. template<typename OpTy>
  554. inline CastClass_match<OpTy, Instruction::BitCast>
  555. m_BitCast(const OpTy &Op) {
  556. return CastClass_match<OpTy, Instruction::BitCast>(Op);
  557. }
  558. /// m_PtrToInt
  559. template<typename OpTy>
  560. inline CastClass_match<OpTy, Instruction::PtrToInt>
  561. m_PtrToInt(const OpTy &Op) {
  562. return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
  563. }
  564. /// m_Trunc
  565. template<typename OpTy>
  566. inline CastClass_match<OpTy, Instruction::Trunc>
  567. m_Trunc(const OpTy &Op) {
  568. return CastClass_match<OpTy, Instruction::Trunc>(Op);
  569. }
  570. /// m_SExt
  571. template<typename OpTy>
  572. inline CastClass_match<OpTy, Instruction::SExt>
  573. m_SExt(const OpTy &Op) {
  574. return CastClass_match<OpTy, Instruction::SExt>(Op);
  575. }
  576. /// m_ZExt
  577. template<typename OpTy>
  578. inline CastClass_match<OpTy, Instruction::ZExt>
  579. m_ZExt(const OpTy &Op) {
  580. return CastClass_match<OpTy, Instruction::ZExt>(Op);
  581. }
  582. //===----------------------------------------------------------------------===//
  583. // Matchers for unary operators
  584. //
  585. template<typename LHS_t>
  586. struct not_match {
  587. LHS_t L;
  588. not_match(const LHS_t &LHS) : L(LHS) {}
  589. template<typename OpTy>
  590. bool match(OpTy *V) {
  591. if (Operator *O = dyn_cast<Operator>(V))
  592. if (O->getOpcode() == Instruction::Xor)
  593. return matchIfNot(O->getOperand(0), O->getOperand(1));
  594. return false;
  595. }
  596. private:
  597. bool matchIfNot(Value *LHS, Value *RHS) {
  598. return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
  599. // FIXME: Remove CV.
  600. isa<ConstantVector>(RHS)) &&
  601. cast<Constant>(RHS)->isAllOnesValue() &&
  602. L.match(LHS);
  603. }
  604. };
  605. template<typename LHS>
  606. inline not_match<LHS> m_Not(const LHS &L) { return L; }
  607. template<typename LHS_t>
  608. struct neg_match {
  609. LHS_t L;
  610. neg_match(const LHS_t &LHS) : L(LHS) {}
  611. template<typename OpTy>
  612. bool match(OpTy *V) {
  613. if (Operator *O = dyn_cast<Operator>(V))
  614. if (O->getOpcode() == Instruction::Sub)
  615. return matchIfNeg(O->getOperand(0), O->getOperand(1));
  616. return false;
  617. }
  618. private:
  619. bool matchIfNeg(Value *LHS, Value *RHS) {
  620. return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
  621. isa<ConstantAggregateZero>(LHS)) &&
  622. L.match(RHS);
  623. }
  624. };
  625. /// m_Neg - Match an integer negate.
  626. template<typename LHS>
  627. inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
  628. template<typename LHS_t>
  629. struct fneg_match {
  630. LHS_t L;
  631. fneg_match(const LHS_t &LHS) : L(LHS) {}
  632. template<typename OpTy>
  633. bool match(OpTy *V) {
  634. if (Operator *O = dyn_cast<Operator>(V))
  635. if (O->getOpcode() == Instruction::FSub)
  636. return matchIfFNeg(O->getOperand(0), O->getOperand(1));
  637. return false;
  638. }
  639. private:
  640. bool matchIfFNeg(Value *LHS, Value *RHS) {
  641. if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
  642. return C->isNegativeZeroValue() && L.match(RHS);
  643. return false;
  644. }
  645. };
  646. /// m_FNeg - Match a floating point negate.
  647. template<typename LHS>
  648. inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
  649. //===----------------------------------------------------------------------===//
  650. // Matchers for control flow.
  651. //
  652. struct br_match {
  653. BasicBlock *&Succ;
  654. br_match(BasicBlock *&Succ)
  655. : Succ(Succ) {
  656. }
  657. template<typename OpTy>
  658. bool match(OpTy *V) {
  659. if (BranchInst *BI = dyn_cast<BranchInst>(V))
  660. if (BI->isUnconditional()) {
  661. Succ = BI->getSuccessor(0);
  662. return true;
  663. }
  664. return false;
  665. }
  666. };
  667. inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
  668. template<typename Cond_t>
  669. struct brc_match {
  670. Cond_t Cond;
  671. BasicBlock *&T, *&F;
  672. brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
  673. : Cond(C), T(t), F(f) {
  674. }
  675. template<typename OpTy>
  676. bool match(OpTy *V) {
  677. if (BranchInst *BI = dyn_cast<BranchInst>(V))
  678. if (BI->isConditional() && Cond.match(BI->getCondition())) {
  679. T = BI->getSuccessor(0);
  680. F = BI->getSuccessor(1);
  681. return true;
  682. }
  683. return false;
  684. }
  685. };
  686. template<typename Cond_t>
  687. inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
  688. return brc_match<Cond_t>(C, T, F);
  689. }
  690. //===----------------------------------------------------------------------===//
  691. // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
  692. //
  693. template<typename LHS_t, typename RHS_t, typename Pred_t>
  694. struct MaxMin_match {
  695. LHS_t L;
  696. RHS_t R;
  697. MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
  698. : L(LHS), R(RHS) {}
  699. template<typename OpTy>
  700. bool match(OpTy *V) {
  701. // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
  702. SelectInst *SI = dyn_cast<SelectInst>(V);
  703. if (!SI)
  704. return false;
  705. ICmpInst *Cmp = dyn_cast<ICmpInst>(SI->getCondition());
  706. if (!Cmp)
  707. return false;
  708. // At this point we have a select conditioned on a comparison. Check that
  709. // it is the values returned by the select that are being compared.
  710. Value *TrueVal = SI->getTrueValue();
  711. Value *FalseVal = SI->getFalseValue();
  712. Value *LHS = Cmp->getOperand(0);
  713. Value *RHS = Cmp->getOperand(1);
  714. if ((TrueVal != LHS || FalseVal != RHS) &&
  715. (TrueVal != RHS || FalseVal != LHS))
  716. return false;
  717. ICmpInst::Predicate Pred = LHS == TrueVal ?
  718. Cmp->getPredicate() : Cmp->getSwappedPredicate();
  719. // Does "(x pred y) ? x : y" represent the desired max/min operation?
  720. if (!Pred_t::match(Pred))
  721. return false;
  722. // It does! Bind the operands.
  723. return L.match(LHS) && R.match(RHS);
  724. }
  725. };
  726. /// smax_pred_ty - Helper class for identifying signed max predicates.
  727. struct smax_pred_ty {
  728. static bool match(ICmpInst::Predicate Pred) {
  729. return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
  730. }
  731. };
  732. /// smin_pred_ty - Helper class for identifying signed min predicates.
  733. struct smin_pred_ty {
  734. static bool match(ICmpInst::Predicate Pred) {
  735. return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
  736. }
  737. };
  738. /// umax_pred_ty - Helper class for identifying unsigned max predicates.
  739. struct umax_pred_ty {
  740. static bool match(ICmpInst::Predicate Pred) {
  741. return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
  742. }
  743. };
  744. /// umin_pred_ty - Helper class for identifying unsigned min predicates.
  745. struct umin_pred_ty {
  746. static bool match(ICmpInst::Predicate Pred) {
  747. return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
  748. }
  749. };
  750. template<typename LHS, typename RHS>
  751. inline MaxMin_match<LHS, RHS, smax_pred_ty>
  752. m_SMax(const LHS &L, const RHS &R) {
  753. return MaxMin_match<LHS, RHS, smax_pred_ty>(L, R);
  754. }
  755. template<typename LHS, typename RHS>
  756. inline MaxMin_match<LHS, RHS, smin_pred_ty>
  757. m_SMin(const LHS &L, const RHS &R) {
  758. return MaxMin_match<LHS, RHS, smin_pred_ty>(L, R);
  759. }
  760. template<typename LHS, typename RHS>
  761. inline MaxMin_match<LHS, RHS, umax_pred_ty>
  762. m_UMax(const LHS &L, const RHS &R) {
  763. return MaxMin_match<LHS, RHS, umax_pred_ty>(L, R);
  764. }
  765. template<typename LHS, typename RHS>
  766. inline MaxMin_match<LHS, RHS, umin_pred_ty>
  767. m_UMin(const LHS &L, const RHS &R) {
  768. return MaxMin_match<LHS, RHS, umin_pred_ty>(L, R);
  769. }
  770. template<typename Opnd_t>
  771. struct Argument_match {
  772. unsigned OpI;
  773. Opnd_t Val;
  774. Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
  775. template<typename OpTy>
  776. bool match(OpTy *V) {
  777. CallSite CS(V);
  778. return CS.isCall() && Val.match(CS.getArgument(OpI));
  779. }
  780. };
  781. /// Match an argument
  782. template<unsigned OpI, typename Opnd_t>
  783. inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
  784. return Argument_match<Opnd_t>(OpI, Op);
  785. }
  786. /// Intrinsic matchers.
  787. struct IntrinsicID_match {
  788. unsigned ID;
  789. IntrinsicID_match(unsigned IntrID) : ID(IntrID) { }
  790. template<typename OpTy>
  791. bool match(OpTy *V) {
  792. IntrinsicInst *II = dyn_cast<IntrinsicInst>(V);
  793. return II && II->getIntrinsicID() == ID;
  794. }
  795. };
  796. /// Intrinsic matches are combinations of ID matchers, and argument
  797. /// matchers. Higher arity matcher are defined recursively in terms of and-ing
  798. /// them with lower arity matchers. Here's some convenient typedefs for up to
  799. /// several arguments, and more can be added as needed
  800. template <typename T0 = void, typename T1 = void, typename T2 = void,
  801. typename T3 = void, typename T4 = void, typename T5 = void,
  802. typename T6 = void, typename T7 = void, typename T8 = void,
  803. typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
  804. template <typename T0>
  805. struct m_Intrinsic_Ty<T0> {
  806. typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
  807. };
  808. template <typename T0, typename T1>
  809. struct m_Intrinsic_Ty<T0, T1> {
  810. typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
  811. Argument_match<T1> > Ty;
  812. };
  813. template <typename T0, typename T1, typename T2>
  814. struct m_Intrinsic_Ty<T0, T1, T2> {
  815. typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
  816. Argument_match<T2> > Ty;
  817. };
  818. template <typename T0, typename T1, typename T2, typename T3>
  819. struct m_Intrinsic_Ty<T0, T1, T2, T3> {
  820. typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
  821. Argument_match<T3> > Ty;
  822. };
  823. /// Match intrinsic calls like this:
  824. /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
  825. template <unsigned IntrID>
  826. inline IntrinsicID_match
  827. m_Intrinsic() { return IntrinsicID_match(IntrID); }
  828. template<unsigned IntrID, typename T0>
  829. inline typename m_Intrinsic_Ty<T0>::Ty
  830. m_Intrinsic(const T0 &Op0) {
  831. return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
  832. }
  833. template<unsigned IntrID, typename T0, typename T1>
  834. inline typename m_Intrinsic_Ty<T0, T1>::Ty
  835. m_Intrinsic(const T0 &Op0, const T1 &Op1) {
  836. return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
  837. }
  838. template<unsigned IntrID, typename T0, typename T1, typename T2>
  839. inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
  840. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
  841. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
  842. }
  843. template<unsigned IntrID, typename T0, typename T1, typename T2, typename T3>
  844. inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
  845. m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
  846. return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
  847. }
  848. // Helper intrinsic matching specializations
  849. template<typename Opnd0>
  850. inline typename m_Intrinsic_Ty<Opnd0>::Ty
  851. m_BSwap(const Opnd0 &Op0) {
  852. return m_Intrinsic<Intrinsic::bswap>(Op0);
  853. }
  854. } // end namespace PatternMatch
  855. } // end namespace llvm
  856. #endif