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