#include "llvm/Constants.h"
#include "llvm/Instructions.h"
-#include "llvm/LLVMContext.h"
namespace llvm {
namespace PatternMatch {
template<typename Val, typename Pattern>
-bool match(Val *V, const Pattern &P, LLVMContext &Context) {
- return const_cast<Pattern&>(P).match(V, Context);
+bool match(Val *V, const Pattern &P) {
+ return const_cast<Pattern&>(P).match(V);
}
template<typename Class>
struct leaf_ty {
template<typename ITy>
- bool match(ITy *V, LLVMContext&) { return isa<Class>(V); }
+ bool match(ITy *V) { return isa<Class>(V); }
};
/// m_Value() - Match an arbitrary value and ignore it.
template<int64_t Val>
struct constantint_ty {
template<typename ITy>
- bool match(ITy *V, LLVMContext&) {
+ bool match(ITy *V) {
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
const APInt &CIV = CI->getValue();
if (Val >= 0)
- return CIV == Val;
+ return CIV == static_cast<uint64_t>(Val);
// 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.
struct zero_ty {
template<typename ITy>
- bool match(ITy *V, LLVMContext&) {
+ 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.
inline zero_ty m_Zero() { return zero_ty(); }
+struct one_ty {
+ template<typename ITy>
+ bool match(ITy *V) {
+ if (const ConstantInt *C = dyn_cast<ConstantInt>(V))
+ return C->isOne();
+ return false;
+ }
+};
+
+/// m_One() - Match a an integer 1.
+inline one_ty m_One() { return one_ty(); }
+
template<typename Class>
struct bind_ty {
bind_ty(Class *&V) : VR(V) {}
template<typename ITy>
- bool match(ITy *V, LLVMContext&) {
+ bool match(ITy *V) {
if (Class *CV = dyn_cast<Class>(V)) {
VR = CV;
return true;
specificval_ty(const Value *V) : Val(V) {}
template<typename ITy>
- bool match(ITy *V, LLVMContext&) {
+ bool match(ITy *V) {
return V == Val;
}
};
BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (V->getValueID() == Value::InstructionVal + Opcode) {
ConcreteTy *I = cast<ConcreteTy>(V);
- return I->getOpcode() == Opcode && L.match(I->getOperand(0), Context) &&
- R.match(I->getOperand(1), Context);
+ return I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
+ R.match(I->getOperand(1));
}
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
- return CE->getOpcode() == Opcode && L.match(CE->getOperand(0), Context) &&
- R.match(CE->getOperand(1), Context);
+ return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) &&
+ R.match(CE->getOperand(1));
return false;
}
};
Shr_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (V->getValueID() == Value::InstructionVal + Instruction::LShr ||
V->getValueID() == Value::InstructionVal + Instruction::AShr) {
ConcreteTy *I = cast<ConcreteTy>(V);
return (I->getOpcode() == Instruction::AShr ||
I->getOpcode() == Instruction::LShr) &&
- L.match(I->getOperand(0), Context) &&
- R.match(I->getOperand(1), Context);
+ L.match(I->getOperand(0)) &&
+ R.match(I->getOperand(1));
}
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
return (CE->getOpcode() == Instruction::LShr ||
CE->getOpcode() == Instruction::AShr) &&
- L.match(CE->getOperand(0), Context) &&
- R.match(CE->getOperand(1), Context);
+ L.match(CE->getOperand(0)) &&
+ R.match(CE->getOperand(1));
return false;
}
};
: Opcode(0), L(LHS), R(RHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (Class *I = dyn_cast<Class>(V))
- if (L.match(I->getOperand(0), Context) &&
- R.match(I->getOperand(1), Context)) {
+ if (L.match(I->getOperand(0)) &&
+ R.match(I->getOperand(1))) {
if (Opcode)
*Opcode = I->getOpcode();
return true;
: Predicate(Pred), L(LHS), R(RHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (Class *I = dyn_cast<Class>(V))
- if (L.match(I->getOperand(0), Context) &&
- R.match(I->getOperand(1), Context)) {
+ if (L.match(I->getOperand(0)) &&
+ R.match(I->getOperand(1))) {
Predicate = I->getPredicate();
return true;
}
: C(Cond), L(LHS), R(RHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (SelectInst *I = dyn_cast<SelectInst>(V))
- return C.match(I->getOperand(0), Context) &&
- L.match(I->getOperand(1), Context) &&
- R.match(I->getOperand(2), Context);
+ return C.match(I->getOperand(0)) &&
+ L.match(I->getOperand(1)) &&
+ R.match(I->getOperand(2));
return false;
}
};
}
/// m_SelectCst - This matches a select of two constants, e.g.:
-/// m_SelectCst(m_Value(V), -1, 0)
+/// m_SelectCst<-1, 0>(m_Value(V))
template<int64_t L, int64_t R, typename Cond>
inline SelectClass_match<Cond, constantint_ty<L>, constantint_ty<R> >
m_SelectCst(const Cond &C) {
// Matchers for CastInst classes
//
-template<typename Op_t, typename Class>
+template<typename Op_t, unsigned Opcode>
struct CastClass_match {
Op_t Op;
CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
- if (Class *I = dyn_cast<Class>(V))
- return Op.match(I->getOperand(0), Context);
+ bool match(OpTy *V) {
+ 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));
return false;
}
};
-template<typename Class, typename OpTy>
-inline CastClass_match<OpTy, Class> m_Cast(const OpTy &Op) {
- return CastClass_match<OpTy, Class>(Op);
+/// m_BitCast
+template<typename OpTy>
+inline CastClass_match<OpTy, Instruction::BitCast>
+m_BitCast(const OpTy &Op) {
+ return CastClass_match<OpTy, Instruction::BitCast>(Op);
+}
+
+/// m_PtrToInt
+template<typename OpTy>
+inline CastClass_match<OpTy, Instruction::PtrToInt>
+m_PtrToInt(const OpTy &Op) {
+ return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
+}
+
+/// m_Trunc
+template<typename OpTy>
+inline CastClass_match<OpTy, Instruction::Trunc>
+m_Trunc(const OpTy &Op) {
+ return CastClass_match<OpTy, Instruction::Trunc>(Op);
+}
+
+/// 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);
+}
+
//===----------------------------------------------------------------------===//
// Matchers for unary operators
not_match(const LHS_t &LHS) : L(LHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (I->getOpcode() == Instruction::Xor)
- return matchIfNot(I->getOperand(0), I->getOperand(1), Context);
+ return matchIfNot(I->getOperand(0), I->getOperand(1));
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
if (CE->getOpcode() == Instruction::Xor)
- return matchIfNot(CE->getOperand(0), CE->getOperand(1), Context);
+ return matchIfNot(CE->getOperand(0), CE->getOperand(1));
if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
- return L.match(Context.getConstantExprNot(CI), Context);
+ return L.match(ConstantExpr::getNot(CI));
return false;
}
private:
- bool matchIfNot(Value *LHS, Value *RHS, LLVMContext &Context) {
+ bool matchIfNot(Value *LHS, Value *RHS) {
if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS))
- return CI->isAllOnesValue() && L.match(LHS, Context);
+ return CI->isAllOnesValue() && L.match(LHS);
if (ConstantInt *CI = dyn_cast<ConstantInt>(LHS))
- return CI->isAllOnesValue() && L.match(RHS, Context);
+ return CI->isAllOnesValue() && L.match(RHS);
if (ConstantVector *CV = dyn_cast<ConstantVector>(RHS))
- return CV->isAllOnesValue() && L.match(LHS, Context);
+ return CV->isAllOnesValue() && L.match(LHS);
if (ConstantVector *CV = dyn_cast<ConstantVector>(LHS))
- return CV->isAllOnesValue() && L.match(RHS, Context);
+ return CV->isAllOnesValue() && L.match(RHS);
return false;
}
};
neg_match(const LHS_t &LHS) : L(LHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (I->getOpcode() == Instruction::Sub)
- return matchIfNeg(I->getOperand(0), I->getOperand(1), Context);
+ return matchIfNeg(I->getOperand(0), I->getOperand(1));
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
if (CE->getOpcode() == Instruction::Sub)
- return matchIfNeg(CE->getOperand(0), CE->getOperand(1), Context);
+ return matchIfNeg(CE->getOperand(0), CE->getOperand(1));
if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
- return L.match(Context.getConstantExprNeg(CI), Context);
+ return L.match(ConstantExpr::getNeg(CI));
return false;
}
private:
- bool matchIfNeg(Value *LHS, Value *RHS, LLVMContext &Context) {
- return LHS == Context.getZeroValueForNegation(LHS->getType()) &&
- L.match(RHS, Context);
+ bool matchIfNeg(Value *LHS, Value *RHS) {
+ return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
+ L.match(RHS);
}
};
fneg_match(const LHS_t &LHS) : L(LHS) {}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (I->getOpcode() == Instruction::FSub)
- return matchIfFNeg(I->getOperand(0), I->getOperand(1), Context);
+ return matchIfFNeg(I->getOperand(0), I->getOperand(1));
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
if (CE->getOpcode() == Instruction::FSub)
- return matchIfFNeg(CE->getOperand(0), CE->getOperand(1), Context);
+ return matchIfFNeg(CE->getOperand(0), CE->getOperand(1));
if (ConstantFP *CF = dyn_cast<ConstantFP>(V))
- return L.match(Context.getConstantExprFNeg(CF), Context);
+ return L.match(ConstantExpr::getFNeg(CF));
return false;
}
private:
- bool matchIfFNeg(Value *LHS, Value *RHS, LLVMContext &Context) {
- return LHS == Context.getZeroValueForNegation(LHS->getType()) &&
- L.match(RHS, Context);
+ bool matchIfFNeg(Value *LHS, Value *RHS) {
+ return LHS == ConstantFP::getZeroValueForNegation(LHS->getType()) &&
+ L.match(RHS);
}
};
}
template<typename OpTy>
- bool match(OpTy *V, LLVMContext &Context) {
+ bool match(OpTy *V) {
if (BranchInst *BI = dyn_cast<BranchInst>(V))
if (BI->isConditional()) {
- if (Cond.match(BI->getCondition(), Context)) {
+ if (Cond.match(BI->getCondition())) {
T = BI->getSuccessor(0);
F = BI->getSuccessor(1);
return true;