From c7d2ce7047d7cb026c3198e6760415ed673b00f7 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Fri, 10 Jul 2009 17:35:01 +0000 Subject: [PATCH] Push LLVMContext through the PatternMatch API. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75255 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/NoFolder.h | 5 +- include/llvm/Support/PatternMatch.h | 107 ++++---- .../Scalar/InstructionCombining.cpp | 242 ++++++++++-------- lib/Transforms/Utils/AddrModeMatcher.cpp | 3 +- 4 files changed, 193 insertions(+), 164 deletions(-) diff --git a/include/llvm/Support/NoFolder.h b/include/llvm/Support/NoFolder.h index 40c1996c319..c110270f322 100644 --- a/include/llvm/Support/NoFolder.h +++ b/include/llvm/Support/NoFolder.h @@ -30,7 +30,10 @@ namespace llvm { /// NoFolder - Create "constants" (actually, values) with no folding. class NoFolder { + LLVMContext &Context; public: + + NoFolder(LLVMContext &C) : Context(C) { } //===--------------------------------------------------------------------===// // Binary Operators @@ -113,7 +116,7 @@ public: Constant *CreateGetElementPtr(Constant *C, Constant* const *IdxList, unsigned NumIdx) const { - return ConstantExpr::getGetElementPtr(C, IdxList, NumIdx); + return Context.getConstantExprGetElementPtr(C, IdxList, NumIdx); } Value *CreateGetElementPtr(Constant *C, Value* const *IdxList, unsigned NumIdx) const { diff --git a/include/llvm/Support/PatternMatch.h b/include/llvm/Support/PatternMatch.h index fda925f5a9a..66dec00f9c3 100644 --- a/include/llvm/Support/PatternMatch.h +++ b/include/llvm/Support/PatternMatch.h @@ -31,19 +31,20 @@ #include "llvm/Constants.h" #include "llvm/Instructions.h" +#include "llvm/LLVMContext.h" namespace llvm { namespace PatternMatch { template -bool match(Val *V, const Pattern &P) { - return const_cast(P).match(V); +bool match(Val *V, const Pattern &P, LLVMContext &Context) { + return const_cast(P).match(V, Context); } template struct leaf_ty { template - bool match(ITy *V) { return isa(V); } + bool match(ITy *V, LLVMContext&) { return isa(V); } }; /// m_Value() - Match an arbitrary value and ignore it. @@ -54,7 +55,7 @@ inline leaf_ty m_ConstantInt() { return leaf_ty(); } template struct constantint_ty { template - bool match(ITy *V) { + bool match(ITy *V, LLVMContext&) { if (const ConstantInt *CI = dyn_cast(V)) { const APInt &CIV = CI->getValue(); if (Val >= 0) @@ -77,7 +78,7 @@ inline constantint_ty m_ConstantInt() { struct zero_ty { template - bool match(ITy *V) { + bool match(ITy *V, LLVMContext&) { if (const Constant *C = dyn_cast(V)) return C->isNullValue(); return false; @@ -94,7 +95,7 @@ struct bind_ty { bind_ty(Class *&V) : VR(V) {} template - bool match(ITy *V) { + bool match(ITy *V, LLVMContext&) { if (Class *CV = dyn_cast(V)) { VR = CV; return true; @@ -115,7 +116,7 @@ struct specificval_ty { specificval_ty(const Value *V) : Val(V) {} template - bool match(ITy *V) { + bool match(ITy *V, LLVMContext&) { return V == Val; } }; @@ -137,15 +138,15 @@ struct BinaryOp_match { BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (V->getValueID() == Value::InstructionVal + Opcode) { ConcreteTy *I = cast(V); - return I->getOpcode() == Opcode && L.match(I->getOperand(0)) && - R.match(I->getOperand(1)); + return I->getOpcode() == Opcode && L.match(I->getOperand(0), Context) && + R.match(I->getOperand(1), Context); } if (ConstantExpr *CE = dyn_cast(V)) - return CE->getOpcode() == Opcode && L.match(CE->getOperand(0)) && - R.match(CE->getOperand(1)); + return CE->getOpcode() == Opcode && L.match(CE->getOperand(0), Context) && + R.match(CE->getOperand(1), Context); return false; } }; @@ -269,20 +270,20 @@ struct Shr_match { Shr_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (V->getValueID() == Value::InstructionVal + Instruction::LShr || V->getValueID() == Value::InstructionVal + Instruction::AShr) { ConcreteTy *I = cast(V); return (I->getOpcode() == Instruction::AShr || I->getOpcode() == Instruction::LShr) && - L.match(I->getOperand(0)) && - R.match(I->getOperand(1)); + L.match(I->getOperand(0), Context) && + R.match(I->getOperand(1), Context); } if (ConstantExpr *CE = dyn_cast(V)) return (CE->getOpcode() == Instruction::LShr || CE->getOpcode() == Instruction::AShr) && - L.match(CE->getOperand(0)) && - R.match(CE->getOperand(1)); + L.match(CE->getOperand(0), Context) && + R.match(CE->getOperand(1), Context); return false; } }; @@ -309,9 +310,10 @@ struct BinaryOpClass_match { : Opcode(0), L(LHS), R(RHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (Class *I = dyn_cast(V)) - if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { + if (L.match(I->getOperand(0), Context) && + R.match(I->getOperand(1), Context)) { if (Opcode) *Opcode = I->getOpcode(); return true; @@ -354,9 +356,10 @@ struct CmpClass_match { : Predicate(Pred), L(LHS), R(RHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (Class *I = dyn_cast(V)) - if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { + if (L.match(I->getOperand(0), Context) && + R.match(I->getOperand(1), Context)) { Predicate = I->getPredicate(); return true; } @@ -393,11 +396,11 @@ struct SelectClass_match { : C(Cond), L(LHS), R(RHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (SelectInst *I = dyn_cast(V)) - return C.match(I->getOperand(0)) && - L.match(I->getOperand(1)) && - R.match(I->getOperand(2)); + return C.match(I->getOperand(0), Context) && + L.match(I->getOperand(1), Context) && + R.match(I->getOperand(2), Context); return false; } }; @@ -430,9 +433,9 @@ struct CastClass_match { CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (Class *I = dyn_cast(V)) - return Op.match(I->getOperand(0)); + return Op.match(I->getOperand(0), Context); return false; } }; @@ -454,27 +457,27 @@ struct not_match { not_match(const LHS_t &LHS) : L(LHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (Instruction *I = dyn_cast(V)) if (I->getOpcode() == Instruction::Xor) - return matchIfNot(I->getOperand(0), I->getOperand(1)); + return matchIfNot(I->getOperand(0), I->getOperand(1), Context); if (ConstantExpr *CE = dyn_cast(V)) if (CE->getOpcode() == Instruction::Xor) - return matchIfNot(CE->getOperand(0), CE->getOperand(1)); + return matchIfNot(CE->getOperand(0), CE->getOperand(1), Context); if (ConstantInt *CI = dyn_cast(V)) - return L.match(ConstantExpr::getNot(CI)); + return L.match(Context.getConstantExprNot(CI), Context); return false; } private: - bool matchIfNot(Value *LHS, Value *RHS) { + bool matchIfNot(Value *LHS, Value *RHS, LLVMContext &Context) { if (ConstantInt *CI = dyn_cast(RHS)) - return CI->isAllOnesValue() && L.match(LHS); + return CI->isAllOnesValue() && L.match(LHS, Context); if (ConstantInt *CI = dyn_cast(LHS)) - return CI->isAllOnesValue() && L.match(RHS); + return CI->isAllOnesValue() && L.match(RHS, Context); if (ConstantVector *CV = dyn_cast(RHS)) - return CV->isAllOnesValue() && L.match(LHS); + return CV->isAllOnesValue() && L.match(LHS, Context); if (ConstantVector *CV = dyn_cast(LHS)) - return CV->isAllOnesValue() && L.match(RHS); + return CV->isAllOnesValue() && L.match(RHS, Context); return false; } }; @@ -490,21 +493,21 @@ struct neg_match { neg_match(const LHS_t &LHS) : L(LHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (Instruction *I = dyn_cast(V)) if (I->getOpcode() == Instruction::Sub) - return matchIfNeg(I->getOperand(0), I->getOperand(1)); + return matchIfNeg(I->getOperand(0), I->getOperand(1), Context); if (ConstantExpr *CE = dyn_cast(V)) if (CE->getOpcode() == Instruction::Sub) - return matchIfNeg(CE->getOperand(0), CE->getOperand(1)); + return matchIfNeg(CE->getOperand(0), CE->getOperand(1), Context); if (ConstantInt *CI = dyn_cast(V)) - return L.match(ConstantExpr::getNeg(CI)); + return L.match(Context.getConstantExprNeg(CI), Context); return false; } private: - bool matchIfNeg(Value *LHS, Value *RHS) { - return LHS == ConstantExpr::getZeroValueForNegationExpr(LHS->getType()) && - L.match(RHS); + bool matchIfNeg(Value *LHS, Value *RHS, LLVMContext &Context) { + return LHS == Context.getZeroValueForNegation(LHS->getType()) && + L.match(RHS, Context); } }; @@ -519,21 +522,21 @@ struct fneg_match { fneg_match(const LHS_t &LHS) : L(LHS) {} template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (Instruction *I = dyn_cast(V)) if (I->getOpcode() == Instruction::FSub) - return matchIfFNeg(I->getOperand(0), I->getOperand(1)); + return matchIfFNeg(I->getOperand(0), I->getOperand(1), Context); if (ConstantExpr *CE = dyn_cast(V)) if (CE->getOpcode() == Instruction::FSub) - return matchIfFNeg(CE->getOperand(0), CE->getOperand(1)); + return matchIfFNeg(CE->getOperand(0), CE->getOperand(1), Context); if (ConstantFP *CF = dyn_cast(V)) - return L.match(ConstantExpr::getFNeg(CF)); + return L.match(Context.getConstantExprFNeg(CF), Context); return false; } private: - bool matchIfFNeg(Value *LHS, Value *RHS) { - return LHS == ConstantExpr::getZeroValueForNegationExpr(LHS->getType()) && - L.match(RHS); + bool matchIfFNeg(Value *LHS, Value *RHS, LLVMContext &Context) { + return LHS == Context.getZeroValueForNegation(LHS->getType()) && + L.match(RHS, Context); } }; @@ -554,10 +557,10 @@ struct brc_match { } template - bool match(OpTy *V) { + bool match(OpTy *V, LLVMContext &Context) { if (BranchInst *BI = dyn_cast(V)) if (BI->isConditional()) { - if (Cond.match(BI->getCondition())) { + if (Cond.match(BI->getCondition(), Context)) { T = BI->getSuccessor(0); F = BI->getSuccessor(1); return true; diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 89b4c6569be..79de4432d8c 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -1889,7 +1889,7 @@ struct AddMaskingAnd { AddMaskingAnd(Constant *c, LLVMContext *C) : C2(c), Context(C) {} bool shouldApply(Value *LHS) const { ConstantInt *C1; - return match(LHS, m_And(m_Value(), m_ConstantInt(C1))) && + return match(LHS, m_And(m_Value(), m_ConstantInt(C1)), *Context) && Context->getConstantExprAnd(C1, C2)->isNullValue(); } Instruction *apply(BinaryOperator &Add) const { @@ -2114,7 +2114,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { ConstantInt *XorRHS = 0; Value *XorLHS = 0; if (isa(RHSC) && - match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)))) { + match(LHS, m_Xor(m_Value(XorLHS), m_ConstantInt(XorRHS)), *Context)) { uint32_t TySizeBits = I.getType()->getScalarSizeInBits(); const APInt& RHSVal = cast(RHSC)->getValue(); @@ -2220,7 +2220,7 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // (A & C1)+(B & C2) --> (A & C1)|(B & C2) iff C1&C2 == 0 - if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)))) + if (match(RHS, m_And(m_Value(), m_ConstantInt(C2)), *Context)) if (Instruction *R = AssociativeOpt(I, AddMaskingAnd(C2, Context), Context)) return R; @@ -2244,8 +2244,8 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // W*X + Y*Z --> W * (X+Z) iff W == Y if (I.getType()->isIntOrIntVector()) { Value *W, *X, *Y, *Z; - if (match(LHS, m_Mul(m_Value(W), m_Value(X))) && - match(RHS, m_Mul(m_Value(Y), m_Value(Z)))) { + if (match(LHS, m_Mul(m_Value(W), m_Value(X)), *Context) && + match(RHS, m_Mul(m_Value(Y), m_Value(Z)), *Context)) { if (W != Y) { if (W == Z) { std::swap(Y, Z); @@ -2267,11 +2267,12 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { if (ConstantInt *CRHS = dyn_cast(RHS)) { Value *X = 0; - if (match(LHS, m_Not(m_Value(X)))) // ~X + C --> (C-1) - X + if (match(LHS, m_Not(m_Value(X)), *Context)) // ~X + C --> (C-1) - X return BinaryOperator::CreateSub(SubOne(CRHS, Context), X); // (X & FF00) + xx00 -> (X+xx00) & FF00 - if (LHS->hasOneUse() && match(LHS, m_And(m_Value(X), m_ConstantInt(C2)))) { + if (LHS->hasOneUse() && + match(LHS, m_And(m_Value(X), m_ConstantInt(C2)), *Context)) { Constant *Anded = Context->getConstantExprAnd(CRHS, C2); if (Anded == CRHS) { // See if all bits from the first bit set in the Add RHS up are included @@ -2336,10 +2337,12 @@ Instruction *InstCombiner::visitAdd(BinaryOperator &I) { // Can we fold the add into the argument of the select? // We check both true and false select arguments for a matching subtract. - if (match(FV, m_Zero()) && match(TV, m_Sub(m_Value(N), m_Specific(A)))) + if (match(FV, m_Zero(), *Context) && + match(TV, m_Sub(m_Value(N), m_Specific(A)), *Context)) // Fold the add into the true select value. return SelectInst::Create(SI->getCondition(), N, A); - if (match(TV, m_Zero()) && match(FV, m_Sub(m_Value(N), m_Specific(A)))) + if (match(TV, m_Zero(), *Context) && + match(FV, m_Sub(m_Value(N), m_Specific(A)), *Context)) // Fold the add into the false select value. return SelectInst::Create(SI->getCondition(), A, N); } @@ -2483,7 +2486,7 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) { // C - ~X == X + (1+C) Value *X = 0; - if (match(Op1, m_Not(m_Value(X)))) + if (match(Op1, m_Not(m_Value(X)), *Context)) return BinaryOperator::CreateAdd(X, AddOne(C, Context)); // -(X >>u 31) -> (X >>s 31) @@ -3786,8 +3789,10 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, ICmpInst::Predicate LHSCC, RHSCC; // This only handles icmp of constants: (icmp1 A, C1) & (icmp2 B, C2). - if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) || - !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst)))) + if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), + m_ConstantInt(LHSCst)), *Context) || + !match(RHS, m_ICmp(RHSCC, m_Value(Val2), + m_ConstantInt(RHSCst)), *Context)) return 0; // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) @@ -4120,30 +4125,30 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { { Value *A = 0, *B = 0, *C = 0, *D = 0; - if (match(Op0, m_Or(m_Value(A), m_Value(B)))) { + if (match(Op0, m_Or(m_Value(A), m_Value(B)), *Context)) { if (A == Op1 || B == Op1) // (A | ?) & A --> A return ReplaceInstUsesWith(I, Op1); // (A|B) & ~(A&B) -> A^B - if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))))) { + if (match(Op1, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) { if ((A == C && B == D) || (A == D && B == C)) return BinaryOperator::CreateXor(A, B); } } - if (match(Op1, m_Or(m_Value(A), m_Value(B)))) { + if (match(Op1, m_Or(m_Value(A), m_Value(B)), *Context)) { if (A == Op0 || B == Op0) // A & (A | ?) --> A return ReplaceInstUsesWith(I, Op0); // ~(A&B) & (A|B) -> A^B - if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))))) { + if (match(Op0, m_Not(m_And(m_Value(C), m_Value(D))), *Context)) { if ((A == C && B == D) || (A == D && B == C)) return BinaryOperator::CreateXor(A, B); } } if (Op0->hasOneUse() && - match(Op0, m_Xor(m_Value(A), m_Value(B)))) { + match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) { if (A == Op1) { // (A^B)&A -> A&(A^B) I.swapOperands(); // Simplify below std::swap(Op0, Op1); @@ -4155,7 +4160,7 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { } if (Op1->hasOneUse() && - match(Op1, m_Xor(m_Value(A), m_Value(B)))) { + match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context)) { if (B == Op0) { // B&(A^B) -> B&(B^A) cast(Op1)->swapOperands(); std::swap(A, B); @@ -4168,11 +4173,11 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { } // (A&((~A)|B)) -> A&B - if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A))) || - match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))))) + if (match(Op0, m_Or(m_Not(m_Specific(Op1)), m_Value(A)), *Context) || + match(Op0, m_Or(m_Value(A), m_Not(m_Specific(Op1))), *Context)) return BinaryOperator::CreateAnd(A, Op1); - if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A))) || - match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))))) + if (match(Op1, m_Or(m_Not(m_Specific(Op0)), m_Value(A)), *Context) || + match(Op1, m_Or(m_Value(A), m_Not(m_Specific(Op0))), *Context)) return BinaryOperator::CreateAnd(A, Op0); } @@ -4238,8 +4243,10 @@ Instruction *InstCombiner::visitAnd(BinaryOperator &I) { } else { Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS; FCmpInst::Predicate Op0CC, Op1CC; - if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), m_Value(Op0RHS))) && - match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), m_Value(Op1RHS)))) { + if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), + m_Value(Op0RHS)), *Context) && + match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), + m_Value(Op1RHS)), *Context)) { if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { // Swap RHS operands to match LHS. Op1CC = FCmpInst::getSwappedPredicate(Op1CC); @@ -4452,21 +4459,22 @@ Instruction *InstCombiner::MatchBSwap(BinaryOperator &I) { /// If A is (cond?-1:0) and either B or D is ~(cond?-1,0) or (cond?0,-1), then /// we can simplify this expression to "cond ? C : D or B". static Instruction *MatchSelectFromAndOr(Value *A, Value *B, - Value *C, Value *D) { + Value *C, Value *D, + LLVMContext *Context) { // If A is not a select of -1/0, this cannot match. Value *Cond = 0; - if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)))) + if (!match(A, m_SelectCst<-1, 0>(m_Value(Cond)), *Context)) return 0; // ((cond?-1:0)&C) | (B&(cond?0:-1)) -> cond ? C : B. - if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)))) + if (match(D, m_SelectCst<0, -1>(m_Specific(Cond)), *Context)) return SelectInst::Create(Cond, C, B); - if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))))) + if (match(D, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context)) return SelectInst::Create(Cond, C, B); // ((cond?-1:0)&C) | ((cond?0:-1)&D) -> cond ? C : D. - if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)))) + if (match(B, m_SelectCst<0, -1>(m_Specific(Cond)), *Context)) return SelectInst::Create(Cond, C, D); - if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))))) + if (match(B, m_Not(m_SelectCst<-1, 0>(m_Specific(Cond))), *Context)) return SelectInst::Create(Cond, C, D); return 0; } @@ -4479,8 +4487,10 @@ Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, ICmpInst::Predicate LHSCC, RHSCC; // This only handles icmp of constants: (icmp1 A, C1) | (icmp2 B, C2). - if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), m_ConstantInt(LHSCst))) || - !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst)))) + if (!match(LHS, m_ICmp(LHSCC, m_Value(Val), + m_ConstantInt(LHSCst)), *Context) || + !match(RHS, m_ICmp(RHSCC, m_Value(Val2), + m_ConstantInt(RHSCst)), *Context)) return 0; // From here on, we only handle: @@ -4651,7 +4661,7 @@ Instruction *InstCombiner::FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *V1 = 0; ConstantInt *CI2 = 0; - if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)))) return 0; + if (!match(Op, m_And(m_Value(V1), m_ConstantInt(CI2)), *Context)) return 0; APInt Xor = CI1->getValue() ^ CI2->getValue(); if (!Xor.isAllOnesValue()) return 0; @@ -4693,7 +4703,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { if (ConstantInt *RHS = dyn_cast(Op1)) { ConstantInt *C1 = 0; Value *X = 0; // (X & C1) | C2 --> (X | C2) & (C1|C2) - if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { + if (match(Op0, m_And(m_Value(X), m_ConstantInt(C1)), *Context) && + isOnlyUse(Op0)) { Instruction *Or = BinaryOperator::CreateOr(X, RHS); InsertNewInstBefore(Or, I); Or->takeName(Op0); @@ -4702,7 +4713,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2) - if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1))) && isOnlyUse(Op0)) { + if (match(Op0, m_Xor(m_Value(X), m_ConstantInt(C1)), *Context) && + isOnlyUse(Op0)) { Instruction *Or = BinaryOperator::CreateOr(X, RHS); InsertNewInstBefore(Or, I); Or->takeName(Op0); @@ -4722,25 +4734,26 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { Value *A = 0, *B = 0; ConstantInt *C1 = 0, *C2 = 0; - if (match(Op0, m_And(m_Value(A), m_Value(B)))) + if (match(Op0, m_And(m_Value(A), m_Value(B)), *Context)) if (A == Op1 || B == Op1) // (A & ?) | A --> A return ReplaceInstUsesWith(I, Op1); - if (match(Op1, m_And(m_Value(A), m_Value(B)))) + if (match(Op1, m_And(m_Value(A), m_Value(B)), *Context)) if (A == Op0 || B == Op0) // A | (A & ?) --> A return ReplaceInstUsesWith(I, Op0); // (A | B) | C and A | (B | C) -> bswap if possible. // (A >> B) | (C << D) and (A << B) | (B >> C) -> bswap if possible. - if (match(Op0, m_Or(m_Value(), m_Value())) || - match(Op1, m_Or(m_Value(), m_Value())) || - (match(Op0, m_Shift(m_Value(), m_Value())) && - match(Op1, m_Shift(m_Value(), m_Value())))) { + if (match(Op0, m_Or(m_Value(), m_Value()), *Context) || + match(Op1, m_Or(m_Value(), m_Value()), *Context) || + (match(Op0, m_Shift(m_Value(), m_Value()), *Context) && + match(Op1, m_Shift(m_Value(), m_Value()), *Context))) { if (Instruction *BSwap = MatchBSwap(I)) return BSwap; } // (X^C)|Y -> (X|Y)^C iff Y&C == 0 - if (Op0->hasOneUse() && match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1))) && + if (Op0->hasOneUse() && + match(Op0, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) && MaskedValueIsZero(Op1, C1->getValue())) { Instruction *NOr = BinaryOperator::CreateOr(A, Op1); InsertNewInstBefore(NOr, I); @@ -4749,7 +4762,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // Y|(X^C) -> (X|Y)^C iff Y&C == 0 - if (Op1->hasOneUse() && match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1))) && + if (Op1->hasOneUse() && + match(Op1, m_Xor(m_Value(A), m_ConstantInt(C1)), *Context) && MaskedValueIsZero(Op0, C1->getValue())) { Instruction *NOr = BinaryOperator::CreateOr(A, Op0); InsertNewInstBefore(NOr, I); @@ -4759,8 +4773,8 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { // (A & C)|(B & D) Value *C = 0, *D = 0; - if (match(Op0, m_And(m_Value(A), m_Value(C))) && - match(Op1, m_And(m_Value(B), m_Value(D)))) { + if (match(Op0, m_And(m_Value(A), m_Value(C)), *Context) && + match(Op1, m_And(m_Value(B), m_Value(D)), *Context)) { Value *V1 = 0, *V2 = 0, *V3 = 0; C1 = dyn_cast(C); C2 = dyn_cast(D); @@ -4770,7 +4784,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { // replace with V+N. if (C1->getValue() == ~C2->getValue()) { if ((C2->getValue() & (C2->getValue()+1)) == 0 && // C2 == 0+1+ - match(A, m_Add(m_Value(V1), m_Value(V2)))) { + match(A, m_Add(m_Value(V1), m_Value(V2)), *Context)) { // Add commutes, try both ways. if (V1 == B && MaskedValueIsZero(V2, C2->getValue())) return ReplaceInstUsesWith(I, A); @@ -4779,7 +4793,7 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // Or commutes, try both ways. if ((C1->getValue() & (C1->getValue()+1)) == 0 && - match(B, m_Add(m_Value(V1), m_Value(V2)))) { + match(B, m_Add(m_Value(V1), m_Value(V2)), *Context)) { // Add commutes, try both ways. if (V1 == A && MaskedValueIsZero(V2, C1->getValue())) return ReplaceInstUsesWith(I, B); @@ -4810,30 +4824,30 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // (A & (C0?-1:0)) | (B & ~(C0?-1:0)) -> C0 ? A : B, and commuted variants - if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D)) + if (Instruction *Match = MatchSelectFromAndOr(A, B, C, D, Context)) return Match; - if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C)) + if (Instruction *Match = MatchSelectFromAndOr(B, A, D, C, Context)) return Match; - if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D)) + if (Instruction *Match = MatchSelectFromAndOr(C, B, A, D, Context)) return Match; - if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C)) + if (Instruction *Match = MatchSelectFromAndOr(D, A, B, C, Context)) return Match; // ((A&~B)|(~A&B)) -> A^B - if ((match(C, m_Not(m_Specific(D))) && - match(B, m_Not(m_Specific(A))))) + if ((match(C, m_Not(m_Specific(D)), *Context) && + match(B, m_Not(m_Specific(A)), *Context))) return BinaryOperator::CreateXor(A, D); // ((~B&A)|(~A&B)) -> A^B - if ((match(A, m_Not(m_Specific(D))) && - match(B, m_Not(m_Specific(C))))) + if ((match(A, m_Not(m_Specific(D)), *Context) && + match(B, m_Not(m_Specific(C)), *Context))) return BinaryOperator::CreateXor(C, D); // ((A&~B)|(B&~A)) -> A^B - if ((match(C, m_Not(m_Specific(B))) && - match(D, m_Not(m_Specific(A))))) + if ((match(C, m_Not(m_Specific(B)), *Context) && + match(D, m_Not(m_Specific(A)), *Context))) return BinaryOperator::CreateXor(A, B); // ((~B&A)|(B&~A)) -> A^B - if ((match(A, m_Not(m_Specific(B))) && - match(D, m_Not(m_Specific(C))))) + if ((match(A, m_Not(m_Specific(B)), *Context) && + match(D, m_Not(m_Specific(C)), *Context))) return BinaryOperator::CreateXor(C, B); } @@ -4853,26 +4867,26 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } // ((A|B)&1)|(B&-2) -> (A&1) | B - if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) || - match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) { + if (match(Op0, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) || + match(Op0, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) { Instruction *Ret = FoldOrWithConstants(I, Op1, A, B, C); if (Ret) return Ret; } // (B&-2)|((A|B)&1) -> (A&1) | B - if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C))) || - match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))))) { + if (match(Op1, m_And(m_Or(m_Value(A), m_Value(B)), m_Value(C)), *Context) || + match(Op1, m_And(m_Value(C), m_Or(m_Value(A), m_Value(B))), *Context)) { Instruction *Ret = FoldOrWithConstants(I, Op0, A, B, C); if (Ret) return Ret; } - if (match(Op0, m_Not(m_Value(A)))) { // ~A | Op1 + if (match(Op0, m_Not(m_Value(A)), *Context)) { // ~A | Op1 if (A == Op1) // ~A | A == -1 return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); } else { A = 0; } // Note, A is still live here! - if (match(Op1, m_Not(m_Value(B)))) { // Op0 | ~B + if (match(Op1, m_Not(m_Value(B)), *Context)) { // Op0 | ~B if (Op0 == B) return ReplaceInstUsesWith(I, Context->getAllOnesValue(I.getType())); @@ -4940,8 +4954,10 @@ Instruction *InstCombiner::visitOr(BinaryOperator &I) { } else { Value *Op0LHS, *Op0RHS, *Op1LHS, *Op1RHS; FCmpInst::Predicate Op0CC, Op1CC; - if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), m_Value(Op0RHS))) && - match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), m_Value(Op1RHS)))) { + if (match(Op0, m_FCmp(Op0CC, m_Value(Op0LHS), + m_Value(Op0RHS)), *Context) && + match(Op1, m_FCmp(Op1CC, m_Value(Op1LHS), + m_Value(Op1RHS)), *Context)) { if (Op0LHS == Op1RHS && Op0RHS == Op1LHS) { // Swap RHS operands to match LHS. Op1CC = FCmpInst::getSwappedPredicate(Op1CC); @@ -5143,7 +5159,7 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { BinaryOperator *Op1I = dyn_cast(Op1); if (Op1I) { Value *A, *B; - if (match(Op1I, m_Or(m_Value(A), m_Value(B)))) { + if (match(Op1I, m_Or(m_Value(A), m_Value(B)), *Context)) { if (A == Op0) { // B^(B|A) == (A|B)^B Op1I->swapOperands(); I.swapOperands(); @@ -5152,11 +5168,12 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { I.swapOperands(); // Simplified below. std::swap(Op0, Op1); } - } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)))) { + } else if (match(Op1I, m_Xor(m_Specific(Op0), m_Value(B)), *Context)) { return ReplaceInstUsesWith(I, B); // A^(A^B) == B - } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)))) { + } else if (match(Op1I, m_Xor(m_Value(A), m_Specific(Op0)), *Context)) { return ReplaceInstUsesWith(I, A); // A^(B^A) == B - } else if (match(Op1I, m_And(m_Value(A), m_Value(B))) && Op1I->hasOneUse()){ + } else if (match(Op1I, m_And(m_Value(A), m_Value(B)), *Context) && + Op1I->hasOneUse()){ if (A == Op0) { // A^(A&B) -> A^(B&A) Op1I->swapOperands(); std::swap(A, B); @@ -5171,7 +5188,8 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { BinaryOperator *Op0I = dyn_cast(Op0); if (Op0I) { Value *A, *B; - if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && Op0I->hasOneUse()) { + if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) && + Op0I->hasOneUse()) { if (A == Op1) // (B|A)^B == (A|B)^B std::swap(A, B); if (B == Op1) { // (A|B)^B == A & ~B @@ -5179,11 +5197,12 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { InsertNewInstBefore(BinaryOperator::CreateNot(Op1, "tmp"), I); return BinaryOperator::CreateAnd(A, NotB); } - } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)))) { + } else if (match(Op0I, m_Xor(m_Specific(Op1), m_Value(B)), *Context)) { return ReplaceInstUsesWith(I, B); // (A^B)^A == B - } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)))) { + } else if (match(Op0I, m_Xor(m_Value(A), m_Specific(Op1)), *Context)) { return ReplaceInstUsesWith(I, A); // (B^A)^A == B - } else if (match(Op0I, m_And(m_Value(A), m_Value(B))) && Op0I->hasOneUse()){ + } else if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) && + Op0I->hasOneUse()){ if (A == Op1) // (A&B)^A -> (B&A)^A std::swap(A, B); if (B == Op1 && // (B&A)^A == ~B & A @@ -5211,22 +5230,22 @@ Instruction *InstCombiner::visitXor(BinaryOperator &I) { if (Op0I && Op1I) { Value *A, *B, *C, *D; // (A & B)^(A | B) -> A ^ B - if (match(Op0I, m_And(m_Value(A), m_Value(B))) && - match(Op1I, m_Or(m_Value(C), m_Value(D)))) { + if (match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) && + match(Op1I, m_Or(m_Value(C), m_Value(D)), *Context)) { if ((A == C && B == D) || (A == D && B == C)) return BinaryOperator::CreateXor(A, B); } // (A | B)^(A & B) -> A ^ B - if (match(Op0I, m_Or(m_Value(A), m_Value(B))) && - match(Op1I, m_And(m_Value(C), m_Value(D)))) { + if (match(Op0I, m_Or(m_Value(A), m_Value(B)), *Context) && + match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) { if ((A == C && B == D) || (A == D && B == C)) return BinaryOperator::CreateXor(A, B); } // (A & B)^(C & D) if ((Op0I->hasOneUse() || Op1I->hasOneUse()) && - match(Op0I, m_And(m_Value(A), m_Value(B))) && - match(Op1I, m_And(m_Value(C), m_Value(D)))) { + match(Op0I, m_And(m_Value(A), m_Value(B)), *Context) && + match(Op1I, m_And(m_Value(C), m_Value(D)), *Context)) { // (X & Y)^(X & Y) -> (Y^Z) & X Value *X = 0, *Y = 0, *Z = 0; if (A == C) @@ -6033,7 +6052,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // (icmp ne/eq (sub A B) 0) -> (icmp ne/eq A, B) if (I.isEquality() && CI->isNullValue() && - match(Op0, m_Sub(m_Value(A), m_Value(B)))) { + match(Op0, m_Sub(m_Value(A), m_Value(B)), *Context)) { // (icmp cond A B) if cond is equality return new ICmpInst(*Context, I.getPredicate(), A, B); } @@ -6431,8 +6450,8 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { // ~x < ~y --> y < x { Value *A, *B; - if (match(Op0, m_Not(m_Value(A))) && - match(Op1, m_Not(m_Value(B)))) + if (match(Op0, m_Not(m_Value(A)), *Context) && + match(Op1, m_Not(m_Value(B)), *Context)) return new ICmpInst(*Context, I.getPredicate(), B, A); } @@ -6440,22 +6459,22 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { Value *A, *B, *C, *D; // -x == -y --> x == y - if (match(Op0, m_Neg(m_Value(A))) && - match(Op1, m_Neg(m_Value(B)))) + if (match(Op0, m_Neg(m_Value(A)), *Context) && + match(Op1, m_Neg(m_Value(B)), *Context)) return new ICmpInst(*Context, I.getPredicate(), A, B); - if (match(Op0, m_Xor(m_Value(A), m_Value(B)))) { + if (match(Op0, m_Xor(m_Value(A), m_Value(B)), *Context)) { if (A == Op1 || B == Op1) { // (A^B) == A -> B == 0 Value *OtherVal = A == Op1 ? B : A; return new ICmpInst(*Context, I.getPredicate(), OtherVal, Context->getNullValue(A->getType())); } - if (match(Op1, m_Xor(m_Value(C), m_Value(D)))) { + if (match(Op1, m_Xor(m_Value(C), m_Value(D)), *Context)) { // A^c1 == C^c2 --> A == C^(c1^c2) ConstantInt *C1, *C2; - if (match(B, m_ConstantInt(C1)) && - match(D, m_ConstantInt(C2)) && Op1->hasOneUse()) { + if (match(B, m_ConstantInt(C1), *Context) && + match(D, m_ConstantInt(C2), *Context) && Op1->hasOneUse()) { Constant *NC = Context->getConstantInt(C1->getValue() ^ C2->getValue()); Instruction *Xor = BinaryOperator::CreateXor(C, NC, "tmp"); @@ -6471,7 +6490,7 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } } - if (match(Op1, m_Xor(m_Value(A), m_Value(B))) && + if (match(Op1, m_Xor(m_Value(A), m_Value(B)), *Context) && (A == Op0 || B == Op0)) { // A == (A^B) -> B == 0 Value *OtherVal = A == Op0 ? B : A; @@ -6480,19 +6499,19 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) { } // (A-B) == A -> B == 0 - if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)))) + if (match(Op0, m_Sub(m_Specific(Op1), m_Value(B)), *Context)) return new ICmpInst(*Context, I.getPredicate(), B, Context->getNullValue(B->getType())); // A == (A-B) -> B == 0 - if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)))) + if (match(Op1, m_Sub(m_Specific(Op0), m_Value(B)), *Context)) return new ICmpInst(*Context, I.getPredicate(), B, Context->getNullValue(B->getType())); // (X&Z) == (Y&Z) -> (X^Y) & Z == 0 if (Op0->hasOneUse() && Op1->hasOneUse() && - match(Op0, m_And(m_Value(A), m_Value(B))) && - match(Op1, m_And(m_Value(C), m_Value(D)))) { + match(Op0, m_And(m_Value(A), m_Value(B)), *Context) && + match(Op1, m_And(m_Value(C), m_Value(D)), *Context)) { Value *X = 0, *Y = 0, *Z = 0; if (A == C) { @@ -7458,7 +7477,8 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, // These operators commute. // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && - match(Op0BO->getOperand(1), m_Shr(m_Value(V1), m_Specific(Op1)))){ + match(Op0BO->getOperand(1), m_Shr(m_Value(V1), + m_Specific(Op1)), *Context)){ Instruction *YS = BinaryOperator::CreateShl( Op0BO->getOperand(0), Op1, Op0BO->getName()); @@ -7477,7 +7497,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, if (isLeftShift && Op0BOOp1->hasOneUse() && match(Op0BOOp1, m_And(m_Shr(m_Value(V1), m_Specific(Op1)), - m_ConstantInt(CC))) && + m_ConstantInt(CC)), *Context) && cast(Op0BOOp1)->getOperand(0)->hasOneUse()) { Instruction *YS = BinaryOperator::CreateShl( Op0BO->getOperand(0), Op1, @@ -7497,7 +7517,8 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, case Instruction::Sub: { // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && - match(Op0BO->getOperand(0), m_Shr(m_Value(V1), m_Specific(Op1)))){ + match(Op0BO->getOperand(0), m_Shr(m_Value(V1), + m_Specific(Op1)), *Context)){ Instruction *YS = BinaryOperator::CreateShl( Op0BO->getOperand(1), Op1, Op0BO->getName()); @@ -7515,7 +7536,7 @@ Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, ConstantInt *Op1, if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && match(Op0BO->getOperand(0), m_And(m_Shr(m_Value(V1), m_Value(V2)), - m_ConstantInt(CC))) && V2 == Op1 && + m_ConstantInt(CC)), *Context) && V2 == Op1 && cast(Op0BO->getOperand(0)) ->getOperand(0)->hasOneUse()) { Instruction *YS = BinaryOperator::CreateShl( @@ -8476,7 +8497,7 @@ Instruction *InstCombiner::visitTrunc(TruncInst &CI) { ConstantInt *ShAmtV = 0; Value *ShiftOp = 0; if (Src->hasOneUse() && - match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)))) { + match(Src, m_LShr(m_Value(ShiftOp), m_ConstantInt(ShAmtV)), *Context)) { uint32_t ShAmt = ShAmtV->getLimitedValue(SrcBitWidth); // Get a mask for the bits shifting in. @@ -8736,7 +8757,7 @@ Instruction *InstCombiner::visitSExt(SExtInst &CI) { Value *A = 0; ConstantInt *BA = 0, *CA = 0; if (match(Src, m_AShr(m_Shl(m_Value(A), m_ConstantInt(BA)), - m_ConstantInt(CA))) && + m_ConstantInt(CA)), *Context) && BA == CA && isa(A)) { Value *I = cast(A)->getOperand(0); if (I->getType() == CI.getType()) { @@ -8923,7 +8944,7 @@ Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { ConstantInt *Cst; Value *X; if (match(CI.getOperand(0), m_Add(m_Cast(m_Value(X)), - m_ConstantInt(Cst)))) { + m_ConstantInt(Cst)), *Context)) { // If the source and destination operands have the same type, see if this // is a single-index GEP. if (X->getType() == CI.getType()) { @@ -8943,7 +8964,8 @@ Instruction *InstCombiner::visitIntToPtr(IntToPtrInst &CI) { // TODO: Could handle other cases, e.g. where add is indexing into field of // struct etc. } else if (CI.getOperand(0)->hasOneUse() && - match(CI.getOperand(0), m_Add(m_Value(X), m_ConstantInt(Cst)))) { + match(CI.getOperand(0), m_Add(m_Value(X), + m_ConstantInt(Cst)), *Context)) { // Otherwise, if this is inttoptr(add x, cst), try to turn this into an // "inttoptr+GEP" instead of "add+intptr". @@ -9319,11 +9341,11 @@ Instruction *InstCombiner::visitSelectInstWithICmp(SelectInst &SI, // (x ashr x, 31 -> all ones if signed // (x >s -1) ? -1 : 0 -> ashr x, 31 -> all ones if not signed CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE; - if (match(TrueVal, m_ConstantInt<-1>()) && - match(FalseVal, m_ConstantInt<0>())) + if (match(TrueVal, m_ConstantInt<-1>(), *Context) && + match(FalseVal, m_ConstantInt<0>(), *Context)) Pred = ICI->getPredicate(); - else if (match(TrueVal, m_ConstantInt<0>()) && - match(FalseVal, m_ConstantInt<-1>())) + else if (match(TrueVal, m_ConstantInt<0>(), *Context) && + match(FalseVal, m_ConstantInt<-1>(), *Context)) Pred = CmpInst::getInversePredicate(ICI->getPredicate()); if (Pred != CmpInst::BAD_ICMP_PREDICATE) { @@ -12101,7 +12123,7 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { Value *X = 0; BasicBlock *TrueDest; BasicBlock *FalseDest; - if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest)) && + if (match(&BI, m_Br(m_Not(m_Value(X)), TrueDest, FalseDest), *Context) && !isa(X)) { // Swap Destinations and condition... BI.setCondition(X); @@ -12113,7 +12135,7 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { // Cannonicalize fcmp_one -> fcmp_oeq FCmpInst::Predicate FPred; Value *Y; if (match(&BI, m_Br(m_FCmp(FPred, m_Value(X), m_Value(Y)), - TrueDest, FalseDest))) + TrueDest, FalseDest), *Context)) if ((FPred == FCmpInst::FCMP_ONE || FPred == FCmpInst::FCMP_OLE || FPred == FCmpInst::FCMP_OGE) && BI.getCondition()->hasOneUse()) { FCmpInst *I = cast(BI.getCondition()); @@ -12133,7 +12155,7 @@ Instruction *InstCombiner::visitBranchInst(BranchInst &BI) { // Cannonicalize icmp_ne -> icmp_eq ICmpInst::Predicate IPred; if (match(&BI, m_Br(m_ICmp(IPred, m_Value(X), m_Value(Y)), - TrueDest, FalseDest))) + TrueDest, FalseDest), *Context)) if ((IPred == ICmpInst::ICMP_NE || IPred == ICmpInst::ICMP_ULE || IPred == ICmpInst::ICMP_SLE || IPred == ICmpInst::ICMP_UGE || IPred == ICmpInst::ICMP_SGE) && BI.getCondition()->hasOneUse()) { diff --git a/lib/Transforms/Utils/AddrModeMatcher.cpp b/lib/Transforms/Utils/AddrModeMatcher.cpp index 71049fa212d..7a2bf8587b1 100644 --- a/lib/Transforms/Utils/AddrModeMatcher.cpp +++ b/lib/Transforms/Utils/AddrModeMatcher.cpp @@ -96,7 +96,8 @@ bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale, // X*Scale + C*Scale to addr mode. ConstantInt *CI = 0; Value *AddLHS = 0; if (isa(ScaleReg) && // not a constant expr. - match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) { + match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)), + *MemoryInst->getParent()->getContext())) { TestAddrMode.ScaledReg = AddLHS; TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale; -- 2.34.1