1cc59952727aca2fefd13f4fc6269bcbd2f122f2
[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 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/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 /// 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>(); }
53
54 template<int64_t Val>
55 struct constantint_ty {
56   template<typename ITy>
57   bool match(ITy *V) {
58     if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
59       const APInt &CIV = CI->getValue();
60       if (Val >= 0)
61         return CIV == 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.
65       return -CIV == -Val;
66     }
67     return false;
68   }
69 };
70
71 /// m_ConstantInt(int64_t) - Match a ConstantInt with a specific value
72 /// and ignore it.
73 template<int64_t Val>
74 inline constantint_ty<Val> m_ConstantInt() {
75   return constantint_ty<Val>();
76 }
77
78 struct zero_ty {
79   template<typename ITy>
80   bool match(ITy *V) {
81     if (const Constant *C = dyn_cast<Constant>(V))
82       return C->isNullValue();
83     return false;
84   }
85 };
86
87 /// m_Zero() - Match an arbitrary zero/null constant.
88 inline zero_ty m_Zero() { return zero_ty(); }
89
90
91 template<typename Class>
92 struct bind_ty {
93   Class *&VR;
94   bind_ty(Class *&V) : VR(V) {}
95
96   template<typename ITy>
97   bool match(ITy *V) {
98     if (Class *CV = dyn_cast<Class>(V)) {
99       VR = CV;
100       return true;
101     }
102     return false;
103   }
104 };
105
106 /// m_Value - Match a value, capturing it if we match.
107 inline bind_ty<Value> m_Value(Value *&V) { return V; }
108
109 /// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
110 inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
111   
112 /// specificval_ty - Match a specified Value*.
113 struct specificval_ty {
114   const Value *Val;
115   specificval_ty(const Value *V) : Val(V) {}
116   
117   template<typename ITy>
118   bool match(ITy *V) {
119     return V == Val;
120   }
121 };
122   
123 /// m_Specific - Match if we have a specific specified value.
124 inline specificval_ty m_Specific(const Value *V) { return V; }
125   
126
127 //===----------------------------------------------------------------------===//
128 // Matchers for specific binary operators.
129 //
130
131 template<typename LHS_t, typename RHS_t, 
132          unsigned Opcode, typename ConcreteTy = BinaryOperator>
133 struct BinaryOp_match {
134   LHS_t L;
135   RHS_t R;
136
137   BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
138
139   template<typename OpTy>
140   bool match(OpTy *V) {
141     if (V->getValueID() == Value::InstructionVal + Opcode) {
142       ConcreteTy *I = cast<ConcreteTy>(V);
143       return I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
144              R.match(I->getOperand(1));
145     }
146     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
147       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
148              R.match(CE->getOperand(1));
149     return false;
150   }
151 };
152
153 template<typename LHS, typename RHS>
154 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
155                                                         const RHS &R) {
156   return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
157 }
158
159 template<typename LHS, typename RHS>
160 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
161                                                         const RHS &R) {
162   return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
163 }
164
165 template<typename LHS, typename RHS>
166 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
167                                                         const RHS &R) {
168   return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
169 }
170
171 template<typename LHS, typename RHS>
172 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
173                                                         const RHS &R) {
174   return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
175 }
176
177 template<typename LHS, typename RHS>
178 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
179                                                         const RHS &R) {
180   return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
181 }
182
183 template<typename LHS, typename RHS>
184 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
185                                                         const RHS &R) {
186   return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
187 }
188
189 template<typename LHS, typename RHS>
190 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
191                                                           const RHS &R) {
192   return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
193 }
194
195 template<typename LHS, typename RHS>
196 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
197                                                           const RHS &R) {
198   return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
199 }
200
201 template<typename LHS, typename RHS>
202 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
203                                                         const RHS &R) {
204   return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
205 }
206
207 template<typename LHS, typename RHS>
208 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
209                                                         const RHS &R) {
210   return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
211 }
212
213 template<typename LHS, typename RHS>
214 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
215                                                       const RHS &R) {
216   return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
217 }
218
219 template<typename LHS, typename RHS>
220 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
221                                                         const RHS &R) {
222   return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
223 }
224
225 template<typename LHS, typename RHS>
226 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, 
227                                                         const RHS &R) {
228   return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
229 }
230
231 template<typename LHS, typename RHS>
232 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, 
233                                                           const RHS &R) {
234   return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
235 }
236
237 template<typename LHS, typename RHS>
238 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, 
239                                                           const RHS &R) {
240   return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
241 }
242
243 //===----------------------------------------------------------------------===//
244 // Matchers for either AShr or LShr .. for convenience
245 //
246 template<typename LHS_t, typename RHS_t, typename ConcreteTy = BinaryOperator>
247 struct Shr_match {
248   LHS_t L;
249   RHS_t R;
250
251   Shr_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
252
253   template<typename OpTy>
254   bool match(OpTy *V) {
255     if (V->getValueID() == Value::InstructionVal + Instruction::LShr ||
256         V->getValueID() == Value::InstructionVal + Instruction::AShr) {
257       ConcreteTy *I = cast<ConcreteTy>(V);
258       return (I->getOpcode() == Instruction::AShr ||
259               I->getOpcode() == Instruction::LShr) &&
260              L.match(I->getOperand(0)) &&
261              R.match(I->getOperand(1));
262     }
263     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
264       return (CE->getOpcode() == Instruction::LShr ||
265               CE->getOpcode() == Instruction::AShr) &&
266              L.match(CE->getOperand(0)) &&
267              R.match(CE->getOperand(1));
268     return false;
269   }
270 };
271
272 template<typename LHS, typename RHS>
273 inline Shr_match<LHS, RHS> m_Shr(const LHS &L, const RHS &R) {
274   return Shr_match<LHS, RHS>(L, R);
275 }
276
277 //===----------------------------------------------------------------------===//
278 // Matchers for binary classes
279 //
280
281 template<typename LHS_t, typename RHS_t, typename Class, typename OpcType>
282 struct BinaryOpClass_match {
283   OpcType *Opcode;
284   LHS_t L;
285   RHS_t R;
286
287   BinaryOpClass_match(OpcType &Op, const LHS_t &LHS,
288                       const RHS_t &RHS)
289     : Opcode(&Op), L(LHS), R(RHS) {}
290   BinaryOpClass_match(const LHS_t &LHS, const RHS_t &RHS)
291     : Opcode(0), L(LHS), R(RHS) {}
292
293   template<typename OpTy>
294   bool match(OpTy *V) {
295     if (Class *I = dyn_cast<Class>(V))
296       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
297         if (Opcode)
298           *Opcode = I->getOpcode();
299         return true;
300       }
301 #if 0  // Doesn't handle constantexprs yet!
302     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
303       return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
304              R.match(CE->getOperand(1));
305 #endif
306     return false;
307   }
308 };
309
310 template<typename LHS, typename RHS>
311 inline BinaryOpClass_match<LHS, RHS, BinaryOperator, Instruction::BinaryOps>
312 m_Shift(Instruction::BinaryOps &Op, const LHS &L, const RHS &R) {
313   return BinaryOpClass_match<LHS, RHS, 
314                              BinaryOperator, Instruction::BinaryOps>(Op, L, R);
315 }
316
317 template<typename LHS, typename RHS>
318 inline BinaryOpClass_match<LHS, RHS, BinaryOperator, Instruction::BinaryOps>
319 m_Shift(const LHS &L, const RHS &R) {
320   return BinaryOpClass_match<LHS, RHS, 
321                              BinaryOperator, Instruction::BinaryOps>(L, R);
322 }
323
324 //===----------------------------------------------------------------------===//
325 // Matchers for CmpInst classes
326 //
327
328 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
329 struct CmpClass_match {
330   PredicateTy &Predicate;
331   LHS_t L;
332   RHS_t R;
333
334   CmpClass_match(PredicateTy &Pred, const LHS_t &LHS,
335                  const RHS_t &RHS)
336     : Predicate(Pred), L(LHS), R(RHS) {}
337
338   template<typename OpTy>
339   bool match(OpTy *V) {
340     if (Class *I = dyn_cast<Class>(V))
341       if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
342         Predicate = I->getPredicate();
343         return true;
344       }
345     return false;
346   }
347 };
348
349 template<typename LHS, typename RHS>
350 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
351 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
352   return CmpClass_match<LHS, RHS,
353                         ICmpInst, ICmpInst::Predicate>(Pred, L, R);
354 }
355
356 template<typename LHS, typename RHS>
357 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
358 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
359   return CmpClass_match<LHS, RHS,
360                         FCmpInst, FCmpInst::Predicate>(Pred, L, R);
361 }
362
363 //===----------------------------------------------------------------------===//
364 // Matchers for SelectInst classes
365 //
366
367 template<typename Cond_t, typename LHS_t, typename RHS_t>
368 struct SelectClass_match {
369   Cond_t C;
370   LHS_t L;
371   RHS_t R;
372
373   SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
374                     const RHS_t &RHS)
375     : C(Cond), L(LHS), R(RHS) {}
376
377   template<typename OpTy>
378   bool match(OpTy *V) {
379     if (SelectInst *I = dyn_cast<SelectInst>(V))
380       return C.match(I->getOperand(0)) &&
381              L.match(I->getOperand(1)) &&
382              R.match(I->getOperand(2));
383     return false;
384   }
385 };
386
387 template<typename Cond, typename LHS, typename RHS>
388 inline SelectClass_match<Cond, RHS, LHS>
389 m_Select(const Cond &C, const LHS &L, const RHS &R) {
390   return SelectClass_match<Cond, LHS, RHS>(C, L, R);
391 }
392
393 /// m_SelectCst - This matches a select of two constants, e.g.:
394 ///    m_SelectCst(m_Value(V), -1, 0)
395 template<int64_t L, int64_t R, typename Cond>
396 inline SelectClass_match<Cond, constantint_ty<L>, constantint_ty<R> >
397 m_SelectCst(const Cond &C) {
398   return SelectClass_match<Cond, constantint_ty<L>, 
399                            constantint_ty<R> >(C, m_ConstantInt<L>(),
400                                            m_ConstantInt<R>());
401 }
402
403
404 //===----------------------------------------------------------------------===//
405 // Matchers for CastInst classes
406 //
407
408 template<typename Op_t, typename Class>
409 struct CastClass_match {
410   Op_t Op;
411   
412   CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
413   
414   template<typename OpTy>
415   bool match(OpTy *V) {
416     if (Class *I = dyn_cast<Class>(V))
417       return Op.match(I->getOperand(0));
418     return false;
419   }
420 };
421
422 template<typename Class, typename OpTy>
423 inline CastClass_match<OpTy, Class> m_Cast(const OpTy &Op) {
424   return CastClass_match<OpTy, Class>(Op);
425 }
426
427   
428 //===----------------------------------------------------------------------===//
429 // Matchers for unary operators
430 //
431
432 template<typename LHS_t>
433 struct not_match {
434   LHS_t L;
435
436   not_match(const LHS_t &LHS) : L(LHS) {}
437
438   template<typename OpTy>
439   bool match(OpTy *V) {
440     if (Instruction *I = dyn_cast<Instruction>(V))
441       if (I->getOpcode() == Instruction::Xor)
442         return matchIfNot(I->getOperand(0), I->getOperand(1));
443     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
444       if (CE->getOpcode() == Instruction::Xor)
445         return matchIfNot(CE->getOperand(0), CE->getOperand(1));
446     if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
447       return L.match(ConstantExpr::getNot(CI));
448     return false;
449   }
450 private:
451   bool matchIfNot(Value *LHS, Value *RHS) {
452     if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
453       return CI->isAllOnesValue() && L.match(LHS);
454     if (ConstantInt *CI = dyn_cast<ConstantInt>(LHS))
455       return CI->isAllOnesValue() && L.match(RHS);
456     if (ConstantVector *CV = dyn_cast<ConstantVector>(RHS))
457       return CV->isAllOnesValue() && L.match(LHS);
458     if (ConstantVector *CV = dyn_cast<ConstantVector>(LHS))
459       return CV->isAllOnesValue() && L.match(RHS);
460     return false;
461   }
462 };
463
464 template<typename LHS>
465 inline not_match<LHS> m_Not(const LHS &L) { return L; }
466
467
468 template<typename LHS_t>
469 struct neg_match {
470   LHS_t L;
471   
472   neg_match(const LHS_t &LHS) : L(LHS) {}
473   
474   template<typename OpTy>
475   bool match(OpTy *V) {
476     if (Instruction *I = dyn_cast<Instruction>(V))
477       if (I->getOpcode() == Instruction::Sub)
478         return matchIfNeg(I->getOperand(0), I->getOperand(1));
479     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
480       if (CE->getOpcode() == Instruction::Sub)
481         return matchIfNeg(CE->getOperand(0), CE->getOperand(1));
482     if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
483       return L.match(ConstantExpr::getNeg(CI));
484     return false;
485   }
486 private:
487   bool matchIfNeg(Value *LHS, Value *RHS) {
488     return LHS == ConstantExpr::getZeroValueForNegationExpr(LHS->getType()) &&
489            L.match(RHS);
490   }
491 };
492
493 template<typename LHS>
494 inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
495
496
497 //===----------------------------------------------------------------------===//
498 // Matchers for control flow
499 //
500
501 template<typename Cond_t>
502 struct brc_match {
503   Cond_t Cond;
504   BasicBlock *&T, *&F;
505   brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
506     : Cond(C), T(t), F(f) {
507   }
508
509   template<typename OpTy>
510   bool match(OpTy *V) {
511     if (BranchInst *BI = dyn_cast<BranchInst>(V))
512       if (BI->isConditional()) {
513         if (Cond.match(BI->getCondition())) {
514           T = BI->getSuccessor(0);
515           F = BI->getSuccessor(1);
516           return true;
517         }
518       }
519     return false;
520   }
521 };
522
523 template<typename Cond_t>
524 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
525   return brc_match<Cond_t>(C, T, F);
526 }
527
528 } // end namespace PatternMatch
529 } // end namespace llvm
530
531 #endif