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