2004-07-30 07:45:00 +00:00
|
|
|
//===-- llvm/Support/PatternMatch.h - Match on the LLVM IR ------*- C++ -*-===//
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2004-07-30 07:45:00 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2004-07-30 07:45:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides a simple and efficient mechanism for performing general
|
|
|
|
// tree-based pattern matches on the LLVM IR. The power of these routines is
|
|
|
|
// that it allows you to write concise patterns that are expressive and easy to
|
2006-11-27 01:05:10 +00:00
|
|
|
// understand. The other major advantage of this is that it allows you to
|
2004-07-30 07:45:00 +00:00
|
|
|
// trivially capture/bind elements in the pattern to variables. For example,
|
|
|
|
// you can do something like this:
|
|
|
|
//
|
|
|
|
// Value *Exp = ...
|
|
|
|
// Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2)
|
|
|
|
// if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)),
|
|
|
|
// m_And(m_Value(Y), m_ConstantInt(C2))))) {
|
|
|
|
// ... Pattern is matched and variables are bound ...
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// This is primarily useful to things like the instruction combiner, but can
|
|
|
|
// also be useful for static analysis tools or code generators.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SUPPORT_PATTERNMATCH_H
|
|
|
|
#define LLVM_SUPPORT_PATTERNMATCH_H
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
2005-04-21 20:48:15 +00:00
|
|
|
namespace PatternMatch {
|
2004-07-30 07:45:00 +00:00
|
|
|
|
|
|
|
template<typename Val, typename Pattern>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(Val *V, const Pattern &P) {
|
|
|
|
return const_cast<Pattern&>(P).match(V);
|
2004-07-30 07:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Class>
|
2011-02-09 17:00:45 +00:00
|
|
|
struct class_match {
|
2004-07-30 07:45:00 +00:00
|
|
|
template<typename ITy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(ITy *V) { return isa<Class>(V); }
|
2004-07-30 07:45:00 +00:00
|
|
|
};
|
|
|
|
|
2007-12-20 04:47:44 +00:00
|
|
|
/// m_Value() - Match an arbitrary value and ignore it.
|
2011-02-09 17:00:45 +00:00
|
|
|
inline class_match<Value> m_Value() { return class_match<Value>(); }
|
2007-12-20 04:47:44 +00:00
|
|
|
/// m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
|
2011-02-09 17:00:45 +00:00
|
|
|
inline class_match<ConstantInt> m_ConstantInt() {
|
|
|
|
return class_match<ConstantInt>();
|
|
|
|
}
|
|
|
|
/// m_Undef() - Match an arbitrary undef constant.
|
|
|
|
inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
|
2004-07-30 07:45:00 +00:00
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
|
|
|
|
|
|
|
|
struct match_zero {
|
|
|
|
template<typename ITy>
|
|
|
|
bool match(ITy *V) {
|
|
|
|
if (const Constant *C = dyn_cast<Constant>(V))
|
|
|
|
return C->isNullValue();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// m_Zero() - Match an arbitrary zero/null constant. This includes
|
|
|
|
/// zero_initializer for vectors and ConstantPointerNull for pointers.
|
|
|
|
inline match_zero m_Zero() { return match_zero(); }
|
|
|
|
|
|
|
|
|
|
|
|
struct apint_match {
|
|
|
|
const APInt *&Res;
|
|
|
|
apint_match(const APInt *&R) : Res(R) {}
|
|
|
|
template<typename ITy>
|
|
|
|
bool match(ITy *V) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
|
|
|
|
if (ConstantInt *CI = cast_or_null<ConstantInt>(CV->getSplatValue())) {
|
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// m_APInt - Match a ConstantInt or splatted ConstantVector, binding the
|
|
|
|
/// specified pointer to the contained APInt.
|
|
|
|
inline apint_match m_APInt(const APInt *&Res) { return Res; }
|
|
|
|
|
|
|
|
|
2009-01-05 23:53:12 +00:00
|
|
|
template<int64_t Val>
|
2011-02-09 17:00:45 +00:00
|
|
|
struct constantint_match {
|
2008-10-30 20:40:10 +00:00
|
|
|
template<typename ITy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(ITy *V) {
|
2009-01-05 23:45:50 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
const APInt &CIV = CI->getValue();
|
2009-01-06 00:06:25 +00:00
|
|
|
if (Val >= 0)
|
2009-09-06 12:56:52 +00:00
|
|
|
return CIV == static_cast<uint64_t>(Val);
|
2009-01-05 23:45:50 +00:00
|
|
|
// If Val is negative, and CI is shorter than it, truncate to the right
|
|
|
|
// number of bits. If it is larger, then we have to sign extend. Just
|
|
|
|
// compare their negated values.
|
|
|
|
return -CIV == -Val;
|
|
|
|
}
|
|
|
|
return false;
|
2008-10-30 20:40:10 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_ConstantInt<int64_t> - Match a ConstantInt with a specific value.
|
2009-01-05 23:53:12 +00:00
|
|
|
template<int64_t Val>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline constantint_match<Val> m_ConstantInt() {
|
|
|
|
return constantint_match<Val>();
|
2008-10-30 20:40:10 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// cst_pred_ty - This helper class is used to match scalar and vector constants
|
|
|
|
/// that satisfy a specified predicate.
|
|
|
|
template<typename Predicate>
|
|
|
|
struct cst_pred_ty : public Predicate {
|
2007-12-20 04:47:44 +00:00
|
|
|
template<typename ITy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(ITy *V) {
|
2011-02-09 17:00:45 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
|
|
|
|
return this->isValue(CI->getValue());
|
|
|
|
if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
|
|
|
|
if (ConstantInt *CI = cast_or_null<ConstantInt>(CV->getSplatValue()))
|
|
|
|
return this->isValue(CI->getValue());
|
2007-12-20 04:47:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2011-02-09 17:00:45 +00:00
|
|
|
|
|
|
|
/// api_pred_ty - This helper class is used to match scalar and vector constants
|
|
|
|
/// that satisfy a specified predicate, and bind them to an APInt.
|
|
|
|
template<typename Predicate>
|
|
|
|
struct api_pred_ty : public Predicate {
|
|
|
|
const APInt *&Res;
|
|
|
|
api_pred_ty(const APInt *&R) : Res(R) {}
|
2009-10-11 07:51:25 +00:00
|
|
|
template<typename ITy>
|
|
|
|
bool match(ITy *V) {
|
2011-02-01 08:39:12 +00:00
|
|
|
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
|
2011-02-09 17:00:45 +00:00
|
|
|
if (this->isValue(CI->getValue())) {
|
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
2011-02-01 08:39:12 +00:00
|
|
|
if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
|
|
|
|
if (ConstantInt *CI = cast_or_null<ConstantInt>(CV->getSplatValue()))
|
2011-02-09 17:00:45 +00:00
|
|
|
if (this->isValue(CI->getValue())) {
|
|
|
|
Res = &CI->getValue();
|
|
|
|
return true;
|
|
|
|
}
|
2009-10-11 07:51:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
|
|
|
|
struct is_one {
|
|
|
|
bool isValue(const APInt &C) { return C == 1; }
|
2010-11-17 18:52:15 +00:00
|
|
|
};
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_One() - Match an integer 1 or a vector with all elements equal to 1.
|
|
|
|
inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); }
|
|
|
|
inline api_pred_ty<is_one> m_One(const APInt *&V) { return V; }
|
|
|
|
|
|
|
|
struct is_all_ones {
|
|
|
|
bool isValue(const APInt &C) { return C.isAllOnesValue(); }
|
|
|
|
};
|
|
|
|
|
2010-11-17 18:52:15 +00:00
|
|
|
/// m_AllOnes() - Match an integer or vector with all bits set to true.
|
2011-02-09 17:00:45 +00:00
|
|
|
inline cst_pred_ty<is_all_ones> m_AllOnes() {return cst_pred_ty<is_all_ones>();}
|
|
|
|
inline api_pred_ty<is_all_ones> m_AllOnes(const APInt *&V) { return V; }
|
2010-11-17 18:52:15 +00:00
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
struct is_sign_bit {
|
|
|
|
bool isValue(const APInt &C) { return C.isSignBit(); }
|
2011-02-01 08:50:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
|
2011-02-09 17:00:45 +00:00
|
|
|
inline cst_pred_ty<is_sign_bit> m_SignBit() {return cst_pred_ty<is_sign_bit>();}
|
|
|
|
inline api_pred_ty<is_sign_bit> m_SignBit(const APInt *&V) { return V; }
|
2011-02-01 08:50:33 +00:00
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
struct is_power2 {
|
|
|
|
bool isValue(const APInt &C) { return C.isPowerOf2(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// m_Power2() - Match an integer or vector power of 2.
|
|
|
|
inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
|
|
|
|
inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
|
2007-12-20 04:47:44 +00:00
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
template<typename Class>
|
|
|
|
struct bind_ty {
|
|
|
|
Class *&VR;
|
2004-08-04 04:45:29 +00:00
|
|
|
bind_ty(Class *&V) : VR(V) {}
|
2004-07-30 07:45:00 +00:00
|
|
|
|
|
|
|
template<typename ITy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(ITy *V) {
|
2004-07-30 07:45:00 +00:00
|
|
|
if (Class *CV = dyn_cast<Class>(V)) {
|
|
|
|
VR = CV;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-12-20 04:47:44 +00:00
|
|
|
/// m_Value - Match a value, capturing it if we match.
|
2004-07-30 07:45:00 +00:00
|
|
|
inline bind_ty<Value> m_Value(Value *&V) { return V; }
|
2007-12-20 04:47:44 +00:00
|
|
|
|
|
|
|
/// m_ConstantInt - Match a ConstantInt, capturing the value if we match.
|
2004-07-30 07:45:00 +00:00
|
|
|
inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_Constant - Match a Constant, capturing the value if we match.
|
|
|
|
inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
|
|
|
|
|
2008-11-16 04:38:30 +00:00
|
|
|
/// specificval_ty - Match a specified Value*.
|
|
|
|
struct specificval_ty {
|
|
|
|
const Value *Val;
|
|
|
|
specificval_ty(const Value *V) : Val(V) {}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-11-16 04:38:30 +00:00
|
|
|
template<typename ITy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(ITy *V) {
|
2008-11-16 04:38:30 +00:00
|
|
|
return V == Val;
|
|
|
|
}
|
|
|
|
};
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-11-16 04:38:30 +00:00
|
|
|
/// m_Specific - Match if we have a specific specified value.
|
|
|
|
inline specificval_ty m_Specific(const Value *V) { return V; }
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-06-15 19:25:28 +00:00
|
|
|
// Matchers for specific binary operators.
|
2004-07-30 07:45:00 +00:00
|
|
|
//
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
template<typename LHS_t, typename RHS_t, unsigned Opcode>
|
2004-07-30 07:45:00 +00:00
|
|
|
struct BinaryOp_match {
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
|
|
|
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
|
|
|
|
|
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2007-04-13 18:12:09 +00:00
|
|
|
if (V->getValueID() == Value::InstructionVal + Opcode) {
|
2011-02-09 17:00:45 +00:00
|
|
|
BinaryOperator *I = cast<BinaryOperator>(V);
|
|
|
|
return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
|
2005-09-27 06:38:05 +00:00
|
|
|
}
|
2004-07-30 07:45:00 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
2009-08-12 16:23:25 +00:00
|
|
|
return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
|
|
|
|
R.match(CE->getOperand(1));
|
2004-07-30 07:45:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-21 20:48:15 +00:00
|
|
|
};
|
2004-07-30 07:45:00 +00:00
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Add>
|
|
|
|
m_Add(const LHS &L, const RHS &R) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
|
|
|
|
}
|
|
|
|
|
2009-06-04 22:49:04 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FAdd>
|
|
|
|
m_FAdd(const LHS &L, const RHS &R) {
|
2009-06-04 22:49:04 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
|
|
|
|
}
|
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Sub>
|
|
|
|
m_Sub(const LHS &L, const RHS &R) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
|
|
|
|
}
|
|
|
|
|
2009-06-04 22:49:04 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FSub>
|
|
|
|
m_FSub(const LHS &L, const RHS &R) {
|
2009-06-04 22:49:04 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
|
|
|
|
}
|
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Mul>
|
|
|
|
m_Mul(const LHS &L, const RHS &R) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
|
|
|
|
}
|
|
|
|
|
2009-06-04 22:49:04 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FMul>
|
|
|
|
m_FMul(const LHS &L, const RHS &R) {
|
2009-06-04 22:49:04 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
|
|
|
|
}
|
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::UDiv>
|
|
|
|
m_UDiv(const LHS &L, const RHS &R) {
|
2006-10-26 06:15:43 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::SDiv>
|
|
|
|
m_SDiv(const LHS &L, const RHS &R) {
|
2006-10-26 06:15:43 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FDiv>
|
|
|
|
m_FDiv(const LHS &L, const RHS &R) {
|
2006-10-26 06:15:43 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
|
2004-07-30 07:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::URem>
|
|
|
|
m_URem(const LHS &L, const RHS &R) {
|
2006-11-02 01:53:59 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::SRem>
|
|
|
|
m_SRem(const LHS &L, const RHS &R) {
|
2006-11-02 01:53:59 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::FRem>
|
|
|
|
m_FRem(const LHS &L, const RHS &R) {
|
2006-11-02 01:53:59 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
|
2004-07-30 07:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::And>
|
|
|
|
m_And(const LHS &L, const RHS &R) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Or>
|
|
|
|
m_Or(const LHS &L, const RHS &R) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Xor>
|
|
|
|
m_Xor(const LHS &L, const RHS &R) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
|
|
|
|
}
|
|
|
|
|
2004-11-13 19:32:45 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::Shl>
|
|
|
|
m_Shl(const LHS &L, const RHS &R) {
|
2007-02-02 02:16:23 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
|
2004-11-13 19:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::LShr>
|
|
|
|
m_LShr(const LHS &L, const RHS &R) {
|
2007-02-02 02:16:23 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
|
2006-11-08 06:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinaryOp_match<LHS, RHS, Instruction::AShr>
|
|
|
|
m_AShr(const LHS &L, const RHS &R) {
|
2007-02-02 02:16:23 +00:00
|
|
|
return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
|
2006-11-08 06:47:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2011-02-09 17:00:45 +00:00
|
|
|
// Class that matches two different binary ops.
|
2011-02-07 09:36:32 +00:00
|
|
|
//
|
2011-02-09 17:00:45 +00:00
|
|
|
template<typename LHS_t, typename RHS_t, unsigned Opc1, unsigned Opc2>
|
|
|
|
struct BinOp2_match {
|
2011-02-07 09:36:32 +00:00
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
BinOp2_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
|
2011-02-07 09:36:32 +00:00
|
|
|
|
|
|
|
template<typename OpTy>
|
|
|
|
bool match(OpTy *V) {
|
2011-02-09 17:00:45 +00:00
|
|
|
if (V->getValueID() == Value::InstructionVal + Opc1 ||
|
|
|
|
V->getValueID() == Value::InstructionVal + Opc2) {
|
|
|
|
BinaryOperator *I = cast<BinaryOperator>(V);
|
|
|
|
return L.match(I->getOperand(0)) && R.match(I->getOperand(1));
|
2011-02-07 09:36:32 +00:00
|
|
|
}
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
2011-02-09 17:00:45 +00:00
|
|
|
return (CE->getOpcode() == Opc1 || CE->getOpcode() == Opc2) &&
|
|
|
|
L.match(CE->getOperand(0)) && R.match(CE->getOperand(1));
|
2011-02-07 09:36:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_Shr - Matches LShr or AShr.
|
2011-02-07 09:36:32 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>
|
|
|
|
m_Shr(const LHS &L, const RHS &R) {
|
|
|
|
return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::AShr>(L, R);
|
2011-02-07 09:36:32 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_LogicalShift - Matches LShr or Shl.
|
2006-06-15 19:25:28 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>
|
|
|
|
m_LogicalShift(const LHS &L, const RHS &R) {
|
|
|
|
return BinOp2_match<LHS, RHS, Instruction::LShr, Instruction::Shl>(L, R);
|
2006-06-15 19:25:28 +00:00
|
|
|
}
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_IDiv - Matches UDiv and SDiv.
|
2006-06-15 19:25:28 +00:00
|
|
|
template<typename LHS, typename RHS>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>
|
|
|
|
m_IDiv(const LHS &L, const RHS &R) {
|
|
|
|
return BinOp2_match<LHS, RHS, Instruction::SDiv, Instruction::UDiv>(L, R);
|
2006-06-15 19:25:28 +00:00
|
|
|
}
|
2004-07-30 07:45:00 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for CmpInst classes
|
|
|
|
//
|
|
|
|
|
|
|
|
template<typename LHS_t, typename RHS_t, typename Class, typename PredicateTy>
|
|
|
|
struct CmpClass_match {
|
|
|
|
PredicateTy &Predicate;
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
|
2006-12-23 06:05:41 +00:00
|
|
|
: Predicate(Pred), L(LHS), R(RHS) {}
|
|
|
|
|
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2006-12-23 06:05:41 +00:00
|
|
|
if (Class *I = dyn_cast<Class>(V))
|
2011-02-09 17:00:45 +00:00
|
|
|
if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
|
2006-12-23 06:05:41 +00:00
|
|
|
Predicate = I->getPredicate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
|
|
|
inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
|
|
|
|
m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
|
|
|
|
return CmpClass_match<LHS, RHS,
|
|
|
|
ICmpInst, ICmpInst::Predicate>(Pred, L, R);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename LHS, typename RHS>
|
|
|
|
inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
|
|
|
|
m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
|
|
|
|
return CmpClass_match<LHS, RHS,
|
|
|
|
FCmpInst, FCmpInst::Predicate>(Pred, L, R);
|
|
|
|
}
|
|
|
|
|
2008-10-30 20:40:10 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for SelectInst classes
|
|
|
|
//
|
|
|
|
|
|
|
|
template<typename Cond_t, typename LHS_t, typename RHS_t>
|
|
|
|
struct SelectClass_match {
|
|
|
|
Cond_t C;
|
|
|
|
LHS_t L;
|
|
|
|
RHS_t R;
|
|
|
|
|
|
|
|
SelectClass_match(const Cond_t &Cond, const LHS_t &LHS,
|
|
|
|
const RHS_t &RHS)
|
|
|
|
: C(Cond), L(LHS), R(RHS) {}
|
|
|
|
|
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2008-10-30 20:40:10 +00:00
|
|
|
if (SelectInst *I = dyn_cast<SelectInst>(V))
|
2009-08-12 16:23:25 +00:00
|
|
|
return C.match(I->getOperand(0)) &&
|
|
|
|
L.match(I->getOperand(1)) &&
|
|
|
|
R.match(I->getOperand(2));
|
2008-10-30 20:40:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Cond, typename LHS, typename RHS>
|
2009-07-14 00:09:42 +00:00
|
|
|
inline SelectClass_match<Cond, LHS, RHS>
|
2008-10-30 20:40:10 +00:00
|
|
|
m_Select(const Cond &C, const LHS &L, const RHS &R) {
|
|
|
|
return SelectClass_match<Cond, LHS, RHS>(C, L, R);
|
|
|
|
}
|
|
|
|
|
2008-11-16 04:33:10 +00:00
|
|
|
/// m_SelectCst - This matches a select of two constants, e.g.:
|
2010-01-24 00:09:49 +00:00
|
|
|
/// m_SelectCst<-1, 0>(m_Value(V))
|
2009-01-05 23:53:12 +00:00
|
|
|
template<int64_t L, int64_t R, typename Cond>
|
2011-02-09 17:00:45 +00:00
|
|
|
inline SelectClass_match<Cond, constantint_match<L>, constantint_match<R> >
|
2009-01-05 23:53:12 +00:00
|
|
|
m_SelectCst(const Cond &C) {
|
2011-02-09 17:00:45 +00:00
|
|
|
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
|
2008-11-16 04:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-08 07:02:44 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for CastInst classes
|
|
|
|
//
|
|
|
|
|
2010-01-01 22:29:12 +00:00
|
|
|
template<typename Op_t, unsigned Opcode>
|
2008-01-08 07:02:44 +00:00
|
|
|
struct CastClass_match {
|
|
|
|
Op_t Op;
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-01-08 07:02:44 +00:00
|
|
|
CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-01-08 07:02:44 +00:00
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2010-01-01 22:29:12 +00:00
|
|
|
if (CastInst *I = dyn_cast<CastInst>(V))
|
|
|
|
return I->getOpcode() == Opcode && Op.match(I->getOperand(0));
|
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
return CE->getOpcode() == Opcode && Op.match(CE->getOperand(0));
|
2008-01-08 07:02:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-26 21:35:52 +00:00
|
|
|
/// m_BitCast
|
|
|
|
template<typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::BitCast>
|
|
|
|
m_BitCast(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::BitCast>(Op);
|
|
|
|
}
|
|
|
|
|
2010-01-01 22:29:12 +00:00
|
|
|
/// m_PtrToInt
|
|
|
|
template<typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::PtrToInt>
|
|
|
|
m_PtrToInt(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
|
2008-01-08 07:02:44 +00:00
|
|
|
}
|
|
|
|
|
2010-01-01 22:29:12 +00:00
|
|
|
/// m_Trunc
|
|
|
|
template<typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::Trunc>
|
|
|
|
m_Trunc(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::Trunc>(Op);
|
|
|
|
}
|
2010-01-24 00:09:49 +00:00
|
|
|
|
|
|
|
/// m_SExt
|
|
|
|
template<typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::SExt>
|
|
|
|
m_SExt(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::SExt>(Op);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// m_ZExt
|
|
|
|
template<typename OpTy>
|
|
|
|
inline CastClass_match<OpTy, Instruction::ZExt>
|
|
|
|
m_ZExt(const OpTy &Op) {
|
|
|
|
return CastClass_match<OpTy, Instruction::ZExt>(Op);
|
|
|
|
}
|
2010-01-01 22:29:12 +00:00
|
|
|
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Matchers for unary operators
|
|
|
|
//
|
|
|
|
|
|
|
|
template<typename LHS_t>
|
|
|
|
struct not_match {
|
|
|
|
LHS_t L;
|
|
|
|
|
|
|
|
not_match(const LHS_t &LHS) : L(LHS) {}
|
|
|
|
|
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2004-07-30 07:45:00 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (I->getOpcode() == Instruction::Xor)
|
2009-08-12 16:23:25 +00:00
|
|
|
return matchIfNot(I->getOperand(0), I->getOperand(1));
|
2004-07-30 07:45:00 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
if (CE->getOpcode() == Instruction::Xor)
|
2009-08-12 16:23:25 +00:00
|
|
|
return matchIfNot(CE->getOperand(0), CE->getOperand(1));
|
2004-07-30 07:45:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private:
|
2009-08-12 16:23:25 +00:00
|
|
|
bool matchIfNot(Value *LHS, Value *RHS) {
|
2007-01-11 12:24:14 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
|
2009-08-12 16:23:25 +00:00
|
|
|
return CI->isAllOnesValue() && L.match(LHS);
|
2007-06-15 06:13:47 +00:00
|
|
|
if (ConstantVector *CV = dyn_cast<ConstantVector>(RHS))
|
2009-08-12 16:23:25 +00:00
|
|
|
return CV->isAllOnesValue() && L.match(LHS);
|
2004-07-30 07:45:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename LHS>
|
|
|
|
inline not_match<LHS> m_Not(const LHS &L) { return L; }
|
|
|
|
|
2006-09-18 05:17:11 +00:00
|
|
|
|
2008-05-09 05:20:27 +00:00
|
|
|
template<typename LHS_t>
|
|
|
|
struct neg_match {
|
|
|
|
LHS_t L;
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-05-09 05:20:27 +00:00
|
|
|
neg_match(const LHS_t &LHS) : L(LHS) {}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2008-05-09 05:20:27 +00:00
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2008-05-09 05:20:27 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (I->getOpcode() == Instruction::Sub)
|
2009-08-12 16:23:25 +00:00
|
|
|
return matchIfNeg(I->getOperand(0), I->getOperand(1));
|
2008-05-09 05:20:27 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
if (CE->getOpcode() == Instruction::Sub)
|
2009-08-12 16:23:25 +00:00
|
|
|
return matchIfNeg(CE->getOperand(0), CE->getOperand(1));
|
2008-05-09 05:20:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private:
|
2009-08-12 16:23:25 +00:00
|
|
|
bool matchIfNeg(Value *LHS, Value *RHS) {
|
2011-02-09 17:00:45 +00:00
|
|
|
if (ConstantInt *C = dyn_cast<ConstantInt>(LHS))
|
|
|
|
return C->isZero() && L.match(RHS);
|
|
|
|
return false;
|
2008-05-09 05:20:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_Neg - Match an integer negate.
|
2008-05-09 05:20:27 +00:00
|
|
|
template<typename LHS>
|
|
|
|
inline neg_match<LHS> m_Neg(const LHS &L) { return L; }
|
|
|
|
|
|
|
|
|
2009-06-04 22:49:04 +00:00
|
|
|
template<typename LHS_t>
|
|
|
|
struct fneg_match {
|
|
|
|
LHS_t L;
|
|
|
|
|
|
|
|
fneg_match(const LHS_t &LHS) : L(LHS) {}
|
|
|
|
|
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2009-06-04 22:49:04 +00:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (I->getOpcode() == Instruction::FSub)
|
2009-08-12 16:23:25 +00:00
|
|
|
return matchIfFNeg(I->getOperand(0), I->getOperand(1));
|
2009-06-04 22:49:04 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
if (CE->getOpcode() == Instruction::FSub)
|
2009-08-12 16:23:25 +00:00
|
|
|
return matchIfFNeg(CE->getOperand(0), CE->getOperand(1));
|
2009-06-04 22:49:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private:
|
2009-08-12 16:23:25 +00:00
|
|
|
bool matchIfFNeg(Value *LHS, Value *RHS) {
|
2011-02-09 17:00:45 +00:00
|
|
|
if (ConstantFP *C = dyn_cast<ConstantFP>(LHS))
|
|
|
|
return C->isNegativeZeroValue() && L.match(RHS);
|
|
|
|
return false;
|
2009-06-04 22:49:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-09 17:00:45 +00:00
|
|
|
/// m_FNeg - Match a floating point negate.
|
2009-06-04 22:49:04 +00:00
|
|
|
template<typename LHS>
|
|
|
|
inline fneg_match<LHS> m_FNeg(const LHS &L) { return L; }
|
|
|
|
|
|
|
|
|
2004-07-30 07:45:00 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2011-02-09 17:00:45 +00:00
|
|
|
// Matchers for control flow.
|
2004-07-30 07:45:00 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
template<typename Cond_t>
|
|
|
|
struct brc_match {
|
|
|
|
Cond_t Cond;
|
|
|
|
BasicBlock *&T, *&F;
|
|
|
|
brc_match(const Cond_t &C, BasicBlock *&t, BasicBlock *&f)
|
|
|
|
: Cond(C), T(t), F(f) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename OpTy>
|
2009-08-12 16:23:25 +00:00
|
|
|
bool match(OpTy *V) {
|
2004-07-30 07:45:00 +00:00
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(V))
|
2011-02-09 17:00:45 +00:00
|
|
|
if (BI->isConditional() && Cond.match(BI->getCondition())) {
|
|
|
|
T = BI->getSuccessor(0);
|
|
|
|
F = BI->getSuccessor(1);
|
|
|
|
return true;
|
2004-07-30 07:45:00 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Cond_t>
|
2009-01-02 20:26:30 +00:00
|
|
|
inline brc_match<Cond_t> m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
|
2004-07-30 07:45:00 +00:00
|
|
|
return brc_match<Cond_t>(C, T, F);
|
|
|
|
}
|
|
|
|
|
2009-01-02 20:26:30 +00:00
|
|
|
} // end namespace PatternMatch
|
|
|
|
} // end namespace llvm
|
2004-07-30 07:45:00 +00:00
|
|
|
|
|
|
|
#endif
|