1 //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file provides a simple and efficient mechanism for performing general 10 // tree-based pattern matches on the LLVM IR. The power of these routines is 11 // that it allows you to write concise patterns that are expressive and easy to 12 // understand. The other major advantage of this is that it allows you to 13 // trivially capture/bind elements in the pattern to variables. For example, 14 // you can do something like this: 15 // 16 // Value *Exp = ... 17 // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2) 18 // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)), 19 // m_And(m_Value(Y), m_ConstantInt(C2))))) { 20 // ... Pattern is matched and variables are bound ... 21 // } 22 // 23 // This is primarily useful to things like the instruction combiner, but can 24 // also be useful for static analysis tools or code generators. 25 // 26 //===----------------------------------------------------------------------===// 27 28 #ifndef LLVM_IR_PATTERNMATCH_H 29 #define LLVM_IR_PATTERNMATCH_H 30 31 #include "llvm/ADT/APFloat.h" 32 #include "llvm/ADT/APInt.h" 33 #include "llvm/IR/Constant.h" 34 #include "llvm/IR/Constants.h" 35 #include "llvm/IR/InstrTypes.h" 36 #include "llvm/IR/Instruction.h" 37 #include "llvm/IR/Instructions.h" 38 #include "llvm/IR/Intrinsics.h" 39 #include "llvm/IR/Operator.h" 40 #include "llvm/IR/Value.h" 41 #include "llvm/Support/Casting.h" 42 #include <cstdint> 43 44 namespace llvm { 45 namespace PatternMatch { 46 47 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) { 48 return const_cast<Pattern &>(P).match(V); 49 } 50 51 template <typename SubPattern_t> struct OneUse_match { 52 SubPattern_t SubPattern; 53 54 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} 55 56 template <typename OpTy> bool match(OpTy *V) { 57 return V->hasOneUse() && SubPattern.match(V); 58 } 59 }; 60 61 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) { 62 return SubPattern; 63 } 64 65 template <typename Class> struct class_match { 66 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); } 67 }; 68 69 /// Match an arbitrary value and ignore it. 70 inline class_match<Value> m_Value() { return class_match<Value>(); } 71 72 /// Match an arbitrary binary operation and ignore it. 73 inline class_match<BinaryOperator> m_BinOp() { 74 return class_match<BinaryOperator>(); 75 } 76 77 /// Matches any compare instruction and ignore it. 78 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); } 79 80 /// Match an arbitrary ConstantInt and ignore it. 81 inline class_match<ConstantInt> m_ConstantInt() { 82 return class_match<ConstantInt>(); 83 } 84 85 /// Match an arbitrary undef constant. 86 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); } 87 88 /// Match an arbitrary Constant and ignore it. 89 inline class_match<Constant> m_Constant() { return class_match<Constant>(); } 90 91 /// Matching combinators 92 template <typename LTy, typename RTy> struct match_combine_or { 93 LTy L; 94 RTy R; 95 96 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} 97 98 template <typename ITy> bool match(ITy *V) { 99 if (L.match(V)) 100 return true; 101 if (R.match(V)) 102 return true; 103 return false; 104 } 105 }; 106 107 template <typename LTy, typename RTy> struct match_combine_and { 108 LTy L; 109 RTy R; 110 111 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} 112 113 template <typename ITy> bool match(ITy *V) { 114 if (L.match(V)) 115 if (R.match(V)) 116 return true; 117 return false; 118 } 119 }; 120 121 /// Combine two pattern matchers matching L || R 122 template <typename LTy, typename RTy> 123 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { 124 return match_combine_or<LTy, RTy>(L, R); 125 } 126 127 /// Combine two pattern matchers matching L && R 128 template <typename LTy, typename RTy> 129 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { 130 return match_combine_and<LTy, RTy>(L, R); 131 } 132 133 struct apint_match { 134 const APInt *&Res; 135 136 apint_match(const APInt *&R) : Res(R) {} 137 138 template <typename ITy> bool match(ITy *V) { 139 if (auto *CI = dyn_cast<ConstantInt>(V)) { 140 Res = &CI->getValue(); 141 return true; 142 } 143 if (V->getType()->isVectorTy()) 144 if (const auto *C = dyn_cast<Constant>(V)) 145 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) { 146 Res = &CI->getValue(); 147 return true; 148 } 149 return false; 150 } 151 }; 152 // Either constexpr if or renaming ConstantFP::getValueAPF to 153 // ConstantFP::getValue is needed to do it via single template 154 // function for both apint/apfloat. 155 struct apfloat_match { 156 const APFloat *&Res; 157 apfloat_match(const APFloat *&R) : Res(R) {} 158 template <typename ITy> bool match(ITy *V) { 159 if (auto *CI = dyn_cast<ConstantFP>(V)) { 160 Res = &CI->getValueAPF(); 161 return true; 162 } 163 if (V->getType()->isVectorTy()) 164 if (const auto *C = dyn_cast<Constant>(V)) 165 if (auto *CI = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) { 166 Res = &CI->getValueAPF(); 167 return true; 168 } 169 return false; 170 } 171 }; 172 173 /// Match a ConstantInt or splatted ConstantVector, binding the 174 /// specified pointer to the contained APInt. 175 inline apint_match m_APInt(const APInt *&Res) { return Res; } 176 177 /// Match a ConstantFP or splatted ConstantVector, binding the 178 /// specified pointer to the contained APFloat. 179 inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; } 180 181 template <int64_t Val> struct constantint_match { 182 template <typename ITy> bool match(ITy *V) { 183 if (const auto *CI = dyn_cast<ConstantInt>(V)) { 184 const APInt &CIV = CI->getValue(); 185 if (Val >= 0) 186 return CIV == static_cast<uint64_t>(Val); 187 // If Val is negative, and CI is shorter than it, truncate to the right 188 // number of bits. If it is larger, then we have to sign extend. Just 189 // compare their negated values. 190 return -CIV == -Val; 191 } 192 return false; 193 } 194 }; 195 196 /// Match a ConstantInt with a specific value. 197 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { 198 return constantint_match<Val>(); 199 } 200 201 /// This helper class is used to match scalar and vector integer constants that 202 /// satisfy a specified predicate. 203 /// For vector constants, undefined elements are ignored. 204 template <typename Predicate> struct cst_pred_ty : public Predicate { 205 template <typename ITy> bool match(ITy *V) { 206 if (const auto *CI = dyn_cast<ConstantInt>(V)) 207 return this->isValue(CI->getValue()); 208 if (V->getType()->isVectorTy()) { 209 if (const auto *C = dyn_cast<Constant>(V)) { 210 if (const auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) 211 return this->isValue(CI->getValue()); 212 213 // Non-splat vector constant: check each element for a match. 214 unsigned NumElts = V->getType()->getVectorNumElements(); 215 assert(NumElts != 0 && "Constant vector with no elements?"); 216 bool HasNonUndefElements = false; 217 for (unsigned i = 0; i != NumElts; ++i) { 218 Constant *Elt = C->getAggregateElement(i); 219 if (!Elt) 220 return false; 221 if (isa<UndefValue>(Elt)) 222 continue; 223 auto *CI = dyn_cast<ConstantInt>(Elt); 224 if (!CI || !this->isValue(CI->getValue())) 225 return false; 226 HasNonUndefElements = true; 227 } 228 return HasNonUndefElements; 229 } 230 } 231 return false; 232 } 233 }; 234 235 /// This helper class is used to match scalar and vector constants that 236 /// satisfy a specified predicate, and bind them to an APInt. 237 template <typename Predicate> struct api_pred_ty : public Predicate { 238 const APInt *&Res; 239 240 api_pred_ty(const APInt *&R) : Res(R) {} 241 242 template <typename ITy> bool match(ITy *V) { 243 if (const auto *CI = dyn_cast<ConstantInt>(V)) 244 if (this->isValue(CI->getValue())) { 245 Res = &CI->getValue(); 246 return true; 247 } 248 if (V->getType()->isVectorTy()) 249 if (const auto *C = dyn_cast<Constant>(V)) 250 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) 251 if (this->isValue(CI->getValue())) { 252 Res = &CI->getValue(); 253 return true; 254 } 255 256 return false; 257 } 258 }; 259 260 /// This helper class is used to match scalar and vector floating-point 261 /// constants that satisfy a specified predicate. 262 /// For vector constants, undefined elements are ignored. 263 template <typename Predicate> struct cstfp_pred_ty : public Predicate { 264 template <typename ITy> bool match(ITy *V) { 265 if (const auto *CF = dyn_cast<ConstantFP>(V)) 266 return this->isValue(CF->getValueAPF()); 267 if (V->getType()->isVectorTy()) { 268 if (const auto *C = dyn_cast<Constant>(V)) { 269 if (const auto *CF = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) 270 return this->isValue(CF->getValueAPF()); 271 272 // Non-splat vector constant: check each element for a match. 273 unsigned NumElts = V->getType()->getVectorNumElements(); 274 assert(NumElts != 0 && "Constant vector with no elements?"); 275 bool HasNonUndefElements = false; 276 for (unsigned i = 0; i != NumElts; ++i) { 277 Constant *Elt = C->getAggregateElement(i); 278 if (!Elt) 279 return false; 280 if (isa<UndefValue>(Elt)) 281 continue; 282 auto *CF = dyn_cast<ConstantFP>(Elt); 283 if (!CF || !this->isValue(CF->getValueAPF())) 284 return false; 285 HasNonUndefElements = true; 286 } 287 return HasNonUndefElements; 288 } 289 } 290 return false; 291 } 292 }; 293 294 /////////////////////////////////////////////////////////////////////////////// 295 // 296 // Encapsulate constant value queries for use in templated predicate matchers. 297 // This allows checking if constants match using compound predicates and works 298 // with vector constants, possibly with relaxed constraints. For example, ignore 299 // undef values. 300 // 301 /////////////////////////////////////////////////////////////////////////////// 302 303 struct is_all_ones { 304 bool isValue(const APInt &C) { return C.isAllOnesValue(); } 305 }; 306 /// Match an integer or vector with all bits set. 307 /// For vectors, this includes constants with undefined elements. 308 inline cst_pred_ty<is_all_ones> m_AllOnes() { 309 return cst_pred_ty<is_all_ones>(); 310 } 311 312 struct is_maxsignedvalue { 313 bool isValue(const APInt &C) { return C.isMaxSignedValue(); } 314 }; 315 /// Match an integer or vector with values having all bits except for the high 316 /// bit set (0x7f...). 317 /// For vectors, this includes constants with undefined elements. 318 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { 319 return cst_pred_ty<is_maxsignedvalue>(); 320 } 321 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { 322 return V; 323 } 324 325 struct is_negative { 326 bool isValue(const APInt &C) { return C.isNegative(); } 327 }; 328 /// Match an integer or vector of negative values. 329 /// For vectors, this includes constants with undefined elements. 330 inline cst_pred_ty<is_negative> m_Negative() { 331 return cst_pred_ty<is_negative>(); 332 } 333 inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { 334 return V; 335 } 336 337 struct is_nonnegative { 338 bool isValue(const APInt &C) { return C.isNonNegative(); } 339 }; 340 /// Match an integer or vector of nonnegative values. 341 /// For vectors, this includes constants with undefined elements. 342 inline cst_pred_ty<is_nonnegative> m_NonNegative() { 343 return cst_pred_ty<is_nonnegative>(); 344 } 345 inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { 346 return V; 347 } 348 349 struct is_one { 350 bool isValue(const APInt &C) { return C.isOneValue(); } 351 }; 352 /// Match an integer 1 or a vector with all elements equal to 1. 353 /// For vectors, this includes constants with undefined elements. 354 inline cst_pred_ty<is_one> m_One() { 355 return cst_pred_ty<is_one>(); 356 } 357 358 struct is_zero_int { 359 bool isValue(const APInt &C) { return C.isNullValue(); } 360 }; 361 /// Match an integer 0 or a vector with all elements equal to 0. 362 /// For vectors, this includes constants with undefined elements. 363 inline cst_pred_ty<is_zero_int> m_ZeroInt() { 364 return cst_pred_ty<is_zero_int>(); 365 } 366 367 struct is_zero { 368 template <typename ITy> bool match(ITy *V) { 369 auto *C = dyn_cast<Constant>(V); 370 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); 371 } 372 }; 373 /// Match any null constant or a vector with all elements equal to 0. 374 /// For vectors, this includes constants with undefined elements. 375 inline is_zero m_Zero() { 376 return is_zero(); 377 } 378 379 struct is_power2 { 380 bool isValue(const APInt &C) { return C.isPowerOf2(); } 381 }; 382 /// Match an integer or vector power-of-2. 383 /// For vectors, this includes constants with undefined elements. 384 inline cst_pred_ty<is_power2> m_Power2() { 385 return cst_pred_ty<is_power2>(); 386 } 387 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { 388 return V; 389 } 390 391 struct is_power2_or_zero { 392 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } 393 }; 394 /// Match an integer or vector of 0 or power-of-2 values. 395 /// For vectors, this includes constants with undefined elements. 396 inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { 397 return cst_pred_ty<is_power2_or_zero>(); 398 } 399 inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { 400 return V; 401 } 402 403 struct is_sign_mask { 404 bool isValue(const APInt &C) { return C.isSignMask(); } 405 }; 406 /// Match an integer or vector with only the sign bit(s) set. 407 /// For vectors, this includes constants with undefined elements. 408 inline cst_pred_ty<is_sign_mask> m_SignMask() { 409 return cst_pred_ty<is_sign_mask>(); 410 } 411 412 struct is_lowbit_mask { 413 bool isValue(const APInt &C) { return C.isMask(); } 414 }; 415 /// Match an integer or vector with only the low bit(s) set. 416 /// For vectors, this includes constants with undefined elements. 417 inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { 418 return cst_pred_ty<is_lowbit_mask>(); 419 } 420 421 struct icmp_pred_with_threshold { 422 ICmpInst::Predicate Pred; 423 const APInt *Thr; 424 bool isValue(const APInt &C) { 425 switch (Pred) { 426 case ICmpInst::Predicate::ICMP_EQ: 427 return C.eq(*Thr); 428 case ICmpInst::Predicate::ICMP_NE: 429 return C.ne(*Thr); 430 case ICmpInst::Predicate::ICMP_UGT: 431 return C.ugt(*Thr); 432 case ICmpInst::Predicate::ICMP_UGE: 433 return C.uge(*Thr); 434 case ICmpInst::Predicate::ICMP_ULT: 435 return C.ult(*Thr); 436 case ICmpInst::Predicate::ICMP_ULE: 437 return C.ule(*Thr); 438 case ICmpInst::Predicate::ICMP_SGT: 439 return C.sgt(*Thr); 440 case ICmpInst::Predicate::ICMP_SGE: 441 return C.sge(*Thr); 442 case ICmpInst::Predicate::ICMP_SLT: 443 return C.slt(*Thr); 444 case ICmpInst::Predicate::ICMP_SLE: 445 return C.sle(*Thr); 446 default: 447 llvm_unreachable("Unhandled ICmp predicate"); 448 } 449 } 450 }; 451 /// Match an integer or vector with every element comparing 'pred' (eg/ne/...) 452 /// to Threshold. For vectors, this includes constants with undefined elements. 453 inline cst_pred_ty<icmp_pred_with_threshold> 454 m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) { 455 cst_pred_ty<icmp_pred_with_threshold> P; 456 P.Pred = Predicate; 457 P.Thr = &Threshold; 458 return P; 459 } 460 461 struct is_nan { 462 bool isValue(const APFloat &C) { return C.isNaN(); } 463 }; 464 /// Match an arbitrary NaN constant. This includes quiet and signalling nans. 465 /// For vectors, this includes constants with undefined elements. 466 inline cstfp_pred_ty<is_nan> m_NaN() { 467 return cstfp_pred_ty<is_nan>(); 468 } 469 470 struct is_any_zero_fp { 471 bool isValue(const APFloat &C) { return C.isZero(); } 472 }; 473 /// Match a floating-point negative zero or positive zero. 474 /// For vectors, this includes constants with undefined elements. 475 inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { 476 return cstfp_pred_ty<is_any_zero_fp>(); 477 } 478 479 struct is_pos_zero_fp { 480 bool isValue(const APFloat &C) { return C.isPosZero(); } 481 }; 482 /// Match a floating-point positive zero. 483 /// For vectors, this includes constants with undefined elements. 484 inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { 485 return cstfp_pred_ty<is_pos_zero_fp>(); 486 } 487 488 struct is_neg_zero_fp { 489 bool isValue(const APFloat &C) { return C.isNegZero(); } 490 }; 491 /// Match a floating-point negative zero. 492 /// For vectors, this includes constants with undefined elements. 493 inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { 494 return cstfp_pred_ty<is_neg_zero_fp>(); 495 } 496 497 /////////////////////////////////////////////////////////////////////////////// 498 499 template <typename Class> struct bind_ty { 500 Class *&VR; 501 502 bind_ty(Class *&V) : VR(V) {} 503 504 template <typename ITy> bool match(ITy *V) { 505 if (auto *CV = dyn_cast<Class>(V)) { 506 VR = CV; 507 return true; 508 } 509 return false; 510 } 511 }; 512 513 /// Match a value, capturing it if we match. 514 inline bind_ty<Value> m_Value(Value *&V) { return V; } 515 inline bind_ty<const Value> m_Value(const Value *&V) { return V; } 516 517 /// Match an instruction, capturing it if we match. 518 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } 519 /// Match a binary operator, capturing it if we match. 520 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } 521 522 /// Match a ConstantInt, capturing the value if we match. 523 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } 524 525 /// Match a Constant, capturing the value if we match. 526 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } 527 528 /// Match a ConstantFP, capturing the value if we match. 529 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } 530 531 /// Match a specified Value*. 532 struct specificval_ty { 533 const Value *Val; 534 535 specificval_ty(const Value *V) : Val(V) {} 536 537 template <typename ITy> bool match(ITy *V) { return V == Val; } 538 }; 539 540 /// Match if we have a specific specified value. 541 inline specificval_ty m_Specific(const Value *V) { return V; } 542 543 /// Stores a reference to the Value *, not the Value * itself, 544 /// thus can be used in commutative matchers. 545 template <typename Class> struct deferredval_ty { 546 Class *const &Val; 547 548 deferredval_ty(Class *const &V) : Val(V) {} 549 550 template <typename ITy> bool match(ITy *const V) { return V == Val; } 551 }; 552 553 /// A commutative-friendly version of m_Specific(). 554 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; } 555 inline deferredval_ty<const Value> m_Deferred(const Value *const &V) { 556 return V; 557 } 558 559 /// Match a specified floating point value or vector of all elements of 560 /// that value. 561 struct specific_fpval { 562 double Val; 563 564 specific_fpval(double V) : Val(V) {} 565 566 template <typename ITy> bool match(ITy *V) { 567 if (const auto *CFP = dyn_cast<ConstantFP>(V)) 568 return CFP->isExactlyValue(Val); 569 if (V->getType()->isVectorTy()) 570 if (const auto *C = dyn_cast<Constant>(V)) 571 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) 572 return CFP->isExactlyValue(Val); 573 return false; 574 } 575 }; 576 577 /// Match a specific floating point value or vector with all elements 578 /// equal to the value. 579 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } 580 581 /// Match a float 1.0 or vector with all elements equal to 1.0. 582 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } 583 584 struct bind_const_intval_ty { 585 uint64_t &VR; 586 587 bind_const_intval_ty(uint64_t &V) : VR(V) {} 588 589 template <typename ITy> bool match(ITy *V) { 590 if (const auto *CV = dyn_cast<ConstantInt>(V)) 591 if (CV->getValue().ule(UINT64_MAX)) { 592 VR = CV->getZExtValue(); 593 return true; 594 } 595 return false; 596 } 597 }; 598 599 /// Match a specified integer value or vector of all elements of that 600 // value. 601 struct specific_intval { 602 uint64_t Val; 603 604 specific_intval(uint64_t V) : Val(V) {} 605 606 template <typename ITy> bool match(ITy *V) { 607 const auto *CI = dyn_cast<ConstantInt>(V); 608 if (!CI && V->getType()->isVectorTy()) 609 if (const auto *C = dyn_cast<Constant>(V)) 610 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()); 611 612 return CI && CI->getValue() == Val; 613 } 614 }; 615 616 /// Match a specific integer value or vector with all elements equal to 617 /// the value. 618 inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); } 619 620 /// Match a ConstantInt and bind to its value. This does not match 621 /// ConstantInts wider than 64-bits. 622 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } 623 624 //===----------------------------------------------------------------------===// 625 // Matcher for any binary operator. 626 // 627 template <typename LHS_t, typename RHS_t, bool Commutable = false> 628 struct AnyBinaryOp_match { 629 LHS_t L; 630 RHS_t R; 631 632 // The evaluation order is always stable, regardless of Commutability. 633 // The LHS is always matched first. 634 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 635 636 template <typename OpTy> bool match(OpTy *V) { 637 if (auto *I = dyn_cast<BinaryOperator>(V)) 638 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || 639 (Commutable && L.match(I->getOperand(1)) && 640 R.match(I->getOperand(0))); 641 return false; 642 } 643 }; 644 645 template <typename LHS, typename RHS> 646 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) { 647 return AnyBinaryOp_match<LHS, RHS>(L, R); 648 } 649 650 //===----------------------------------------------------------------------===// 651 // Matchers for specific binary operators. 652 // 653 654 template <typename LHS_t, typename RHS_t, unsigned Opcode, 655 bool Commutable = false> 656 struct BinaryOp_match { 657 LHS_t L; 658 RHS_t R; 659 660 // The evaluation order is always stable, regardless of Commutability. 661 // The LHS is always matched first. 662 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 663 664 template <typename OpTy> bool match(OpTy *V) { 665 if (V->getValueID() == Value::InstructionVal + Opcode) { 666 auto *I = cast<BinaryOperator>(V); 667 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || 668 (Commutable && L.match(I->getOperand(1)) && 669 R.match(I->getOperand(0))); 670 } 671 if (auto *CE = dyn_cast<ConstantExpr>(V)) 672 return CE->getOpcode() == Opcode && 673 ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) || 674 (Commutable && L.match(CE->getOperand(1)) && 675 R.match(CE->getOperand(0)))); 676 return false; 677 } 678 }; 679 680 template <typename LHS, typename RHS> 681 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L, 682 const RHS &R) { 683 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); 684 } 685 686 template <typename LHS, typename RHS> 687 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L, 688 const RHS &R) { 689 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); 690 } 691 692 template <typename LHS, typename RHS> 693 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L, 694 const RHS &R) { 695 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); 696 } 697 698 template <typename LHS, typename RHS> 699 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L, 700 const RHS &R) { 701 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); 702 } 703 704 template <typename Op_t> struct FNeg_match { 705 Op_t X; 706 707 FNeg_match(const Op_t &Op) : X(Op) {} 708 template <typename OpTy> bool match(OpTy *V) { 709 auto *FPMO = dyn_cast<FPMathOperator>(V); 710 if (!FPMO) return false; 711 712 if (FPMO->getOpcode() == Instruction::FNeg) 713 return X.match(FPMO->getOperand(0)); 714 715 if (FPMO->getOpcode() == Instruction::FSub) { 716 if (FPMO->hasNoSignedZeros()) { 717 // With 'nsz', any zero goes. 718 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0))) 719 return false; 720 } else { 721 // Without 'nsz', we need fsub -0.0, X exactly. 722 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0))) 723 return false; 724 } 725 726 return X.match(FPMO->getOperand(1)); 727 } 728 729 return false; 730 } 731 }; 732 733 /// Match 'fneg X' as 'fsub -0.0, X'. 734 template <typename OpTy> 735 inline FNeg_match<OpTy> 736 m_FNeg(const OpTy &X) { 737 return FNeg_match<OpTy>(X); 738 } 739 740 /// Match 'fneg X' as 'fsub +-0.0, X'. 741 template <typename RHS> 742 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub> 743 m_FNegNSZ(const RHS &X) { 744 return m_FSub(m_AnyZeroFP(), X); 745 } 746 747 template <typename LHS, typename RHS> 748 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L, 749 const RHS &R) { 750 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); 751 } 752 753 template <typename LHS, typename RHS> 754 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L, 755 const RHS &R) { 756 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); 757 } 758 759 template <typename LHS, typename RHS> 760 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L, 761 const RHS &R) { 762 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); 763 } 764 765 template <typename LHS, typename RHS> 766 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L, 767 const RHS &R) { 768 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); 769 } 770 771 template <typename LHS, typename RHS> 772 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L, 773 const RHS &R) { 774 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); 775 } 776 777 template <typename LHS, typename RHS> 778 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L, 779 const RHS &R) { 780 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); 781 } 782 783 template <typename LHS, typename RHS> 784 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L, 785 const RHS &R) { 786 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); 787 } 788 789 template <typename LHS, typename RHS> 790 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L, 791 const RHS &R) { 792 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); 793 } 794 795 template <typename LHS, typename RHS> 796 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L, 797 const RHS &R) { 798 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); 799 } 800 801 template <typename LHS, typename RHS> 802 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L, 803 const RHS &R) { 804 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); 805 } 806 807 template <typename LHS, typename RHS> 808 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L, 809 const RHS &R) { 810 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); 811 } 812 813 template <typename LHS, typename RHS> 814 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, 815 const RHS &R) { 816 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); 817 } 818 819 template <typename LHS, typename RHS> 820 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, 821 const RHS &R) { 822 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); 823 } 824 825 template <typename LHS, typename RHS> 826 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, 827 const RHS &R) { 828 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); 829 } 830 831 template <typename LHS_t, typename RHS_t, unsigned Opcode, 832 unsigned WrapFlags = 0> 833 struct OverflowingBinaryOp_match { 834 LHS_t L; 835 RHS_t R; 836 837 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) 838 : L(LHS), R(RHS) {} 839 840 template <typename OpTy> bool match(OpTy *V) { 841 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) { 842 if (Op->getOpcode() != Opcode) 843 return false; 844 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap && 845 !Op->hasNoUnsignedWrap()) 846 return false; 847 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap && 848 !Op->hasNoSignedWrap()) 849 return false; 850 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1)); 851 } 852 return false; 853 } 854 }; 855 856 template <typename LHS, typename RHS> 857 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 858 OverflowingBinaryOperator::NoSignedWrap> 859 m_NSWAdd(const LHS &L, const RHS &R) { 860 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 861 OverflowingBinaryOperator::NoSignedWrap>( 862 L, R); 863 } 864 template <typename LHS, typename RHS> 865 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 866 OverflowingBinaryOperator::NoSignedWrap> 867 m_NSWSub(const LHS &L, const RHS &R) { 868 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 869 OverflowingBinaryOperator::NoSignedWrap>( 870 L, R); 871 } 872 template <typename LHS, typename RHS> 873 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 874 OverflowingBinaryOperator::NoSignedWrap> 875 m_NSWMul(const LHS &L, const RHS &R) { 876 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 877 OverflowingBinaryOperator::NoSignedWrap>( 878 L, R); 879 } 880 template <typename LHS, typename RHS> 881 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 882 OverflowingBinaryOperator::NoSignedWrap> 883 m_NSWShl(const LHS &L, const RHS &R) { 884 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 885 OverflowingBinaryOperator::NoSignedWrap>( 886 L, R); 887 } 888 889 template <typename LHS, typename RHS> 890 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 891 OverflowingBinaryOperator::NoUnsignedWrap> 892 m_NUWAdd(const LHS &L, const RHS &R) { 893 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 894 OverflowingBinaryOperator::NoUnsignedWrap>( 895 L, R); 896 } 897 template <typename LHS, typename RHS> 898 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 899 OverflowingBinaryOperator::NoUnsignedWrap> 900 m_NUWSub(const LHS &L, const RHS &R) { 901 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 902 OverflowingBinaryOperator::NoUnsignedWrap>( 903 L, R); 904 } 905 template <typename LHS, typename RHS> 906 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 907 OverflowingBinaryOperator::NoUnsignedWrap> 908 m_NUWMul(const LHS &L, const RHS &R) { 909 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 910 OverflowingBinaryOperator::NoUnsignedWrap>( 911 L, R); 912 } 913 template <typename LHS, typename RHS> 914 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 915 OverflowingBinaryOperator::NoUnsignedWrap> 916 m_NUWShl(const LHS &L, const RHS &R) { 917 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 918 OverflowingBinaryOperator::NoUnsignedWrap>( 919 L, R); 920 } 921 922 //===----------------------------------------------------------------------===// 923 // Class that matches a group of binary opcodes. 924 // 925 template <typename LHS_t, typename RHS_t, typename Predicate> 926 struct BinOpPred_match : Predicate { 927 LHS_t L; 928 RHS_t R; 929 930 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 931 932 template <typename OpTy> bool match(OpTy *V) { 933 if (auto *I = dyn_cast<Instruction>(V)) 934 return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) && 935 R.match(I->getOperand(1)); 936 if (auto *CE = dyn_cast<ConstantExpr>(V)) 937 return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) && 938 R.match(CE->getOperand(1)); 939 return false; 940 } 941 }; 942 943 struct is_shift_op { 944 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); } 945 }; 946 947 struct is_right_shift_op { 948 bool isOpType(unsigned Opcode) { 949 return Opcode == Instruction::LShr || Opcode == Instruction::AShr; 950 } 951 }; 952 953 struct is_logical_shift_op { 954 bool isOpType(unsigned Opcode) { 955 return Opcode == Instruction::LShr || Opcode == Instruction::Shl; 956 } 957 }; 958 959 struct is_bitwiselogic_op { 960 bool isOpType(unsigned Opcode) { 961 return Instruction::isBitwiseLogicOp(Opcode); 962 } 963 }; 964 965 struct is_idiv_op { 966 bool isOpType(unsigned Opcode) { 967 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv; 968 } 969 }; 970 971 /// Matches shift operations. 972 template <typename LHS, typename RHS> 973 inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L, 974 const RHS &R) { 975 return BinOpPred_match<LHS, RHS, is_shift_op>(L, R); 976 } 977 978 /// Matches logical shift operations. 979 template <typename LHS, typename RHS> 980 inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L, 981 const RHS &R) { 982 return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R); 983 } 984 985 /// Matches logical shift operations. 986 template <typename LHS, typename RHS> 987 inline BinOpPred_match<LHS, RHS, is_logical_shift_op> 988 m_LogicalShift(const LHS &L, const RHS &R) { 989 return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R); 990 } 991 992 /// Matches bitwise logic operations. 993 template <typename LHS, typename RHS> 994 inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op> 995 m_BitwiseLogic(const LHS &L, const RHS &R) { 996 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R); 997 } 998 999 /// Matches integer division operations. 1000 template <typename LHS, typename RHS> 1001 inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L, 1002 const RHS &R) { 1003 return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R); 1004 } 1005 1006 //===----------------------------------------------------------------------===// 1007 // Class that matches exact binary ops. 1008 // 1009 template <typename SubPattern_t> struct Exact_match { 1010 SubPattern_t SubPattern; 1011 1012 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} 1013 1014 template <typename OpTy> bool match(OpTy *V) { 1015 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V)) 1016 return PEO->isExact() && SubPattern.match(V); 1017 return false; 1018 } 1019 }; 1020 1021 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) { 1022 return SubPattern; 1023 } 1024 1025 //===----------------------------------------------------------------------===// 1026 // Matchers for CmpInst classes 1027 // 1028 1029 template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy, 1030 bool Commutable = false> 1031 struct CmpClass_match { 1032 PredicateTy &Predicate; 1033 LHS_t L; 1034 RHS_t R; 1035 1036 // The evaluation order is always stable, regardless of Commutability. 1037 // The LHS is always matched first. 1038 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS) 1039 : Predicate(Pred), L(LHS), R(RHS) {} 1040 1041 template <typename OpTy> bool match(OpTy *V) { 1042 if (auto *I = dyn_cast<Class>(V)) 1043 if ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || 1044 (Commutable && L.match(I->getOperand(1)) && 1045 R.match(I->getOperand(0)))) { 1046 Predicate = I->getPredicate(); 1047 return true; 1048 } 1049 return false; 1050 } 1051 }; 1052 1053 template <typename LHS, typename RHS> 1054 inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate> 1055 m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 1056 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R); 1057 } 1058 1059 template <typename LHS, typename RHS> 1060 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate> 1061 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 1062 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R); 1063 } 1064 1065 template <typename LHS, typename RHS> 1066 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate> 1067 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 1068 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R); 1069 } 1070 1071 //===----------------------------------------------------------------------===// 1072 // Matchers for instructions with a given opcode and number of operands. 1073 // 1074 1075 /// Matches instructions with Opcode and three operands. 1076 template <typename T0, unsigned Opcode> struct OneOps_match { 1077 T0 Op1; 1078 1079 OneOps_match(const T0 &Op1) : Op1(Op1) {} 1080 1081 template <typename OpTy> bool match(OpTy *V) { 1082 if (V->getValueID() == Value::InstructionVal + Opcode) { 1083 auto *I = cast<Instruction>(V); 1084 return Op1.match(I->getOperand(0)); 1085 } 1086 return false; 1087 } 1088 }; 1089 1090 /// Matches instructions with Opcode and three operands. 1091 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match { 1092 T0 Op1; 1093 T1 Op2; 1094 1095 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {} 1096 1097 template <typename OpTy> bool match(OpTy *V) { 1098 if (V->getValueID() == Value::InstructionVal + Opcode) { 1099 auto *I = cast<Instruction>(V); 1100 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)); 1101 } 1102 return false; 1103 } 1104 }; 1105 1106 /// Matches instructions with Opcode and three operands. 1107 template <typename T0, typename T1, typename T2, unsigned Opcode> 1108 struct ThreeOps_match { 1109 T0 Op1; 1110 T1 Op2; 1111 T2 Op3; 1112 1113 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3) 1114 : Op1(Op1), Op2(Op2), Op3(Op3) {} 1115 1116 template <typename OpTy> bool match(OpTy *V) { 1117 if (V->getValueID() == Value::InstructionVal + Opcode) { 1118 auto *I = cast<Instruction>(V); 1119 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && 1120 Op3.match(I->getOperand(2)); 1121 } 1122 return false; 1123 } 1124 }; 1125 1126 /// Matches SelectInst. 1127 template <typename Cond, typename LHS, typename RHS> 1128 inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select> 1129 m_Select(const Cond &C, const LHS &L, const RHS &R) { 1130 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R); 1131 } 1132 1133 /// This matches a select of two constants, e.g.: 1134 /// m_SelectCst<-1, 0>(m_Value(V)) 1135 template <int64_t L, int64_t R, typename Cond> 1136 inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>, 1137 Instruction::Select> 1138 m_SelectCst(const Cond &C) { 1139 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); 1140 } 1141 1142 /// Matches InsertElementInst. 1143 template <typename Val_t, typename Elt_t, typename Idx_t> 1144 inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement> 1145 m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) { 1146 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>( 1147 Val, Elt, Idx); 1148 } 1149 1150 /// Matches ExtractElementInst. 1151 template <typename Val_t, typename Idx_t> 1152 inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement> 1153 m_ExtractElement(const Val_t &Val, const Idx_t &Idx) { 1154 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx); 1155 } 1156 1157 /// Matches ShuffleVectorInst. 1158 template <typename V1_t, typename V2_t, typename Mask_t> 1159 inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector> 1160 m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) { 1161 return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2, 1162 m); 1163 } 1164 1165 /// Matches LoadInst. 1166 template <typename OpTy> 1167 inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) { 1168 return OneOps_match<OpTy, Instruction::Load>(Op); 1169 } 1170 1171 /// Matches StoreInst. 1172 template <typename ValueOpTy, typename PointerOpTy> 1173 inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store> 1174 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) { 1175 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp, 1176 PointerOp); 1177 } 1178 1179 //===----------------------------------------------------------------------===// 1180 // Matchers for CastInst classes 1181 // 1182 1183 template <typename Op_t, unsigned Opcode> struct CastClass_match { 1184 Op_t Op; 1185 1186 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} 1187 1188 template <typename OpTy> bool match(OpTy *V) { 1189 if (auto *O = dyn_cast<Operator>(V)) 1190 return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); 1191 return false; 1192 } 1193 }; 1194 1195 /// Matches BitCast. 1196 template <typename OpTy> 1197 inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) { 1198 return CastClass_match<OpTy, Instruction::BitCast>(Op); 1199 } 1200 1201 /// Matches PtrToInt. 1202 template <typename OpTy> 1203 inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) { 1204 return CastClass_match<OpTy, Instruction::PtrToInt>(Op); 1205 } 1206 1207 /// Matches Trunc. 1208 template <typename OpTy> 1209 inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) { 1210 return CastClass_match<OpTy, Instruction::Trunc>(Op); 1211 } 1212 1213 /// Matches SExt. 1214 template <typename OpTy> 1215 inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) { 1216 return CastClass_match<OpTy, Instruction::SExt>(Op); 1217 } 1218 1219 /// Matches ZExt. 1220 template <typename OpTy> 1221 inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) { 1222 return CastClass_match<OpTy, Instruction::ZExt>(Op); 1223 } 1224 1225 template <typename OpTy> 1226 inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, 1227 CastClass_match<OpTy, Instruction::SExt>> 1228 m_ZExtOrSExt(const OpTy &Op) { 1229 return m_CombineOr(m_ZExt(Op), m_SExt(Op)); 1230 } 1231 1232 /// Matches UIToFP. 1233 template <typename OpTy> 1234 inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) { 1235 return CastClass_match<OpTy, Instruction::UIToFP>(Op); 1236 } 1237 1238 /// Matches SIToFP. 1239 template <typename OpTy> 1240 inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) { 1241 return CastClass_match<OpTy, Instruction::SIToFP>(Op); 1242 } 1243 1244 /// Matches FPTrunc 1245 template <typename OpTy> 1246 inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) { 1247 return CastClass_match<OpTy, Instruction::FPTrunc>(Op); 1248 } 1249 1250 /// Matches FPExt 1251 template <typename OpTy> 1252 inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) { 1253 return CastClass_match<OpTy, Instruction::FPExt>(Op); 1254 } 1255 1256 //===----------------------------------------------------------------------===// 1257 // Matchers for control flow. 1258 // 1259 1260 struct br_match { 1261 BasicBlock *&Succ; 1262 1263 br_match(BasicBlock *&Succ) : Succ(Succ) {} 1264 1265 template <typename OpTy> bool match(OpTy *V) { 1266 if (auto *BI = dyn_cast<BranchInst>(V)) 1267 if (BI->isUnconditional()) { 1268 Succ = BI->getSuccessor(0); 1269 return true; 1270 } 1271 return false; 1272 } 1273 }; 1274 1275 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } 1276 1277 template <typename Cond_t> struct brc_match { 1278 Cond_t Cond; 1279 BasicBlock *&T, *&F; 1280 1281 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f) 1282 : Cond(C), T(t), F(f) {} 1283 1284 template <typename OpTy> bool match(OpTy *V) { 1285 if (auto *BI = dyn_cast<BranchInst>(V)) 1286 if (BI->isConditional() && Cond.match(BI->getCondition())) { 1287 T = BI->getSuccessor(0); 1288 F = BI->getSuccessor(1); 1289 return true; 1290 } 1291 return false; 1292 } 1293 }; 1294 1295 template <typename Cond_t> 1296 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { 1297 return brc_match<Cond_t>(C, T, F); 1298 } 1299 1300 //===----------------------------------------------------------------------===// 1301 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). 1302 // 1303 1304 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, 1305 bool Commutable = false> 1306 struct MaxMin_match { 1307 LHS_t L; 1308 RHS_t R; 1309 1310 // The evaluation order is always stable, regardless of Commutability. 1311 // The LHS is always matched first. 1312 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 1313 1314 template <typename OpTy> bool match(OpTy *V) { 1315 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". 1316 auto *SI = dyn_cast<SelectInst>(V); 1317 if (!SI) 1318 return false; 1319 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); 1320 if (!Cmp) 1321 return false; 1322 // At this point we have a select conditioned on a comparison. Check that 1323 // it is the values returned by the select that are being compared. 1324 Value *TrueVal = SI->getTrueValue(); 1325 Value *FalseVal = SI->getFalseValue(); 1326 Value *LHS = Cmp->getOperand(0); 1327 Value *RHS = Cmp->getOperand(1); 1328 if ((TrueVal != LHS || FalseVal != RHS) && 1329 (TrueVal != RHS || FalseVal != LHS)) 1330 return false; 1331 typename CmpInst_t::Predicate Pred = 1332 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); 1333 // Does "(x pred y) ? x : y" represent the desired max/min operation? 1334 if (!Pred_t::match(Pred)) 1335 return false; 1336 // It does! Bind the operands. 1337 return (L.match(LHS) && R.match(RHS)) || 1338 (Commutable && L.match(RHS) && R.match(LHS)); 1339 } 1340 }; 1341 1342 /// Helper class for identifying signed max predicates. 1343 struct smax_pred_ty { 1344 static bool match(ICmpInst::Predicate Pred) { 1345 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; 1346 } 1347 }; 1348 1349 /// Helper class for identifying signed min predicates. 1350 struct smin_pred_ty { 1351 static bool match(ICmpInst::Predicate Pred) { 1352 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; 1353 } 1354 }; 1355 1356 /// Helper class for identifying unsigned max predicates. 1357 struct umax_pred_ty { 1358 static bool match(ICmpInst::Predicate Pred) { 1359 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; 1360 } 1361 }; 1362 1363 /// Helper class for identifying unsigned min predicates. 1364 struct umin_pred_ty { 1365 static bool match(ICmpInst::Predicate Pred) { 1366 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; 1367 } 1368 }; 1369 1370 /// Helper class for identifying ordered max predicates. 1371 struct ofmax_pred_ty { 1372 static bool match(FCmpInst::Predicate Pred) { 1373 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; 1374 } 1375 }; 1376 1377 /// Helper class for identifying ordered min predicates. 1378 struct ofmin_pred_ty { 1379 static bool match(FCmpInst::Predicate Pred) { 1380 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; 1381 } 1382 }; 1383 1384 /// Helper class for identifying unordered max predicates. 1385 struct ufmax_pred_ty { 1386 static bool match(FCmpInst::Predicate Pred) { 1387 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; 1388 } 1389 }; 1390 1391 /// Helper class for identifying unordered min predicates. 1392 struct ufmin_pred_ty { 1393 static bool match(FCmpInst::Predicate Pred) { 1394 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; 1395 } 1396 }; 1397 1398 template <typename LHS, typename RHS> 1399 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, 1400 const RHS &R) { 1401 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); 1402 } 1403 1404 template <typename LHS, typename RHS> 1405 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, 1406 const RHS &R) { 1407 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); 1408 } 1409 1410 template <typename LHS, typename RHS> 1411 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, 1412 const RHS &R) { 1413 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); 1414 } 1415 1416 template <typename LHS, typename RHS> 1417 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, 1418 const RHS &R) { 1419 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); 1420 } 1421 1422 /// Match an 'ordered' floating point maximum function. 1423 /// Floating point has one special value 'NaN'. Therefore, there is no total 1424 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1425 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 1426 /// semantics. In the presence of 'NaN' we have to preserve the original 1427 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. 1428 /// 1429 /// max(L, R) iff L and R are not NaN 1430 /// m_OrdFMax(L, R) = R iff L or R are NaN 1431 template <typename LHS, typename RHS> 1432 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, 1433 const RHS &R) { 1434 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); 1435 } 1436 1437 /// Match an 'ordered' floating point minimum function. 1438 /// Floating point has one special value 'NaN'. Therefore, there is no total 1439 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1440 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 1441 /// semantics. In the presence of 'NaN' we have to preserve the original 1442 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. 1443 /// 1444 /// min(L, R) iff L and R are not NaN 1445 /// m_OrdFMin(L, R) = R iff L or R are NaN 1446 template <typename LHS, typename RHS> 1447 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, 1448 const RHS &R) { 1449 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); 1450 } 1451 1452 /// Match an 'unordered' floating point maximum function. 1453 /// Floating point has one special value 'NaN'. Therefore, there is no total 1454 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1455 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 1456 /// semantics. In the presence of 'NaN' we have to preserve the original 1457 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. 1458 /// 1459 /// max(L, R) iff L and R are not NaN 1460 /// m_UnordFMax(L, R) = L iff L or R are NaN 1461 template <typename LHS, typename RHS> 1462 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> 1463 m_UnordFMax(const LHS &L, const RHS &R) { 1464 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); 1465 } 1466 1467 /// Match an 'unordered' floating point minimum function. 1468 /// Floating point has one special value 'NaN'. Therefore, there is no total 1469 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1470 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 1471 /// semantics. In the presence of 'NaN' we have to preserve the original 1472 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. 1473 /// 1474 /// min(L, R) iff L and R are not NaN 1475 /// m_UnordFMin(L, R) = L iff L or R are NaN 1476 template <typename LHS, typename RHS> 1477 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> 1478 m_UnordFMin(const LHS &L, const RHS &R) { 1479 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); 1480 } 1481 1482 //===----------------------------------------------------------------------===// 1483 // Matchers for overflow check patterns: e.g. (a + b) u< a 1484 // 1485 1486 template <typename LHS_t, typename RHS_t, typename Sum_t> 1487 struct UAddWithOverflow_match { 1488 LHS_t L; 1489 RHS_t R; 1490 Sum_t S; 1491 1492 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) 1493 : L(L), R(R), S(S) {} 1494 1495 template <typename OpTy> bool match(OpTy *V) { 1496 Value *ICmpLHS, *ICmpRHS; 1497 ICmpInst::Predicate Pred; 1498 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) 1499 return false; 1500 1501 Value *AddLHS, *AddRHS; 1502 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); 1503 1504 // (a + b) u< a, (a + b) u< b 1505 if (Pred == ICmpInst::ICMP_ULT) 1506 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) 1507 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); 1508 1509 // a >u (a + b), b >u (a + b) 1510 if (Pred == ICmpInst::ICMP_UGT) 1511 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) 1512 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); 1513 1514 // Match special-case for increment-by-1. 1515 if (Pred == ICmpInst::ICMP_EQ) { 1516 // (a + 1) == 0 1517 // (1 + a) == 0 1518 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && 1519 (m_One().match(AddLHS) || m_One().match(AddRHS))) 1520 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); 1521 // 0 == (a + 1) 1522 // 0 == (1 + a) 1523 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && 1524 (m_One().match(AddLHS) || m_One().match(AddRHS))) 1525 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); 1526 } 1527 1528 return false; 1529 } 1530 }; 1531 1532 /// Match an icmp instruction checking for unsigned overflow on addition. 1533 /// 1534 /// S is matched to the addition whose result is being checked for overflow, and 1535 /// L and R are matched to the LHS and RHS of S. 1536 template <typename LHS_t, typename RHS_t, typename Sum_t> 1537 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> 1538 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { 1539 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); 1540 } 1541 1542 template <typename Opnd_t> struct Argument_match { 1543 unsigned OpI; 1544 Opnd_t Val; 1545 1546 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} 1547 1548 template <typename OpTy> bool match(OpTy *V) { 1549 // FIXME: Should likely be switched to use `CallBase`. 1550 if (const auto *CI = dyn_cast<CallInst>(V)) 1551 return Val.match(CI->getArgOperand(OpI)); 1552 return false; 1553 } 1554 }; 1555 1556 /// Match an argument. 1557 template <unsigned OpI, typename Opnd_t> 1558 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { 1559 return Argument_match<Opnd_t>(OpI, Op); 1560 } 1561 1562 /// Intrinsic matchers. 1563 struct IntrinsicID_match { 1564 unsigned ID; 1565 1566 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} 1567 1568 template <typename OpTy> bool match(OpTy *V) { 1569 if (const auto *CI = dyn_cast<CallInst>(V)) 1570 if (const auto *F = CI->getCalledFunction()) 1571 return F->getIntrinsicID() == ID; 1572 return false; 1573 } 1574 }; 1575 1576 /// Intrinsic matches are combinations of ID matchers, and argument 1577 /// matchers. Higher arity matcher are defined recursively in terms of and-ing 1578 /// them with lower arity matchers. Here's some convenient typedefs for up to 1579 /// several arguments, and more can be added as needed 1580 template <typename T0 = void, typename T1 = void, typename T2 = void, 1581 typename T3 = void, typename T4 = void, typename T5 = void, 1582 typename T6 = void, typename T7 = void, typename T8 = void, 1583 typename T9 = void, typename T10 = void> 1584 struct m_Intrinsic_Ty; 1585 template <typename T0> struct m_Intrinsic_Ty<T0> { 1586 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; 1587 }; 1588 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { 1589 using Ty = 1590 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; 1591 }; 1592 template <typename T0, typename T1, typename T2> 1593 struct m_Intrinsic_Ty<T0, T1, T2> { 1594 using Ty = 1595 match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, 1596 Argument_match<T2>>; 1597 }; 1598 template <typename T0, typename T1, typename T2, typename T3> 1599 struct m_Intrinsic_Ty<T0, T1, T2, T3> { 1600 using Ty = 1601 match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, 1602 Argument_match<T3>>; 1603 }; 1604 1605 /// Match intrinsic calls like this: 1606 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) 1607 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { 1608 return IntrinsicID_match(IntrID); 1609 } 1610 1611 template <Intrinsic::ID IntrID, typename T0> 1612 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { 1613 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); 1614 } 1615 1616 template <Intrinsic::ID IntrID, typename T0, typename T1> 1617 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, 1618 const T1 &Op1) { 1619 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); 1620 } 1621 1622 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> 1623 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty 1624 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { 1625 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); 1626 } 1627 1628 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 1629 typename T3> 1630 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty 1631 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { 1632 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); 1633 } 1634 1635 // Helper intrinsic matching specializations. 1636 template <typename Opnd0> 1637 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { 1638 return m_Intrinsic<Intrinsic::bitreverse>(Op0); 1639 } 1640 1641 template <typename Opnd0> 1642 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { 1643 return m_Intrinsic<Intrinsic::bswap>(Op0); 1644 } 1645 1646 template <typename Opnd0> 1647 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { 1648 return m_Intrinsic<Intrinsic::fabs>(Op0); 1649 } 1650 1651 template <typename Opnd0> 1652 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { 1653 return m_Intrinsic<Intrinsic::canonicalize>(Op0); 1654 } 1655 1656 template <typename Opnd0, typename Opnd1> 1657 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, 1658 const Opnd1 &Op1) { 1659 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); 1660 } 1661 1662 template <typename Opnd0, typename Opnd1> 1663 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, 1664 const Opnd1 &Op1) { 1665 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); 1666 } 1667 1668 //===----------------------------------------------------------------------===// 1669 // Matchers for two-operands operators with the operators in either order 1670 // 1671 1672 /// Matches a BinaryOperator with LHS and RHS in either order. 1673 template <typename LHS, typename RHS> 1674 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { 1675 return AnyBinaryOp_match<LHS, RHS, true>(L, R); 1676 } 1677 1678 /// Matches an ICmp with a predicate over LHS and RHS in either order. 1679 /// Does not swap the predicate. 1680 template <typename LHS, typename RHS> 1681 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true> 1682 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 1683 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L, 1684 R); 1685 } 1686 1687 /// Matches a Add with LHS and RHS in either order. 1688 template <typename LHS, typename RHS> 1689 inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, 1690 const RHS &R) { 1691 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); 1692 } 1693 1694 /// Matches a Mul with LHS and RHS in either order. 1695 template <typename LHS, typename RHS> 1696 inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, 1697 const RHS &R) { 1698 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); 1699 } 1700 1701 /// Matches an And with LHS and RHS in either order. 1702 template <typename LHS, typename RHS> 1703 inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, 1704 const RHS &R) { 1705 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); 1706 } 1707 1708 /// Matches an Or with LHS and RHS in either order. 1709 template <typename LHS, typename RHS> 1710 inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, 1711 const RHS &R) { 1712 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); 1713 } 1714 1715 /// Matches an Xor with LHS and RHS in either order. 1716 template <typename LHS, typename RHS> 1717 inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, 1718 const RHS &R) { 1719 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); 1720 } 1721 1722 /// Matches a 'Neg' as 'sub 0, V'. 1723 template <typename ValTy> 1724 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> 1725 m_Neg(const ValTy &V) { 1726 return m_Sub(m_ZeroInt(), V); 1727 } 1728 1729 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'. 1730 template <typename ValTy> 1731 inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true> 1732 m_Not(const ValTy &V) { 1733 return m_c_Xor(V, m_AllOnes()); 1734 } 1735 1736 /// Matches an SMin with LHS and RHS in either order. 1737 template <typename LHS, typename RHS> 1738 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> 1739 m_c_SMin(const LHS &L, const RHS &R) { 1740 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); 1741 } 1742 /// Matches an SMax with LHS and RHS in either order. 1743 template <typename LHS, typename RHS> 1744 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> 1745 m_c_SMax(const LHS &L, const RHS &R) { 1746 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); 1747 } 1748 /// Matches a UMin with LHS and RHS in either order. 1749 template <typename LHS, typename RHS> 1750 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> 1751 m_c_UMin(const LHS &L, const RHS &R) { 1752 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); 1753 } 1754 /// Matches a UMax with LHS and RHS in either order. 1755 template <typename LHS, typename RHS> 1756 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> 1757 m_c_UMax(const LHS &L, const RHS &R) { 1758 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); 1759 } 1760 1761 /// Matches FAdd with LHS and RHS in either order. 1762 template <typename LHS, typename RHS> 1763 inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> 1764 m_c_FAdd(const LHS &L, const RHS &R) { 1765 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); 1766 } 1767 1768 /// Matches FMul with LHS and RHS in either order. 1769 template <typename LHS, typename RHS> 1770 inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> 1771 m_c_FMul(const LHS &L, const RHS &R) { 1772 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); 1773 } 1774 1775 template <typename Opnd_t> struct Signum_match { 1776 Opnd_t Val; 1777 Signum_match(const Opnd_t &V) : Val(V) {} 1778 1779 template <typename OpTy> bool match(OpTy *V) { 1780 unsigned TypeSize = V->getType()->getScalarSizeInBits(); 1781 if (TypeSize == 0) 1782 return false; 1783 1784 unsigned ShiftWidth = TypeSize - 1; 1785 Value *OpL = nullptr, *OpR = nullptr; 1786 1787 // This is the representation of signum we match: 1788 // 1789 // signum(x) == (x >> 63) | (-x >>u 63) 1790 // 1791 // An i1 value is its own signum, so it's correct to match 1792 // 1793 // signum(x) == (x >> 0) | (-x >>u 0) 1794 // 1795 // for i1 values. 1796 1797 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth)); 1798 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth)); 1799 auto Signum = m_Or(LHS, RHS); 1800 1801 return Signum.match(V) && OpL == OpR && Val.match(OpL); 1802 } 1803 }; 1804 1805 /// Matches a signum pattern. 1806 /// 1807 /// signum(x) = 1808 /// x > 0 -> 1 1809 /// x == 0 -> 0 1810 /// x < 0 -> -1 1811 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { 1812 return Signum_match<Val_t>(V); 1813 } 1814 1815 } // end namespace PatternMatch 1816 } // end namespace llvm 1817 1818 #endif // LLVM_IR_PATTERNMATCH_H 1819