Skip to content
Open
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
16 changes: 10 additions & 6 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2965,19 +2965,23 @@ unsigned CastInst::isEliminableCastPair(Instruction::CastOps firstOp,
// zext, sext -> zext, because sext can't sign extend after zext
return Instruction::ZExt;
case 11: {
// inttoptr, ptrtoint/ptrtoaddr -> bitcast if SrcSize<=PtrSize/AddrSize
// and SrcSize==DstSize
// inttoptr, ptrtoint/ptrtoaddr -> integer cast
if (!DL)
return 0;
unsigned MidSize = secondOp == Instruction::PtrToAddr
? DL->getAddressSizeInBits(MidTy)
: DL->getPointerTypeSizeInBits(MidTy);
unsigned SrcSize = SrcTy->getScalarSizeInBits();
unsigned DstSize = DstTy->getScalarSizeInBits();
// TODO: Could also produce zext or trunc here.
if (SrcSize <= MidSize && SrcSize == DstSize)
return Instruction::BitCast;
return 0;
// If the middle size is smaller than both source and destination,
// an additional masking operation would be required.
if (MidSize < SrcSize && MidSize < DstSize)
return 0;
if (DstSize < SrcSize)
return Instruction::Trunc;
if (DstSize > SrcSize)
return Instruction::ZExt;
return Instruction::BitCast;
}
case 12:
// addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
Expand Down
5 changes: 1 addition & 4 deletions llvm/test/Transforms/InstCombine/ptrtoaddr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ define i64 @ptrtoaddr_inttoptr_arg(i64 %a) {
define i32 @ptrtoaddr_inttoptr_arg_addrsize(i32 %a) {
; CHECK-LABEL: define i32 @ptrtoaddr_inttoptr_arg_addrsize(
; CHECK-SAME: i32 [[A:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = zext i32 [[A]] to i64
; CHECK-NEXT: [[TOPTR:%.*]] = inttoptr i64 [[TMP1]] to ptr addrspace(1)
; CHECK-NEXT: [[TOADDR:%.*]] = ptrtoaddr ptr addrspace(1) [[TOPTR]] to i32
; CHECK-NEXT: ret i32 [[TOADDR]]
; CHECK-NEXT: ret i32 [[A]]
;
%toptr = inttoptr i32 %a to ptr addrspace(1)
%toaddr = ptrtoaddr ptr addrspace(1) %toptr to i32
Expand Down
24 changes: 24 additions & 0 deletions llvm/unittests/IR/InstructionsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,30 @@ TEST(InstructionsTest, isEliminableCastPair) {
Int64Ty, &DL1),
0U);

// Destination larger than source. Pointer type same as destination.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::PtrToInt, Int16Ty, PtrTy64,
Int64Ty, &DL1),
CastInst::ZExt);

// Destination larger than source. Pointer type different from destination.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::PtrToInt, Int16Ty, PtrTy32,
Int64Ty, &DL1),
CastInst::ZExt);

// Destination smaller than source. Pointer type same as source.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
CastInst::PtrToInt, Int64Ty, PtrTy64,
Int16Ty, &DL1),
CastInst::Trunc);

// Destination smaller than source. Pointer type different from source.
EXPECT_EQ(CastInst::isEliminableCastPair(CastInst::IntToPtr,
Copy link
Member

Choose a reason for hiding this comment

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

Could you also add a ptrtoaddr test here with e.g. p2:64:64:64:32 data layout? Showing it only works for the address bits?

CastInst::PtrToInt, Int64Ty, PtrTy32,
Int16Ty, &DL1),
CastInst::Trunc);

// Test that we don't eliminate bitcasts between different address spaces,
// or if we don't have available pointer size information.
DataLayout DL2("e-p:32:32:32-p1:16:16:16-p2:64:64:64-i1:8:8-i8:8:8-i16:16:16"
Expand Down