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 {
ConstantInt *XorRHS = 0;
Value *XorLHS = 0;
if (isa<ConstantInt>(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<ConstantInt>(RHSC)->getValue();
// (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;
// 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);
if (ConstantInt *CRHS = dyn_cast<ConstantInt>(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
// 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);
}
// 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)
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)
{
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);
}
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<BinaryOperator>(Op1)->swapOperands();
std::swap(A, B);
}
// (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);
}
} 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);
/// 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;
}
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:
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;
if (ConstantInt *RHS = dyn_cast<ConstantInt>(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);
}
// (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);
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);
}
// 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);
// (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<ConstantInt>(C);
C2 = dyn_cast<ConstantInt>(D);
// 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);
}
// 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);
}
// (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);
}
}
// ((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()));
} 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);
BinaryOperator *Op1I = dyn_cast<BinaryOperator>(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();
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);
BinaryOperator *Op0I = dyn_cast<BinaryOperator>(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
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
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)
// (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);
}
// ~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);
}
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");
}
}
- 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;
}
// (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) {
// 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());
if (isLeftShift && Op0BOOp1->hasOneUse() &&
match(Op0BOOp1,
m_And(m_Shr(m_Value(V1), m_Specific(Op1)),
- m_ConstantInt(CC))) &&
+ m_ConstantInt(CC)), *Context) &&
cast<BinaryOperator>(Op0BOOp1)->getOperand(0)->hasOneUse()) {
Instruction *YS = BinaryOperator::CreateShl(
Op0BO->getOperand(0), 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());
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<BinaryOperator>(Op0BO->getOperand(0))
->getOperand(0)->hasOneUse()) {
Instruction *YS = BinaryOperator::CreateShl(
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.
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<TruncInst>(A)) {
Value *I = cast<TruncInst>(A)->getOperand(0);
if (I->getType() == CI.getType()) {
ConstantInt *Cst;
Value *X;
if (match(CI.getOperand(0), m_Add(m_Cast<PtrToIntInst>(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()) {
// 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".
// (x <s 0) ? -1 : 0 -> 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) {
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<Constant>(X)) {
// Swap Destinations and condition...
BI.setCondition(X);
// 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<FCmpInst>(BI.getCondition());
// 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()) {