Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,8 @@ Constant *llvm::ConstantFoldCompareInstOperands(

// Only do this transformation if the int is intptrty in size, otherwise
// there is a truncation or extension that we aren't modeling.
if (CE0->getOpcode() == Instruction::PtrToInt) {
if (CE0->getOpcode() == Instruction::PtrToInt ||
CE0->getOpcode() == Instruction::PtrToAddr) {
Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we seem to be aligned on using address width only for icmp ptr, I believe all of these DL.getIntPtrType() should be DL.getAddressType()? This change probably won't be caught by existing tests since we won't have many for index size != pointer size.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is under the assumption that icmp compares all bits. I felt that there wasn't sufficient consensus for the address-only interpretation yet. I'll put this into draft for now and revisit this once we have more clarity.

if (CE0->getType() == IntPtrTy) {
Constant *C = CE0->getOperand(0);
Expand All @@ -1252,7 +1253,8 @@ Constant *llvm::ConstantFoldCompareInstOperands(

// Only do this transformation if the int is intptrty in size, otherwise
// there is a truncation or extension that we aren't modeling.
if (CE0->getOpcode() == Instruction::PtrToInt) {
if (CE0->getOpcode() == Instruction::PtrToInt ||
CE0->getOpcode() == Instruction::PtrToAddr) {
Type *IntPtrTy = DL.getIntPtrType(CE0->getOperand(0)->getType());
if (CE0->getType() == IntPtrTy &&
CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType()) {
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/Transforms/InstCombine/ptrtoaddr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,39 @@ define i32 @ptrtoaddr_sub_consts_offset_addrsize() {
;
ret i32 sub (i32 ptrtoaddr (ptr addrspace(1) getelementptr (i8, ptr addrspace(1) @g.as1, i32 42) to i32), i32 ptrtoaddr (ptr addrspace(1) @g.as1 to i32))
}

define i1 @icmp_ptrtoaddr_0() {
; CHECK-LABEL: define i1 @icmp_ptrtoaddr_0() {
; CHECK-NEXT: ret i1 true
;
%cmp = icmp ne i64 ptrtoaddr (ptr @g to i64), 0
ret i1 %cmp
}

; FIXME: This can still fold, but not by converting to pointer comparison.
define i1 @icmp_ptrtoaddr_0_addrsize() {
; CHECK-LABEL: define i1 @icmp_ptrtoaddr_0_addrsize() {
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 ptrtoaddr (ptr addrspace(1) @g.as1 to i32), 0
; CHECK-NEXT: ret i1 [[CMP]]
;
%cmp = icmp ne i32 ptrtoaddr (ptr addrspace(1) @g.as1 to i32), 0
ret i1 %cmp
}

define i1 @icmp_ptrtoaddr_ptrtoaddr() {
; CHECK-LABEL: define i1 @icmp_ptrtoaddr_ptrtoaddr() {
; CHECK-NEXT: ret i1 true
;
%cmp = icmp ne i64 ptrtoaddr (ptr @g to i64), ptrtoaddr (ptr @g2 to i64)
ret i1 %cmp
}

; FIXME: This can still fold, but not by converting to pointer comparison.
define i1 @icmp_ptrtoaddr_ptrtoaddr_addrsize() {
; CHECK-LABEL: define i1 @icmp_ptrtoaddr_ptrtoaddr_addrsize() {
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 ptrtoaddr (ptr addrspace(1) @g.as1 to i32), ptrtoaddr (ptr addrspace(1) @g2.as1 to i32)
; CHECK-NEXT: ret i1 [[CMP]]
;
%cmp = icmp ne i32 ptrtoaddr (ptr addrspace(1) @g.as1 to i32), ptrtoaddr (ptr addrspace(1) @g2.as1 to i32)
ret i1 %cmp
}