f4c9ad50987114fe5c785ccad501293b37eb351f
[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, 
75          unsigned Opcode, typename ConcreteTy = BinaryOperator>
76 struct BinaryOp_match {
77   LHS_t L;
78   RHS_t R;
79
80   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
81
82   template<typename OpTy>
83   bool match(OpTy *V) {
84     if (V->getValueType() == Value::InstructionVal + Opcode) {
85       ConcreteTy *I = cast<ConcreteTy>(V);
86       return I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
87              R.match(I->getOperand(1));
88     }
89     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
90       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
91              R.match(CE->getOperand(1));
92     return false;
93   }
94 };
95
96 template<typename LHS, typename RHS>
97 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
98                                                         const RHS &R) {
99   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
100 }
101
102 template<typename LHS, typename RHS>
103 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
104                                                         const RHS &R) {
105   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
106 }
107
108 template<typename LHS, typename RHS>
109 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
110                                                         const RHS &R) {
111   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
112 }
113
114 template<typename LHS, typename RHS>
115 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
116                                                         const RHS &R) {
117   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
118 }
119
120 template<typename LHS, typename RHS>
121 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
122                                                         const RHS &R) {
123   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
124 }
125
126 template<typename LHS, typename RHS>
127 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
128                                                         const RHS &R) {
129   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
130 }
131
132 template<typename LHS, typename RHS>
133 inline BinaryOp_match<LHS, RHS, Instruction::Rem> m_Rem(const LHS &L,
134                                                         const RHS &R) {
135   return BinaryOp_match<LHS, RHS, Instruction::Rem>(L, R);
136 }
137
138 template<typename LHS, typename RHS>
139 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
140                                                         const RHS &R) {
141   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
142 }
143
144 template<typename LHS, typename RHS>
145 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
146                                                       const RHS &R) {
147   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
148 }
149
150 template<typename LHS, typename RHS>
151 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
152                                                         const RHS &R) {
153   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
154 }
155
156 template<typename LHS, typename RHS>
157 inline BinaryOp_match<LHS, RHS, Instruction::Shl, 
158                       ShiftInst> m_Shl(const LHS &L, const RHS &R) {
159   return BinaryOp_match<LHS, RHS, Instruction::Shl, ShiftInst>(L, R);
160 }
161
162 template<typename LHS, typename RHS>
163 inline BinaryOp_match<LHS, RHS, Instruction::Shr, 
164                       ShiftInst> m_Shr(const LHS &L, const RHS &R) {
165   return BinaryOp_match<LHS, RHS, Instruction::Shr, ShiftInst>(L, R);
166 }
167
168 //===----------------------------------------------------------------------===//
169 // Matchers for binary classes
170 //
171
172 template<typename LHS_t, typename RHS_t, typename Class, typename OpcType>
173 struct BinaryOpClass_match {
174   OpcType &Opcode;
175   LHS_t L;
176   RHS_t R;
177
178   BinaryOpClass_match(OpcType &Op, const LHS_t &LHS,
179                       const RHS_t &RHS)
180     : Opcode(Op), L(LHS), R(RHS) {}
181
182   template<typename OpTy>
183   bool match(OpTy *V) {
184     if (Class *I = dyn_cast<Class>(V))
185       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
186         Opcode = I->getOpcode();
187         return true;
188       }
189 #if 0  // Doesn't handle constantexprs yet!
190     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
191       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
192              R.match(CE->getOperand(1));
193 #endif
194     return false;
195   }
196 };
197
198 template<typename LHS, typename RHS>
199 inline BinaryOpClass_match<LHS, RHS, SetCondInst, Instruction::BinaryOps>
200 m_SetCond(Instruction::BinaryOps &Op, const LHS &L, const RHS &R) {
201   return BinaryOpClass_match<LHS, RHS, 
202                              SetCondInst, Instruction::BinaryOps>(Op, L, R);
203 }
204
205 template<typename LHS, typename RHS>
206 inline BinaryOpClass_match<LHS, RHS, ShiftInst, Instruction::OtherOps>
207 m_Shift(Instruction::OtherOps &Op, const LHS &L, const RHS &R) {
208   return BinaryOpClass_match<LHS, RHS, 
209                              ShiftInst, Instruction::OtherOps>(Op, L, R);
210 }
211
212 template<typename LHS, typename RHS>
213 inline BinaryOpClass_match<LHS, RHS, ShiftInst, Instruction::OtherOps>
214 m_Shift(const LHS &L, const RHS &R) {
215   Instruction::OtherOps Op; 
216   return BinaryOpClass_match<LHS, RHS, 
217                              ShiftInst, Instruction::OtherOps>(Op, L, R);
218 }
219
220 //===----------------------------------------------------------------------===//
221 // Matchers for unary operators
222 //
223
224 template<typename LHS_t>
225 struct neg_match {
226   LHS_t L;
227
228   neg_match(const LHS_t &LHS) : L(LHS) {}
229
230   template<typename OpTy>
231   bool match(OpTy *V) {
232     if (Instruction *I = dyn_cast<Instruction>(V))
233       if (I->getOpcode() == Instruction::Sub)
234         return matchIfNeg(I->getOperand(0), I->getOperand(1));
235     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
236       if (CE->getOpcode() == Instruction::Sub)
237         return matchIfNeg(CE->getOperand(0), CE->getOperand(1));
238     if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
239       return L.match(ConstantExpr::getNeg(CI));
240     return false;
241   }
242 private:
243   bool matchIfNeg(Value *LHS, Value *RHS) {
244     if (!LHS->getType()->isFloatingPoint())
245       return LHS == Constant::getNullValue(LHS->getType()) && L.match(RHS);
246     else
247       return LHS == ConstantFP::get(LHS->getType(), -0.0) && L.match(RHS);
248   }
249 };
250
251 template<typename LHS>
252 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
253
254
255 template<typename LHS_t>
256 struct not_match {
257   LHS_t L;
258
259   not_match(const LHS_t &LHS) : L(LHS) {}
260
261   template<typename OpTy>
262   bool match(OpTy *V) {
263     if (Instruction *I = dyn_cast<Instruction>(V))
264       if (I->getOpcode() == Instruction::Xor)
265         return matchIfNot(I->getOperand(0), I->getOperand(1));
266     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
267       if (CE->getOpcode() == Instruction::Xor)
268         return matchIfNot(CE->getOperand(0), CE->getOperand(1));
269     if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
270       return L.match(ConstantExpr::getNot(CI));
271     return false;
272   }
273 private:
274   bool matchIfNot(Value *LHS, Value *RHS) {
275     if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(RHS))
276       return CI->isAllOnesValue() && L.match(LHS);
277     else if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(LHS))
278       return CI->isAllOnesValue() && L.match(RHS);
279     return false;
280   }
281 };
282
283 template<typename LHS>
284 inline not_match<LHS> m_Not(const LHS &L) { return L; }
285
286
287 template<typename Op_t>
288 struct cast_match {
289   Op_t Op;
290   const Type **DestTy;
291   
292   cast_match(const Op_t &op, const Type **destTy) : Op(op), DestTy(destTy) {}
293   
294   template<typename OpTy>
295   bool match(OpTy *V) {
296     if (CastInst *I = dyn_cast<CastInst>(V)) {
297       if (DestTy) *DestTy = I->getType();
298       return Op.match(I->getOperand(0));
299     } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
300       if (CE->getOpcode() == Instruction::Cast) {
301         if (DestTy) *DestTy = I->getType();
302         return Op.match(CE->getOperand(0));
303       }
304     }
305     return false;
306   }
307 };
308
309 template<typename Op_t>
310 inline cast_match<Op_t> m_Cast(const Op_t &Op, const Type *&Ty) {
311   return cast_match<Op_t>(Op, &Ty);
312 }
313 template<typename Op_t>
314 inline cast_match<Op_t> m_Cast(const Op_t &Op) {
315   return cast_match<Op_t>(Op, 0);
316 }
317
318
319 //===----------------------------------------------------------------------===//
320 // Matchers for control flow
321 //
322
323 template<typename Cond_t>
324 struct brc_match {
325   Cond_t Cond;
326   BasicBlock *&T, *&F;
327   brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
328     : Cond(C), T(t), F(f) {
329   }
330
331   template<typename OpTy>
332   bool match(OpTy *V) {
333     if (BranchInst *BI = dyn_cast<BranchInst>(V))
334       if (BI->isConditional()) {
335         if (Cond.match(BI->getCondition())) {
336           T = BI->getSuccessor(0);
337           F = BI->getSuccessor(1);
338           return true;
339         }
340       }
341     return false;
342   }
343 };
344
345 template<typename Cond_t>
346 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F){
347   return brc_match<Cond_t>(C, T, F);
348 }
349
350
351 }} // end llvm::match
352
353
354 #endif
355