1 //===-- llvm/Support/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_SUPPORT_PATTERNMATCH_H
30 #define LLVM_SUPPORT_PATTERNMATCH_H
31
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/Operator.h"
36 #include "llvm/Support/CallSite.h"
37
38 namespace llvm {
39 namespace PatternMatch {
40
41 template<typename Val, typename Pattern>
match(Val * V,const Pattern & P)42 bool match(Val *V, const Pattern &P) {
43 return const_cast<Pattern&>(P).match(V);
44 }
45
46
47 template<typename SubPattern_t>
48 struct OneUse_match {
49 SubPattern_t SubPattern;
50
OneUse_matchOneUse_match51 OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {}
52
53 template<typename OpTy>
matchOneUse_match54 bool match(OpTy *V) {
55 return V->hasOneUse() && SubPattern.match(V);
56 }
57 };
58
59 template<typename T>
m_OneUse(const T & SubPattern)60 inline OneUse_match<T> m_OneUse(const T &SubPattern) { return SubPattern; }
61
62
63 template<typename Class>
64 struct class_match {
65 template<typename ITy>
matchclass_match66 bool match(ITy *V) { return isa<Class>(V); }
67 };
68
69 /// m_Value() - Match an arbitrary value and ignore it.
m_Value()70 inline class_match<Value> m_Value() { return class_match<Value>(); }
71 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
m_ConstantInt()72 inline class_match<ConstantInt> m_ConstantInt() {
73 return class_match<ConstantInt>();
74 }
75 /// m_Undef() - Match an arbitrary undef constant.
m_Undef()76 inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
77
m_Constant()78 inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
79
80 /// Matching combinators
81 template<typename LTy, typename RTy>
82 struct match_combine_or {
83 LTy L;
84 RTy R;
85
match_combine_ormatch_combine_or86 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
87
88 template<typename ITy>
matchmatch_combine_or89 bool match(ITy *V) {
90 if (L.match(V))
91 return true;
92 if (R.match(V))
93 return true;
94 return false;
95 }
96 };
97
98 template<typename LTy, typename RTy>
99 struct match_combine_and {
100 LTy L;
101 RTy R;
102
match_combine_andmatch_combine_and103 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) { }
104
105 template<typename ITy>
matchmatch_combine_and106 bool match(ITy *V) {
107 if (L.match(V))
108 if (R.match(V))
109 return true;
110 return false;
111 }
112 };
113
114 /// Combine two pattern matchers matching L || R
115 template<typename LTy, typename RTy>
m_CombineOr(const LTy & L,const RTy & R)116 inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
117 return match_combine_or<LTy, RTy>(L, R);
118 }
119
120 /// Combine two pattern matchers matching L && R
121 template<typename LTy, typename RTy>
m_CombineAnd(const LTy & L,const RTy & R)122 inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
123 return match_combine_and<LTy, RTy>(L, R);
124 }
125
126 struct match_zero {
127 template<typename ITy>
matchmatch_zero128 bool match(ITy *V) {
129 if (const Constant *C = dyn_cast<Constant>(V))
130 return C->isNullValue();
131 return false;
132 }
133 };
134
135 /// m_Zero() - Match an arbitrary zero/null constant. This includes
136 /// zero_initializer for vectors and ConstantPointerNull for pointers.
m_Zero()137 inline match_zero m_Zero() { return match_zero(); }
138
139 struct match_neg_zero {
140 template<typename ITy>
matchmatch_neg_zero141 bool match(ITy *V) {
142 if (const Constant *C = dyn_cast<Constant>(V))
143 return C->isNegativeZeroValue();
144 return false;
145 }
146 };
147
148 /// m_NegZero() - Match an arbitrary zero/null constant. This includes
149 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
150 /// floating point constants, this will match negative zero but not positive
151 /// zero
m_NegZero()152 inline match_neg_zero m_NegZero() { return match_neg_zero(); }
153
154 /// m_AnyZero() - Match an arbitrary zero/null constant. This includes
155 /// zero_initializer for vectors and ConstantPointerNull for pointers. For
156 /// floating point constants, this will match negative zero and positive zero
m_AnyZero()157 inline match_combine_or<match_zero, match_neg_zero> m_AnyZero() {
158 return m_CombineOr(m_Zero(), m_NegZero());
159 }
160
161 struct apint_match {
162 const APInt *&Res;
apint_matchapint_match163 apint_match(const APInt *&R) : Res(R) {}
164 template<typename ITy>
matchapint_match165 bool match(ITy *V) {
166 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
167 Res = &CI->getValue();
168 return true;
169 }
170 if (V->getType()->isVectorTy())
171 if (const Constant *C = dyn_cast<Constant>(V))
172 if (ConstantInt *CI =
173 dyn_cast_or_null<ConstantInt>(C->getSplatValue())) {
174 Res = &CI->getValue();
175 return true;
176 }
177 return false;
178 }
179 };
180
181 /// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
182 /// specified pointer to the contained APInt.
m_APInt(const APInt * & Res)183 inline apint_match m_APInt(const APInt *&Res) { return Res; }
184
185
186 template<int64_t Val>
187 struct constantint_match {
188 template<typename ITy>
matchconstantint_match189 bool match(ITy *V) {
190 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
191 const APInt &CIV = CI->getValue();
192 if (Val >= 0)
193 return CIV == static_cast<uint64_t>(Val);
194 // If Val is negative, and CI is shorter than it, truncate to the right
195 // number of bits. If it is larger, then we have to sign extend. Just
196 // compare their negated values.
197 return -CIV == -Val;
198 }
199 return false;
200 }
201 };
202
203 /// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
204 template<int64_t Val>
m_ConstantInt()205 inline constantint_match<Val> m_ConstantInt() {
206 return constantint_match<Val>();
207 }
208
209 /// cst_pred_ty - This helper class is used to match scalar and vector constants
210 /// that satisfy a specified predicate.
211 template<typename Predicate>
212 struct cst_pred_ty : public Predicate {
213 template<typename ITy>
matchcst_pred_ty214 bool match(ITy *V) {
215 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
216 return this->isValue(CI->getValue());
217 if (V->getType()->isVectorTy())
218 if (const Constant *C = dyn_cast<Constant>(V))
219 if (const ConstantInt *CI =
220 dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
221 return this->isValue(CI->getValue());
222 return false;
223 }
224 };
225
226 /// api_pred_ty - This helper class is used to match scalar and vector constants
227 /// that satisfy a specified predicate, and bind them to an APInt.
228 template<typename Predicate>
229 struct api_pred_ty : public Predicate {
230 const APInt *&Res;
api_pred_tyapi_pred_ty231 api_pred_ty(const APInt *&R) : Res(R) {}
232 template<typename ITy>
matchapi_pred_ty233 bool match(ITy *V) {
234 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
235 if (this->isValue(CI->getValue())) {
236 Res = &CI->getValue();
237 return true;
238 }
239 if (V->getType()->isVectorTy())
240 if (const Constant *C = dyn_cast<Constant>(V))
241 if (ConstantInt *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
242 if (this->isValue(CI->getValue())) {
243 Res = &CI->getValue();
244 return true;
245 }
246
247 return false;
248 }
249 };
250
251
252 struct is_one {
isValueis_one253 bool isValue(const APInt &C) { return C == 1; }
254 };
255
256 /// m_One() - Match an integer 1 or a vector with all elements equal to 1.
m_One()257 inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
m_One(const APInt * & V)258 inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
259
260 struct is_all_ones {
isValueis_all_ones261 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
262 };
263
264 /// m_AllOnes() - Match an integer or vector with all bits set to true.
m_AllOnes()265 inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
m_AllOnes(const APInt * & V)266 inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
267
268 struct is_sign_bit {
isValueis_sign_bit269 bool isValue(const APInt &C) { return C.isSignBit(); }
270 };
271
272 /// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
m_SignBit()273 inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
m_SignBit(const APInt * & V)274 inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
275
276 struct is_power2 {
isValueis_power2277 bool isValue(const APInt &C) { return C.isPowerOf2(); }
278 };
279
280 /// m_Power2() - Match an integer or vector power of 2.
m_Power2()281 inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
m_Power2(const APInt * & V)282 inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
283
284 template<typename Class>
285 struct bind_ty {
286 Class *&VR;
bind_tybind_ty287 bind_ty(Class *&V) : VR(V) {}
288
289 template<typename ITy>
matchbind_ty290 bool match(ITy *V) {
291 if (Class *CV = dyn_cast<Class>(V)) {
292 VR = CV;
293 return true;
294 }
295 return false;
296 }
297 };
298
299 /// m_Value - Match a value, capturing it if we match.
m_Value(Value * & V)300 inline bind_ty<Value> m_Value(Value *&V) { return V; }
301
302 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
m_ConstantInt(ConstantInt * & CI)303 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
304
305 /// m_Constant - Match a Constant, capturing the value if we match.
m_Constant(Constant * & C)306 inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
307
308 /// m_ConstantFP - Match a ConstantFP, capturing the value if we match.
m_ConstantFP(ConstantFP * & C)309 inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
310
311 /// specificval_ty - Match a specified Value*.
312 struct specificval_ty {
313 const Value *Val;
specificval_tyspecificval_ty314 specificval_ty(const Value *V) : Val(V) {}
315
316 template<typename ITy>
matchspecificval_ty317 bool match(ITy *V) {
318 return V == Val;
319 }
320 };
321
322 /// m_Specific - Match if we have a specific specified value.
m_Specific(const Value * V)323 inline specificval_ty m_Specific(const Value *V) { return V; }
324
325 /// Match a specified floating point value or vector of all elements of that
326 /// value.
327 struct specific_fpval {
328 double Val;
specific_fpvalspecific_fpval329 specific_fpval(double V) : Val(V) {}
330
331 template<typename ITy>
matchspecific_fpval332 bool match(ITy *V) {
333 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(V))
334 return CFP->isExactlyValue(Val);
335 if (V->getType()->isVectorTy())
336 if (const Constant *C = dyn_cast<Constant>(V))
337 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
338 return CFP->isExactlyValue(Val);
339 return false;
340 }
341 };
342
343 /// Match a specific floating point value or vector with all elements equal to
344 /// the value.
m_SpecificFP(double V)345 inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
346
347 /// Match a float 1.0 or vector with all elements equal to 1.0.
m_FPOne()348 inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
349
350 struct bind_const_intval_ty {
351 uint64_t &VR;
bind_const_intval_tybind_const_intval_ty352 bind_const_intval_ty(uint64_t &V) : VR(V) {}
353
354 template<typename ITy>
matchbind_const_intval_ty355 bool match(ITy *V) {
356 if (ConstantInt *CV = dyn_cast<ConstantInt>(V))
357 if (CV->getBitWidth() <= 64) {
358 VR = CV->getZExtValue();
359 return true;
360 }
361 return false;
362 }
363 };
364
365 /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not
366 /// match ConstantInts wider than 64-bits.
m_ConstantInt(uint64_t & V)367 inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
368
369 //===----------------------------------------------------------------------===//
370 // Matchers for specific binary operators.
371 //
372
373 template<typename LHS_t, typename RHS_t, unsigned Opcode>
374 struct BinaryOp_match {
375 LHS_t L;
376 RHS_t R;
377
BinaryOp_matchBinaryOp_match378 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
379
380 template<typename OpTy>
matchBinaryOp_match381 bool match(OpTy *V) {
382 if (V->getValueID() == Value::InstructionVal + Opcode) {
383 BinaryOperator *I = cast<BinaryOperator>(V);
384 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
385 }
386 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
387 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
388 R.match(CE->getOperand(1));
389 return false;
390 }
391 };
392
393 template<typename LHS, typename RHS>
394 inline BinaryOp_match<LHS, RHS, Instruction::Add>
m_Add(const LHS & L,const RHS & R)395 m_Add(const LHS &L, const RHS &R) {
396 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
397 }
398
399 template<typename LHS, typename RHS>
400 inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
m_FAdd(const LHS & L,const RHS & R)401 m_FAdd(const LHS &L, const RHS &R) {
402 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
403 }
404
405 template<typename LHS, typename RHS>
406 inline BinaryOp_match<LHS, RHS, Instruction::Sub>
m_Sub(const LHS & L,const RHS & R)407 m_Sub(const LHS &L, const RHS &R) {
408 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
409 }
410
411 template<typename LHS, typename RHS>
412 inline BinaryOp_match<LHS, RHS, Instruction::FSub>
m_FSub(const LHS & L,const RHS & R)413 m_FSub(const LHS &L, const RHS &R) {
414 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
415 }
416
417 template<typename LHS, typename RHS>
418 inline BinaryOp_match<LHS, RHS, Instruction::Mul>
m_Mul(const LHS & L,const RHS & R)419 m_Mul(const LHS &L, const RHS &R) {
420 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
421 }
422
423 template<typename LHS, typename RHS>
424 inline BinaryOp_match<LHS, RHS, Instruction::FMul>
m_FMul(const LHS & L,const RHS & R)425 m_FMul(const LHS &L, const RHS &R) {
426 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
427 }
428
429 template<typename LHS, typename RHS>
430 inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
m_UDiv(const LHS & L,const RHS & R)431 m_UDiv(const LHS &L, const RHS &R) {
432 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
433 }
434
435 template<typename LHS, typename RHS>
436 inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
m_SDiv(const LHS & L,const RHS & R)437 m_SDiv(const LHS &L, const RHS &R) {
438 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
439 }
440
441 template<typename LHS, typename RHS>
442 inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
m_FDiv(const LHS & L,const RHS & R)443 m_FDiv(const LHS &L, const RHS &R) {
444 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
445 }
446
447 template<typename LHS, typename RHS>
448 inline BinaryOp_match<LHS, RHS, Instruction::URem>
m_URem(const LHS & L,const RHS & R)449 m_URem(const LHS &L, const RHS &R) {
450 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
451 }
452
453 template<typename LHS, typename RHS>
454 inline BinaryOp_match<LHS, RHS, Instruction::SRem>
m_SRem(const LHS & L,const RHS & R)455 m_SRem(const LHS &L, const RHS &R) {
456 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
457 }
458
459 template<typename LHS, typename RHS>
460 inline BinaryOp_match<LHS, RHS, Instruction::FRem>
m_FRem(const LHS & L,const RHS & R)461 m_FRem(const LHS &L, const RHS &R) {
462 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
463 }
464
465 template<typename LHS, typename RHS>
466 inline BinaryOp_match<LHS, RHS, Instruction::And>
m_And(const LHS & L,const RHS & R)467 m_And(const LHS &L, const RHS &R) {
468 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
469 }
470
471 template<typename LHS, typename RHS>
472 inline BinaryOp_match<LHS, RHS, Instruction::Or>
m_Or(const LHS & L,const RHS & R)473 m_Or(const LHS &L, const RHS &R) {
474 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
475 }
476
477 template<typename LHS, typename RHS>
478 inline BinaryOp_match<LHS, RHS, Instruction::Xor>
m_Xor(const LHS & L,const RHS & R)479 m_Xor(const LHS &L, const RHS &R) {
480 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
481 }
482
483 template<typename LHS, typename RHS>
484 inline BinaryOp_match<LHS, RHS, Instruction::Shl>
m_Shl(const LHS & L,const RHS & R)485 m_Shl(const LHS &L, const RHS &R) {
486 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
487 }
488
489 template<typename LHS, typename RHS>
490 inline BinaryOp_match<LHS, RHS, Instruction::LShr>
m_LShr(const LHS & L,const RHS & R)491 m_LShr(const LHS &L, const RHS &R) {
492 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
493 }
494
495 template<typename LHS, typename RHS>
496 inline BinaryOp_match<LHS, RHS, Instruction::AShr>
m_AShr(const LHS & L,const RHS & R)497 m_AShr(const LHS &L, const RHS &R) {
498 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
499 }
500
501 //===----------------------------------------------------------------------===//
502 // Class that matches two different binary ops.
503 //
504 template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
505 struct BinOp2_match {
506 LHS_t L;
507 RHS_t R;
508
BinOp2_matchBinOp2_match509 BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
510
511 template<typename OpTy>
matchBinOp2_match512 bool match(OpTy *V) {
513 if (V->getValueID() == Value::InstructionVal + Opc1 ||
514 V->getValueID() == Value::InstructionVal + Opc2) {
515 BinaryOperator *I = cast<BinaryOperator>(V);
516 return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
517 }
518 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
519 return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
520 L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
521 return false;
522 }
523 };
524
525 /// m_Shr - Matches LShr or AShr.
526 template<typename LHS, typename RHS>
527 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
m_Shr(const LHS & L,const RHS & R)528 m_Shr(const LHS &L, const RHS &R) {
529 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
530 }
531
532 /// m_LogicalShift - Matches LShr or Shl.
533 template<typename LHS, typename RHS>
534 inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
m_LogicalShift(const LHS & L,const RHS & R)535 m_LogicalShift(const LHS &L, const RHS &R) {
536 return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
537 }
538
539 /// m_IDiv - Matches UDiv and SDiv.
540 template<typename LHS, typename RHS>
541 inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
m_IDiv(const LHS & L,const RHS & R)542 m_IDiv(const LHS &L, const RHS &R) {
543 return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
544 }
545
546 //===----------------------------------------------------------------------===//
547 // Class that matches exact binary ops.
548 //
549 template<typename SubPattern_t>
550 struct Exact_match {
551 SubPattern_t SubPattern;
552
Exact_matchExact_match553 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
554
555 template<typename OpTy>
matchExact_match556 bool match(OpTy *V) {
557 if (PossiblyExactOperator *PEO = dyn_cast<PossiblyExactOperator>(V))
558 return PEO->isExact() && SubPattern.match(V);
559 return false;
560 }
561 };
562
563 template<typename T>
m_Exact(const T & SubPattern)564 inline Exact_match<T> m_Exact(const T &SubPattern) { return SubPattern; }
565
566 //===----------------------------------------------------------------------===//
567 // Matchers for CmpInst classes
568 //
569
570 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
571 struct CmpClass_match {
572 PredicateTy &Predicate;
573 LHS_t L;
574 RHS_t R;
575
CmpClass_matchCmpClass_match576 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
577 : Predicate(Pred), L(LHS), R(RHS) {}
578
579 template<typename OpTy>
matchCmpClass_match580 bool match(OpTy *V) {
581 if (Class *I = dyn_cast<Class>(V))
582 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
583 Predicate = I->getPredicate();
584 return true;
585 }
586 return false;
587 }
588 };
589
590 template<typename LHS, typename RHS>
591 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
m_ICmp(ICmpInst::Predicate & Pred,const LHS & L,const RHS & R)592 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
593 return CmpClass_match<LHS, RHS,
594 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
595 }
596
597 template<typename LHS, typename RHS>
598 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
m_FCmp(FCmpInst::Predicate & Pred,const LHS & L,const RHS & R)599 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
600 return CmpClass_match<LHS, RHS,
601 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
602 }
603
604 //===----------------------------------------------------------------------===//
605 // Matchers for SelectInst classes
606 //
607
608 template<typename Cond_t, typename LHS_t, typename RHS_t>
609 struct SelectClass_match {
610 Cond_t C;
611 LHS_t L;
612 RHS_t R;
613
SelectClass_matchSelectClass_match614 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
615 const RHS_t &RHS)
616 : C(Cond), L(LHS), R(RHS) {}
617
618 template<typename OpTy>
matchSelectClass_match619 bool match(OpTy *V) {
620 if (SelectInst *I = dyn_cast<SelectInst>(V))
621 return C.match(I->getOperand(0)) &&
622 L.match(I->getOperand(1)) &&
623 R.match(I->getOperand(2));
624 return false;
625 }
626 };
627
628 template<typename Cond, typename LHS, typename RHS>
629 inline SelectClass_match<Cond, LHS, RHS>
m_Select(const Cond & C,const LHS & L,const RHS & R)630 m_Select(const Cond &C, const LHS &L, const RHS &R) {
631 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
632 }
633
634 /// m_SelectCst - This matches a select of two constants, e.g.:
635 /// m_SelectCst<-1, 0>(m_Value(V))
636 template<int64_t L, int64_t R, typename Cond>
637 inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
m_SelectCst(const Cond & C)638 m_SelectCst(const Cond &C) {
639 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
640 }
641
642
643 //===----------------------------------------------------------------------===//
644 // Matchers for CastInst classes
645 //
646
647 template<typename Op_t, unsigned Opcode>
648 struct CastClass_match {
649 Op_t Op;
650
CastClass_matchCastClass_match651 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
652
653 template<typename OpTy>
matchCastClass_match654 bool match(OpTy *V) {
655 if (Operator *O = dyn_cast<Operator>(V))
656 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
657 return false;
658 }
659 };
660
661 /// m_BitCast
662 template<typename OpTy>
663 inline CastClass_match<OpTy, Instruction::BitCast>
m_BitCast(const OpTy & Op)664 m_BitCast(const OpTy &Op) {
665 return CastClass_match<OpTy, Instruction::BitCast>(Op);
666 }
667
668 /// m_PtrToInt
669 template<typename OpTy>
670 inline CastClass_match<OpTy, Instruction::PtrToInt>
m_PtrToInt(const OpTy & Op)671 m_PtrToInt(const OpTy &Op) {
672 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
673 }
674
675 /// m_Trunc
676 template<typename OpTy>
677 inline CastClass_match<OpTy, Instruction::Trunc>
m_Trunc(const OpTy & Op)678 m_Trunc(const OpTy &Op) {
679 return CastClass_match<OpTy, Instruction::Trunc>(Op);
680 }
681
682 /// m_SExt
683 template<typename OpTy>
684 inline CastClass_match<OpTy, Instruction::SExt>
m_SExt(const OpTy & Op)685 m_SExt(const OpTy &Op) {
686 return CastClass_match<OpTy, Instruction::SExt>(Op);
687 }
688
689 /// m_ZExt
690 template<typename OpTy>
691 inline CastClass_match<OpTy, Instruction::ZExt>
m_ZExt(const OpTy & Op)692 m_ZExt(const OpTy &Op) {
693 return CastClass_match<OpTy, Instruction::ZExt>(Op);
694 }
695
696
697 //===----------------------------------------------------------------------===//
698 // Matchers for unary operators
699 //
700
701 template<typename LHS_t>
702 struct not_match {
703 LHS_t L;
704
not_matchnot_match705 not_match(const LHS_t &LHS) : L(LHS) {}
706
707 template<typename OpTy>
matchnot_match708 bool match(OpTy *V) {
709 if (Operator *O = dyn_cast<Operator>(V))
710 if (O->getOpcode() == Instruction::Xor)
711 return matchIfNot(O->getOperand(0), O->getOperand(1));
712 return false;
713 }
714 private:
matchIfNotnot_match715 bool matchIfNot(Value *LHS, Value *RHS) {
716 return (isa<ConstantInt>(RHS) || isa<ConstantDataVector>(RHS) ||
717 // FIXME: Remove CV.
718 isa<ConstantVector>(RHS)) &&
719 cast<Constant>(RHS)->isAllOnesValue() &&
720 L.match(LHS);
721 }
722 };
723
724 template<typename LHS>
m_Not(const LHS & L)725 inline not_match<LHS> m_Not(const LHS &L) { return L; }
726
727
728 template<typename LHS_t>
729 struct neg_match {
730 LHS_t L;
731
neg_matchneg_match732 neg_match(const LHS_t &LHS) : L(LHS) {}
733
734 template<typename OpTy>
matchneg_match735 bool match(OpTy *V) {
736 if (Operator *O = dyn_cast<Operator>(V))
737 if (O->getOpcode() == Instruction::Sub)
738 return matchIfNeg(O->getOperand(0), O->getOperand(1));
739 return false;
740 }
741 private:
matchIfNegneg_match742 bool matchIfNeg(Value *LHS, Value *RHS) {
743 return ((isa<ConstantInt>(LHS) && cast<ConstantInt>(LHS)->isZero()) ||
744 isa<ConstantAggregateZero>(LHS)) &&
745 L.match(RHS);
746 }
747 };
748
749 /// m_Neg - Match an integer negate.
750 template<typename LHS>
m_Neg(const LHS & L)751 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
752
753
754 template<typename LHS_t>
755 struct fneg_match {
756 LHS_t L;
757
fneg_matchfneg_match758 fneg_match(const LHS_t &LHS) : L(LHS) {}
759
760 template<typename OpTy>
matchfneg_match761 bool match(OpTy *V) {
762 if (Operator *O = dyn_cast<Operator>(V))
763 if (O->getOpcode() == Instruction::FSub)
764 return matchIfFNeg(O->getOperand(0), O->getOperand(1));
765 return false;
766 }
767 private:
matchIfFNegfneg_match768 bool matchIfFNeg(Value *LHS, Value *RHS) {
769 if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
770 return C->isNegativeZeroValue() && L.match(RHS);
771 return false;
772 }
773 };
774
775 /// m_FNeg - Match a floating point negate.
776 template<typename LHS>
m_FNeg(const LHS & L)777 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
778
779
780 //===----------------------------------------------------------------------===//
781 // Matchers for control flow.
782 //
783
784 struct br_match {
785 BasicBlock *&Succ;
br_matchbr_match786 br_match(BasicBlock *&Succ)
787 : Succ(Succ) {
788 }
789
790 template<typename OpTy>
matchbr_match791 bool match(OpTy *V) {
792 if (BranchInst *BI = dyn_cast<BranchInst>(V))
793 if (BI->isUnconditional()) {
794 Succ = BI->getSuccessor(0);
795 return true;
796 }
797 return false;
798 }
799 };
800
m_UnconditionalBr(BasicBlock * & Succ)801 inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
802
803 template<typename Cond_t>
804 struct brc_match {
805 Cond_t Cond;
806 BasicBlock *&T, *&F;
brc_matchbrc_match807 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
808 : Cond(C), T(t), F(f) {
809 }
810
811 template<typename OpTy>
matchbrc_match812 bool match(OpTy *V) {
813 if (BranchInst *BI = dyn_cast<BranchInst>(V))
814 if (BI->isConditional() && Cond.match(BI->getCondition())) {
815 T = BI->getSuccessor(0);
816 F = BI->getSuccessor(1);
817 return true;
818 }
819 return false;
820 }
821 };
822
823 template<typename Cond_t>
m_Br(const Cond_t & C,BasicBlock * & T,BasicBlock * & F)824 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
825 return brc_match<Cond_t>(C, T, F);
826 }
827
828
829 //===----------------------------------------------------------------------===//
830 // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
831 //
832
833 template<typename LHS_t, typename RHS_t, typename Pred_t>
834 struct MaxMin_match {
835 LHS_t L;
836 RHS_t R;
837
MaxMin_matchMaxMin_match838 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS)
839 : L(LHS), R(RHS) {}
840
841 template<typename OpTy>
matchMaxMin_match842 bool match(OpTy *V) {
843 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
844 SelectInst *SI = dyn_cast<SelectInst>(V);
845 if (!SI)
846 return false;
847 ICmpInst *Cmp = dyn_cast<ICmpInst>(SI->getCondition());
848 if (!Cmp)
849 return false;
850 // At this point we have a select conditioned on a comparison. Check that
851 // it is the values returned by the select that are being compared.
852 Value *TrueVal = SI->getTrueValue();
853 Value *FalseVal = SI->getFalseValue();
854 Value *LHS = Cmp->getOperand(0);
855 Value *RHS = Cmp->getOperand(1);
856 if ((TrueVal != LHS || FalseVal != RHS) &&
857 (TrueVal != RHS || FalseVal != LHS))
858 return false;
859 ICmpInst::Predicate Pred = LHS == TrueVal ?
860 Cmp->getPredicate() : Cmp->getSwappedPredicate();
861 // Does "(x pred y) ? x : y" represent the desired max/min operation?
862 if (!Pred_t::match(Pred))
863 return false;
864 // It does! Bind the operands.
865 return L.match(LHS) && R.match(RHS);
866 }
867 };
868
869 /// smax_pred_ty - Helper class for identifying signed max predicates.
870 struct smax_pred_ty {
matchsmax_pred_ty871 static bool match(ICmpInst::Predicate Pred) {
872 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
873 }
874 };
875
876 /// smin_pred_ty - Helper class for identifying signed min predicates.
877 struct smin_pred_ty {
matchsmin_pred_ty878 static bool match(ICmpInst::Predicate Pred) {
879 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
880 }
881 };
882
883 /// umax_pred_ty - Helper class for identifying unsigned max predicates.
884 struct umax_pred_ty {
matchumax_pred_ty885 static bool match(ICmpInst::Predicate Pred) {
886 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
887 }
888 };
889
890 /// umin_pred_ty - Helper class for identifying unsigned min predicates.
891 struct umin_pred_ty {
matchumin_pred_ty892 static bool match(ICmpInst::Predicate Pred) {
893 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
894 }
895 };
896
897 template<typename LHS, typename RHS>
898 inline MaxMin_match<LHS, RHS, smax_pred_ty>
m_SMax(const LHS & L,const RHS & R)899 m_SMax(const LHS &L, const RHS &R) {
900 return MaxMin_match<LHS, RHS, smax_pred_ty>(L, R);
901 }
902
903 template<typename LHS, typename RHS>
904 inline MaxMin_match<LHS, RHS, smin_pred_ty>
m_SMin(const LHS & L,const RHS & R)905 m_SMin(const LHS &L, const RHS &R) {
906 return MaxMin_match<LHS, RHS, smin_pred_ty>(L, R);
907 }
908
909 template<typename LHS, typename RHS>
910 inline MaxMin_match<LHS, RHS, umax_pred_ty>
m_UMax(const LHS & L,const RHS & R)911 m_UMax(const LHS &L, const RHS &R) {
912 return MaxMin_match<LHS, RHS, umax_pred_ty>(L, R);
913 }
914
915 template<typename LHS, typename RHS>
916 inline MaxMin_match<LHS, RHS, umin_pred_ty>
m_UMin(const LHS & L,const RHS & R)917 m_UMin(const LHS &L, const RHS &R) {
918 return MaxMin_match<LHS, RHS, umin_pred_ty>(L, R);
919 }
920
921 template<typename Opnd_t>
922 struct Argument_match {
923 unsigned OpI;
924 Opnd_t Val;
Argument_matchArgument_match925 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) { }
926
927 template<typename OpTy>
matchArgument_match928 bool match(OpTy *V) {
929 CallSite CS(V);
930 return CS.isCall() && Val.match(CS.getArgument(OpI));
931 }
932 };
933
934 /// Match an argument
935 template<unsigned OpI, typename Opnd_t>
m_Argument(const Opnd_t & Op)936 inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
937 return Argument_match<Opnd_t>(OpI, Op);
938 }
939
940 /// Intrinsic matchers.
941 struct IntrinsicID_match {
942 unsigned ID;
IntrinsicID_matchIntrinsicID_match943 IntrinsicID_match(unsigned IntrID) : ID(IntrID) { }
944
945 template<typename OpTy>
matchIntrinsicID_match946 bool match(OpTy *V) {
947 IntrinsicInst *II = dyn_cast<IntrinsicInst>(V);
948 return II && II->getIntrinsicID() == ID;
949 }
950 };
951
952 /// Intrinsic matches are combinations of ID matchers, and argument
953 /// matchers. Higher arity matcher are defined recursively in terms of and-ing
954 /// them with lower arity matchers. Here's some convenient typedefs for up to
955 /// several arguments, and more can be added as needed
956 template <typename T0 = void, typename T1 = void, typename T2 = void,
957 typename T3 = void, typename T4 = void, typename T5 = void,
958 typename T6 = void, typename T7 = void, typename T8 = void,
959 typename T9 = void, typename T10 = void> struct m_Intrinsic_Ty;
960 template <typename T0>
961 struct m_Intrinsic_Ty<T0> {
962 typedef match_combine_and<IntrinsicID_match, Argument_match<T0> > Ty;
963 };
964 template <typename T0, typename T1>
965 struct m_Intrinsic_Ty<T0, T1> {
966 typedef match_combine_and<typename m_Intrinsic_Ty<T0>::Ty,
967 Argument_match<T1> > Ty;
968 };
969 template <typename T0, typename T1, typename T2>
970 struct m_Intrinsic_Ty<T0, T1, T2> {
971 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
972 Argument_match<T2> > Ty;
973 };
974 template <typename T0, typename T1, typename T2, typename T3>
975 struct m_Intrinsic_Ty<T0, T1, T2, T3> {
976 typedef match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
977 Argument_match<T3> > Ty;
978 };
979
980 /// Match intrinsic calls like this:
981 /// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
982 template <unsigned IntrID>
983 inline IntrinsicID_match
984 m_Intrinsic() { return IntrinsicID_match(IntrID); }
985
986 template<unsigned IntrID, typename T0>
987 inline typename m_Intrinsic_Ty<T0>::Ty
988 m_Intrinsic(const T0 &Op0) {
989 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
990 }
991
992 template<unsigned IntrID, typename T0, typename T1>
993 inline typename m_Intrinsic_Ty<T0, T1>::Ty
994 m_Intrinsic(const T0 &Op0, const T1 &Op1) {
995 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
996 }
997
998 template<unsigned IntrID, typename T0, typename T1, typename T2>
999 inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
1000 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
1001 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
1002 }
1003
1004 template<unsigned IntrID, typename T0, typename T1, typename T2, typename T3>
1005 inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
1006 m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
1007 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
1008 }
1009
1010 // Helper intrinsic matching specializations
1011 template<typename Opnd0>
1012 inline typename m_Intrinsic_Ty<Opnd0>::Ty
1013 m_BSwap(const Opnd0 &Op0) {
1014 return m_Intrinsic<Intrinsic::bswap>(Op0);
1015 }
1016
1017 } // end namespace PatternMatch
1018 } // end namespace llvm
1019
1020 #endif
1021