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 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 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,
75 unsigned Opcode, typename ConcreteTy = BinaryOperator>
76 struct BinaryOp_match {
80 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
82 template<typename OpTy>
84 if (V->getValueID() == 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));
89 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
90 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
91 R.match(CE->getOperand(1));
96 template<typename LHS, typename RHS>
97 inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
99 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
102 template<typename LHS, typename RHS>
103 inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
105 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
108 template<typename LHS, typename RHS>
109 inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
111 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
114 template<typename LHS, typename RHS>
115 inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
117 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
120 template<typename LHS, typename RHS>
121 inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
123 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
126 template<typename LHS, typename RHS>
127 inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
129 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
132 template<typename LHS, typename RHS>
133 inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
135 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
138 template<typename LHS, typename RHS>
139 inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
141 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
144 template<typename LHS, typename RHS>
145 inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
147 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
150 template<typename LHS, typename RHS>
151 inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
153 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
156 template<typename LHS, typename RHS>
157 inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
159 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
162 template<typename LHS, typename RHS>
163 inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
165 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
168 template<typename LHS, typename RHS>
169 inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
171 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
174 template<typename LHS, typename RHS>
175 inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
177 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
180 template<typename LHS, typename RHS>
181 inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
183 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
186 //===----------------------------------------------------------------------===//
187 // Matchers for either AShr or LShr .. for convenience
189 template<typename LHS_t, typename RHS_t, typename ConcreteTy = BinaryOperator>
194 Shr_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
196 template<typename OpTy>
197 bool match(OpTy *V) {
198 if (V->getValueID() == Value::InstructionVal + Instruction::LShr ||
199 V->getValueID() == Value::InstructionVal + Instruction::AShr) {
200 ConcreteTy *I = cast<ConcreteTy>(V);
201 return (I->getOpcode() == Instruction::AShr ||
202 I->getOpcode() == Instruction::LShr) &&
203 L.match(I->getOperand(0)) &&
204 R.match(I->getOperand(1));
206 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
207 return (CE->getOpcode() == Instruction::LShr ||
208 CE->getOpcode() == Instruction::AShr) &&
209 L.match(CE->getOperand(0)) &&
210 R.match(CE->getOperand(1));
215 template<typename LHS, typename RHS>
216 inline Shr_match<LHS, RHS> m_Shr(const LHS &L, const RHS &R) {
217 return Shr_match<LHS, RHS>(L, R);
220 //===----------------------------------------------------------------------===//
221 // Matchers for binary classes
224 template<typename LHS_t, typename RHS_t, typename Class, typename OpcType>
225 struct BinaryOpClass_match {
230 BinaryOpClass_match(OpcType &Op, const LHS_t &LHS,
232 : Opcode(&Op), L(LHS), R(RHS) {}
233 BinaryOpClass_match(const LHS_t &LHS, const RHS_t &RHS)
234 : Opcode(0), L(LHS), R(RHS) {}
236 template<typename OpTy>
237 bool match(OpTy *V) {
238 if (Class *I = dyn_cast<Class>(V))
239 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
241 *Opcode = I->getOpcode();
244 #if 0 // Doesn't handle constantexprs yet!
245 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
246 return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
247 R.match(CE->getOperand(1));
253 template<typename LHS, typename RHS>
254 inline BinaryOpClass_match<LHS, RHS, BinaryOperator, Instruction::BinaryOps>
255 m_Shift(Instruction::BinaryOps &Op, const LHS &L, const RHS &R) {
256 return BinaryOpClass_match<LHS, RHS,
257 BinaryOperator, Instruction::BinaryOps>(Op, L, R);
260 template<typename LHS, typename RHS>
261 inline BinaryOpClass_match<LHS, RHS, BinaryOperator, Instruction::BinaryOps>
262 m_Shift(const LHS &L, const RHS &R) {
263 return BinaryOpClass_match<LHS, RHS,
264 BinaryOperator, Instruction::BinaryOps>(L, R);
267 //===----------------------------------------------------------------------===//
268 // Matchers for CmpInst classes
271 template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
272 struct CmpClass_match {
273 PredicateTy &Predicate;
277 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS,
279 : Predicate(Pred), L(LHS), R(RHS) {}
281 template<typename OpTy>
282 bool match(OpTy *V) {
283 if (Class *I = dyn_cast<Class>(V))
284 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
285 Predicate = I->getPredicate();
292 template<typename LHS, typename RHS>
293 inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
294 m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
295 return CmpClass_match<LHS, RHS,
296 ICmpInst, ICmpInst::Predicate>(Pred, L, R);
299 template<typename LHS, typename RHS>
300 inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
301 m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
302 return CmpClass_match<LHS, RHS,
303 FCmpInst, FCmpInst::Predicate>(Pred, L, R);
306 //===----------------------------------------------------------------------===//
307 // Matchers for unary operators
310 template<typename LHS_t>
314 not_match(const LHS_t &LHS) : L(LHS) {}
316 template<typename OpTy>
317 bool match(OpTy *V) {
318 if (Instruction *I = dyn_cast<Instruction>(V))
319 if (I->getOpcode() == Instruction::Xor)
320 return matchIfNot(I->getOperand(0), I->getOperand(1));
321 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
322 if (CE->getOpcode() == Instruction::Xor)
323 return matchIfNot(CE->getOperand(0), CE->getOperand(1));
324 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
325 return L.match(ConstantExpr::getNot(CI));
329 bool matchIfNot(Value *LHS, Value *RHS) {
330 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
331 return CI->isAllOnesValue() && L.match(LHS);
332 else if (ConstantInt *CI = dyn_cast<ConstantInt>(LHS))
333 return CI->isAllOnesValue() && L.match(RHS);
338 template<typename LHS>
339 inline not_match<LHS> m_Not(const LHS &L) { return L; }
342 //===----------------------------------------------------------------------===//
343 // Matchers for control flow
346 template<typename Cond_t>
350 brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
351 : Cond(C), T(t), F(f) {
354 template<typename OpTy>
355 bool match(OpTy *V) {
356 if (BranchInst *BI = dyn_cast<BranchInst>(V))
357 if (BI->isConditional()) {
358 if (Cond.match(BI->getCondition())) {
359 T = BI->getSuccessor(0);
360 F = BI->getSuccessor(1);
368 template<typename Cond_t>
369 inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F){
370 return brc_match<Cond_t>(C, T, F);
374 }} // end llvm::match