Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
[InstSimplify] Try hard to simplify pointer comparisons
Browse files Browse the repository at this point in the history
Simplify ptrtoint comparisons involving operands with different source
types.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277951 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
majnemer committed Aug 7, 2016
1 parent 0c8a344 commit 2a07d9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3094,6 +3094,16 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
if (LHS->getType()->isPointerTy())
if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, LHS, RHS))
return C;
if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
Q.DL.getTypeSizeInBits(CLHS->getType()) &&
Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
Q.DL.getTypeSizeInBits(CRHS->getType()))
if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI,
CLHS->getPointerOperand(),
CRHS->getPointerOperand()))
return C;

if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) {
if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
Expand Down
13 changes: 13 additions & 0 deletions test/Transforms/InstSimplify/compare.ll
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ define i1 @gep16(i8* %ptr, i32 %a) {
; CHECK-NEXT: ret i1 false
}

define i1 @gep17() {
; CHECK-LABEL: @gep17(
%alloca = alloca i32, align 4
%bc = bitcast i32* %alloca to [4 x i8]*
%gep1 = getelementptr inbounds i32, i32* %alloca, i32 1
%pti1 = ptrtoint i32* %gep1 to i32
%gep2 = getelementptr inbounds [4 x i8], [4 x i8]* %bc, i32 0, i32 1
%pti2 = ptrtoint i8* %gep2 to i32
%cmp = icmp ugt i32 %pti1, %pti2
ret i1 %cmp
; CHECK-NEXT: ret i1 true
}

define i1 @zext(i32 %x) {
; CHECK-LABEL: @zext(
%e1 = zext i32 %x to i64
Expand Down

0 comments on commit 2a07d9f

Please sign in to comment.