Add support for emitting ARM file attributes.
[oota-llvm.git] / include / llvm / Support / PatternMatch.h
index 66dec00f9c386a07f6a77d65b33fd54fbdb0d9c4..bee676863780e107257ada795b0a2dcc4a4a2a8a 100644 (file)
 
 #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.
@@ -55,11 +54,11 @@ inline leaf_ty<ConstantInt> m_ConstantInt() { return leaf_ty<ConstantInt>(); }
 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.
@@ -78,7 +77,7 @@ inline constantint_ty<Val> m_ConstantInt() {
 
 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;
@@ -88,6 +87,18 @@ struct zero_ty {
 /// 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 {
@@ -95,7 +106,7 @@ 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;
@@ -116,7 +127,7 @@ struct specificval_ty {
   specificval_ty(const Value *V) : Val(V) {}
 
   template<typename ITy>
-  bool match(ITy *V, LLVMContext&) {
+  bool match(ITy *V) {
     return V == Val;
   }
 };
@@ -138,15 +149,15 @@ struct BinaryOp_match {
   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;
   }
 };
@@ -270,20 +281,20 @@ struct Shr_match {
   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;
   }
 };
@@ -310,10 +321,10 @@ struct BinaryOpClass_match {
     : 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;
@@ -356,10 +367,10 @@ struct CmpClass_match {
     : 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;
       }
@@ -396,23 +407,23 @@ struct SelectClass_match {
     : 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;
   }
 };
 
 template<typename Cond, typename LHS, typename RHS>
-inline SelectClass_match<Cond, RHS, LHS>
+inline SelectClass_match<Cond, LHS, RHS>
 m_Select(const Cond &C, const LHS &L, const RHS &R) {
   return SelectClass_match<Cond, LHS, RHS>(C, L, R);
 }
 
 /// 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) {
@@ -426,25 +437,57 @@ 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
@@ -457,27 +500,27 @@ struct not_match {
   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;
   }
 };
@@ -493,21 +536,21 @@ struct neg_match {
   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);
   }
 };
 
@@ -522,21 +565,21 @@ struct fneg_match {
   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);
   }
 };
 
@@ -557,10 +600,10 @@ struct brc_match {
   }
 
   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;