Fix #include flavor
[oota-llvm.git] / include / llvm / Support / PatternMatch.h
1 //===-- llvm/Support/PatternMatch.h - Match on the LLVM IR ------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
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 is allows to you
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/Constants.h"
33 #include "llvm/Instructions.h"
34
35 namespace llvm {
36 namespace PatternMatch { 
37
38 template<typename Val, typename Pattern>
39 bool match(Val *V, const Pattern &P) {
40   return const_cast<Pattern&>(P).match(V);
41 }
42
43 template<typename Class>
44 struct leaf_ty {
45   template<typename ITy>
46   bool match(ITy *V) { return isa<Class>(V); }
47 };
48
49 inline leaf_ty<Value> m_Value() { return leaf_ty<Value>(); }
50 inline leaf_ty<ConstantInt> m_ConstantInt() { return leaf_ty<ConstantInt>(); }
51
52 template<typename Class>
53 struct bind_ty {
54   Class *&VR;
55   bind_ty(Class *&V) : VR(V) {}
56
57   template<typename ITy>
58   bool match(ITy *V) {
59     if (Class *CV = dyn_cast<Class>(V)) {
60       VR = CV;
61       return true;
62     }
63     return false;
64   }
65 };
66
67 inline bind_ty<Value> m_Value(Value *&V) { return V; }
68 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
69
70 //===----------------------------------------------------------------------===//
71 // Matchers for specific binary operators
72 //
73
74 template<typename LHS_t, typename RHS_t, unsigned Opcode>
75 struct BinaryOp_match {
76   LHS_t L;
77   RHS_t R;
78
79   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
80
81   template<typename OpTy>
82   bool match(OpTy *V) {
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));
89     return false;
90   }
91 }; 
92
93 template<typename LHS, typename RHS>
94 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
95                                                         const RHS &R) {
96   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
97 }
98
99 template<typename LHS, typename RHS>
100 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
101                                                         const RHS &R) {
102   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
103 }
104
105 template<typename LHS, typename RHS>
106 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
107                                                         const RHS &R) {
108   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
109 }
110
111 template<typename LHS, typename RHS>
112 inline BinaryOp_match<LHS, RHS, Instruction::Div> m_Div(const LHS &L,
113                                                         const RHS &R) {
114   return BinaryOp_match<LHS, RHS, Instruction::Div>(L, R);
115 }
116
117 template<typename LHS, typename RHS>
118 inline BinaryOp_match<LHS, RHS, Instruction::Rem> m_Rem(const LHS &L,
119                                                         const RHS &R) {
120   return BinaryOp_match<LHS, RHS, Instruction::Rem>(L, R);
121 }
122
123 template<typename LHS, typename RHS>
124 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
125                                                         const RHS &R) {
126   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
127 }
128
129 template<typename LHS, typename RHS>
130 inline BinaryOp_match<LHS, RHS, Instruction::Rem> m_Or(const LHS &L,
131                                                        const RHS &R) {
132   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
133 }
134
135 template<typename LHS, typename RHS>
136 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
137                                                         const RHS &R) {
138   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
139 }
140
141 //===----------------------------------------------------------------------===//
142 // Matchers for binary classes
143 //
144
145 template<typename LHS_t, typename RHS_t, typename Class>
146 struct BinaryOpClass_match {
147   Instruction::BinaryOps &Opcode;
148   LHS_t L;
149   RHS_t R;
150
151   BinaryOpClass_match(Instruction::BinaryOps &Op, const LHS_t &LHS,
152                       const RHS_t &RHS)
153     : Opcode(Op), L(LHS), R(RHS) {}
154
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();
160         return true;
161       }
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));
166 #endif
167     return false;
168   }
169 }; 
170
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);
175 }
176
177
178 //===----------------------------------------------------------------------===//
179 // Matchers for unary operators
180 //
181
182 template<typename LHS_t>
183 struct neg_match {
184   LHS_t L;
185
186   neg_match(const LHS_t &LHS) : L(LHS) {}
187
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));
198     return false;
199   }
200 private:
201   bool matchIfNeg(Value *LHS, Value *RHS) {
202     if (!LHS->getType()->isFloatingPoint())
203       return LHS == Constant::getNullValue(LHS->getType()) && L.match(RHS);
204     else
205       return LHS == ConstantFP::get(LHS->getType(), -0.0) && L.match(RHS);
206   }
207 }; 
208
209 template<typename LHS>
210 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
211
212
213 template<typename LHS_t>
214 struct not_match {
215   LHS_t L;
216
217   not_match(const LHS_t &LHS) : L(LHS) {}
218
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));
229     return false;
230   }
231 private:
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);
237     return false;
238   }
239 };
240
241 template<typename LHS>
242 inline not_match<LHS> m_Not(const LHS &L) { return L; }
243
244 //===----------------------------------------------------------------------===//
245 // Matchers for control flow
246 //
247
248 template<typename Cond_t>
249 struct brc_match {
250   Cond_t Cond;
251   BasicBlock *&T, *&F;
252   brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
253     : Cond(C), T(t), F(f) {
254   }
255
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);
263           return true;
264         }
265       }
266     return false;
267   }
268 };
269
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);
273 }
274
275
276 }} // end llvm::match
277
278
279 #endif
280