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