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/DataLayout.h" 36 #include "llvm/IR/InstrTypes.h" 37 #include "llvm/IR/Instruction.h" 38 #include "llvm/IR/Instructions.h" 39 #include "llvm/IR/IntrinsicInst.h" 40 #include "llvm/IR/Intrinsics.h" 41 #include "llvm/IR/Operator.h" 42 #include "llvm/IR/Value.h" 43 #include "llvm/Support/Casting.h" 44 #include <cstdint> 45 46 namespace llvm { 47 namespace PatternMatch { 48 49 template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) { 50 return const_cast<Pattern &>(P).match(V); 51 } 52 53 template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) { 54 return const_cast<Pattern &>(P).match(Mask); 55 } 56 57 template <typename SubPattern_t> struct OneUse_match { 58 SubPattern_t SubPattern; 59 60 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} 61 62 template <typename OpTy> bool match(OpTy *V) { 63 return V->hasOneUse() && SubPattern.match(V); 64 } 65 }; 66 67 template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) { 68 return SubPattern; 69 } 70 71 template <typename SubPattern_t> struct AllowReassoc_match { 72 SubPattern_t SubPattern; 73 74 AllowReassoc_match(const SubPattern_t &SP) : SubPattern(SP) {} 75 76 template <typename OpTy> bool match(OpTy *V) { 77 auto *I = dyn_cast<FPMathOperator>(V); 78 return I && I->hasAllowReassoc() && SubPattern.match(I); 79 } 80 }; 81 82 template <typename T> 83 inline AllowReassoc_match<T> m_AllowReassoc(const T &SubPattern) { 84 return SubPattern; 85 } 86 87 template <typename Class> struct class_match { 88 template <typename ITy> bool match(ITy *V) { return isa<Class>(V); } 89 }; 90 91 /// Match an arbitrary value and ignore it. 92 inline class_match<Value> m_Value() { return class_match<Value>(); } 93 94 /// Match an arbitrary unary operation and ignore it. 95 inline class_match<UnaryOperator> m_UnOp() { 96 return class_match<UnaryOperator>(); 97 } 98 99 /// Match an arbitrary binary operation and ignore it. 100 inline class_match<BinaryOperator> m_BinOp() { 101 return class_match<BinaryOperator>(); 102 } 103 104 /// Matches any compare instruction and ignore it. 105 inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); } 106 107 struct undef_match { 108 static bool check(const Value *V) { 109 if (isa<UndefValue>(V)) 110 return true; 111 112 const auto *CA = dyn_cast<ConstantAggregate>(V); 113 if (!CA) 114 return false; 115 116 SmallPtrSet<const ConstantAggregate *, 8> Seen; 117 SmallVector<const ConstantAggregate *, 8> Worklist; 118 119 // Either UndefValue, PoisonValue, or an aggregate that only contains 120 // these is accepted by matcher. 121 // CheckValue returns false if CA cannot satisfy this constraint. 122 auto CheckValue = [&](const ConstantAggregate *CA) { 123 for (const Value *Op : CA->operand_values()) { 124 if (isa<UndefValue>(Op)) 125 continue; 126 127 const auto *CA = dyn_cast<ConstantAggregate>(Op); 128 if (!CA) 129 return false; 130 if (Seen.insert(CA).second) 131 Worklist.emplace_back(CA); 132 } 133 134 return true; 135 }; 136 137 if (!CheckValue(CA)) 138 return false; 139 140 while (!Worklist.empty()) { 141 if (!CheckValue(Worklist.pop_back_val())) 142 return false; 143 } 144 return true; 145 } 146 template <typename ITy> bool match(ITy *V) { return check(V); } 147 }; 148 149 /// Match an arbitrary undef constant. This matches poison as well. 150 /// If this is an aggregate and contains a non-aggregate element that is 151 /// neither undef nor poison, the aggregate is not matched. 152 inline auto m_Undef() { return undef_match(); } 153 154 /// Match an arbitrary UndefValue constant. 155 inline class_match<UndefValue> m_UndefValue() { 156 return class_match<UndefValue>(); 157 } 158 159 /// Match an arbitrary poison constant. 160 inline class_match<PoisonValue> m_Poison() { 161 return class_match<PoisonValue>(); 162 } 163 164 /// Match an arbitrary Constant and ignore it. 165 inline class_match<Constant> m_Constant() { return class_match<Constant>(); } 166 167 /// Match an arbitrary ConstantInt and ignore it. 168 inline class_match<ConstantInt> m_ConstantInt() { 169 return class_match<ConstantInt>(); 170 } 171 172 /// Match an arbitrary ConstantFP and ignore it. 173 inline class_match<ConstantFP> m_ConstantFP() { 174 return class_match<ConstantFP>(); 175 } 176 177 struct constantexpr_match { 178 template <typename ITy> bool match(ITy *V) { 179 auto *C = dyn_cast<Constant>(V); 180 return C && (isa<ConstantExpr>(C) || C->containsConstantExpression()); 181 } 182 }; 183 184 /// Match a constant expression or a constant that contains a constant 185 /// expression. 186 inline constantexpr_match m_ConstantExpr() { return constantexpr_match(); } 187 188 /// Match an arbitrary basic block value and ignore it. 189 inline class_match<BasicBlock> m_BasicBlock() { 190 return class_match<BasicBlock>(); 191 } 192 193 /// Inverting matcher 194 template <typename Ty> struct match_unless { 195 Ty M; 196 197 match_unless(const Ty &Matcher) : M(Matcher) {} 198 199 template <typename ITy> bool match(ITy *V) { return !M.match(V); } 200 }; 201 202 /// Match if the inner matcher does *NOT* match. 203 template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) { 204 return match_unless<Ty>(M); 205 } 206 207 /// Matching combinators 208 template <typename LTy, typename RTy> struct match_combine_or { 209 LTy L; 210 RTy R; 211 212 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} 213 214 template <typename ITy> bool match(ITy *V) { 215 if (L.match(V)) 216 return true; 217 if (R.match(V)) 218 return true; 219 return false; 220 } 221 }; 222 223 template <typename LTy, typename RTy> struct match_combine_and { 224 LTy L; 225 RTy R; 226 227 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} 228 229 template <typename ITy> bool match(ITy *V) { 230 if (L.match(V)) 231 if (R.match(V)) 232 return true; 233 return false; 234 } 235 }; 236 237 /// Combine two pattern matchers matching L || R 238 template <typename LTy, typename RTy> 239 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { 240 return match_combine_or<LTy, RTy>(L, R); 241 } 242 243 /// Combine two pattern matchers matching L && R 244 template <typename LTy, typename RTy> 245 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { 246 return match_combine_and<LTy, RTy>(L, R); 247 } 248 249 struct apint_match { 250 const APInt *&Res; 251 bool AllowPoison; 252 253 apint_match(const APInt *&Res, bool AllowPoison) 254 : Res(Res), AllowPoison(AllowPoison) {} 255 256 template <typename ITy> bool match(ITy *V) { 257 if (auto *CI = dyn_cast<ConstantInt>(V)) { 258 Res = &CI->getValue(); 259 return true; 260 } 261 if (V->getType()->isVectorTy()) 262 if (const auto *C = dyn_cast<Constant>(V)) 263 if (auto *CI = 264 dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison))) { 265 Res = &CI->getValue(); 266 return true; 267 } 268 return false; 269 } 270 }; 271 // Either constexpr if or renaming ConstantFP::getValueAPF to 272 // ConstantFP::getValue is needed to do it via single template 273 // function for both apint/apfloat. 274 struct apfloat_match { 275 const APFloat *&Res; 276 bool AllowPoison; 277 278 apfloat_match(const APFloat *&Res, bool AllowPoison) 279 : Res(Res), AllowPoison(AllowPoison) {} 280 281 template <typename ITy> bool match(ITy *V) { 282 if (auto *CI = dyn_cast<ConstantFP>(V)) { 283 Res = &CI->getValueAPF(); 284 return true; 285 } 286 if (V->getType()->isVectorTy()) 287 if (const auto *C = dyn_cast<Constant>(V)) 288 if (auto *CI = 289 dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowPoison))) { 290 Res = &CI->getValueAPF(); 291 return true; 292 } 293 return false; 294 } 295 }; 296 297 /// Match a ConstantInt or splatted ConstantVector, binding the 298 /// specified pointer to the contained APInt. 299 inline apint_match m_APInt(const APInt *&Res) { 300 // Forbid poison by default to maintain previous behavior. 301 return apint_match(Res, /* AllowPoison */ false); 302 } 303 304 /// Match APInt while allowing poison in splat vector constants. 305 inline apint_match m_APIntAllowPoison(const APInt *&Res) { 306 return apint_match(Res, /* AllowPoison */ true); 307 } 308 309 /// Match APInt while forbidding poison in splat vector constants. 310 inline apint_match m_APIntForbidPoison(const APInt *&Res) { 311 return apint_match(Res, /* AllowPoison */ false); 312 } 313 314 /// Match a ConstantFP or splatted ConstantVector, binding the 315 /// specified pointer to the contained APFloat. 316 inline apfloat_match m_APFloat(const APFloat *&Res) { 317 // Forbid undefs by default to maintain previous behavior. 318 return apfloat_match(Res, /* AllowPoison */ false); 319 } 320 321 /// Match APFloat while allowing poison in splat vector constants. 322 inline apfloat_match m_APFloatAllowPoison(const APFloat *&Res) { 323 return apfloat_match(Res, /* AllowPoison */ true); 324 } 325 326 /// Match APFloat while forbidding poison in splat vector constants. 327 inline apfloat_match m_APFloatForbidPoison(const APFloat *&Res) { 328 return apfloat_match(Res, /* AllowPoison */ false); 329 } 330 331 template <int64_t Val> struct constantint_match { 332 template <typename ITy> bool match(ITy *V) { 333 if (const auto *CI = dyn_cast<ConstantInt>(V)) { 334 const APInt &CIV = CI->getValue(); 335 if (Val >= 0) 336 return CIV == static_cast<uint64_t>(Val); 337 // If Val is negative, and CI is shorter than it, truncate to the right 338 // number of bits. If it is larger, then we have to sign extend. Just 339 // compare their negated values. 340 return -CIV == -Val; 341 } 342 return false; 343 } 344 }; 345 346 /// Match a ConstantInt with a specific value. 347 template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { 348 return constantint_match<Val>(); 349 } 350 351 /// This helper class is used to match constant scalars, vector splats, 352 /// and fixed width vectors that satisfy a specified predicate. 353 /// For fixed width vector constants, poison elements are ignored if AllowPoison 354 /// is true. 355 template <typename Predicate, typename ConstantVal, bool AllowPoison> 356 struct cstval_pred_ty : public Predicate { 357 const Constant **Res = nullptr; 358 template <typename ITy> bool match_impl(ITy *V) { 359 if (const auto *CV = dyn_cast<ConstantVal>(V)) 360 return this->isValue(CV->getValue()); 361 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) { 362 if (const auto *C = dyn_cast<Constant>(V)) { 363 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue())) 364 return this->isValue(CV->getValue()); 365 366 // Number of elements of a scalable vector unknown at compile time 367 auto *FVTy = dyn_cast<FixedVectorType>(VTy); 368 if (!FVTy) 369 return false; 370 371 // Non-splat vector constant: check each element for a match. 372 unsigned NumElts = FVTy->getNumElements(); 373 assert(NumElts != 0 && "Constant vector with no elements?"); 374 bool HasNonPoisonElements = false; 375 for (unsigned i = 0; i != NumElts; ++i) { 376 Constant *Elt = C->getAggregateElement(i); 377 if (!Elt) 378 return false; 379 if (AllowPoison && isa<PoisonValue>(Elt)) 380 continue; 381 auto *CV = dyn_cast<ConstantVal>(Elt); 382 if (!CV || !this->isValue(CV->getValue())) 383 return false; 384 HasNonPoisonElements = true; 385 } 386 return HasNonPoisonElements; 387 } 388 } 389 return false; 390 } 391 392 template <typename ITy> bool match(ITy *V) { 393 if (this->match_impl(V)) { 394 if (Res) 395 *Res = cast<Constant>(V); 396 return true; 397 } 398 return false; 399 } 400 }; 401 402 /// specialization of cstval_pred_ty for ConstantInt 403 template <typename Predicate, bool AllowPoison = true> 404 using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt, AllowPoison>; 405 406 /// specialization of cstval_pred_ty for ConstantFP 407 template <typename Predicate> 408 using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP, 409 /*AllowPoison=*/true>; 410 411 /// This helper class is used to match scalar and vector constants that 412 /// satisfy a specified predicate, and bind them to an APInt. 413 template <typename Predicate> struct api_pred_ty : public Predicate { 414 const APInt *&Res; 415 416 api_pred_ty(const APInt *&R) : Res(R) {} 417 418 template <typename ITy> bool match(ITy *V) { 419 if (const auto *CI = dyn_cast<ConstantInt>(V)) 420 if (this->isValue(CI->getValue())) { 421 Res = &CI->getValue(); 422 return true; 423 } 424 if (V->getType()->isVectorTy()) 425 if (const auto *C = dyn_cast<Constant>(V)) 426 if (auto *CI = dyn_cast_or_null<ConstantInt>( 427 C->getSplatValue(/*AllowPoison=*/true))) 428 if (this->isValue(CI->getValue())) { 429 Res = &CI->getValue(); 430 return true; 431 } 432 433 return false; 434 } 435 }; 436 437 /// This helper class is used to match scalar and vector constants that 438 /// satisfy a specified predicate, and bind them to an APFloat. 439 /// Poison is allowed in splat vector constants. 440 template <typename Predicate> struct apf_pred_ty : public Predicate { 441 const APFloat *&Res; 442 443 apf_pred_ty(const APFloat *&R) : Res(R) {} 444 445 template <typename ITy> bool match(ITy *V) { 446 if (const auto *CI = dyn_cast<ConstantFP>(V)) 447 if (this->isValue(CI->getValue())) { 448 Res = &CI->getValue(); 449 return true; 450 } 451 if (V->getType()->isVectorTy()) 452 if (const auto *C = dyn_cast<Constant>(V)) 453 if (auto *CI = dyn_cast_or_null<ConstantFP>( 454 C->getSplatValue(/* AllowPoison */ true))) 455 if (this->isValue(CI->getValue())) { 456 Res = &CI->getValue(); 457 return true; 458 } 459 460 return false; 461 } 462 }; 463 464 /////////////////////////////////////////////////////////////////////////////// 465 // 466 // Encapsulate constant value queries for use in templated predicate matchers. 467 // This allows checking if constants match using compound predicates and works 468 // with vector constants, possibly with relaxed constraints. For example, ignore 469 // undef values. 470 // 471 /////////////////////////////////////////////////////////////////////////////// 472 473 template <typename APTy> struct custom_checkfn { 474 function_ref<bool(const APTy &)> CheckFn; 475 bool isValue(const APTy &C) { return CheckFn(C); } 476 }; 477 478 /// Match an integer or vector where CheckFn(ele) for each element is true. 479 /// For vectors, poison elements are assumed to match. 480 inline cst_pred_ty<custom_checkfn<APInt>> 481 m_CheckedInt(function_ref<bool(const APInt &)> CheckFn) { 482 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}}; 483 } 484 485 inline cst_pred_ty<custom_checkfn<APInt>> 486 m_CheckedInt(const Constant *&V, function_ref<bool(const APInt &)> CheckFn) { 487 return cst_pred_ty<custom_checkfn<APInt>>{{CheckFn}, &V}; 488 } 489 490 /// Match a float or vector where CheckFn(ele) for each element is true. 491 /// For vectors, poison elements are assumed to match. 492 inline cstfp_pred_ty<custom_checkfn<APFloat>> 493 m_CheckedFp(function_ref<bool(const APFloat &)> CheckFn) { 494 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}}; 495 } 496 497 inline cstfp_pred_ty<custom_checkfn<APFloat>> 498 m_CheckedFp(const Constant *&V, function_ref<bool(const APFloat &)> CheckFn) { 499 return cstfp_pred_ty<custom_checkfn<APFloat>>{{CheckFn}, &V}; 500 } 501 502 struct is_any_apint { 503 bool isValue(const APInt &C) { return true; } 504 }; 505 /// Match an integer or vector with any integral constant. 506 /// For vectors, this includes constants with undefined elements. 507 inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() { 508 return cst_pred_ty<is_any_apint>(); 509 } 510 511 struct is_shifted_mask { 512 bool isValue(const APInt &C) { return C.isShiftedMask(); } 513 }; 514 515 inline cst_pred_ty<is_shifted_mask> m_ShiftedMask() { 516 return cst_pred_ty<is_shifted_mask>(); 517 } 518 519 struct is_all_ones { 520 bool isValue(const APInt &C) { return C.isAllOnes(); } 521 }; 522 /// Match an integer or vector with all bits set. 523 /// For vectors, this includes constants with undefined elements. 524 inline cst_pred_ty<is_all_ones> m_AllOnes() { 525 return cst_pred_ty<is_all_ones>(); 526 } 527 528 inline cst_pred_ty<is_all_ones, false> m_AllOnesForbidPoison() { 529 return cst_pred_ty<is_all_ones, false>(); 530 } 531 532 struct is_maxsignedvalue { 533 bool isValue(const APInt &C) { return C.isMaxSignedValue(); } 534 }; 535 /// Match an integer or vector with values having all bits except for the high 536 /// bit set (0x7f...). 537 /// For vectors, this includes constants with undefined elements. 538 inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { 539 return cst_pred_ty<is_maxsignedvalue>(); 540 } 541 inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { 542 return V; 543 } 544 545 struct is_negative { 546 bool isValue(const APInt &C) { return C.isNegative(); } 547 }; 548 /// Match an integer or vector of negative values. 549 /// For vectors, this includes constants with undefined elements. 550 inline cst_pred_ty<is_negative> m_Negative() { 551 return cst_pred_ty<is_negative>(); 552 } 553 inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; } 554 555 struct is_nonnegative { 556 bool isValue(const APInt &C) { return C.isNonNegative(); } 557 }; 558 /// Match an integer or vector of non-negative values. 559 /// For vectors, this includes constants with undefined elements. 560 inline cst_pred_ty<is_nonnegative> m_NonNegative() { 561 return cst_pred_ty<is_nonnegative>(); 562 } 563 inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; } 564 565 struct is_strictlypositive { 566 bool isValue(const APInt &C) { return C.isStrictlyPositive(); } 567 }; 568 /// Match an integer or vector of strictly positive values. 569 /// For vectors, this includes constants with undefined elements. 570 inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() { 571 return cst_pred_ty<is_strictlypositive>(); 572 } 573 inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) { 574 return V; 575 } 576 577 struct is_nonpositive { 578 bool isValue(const APInt &C) { return C.isNonPositive(); } 579 }; 580 /// Match an integer or vector of non-positive values. 581 /// For vectors, this includes constants with undefined elements. 582 inline cst_pred_ty<is_nonpositive> m_NonPositive() { 583 return cst_pred_ty<is_nonpositive>(); 584 } 585 inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; } 586 587 struct is_one { 588 bool isValue(const APInt &C) { return C.isOne(); } 589 }; 590 /// Match an integer 1 or a vector with all elements equal to 1. 591 /// For vectors, this includes constants with undefined elements. 592 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); } 593 594 struct is_zero_int { 595 bool isValue(const APInt &C) { return C.isZero(); } 596 }; 597 /// Match an integer 0 or a vector with all elements equal to 0. 598 /// For vectors, this includes constants with undefined elements. 599 inline cst_pred_ty<is_zero_int> m_ZeroInt() { 600 return cst_pred_ty<is_zero_int>(); 601 } 602 603 struct is_zero { 604 template <typename ITy> bool match(ITy *V) { 605 auto *C = dyn_cast<Constant>(V); 606 // FIXME: this should be able to do something for scalable vectors 607 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); 608 } 609 }; 610 /// Match any null constant or a vector with all elements equal to 0. 611 /// For vectors, this includes constants with undefined elements. 612 inline is_zero m_Zero() { return is_zero(); } 613 614 struct is_power2 { 615 bool isValue(const APInt &C) { return C.isPowerOf2(); } 616 }; 617 /// Match an integer or vector power-of-2. 618 /// For vectors, this includes constants with undefined elements. 619 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); } 620 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; } 621 622 struct is_negated_power2 { 623 bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); } 624 }; 625 /// Match a integer or vector negated power-of-2. 626 /// For vectors, this includes constants with undefined elements. 627 inline cst_pred_ty<is_negated_power2> m_NegatedPower2() { 628 return cst_pred_ty<is_negated_power2>(); 629 } 630 inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) { 631 return V; 632 } 633 634 struct is_negated_power2_or_zero { 635 bool isValue(const APInt &C) { return !C || C.isNegatedPowerOf2(); } 636 }; 637 /// Match a integer or vector negated power-of-2. 638 /// For vectors, this includes constants with undefined elements. 639 inline cst_pred_ty<is_negated_power2_or_zero> m_NegatedPower2OrZero() { 640 return cst_pred_ty<is_negated_power2_or_zero>(); 641 } 642 inline api_pred_ty<is_negated_power2_or_zero> 643 m_NegatedPower2OrZero(const APInt *&V) { 644 return V; 645 } 646 647 struct is_power2_or_zero { 648 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } 649 }; 650 /// Match an integer or vector of 0 or power-of-2 values. 651 /// For vectors, this includes constants with undefined elements. 652 inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { 653 return cst_pred_ty<is_power2_or_zero>(); 654 } 655 inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { 656 return V; 657 } 658 659 struct is_sign_mask { 660 bool isValue(const APInt &C) { return C.isSignMask(); } 661 }; 662 /// Match an integer or vector with only the sign bit(s) set. 663 /// For vectors, this includes constants with undefined elements. 664 inline cst_pred_ty<is_sign_mask> m_SignMask() { 665 return cst_pred_ty<is_sign_mask>(); 666 } 667 668 struct is_lowbit_mask { 669 bool isValue(const APInt &C) { return C.isMask(); } 670 }; 671 /// Match an integer or vector with only the low bit(s) set. 672 /// For vectors, this includes constants with undefined elements. 673 inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { 674 return cst_pred_ty<is_lowbit_mask>(); 675 } 676 inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; } 677 678 struct is_lowbit_mask_or_zero { 679 bool isValue(const APInt &C) { return !C || C.isMask(); } 680 }; 681 /// Match an integer or vector with only the low bit(s) set. 682 /// For vectors, this includes constants with undefined elements. 683 inline cst_pred_ty<is_lowbit_mask_or_zero> m_LowBitMaskOrZero() { 684 return cst_pred_ty<is_lowbit_mask_or_zero>(); 685 } 686 inline api_pred_ty<is_lowbit_mask_or_zero> m_LowBitMaskOrZero(const APInt *&V) { 687 return V; 688 } 689 690 struct icmp_pred_with_threshold { 691 CmpPredicate Pred; 692 const APInt *Thr; 693 bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); } 694 }; 695 /// Match an integer or vector with every element comparing 'pred' (eg/ne/...) 696 /// to Threshold. For vectors, this includes constants with undefined elements. 697 inline cst_pred_ty<icmp_pred_with_threshold> 698 m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) { 699 cst_pred_ty<icmp_pred_with_threshold> P; 700 P.Pred = Predicate; 701 P.Thr = &Threshold; 702 return P; 703 } 704 705 struct is_nan { 706 bool isValue(const APFloat &C) { return C.isNaN(); } 707 }; 708 /// Match an arbitrary NaN constant. This includes quiet and signalling nans. 709 /// For vectors, this includes constants with undefined elements. 710 inline cstfp_pred_ty<is_nan> m_NaN() { return cstfp_pred_ty<is_nan>(); } 711 712 struct is_nonnan { 713 bool isValue(const APFloat &C) { return !C.isNaN(); } 714 }; 715 /// Match a non-NaN FP constant. 716 /// For vectors, this includes constants with undefined elements. 717 inline cstfp_pred_ty<is_nonnan> m_NonNaN() { 718 return cstfp_pred_ty<is_nonnan>(); 719 } 720 721 struct is_inf { 722 bool isValue(const APFloat &C) { return C.isInfinity(); } 723 }; 724 /// Match a positive or negative infinity FP constant. 725 /// For vectors, this includes constants with undefined elements. 726 inline cstfp_pred_ty<is_inf> m_Inf() { return cstfp_pred_ty<is_inf>(); } 727 728 struct is_noninf { 729 bool isValue(const APFloat &C) { return !C.isInfinity(); } 730 }; 731 /// Match a non-infinity FP constant, i.e. finite or NaN. 732 /// For vectors, this includes constants with undefined elements. 733 inline cstfp_pred_ty<is_noninf> m_NonInf() { 734 return cstfp_pred_ty<is_noninf>(); 735 } 736 737 struct is_finite { 738 bool isValue(const APFloat &C) { return C.isFinite(); } 739 }; 740 /// Match a finite FP constant, i.e. not infinity or NaN. 741 /// For vectors, this includes constants with undefined elements. 742 inline cstfp_pred_ty<is_finite> m_Finite() { 743 return cstfp_pred_ty<is_finite>(); 744 } 745 inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; } 746 747 struct is_finitenonzero { 748 bool isValue(const APFloat &C) { return C.isFiniteNonZero(); } 749 }; 750 /// Match a finite non-zero FP constant. 751 /// For vectors, this includes constants with undefined elements. 752 inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() { 753 return cstfp_pred_ty<is_finitenonzero>(); 754 } 755 inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) { 756 return V; 757 } 758 759 struct is_any_zero_fp { 760 bool isValue(const APFloat &C) { return C.isZero(); } 761 }; 762 /// Match a floating-point negative zero or positive zero. 763 /// For vectors, this includes constants with undefined elements. 764 inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { 765 return cstfp_pred_ty<is_any_zero_fp>(); 766 } 767 768 struct is_pos_zero_fp { 769 bool isValue(const APFloat &C) { return C.isPosZero(); } 770 }; 771 /// Match a floating-point positive zero. 772 /// For vectors, this includes constants with undefined elements. 773 inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { 774 return cstfp_pred_ty<is_pos_zero_fp>(); 775 } 776 777 struct is_neg_zero_fp { 778 bool isValue(const APFloat &C) { return C.isNegZero(); } 779 }; 780 /// Match a floating-point negative zero. 781 /// For vectors, this includes constants with undefined elements. 782 inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { 783 return cstfp_pred_ty<is_neg_zero_fp>(); 784 } 785 786 struct is_non_zero_fp { 787 bool isValue(const APFloat &C) { return C.isNonZero(); } 788 }; 789 /// Match a floating-point non-zero. 790 /// For vectors, this includes constants with undefined elements. 791 inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() { 792 return cstfp_pred_ty<is_non_zero_fp>(); 793 } 794 795 struct is_non_zero_not_denormal_fp { 796 bool isValue(const APFloat &C) { return !C.isDenormal() && C.isNonZero(); } 797 }; 798 799 /// Match a floating-point non-zero that is not a denormal. 800 /// For vectors, this includes constants with undefined elements. 801 inline cstfp_pred_ty<is_non_zero_not_denormal_fp> m_NonZeroNotDenormalFP() { 802 return cstfp_pred_ty<is_non_zero_not_denormal_fp>(); 803 } 804 805 /////////////////////////////////////////////////////////////////////////////// 806 807 template <typename Class> struct bind_ty { 808 Class *&VR; 809 810 bind_ty(Class *&V) : VR(V) {} 811 812 template <typename ITy> bool match(ITy *V) { 813 if (auto *CV = dyn_cast<Class>(V)) { 814 VR = CV; 815 return true; 816 } 817 return false; 818 } 819 }; 820 821 /// Match a value, capturing it if we match. 822 inline bind_ty<Value> m_Value(Value *&V) { return V; } 823 inline bind_ty<const Value> m_Value(const Value *&V) { return V; } 824 825 /// Match an instruction, capturing it if we match. 826 inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } 827 /// Match a unary operator, capturing it if we match. 828 inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; } 829 /// Match a binary operator, capturing it if we match. 830 inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } 831 /// Match a with overflow intrinsic, capturing it if we match. 832 inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { 833 return I; 834 } 835 inline bind_ty<const WithOverflowInst> 836 m_WithOverflowInst(const WithOverflowInst *&I) { 837 return I; 838 } 839 840 /// Match an UndefValue, capturing the value if we match. 841 inline bind_ty<UndefValue> m_UndefValue(UndefValue *&U) { return U; } 842 843 /// Match a Constant, capturing the value if we match. 844 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } 845 846 /// Match a ConstantInt, capturing the value if we match. 847 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } 848 849 /// Match a ConstantFP, capturing the value if we match. 850 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } 851 852 /// Match a ConstantExpr, capturing the value if we match. 853 inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; } 854 855 /// Match a basic block value, capturing it if we match. 856 inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; } 857 inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) { 858 return V; 859 } 860 861 /// Match an arbitrary immediate Constant and ignore it. 862 inline match_combine_and<class_match<Constant>, 863 match_unless<constantexpr_match>> 864 m_ImmConstant() { 865 return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr())); 866 } 867 868 /// Match an immediate Constant, capturing the value if we match. 869 inline match_combine_and<bind_ty<Constant>, 870 match_unless<constantexpr_match>> 871 m_ImmConstant(Constant *&C) { 872 return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr())); 873 } 874 875 /// Match a specified Value*. 876 struct specificval_ty { 877 const Value *Val; 878 879 specificval_ty(const Value *V) : Val(V) {} 880 881 template <typename ITy> bool match(ITy *V) { return V == Val; } 882 }; 883 884 /// Match if we have a specific specified value. 885 inline specificval_ty m_Specific(const Value *V) { return V; } 886 887 /// Stores a reference to the Value *, not the Value * itself, 888 /// thus can be used in commutative matchers. 889 template <typename Class> struct deferredval_ty { 890 Class *const &Val; 891 892 deferredval_ty(Class *const &V) : Val(V) {} 893 894 template <typename ITy> bool match(ITy *const V) { return V == Val; } 895 }; 896 897 /// Like m_Specific(), but works if the specific value to match is determined 898 /// as part of the same match() expression. For example: 899 /// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will 900 /// bind X before the pattern match starts. 901 /// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against 902 /// whichever value m_Value(X) populated. 903 inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; } 904 inline deferredval_ty<const Value> m_Deferred(const Value *const &V) { 905 return V; 906 } 907 908 /// Match a specified floating point value or vector of all elements of 909 /// that value. 910 struct specific_fpval { 911 double Val; 912 913 specific_fpval(double V) : Val(V) {} 914 915 template <typename ITy> bool match(ITy *V) { 916 if (const auto *CFP = dyn_cast<ConstantFP>(V)) 917 return CFP->isExactlyValue(Val); 918 if (V->getType()->isVectorTy()) 919 if (const auto *C = dyn_cast<Constant>(V)) 920 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) 921 return CFP->isExactlyValue(Val); 922 return false; 923 } 924 }; 925 926 /// Match a specific floating point value or vector with all elements 927 /// equal to the value. 928 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } 929 930 /// Match a float 1.0 or vector with all elements equal to 1.0. 931 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } 932 933 struct bind_const_intval_ty { 934 uint64_t &VR; 935 936 bind_const_intval_ty(uint64_t &V) : VR(V) {} 937 938 template <typename ITy> bool match(ITy *V) { 939 if (const auto *CV = dyn_cast<ConstantInt>(V)) 940 if (CV->getValue().ule(UINT64_MAX)) { 941 VR = CV->getZExtValue(); 942 return true; 943 } 944 return false; 945 } 946 }; 947 948 /// Match a specified integer value or vector of all elements of that 949 /// value. 950 template <bool AllowPoison> struct specific_intval { 951 const APInt &Val; 952 953 specific_intval(const APInt &V) : Val(V) {} 954 955 template <typename ITy> bool match(ITy *V) { 956 const auto *CI = dyn_cast<ConstantInt>(V); 957 if (!CI && V->getType()->isVectorTy()) 958 if (const auto *C = dyn_cast<Constant>(V)) 959 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison)); 960 961 return CI && APInt::isSameValue(CI->getValue(), Val); 962 } 963 }; 964 965 template <bool AllowPoison> struct specific_intval64 { 966 uint64_t Val; 967 968 specific_intval64(uint64_t V) : Val(V) {} 969 970 template <typename ITy> bool match(ITy *V) { 971 const auto *CI = dyn_cast<ConstantInt>(V); 972 if (!CI && V->getType()->isVectorTy()) 973 if (const auto *C = dyn_cast<Constant>(V)) 974 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowPoison)); 975 976 return CI && CI->getValue() == Val; 977 } 978 }; 979 980 /// Match a specific integer value or vector with all elements equal to 981 /// the value. 982 inline specific_intval<false> m_SpecificInt(const APInt &V) { 983 return specific_intval<false>(V); 984 } 985 986 inline specific_intval64<false> m_SpecificInt(uint64_t V) { 987 return specific_intval64<false>(V); 988 } 989 990 inline specific_intval<true> m_SpecificIntAllowPoison(const APInt &V) { 991 return specific_intval<true>(V); 992 } 993 994 inline specific_intval64<true> m_SpecificIntAllowPoison(uint64_t V) { 995 return specific_intval64<true>(V); 996 } 997 998 /// Match a ConstantInt and bind to its value. This does not match 999 /// ConstantInts wider than 64-bits. 1000 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } 1001 1002 /// Match a specified basic block value. 1003 struct specific_bbval { 1004 BasicBlock *Val; 1005 1006 specific_bbval(BasicBlock *Val) : Val(Val) {} 1007 1008 template <typename ITy> bool match(ITy *V) { 1009 const auto *BB = dyn_cast<BasicBlock>(V); 1010 return BB && BB == Val; 1011 } 1012 }; 1013 1014 /// Match a specific basic block value. 1015 inline specific_bbval m_SpecificBB(BasicBlock *BB) { 1016 return specific_bbval(BB); 1017 } 1018 1019 /// A commutative-friendly version of m_Specific(). 1020 inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) { 1021 return BB; 1022 } 1023 inline deferredval_ty<const BasicBlock> 1024 m_Deferred(const BasicBlock *const &BB) { 1025 return BB; 1026 } 1027 1028 //===----------------------------------------------------------------------===// 1029 // Matcher for any binary operator. 1030 // 1031 template <typename LHS_t, typename RHS_t, bool Commutable = false> 1032 struct AnyBinaryOp_match { 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 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 1039 1040 template <typename OpTy> bool match(OpTy *V) { 1041 if (auto *I = dyn_cast<BinaryOperator>(V)) 1042 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || 1043 (Commutable && L.match(I->getOperand(1)) && 1044 R.match(I->getOperand(0))); 1045 return false; 1046 } 1047 }; 1048 1049 template <typename LHS, typename RHS> 1050 inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) { 1051 return AnyBinaryOp_match<LHS, RHS>(L, R); 1052 } 1053 1054 //===----------------------------------------------------------------------===// 1055 // Matcher for any unary operator. 1056 // TODO fuse unary, binary matcher into n-ary matcher 1057 // 1058 template <typename OP_t> struct AnyUnaryOp_match { 1059 OP_t X; 1060 1061 AnyUnaryOp_match(const OP_t &X) : X(X) {} 1062 1063 template <typename OpTy> bool match(OpTy *V) { 1064 if (auto *I = dyn_cast<UnaryOperator>(V)) 1065 return X.match(I->getOperand(0)); 1066 return false; 1067 } 1068 }; 1069 1070 template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) { 1071 return AnyUnaryOp_match<OP_t>(X); 1072 } 1073 1074 //===----------------------------------------------------------------------===// 1075 // Matchers for specific binary operators. 1076 // 1077 1078 template <typename LHS_t, typename RHS_t, unsigned Opcode, 1079 bool Commutable = false> 1080 struct BinaryOp_match { 1081 LHS_t L; 1082 RHS_t R; 1083 1084 // The evaluation order is always stable, regardless of Commutability. 1085 // The LHS is always matched first. 1086 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 1087 1088 template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) { 1089 if (V->getValueID() == Value::InstructionVal + Opc) { 1090 auto *I = cast<BinaryOperator>(V); 1091 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || 1092 (Commutable && L.match(I->getOperand(1)) && 1093 R.match(I->getOperand(0))); 1094 } 1095 return false; 1096 } 1097 1098 template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); } 1099 }; 1100 1101 template <typename LHS, typename RHS> 1102 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L, 1103 const RHS &R) { 1104 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); 1105 } 1106 1107 template <typename LHS, typename RHS> 1108 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L, 1109 const RHS &R) { 1110 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); 1111 } 1112 1113 template <typename LHS, typename RHS> 1114 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L, 1115 const RHS &R) { 1116 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); 1117 } 1118 1119 template <typename LHS, typename RHS> 1120 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L, 1121 const RHS &R) { 1122 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); 1123 } 1124 1125 template <typename Op_t> struct FNeg_match { 1126 Op_t X; 1127 1128 FNeg_match(const Op_t &Op) : X(Op) {} 1129 template <typename OpTy> bool match(OpTy *V) { 1130 auto *FPMO = dyn_cast<FPMathOperator>(V); 1131 if (!FPMO) 1132 return false; 1133 1134 if (FPMO->getOpcode() == Instruction::FNeg) 1135 return X.match(FPMO->getOperand(0)); 1136 1137 if (FPMO->getOpcode() == Instruction::FSub) { 1138 if (FPMO->hasNoSignedZeros()) { 1139 // With 'nsz', any zero goes. 1140 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0))) 1141 return false; 1142 } else { 1143 // Without 'nsz', we need fsub -0.0, X exactly. 1144 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0))) 1145 return false; 1146 } 1147 1148 return X.match(FPMO->getOperand(1)); 1149 } 1150 1151 return false; 1152 } 1153 }; 1154 1155 /// Match 'fneg X' as 'fsub -0.0, X'. 1156 template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) { 1157 return FNeg_match<OpTy>(X); 1158 } 1159 1160 /// Match 'fneg X' as 'fsub +-0.0, X'. 1161 template <typename RHS> 1162 inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub> 1163 m_FNegNSZ(const RHS &X) { 1164 return m_FSub(m_AnyZeroFP(), X); 1165 } 1166 1167 template <typename LHS, typename RHS> 1168 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L, 1169 const RHS &R) { 1170 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); 1171 } 1172 1173 template <typename LHS, typename RHS> 1174 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L, 1175 const RHS &R) { 1176 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); 1177 } 1178 1179 template <typename LHS, typename RHS> 1180 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L, 1181 const RHS &R) { 1182 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); 1183 } 1184 1185 template <typename LHS, typename RHS> 1186 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L, 1187 const RHS &R) { 1188 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); 1189 } 1190 1191 template <typename LHS, typename RHS> 1192 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L, 1193 const RHS &R) { 1194 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); 1195 } 1196 1197 template <typename LHS, typename RHS> 1198 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L, 1199 const RHS &R) { 1200 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); 1201 } 1202 1203 template <typename LHS, typename RHS> 1204 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L, 1205 const RHS &R) { 1206 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); 1207 } 1208 1209 template <typename LHS, typename RHS> 1210 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L, 1211 const RHS &R) { 1212 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); 1213 } 1214 1215 template <typename LHS, typename RHS> 1216 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L, 1217 const RHS &R) { 1218 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); 1219 } 1220 1221 template <typename LHS, typename RHS> 1222 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L, 1223 const RHS &R) { 1224 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); 1225 } 1226 1227 template <typename LHS, typename RHS> 1228 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L, 1229 const RHS &R) { 1230 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); 1231 } 1232 1233 template <typename LHS, typename RHS> 1234 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, 1235 const RHS &R) { 1236 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); 1237 } 1238 1239 template <typename LHS, typename RHS> 1240 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, 1241 const RHS &R) { 1242 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); 1243 } 1244 1245 template <typename LHS, typename RHS> 1246 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, 1247 const RHS &R) { 1248 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); 1249 } 1250 1251 template <typename LHS_t, typename RHS_t, unsigned Opcode, 1252 unsigned WrapFlags = 0, bool Commutable = false> 1253 struct OverflowingBinaryOp_match { 1254 LHS_t L; 1255 RHS_t R; 1256 1257 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) 1258 : L(LHS), R(RHS) {} 1259 1260 template <typename OpTy> bool match(OpTy *V) { 1261 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) { 1262 if (Op->getOpcode() != Opcode) 1263 return false; 1264 if ((WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap) && 1265 !Op->hasNoUnsignedWrap()) 1266 return false; 1267 if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) && 1268 !Op->hasNoSignedWrap()) 1269 return false; 1270 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) || 1271 (Commutable && L.match(Op->getOperand(1)) && 1272 R.match(Op->getOperand(0))); 1273 } 1274 return false; 1275 } 1276 }; 1277 1278 template <typename LHS, typename RHS> 1279 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1280 OverflowingBinaryOperator::NoSignedWrap> 1281 m_NSWAdd(const LHS &L, const RHS &R) { 1282 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1283 OverflowingBinaryOperator::NoSignedWrap>(L, 1284 R); 1285 } 1286 template <typename LHS, typename RHS> 1287 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 1288 OverflowingBinaryOperator::NoSignedWrap> 1289 m_NSWSub(const LHS &L, const RHS &R) { 1290 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 1291 OverflowingBinaryOperator::NoSignedWrap>(L, 1292 R); 1293 } 1294 template <typename LHS, typename RHS> 1295 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 1296 OverflowingBinaryOperator::NoSignedWrap> 1297 m_NSWMul(const LHS &L, const RHS &R) { 1298 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 1299 OverflowingBinaryOperator::NoSignedWrap>(L, 1300 R); 1301 } 1302 template <typename LHS, typename RHS> 1303 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 1304 OverflowingBinaryOperator::NoSignedWrap> 1305 m_NSWShl(const LHS &L, const RHS &R) { 1306 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 1307 OverflowingBinaryOperator::NoSignedWrap>(L, 1308 R); 1309 } 1310 1311 template <typename LHS, typename RHS> 1312 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1313 OverflowingBinaryOperator::NoUnsignedWrap> 1314 m_NUWAdd(const LHS &L, const RHS &R) { 1315 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1316 OverflowingBinaryOperator::NoUnsignedWrap>( 1317 L, R); 1318 } 1319 1320 template <typename LHS, typename RHS> 1321 inline OverflowingBinaryOp_match< 1322 LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true> 1323 m_c_NUWAdd(const LHS &L, const RHS &R) { 1324 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1325 OverflowingBinaryOperator::NoUnsignedWrap, 1326 true>(L, R); 1327 } 1328 1329 template <typename LHS, typename RHS> 1330 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 1331 OverflowingBinaryOperator::NoUnsignedWrap> 1332 m_NUWSub(const LHS &L, const RHS &R) { 1333 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, 1334 OverflowingBinaryOperator::NoUnsignedWrap>( 1335 L, R); 1336 } 1337 template <typename LHS, typename RHS> 1338 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 1339 OverflowingBinaryOperator::NoUnsignedWrap> 1340 m_NUWMul(const LHS &L, const RHS &R) { 1341 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, 1342 OverflowingBinaryOperator::NoUnsignedWrap>( 1343 L, R); 1344 } 1345 template <typename LHS, typename RHS> 1346 inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 1347 OverflowingBinaryOperator::NoUnsignedWrap> 1348 m_NUWShl(const LHS &L, const RHS &R) { 1349 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, 1350 OverflowingBinaryOperator::NoUnsignedWrap>( 1351 L, R); 1352 } 1353 1354 template <typename LHS_t, typename RHS_t, bool Commutable = false> 1355 struct SpecificBinaryOp_match 1356 : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> { 1357 unsigned Opcode; 1358 1359 SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS) 1360 : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {} 1361 1362 template <typename OpTy> bool match(OpTy *V) { 1363 return BinaryOp_match<LHS_t, RHS_t, 0, Commutable>::match(Opcode, V); 1364 } 1365 }; 1366 1367 /// Matches a specific opcode. 1368 template <typename LHS, typename RHS> 1369 inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L, 1370 const RHS &R) { 1371 return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R); 1372 } 1373 1374 template <typename LHS, typename RHS, bool Commutable = false> 1375 struct DisjointOr_match { 1376 LHS L; 1377 RHS R; 1378 1379 DisjointOr_match(const LHS &L, const RHS &R) : L(L), R(R) {} 1380 1381 template <typename OpTy> bool match(OpTy *V) { 1382 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(V)) { 1383 assert(PDI->getOpcode() == Instruction::Or && "Only or can be disjoint"); 1384 if (!PDI->isDisjoint()) 1385 return false; 1386 return (L.match(PDI->getOperand(0)) && R.match(PDI->getOperand(1))) || 1387 (Commutable && L.match(PDI->getOperand(1)) && 1388 R.match(PDI->getOperand(0))); 1389 } 1390 return false; 1391 } 1392 }; 1393 1394 template <typename LHS, typename RHS> 1395 inline DisjointOr_match<LHS, RHS> m_DisjointOr(const LHS &L, const RHS &R) { 1396 return DisjointOr_match<LHS, RHS>(L, R); 1397 } 1398 1399 template <typename LHS, typename RHS> 1400 inline DisjointOr_match<LHS, RHS, true> m_c_DisjointOr(const LHS &L, 1401 const RHS &R) { 1402 return DisjointOr_match<LHS, RHS, true>(L, R); 1403 } 1404 1405 /// Match either "add" or "or disjoint". 1406 template <typename LHS, typename RHS> 1407 inline match_combine_or<BinaryOp_match<LHS, RHS, Instruction::Add>, 1408 DisjointOr_match<LHS, RHS>> 1409 m_AddLike(const LHS &L, const RHS &R) { 1410 return m_CombineOr(m_Add(L, R), m_DisjointOr(L, R)); 1411 } 1412 1413 /// Match either "add nsw" or "or disjoint" 1414 template <typename LHS, typename RHS> 1415 inline match_combine_or< 1416 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1417 OverflowingBinaryOperator::NoSignedWrap>, 1418 DisjointOr_match<LHS, RHS>> 1419 m_NSWAddLike(const LHS &L, const RHS &R) { 1420 return m_CombineOr(m_NSWAdd(L, R), m_DisjointOr(L, R)); 1421 } 1422 1423 /// Match either "add nuw" or "or disjoint" 1424 template <typename LHS, typename RHS> 1425 inline match_combine_or< 1426 OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, 1427 OverflowingBinaryOperator::NoUnsignedWrap>, 1428 DisjointOr_match<LHS, RHS>> 1429 m_NUWAddLike(const LHS &L, const RHS &R) { 1430 return m_CombineOr(m_NUWAdd(L, R), m_DisjointOr(L, R)); 1431 } 1432 1433 template <typename LHS, typename RHS> 1434 struct XorLike_match { 1435 LHS L; 1436 RHS R; 1437 1438 XorLike_match(const LHS &L, const RHS &R) : L(L), R(R) {} 1439 1440 template <typename OpTy> bool match(OpTy *V) { 1441 if (auto *Op = dyn_cast<BinaryOperator>(V)) { 1442 if (Op->getOpcode() == Instruction::Sub && Op->hasNoUnsignedWrap() && 1443 PatternMatch::match(Op->getOperand(0), m_LowBitMask())) 1444 ; // Pass 1445 else if (Op->getOpcode() != Instruction::Xor) 1446 return false; 1447 return (L.match(Op->getOperand(0)) && R.match(Op->getOperand(1))) || 1448 (L.match(Op->getOperand(1)) && R.match(Op->getOperand(0))); 1449 } 1450 return false; 1451 } 1452 }; 1453 1454 /// Match either `(xor L, R)`, `(xor R, L)` or `(sub nuw R, L)` iff `R.isMask()` 1455 /// Only commutative matcher as the `sub` will need to swap the L and R. 1456 template <typename LHS, typename RHS> 1457 inline auto m_c_XorLike(const LHS &L, const RHS &R) { 1458 return XorLike_match<LHS, RHS>(L, R); 1459 } 1460 1461 //===----------------------------------------------------------------------===// 1462 // Class that matches a group of binary opcodes. 1463 // 1464 template <typename LHS_t, typename RHS_t, typename Predicate, 1465 bool Commutable = false> 1466 struct BinOpPred_match : Predicate { 1467 LHS_t L; 1468 RHS_t R; 1469 1470 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 1471 1472 template <typename OpTy> bool match(OpTy *V) { 1473 if (auto *I = dyn_cast<Instruction>(V)) 1474 return this->isOpType(I->getOpcode()) && 1475 ((L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || 1476 (Commutable && L.match(I->getOperand(1)) && 1477 R.match(I->getOperand(0)))); 1478 return false; 1479 } 1480 }; 1481 1482 struct is_shift_op { 1483 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); } 1484 }; 1485 1486 struct is_right_shift_op { 1487 bool isOpType(unsigned Opcode) { 1488 return Opcode == Instruction::LShr || Opcode == Instruction::AShr; 1489 } 1490 }; 1491 1492 struct is_logical_shift_op { 1493 bool isOpType(unsigned Opcode) { 1494 return Opcode == Instruction::LShr || Opcode == Instruction::Shl; 1495 } 1496 }; 1497 1498 struct is_bitwiselogic_op { 1499 bool isOpType(unsigned Opcode) { 1500 return Instruction::isBitwiseLogicOp(Opcode); 1501 } 1502 }; 1503 1504 struct is_idiv_op { 1505 bool isOpType(unsigned Opcode) { 1506 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv; 1507 } 1508 }; 1509 1510 struct is_irem_op { 1511 bool isOpType(unsigned Opcode) { 1512 return Opcode == Instruction::SRem || Opcode == Instruction::URem; 1513 } 1514 }; 1515 1516 /// Matches shift operations. 1517 template <typename LHS, typename RHS> 1518 inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L, 1519 const RHS &R) { 1520 return BinOpPred_match<LHS, RHS, is_shift_op>(L, R); 1521 } 1522 1523 /// Matches logical shift operations. 1524 template <typename LHS, typename RHS> 1525 inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L, 1526 const RHS &R) { 1527 return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R); 1528 } 1529 1530 /// Matches logical shift operations. 1531 template <typename LHS, typename RHS> 1532 inline BinOpPred_match<LHS, RHS, is_logical_shift_op> 1533 m_LogicalShift(const LHS &L, const RHS &R) { 1534 return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R); 1535 } 1536 1537 /// Matches bitwise logic operations. 1538 template <typename LHS, typename RHS> 1539 inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op> 1540 m_BitwiseLogic(const LHS &L, const RHS &R) { 1541 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R); 1542 } 1543 1544 /// Matches bitwise logic operations in either order. 1545 template <typename LHS, typename RHS> 1546 inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op, true> 1547 m_c_BitwiseLogic(const LHS &L, const RHS &R) { 1548 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op, true>(L, R); 1549 } 1550 1551 /// Matches integer division operations. 1552 template <typename LHS, typename RHS> 1553 inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L, 1554 const RHS &R) { 1555 return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R); 1556 } 1557 1558 /// Matches integer remainder operations. 1559 template <typename LHS, typename RHS> 1560 inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L, 1561 const RHS &R) { 1562 return BinOpPred_match<LHS, RHS, is_irem_op>(L, R); 1563 } 1564 1565 //===----------------------------------------------------------------------===// 1566 // Class that matches exact binary ops. 1567 // 1568 template <typename SubPattern_t> struct Exact_match { 1569 SubPattern_t SubPattern; 1570 1571 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} 1572 1573 template <typename OpTy> bool match(OpTy *V) { 1574 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V)) 1575 return PEO->isExact() && SubPattern.match(V); 1576 return false; 1577 } 1578 }; 1579 1580 template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) { 1581 return SubPattern; 1582 } 1583 1584 //===----------------------------------------------------------------------===// 1585 // Matchers for CmpInst classes 1586 // 1587 1588 template <typename LHS_t, typename RHS_t, typename Class, 1589 bool Commutable = false> 1590 struct CmpClass_match { 1591 CmpPredicate *Predicate; 1592 LHS_t L; 1593 RHS_t R; 1594 1595 // The evaluation order is always stable, regardless of Commutability. 1596 // The LHS is always matched first. 1597 CmpClass_match(CmpPredicate &Pred, const LHS_t &LHS, const RHS_t &RHS) 1598 : Predicate(&Pred), L(LHS), R(RHS) {} 1599 CmpClass_match(const LHS_t &LHS, const RHS_t &RHS) 1600 : Predicate(nullptr), L(LHS), R(RHS) {} 1601 1602 template <typename OpTy> bool match(OpTy *V) { 1603 if (auto *I = dyn_cast<Class>(V)) { 1604 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { 1605 if (Predicate) 1606 *Predicate = CmpPredicate::get(I); 1607 return true; 1608 } 1609 if (Commutable && L.match(I->getOperand(1)) && 1610 R.match(I->getOperand(0))) { 1611 if (Predicate) 1612 *Predicate = CmpPredicate::getSwapped(I); 1613 return true; 1614 } 1615 } 1616 return false; 1617 } 1618 }; 1619 1620 template <typename LHS, typename RHS> 1621 inline CmpClass_match<LHS, RHS, CmpInst> m_Cmp(CmpPredicate &Pred, const LHS &L, 1622 const RHS &R) { 1623 return CmpClass_match<LHS, RHS, CmpInst>(Pred, L, R); 1624 } 1625 1626 template <typename LHS, typename RHS> 1627 inline CmpClass_match<LHS, RHS, ICmpInst> m_ICmp(CmpPredicate &Pred, 1628 const LHS &L, const RHS &R) { 1629 return CmpClass_match<LHS, RHS, ICmpInst>(Pred, L, R); 1630 } 1631 1632 template <typename LHS, typename RHS> 1633 inline CmpClass_match<LHS, RHS, FCmpInst> m_FCmp(CmpPredicate &Pred, 1634 const LHS &L, const RHS &R) { 1635 return CmpClass_match<LHS, RHS, FCmpInst>(Pred, L, R); 1636 } 1637 1638 template <typename LHS, typename RHS> 1639 inline CmpClass_match<LHS, RHS, CmpInst> m_Cmp(const LHS &L, const RHS &R) { 1640 return CmpClass_match<LHS, RHS, CmpInst>(L, R); 1641 } 1642 1643 template <typename LHS, typename RHS> 1644 inline CmpClass_match<LHS, RHS, ICmpInst> m_ICmp(const LHS &L, const RHS &R) { 1645 return CmpClass_match<LHS, RHS, ICmpInst>(L, R); 1646 } 1647 1648 template <typename LHS, typename RHS> 1649 inline CmpClass_match<LHS, RHS, FCmpInst> m_FCmp(const LHS &L, const RHS &R) { 1650 return CmpClass_match<LHS, RHS, FCmpInst>(L, R); 1651 } 1652 1653 // Same as CmpClass, but instead of saving Pred as out output variable, match a 1654 // specific input pred for equality. 1655 template <typename LHS_t, typename RHS_t, typename Class, 1656 bool Commutable = false> 1657 struct SpecificCmpClass_match { 1658 const CmpPredicate Predicate; 1659 LHS_t L; 1660 RHS_t R; 1661 1662 SpecificCmpClass_match(CmpPredicate Pred, const LHS_t &LHS, const RHS_t &RHS) 1663 : Predicate(Pred), L(LHS), R(RHS) {} 1664 1665 template <typename OpTy> bool match(OpTy *V) { 1666 if (auto *I = dyn_cast<Class>(V)) { 1667 if (CmpPredicate::getMatching(CmpPredicate::get(I), Predicate) && 1668 L.match(I->getOperand(0)) && R.match(I->getOperand(1))) 1669 return true; 1670 if constexpr (Commutable) { 1671 if (CmpPredicate::getMatching(CmpPredicate::get(I), 1672 CmpPredicate::getSwapped(Predicate)) && 1673 L.match(I->getOperand(1)) && R.match(I->getOperand(0))) 1674 return true; 1675 } 1676 } 1677 1678 return false; 1679 } 1680 }; 1681 1682 template <typename LHS, typename RHS> 1683 inline SpecificCmpClass_match<LHS, RHS, CmpInst> 1684 m_SpecificCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) { 1685 return SpecificCmpClass_match<LHS, RHS, CmpInst>(MatchPred, L, R); 1686 } 1687 1688 template <typename LHS, typename RHS> 1689 inline SpecificCmpClass_match<LHS, RHS, ICmpInst> 1690 m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) { 1691 return SpecificCmpClass_match<LHS, RHS, ICmpInst>(MatchPred, L, R); 1692 } 1693 1694 template <typename LHS, typename RHS> 1695 inline SpecificCmpClass_match<LHS, RHS, ICmpInst, true> 1696 m_c_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) { 1697 return SpecificCmpClass_match<LHS, RHS, ICmpInst, true>(MatchPred, L, R); 1698 } 1699 1700 template <typename LHS, typename RHS> 1701 inline SpecificCmpClass_match<LHS, RHS, FCmpInst> 1702 m_SpecificFCmp(CmpPredicate MatchPred, const LHS &L, const RHS &R) { 1703 return SpecificCmpClass_match<LHS, RHS, FCmpInst>(MatchPred, L, R); 1704 } 1705 1706 //===----------------------------------------------------------------------===// 1707 // Matchers for instructions with a given opcode and number of operands. 1708 // 1709 1710 /// Matches instructions with Opcode and three operands. 1711 template <typename T0, unsigned Opcode> struct OneOps_match { 1712 T0 Op1; 1713 1714 OneOps_match(const T0 &Op1) : Op1(Op1) {} 1715 1716 template <typename OpTy> bool match(OpTy *V) { 1717 if (V->getValueID() == Value::InstructionVal + Opcode) { 1718 auto *I = cast<Instruction>(V); 1719 return Op1.match(I->getOperand(0)); 1720 } 1721 return false; 1722 } 1723 }; 1724 1725 /// Matches instructions with Opcode and three operands. 1726 template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match { 1727 T0 Op1; 1728 T1 Op2; 1729 1730 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {} 1731 1732 template <typename OpTy> bool match(OpTy *V) { 1733 if (V->getValueID() == Value::InstructionVal + Opcode) { 1734 auto *I = cast<Instruction>(V); 1735 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)); 1736 } 1737 return false; 1738 } 1739 }; 1740 1741 /// Matches instructions with Opcode and three operands. 1742 template <typename T0, typename T1, typename T2, unsigned Opcode, 1743 bool CommutableOp2Op3 = false> 1744 struct ThreeOps_match { 1745 T0 Op1; 1746 T1 Op2; 1747 T2 Op3; 1748 1749 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3) 1750 : Op1(Op1), Op2(Op2), Op3(Op3) {} 1751 1752 template <typename OpTy> bool match(OpTy *V) { 1753 if (V->getValueID() == Value::InstructionVal + Opcode) { 1754 auto *I = cast<Instruction>(V); 1755 if (!Op1.match(I->getOperand(0))) 1756 return false; 1757 if (Op2.match(I->getOperand(1)) && Op3.match(I->getOperand(2))) 1758 return true; 1759 return CommutableOp2Op3 && Op2.match(I->getOperand(2)) && 1760 Op3.match(I->getOperand(1)); 1761 } 1762 return false; 1763 } 1764 }; 1765 1766 /// Matches instructions with Opcode and any number of operands 1767 template <unsigned Opcode, typename... OperandTypes> struct AnyOps_match { 1768 std::tuple<OperandTypes...> Operands; 1769 1770 AnyOps_match(const OperandTypes &...Ops) : Operands(Ops...) {} 1771 1772 // Operand matching works by recursively calling match_operands, matching the 1773 // operands left to right. The first version is called for each operand but 1774 // the last, for which the second version is called. The second version of 1775 // match_operands is also used to match each individual operand. 1776 template <int Idx, int Last> 1777 std::enable_if_t<Idx != Last, bool> match_operands(const Instruction *I) { 1778 return match_operands<Idx, Idx>(I) && match_operands<Idx + 1, Last>(I); 1779 } 1780 1781 template <int Idx, int Last> 1782 std::enable_if_t<Idx == Last, bool> match_operands(const Instruction *I) { 1783 return std::get<Idx>(Operands).match(I->getOperand(Idx)); 1784 } 1785 1786 template <typename OpTy> bool match(OpTy *V) { 1787 if (V->getValueID() == Value::InstructionVal + Opcode) { 1788 auto *I = cast<Instruction>(V); 1789 return I->getNumOperands() == sizeof...(OperandTypes) && 1790 match_operands<0, sizeof...(OperandTypes) - 1>(I); 1791 } 1792 return false; 1793 } 1794 }; 1795 1796 /// Matches SelectInst. 1797 template <typename Cond, typename LHS, typename RHS> 1798 inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select> 1799 m_Select(const Cond &C, const LHS &L, const RHS &R) { 1800 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R); 1801 } 1802 1803 /// This matches a select of two constants, e.g.: 1804 /// m_SelectCst<-1, 0>(m_Value(V)) 1805 template <int64_t L, int64_t R, typename Cond> 1806 inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>, 1807 Instruction::Select> 1808 m_SelectCst(const Cond &C) { 1809 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); 1810 } 1811 1812 /// Match Select(C, LHS, RHS) or Select(C, RHS, LHS) 1813 template <typename LHS, typename RHS> 1814 inline ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, true> 1815 m_c_Select(const LHS &L, const RHS &R) { 1816 return ThreeOps_match<decltype(m_Value()), LHS, RHS, Instruction::Select, 1817 true>(m_Value(), L, R); 1818 } 1819 1820 /// Matches FreezeInst. 1821 template <typename OpTy> 1822 inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) { 1823 return OneOps_match<OpTy, Instruction::Freeze>(Op); 1824 } 1825 1826 /// Matches InsertElementInst. 1827 template <typename Val_t, typename Elt_t, typename Idx_t> 1828 inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement> 1829 m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) { 1830 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>( 1831 Val, Elt, Idx); 1832 } 1833 1834 /// Matches ExtractElementInst. 1835 template <typename Val_t, typename Idx_t> 1836 inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement> 1837 m_ExtractElt(const Val_t &Val, const Idx_t &Idx) { 1838 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx); 1839 } 1840 1841 /// Matches shuffle. 1842 template <typename T0, typename T1, typename T2> struct Shuffle_match { 1843 T0 Op1; 1844 T1 Op2; 1845 T2 Mask; 1846 1847 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask) 1848 : Op1(Op1), Op2(Op2), Mask(Mask) {} 1849 1850 template <typename OpTy> bool match(OpTy *V) { 1851 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) { 1852 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && 1853 Mask.match(I->getShuffleMask()); 1854 } 1855 return false; 1856 } 1857 }; 1858 1859 struct m_Mask { 1860 ArrayRef<int> &MaskRef; 1861 m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} 1862 bool match(ArrayRef<int> Mask) { 1863 MaskRef = Mask; 1864 return true; 1865 } 1866 }; 1867 1868 struct m_ZeroMask { 1869 bool match(ArrayRef<int> Mask) { 1870 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; }); 1871 } 1872 }; 1873 1874 struct m_SpecificMask { 1875 ArrayRef<int> Val; 1876 m_SpecificMask(ArrayRef<int> Val) : Val(Val) {} 1877 bool match(ArrayRef<int> Mask) { return Val == Mask; } 1878 }; 1879 1880 struct m_SplatOrPoisonMask { 1881 int &SplatIndex; 1882 m_SplatOrPoisonMask(int &SplatIndex) : SplatIndex(SplatIndex) {} 1883 bool match(ArrayRef<int> Mask) { 1884 const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; }); 1885 if (First == Mask.end()) 1886 return false; 1887 SplatIndex = *First; 1888 return all_of(Mask, 1889 [First](int Elem) { return Elem == *First || Elem == -1; }); 1890 } 1891 }; 1892 1893 template <typename PointerOpTy, typename OffsetOpTy> struct PtrAdd_match { 1894 PointerOpTy PointerOp; 1895 OffsetOpTy OffsetOp; 1896 1897 PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) 1898 : PointerOp(PointerOp), OffsetOp(OffsetOp) {} 1899 1900 template <typename OpTy> bool match(OpTy *V) { 1901 auto *GEP = dyn_cast<GEPOperator>(V); 1902 return GEP && GEP->getSourceElementType()->isIntegerTy(8) && 1903 PointerOp.match(GEP->getPointerOperand()) && 1904 OffsetOp.match(GEP->idx_begin()->get()); 1905 } 1906 }; 1907 1908 /// Matches ShuffleVectorInst independently of mask value. 1909 template <typename V1_t, typename V2_t> 1910 inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector> 1911 m_Shuffle(const V1_t &v1, const V2_t &v2) { 1912 return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2); 1913 } 1914 1915 template <typename V1_t, typename V2_t, typename Mask_t> 1916 inline Shuffle_match<V1_t, V2_t, Mask_t> 1917 m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) { 1918 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask); 1919 } 1920 1921 /// Matches LoadInst. 1922 template <typename OpTy> 1923 inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) { 1924 return OneOps_match<OpTy, Instruction::Load>(Op); 1925 } 1926 1927 /// Matches StoreInst. 1928 template <typename ValueOpTy, typename PointerOpTy> 1929 inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store> 1930 m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) { 1931 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp, 1932 PointerOp); 1933 } 1934 1935 /// Matches GetElementPtrInst. 1936 template <typename... OperandTypes> 1937 inline auto m_GEP(const OperandTypes &...Ops) { 1938 return AnyOps_match<Instruction::GetElementPtr, OperandTypes...>(Ops...); 1939 } 1940 1941 /// Matches GEP with i8 source element type 1942 template <typename PointerOpTy, typename OffsetOpTy> 1943 inline PtrAdd_match<PointerOpTy, OffsetOpTy> 1944 m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) { 1945 return PtrAdd_match<PointerOpTy, OffsetOpTy>(PointerOp, OffsetOp); 1946 } 1947 1948 //===----------------------------------------------------------------------===// 1949 // Matchers for CastInst classes 1950 // 1951 1952 template <typename Op_t, unsigned Opcode> struct CastOperator_match { 1953 Op_t Op; 1954 1955 CastOperator_match(const Op_t &OpMatch) : Op(OpMatch) {} 1956 1957 template <typename OpTy> bool match(OpTy *V) { 1958 if (auto *O = dyn_cast<Operator>(V)) 1959 return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); 1960 return false; 1961 } 1962 }; 1963 1964 template <typename Op_t, typename Class> struct CastInst_match { 1965 Op_t Op; 1966 1967 CastInst_match(const Op_t &OpMatch) : Op(OpMatch) {} 1968 1969 template <typename OpTy> bool match(OpTy *V) { 1970 if (auto *I = dyn_cast<Class>(V)) 1971 return Op.match(I->getOperand(0)); 1972 return false; 1973 } 1974 }; 1975 1976 template <typename Op_t> struct PtrToIntSameSize_match { 1977 const DataLayout &DL; 1978 Op_t Op; 1979 1980 PtrToIntSameSize_match(const DataLayout &DL, const Op_t &OpMatch) 1981 : DL(DL), Op(OpMatch) {} 1982 1983 template <typename OpTy> bool match(OpTy *V) { 1984 if (auto *O = dyn_cast<Operator>(V)) 1985 return O->getOpcode() == Instruction::PtrToInt && 1986 DL.getTypeSizeInBits(O->getType()) == 1987 DL.getTypeSizeInBits(O->getOperand(0)->getType()) && 1988 Op.match(O->getOperand(0)); 1989 return false; 1990 } 1991 }; 1992 1993 template <typename Op_t> struct NNegZExt_match { 1994 Op_t Op; 1995 1996 NNegZExt_match(const Op_t &OpMatch) : Op(OpMatch) {} 1997 1998 template <typename OpTy> bool match(OpTy *V) { 1999 if (auto *I = dyn_cast<ZExtInst>(V)) 2000 return I->hasNonNeg() && Op.match(I->getOperand(0)); 2001 return false; 2002 } 2003 }; 2004 2005 template <typename Op_t, unsigned WrapFlags = 0> struct NoWrapTrunc_match { 2006 Op_t Op; 2007 2008 NoWrapTrunc_match(const Op_t &OpMatch) : Op(OpMatch) {} 2009 2010 template <typename OpTy> bool match(OpTy *V) { 2011 if (auto *I = dyn_cast<TruncInst>(V)) 2012 return (I->getNoWrapKind() & WrapFlags) == WrapFlags && 2013 Op.match(I->getOperand(0)); 2014 return false; 2015 } 2016 }; 2017 2018 /// Matches BitCast. 2019 template <typename OpTy> 2020 inline CastOperator_match<OpTy, Instruction::BitCast> 2021 m_BitCast(const OpTy &Op) { 2022 return CastOperator_match<OpTy, Instruction::BitCast>(Op); 2023 } 2024 2025 template <typename Op_t> struct ElementWiseBitCast_match { 2026 Op_t Op; 2027 2028 ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {} 2029 2030 template <typename OpTy> bool match(OpTy *V) { 2031 auto *I = dyn_cast<BitCastInst>(V); 2032 if (!I) 2033 return false; 2034 Type *SrcType = I->getSrcTy(); 2035 Type *DstType = I->getType(); 2036 // Make sure the bitcast doesn't change between scalar and vector and 2037 // doesn't change the number of vector elements. 2038 if (SrcType->isVectorTy() != DstType->isVectorTy()) 2039 return false; 2040 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcType); 2041 SrcVecTy && SrcVecTy->getElementCount() != 2042 cast<VectorType>(DstType)->getElementCount()) 2043 return false; 2044 return Op.match(I->getOperand(0)); 2045 } 2046 }; 2047 2048 template <typename OpTy> 2049 inline ElementWiseBitCast_match<OpTy> m_ElementWiseBitCast(const OpTy &Op) { 2050 return ElementWiseBitCast_match<OpTy>(Op); 2051 } 2052 2053 /// Matches PtrToInt. 2054 template <typename OpTy> 2055 inline CastOperator_match<OpTy, Instruction::PtrToInt> 2056 m_PtrToInt(const OpTy &Op) { 2057 return CastOperator_match<OpTy, Instruction::PtrToInt>(Op); 2058 } 2059 2060 template <typename OpTy> 2061 inline PtrToIntSameSize_match<OpTy> m_PtrToIntSameSize(const DataLayout &DL, 2062 const OpTy &Op) { 2063 return PtrToIntSameSize_match<OpTy>(DL, Op); 2064 } 2065 2066 /// Matches IntToPtr. 2067 template <typename OpTy> 2068 inline CastOperator_match<OpTy, Instruction::IntToPtr> 2069 m_IntToPtr(const OpTy &Op) { 2070 return CastOperator_match<OpTy, Instruction::IntToPtr>(Op); 2071 } 2072 2073 /// Matches Trunc. 2074 template <typename OpTy> 2075 inline CastInst_match<OpTy, TruncInst> m_Trunc(const OpTy &Op) { 2076 return CastInst_match<OpTy, TruncInst>(Op); 2077 } 2078 2079 /// Matches trunc nuw. 2080 template <typename OpTy> 2081 inline NoWrapTrunc_match<OpTy, TruncInst::NoUnsignedWrap> 2082 m_NUWTrunc(const OpTy &Op) { 2083 return NoWrapTrunc_match<OpTy, TruncInst::NoUnsignedWrap>(Op); 2084 } 2085 2086 /// Matches trunc nsw. 2087 template <typename OpTy> 2088 inline NoWrapTrunc_match<OpTy, TruncInst::NoSignedWrap> 2089 m_NSWTrunc(const OpTy &Op) { 2090 return NoWrapTrunc_match<OpTy, TruncInst::NoSignedWrap>(Op); 2091 } 2092 2093 template <typename OpTy> 2094 inline match_combine_or<CastInst_match<OpTy, TruncInst>, OpTy> 2095 m_TruncOrSelf(const OpTy &Op) { 2096 return m_CombineOr(m_Trunc(Op), Op); 2097 } 2098 2099 /// Matches SExt. 2100 template <typename OpTy> 2101 inline CastInst_match<OpTy, SExtInst> m_SExt(const OpTy &Op) { 2102 return CastInst_match<OpTy, SExtInst>(Op); 2103 } 2104 2105 /// Matches ZExt. 2106 template <typename OpTy> 2107 inline CastInst_match<OpTy, ZExtInst> m_ZExt(const OpTy &Op) { 2108 return CastInst_match<OpTy, ZExtInst>(Op); 2109 } 2110 2111 template <typename OpTy> 2112 inline NNegZExt_match<OpTy> m_NNegZExt(const OpTy &Op) { 2113 return NNegZExt_match<OpTy>(Op); 2114 } 2115 2116 template <typename OpTy> 2117 inline match_combine_or<CastInst_match<OpTy, ZExtInst>, OpTy> 2118 m_ZExtOrSelf(const OpTy &Op) { 2119 return m_CombineOr(m_ZExt(Op), Op); 2120 } 2121 2122 template <typename OpTy> 2123 inline match_combine_or<CastInst_match<OpTy, SExtInst>, OpTy> 2124 m_SExtOrSelf(const OpTy &Op) { 2125 return m_CombineOr(m_SExt(Op), Op); 2126 } 2127 2128 /// Match either "sext" or "zext nneg". 2129 template <typename OpTy> 2130 inline match_combine_or<CastInst_match<OpTy, SExtInst>, NNegZExt_match<OpTy>> 2131 m_SExtLike(const OpTy &Op) { 2132 return m_CombineOr(m_SExt(Op), m_NNegZExt(Op)); 2133 } 2134 2135 template <typename OpTy> 2136 inline match_combine_or<CastInst_match<OpTy, ZExtInst>, 2137 CastInst_match<OpTy, SExtInst>> 2138 m_ZExtOrSExt(const OpTy &Op) { 2139 return m_CombineOr(m_ZExt(Op), m_SExt(Op)); 2140 } 2141 2142 template <typename OpTy> 2143 inline match_combine_or<match_combine_or<CastInst_match<OpTy, ZExtInst>, 2144 CastInst_match<OpTy, SExtInst>>, 2145 OpTy> 2146 m_ZExtOrSExtOrSelf(const OpTy &Op) { 2147 return m_CombineOr(m_ZExtOrSExt(Op), Op); 2148 } 2149 2150 template <typename OpTy> 2151 inline CastInst_match<OpTy, UIToFPInst> m_UIToFP(const OpTy &Op) { 2152 return CastInst_match<OpTy, UIToFPInst>(Op); 2153 } 2154 2155 template <typename OpTy> 2156 inline CastInst_match<OpTy, SIToFPInst> m_SIToFP(const OpTy &Op) { 2157 return CastInst_match<OpTy, SIToFPInst>(Op); 2158 } 2159 2160 template <typename OpTy> 2161 inline CastInst_match<OpTy, FPToUIInst> m_FPToUI(const OpTy &Op) { 2162 return CastInst_match<OpTy, FPToUIInst>(Op); 2163 } 2164 2165 template <typename OpTy> 2166 inline CastInst_match<OpTy, FPToSIInst> m_FPToSI(const OpTy &Op) { 2167 return CastInst_match<OpTy, FPToSIInst>(Op); 2168 } 2169 2170 template <typename OpTy> 2171 inline CastInst_match<OpTy, FPTruncInst> m_FPTrunc(const OpTy &Op) { 2172 return CastInst_match<OpTy, FPTruncInst>(Op); 2173 } 2174 2175 template <typename OpTy> 2176 inline CastInst_match<OpTy, FPExtInst> m_FPExt(const OpTy &Op) { 2177 return CastInst_match<OpTy, FPExtInst>(Op); 2178 } 2179 2180 //===----------------------------------------------------------------------===// 2181 // Matchers for control flow. 2182 // 2183 2184 struct br_match { 2185 BasicBlock *&Succ; 2186 2187 br_match(BasicBlock *&Succ) : Succ(Succ) {} 2188 2189 template <typename OpTy> bool match(OpTy *V) { 2190 if (auto *BI = dyn_cast<BranchInst>(V)) 2191 if (BI->isUnconditional()) { 2192 Succ = BI->getSuccessor(0); 2193 return true; 2194 } 2195 return false; 2196 } 2197 }; 2198 2199 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } 2200 2201 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> 2202 struct brc_match { 2203 Cond_t Cond; 2204 TrueBlock_t T; 2205 FalseBlock_t F; 2206 2207 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f) 2208 : Cond(C), T(t), F(f) {} 2209 2210 template <typename OpTy> bool match(OpTy *V) { 2211 if (auto *BI = dyn_cast<BranchInst>(V)) 2212 if (BI->isConditional() && Cond.match(BI->getCondition())) 2213 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1)); 2214 return false; 2215 } 2216 }; 2217 2218 template <typename Cond_t> 2219 inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>> 2220 m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { 2221 return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>( 2222 C, m_BasicBlock(T), m_BasicBlock(F)); 2223 } 2224 2225 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> 2226 inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t> 2227 m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) { 2228 return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F); 2229 } 2230 2231 //===----------------------------------------------------------------------===// 2232 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). 2233 // 2234 2235 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, 2236 bool Commutable = false> 2237 struct MaxMin_match { 2238 using PredType = Pred_t; 2239 LHS_t L; 2240 RHS_t R; 2241 2242 // The evaluation order is always stable, regardless of Commutability. 2243 // The LHS is always matched first. 2244 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 2245 2246 template <typename OpTy> bool match(OpTy *V) { 2247 if (auto *II = dyn_cast<IntrinsicInst>(V)) { 2248 Intrinsic::ID IID = II->getIntrinsicID(); 2249 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) || 2250 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) || 2251 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) || 2252 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) { 2253 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1); 2254 return (L.match(LHS) && R.match(RHS)) || 2255 (Commutable && L.match(RHS) && R.match(LHS)); 2256 } 2257 } 2258 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". 2259 auto *SI = dyn_cast<SelectInst>(V); 2260 if (!SI) 2261 return false; 2262 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); 2263 if (!Cmp) 2264 return false; 2265 // At this point we have a select conditioned on a comparison. Check that 2266 // it is the values returned by the select that are being compared. 2267 auto *TrueVal = SI->getTrueValue(); 2268 auto *FalseVal = SI->getFalseValue(); 2269 auto *LHS = Cmp->getOperand(0); 2270 auto *RHS = Cmp->getOperand(1); 2271 if ((TrueVal != LHS || FalseVal != RHS) && 2272 (TrueVal != RHS || FalseVal != LHS)) 2273 return false; 2274 typename CmpInst_t::Predicate Pred = 2275 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); 2276 // Does "(x pred y) ? x : y" represent the desired max/min operation? 2277 if (!Pred_t::match(Pred)) 2278 return false; 2279 // It does! Bind the operands. 2280 return (L.match(LHS) && R.match(RHS)) || 2281 (Commutable && L.match(RHS) && R.match(LHS)); 2282 } 2283 }; 2284 2285 /// Helper class for identifying signed max predicates. 2286 struct smax_pred_ty { 2287 static bool match(ICmpInst::Predicate Pred) { 2288 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; 2289 } 2290 }; 2291 2292 /// Helper class for identifying signed min predicates. 2293 struct smin_pred_ty { 2294 static bool match(ICmpInst::Predicate Pred) { 2295 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; 2296 } 2297 }; 2298 2299 /// Helper class for identifying unsigned max predicates. 2300 struct umax_pred_ty { 2301 static bool match(ICmpInst::Predicate Pred) { 2302 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; 2303 } 2304 }; 2305 2306 /// Helper class for identifying unsigned min predicates. 2307 struct umin_pred_ty { 2308 static bool match(ICmpInst::Predicate Pred) { 2309 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; 2310 } 2311 }; 2312 2313 /// Helper class for identifying ordered max predicates. 2314 struct ofmax_pred_ty { 2315 static bool match(FCmpInst::Predicate Pred) { 2316 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; 2317 } 2318 }; 2319 2320 /// Helper class for identifying ordered min predicates. 2321 struct ofmin_pred_ty { 2322 static bool match(FCmpInst::Predicate Pred) { 2323 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; 2324 } 2325 }; 2326 2327 /// Helper class for identifying unordered max predicates. 2328 struct ufmax_pred_ty { 2329 static bool match(FCmpInst::Predicate Pred) { 2330 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; 2331 } 2332 }; 2333 2334 /// Helper class for identifying unordered min predicates. 2335 struct ufmin_pred_ty { 2336 static bool match(FCmpInst::Predicate Pred) { 2337 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; 2338 } 2339 }; 2340 2341 template <typename LHS, typename RHS> 2342 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, 2343 const RHS &R) { 2344 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); 2345 } 2346 2347 template <typename LHS, typename RHS> 2348 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, 2349 const RHS &R) { 2350 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); 2351 } 2352 2353 template <typename LHS, typename RHS> 2354 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, 2355 const RHS &R) { 2356 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); 2357 } 2358 2359 template <typename LHS, typename RHS> 2360 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, 2361 const RHS &R) { 2362 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); 2363 } 2364 2365 template <typename LHS, typename RHS> 2366 inline match_combine_or< 2367 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>, 2368 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>, 2369 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>, 2370 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>> 2371 m_MaxOrMin(const LHS &L, const RHS &R) { 2372 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)), 2373 m_CombineOr(m_UMax(L, R), m_UMin(L, R))); 2374 } 2375 2376 /// Match an 'ordered' floating point maximum function. 2377 /// Floating point has one special value 'NaN'. Therefore, there is no total 2378 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2379 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 2380 /// semantics. In the presence of 'NaN' we have to preserve the original 2381 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. 2382 /// 2383 /// max(L, R) iff L and R are not NaN 2384 /// m_OrdFMax(L, R) = R iff L or R are NaN 2385 template <typename LHS, typename RHS> 2386 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, 2387 const RHS &R) { 2388 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); 2389 } 2390 2391 /// Match an 'ordered' floating point minimum function. 2392 /// Floating point has one special value 'NaN'. Therefore, there is no total 2393 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2394 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 2395 /// semantics. In the presence of 'NaN' we have to preserve the original 2396 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. 2397 /// 2398 /// min(L, R) iff L and R are not NaN 2399 /// m_OrdFMin(L, R) = R iff L or R are NaN 2400 template <typename LHS, typename RHS> 2401 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, 2402 const RHS &R) { 2403 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); 2404 } 2405 2406 /// Match an 'unordered' floating point maximum function. 2407 /// Floating point has one special value 'NaN'. Therefore, there is no total 2408 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2409 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 2410 /// semantics. In the presence of 'NaN' we have to preserve the original 2411 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. 2412 /// 2413 /// max(L, R) iff L and R are not NaN 2414 /// m_UnordFMax(L, R) = L iff L or R are NaN 2415 template <typename LHS, typename RHS> 2416 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> 2417 m_UnordFMax(const LHS &L, const RHS &R) { 2418 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); 2419 } 2420 2421 /// Match an 'unordered' floating point minimum function. 2422 /// Floating point has one special value 'NaN'. Therefore, there is no total 2423 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2424 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 2425 /// semantics. In the presence of 'NaN' we have to preserve the original 2426 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. 2427 /// 2428 /// min(L, R) iff L and R are not NaN 2429 /// m_UnordFMin(L, R) = L iff L or R are NaN 2430 template <typename LHS, typename RHS> 2431 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> 2432 m_UnordFMin(const LHS &L, const RHS &R) { 2433 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); 2434 } 2435 2436 /// Match an 'ordered' or 'unordered' floating point maximum function. 2437 /// Floating point has one special value 'NaN'. Therefore, there is no total 2438 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2439 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 2440 /// semantics. 2441 template <typename LHS, typename RHS> 2442 inline match_combine_or<MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>, 2443 MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>> 2444 m_OrdOrUnordFMax(const LHS &L, const RHS &R) { 2445 return m_CombineOr(MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R), 2446 MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R)); 2447 } 2448 2449 /// Match an 'ordered' or 'unordered' floating point minimum function. 2450 /// Floating point has one special value 'NaN'. Therefore, there is no total 2451 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2452 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 2453 /// semantics. 2454 template <typename LHS, typename RHS> 2455 inline match_combine_or<MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>, 2456 MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>> 2457 m_OrdOrUnordFMin(const LHS &L, const RHS &R) { 2458 return m_CombineOr(MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R), 2459 MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R)); 2460 } 2461 2462 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'. 2463 /// NOTE: we first match the 'Not' (by matching '-1'), 2464 /// and only then match the inner matcher! 2465 template <typename ValTy> 2466 inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true> 2467 m_Not(const ValTy &V) { 2468 return m_c_Xor(m_AllOnes(), V); 2469 } 2470 2471 template <typename ValTy> 2472 inline BinaryOp_match<cst_pred_ty<is_all_ones, false>, ValTy, Instruction::Xor, 2473 true> 2474 m_NotForbidPoison(const ValTy &V) { 2475 return m_c_Xor(m_AllOnesForbidPoison(), V); 2476 } 2477 2478 //===----------------------------------------------------------------------===// 2479 // Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b 2480 // Note that S might be matched to other instructions than AddInst. 2481 // 2482 2483 template <typename LHS_t, typename RHS_t, typename Sum_t> 2484 struct UAddWithOverflow_match { 2485 LHS_t L; 2486 RHS_t R; 2487 Sum_t S; 2488 2489 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) 2490 : L(L), R(R), S(S) {} 2491 2492 template <typename OpTy> bool match(OpTy *V) { 2493 Value *ICmpLHS, *ICmpRHS; 2494 CmpPredicate Pred; 2495 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) 2496 return false; 2497 2498 Value *AddLHS, *AddRHS; 2499 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); 2500 2501 // (a + b) u< a, (a + b) u< b 2502 if (Pred == ICmpInst::ICMP_ULT) 2503 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) 2504 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); 2505 2506 // a >u (a + b), b >u (a + b) 2507 if (Pred == ICmpInst::ICMP_UGT) 2508 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) 2509 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); 2510 2511 Value *Op1; 2512 auto XorExpr = m_OneUse(m_Not(m_Value(Op1))); 2513 // (~a) <u b 2514 if (Pred == ICmpInst::ICMP_ULT) { 2515 if (XorExpr.match(ICmpLHS)) 2516 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS); 2517 } 2518 // b > u (~a) 2519 if (Pred == ICmpInst::ICMP_UGT) { 2520 if (XorExpr.match(ICmpRHS)) 2521 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS); 2522 } 2523 2524 // Match special-case for increment-by-1. 2525 if (Pred == ICmpInst::ICMP_EQ) { 2526 // (a + 1) == 0 2527 // (1 + a) == 0 2528 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && 2529 (m_One().match(AddLHS) || m_One().match(AddRHS))) 2530 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); 2531 // 0 == (a + 1) 2532 // 0 == (1 + a) 2533 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && 2534 (m_One().match(AddLHS) || m_One().match(AddRHS))) 2535 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); 2536 } 2537 2538 return false; 2539 } 2540 }; 2541 2542 /// Match an icmp instruction checking for unsigned overflow on addition. 2543 /// 2544 /// S is matched to the addition whose result is being checked for overflow, and 2545 /// L and R are matched to the LHS and RHS of S. 2546 template <typename LHS_t, typename RHS_t, typename Sum_t> 2547 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> 2548 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { 2549 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); 2550 } 2551 2552 template <typename Opnd_t> struct Argument_match { 2553 unsigned OpI; 2554 Opnd_t Val; 2555 2556 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} 2557 2558 template <typename OpTy> bool match(OpTy *V) { 2559 // FIXME: Should likely be switched to use `CallBase`. 2560 if (const auto *CI = dyn_cast<CallInst>(V)) 2561 return Val.match(CI->getArgOperand(OpI)); 2562 return false; 2563 } 2564 }; 2565 2566 /// Match an argument. 2567 template <unsigned OpI, typename Opnd_t> 2568 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { 2569 return Argument_match<Opnd_t>(OpI, Op); 2570 } 2571 2572 /// Intrinsic matchers. 2573 struct IntrinsicID_match { 2574 unsigned ID; 2575 2576 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} 2577 2578 template <typename OpTy> bool match(OpTy *V) { 2579 if (const auto *CI = dyn_cast<CallInst>(V)) 2580 if (const auto *F = CI->getCalledFunction()) 2581 return F->getIntrinsicID() == ID; 2582 return false; 2583 } 2584 }; 2585 2586 /// Intrinsic matches are combinations of ID matchers, and argument 2587 /// matchers. Higher arity matcher are defined recursively in terms of and-ing 2588 /// them with lower arity matchers. Here's some convenient typedefs for up to 2589 /// several arguments, and more can be added as needed 2590 template <typename T0 = void, typename T1 = void, typename T2 = void, 2591 typename T3 = void, typename T4 = void, typename T5 = void, 2592 typename T6 = void, typename T7 = void, typename T8 = void, 2593 typename T9 = void, typename T10 = void> 2594 struct m_Intrinsic_Ty; 2595 template <typename T0> struct m_Intrinsic_Ty<T0> { 2596 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; 2597 }; 2598 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { 2599 using Ty = 2600 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; 2601 }; 2602 template <typename T0, typename T1, typename T2> 2603 struct m_Intrinsic_Ty<T0, T1, T2> { 2604 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, 2605 Argument_match<T2>>; 2606 }; 2607 template <typename T0, typename T1, typename T2, typename T3> 2608 struct m_Intrinsic_Ty<T0, T1, T2, T3> { 2609 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, 2610 Argument_match<T3>>; 2611 }; 2612 2613 template <typename T0, typename T1, typename T2, typename T3, typename T4> 2614 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> { 2615 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty, 2616 Argument_match<T4>>; 2617 }; 2618 2619 template <typename T0, typename T1, typename T2, typename T3, typename T4, 2620 typename T5> 2621 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> { 2622 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty, 2623 Argument_match<T5>>; 2624 }; 2625 2626 /// Match intrinsic calls like this: 2627 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) 2628 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { 2629 return IntrinsicID_match(IntrID); 2630 } 2631 2632 /// Matches MaskedLoad Intrinsic. 2633 template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> 2634 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty 2635 m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, 2636 const Opnd3 &Op3) { 2637 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3); 2638 } 2639 2640 /// Matches MaskedGather Intrinsic. 2641 template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> 2642 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty 2643 m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, 2644 const Opnd3 &Op3) { 2645 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3); 2646 } 2647 2648 template <Intrinsic::ID IntrID, typename T0> 2649 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { 2650 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); 2651 } 2652 2653 template <Intrinsic::ID IntrID, typename T0, typename T1> 2654 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, 2655 const T1 &Op1) { 2656 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); 2657 } 2658 2659 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> 2660 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty 2661 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { 2662 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); 2663 } 2664 2665 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 2666 typename T3> 2667 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty 2668 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { 2669 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); 2670 } 2671 2672 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 2673 typename T3, typename T4> 2674 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty 2675 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, 2676 const T4 &Op4) { 2677 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3), 2678 m_Argument<4>(Op4)); 2679 } 2680 2681 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 2682 typename T3, typename T4, typename T5> 2683 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty 2684 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, 2685 const T4 &Op4, const T5 &Op5) { 2686 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4), 2687 m_Argument<5>(Op5)); 2688 } 2689 2690 // Helper intrinsic matching specializations. 2691 template <typename Opnd0> 2692 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { 2693 return m_Intrinsic<Intrinsic::bitreverse>(Op0); 2694 } 2695 2696 template <typename Opnd0> 2697 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { 2698 return m_Intrinsic<Intrinsic::bswap>(Op0); 2699 } 2700 2701 template <typename Opnd0> 2702 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { 2703 return m_Intrinsic<Intrinsic::fabs>(Op0); 2704 } 2705 2706 template <typename Opnd0> 2707 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { 2708 return m_Intrinsic<Intrinsic::canonicalize>(Op0); 2709 } 2710 2711 template <typename Opnd0, typename Opnd1> 2712 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, 2713 const Opnd1 &Op1) { 2714 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); 2715 } 2716 2717 template <typename Opnd0, typename Opnd1> 2718 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, 2719 const Opnd1 &Op1) { 2720 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); 2721 } 2722 2723 template <typename Opnd0, typename Opnd1, typename Opnd2> 2724 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty 2725 m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { 2726 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2); 2727 } 2728 2729 template <typename Opnd0, typename Opnd1, typename Opnd2> 2730 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty 2731 m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { 2732 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2); 2733 } 2734 2735 template <typename Opnd0> 2736 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) { 2737 return m_Intrinsic<Intrinsic::sqrt>(Op0); 2738 } 2739 2740 template <typename Opnd0, typename Opnd1> 2741 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0, 2742 const Opnd1 &Op1) { 2743 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1); 2744 } 2745 2746 template <typename Opnd0> 2747 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) { 2748 return m_Intrinsic<Intrinsic::vector_reverse>(Op0); 2749 } 2750 2751 //===----------------------------------------------------------------------===// 2752 // Matchers for two-operands operators with the operators in either order 2753 // 2754 2755 /// Matches a BinaryOperator with LHS and RHS in either order. 2756 template <typename LHS, typename RHS> 2757 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { 2758 return AnyBinaryOp_match<LHS, RHS, true>(L, R); 2759 } 2760 2761 /// Matches an ICmp with a predicate over LHS and RHS in either order. 2762 /// Swaps the predicate if operands are commuted. 2763 template <typename LHS, typename RHS> 2764 inline CmpClass_match<LHS, RHS, ICmpInst, true> 2765 m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R) { 2766 return CmpClass_match<LHS, RHS, ICmpInst, true>(Pred, L, R); 2767 } 2768 2769 template <typename LHS, typename RHS> 2770 inline CmpClass_match<LHS, RHS, ICmpInst, true> m_c_ICmp(const LHS &L, 2771 const RHS &R) { 2772 return CmpClass_match<LHS, RHS, ICmpInst, true>(L, R); 2773 } 2774 2775 /// Matches a specific opcode with LHS and RHS in either order. 2776 template <typename LHS, typename RHS> 2777 inline SpecificBinaryOp_match<LHS, RHS, true> 2778 m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) { 2779 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R); 2780 } 2781 2782 /// Matches a Add with LHS and RHS in either order. 2783 template <typename LHS, typename RHS> 2784 inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, 2785 const RHS &R) { 2786 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); 2787 } 2788 2789 /// Matches a Mul with LHS and RHS in either order. 2790 template <typename LHS, typename RHS> 2791 inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, 2792 const RHS &R) { 2793 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); 2794 } 2795 2796 /// Matches an And with LHS and RHS in either order. 2797 template <typename LHS, typename RHS> 2798 inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, 2799 const RHS &R) { 2800 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); 2801 } 2802 2803 /// Matches an Or with LHS and RHS in either order. 2804 template <typename LHS, typename RHS> 2805 inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, 2806 const RHS &R) { 2807 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); 2808 } 2809 2810 /// Matches an Xor with LHS and RHS in either order. 2811 template <typename LHS, typename RHS> 2812 inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, 2813 const RHS &R) { 2814 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); 2815 } 2816 2817 /// Matches a 'Neg' as 'sub 0, V'. 2818 template <typename ValTy> 2819 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> 2820 m_Neg(const ValTy &V) { 2821 return m_Sub(m_ZeroInt(), V); 2822 } 2823 2824 /// Matches a 'Neg' as 'sub nsw 0, V'. 2825 template <typename ValTy> 2826 inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, 2827 Instruction::Sub, 2828 OverflowingBinaryOperator::NoSignedWrap> 2829 m_NSWNeg(const ValTy &V) { 2830 return m_NSWSub(m_ZeroInt(), V); 2831 } 2832 2833 /// Matches an SMin with LHS and RHS in either order. 2834 template <typename LHS, typename RHS> 2835 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> 2836 m_c_SMin(const LHS &L, const RHS &R) { 2837 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); 2838 } 2839 /// Matches an SMax with LHS and RHS in either order. 2840 template <typename LHS, typename RHS> 2841 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> 2842 m_c_SMax(const LHS &L, const RHS &R) { 2843 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); 2844 } 2845 /// Matches a UMin with LHS and RHS in either order. 2846 template <typename LHS, typename RHS> 2847 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> 2848 m_c_UMin(const LHS &L, const RHS &R) { 2849 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); 2850 } 2851 /// Matches a UMax with LHS and RHS in either order. 2852 template <typename LHS, typename RHS> 2853 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> 2854 m_c_UMax(const LHS &L, const RHS &R) { 2855 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); 2856 } 2857 2858 template <typename LHS, typename RHS> 2859 inline match_combine_or< 2860 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>, 2861 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>, 2862 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>, 2863 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>> 2864 m_c_MaxOrMin(const LHS &L, const RHS &R) { 2865 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)), 2866 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R))); 2867 } 2868 2869 template <Intrinsic::ID IntrID, typename T0, typename T1> 2870 inline match_combine_or<typename m_Intrinsic_Ty<T0, T1>::Ty, 2871 typename m_Intrinsic_Ty<T1, T0>::Ty> 2872 m_c_Intrinsic(const T0 &Op0, const T1 &Op1) { 2873 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1), 2874 m_Intrinsic<IntrID>(Op1, Op0)); 2875 } 2876 2877 /// Matches FAdd with LHS and RHS in either order. 2878 template <typename LHS, typename RHS> 2879 inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> 2880 m_c_FAdd(const LHS &L, const RHS &R) { 2881 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); 2882 } 2883 2884 /// Matches FMul with LHS and RHS in either order. 2885 template <typename LHS, typename RHS> 2886 inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> 2887 m_c_FMul(const LHS &L, const RHS &R) { 2888 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); 2889 } 2890 2891 template <typename Opnd_t> struct Signum_match { 2892 Opnd_t Val; 2893 Signum_match(const Opnd_t &V) : Val(V) {} 2894 2895 template <typename OpTy> bool match(OpTy *V) { 2896 unsigned TypeSize = V->getType()->getScalarSizeInBits(); 2897 if (TypeSize == 0) 2898 return false; 2899 2900 unsigned ShiftWidth = TypeSize - 1; 2901 Value *Op; 2902 2903 // This is the representation of signum we match: 2904 // 2905 // signum(x) == (x >> 63) | (-x >>u 63) 2906 // 2907 // An i1 value is its own signum, so it's correct to match 2908 // 2909 // signum(x) == (x >> 0) | (-x >>u 0) 2910 // 2911 // for i1 values. 2912 2913 auto LHS = m_AShr(m_Value(Op), m_SpecificInt(ShiftWidth)); 2914 auto RHS = m_LShr(m_Neg(m_Deferred(Op)), m_SpecificInt(ShiftWidth)); 2915 auto Signum = m_c_Or(LHS, RHS); 2916 2917 return Signum.match(V) && Val.match(Op); 2918 } 2919 }; 2920 2921 /// Matches a signum pattern. 2922 /// 2923 /// signum(x) = 2924 /// x > 0 -> 1 2925 /// x == 0 -> 0 2926 /// x < 0 -> -1 2927 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { 2928 return Signum_match<Val_t>(V); 2929 } 2930 2931 template <int Ind, typename Opnd_t> struct ExtractValue_match { 2932 Opnd_t Val; 2933 ExtractValue_match(const Opnd_t &V) : Val(V) {} 2934 2935 template <typename OpTy> bool match(OpTy *V) { 2936 if (auto *I = dyn_cast<ExtractValueInst>(V)) { 2937 // If Ind is -1, don't inspect indices 2938 if (Ind != -1 && 2939 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind)) 2940 return false; 2941 return Val.match(I->getAggregateOperand()); 2942 } 2943 return false; 2944 } 2945 }; 2946 2947 /// Match a single index ExtractValue instruction. 2948 /// For example m_ExtractValue<1>(...) 2949 template <int Ind, typename Val_t> 2950 inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) { 2951 return ExtractValue_match<Ind, Val_t>(V); 2952 } 2953 2954 /// Match an ExtractValue instruction with any index. 2955 /// For example m_ExtractValue(...) 2956 template <typename Val_t> 2957 inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) { 2958 return ExtractValue_match<-1, Val_t>(V); 2959 } 2960 2961 /// Matcher for a single index InsertValue instruction. 2962 template <int Ind, typename T0, typename T1> struct InsertValue_match { 2963 T0 Op0; 2964 T1 Op1; 2965 2966 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {} 2967 2968 template <typename OpTy> bool match(OpTy *V) { 2969 if (auto *I = dyn_cast<InsertValueInst>(V)) { 2970 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) && 2971 I->getNumIndices() == 1 && Ind == I->getIndices()[0]; 2972 } 2973 return false; 2974 } 2975 }; 2976 2977 /// Matches a single index InsertValue instruction. 2978 template <int Ind, typename Val_t, typename Elt_t> 2979 inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val, 2980 const Elt_t &Elt) { 2981 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt); 2982 } 2983 2984 /// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or 2985 /// the constant expression 2986 /// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>` 2987 /// under the right conditions determined by DataLayout. 2988 struct VScaleVal_match { 2989 template <typename ITy> bool match(ITy *V) { 2990 if (m_Intrinsic<Intrinsic::vscale>().match(V)) 2991 return true; 2992 2993 Value *Ptr; 2994 if (m_PtrToInt(m_Value(Ptr)).match(V)) { 2995 if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { 2996 auto *DerefTy = 2997 dyn_cast<ScalableVectorType>(GEP->getSourceElementType()); 2998 if (GEP->getNumIndices() == 1 && DerefTy && 2999 DerefTy->getElementType()->isIntegerTy(8) && 3000 m_Zero().match(GEP->getPointerOperand()) && 3001 m_SpecificInt(1).match(GEP->idx_begin()->get())) 3002 return true; 3003 } 3004 } 3005 3006 return false; 3007 } 3008 }; 3009 3010 inline VScaleVal_match m_VScale() { 3011 return VScaleVal_match(); 3012 } 3013 3014 template <typename Opnd0, typename Opnd1> 3015 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty 3016 m_Interleave2(const Opnd0 &Op0, const Opnd1 &Op1) { 3017 return m_Intrinsic<Intrinsic::vector_interleave2>(Op0, Op1); 3018 } 3019 3020 template <typename Opnd> 3021 inline typename m_Intrinsic_Ty<Opnd>::Ty m_Deinterleave2(const Opnd &Op) { 3022 return m_Intrinsic<Intrinsic::vector_deinterleave2>(Op); 3023 } 3024 3025 template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false> 3026 struct LogicalOp_match { 3027 LHS L; 3028 RHS R; 3029 3030 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {} 3031 3032 template <typename T> bool match(T *V) { 3033 auto *I = dyn_cast<Instruction>(V); 3034 if (!I || !I->getType()->isIntOrIntVectorTy(1)) 3035 return false; 3036 3037 if (I->getOpcode() == Opcode) { 3038 auto *Op0 = I->getOperand(0); 3039 auto *Op1 = I->getOperand(1); 3040 return (L.match(Op0) && R.match(Op1)) || 3041 (Commutable && L.match(Op1) && R.match(Op0)); 3042 } 3043 3044 if (auto *Select = dyn_cast<SelectInst>(I)) { 3045 auto *Cond = Select->getCondition(); 3046 auto *TVal = Select->getTrueValue(); 3047 auto *FVal = Select->getFalseValue(); 3048 3049 // Don't match a scalar select of bool vectors. 3050 // Transforms expect a single type for operands if this matches. 3051 if (Cond->getType() != Select->getType()) 3052 return false; 3053 3054 if (Opcode == Instruction::And) { 3055 auto *C = dyn_cast<Constant>(FVal); 3056 if (C && C->isNullValue()) 3057 return (L.match(Cond) && R.match(TVal)) || 3058 (Commutable && L.match(TVal) && R.match(Cond)); 3059 } else { 3060 assert(Opcode == Instruction::Or); 3061 auto *C = dyn_cast<Constant>(TVal); 3062 if (C && C->isOneValue()) 3063 return (L.match(Cond) && R.match(FVal)) || 3064 (Commutable && L.match(FVal) && R.match(Cond)); 3065 } 3066 } 3067 3068 return false; 3069 } 3070 }; 3071 3072 /// Matches L && R either in the form of L & R or L ? R : false. 3073 /// Note that the latter form is poison-blocking. 3074 template <typename LHS, typename RHS> 3075 inline LogicalOp_match<LHS, RHS, Instruction::And> m_LogicalAnd(const LHS &L, 3076 const RHS &R) { 3077 return LogicalOp_match<LHS, RHS, Instruction::And>(L, R); 3078 } 3079 3080 /// Matches L && R where L and R are arbitrary values. 3081 inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); } 3082 3083 /// Matches L && R with LHS and RHS in either order. 3084 template <typename LHS, typename RHS> 3085 inline LogicalOp_match<LHS, RHS, Instruction::And, true> 3086 m_c_LogicalAnd(const LHS &L, const RHS &R) { 3087 return LogicalOp_match<LHS, RHS, Instruction::And, true>(L, R); 3088 } 3089 3090 /// Matches L || R either in the form of L | R or L ? true : R. 3091 /// Note that the latter form is poison-blocking. 3092 template <typename LHS, typename RHS> 3093 inline LogicalOp_match<LHS, RHS, Instruction::Or> m_LogicalOr(const LHS &L, 3094 const RHS &R) { 3095 return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R); 3096 } 3097 3098 /// Matches L || R where L and R are arbitrary values. 3099 inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); } 3100 3101 /// Matches L || R with LHS and RHS in either order. 3102 template <typename LHS, typename RHS> 3103 inline LogicalOp_match<LHS, RHS, Instruction::Or, true> 3104 m_c_LogicalOr(const LHS &L, const RHS &R) { 3105 return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R); 3106 } 3107 3108 /// Matches either L && R or L || R, 3109 /// either one being in the either binary or logical form. 3110 /// Note that the latter form is poison-blocking. 3111 template <typename LHS, typename RHS, bool Commutable = false> 3112 inline auto m_LogicalOp(const LHS &L, const RHS &R) { 3113 return m_CombineOr( 3114 LogicalOp_match<LHS, RHS, Instruction::And, Commutable>(L, R), 3115 LogicalOp_match<LHS, RHS, Instruction::Or, Commutable>(L, R)); 3116 } 3117 3118 /// Matches either L && R or L || R where L and R are arbitrary values. 3119 inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); } 3120 3121 /// Matches either L && R or L || R with LHS and RHS in either order. 3122 template <typename LHS, typename RHS> 3123 inline auto m_c_LogicalOp(const LHS &L, const RHS &R) { 3124 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R); 3125 } 3126 3127 } // end namespace PatternMatch 3128 } // end namespace llvm 3129 3130 #endif // LLVM_IR_PATTERNMATCH_H 3131