From: Chris Lattner Date: Fri, 9 May 2008 05:20:27 +0000 (+0000) Subject: add support for pattern matching 'neg' X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=4ca7913072a60b11eda9da8aa8a82c7cec18bd85;p=oota-llvm.git add support for pattern matching 'neg' git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50883 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/InstrTypes.h b/include/llvm/InstrTypes.h index ef0ee89a3ed..f735602ca8c 100644 --- a/include/llvm/InstrTypes.h +++ b/include/llvm/InstrTypes.h @@ -236,10 +236,8 @@ public: /// swapOperands - Exchange the two operands to this instruction. /// This instruction is safe to use on any binary instruction and - /// does not modify the semantics of the instruction. If the - /// instruction is order dependent (SetLT f.e.) the opcode is - /// changed. If the instruction cannot be reversed (ie, it's a Div), - /// then return true. + /// does not modify the semantics of the instruction. If the instruction + /// cannot be reversed (ie, it's a Div), then return true. /// bool swapOperands(); diff --git a/include/llvm/Support/PatternMatch.h b/include/llvm/Support/PatternMatch.h index ca558821021..a3951e2dd39 100644 --- a/include/llvm/Support/PatternMatch.h +++ b/include/llvm/Support/PatternMatch.h @@ -385,6 +385,35 @@ template inline not_match m_Not(const LHS &L) { return L; } +template +struct neg_match { + LHS_t L; + + neg_match(const LHS_t &LHS) : L(LHS) {} + + template + bool match(OpTy *V) { + if (Instruction *I = dyn_cast(V)) + if (I->getOpcode() == Instruction::Sub) + return matchIfNeg(I->getOperand(0), I->getOperand(1)); + if (ConstantExpr *CE = dyn_cast(V)) + if (CE->getOpcode() == Instruction::Sub) + return matchIfNeg(CE->getOperand(0), CE->getOperand(1)); + if (ConstantInt *CI = dyn_cast(V)) + return L.match(ConstantExpr::getNeg(CI)); + return false; + } +private: + bool matchIfNeg(Value *LHS, Value *RHS) { + return LHS == ConstantExpr::getZeroValueForNegationExpr(LHS->getType()) && + L.match(RHS); + } +}; + +template +inline neg_match m_Neg(const LHS &L) { return L; } + + //===----------------------------------------------------------------------===// // Matchers for control flow //