1 //===-- llvm/Support/PatternMatch.h - Match on the LLVM IR ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 is allows to you
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 inline leaf_ty<Value> m_Value() { return leaf_ty<Value>(); }
50 inline leaf_ty<ConstantInt> m_ConstantInt() { return leaf_ty<ConstantInt>(); }
52 template<typename Class>
55 bind_ty(Class *&V) : VR(V) {}
57 template<typename ITy>
59 if (Class *CV = dyn_cast<Class>(V)) {
67 inline bind_ty<Value> m_Value(Value *&V) { return V; }
68 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
70 //===----------------------------------------------------------------------===//
71 // Matchers for specific binary operators
74 template<typename LHS_t, typename RHS_t, unsigned Opcode>
75 struct BinaryOp_match {
79 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
81 template<typename OpTy>
83 if (Instruction *I = dyn_cast<Instruction>(V))
84 return I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
85 R.match(I->getOperand(1));
86 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
87 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
88 R.match(CE->getOperand(1));
93 template<typename LHS, typename RHS>
94 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
96 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
99 template<typename LHS, typename RHS>
100 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
102 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
105 template<typename LHS, typename RHS>
106 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
108 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
111 template<typename LHS, typename RHS>
112 inline BinaryOp_match<LHS, RHS, Instruction::Div> m_Div(const LHS &L,
114 return BinaryOp_match<LHS, RHS, Instruction::Div>(L, R);
117 template<typename LHS, typename RHS>
118 inline BinaryOp_match<LHS, RHS, Instruction::Rem> m_Rem(const LHS &L,
120 return BinaryOp_match<LHS, RHS, Instruction::Rem>(L, R);
123 template<typename LHS, typename RHS>
124 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
126 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
129 template<typename LHS, typename RHS>
130 inline BinaryOp_match<LHS, RHS, Instruction::Rem> m_Or(const LHS &L,
132 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
135 template<typename LHS, typename RHS>
136 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
138 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
141 //===----------------------------------------------------------------------===//
142 // Matchers for binary classes
145 template<typename LHS_t, typename RHS_t, typename Class>
146 struct BinaryOpClass_match {
147 Instruction::BinaryOps &Opcode;
151 BinaryOpClass_match(Instruction::BinaryOps &Op, const LHS_t &LHS,
153 : Opcode(Op), L(LHS), R(RHS) {}
155 template<typename OpTy>
156 bool match(OpTy *V) {
157 if (Class *I = dyn_cast<Class>(V))
158 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
159 Opcode = I->getOpcode();
162 #if 0 // Doesn't handle constantexprs yet!
163 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
164 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
165 R.match(CE->getOperand(1));
171 template<typename LHS, typename RHS>
172 inline BinaryOpClass_match<LHS, RHS, SetCondInst>
173 m_SetCond(Instruction::BinaryOps &Op, const LHS &L, const RHS &R) {
174 return BinaryOpClass_match<LHS, RHS, SetCondInst>(Op, L, R);
178 //===----------------------------------------------------------------------===//
179 // Matchers for unary operators
182 template<typename LHS_t>
186 neg_match(const LHS_t &LHS) : L(LHS) {}
188 template<typename OpTy>
189 bool match(OpTy *V) {
190 if (Instruction *I = dyn_cast<Instruction>(V))
191 if (I->getOpcode() == Instruction::Sub)
192 return matchIfNeg(I->getOperand(0), I->getOperand(1));
193 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
194 if (CE->getOpcode() == Instruction::Sub)
195 return matchIfNeg(CE->getOperand(0), CE->getOperand(1));
196 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
197 return L.match(ConstantExpr::getNeg(CI));
201 bool matchIfNeg(Value *LHS, Value *RHS) {
202 if (!LHS->getType()->isFloatingPoint())
203 return LHS == Constant::getNullValue(LHS->getType()) && L.match(RHS);
205 return LHS == ConstantFP::get(LHS->getType(), -0.0) && L.match(RHS);
209 template<typename LHS>
210 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
213 template<typename LHS_t>
217 not_match(const LHS_t &LHS) : L(LHS) {}
219 template<typename OpTy>
220 bool match(OpTy *V) {
221 if (Instruction *I = dyn_cast<Instruction>(V))
222 if (I->getOpcode() == Instruction::Xor)
223 return matchIfNot(I->getOperand(0), I->getOperand(1));
224 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
225 if (CE->getOpcode() == Instruction::Xor)
226 return matchIfNot(CE->getOperand(0), CE->getOperand(1));
227 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
228 return L.match(ConstantExpr::getNot(CI));
232 bool matchIfNot(Value *LHS, Value *RHS) {
233 if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(RHS))
234 return CI->isAllOnesValue() && L.match(LHS);
235 else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(LHS))
236 return CI->isAllOnesValue() && L.match(RHS);
241 template<typename LHS>
242 inline not_match<LHS> m_Not(const LHS &L) { return L; }
244 //===----------------------------------------------------------------------===//
245 // Matchers for control flow
248 template<typename Cond_t>
252 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
253 : Cond(C), T(t), F(f) {
256 template<typename OpTy>
257 bool match(OpTy *V) {
258 if (BranchInst *BI = dyn_cast<BranchInst>(V))
259 if (BI->isConditional()) {
260 if (Cond.match(BI->getCondition())) {
261 T = BI->getSuccessor(0);
262 F = BI->getSuccessor(1);
270 template<typename Cond_t>
271 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F){
272 return brc_match<Cond_t>(C, T, F);
276 }} // end llvm::match