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 "and" 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 /// Matches BitCast. 1660 template <typename OpTy> 1661 inline CastOperator_match<OpTy, Instruction::BitCast> 1662 m_BitCast(const OpTy &Op) { 1663 return CastOperator_match<OpTy, Instruction::BitCast>(Op); 1664 } 1665 1666 /// Matches PtrToInt. 1667 template <typename OpTy> 1668 inline CastOperator_match<OpTy, Instruction::PtrToInt> 1669 m_PtrToInt(const OpTy &Op) { 1670 return CastOperator_match<OpTy, Instruction::PtrToInt>(Op); 1671 } 1672 1673 template <typename OpTy> 1674 inline PtrToIntSameSize_match<OpTy> m_PtrToIntSameSize(const DataLayout &DL, 1675 const OpTy &Op) { 1676 return PtrToIntSameSize_match<OpTy>(DL, Op); 1677 } 1678 1679 /// Matches IntToPtr. 1680 template <typename OpTy> 1681 inline CastOperator_match<OpTy, Instruction::IntToPtr> 1682 m_IntToPtr(const OpTy &Op) { 1683 return CastOperator_match<OpTy, Instruction::IntToPtr>(Op); 1684 } 1685 1686 /// Matches Trunc. 1687 template <typename OpTy> 1688 inline CastOperator_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) { 1689 return CastOperator_match<OpTy, Instruction::Trunc>(Op); 1690 } 1691 1692 template <typename OpTy> 1693 inline match_combine_or<CastOperator_match<OpTy, Instruction::Trunc>, OpTy> 1694 m_TruncOrSelf(const OpTy &Op) { 1695 return m_CombineOr(m_Trunc(Op), Op); 1696 } 1697 1698 /// Matches SExt. 1699 template <typename OpTy> 1700 inline CastInst_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) { 1701 return CastInst_match<OpTy, Instruction::SExt>(Op); 1702 } 1703 1704 /// Matches ZExt. 1705 template <typename OpTy> 1706 inline CastInst_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) { 1707 return CastInst_match<OpTy, Instruction::ZExt>(Op); 1708 } 1709 1710 template <typename OpTy> 1711 inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>, OpTy> 1712 m_ZExtOrSelf(const OpTy &Op) { 1713 return m_CombineOr(m_ZExt(Op), Op); 1714 } 1715 1716 template <typename OpTy> 1717 inline match_combine_or<CastInst_match<OpTy, Instruction::SExt>, OpTy> 1718 m_SExtOrSelf(const OpTy &Op) { 1719 return m_CombineOr(m_SExt(Op), Op); 1720 } 1721 1722 template <typename OpTy> 1723 inline match_combine_or<CastInst_match<OpTy, Instruction::ZExt>, 1724 CastInst_match<OpTy, Instruction::SExt>> 1725 m_ZExtOrSExt(const OpTy &Op) { 1726 return m_CombineOr(m_ZExt(Op), m_SExt(Op)); 1727 } 1728 1729 template <typename OpTy> 1730 inline match_combine_or< 1731 match_combine_or<CastInst_match<OpTy, Instruction::ZExt>, 1732 CastInst_match<OpTy, Instruction::SExt>>, 1733 OpTy> 1734 m_ZExtOrSExtOrSelf(const OpTy &Op) { 1735 return m_CombineOr(m_ZExtOrSExt(Op), Op); 1736 } 1737 1738 template <typename OpTy> 1739 inline CastInst_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) { 1740 return CastInst_match<OpTy, Instruction::UIToFP>(Op); 1741 } 1742 1743 template <typename OpTy> 1744 inline CastInst_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) { 1745 return CastInst_match<OpTy, Instruction::SIToFP>(Op); 1746 } 1747 1748 template <typename OpTy> 1749 inline CastInst_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) { 1750 return CastInst_match<OpTy, Instruction::FPToUI>(Op); 1751 } 1752 1753 template <typename OpTy> 1754 inline CastInst_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) { 1755 return CastInst_match<OpTy, Instruction::FPToSI>(Op); 1756 } 1757 1758 template <typename OpTy> 1759 inline CastInst_match<OpTy, Instruction::FPTrunc> 1760 m_FPTrunc(const OpTy &Op) { 1761 return CastInst_match<OpTy, Instruction::FPTrunc>(Op); 1762 } 1763 1764 template <typename OpTy> 1765 inline CastInst_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) { 1766 return CastInst_match<OpTy, Instruction::FPExt>(Op); 1767 } 1768 1769 //===----------------------------------------------------------------------===// 1770 // Matchers for control flow. 1771 // 1772 1773 struct br_match { 1774 BasicBlock *&Succ; 1775 1776 br_match(BasicBlock *&Succ) : Succ(Succ) {} 1777 1778 template <typename OpTy> bool match(OpTy *V) { 1779 if (auto *BI = dyn_cast<BranchInst>(V)) 1780 if (BI->isUnconditional()) { 1781 Succ = BI->getSuccessor(0); 1782 return true; 1783 } 1784 return false; 1785 } 1786 }; 1787 1788 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } 1789 1790 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> 1791 struct brc_match { 1792 Cond_t Cond; 1793 TrueBlock_t T; 1794 FalseBlock_t F; 1795 1796 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f) 1797 : Cond(C), T(t), F(f) {} 1798 1799 template <typename OpTy> bool match(OpTy *V) { 1800 if (auto *BI = dyn_cast<BranchInst>(V)) 1801 if (BI->isConditional() && Cond.match(BI->getCondition())) 1802 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1)); 1803 return false; 1804 } 1805 }; 1806 1807 template <typename Cond_t> 1808 inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>> 1809 m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { 1810 return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>( 1811 C, m_BasicBlock(T), m_BasicBlock(F)); 1812 } 1813 1814 template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> 1815 inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t> 1816 m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) { 1817 return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F); 1818 } 1819 1820 //===----------------------------------------------------------------------===// 1821 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). 1822 // 1823 1824 template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, 1825 bool Commutable = false> 1826 struct MaxMin_match { 1827 using PredType = Pred_t; 1828 LHS_t L; 1829 RHS_t R; 1830 1831 // The evaluation order is always stable, regardless of Commutability. 1832 // The LHS is always matched first. 1833 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} 1834 1835 template <typename OpTy> bool match(OpTy *V) { 1836 if (auto *II = dyn_cast<IntrinsicInst>(V)) { 1837 Intrinsic::ID IID = II->getIntrinsicID(); 1838 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) || 1839 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) || 1840 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) || 1841 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) { 1842 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1); 1843 return (L.match(LHS) && R.match(RHS)) || 1844 (Commutable && L.match(RHS) && R.match(LHS)); 1845 } 1846 } 1847 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". 1848 auto *SI = dyn_cast<SelectInst>(V); 1849 if (!SI) 1850 return false; 1851 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); 1852 if (!Cmp) 1853 return false; 1854 // At this point we have a select conditioned on a comparison. Check that 1855 // it is the values returned by the select that are being compared. 1856 auto *TrueVal = SI->getTrueValue(); 1857 auto *FalseVal = SI->getFalseValue(); 1858 auto *LHS = Cmp->getOperand(0); 1859 auto *RHS = Cmp->getOperand(1); 1860 if ((TrueVal != LHS || FalseVal != RHS) && 1861 (TrueVal != RHS || FalseVal != LHS)) 1862 return false; 1863 typename CmpInst_t::Predicate Pred = 1864 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); 1865 // Does "(x pred y) ? x : y" represent the desired max/min operation? 1866 if (!Pred_t::match(Pred)) 1867 return false; 1868 // It does! Bind the operands. 1869 return (L.match(LHS) && R.match(RHS)) || 1870 (Commutable && L.match(RHS) && R.match(LHS)); 1871 } 1872 }; 1873 1874 /// Helper class for identifying signed max predicates. 1875 struct smax_pred_ty { 1876 static bool match(ICmpInst::Predicate Pred) { 1877 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; 1878 } 1879 }; 1880 1881 /// Helper class for identifying signed min predicates. 1882 struct smin_pred_ty { 1883 static bool match(ICmpInst::Predicate Pred) { 1884 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; 1885 } 1886 }; 1887 1888 /// Helper class for identifying unsigned max predicates. 1889 struct umax_pred_ty { 1890 static bool match(ICmpInst::Predicate Pred) { 1891 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; 1892 } 1893 }; 1894 1895 /// Helper class for identifying unsigned min predicates. 1896 struct umin_pred_ty { 1897 static bool match(ICmpInst::Predicate Pred) { 1898 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; 1899 } 1900 }; 1901 1902 /// Helper class for identifying ordered max predicates. 1903 struct ofmax_pred_ty { 1904 static bool match(FCmpInst::Predicate Pred) { 1905 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; 1906 } 1907 }; 1908 1909 /// Helper class for identifying ordered min predicates. 1910 struct ofmin_pred_ty { 1911 static bool match(FCmpInst::Predicate Pred) { 1912 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; 1913 } 1914 }; 1915 1916 /// Helper class for identifying unordered max predicates. 1917 struct ufmax_pred_ty { 1918 static bool match(FCmpInst::Predicate Pred) { 1919 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; 1920 } 1921 }; 1922 1923 /// Helper class for identifying unordered min predicates. 1924 struct ufmin_pred_ty { 1925 static bool match(FCmpInst::Predicate Pred) { 1926 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; 1927 } 1928 }; 1929 1930 template <typename LHS, typename RHS> 1931 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, 1932 const RHS &R) { 1933 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); 1934 } 1935 1936 template <typename LHS, typename RHS> 1937 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, 1938 const RHS &R) { 1939 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); 1940 } 1941 1942 template <typename LHS, typename RHS> 1943 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, 1944 const RHS &R) { 1945 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); 1946 } 1947 1948 template <typename LHS, typename RHS> 1949 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, 1950 const RHS &R) { 1951 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); 1952 } 1953 1954 template <typename LHS, typename RHS> 1955 inline match_combine_or< 1956 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>, 1957 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>, 1958 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>, 1959 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>> 1960 m_MaxOrMin(const LHS &L, const RHS &R) { 1961 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)), 1962 m_CombineOr(m_UMax(L, R), m_UMin(L, R))); 1963 } 1964 1965 /// Match an 'ordered' floating point maximum function. 1966 /// Floating point has one special value 'NaN'. Therefore, there is no total 1967 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1968 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 1969 /// semantics. In the presence of 'NaN' we have to preserve the original 1970 /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. 1971 /// 1972 /// max(L, R) iff L and R are not NaN 1973 /// m_OrdFMax(L, R) = R iff L or R are NaN 1974 template <typename LHS, typename RHS> 1975 inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, 1976 const RHS &R) { 1977 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); 1978 } 1979 1980 /// Match an 'ordered' floating point minimum function. 1981 /// Floating point has one special value 'NaN'. Therefore, there is no total 1982 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1983 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 1984 /// semantics. In the presence of 'NaN' we have to preserve the original 1985 /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. 1986 /// 1987 /// min(L, R) iff L and R are not NaN 1988 /// m_OrdFMin(L, R) = R iff L or R are NaN 1989 template <typename LHS, typename RHS> 1990 inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, 1991 const RHS &R) { 1992 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); 1993 } 1994 1995 /// Match an 'unordered' floating point maximum function. 1996 /// Floating point has one special value 'NaN'. Therefore, there is no total 1997 /// order. However, if we can ignore the 'NaN' value (for example, because of a 1998 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' 1999 /// semantics. In the presence of 'NaN' we have to preserve the original 2000 /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. 2001 /// 2002 /// max(L, R) iff L and R are not NaN 2003 /// m_UnordFMax(L, R) = L iff L or R are NaN 2004 template <typename LHS, typename RHS> 2005 inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> 2006 m_UnordFMax(const LHS &L, const RHS &R) { 2007 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); 2008 } 2009 2010 /// Match an 'unordered' floating point minimum function. 2011 /// Floating point has one special value 'NaN'. Therefore, there is no total 2012 /// order. However, if we can ignore the 'NaN' value (for example, because of a 2013 /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' 2014 /// semantics. In the presence of 'NaN' we have to preserve the original 2015 /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. 2016 /// 2017 /// min(L, R) iff L and R are not NaN 2018 /// m_UnordFMin(L, R) = L iff L or R are NaN 2019 template <typename LHS, typename RHS> 2020 inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> 2021 m_UnordFMin(const LHS &L, const RHS &R) { 2022 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); 2023 } 2024 2025 //===----------------------------------------------------------------------===// 2026 // Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b 2027 // Note that S might be matched to other instructions than AddInst. 2028 // 2029 2030 template <typename LHS_t, typename RHS_t, typename Sum_t> 2031 struct UAddWithOverflow_match { 2032 LHS_t L; 2033 RHS_t R; 2034 Sum_t S; 2035 2036 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) 2037 : L(L), R(R), S(S) {} 2038 2039 template <typename OpTy> bool match(OpTy *V) { 2040 Value *ICmpLHS, *ICmpRHS; 2041 ICmpInst::Predicate Pred; 2042 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) 2043 return false; 2044 2045 Value *AddLHS, *AddRHS; 2046 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); 2047 2048 // (a + b) u< a, (a + b) u< b 2049 if (Pred == ICmpInst::ICMP_ULT) 2050 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) 2051 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); 2052 2053 // a >u (a + b), b >u (a + b) 2054 if (Pred == ICmpInst::ICMP_UGT) 2055 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) 2056 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); 2057 2058 Value *Op1; 2059 auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes())); 2060 // (a ^ -1) <u b 2061 if (Pred == ICmpInst::ICMP_ULT) { 2062 if (XorExpr.match(ICmpLHS)) 2063 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS); 2064 } 2065 // b > u (a ^ -1) 2066 if (Pred == ICmpInst::ICMP_UGT) { 2067 if (XorExpr.match(ICmpRHS)) 2068 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS); 2069 } 2070 2071 // Match special-case for increment-by-1. 2072 if (Pred == ICmpInst::ICMP_EQ) { 2073 // (a + 1) == 0 2074 // (1 + a) == 0 2075 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && 2076 (m_One().match(AddLHS) || m_One().match(AddRHS))) 2077 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); 2078 // 0 == (a + 1) 2079 // 0 == (1 + a) 2080 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && 2081 (m_One().match(AddLHS) || m_One().match(AddRHS))) 2082 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); 2083 } 2084 2085 return false; 2086 } 2087 }; 2088 2089 /// Match an icmp instruction checking for unsigned overflow on addition. 2090 /// 2091 /// S is matched to the addition whose result is being checked for overflow, and 2092 /// L and R are matched to the LHS and RHS of S. 2093 template <typename LHS_t, typename RHS_t, typename Sum_t> 2094 UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> 2095 m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { 2096 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); 2097 } 2098 2099 template <typename Opnd_t> struct Argument_match { 2100 unsigned OpI; 2101 Opnd_t Val; 2102 2103 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} 2104 2105 template <typename OpTy> bool match(OpTy *V) { 2106 // FIXME: Should likely be switched to use `CallBase`. 2107 if (const auto *CI = dyn_cast<CallInst>(V)) 2108 return Val.match(CI->getArgOperand(OpI)); 2109 return false; 2110 } 2111 }; 2112 2113 /// Match an argument. 2114 template <unsigned OpI, typename Opnd_t> 2115 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { 2116 return Argument_match<Opnd_t>(OpI, Op); 2117 } 2118 2119 /// Intrinsic matchers. 2120 struct IntrinsicID_match { 2121 unsigned ID; 2122 2123 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} 2124 2125 template <typename OpTy> bool match(OpTy *V) { 2126 if (const auto *CI = dyn_cast<CallInst>(V)) 2127 if (const auto *F = CI->getCalledFunction()) 2128 return F->getIntrinsicID() == ID; 2129 return false; 2130 } 2131 }; 2132 2133 /// Intrinsic matches are combinations of ID matchers, and argument 2134 /// matchers. Higher arity matcher are defined recursively in terms of and-ing 2135 /// them with lower arity matchers. Here's some convenient typedefs for up to 2136 /// several arguments, and more can be added as needed 2137 template <typename T0 = void, typename T1 = void, typename T2 = void, 2138 typename T3 = void, typename T4 = void, typename T5 = void, 2139 typename T6 = void, typename T7 = void, typename T8 = void, 2140 typename T9 = void, typename T10 = void> 2141 struct m_Intrinsic_Ty; 2142 template <typename T0> struct m_Intrinsic_Ty<T0> { 2143 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; 2144 }; 2145 template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { 2146 using Ty = 2147 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; 2148 }; 2149 template <typename T0, typename T1, typename T2> 2150 struct m_Intrinsic_Ty<T0, T1, T2> { 2151 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, 2152 Argument_match<T2>>; 2153 }; 2154 template <typename T0, typename T1, typename T2, typename T3> 2155 struct m_Intrinsic_Ty<T0, T1, T2, T3> { 2156 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, 2157 Argument_match<T3>>; 2158 }; 2159 2160 template <typename T0, typename T1, typename T2, typename T3, typename T4> 2161 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> { 2162 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty, 2163 Argument_match<T4>>; 2164 }; 2165 2166 template <typename T0, typename T1, typename T2, typename T3, typename T4, 2167 typename T5> 2168 struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> { 2169 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty, 2170 Argument_match<T5>>; 2171 }; 2172 2173 /// Match intrinsic calls like this: 2174 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) 2175 template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { 2176 return IntrinsicID_match(IntrID); 2177 } 2178 2179 /// Matches MaskedLoad Intrinsic. 2180 template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> 2181 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty 2182 m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, 2183 const Opnd3 &Op3) { 2184 return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3); 2185 } 2186 2187 /// Matches MaskedGather Intrinsic. 2188 template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> 2189 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty 2190 m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, 2191 const Opnd3 &Op3) { 2192 return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3); 2193 } 2194 2195 template <Intrinsic::ID IntrID, typename T0> 2196 inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { 2197 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); 2198 } 2199 2200 template <Intrinsic::ID IntrID, typename T0, typename T1> 2201 inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, 2202 const T1 &Op1) { 2203 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); 2204 } 2205 2206 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> 2207 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty 2208 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { 2209 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); 2210 } 2211 2212 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 2213 typename T3> 2214 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty 2215 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { 2216 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); 2217 } 2218 2219 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 2220 typename T3, typename T4> 2221 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty 2222 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, 2223 const T4 &Op4) { 2224 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3), 2225 m_Argument<4>(Op4)); 2226 } 2227 2228 template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, 2229 typename T3, typename T4, typename T5> 2230 inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty 2231 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, 2232 const T4 &Op4, const T5 &Op5) { 2233 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4), 2234 m_Argument<5>(Op5)); 2235 } 2236 2237 // Helper intrinsic matching specializations. 2238 template <typename Opnd0> 2239 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { 2240 return m_Intrinsic<Intrinsic::bitreverse>(Op0); 2241 } 2242 2243 template <typename Opnd0> 2244 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { 2245 return m_Intrinsic<Intrinsic::bswap>(Op0); 2246 } 2247 2248 template <typename Opnd0> 2249 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { 2250 return m_Intrinsic<Intrinsic::fabs>(Op0); 2251 } 2252 2253 template <typename Opnd0> 2254 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { 2255 return m_Intrinsic<Intrinsic::canonicalize>(Op0); 2256 } 2257 2258 template <typename Opnd0, typename Opnd1> 2259 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, 2260 const Opnd1 &Op1) { 2261 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); 2262 } 2263 2264 template <typename Opnd0, typename Opnd1> 2265 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, 2266 const Opnd1 &Op1) { 2267 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); 2268 } 2269 2270 template <typename Opnd0, typename Opnd1, typename Opnd2> 2271 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty 2272 m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { 2273 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2); 2274 } 2275 2276 template <typename Opnd0, typename Opnd1, typename Opnd2> 2277 inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty 2278 m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { 2279 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2); 2280 } 2281 2282 template <typename Opnd0> 2283 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) { 2284 return m_Intrinsic<Intrinsic::sqrt>(Op0); 2285 } 2286 2287 template <typename Opnd0, typename Opnd1> 2288 inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0, 2289 const Opnd1 &Op1) { 2290 return m_Intrinsic<Intrinsic::copysign>(Op0, Op1); 2291 } 2292 2293 template <typename Opnd0> 2294 inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) { 2295 return m_Intrinsic<Intrinsic::experimental_vector_reverse>(Op0); 2296 } 2297 2298 //===----------------------------------------------------------------------===// 2299 // Matchers for two-operands operators with the operators in either order 2300 // 2301 2302 /// Matches a BinaryOperator with LHS and RHS in either order. 2303 template <typename LHS, typename RHS> 2304 inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { 2305 return AnyBinaryOp_match<LHS, RHS, true>(L, R); 2306 } 2307 2308 /// Matches an ICmp with a predicate over LHS and RHS in either order. 2309 /// Swaps the predicate if operands are commuted. 2310 template <typename LHS, typename RHS> 2311 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true> 2312 m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { 2313 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L, 2314 R); 2315 } 2316 2317 /// Matches a specific opcode with LHS and RHS in either order. 2318 template <typename LHS, typename RHS> 2319 inline SpecificBinaryOp_match<LHS, RHS, true> 2320 m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) { 2321 return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R); 2322 } 2323 2324 /// Matches a Add with LHS and RHS in either order. 2325 template <typename LHS, typename RHS> 2326 inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, 2327 const RHS &R) { 2328 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); 2329 } 2330 2331 /// Matches a Mul with LHS and RHS in either order. 2332 template <typename LHS, typename RHS> 2333 inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, 2334 const RHS &R) { 2335 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); 2336 } 2337 2338 /// Matches an And with LHS and RHS in either order. 2339 template <typename LHS, typename RHS> 2340 inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, 2341 const RHS &R) { 2342 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); 2343 } 2344 2345 /// Matches an Or with LHS and RHS in either order. 2346 template <typename LHS, typename RHS> 2347 inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, 2348 const RHS &R) { 2349 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); 2350 } 2351 2352 /// Matches an Xor with LHS and RHS in either order. 2353 template <typename LHS, typename RHS> 2354 inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, 2355 const RHS &R) { 2356 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); 2357 } 2358 2359 /// Matches a 'Neg' as 'sub 0, V'. 2360 template <typename ValTy> 2361 inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> 2362 m_Neg(const ValTy &V) { 2363 return m_Sub(m_ZeroInt(), V); 2364 } 2365 2366 /// Matches a 'Neg' as 'sub nsw 0, V'. 2367 template <typename ValTy> 2368 inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, 2369 Instruction::Sub, 2370 OverflowingBinaryOperator::NoSignedWrap> 2371 m_NSWNeg(const ValTy &V) { 2372 return m_NSWSub(m_ZeroInt(), V); 2373 } 2374 2375 /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'. 2376 /// NOTE: we first match the 'Not' (by matching '-1'), 2377 /// and only then match the inner matcher! 2378 template <typename ValTy> 2379 inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true> 2380 m_Not(const ValTy &V) { 2381 return m_c_Xor(m_AllOnes(), V); 2382 } 2383 2384 template <typename ValTy> struct NotForbidUndef_match { 2385 ValTy Val; 2386 NotForbidUndef_match(const ValTy &V) : Val(V) {} 2387 2388 template <typename OpTy> bool match(OpTy *V) { 2389 // We do not use m_c_Xor because that could match an arbitrary APInt that is 2390 // not -1 as C and then fail to match the other operand if it is -1. 2391 // This code should still work even when both operands are constants. 2392 Value *X; 2393 const APInt *C; 2394 if (m_Xor(m_Value(X), m_APIntForbidUndef(C)).match(V) && C->isAllOnes()) 2395 return Val.match(X); 2396 if (m_Xor(m_APIntForbidUndef(C), m_Value(X)).match(V) && C->isAllOnes()) 2397 return Val.match(X); 2398 return false; 2399 } 2400 }; 2401 2402 /// Matches a bitwise 'not' as 'xor V, -1' or 'xor -1, V'. For vectors, the 2403 /// constant value must be composed of only -1 scalar elements. 2404 template <typename ValTy> 2405 inline NotForbidUndef_match<ValTy> m_NotForbidUndef(const ValTy &V) { 2406 return NotForbidUndef_match<ValTy>(V); 2407 } 2408 2409 /// Matches an SMin with LHS and RHS in either order. 2410 template <typename LHS, typename RHS> 2411 inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> 2412 m_c_SMin(const LHS &L, const RHS &R) { 2413 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); 2414 } 2415 /// Matches an SMax with LHS and RHS in either order. 2416 template <typename LHS, typename RHS> 2417 inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> 2418 m_c_SMax(const LHS &L, const RHS &R) { 2419 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); 2420 } 2421 /// Matches a UMin with LHS and RHS in either order. 2422 template <typename LHS, typename RHS> 2423 inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> 2424 m_c_UMin(const LHS &L, const RHS &R) { 2425 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); 2426 } 2427 /// Matches a UMax with LHS and RHS in either order. 2428 template <typename LHS, typename RHS> 2429 inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> 2430 m_c_UMax(const LHS &L, const RHS &R) { 2431 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); 2432 } 2433 2434 template <typename LHS, typename RHS> 2435 inline match_combine_or< 2436 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>, 2437 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>, 2438 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>, 2439 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>> 2440 m_c_MaxOrMin(const LHS &L, const RHS &R) { 2441 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)), 2442 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R))); 2443 } 2444 2445 template <Intrinsic::ID IntrID, typename T0, typename T1> 2446 inline match_combine_or<typename m_Intrinsic_Ty<T0, T1>::Ty, 2447 typename m_Intrinsic_Ty<T1, T0>::Ty> 2448 m_c_Intrinsic(const T0 &Op0, const T1 &Op1) { 2449 return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1), 2450 m_Intrinsic<IntrID>(Op1, Op0)); 2451 } 2452 2453 /// Matches FAdd with LHS and RHS in either order. 2454 template <typename LHS, typename RHS> 2455 inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> 2456 m_c_FAdd(const LHS &L, const RHS &R) { 2457 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); 2458 } 2459 2460 /// Matches FMul with LHS and RHS in either order. 2461 template <typename LHS, typename RHS> 2462 inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> 2463 m_c_FMul(const LHS &L, const RHS &R) { 2464 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); 2465 } 2466 2467 template <typename Opnd_t> struct Signum_match { 2468 Opnd_t Val; 2469 Signum_match(const Opnd_t &V) : Val(V) {} 2470 2471 template <typename OpTy> bool match(OpTy *V) { 2472 unsigned TypeSize = V->getType()->getScalarSizeInBits(); 2473 if (TypeSize == 0) 2474 return false; 2475 2476 unsigned ShiftWidth = TypeSize - 1; 2477 Value *OpL = nullptr, *OpR = nullptr; 2478 2479 // This is the representation of signum we match: 2480 // 2481 // signum(x) == (x >> 63) | (-x >>u 63) 2482 // 2483 // An i1 value is its own signum, so it's correct to match 2484 // 2485 // signum(x) == (x >> 0) | (-x >>u 0) 2486 // 2487 // for i1 values. 2488 2489 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth)); 2490 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth)); 2491 auto Signum = m_Or(LHS, RHS); 2492 2493 return Signum.match(V) && OpL == OpR && Val.match(OpL); 2494 } 2495 }; 2496 2497 /// Matches a signum pattern. 2498 /// 2499 /// signum(x) = 2500 /// x > 0 -> 1 2501 /// x == 0 -> 0 2502 /// x < 0 -> -1 2503 template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { 2504 return Signum_match<Val_t>(V); 2505 } 2506 2507 template <int Ind, typename Opnd_t> struct ExtractValue_match { 2508 Opnd_t Val; 2509 ExtractValue_match(const Opnd_t &V) : Val(V) {} 2510 2511 template <typename OpTy> bool match(OpTy *V) { 2512 if (auto *I = dyn_cast<ExtractValueInst>(V)) { 2513 // If Ind is -1, don't inspect indices 2514 if (Ind != -1 && 2515 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind)) 2516 return false; 2517 return Val.match(I->getAggregateOperand()); 2518 } 2519 return false; 2520 } 2521 }; 2522 2523 /// Match a single index ExtractValue instruction. 2524 /// For example m_ExtractValue<1>(...) 2525 template <int Ind, typename Val_t> 2526 inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) { 2527 return ExtractValue_match<Ind, Val_t>(V); 2528 } 2529 2530 /// Match an ExtractValue instruction with any index. 2531 /// For example m_ExtractValue(...) 2532 template <typename Val_t> 2533 inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) { 2534 return ExtractValue_match<-1, Val_t>(V); 2535 } 2536 2537 /// Matcher for a single index InsertValue instruction. 2538 template <int Ind, typename T0, typename T1> struct InsertValue_match { 2539 T0 Op0; 2540 T1 Op1; 2541 2542 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {} 2543 2544 template <typename OpTy> bool match(OpTy *V) { 2545 if (auto *I = dyn_cast<InsertValueInst>(V)) { 2546 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) && 2547 I->getNumIndices() == 1 && Ind == I->getIndices()[0]; 2548 } 2549 return false; 2550 } 2551 }; 2552 2553 /// Matches a single index InsertValue instruction. 2554 template <int Ind, typename Val_t, typename Elt_t> 2555 inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val, 2556 const Elt_t &Elt) { 2557 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt); 2558 } 2559 2560 /// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or 2561 /// the constant expression 2562 /// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>` 2563 /// under the right conditions determined by DataLayout. 2564 struct VScaleVal_match { 2565 template <typename ITy> bool match(ITy *V) { 2566 if (m_Intrinsic<Intrinsic::vscale>().match(V)) 2567 return true; 2568 2569 Value *Ptr; 2570 if (m_PtrToInt(m_Value(Ptr)).match(V)) { 2571 if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { 2572 auto *DerefTy = 2573 dyn_cast<ScalableVectorType>(GEP->getSourceElementType()); 2574 if (GEP->getNumIndices() == 1 && DerefTy && 2575 DerefTy->getElementType()->isIntegerTy(8) && 2576 m_Zero().match(GEP->getPointerOperand()) && 2577 m_SpecificInt(1).match(GEP->idx_begin()->get())) 2578 return true; 2579 } 2580 } 2581 2582 return false; 2583 } 2584 }; 2585 2586 inline VScaleVal_match m_VScale() { 2587 return VScaleVal_match(); 2588 } 2589 2590 template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false> 2591 struct LogicalOp_match { 2592 LHS L; 2593 RHS R; 2594 2595 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {} 2596 2597 template <typename T> bool match(T *V) { 2598 auto *I = dyn_cast<Instruction>(V); 2599 if (!I || !I->getType()->isIntOrIntVectorTy(1)) 2600 return false; 2601 2602 if (I->getOpcode() == Opcode) { 2603 auto *Op0 = I->getOperand(0); 2604 auto *Op1 = I->getOperand(1); 2605 return (L.match(Op0) && R.match(Op1)) || 2606 (Commutable && L.match(Op1) && R.match(Op0)); 2607 } 2608 2609 if (auto *Select = dyn_cast<SelectInst>(I)) { 2610 auto *Cond = Select->getCondition(); 2611 auto *TVal = Select->getTrueValue(); 2612 auto *FVal = Select->getFalseValue(); 2613 2614 // Don't match a scalar select of bool vectors. 2615 // Transforms expect a single type for operands if this matches. 2616 if (Cond->getType() != Select->getType()) 2617 return false; 2618 2619 if (Opcode == Instruction::And) { 2620 auto *C = dyn_cast<Constant>(FVal); 2621 if (C && C->isNullValue()) 2622 return (L.match(Cond) && R.match(TVal)) || 2623 (Commutable && L.match(TVal) && R.match(Cond)); 2624 } else { 2625 assert(Opcode == Instruction::Or); 2626 auto *C = dyn_cast<Constant>(TVal); 2627 if (C && C->isOneValue()) 2628 return (L.match(Cond) && R.match(FVal)) || 2629 (Commutable && L.match(FVal) && R.match(Cond)); 2630 } 2631 } 2632 2633 return false; 2634 } 2635 }; 2636 2637 /// Matches L && R either in the form of L & R or L ? R : false. 2638 /// Note that the latter form is poison-blocking. 2639 template <typename LHS, typename RHS> 2640 inline LogicalOp_match<LHS, RHS, Instruction::And> m_LogicalAnd(const LHS &L, 2641 const RHS &R) { 2642 return LogicalOp_match<LHS, RHS, Instruction::And>(L, R); 2643 } 2644 2645 /// Matches L && R where L and R are arbitrary values. 2646 inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); } 2647 2648 /// Matches L && R with LHS and RHS in either order. 2649 template <typename LHS, typename RHS> 2650 inline LogicalOp_match<LHS, RHS, Instruction::And, true> 2651 m_c_LogicalAnd(const LHS &L, const RHS &R) { 2652 return LogicalOp_match<LHS, RHS, Instruction::And, true>(L, R); 2653 } 2654 2655 /// Matches L || R either in the form of L | R or L ? true : R. 2656 /// Note that the latter form is poison-blocking. 2657 template <typename LHS, typename RHS> 2658 inline LogicalOp_match<LHS, RHS, Instruction::Or> m_LogicalOr(const LHS &L, 2659 const RHS &R) { 2660 return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R); 2661 } 2662 2663 /// Matches L || R where L and R are arbitrary values. 2664 inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); } 2665 2666 /// Matches L || R with LHS and RHS in either order. 2667 template <typename LHS, typename RHS> 2668 inline LogicalOp_match<LHS, RHS, Instruction::Or, true> 2669 m_c_LogicalOr(const LHS &L, const RHS &R) { 2670 return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R); 2671 } 2672 2673 /// Matches either L && R or L || R, 2674 /// either one being in the either binary or logical form. 2675 /// Note that the latter form is poison-blocking. 2676 template <typename LHS, typename RHS, bool Commutable = false> 2677 inline auto m_LogicalOp(const LHS &L, const RHS &R) { 2678 return m_CombineOr( 2679 LogicalOp_match<LHS, RHS, Instruction::And, Commutable>(L, R), 2680 LogicalOp_match<LHS, RHS, Instruction::Or, Commutable>(L, R)); 2681 } 2682 2683 /// Matches either L && R or L || R where L and R are arbitrary values. 2684 inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); } 2685 2686 /// Matches either L && R or L || R with LHS and RHS in either order. 2687 template <typename LHS, typename RHS> 2688 inline auto m_c_LogicalOp(const LHS &L, const RHS &R) { 2689 return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R); 2690 } 2691 2692 } // end namespace PatternMatch 2693 } // end namespace llvm 2694 2695 #endif // LLVM_IR_PATTERNMATCH_H 2696