Implement PR5634.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@90046 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-11-29 00:51:17 +00:00
parent d801c10de6
commit 3f40e23392
2 changed files with 97 additions and 11 deletions

View File

@ -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<ConstantPointerNull>(LHS->getOperand(1)) &&
isa<ConstantPointerNull>(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<ConstantPointerNull>(LHS->getOperand(1)) &&
isa<ConstantPointerNull>(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.

View File

@ -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
}