blob: 7c058342265513731fdaa260eb444f248a7eeaea [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- 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
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return const_cast<Pattern &>(P).match(V);
51}
52
53template <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
63template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
64 return SubPattern;
65}
66
67template <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.
72inline class_match<Value> m_Value() { return class_match<Value>(); }
73
74/// Match an arbitrary binary operation and ignore it.
75inline class_match<BinaryOperator> m_BinOp() {
76 return class_match<BinaryOperator>();
77}
78
79/// Matches any compare instruction and ignore it.
80inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
81
82/// Match an arbitrary ConstantInt and ignore it.
83inline class_match<ConstantInt> m_ConstantInt() {
84 return class_match<ConstantInt>();
85}
86
87/// Match an arbitrary undef constant.
88inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
89
90/// Match an arbitrary Constant and ignore it.
91inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
92
93/// Matching combinators
94template <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
109template <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
124template <typename LTy, typename RTy>
125inline 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
130template <typename LTy, typename RTy>
131inline 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 Scull5e1ddfa2018-08-14 10:06:54 +0100135struct 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.
157struct 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.
177inline 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.
181inline apfloat_match m_APFloat(const APFloat *&Res) { return Res; }
182
183template <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.
199template <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.
206template <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.
237template <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.
263template <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
301struct 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.
306inline cst_pred_ty<is_all_ones> m_AllOnes() {
307 return cst_pred_ty<is_all_ones>();
308}
309
310struct 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.
316inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
317 return cst_pred_ty<is_maxsignedvalue>();
318}
319inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
320 return V;
321}
322
323struct 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.
328inline cst_pred_ty<is_negative> m_Negative() {
329 return cst_pred_ty<is_negative>();
330}
331inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
332 return V;
333}
334
335struct 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.
340inline cst_pred_ty<is_nonnegative> m_NonNegative() {
341 return cst_pred_ty<is_nonnegative>();
342}
343inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
344 return V;
345}
346
347struct 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.
352inline cst_pred_ty<is_one> m_One() {
353 return cst_pred_ty<is_one>();
354}
355
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100356struct 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.
361inline cst_pred_ty<is_zero_int> m_ZeroInt() {
362 return cst_pred_ty<is_zero_int>();
363}
364
365struct 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.
373inline is_zero m_Zero() {
374 return is_zero();
375}
376
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100377struct 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.
382inline cst_pred_ty<is_power2> m_Power2() {
383 return cst_pred_ty<is_power2>();
384}
385inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
386 return V;
387}
388
389struct 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.
394inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
395 return cst_pred_ty<is_power2_or_zero>();
396}
397inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
398 return V;
399}
400
401struct 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.
406inline cst_pred_ty<is_sign_mask> m_SignMask() {
407 return cst_pred_ty<is_sign_mask>();
408}
409
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100410struct 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.
415inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
416 return cst_pred_ty<is_lowbit_mask>();
417}
418
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100419struct 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.
424inline cstfp_pred_ty<is_nan> m_NaN() {
425 return cstfp_pred_ty<is_nan>();
426}
427
428struct 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.
433inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
434 return cstfp_pred_ty<is_any_zero_fp>();
435}
436
437struct 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.
442inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
443 return cstfp_pred_ty<is_pos_zero_fp>();
444}
445
446struct 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.
451inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
452 return cstfp_pred_ty<is_neg_zero_fp>();
453}
454
455///////////////////////////////////////////////////////////////////////////////
456
457template <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.
472inline bind_ty<Value> m_Value(Value *&V) { return V; }
473inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
474
475/// Match an instruction, capturing it if we match.
476inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
477/// Match a binary operator, capturing it if we match.
478inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
479
480/// Match a ConstantInt, capturing the value if we match.
481inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
482
483/// Match a Constant, capturing the value if we match.
484inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
485
486/// Match a ConstantFP, capturing the value if we match.
487inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
488
489/// Match a specified Value*.
490struct 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.
499inline specificval_ty m_Specific(const Value *V) { return V; }
500
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100501/// Stores a reference to the Value *, not the Value * itself,
502/// thus can be used in commutative matchers.
503template <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().
512inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
513inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
514 return V;
515}
516
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100517/// Match a specified floating point value or vector of all elements of
518/// that value.
519struct 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.
537inline 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.
540inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
541
542struct 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.
559struct 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.
576inline 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.
580inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
581
582//===----------------------------------------------------------------------===//
583// Matcher for any binary operator.
584//
585template <typename LHS_t, typename RHS_t, bool Commutable = false>
586struct AnyBinaryOp_match {
587 LHS_t L;
588 RHS_t R;
589
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100590 // The evaluation order is always stable, regardless of Commutability.
591 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100592 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 Scullcdfcccc2018-10-05 20:58:37 +0100597 (Commutable && L.match(I->getOperand(1)) &&
598 R.match(I->getOperand(0)));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100599 return false;
600 }
601};
602
603template <typename LHS, typename RHS>
604inline 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
612template <typename LHS_t, typename RHS_t, unsigned Opcode,
613 bool Commutable = false>
614struct BinaryOp_match {
615 LHS_t L;
616 RHS_t R;
617
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100618 // The evaluation order is always stable, regardless of Commutability.
619 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100620 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 Scullcdfcccc2018-10-05 20:58:37 +0100626 (Commutable && L.match(I->getOperand(1)) &&
627 R.match(I->getOperand(0)));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100628 }
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 Scullcdfcccc2018-10-05 20:58:37 +0100632 (Commutable && L.match(CE->getOperand(1)) &&
633 R.match(CE->getOperand(0))));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100634 return false;
635 }
636};
637
638template <typename LHS, typename RHS>
639inline 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
644template <typename LHS, typename RHS>
645inline 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
650template <typename LHS, typename RHS>
651inline 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
656template <typename LHS, typename RHS>
657inline 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 Scullcdfcccc2018-10-05 20:58:37 +0100662/// Match 'fneg X' as 'fsub -0.0, X'.
663template <typename RHS>
664inline BinaryOp_match<cstfp_pred_ty<is_neg_zero_fp>, RHS, Instruction::FSub>
665m_FNeg(const RHS &X) {
666 return m_FSub(m_NegZeroFP(), X);
667}
668
Andrew Scull0372a572018-11-16 15:47:06 +0000669/// Match 'fneg X' as 'fsub +-0.0, X'.
670template <typename RHS>
671inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
672m_FNegNSZ(const RHS &X) {
673 return m_FSub(m_AnyZeroFP(), X);
674}
675
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100676template <typename LHS, typename RHS>
677inline 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
682template <typename LHS, typename RHS>
683inline 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
688template <typename LHS, typename RHS>
689inline 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
694template <typename LHS, typename RHS>
695inline 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
700template <typename LHS, typename RHS>
701inline 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
706template <typename LHS, typename RHS>
707inline 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
712template <typename LHS, typename RHS>
713inline 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
718template <typename LHS, typename RHS>
719inline 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
724template <typename LHS, typename RHS>
725inline 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
730template <typename LHS, typename RHS>
731inline 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
736template <typename LHS, typename RHS>
737inline 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
742template <typename LHS, typename RHS>
743inline 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
748template <typename LHS, typename RHS>
749inline 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
754template <typename LHS, typename RHS>
755inline 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
760template <typename LHS_t, typename RHS_t, unsigned Opcode,
761 unsigned WrapFlags = 0>
762struct 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
785template <typename LHS, typename RHS>
786inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
787 OverflowingBinaryOperator::NoSignedWrap>
788m_NSWAdd(const LHS &L, const RHS &R) {
789 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
790 OverflowingBinaryOperator::NoSignedWrap>(
791 L, R);
792}
793template <typename LHS, typename RHS>
794inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
795 OverflowingBinaryOperator::NoSignedWrap>
796m_NSWSub(const LHS &L, const RHS &R) {
797 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
798 OverflowingBinaryOperator::NoSignedWrap>(
799 L, R);
800}
801template <typename LHS, typename RHS>
802inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
803 OverflowingBinaryOperator::NoSignedWrap>
804m_NSWMul(const LHS &L, const RHS &R) {
805 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
806 OverflowingBinaryOperator::NoSignedWrap>(
807 L, R);
808}
809template <typename LHS, typename RHS>
810inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
811 OverflowingBinaryOperator::NoSignedWrap>
812m_NSWShl(const LHS &L, const RHS &R) {
813 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
814 OverflowingBinaryOperator::NoSignedWrap>(
815 L, R);
816}
817
818template <typename LHS, typename RHS>
819inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
820 OverflowingBinaryOperator::NoUnsignedWrap>
821m_NUWAdd(const LHS &L, const RHS &R) {
822 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
823 OverflowingBinaryOperator::NoUnsignedWrap>(
824 L, R);
825}
826template <typename LHS, typename RHS>
827inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
828 OverflowingBinaryOperator::NoUnsignedWrap>
829m_NUWSub(const LHS &L, const RHS &R) {
830 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
831 OverflowingBinaryOperator::NoUnsignedWrap>(
832 L, R);
833}
834template <typename LHS, typename RHS>
835inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
836 OverflowingBinaryOperator::NoUnsignedWrap>
837m_NUWMul(const LHS &L, const RHS &R) {
838 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
839 OverflowingBinaryOperator::NoUnsignedWrap>(
840 L, R);
841}
842template <typename LHS, typename RHS>
843inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
844 OverflowingBinaryOperator::NoUnsignedWrap>
845m_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//
854template <typename LHS_t, typename RHS_t, typename Predicate>
855struct 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
872struct is_shift_op {
873 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
874};
875
876struct is_right_shift_op {
877 bool isOpType(unsigned Opcode) {
878 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
879 }
880};
881
882struct is_logical_shift_op {
883 bool isOpType(unsigned Opcode) {
884 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
885 }
886};
887
888struct is_bitwiselogic_op {
889 bool isOpType(unsigned Opcode) {
890 return Instruction::isBitwiseLogicOp(Opcode);
891 }
892};
893
894struct is_idiv_op {
895 bool isOpType(unsigned Opcode) {
896 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
897 }
898};
899
900/// Matches shift operations.
901template <typename LHS, typename RHS>
902inline 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.
908template <typename LHS, typename RHS>
909inline 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.
915template <typename LHS, typename RHS>
916inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
917m_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.
922template <typename LHS, typename RHS>
923inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
924m_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.
929template <typename LHS, typename RHS>
930inline 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//
938template <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
950template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
951 return SubPattern;
952}
953
954//===----------------------------------------------------------------------===//
955// Matchers for CmpInst classes
956//
957
958template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
959 bool Commutable = false>
960struct CmpClass_match {
961 PredicateTy &Predicate;
962 LHS_t L;
963 RHS_t R;
964
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100965 // The evaluation order is always stable, regardless of Commutability.
966 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100967 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 Scullcdfcccc2018-10-05 20:58:37 +0100973 (Commutable && L.match(I->getOperand(1)) &&
974 R.match(I->getOperand(0)))) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100975 Predicate = I->getPredicate();
976 return true;
977 }
978 return false;
979 }
980};
981
982template <typename LHS, typename RHS>
983inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
984m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
985 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
986}
987
988template <typename LHS, typename RHS>
989inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
990m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
991 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
992}
993
994template <typename LHS, typename RHS>
995inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
996m_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 Scull0372a572018-11-16 15:47:06 +00001001// Matchers for instructions with a given opcode and number of operands.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001002//
1003
Andrew Scull0372a572018-11-16 15:47:06 +00001004/// Matches instructions with Opcode and three operands.
1005template <typename T0, unsigned Opcode> struct OneOps_match {
1006 T0 Op1;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001007
Andrew Scull0372a572018-11-16 15:47:06 +00001008 OneOps_match(const T0 &Op1) : Op1(Op1) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001009
1010 template <typename OpTy> bool match(OpTy *V) {
Andrew Scull0372a572018-11-16 15:47:06 +00001011 if (V->getValueID() == Value::InstructionVal + Opcode) {
1012 auto *I = cast<Instruction>(V);
1013 return Op1.match(I->getOperand(0));
1014 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001015 return false;
1016 }
1017};
1018
Andrew Scull0372a572018-11-16 15:47:06 +00001019/// Matches instructions with Opcode and three operands.
1020template <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.
1036template <typename T0, typename T1, typename T2, unsigned Opcode>
1037struct 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 Scull5e1ddfa2018-08-14 10:06:54 +01001056template <typename Cond, typename LHS, typename RHS>
Andrew Scull0372a572018-11-16 15:47:06 +00001057inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
1058m_Select(const Cond &C, const LHS &L, const RHS &R) {
1059 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001060}
1061
1062/// This matches a select of two constants, e.g.:
1063/// m_SelectCst<-1, 0>(m_Value(V))
1064template <int64_t L, int64_t R, typename Cond>
Andrew Scull0372a572018-11-16 15:47:06 +00001065inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1066 Instruction::Select>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001067m_SelectCst(const Cond &C) {
1068 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1069}
1070
Andrew Scull0372a572018-11-16 15:47:06 +00001071/// Matches InsertElementInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001072template <typename Val_t, typename Elt_t, typename Idx_t>
Andrew Scull0372a572018-11-16 15:47:06 +00001073inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001074m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
Andrew Scull0372a572018-11-16 15:47:06 +00001075 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1076 Val, Elt, Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001077}
1078
Andrew Scull0372a572018-11-16 15:47:06 +00001079/// Matches ExtractElementInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001080template <typename Val_t, typename Idx_t>
Andrew Scull0372a572018-11-16 15:47:06 +00001081inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001082m_ExtractElement(const Val_t &Val, const Idx_t &Idx) {
Andrew Scull0372a572018-11-16 15:47:06 +00001083 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001084}
1085
Andrew Scull0372a572018-11-16 15:47:06 +00001086/// Matches ShuffleVectorInst.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001087template <typename V1_t, typename V2_t, typename Mask_t>
Andrew Scull0372a572018-11-16 15:47:06 +00001088inline ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001089m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m) {
Andrew Scull0372a572018-11-16 15:47:06 +00001090 return ThreeOps_match<V1_t, V2_t, Mask_t, Instruction::ShuffleVector>(v1, v2,
1091 m);
1092}
1093
1094/// Matches LoadInst.
1095template <typename OpTy>
1096inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1097 return OneOps_match<OpTy, Instruction::Load>(Op);
1098}
1099
1100/// Matches StoreInst.
1101template <typename ValueOpTy, typename PointerOpTy>
1102inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
1103m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1104 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1105 PointerOp);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001106}
1107
1108//===----------------------------------------------------------------------===//
1109// Matchers for CastInst classes
1110//
1111
1112template <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.
1125template <typename OpTy>
1126inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
1127 return CastClass_match<OpTy, Instruction::BitCast>(Op);
1128}
1129
1130/// Matches PtrToInt.
1131template <typename OpTy>
1132inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
1133 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
1134}
1135
1136/// Matches Trunc.
1137template <typename OpTy>
1138inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1139 return CastClass_match<OpTy, Instruction::Trunc>(Op);
1140}
1141
1142/// Matches SExt.
1143template <typename OpTy>
1144inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1145 return CastClass_match<OpTy, Instruction::SExt>(Op);
1146}
1147
1148/// Matches ZExt.
1149template <typename OpTy>
1150inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1151 return CastClass_match<OpTy, Instruction::ZExt>(Op);
1152}
1153
1154template <typename OpTy>
1155inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1156 CastClass_match<OpTy, Instruction::SExt>>
1157m_ZExtOrSExt(const OpTy &Op) {
1158 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1159}
1160
1161/// Matches UIToFP.
1162template <typename OpTy>
1163inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1164 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
1165}
1166
1167/// Matches SIToFP.
1168template <typename OpTy>
1169inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1170 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
1171}
1172
1173/// Matches FPTrunc
1174template <typename OpTy>
1175inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
1176 return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
1177}
1178
1179/// Matches FPExt
1180template <typename OpTy>
1181inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1182 return CastClass_match<OpTy, Instruction::FPExt>(Op);
1183}
1184
1185//===----------------------------------------------------------------------===//
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001186// Matchers for control flow.
1187//
1188
1189struct 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
1204inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1205
1206template <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
1224template <typename Cond_t>
1225inline 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
1233template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1234 bool Commutable = false>
1235struct MaxMin_match {
1236 LHS_t L;
1237 RHS_t R;
1238
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001239 // The evaluation order is always stable, regardless of Commutability.
1240 // The LHS is always matched first.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001241 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 Scullcdfcccc2018-10-05 20:58:37 +01001267 (Commutable && L.match(RHS) && R.match(LHS));
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001268 }
1269};
1270
1271/// Helper class for identifying signed max predicates.
1272struct 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.
1279struct 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.
1286struct 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.
1293struct 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.
1300struct 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.
1307struct 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.
1314struct 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.
1321struct ufmin_pred_ty {
1322 static bool match(FCmpInst::Predicate Pred) {
1323 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1324 }
1325};
1326
1327template <typename LHS, typename RHS>
1328inline 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
1333template <typename LHS, typename RHS>
1334inline 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
1339template <typename LHS, typename RHS>
1340inline 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
1345template <typename LHS, typename RHS>
1346inline 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
1360template <typename LHS, typename RHS>
1361inline 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
1375template <typename LHS, typename RHS>
1376inline 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
1390template <typename LHS, typename RHS>
1391inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1392m_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
1405template <typename LHS, typename RHS>
1406inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1407m_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
1415template <typename LHS_t, typename RHS_t, typename Sum_t>
1416struct 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.
1451template <typename LHS_t, typename RHS_t, typename Sum_t>
1452UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
1453m_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
1457template <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.
1470template <unsigned OpI, typename Opnd_t>
1471inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1472 return Argument_match<Opnd_t>(OpI, Op);
1473}
1474
1475/// Intrinsic matchers.
1476struct 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
1493template <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>
1497struct m_Intrinsic_Ty;
1498template <typename T0> struct m_Intrinsic_Ty<T0> {
1499 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
1500};
1501template <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};
1505template <typename T0, typename T1, typename T2>
1506struct 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};
1511template <typename T0, typename T1, typename T2, typename T3>
1512struct 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))
1520template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
1521 return IntrinsicID_match(IntrID);
1522}
1523
1524template <Intrinsic::ID IntrID, typename T0>
1525inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
1526 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
1527}
1528
1529template <Intrinsic::ID IntrID, typename T0, typename T1>
1530inline 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
1535template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
1536inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1537m_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
1541template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
1542 typename T3>
1543inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1544m_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.
1549template <typename Opnd0>
1550inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
1551 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
1552}
1553
1554template <typename Opnd0>
1555inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
1556 return m_Intrinsic<Intrinsic::bswap>(Op0);
1557}
1558
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001559template <typename Opnd0>
1560inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
1561 return m_Intrinsic<Intrinsic::fabs>(Op0);
1562}
1563
1564template <typename Opnd0>
1565inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
1566 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
1567}
1568
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001569template <typename Opnd0, typename Opnd1>
1570inline 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
1575template <typename Opnd0, typename Opnd1>
1576inline 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 Scull5e1ddfa2018-08-14 10:06:54 +01001581//===----------------------------------------------------------------------===//
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.
1586template <typename LHS, typename RHS>
1587inline 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.
1593template <typename LHS, typename RHS>
1594inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
1595m_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.
1601template <typename LHS, typename RHS>
1602inline 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.
1608template <typename LHS, typename RHS>
1609inline 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.
1615template <typename LHS, typename RHS>
1616inline 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.
1622template <typename LHS, typename RHS>
1623inline 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.
1629template <typename LHS, typename RHS>
1630inline 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 Scullcdfcccc2018-10-05 20:58:37 +01001635/// Matches a 'Neg' as 'sub 0, V'.
1636template <typename ValTy>
1637inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
1638m_Neg(const ValTy &V) {
1639 return m_Sub(m_ZeroInt(), V);
1640}
1641
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001642/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
1643template <typename ValTy>
1644inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
1645m_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.
1650template <typename LHS, typename RHS>
1651inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
1652m_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.
1656template <typename LHS, typename RHS>
1657inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
1658m_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.
1662template <typename LHS, typename RHS>
1663inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
1664m_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.
1668template <typename LHS, typename RHS>
1669inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
1670m_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.
1675template <typename LHS, typename RHS>
1676inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
1677m_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.
1682template <typename LHS, typename RHS>
1683inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
1684m_c_FMul(const LHS &L, const RHS &R) {
1685 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
1686}
1687
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001688template <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
1724template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
1725 return Signum_match<Val_t>(V);
1726}
1727
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001728} // end namespace PatternMatch
1729} // end namespace llvm
1730
1731#endif // LLVM_IR_PATTERNMATCH_H