Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit d31c987

Browse files
majnemereddyb
authored andcommitted
[InstSimplify] Try hard to simplify pointer comparisons
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
1 parent ae32815 commit d31c987

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/Analysis/InstructionSimplify.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,6 +3092,16 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
30923092
if (LHS->getType()->isPointerTy())
30933093
if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI, LHS, RHS))
30943094
return C;
3095+
if (auto *CLHS = dyn_cast<PtrToIntOperator>(LHS))
3096+
if (auto *CRHS = dyn_cast<PtrToIntOperator>(RHS))
3097+
if (Q.DL.getTypeSizeInBits(CLHS->getPointerOperandType()) ==
3098+
Q.DL.getTypeSizeInBits(CLHS->getType()) &&
3099+
Q.DL.getTypeSizeInBits(CRHS->getPointerOperandType()) ==
3100+
Q.DL.getTypeSizeInBits(CRHS->getType()))
3101+
if (auto *C = computePointerICmp(Q.DL, Q.TLI, Q.DT, Pred, Q.CxtI,
3102+
CLHS->getPointerOperand(),
3103+
CRHS->getPointerOperand()))
3104+
return C;
30953105

30963106
if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) {
30973107
if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {

test/Transforms/InstSimplify/compare.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ define i1 @gep16(i8* %ptr, i32 %a) {
205205
; CHECK-NEXT: ret i1 false
206206
}
207207

208+
define i1 @gep17() {
209+
; CHECK-LABEL: @gep17(
210+
%alloca = alloca i32, align 4
211+
%bc = bitcast i32* %alloca to [4 x i8]*
212+
%gep1 = getelementptr inbounds i32, i32* %alloca, i32 1
213+
%pti1 = ptrtoint i32* %gep1 to i32
214+
%gep2 = getelementptr inbounds [4 x i8], [4 x i8]* %bc, i32 0, i32 1
215+
%pti2 = ptrtoint i8* %gep2 to i32
216+
%cmp = icmp ugt i32 %pti1, %pti2
217+
ret i1 %cmp
218+
; CHECK-NEXT: ret i1 true
219+
}
220+
208221
define i1 @zext(i32 %x) {
209222
; CHECK-LABEL: @zext(
210223
%e1 = zext i32 %x to i64

0 commit comments

Comments
 (0)