From d48ff7ea6a55e5ba5b60253172f3124c3c7d7312 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 29 Nov 2009 00:51:17 +0000 Subject: [PATCH] Implement PR5634. llvm-svn: 90046 --- .../Scalar/InstructionCombining.cpp | 64 ++++++++++++++++--- test/Transforms/InstCombine/or.ll | 44 ++++++++++++- 2 files changed, 97 insertions(+), 11 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index c591ef841c0..ce471b398c5 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -4067,6 +4067,21 @@ Value *InstCombiner::FoldLogicalPlusAnd(Value *LHS, Value *RHS, /// FoldAndOfICmps - Fold (icmp)&(icmp) if possible. Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS) { + // (icmp eq A, null) & (icmp eq B, null) --> + // (icmp eq (ptrtoint(A)|ptrtoint(B)), 0) + if (TD && + LHS->getPredicate() == ICmpInst::ICMP_EQ && + RHS->getPredicate() == ICmpInst::ICMP_EQ && + isa(LHS->getOperand(1)) && + isa(RHS->getOperand(1))) { + const Type *IntPtrTy = TD->getIntPtrType(I.getContext()); + Value *A = Builder->CreatePtrToInt(LHS->getOperand(0), IntPtrTy); + Value *B = Builder->CreatePtrToInt(RHS->getOperand(0), IntPtrTy); + Value *NewOr = Builder->CreateOr(A, B); + return new ICmpInst(ICmpInst::ICMP_EQ, NewOr, + Constant::getNullValue(IntPtrTy)); + } + Value *Val, *Val2; ConstantInt *LHSCst, *RHSCst; ICmpInst::Predicate LHSCC, RHSCC; @@ -4078,12 +4093,20 @@ Instruction *InstCombiner::FoldAndOfICmps(Instruction &I, m_ConstantInt(RHSCst)))) return 0; - // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) - // where C is a power of 2 - if (LHSCst == RHSCst && LHSCC == RHSCC && LHSCC == ICmpInst::ICMP_ULT && - LHSCst->getValue().isPowerOf2()) { - Value *NewOr = Builder->CreateOr(Val, Val2); - return new ICmpInst(LHSCC, NewOr, LHSCst); + if (LHSCst == RHSCst && LHSCC == RHSCC) { + // (icmp ult A, C) & (icmp ult B, C) --> (icmp ult (A|B), C) + // where C is a power of 2 + if (LHSCC == ICmpInst::ICMP_ULT && + LHSCst->getValue().isPowerOf2()) { + Value *NewOr = Builder->CreateOr(Val, Val2); + return new ICmpInst(LHSCC, NewOr, LHSCst); + } + + // (icmp eq A, 0) & (icmp eq B, 0) --> (icmp eq (A|B), 0) + if (LHSCC == ICmpInst::ICMP_EQ && LHSCst->isZero()) { + Value *NewOr = Builder->CreateOr(Val, Val2); + return new ICmpInst(LHSCC, NewOr, LHSCst); + } } // From here on, we only handle: @@ -4739,16 +4762,37 @@ static Instruction *MatchSelectFromAndOr(Value *A, Value *B, /// FoldOrOfICmps - Fold (icmp)|(icmp) if possible. Instruction *InstCombiner::FoldOrOfICmps(Instruction &I, ICmpInst *LHS, ICmpInst *RHS) { + // (icmp ne A, null) | (icmp ne B, null) --> + // (icmp ne (ptrtoint(A)|ptrtoint(B)), 0) + if (TD && + LHS->getPredicate() == ICmpInst::ICMP_NE && + RHS->getPredicate() == ICmpInst::ICMP_NE && + isa(LHS->getOperand(1)) && + isa(RHS->getOperand(1))) { + const Type *IntPtrTy = TD->getIntPtrType(I.getContext()); + Value *A = Builder->CreatePtrToInt(LHS->getOperand(0), IntPtrTy); + Value *B = Builder->CreatePtrToInt(RHS->getOperand(0), IntPtrTy); + Value *NewOr = Builder->CreateOr(A, B); + return new ICmpInst(ICmpInst::ICMP_NE, NewOr, + Constant::getNullValue(IntPtrTy)); + } + Value *Val, *Val2; ConstantInt *LHSCst, *RHSCst; 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))) || + !match(RHS, m_ICmp(RHSCC, m_Value(Val2), m_ConstantInt(RHSCst)))) return 0; + + + // (icmp ne A, 0) | (icmp ne B, 0) --> (icmp ne (A|B), 0) + if (LHSCst == RHSCst && LHSCC == RHSCC && + LHSCC == ICmpInst::ICMP_NE && LHSCst->isZero()) { + Value *NewOr = Builder->CreateOr(Val, Val2); + return new ICmpInst(LHSCC, NewOr, LHSCst); + } // From here on, we only handle: // (icmp1 A, C1) | (icmp2 A, C2) --> something simpler. diff --git a/test/Transforms/InstCombine/or.ll b/test/Transforms/InstCombine/or.ll index b72480b4f9d..4a140815f62 100644 --- a/test/Transforms/InstCombine/or.ll +++ b/test/Transforms/InstCombine/or.ll @@ -1,7 +1,8 @@ ; This test makes sure that these instructions are properly eliminated. -; ; RUN: opt < %s -instcombine -S | FileCheck %s +target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128" + define i32 @test1(i32 %A) { %B = or i32 %A, 0 ret i32 %B @@ -253,3 +254,44 @@ define i1 @test25(i32 %A, i32 %B) { ; CHECK-NEXT: %F = and i1 ; CHECK-NEXT: ret i1 %F } + +; PR5634 +define i1 @test26(i32 %A, i32 %B) { + %C1 = icmp eq i32 %A, 0 + %C2 = icmp eq i32 %B, 0 + ; (A == 0) & (A == 0) --> (A|B) == 0 + %D = and i1 %C1, %C2 + ret i1 %D +; CHECK: @test26 +; CHECK: or i32 %A, %B +; CHECK: icmp eq i32 {{.*}}, 0 +; CHECK: ret i1 +} + +; PR5634 +define i1 @test27(i32* %A, i32* %B) { + %C1 = icmp eq i32* %A, null + %C2 = icmp eq i32* %B, null + ; (A == 0) & (A == 0) --> (A|B) == 0 + %D = and i1 %C1, %C2 + ret i1 %D +; CHECK: @test27 +; CHECK: ptrtoint i32* %A +; CHECK: ptrtoint i32* %B +; CHECK: or i32 +; CHECK: icmp eq i32 {{.*}}, 0 +; CHECK: ret i1 +} + +; PR5634 +define i1 @test28(i32 %A, i32 %B) { + %C1 = icmp ne i32 %A, 0 + %C2 = icmp ne i32 %B, 0 + ; (A != 0) | (A != 0) --> (A|B) != 0 + %D = or i1 %C1, %C2 + ret i1 %D +; CHECK: @test28 +; CHECK: or i32 %A, %B +; CHECK: icmp ne i32 {{.*}}, 0 +; CHECK: ret i1 +}