1 //===-- llvm/Support/PatternMatch.h - Match on the LLVM IR ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
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 ...
24 // This is primarily useful to things like the instruction combiner, but can
25 // also be useful for static analysis tools or code generators.
27 //===----------------------------------------------------------------------===//
29 #ifndef LLVM_SUPPORT_PATTERNMATCH_H
30 #define LLVM_SUPPORT_PATTERNMATCH_H
32 #include "llvm/Constants.h"
33 #include "llvm/Instructions.h"
36 namespace PatternMatch {
38 template<typename Val, typename Pattern>
39 bool match(Val *V, const Pattern &P) {
40 return const_cast<Pattern&>(P).match(V);
43 template<typename Class>
45 template<typename ITy>
46 bool match(ITy *V) { return isa<Class>(V); }
49 /// m_Value() - Match an arbitrary value and ignore it.
50 inline leaf_ty<Value> m_Value() { return leaf_ty<Value>(); }
51 /// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
52 inline leaf_ty<ConstantInt> m_ConstantInt() { return leaf_ty<ConstantInt>(); }
55 struct constantint_ty {
56 template<typename ITy>
58 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
59 const APInt &CIV = CI->getValue();
61 return CIV == static_cast<uint64_t>(Val);
62 // If Val is negative, and CI is shorter than it, truncate to the right
63 // number of bits. If it is larger, then we have to sign extend. Just
64 // compare their negated values.
71 /// m_ConstantInt(int64_t) - Match a ConstantInt with a specific value
74 inline constantint_ty<Val> m_ConstantInt() {
75 return constantint_ty<Val>();
79 template<typename ITy>
81 if (const Constant *C = dyn_cast<Constant>(V))
82 return C->isNullValue();
87 /// m_Zero() - Match an arbitrary zero/null constant.
88 inline zero_ty m_Zero() { return zero_ty(); }
91 template<typename ITy>
93 if (const ConstantInt *C = dyn_cast<ConstantInt>(V))
99 /// m_One() - Match an integer 1.
100 inline one_ty m_One() { return one_ty(); }
103 template<typename ITy>
105 if (const ConstantInt *C = dyn_cast<ConstantInt>(V))
106 return C->isAllOnesValue();
107 if (const ConstantVector *C = dyn_cast<ConstantVector>(V))
108 return C->isAllOnesValue();
113 /// m_AllOnes() - Match an integer or vector with all bits set to true.
114 inline all_ones_ty m_AllOnes() { return all_ones_ty(); }
117 template<typename Class>
120 bind_ty(Class *&V) : VR(V) {}
122 template<typename ITy>
124 if (Class *CV = dyn_cast<Class>(V)) {
132 /// m_Value - Match a value, capturing it if we match.
133 inline bind_ty<Value> m_Value(Value *&V) { return V; }
135 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
136 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
138 /// specificval_ty - Match a specified Value*.
139 struct specificval_ty {
141 specificval_ty(const Value *V) : Val(V) {}
143 template<typename ITy>
149 /// m_Specific - Match if we have a specific specified value.
150 inline specificval_ty m_Specific(const Value *V) { return V; }
153 //===----------------------------------------------------------------------===//
154 // Matchers for specific binary operators.
157 template<typename LHS_t, typename RHS_t,
158 unsigned Opcode, typename ConcreteTy = BinaryOperator>
159 struct BinaryOp_match {
163 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
165 template<typename OpTy>
166 bool match(OpTy *V) {
167 if (V->getValueID() == Value::InstructionVal + Opcode) {
168 ConcreteTy *I = cast<ConcreteTy>(V);
169 return I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
170 R.match(I->getOperand(1));
172 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
173 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
174 R.match(CE->getOperand(1));
179 template<typename LHS, typename RHS>
180 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
182 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
185 template<typename LHS, typename RHS>
186 inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
188 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
191 template<typename LHS, typename RHS>
192 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
194 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
197 template<typename LHS, typename RHS>
198 inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
200 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
203 template<typename LHS, typename RHS>
204 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
206 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
209 template<typename LHS, typename RHS>
210 inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
212 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
215 template<typename LHS, typename RHS>
216 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
218 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
221 template<typename LHS, typename RHS>
222 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
224 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
227 template<typename LHS, typename RHS>
228 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
230 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
233 template<typename LHS, typename RHS>
234 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
236 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
239 template<typename LHS, typename RHS>
240 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
242 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
245 template<typename LHS, typename RHS>
246 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
248 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
251 template<typename LHS, typename RHS>
252 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
254 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
257 template<typename LHS, typename RHS>
258 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
260 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
263 template<typename LHS, typename RHS>
264 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
266 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
269 template<typename LHS, typename RHS>
270 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
272 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
275 template<typename LHS, typename RHS>
276 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
278 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
281 template<typename LHS, typename RHS>
282 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
284 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
287 //===----------------------------------------------------------------------===//
288 // Matchers for either AShr or LShr .. for convenience
290 template<typename LHS_t, typename RHS_t, typename ConcreteTy = BinaryOperator>
295 Shr_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
297 template<typename OpTy>
298 bool match(OpTy *V) {
299 if (V->getValueID() == Value::InstructionVal + Instruction::LShr ||
300 V->getValueID() == Value::InstructionVal + Instruction::AShr) {
301 ConcreteTy *I = cast<ConcreteTy>(V);
302 return (I->getOpcode() == Instruction::AShr ||
303 I->getOpcode() == Instruction::LShr) &&
304 L.match(I->getOperand(0)) &&
305 R.match(I->getOperand(1));
307 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
308 return (CE->getOpcode() == Instruction::LShr ||
309 CE->getOpcode() == Instruction::AShr) &&
310 L.match(CE->getOperand(0)) &&
311 R.match(CE->getOperand(1));
316 template<typename LHS, typename RHS>
317 inline Shr_match<LHS, RHS> m_Shr(const LHS &L, const RHS &R) {
318 return Shr_match<LHS, RHS>(L, R);
321 //===----------------------------------------------------------------------===//
322 // Matchers for binary classes
325 template<typename LHS_t, typename RHS_t, typename Class, typename OpcType>
326 struct BinaryOpClass_match {
331 BinaryOpClass_match(OpcType &Op, const LHS_t &LHS,
333 : Opcode(&Op), L(LHS), R(RHS) {}
334 BinaryOpClass_match(const LHS_t &LHS, const RHS_t &RHS)
335 : Opcode(0), L(LHS), R(RHS) {}
337 template<typename OpTy>
338 bool match(OpTy *V) {
339 if (Class *I = dyn_cast<Class>(V))
340 if (L.match(I->getOperand(0)) &&
341 R.match(I->getOperand(1))) {
343 *Opcode = I->getOpcode();
346 #if 0 // Doesn't handle constantexprs yet!
347 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
348 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
349 R.match(CE->getOperand(1));
355 template<typename LHS, typename RHS>
356 inline BinaryOpClass_match<LHS, RHS, BinaryOperator, Instruction::BinaryOps>
357 m_Shift(Instruction::BinaryOps &Op, const LHS &L, const RHS &R) {
358 return BinaryOpClass_match<LHS, RHS,
359 BinaryOperator, Instruction::BinaryOps>(Op, L, R);
362 template<typename LHS, typename RHS>
363 inline BinaryOpClass_match<LHS, RHS, BinaryOperator, Instruction::BinaryOps>
364 m_Shift(const LHS &L, const RHS &R) {
365 return BinaryOpClass_match<LHS, RHS,
366 BinaryOperator, Instruction::BinaryOps>(L, R);
369 //===----------------------------------------------------------------------===//
370 // Matchers for CmpInst classes
373 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
374 struct CmpClass_match {
375 PredicateTy &Predicate;
379 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS,
381 : Predicate(Pred), L(LHS), R(RHS) {}
383 template<typename OpTy>
384 bool match(OpTy *V) {
385 if (Class *I = dyn_cast<Class>(V))
386 if (L.match(I->getOperand(0)) &&
387 R.match(I->getOperand(1))) {
388 Predicate = I->getPredicate();
395 template<typename LHS, typename RHS>
396 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
397 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
398 return CmpClass_match<LHS, RHS,
399 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
402 template<typename LHS, typename RHS>
403 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
404 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
405 return CmpClass_match<LHS, RHS,
406 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
409 //===----------------------------------------------------------------------===//
410 // Matchers for SelectInst classes
413 template<typename Cond_t, typename LHS_t, typename RHS_t>
414 struct SelectClass_match {
419 SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
421 : C(Cond), L(LHS), R(RHS) {}
423 template<typename OpTy>
424 bool match(OpTy *V) {
425 if (SelectInst *I = dyn_cast<SelectInst>(V))
426 return C.match(I->getOperand(0)) &&
427 L.match(I->getOperand(1)) &&
428 R.match(I->getOperand(2));
433 template<typename Cond, typename LHS, typename RHS>
434 inline SelectClass_match<Cond, LHS, RHS>
435 m_Select(const Cond &C, const LHS &L, const RHS &R) {
436 return SelectClass_match<Cond, LHS, RHS>(C, L, R);
439 /// m_SelectCst - This matches a select of two constants, e.g.:
440 /// m_SelectCst<-1, 0>(m_Value(V))
441 template<int64_t L, int64_t R, typename Cond>
442 inline SelectClass_match<Cond, constantint_ty<L>, constantint_ty<R> >
443 m_SelectCst(const Cond &C) {
444 return SelectClass_match<Cond, constantint_ty<L>,
445 constantint_ty<R> >(C, m_ConstantInt<L>(),
450 //===----------------------------------------------------------------------===//
451 // Matchers for CastInst classes
454 template<typename Op_t, unsigned Opcode>
455 struct CastClass_match {
458 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
460 template<typename OpTy>
461 bool match(OpTy *V) {
462 if (CastInst *I = dyn_cast<CastInst>(V))
463 return I->getOpcode() == Opcode && Op.match(I->getOperand(0));
464 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
465 return CE->getOpcode() == Opcode && Op.match(CE->getOperand(0));
471 template<typename OpTy>
472 inline CastClass_match<OpTy, Instruction::BitCast>
473 m_BitCast(const OpTy &Op) {
474 return CastClass_match<OpTy, Instruction::BitCast>(Op);
478 template<typename OpTy>
479 inline CastClass_match<OpTy, Instruction::PtrToInt>
480 m_PtrToInt(const OpTy &Op) {
481 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
485 template<typename OpTy>
486 inline CastClass_match<OpTy, Instruction::Trunc>
487 m_Trunc(const OpTy &Op) {
488 return CastClass_match<OpTy, Instruction::Trunc>(Op);
492 template<typename OpTy>
493 inline CastClass_match<OpTy, Instruction::SExt>
494 m_SExt(const OpTy &Op) {
495 return CastClass_match<OpTy, Instruction::SExt>(Op);
499 template<typename OpTy>
500 inline CastClass_match<OpTy, Instruction::ZExt>
501 m_ZExt(const OpTy &Op) {
502 return CastClass_match<OpTy, Instruction::ZExt>(Op);
506 //===----------------------------------------------------------------------===//
507 // Matchers for unary operators
510 template<typename LHS_t>
514 not_match(const LHS_t &LHS) : L(LHS) {}
516 template<typename OpTy>
517 bool match(OpTy *V) {
518 if (Instruction *I = dyn_cast<Instruction>(V))
519 if (I->getOpcode() == Instruction::Xor)
520 return matchIfNot(I->getOperand(0), I->getOperand(1));
521 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
522 if (CE->getOpcode() == Instruction::Xor)
523 return matchIfNot(CE->getOperand(0), CE->getOperand(1));
524 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
525 return L.match(ConstantExpr::getNot(CI));
529 bool matchIfNot(Value *LHS, Value *RHS) {
530 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
531 return CI->isAllOnesValue() && L.match(LHS);
532 if (ConstantInt *CI = dyn_cast<ConstantInt>(LHS))
533 return CI->isAllOnesValue() && L.match(RHS);
534 if (ConstantVector *CV = dyn_cast<ConstantVector>(RHS))
535 return CV->isAllOnesValue() && L.match(LHS);
536 if (ConstantVector *CV = dyn_cast<ConstantVector>(LHS))
537 return CV->isAllOnesValue() && L.match(RHS);
542 template<typename LHS>
543 inline not_match<LHS> m_Not(const LHS &L) { return L; }
546 template<typename LHS_t>
550 neg_match(const LHS_t &LHS) : L(LHS) {}
552 template<typename OpTy>
553 bool match(OpTy *V) {
554 if (Instruction *I = dyn_cast<Instruction>(V))
555 if (I->getOpcode() == Instruction::Sub)
556 return matchIfNeg(I->getOperand(0), I->getOperand(1));
557 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
558 if (CE->getOpcode() == Instruction::Sub)
559 return matchIfNeg(CE->getOperand(0), CE->getOperand(1));
560 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
561 return L.match(ConstantExpr::getNeg(CI));
565 bool matchIfNeg(Value *LHS, Value *RHS) {
566 return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
571 template<typename LHS>
572 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
575 template<typename LHS_t>
579 fneg_match(const LHS_t &LHS) : L(LHS) {}
581 template<typename OpTy>
582 bool match(OpTy *V) {
583 if (Instruction *I = dyn_cast<Instruction>(V))
584 if (I->getOpcode() == Instruction::FSub)
585 return matchIfFNeg(I->getOperand(0), I->getOperand(1));
586 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
587 if (CE->getOpcode() == Instruction::FSub)
588 return matchIfFNeg(CE->getOperand(0), CE->getOperand(1));
589 if (ConstantFP *CF = dyn_cast<ConstantFP>(V))
590 return L.match(ConstantExpr::getFNeg(CF));
594 bool matchIfFNeg(Value *LHS, Value *RHS) {
595 return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
600 template<typename LHS>
601 inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
604 //===----------------------------------------------------------------------===//
605 // Matchers for control flow
608 template<typename Cond_t>
612 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
613 : Cond(C), T(t), F(f) {
616 template<typename OpTy>
617 bool match(OpTy *V) {
618 if (BranchInst *BI = dyn_cast<BranchInst>(V))
619 if (BI->isConditional()) {
620 if (Cond.match(BI->getCondition())) {
621 T = BI->getSuccessor(0);
622 F = BI->getSuccessor(1);
630 template<typename Cond_t>
631 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
632 return brc_match<Cond_t>(C, T, F);
635 } // end namespace PatternMatch
636 } // end namespace llvm