[llvm] d08e3ae - [InstCombine] Support vectors in simplifyIntToPtrRoundTripCast.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 12:06:33 PST 2022


Author: Craig Topper
Date: 2022-12-13T12:02:11-08:00
New Revision: d08e3ae271a9748af6c99be7e455730fb110b0a5

URL: https://github.com/llvm/llvm-project/commit/d08e3ae271a9748af6c99be7e455730fb110b0a5
DIFF: https://github.com/llvm/llvm-project/commit/d08e3ae271a9748af6c99be7e455730fb110b0a5.diff

LOG: [InstCombine] Support vectors in simplifyIntToPtrRoundTripCast.

This code compares getPointerTypeSizeInBits and getTypeSizeInBits.
getPointerTypeSizeInBits contains a call to getScalarType while
getTypeSizeInBits does not. This makes the code incorrect for vectors.

For scalable vectors this caused a warning about a scalable TypeSize
being converted to unsigned.

Switch to DL.getTypeSizeInBits for the pointers too. This should
work since inttoptr/ptrtoint can't change the number of elements.
This was suggested by @nikic in D139911.

Fixes PR59480.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D139962

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 2e41886359dab..38a79f055e9ed 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -370,14 +370,14 @@ static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1,
 // inttoptr ( ptrtoint (x) ) --> x
 Value *InstCombinerImpl::simplifyIntToPtrRoundTripCast(Value *Val) {
   auto *IntToPtr = dyn_cast<IntToPtrInst>(Val);
-  if (IntToPtr && DL.getPointerTypeSizeInBits(IntToPtr->getDestTy()) ==
+  if (IntToPtr && DL.getTypeSizeInBits(IntToPtr->getDestTy()) ==
                       DL.getTypeSizeInBits(IntToPtr->getSrcTy())) {
     auto *PtrToInt = dyn_cast<PtrToIntInst>(IntToPtr->getOperand(0));
     Type *CastTy = IntToPtr->getDestTy();
     if (PtrToInt &&
         CastTy->getPointerAddressSpace() ==
             PtrToInt->getSrcTy()->getPointerAddressSpace() &&
-        DL.getPointerTypeSizeInBits(PtrToInt->getSrcTy()) ==
+        DL.getTypeSizeInBits(PtrToInt->getSrcTy()) ==
             DL.getTypeSizeInBits(PtrToInt->getDestTy())) {
       return CastInst::CreateBitOrPointerCast(PtrToInt->getOperand(0), CastTy,
                                               "", PtrToInt);

diff  --git a/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll b/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll
index a73dc419d7e6b..5249aa4269e87 100644
--- a/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll
+++ b/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll
@@ -17,6 +17,28 @@ define i1 @func(ptr %X, ptr %Y) {
   ret i1 %cmp
 }
 
+define <2 x i1> @func_vec(<2 x ptr> %X, <2 x ptr> %Y) {
+; CHECK-LABEL: @func_vec(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x ptr> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret <2 x i1> [[CMP]]
+;
+  %i = ptrtoint <2 x ptr> %X to <2 x i64>
+  %p = inttoptr <2 x i64> %i to <2 x ptr>
+  %cmp = icmp eq <2 x ptr> %p, %Y
+  ret <2 x i1> %cmp
+}
+
+define <vscale x 2 x i1> @func_svec(<vscale x 2 x ptr> %X, <vscale x 2 x ptr> %Y) {
+; CHECK-LABEL: @func_svec(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <vscale x 2 x ptr> [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret <vscale x 2 x i1> [[CMP]]
+;
+  %i = ptrtoint <vscale x 2 x ptr> %X to <vscale x 2 x i64>
+  %p = inttoptr <vscale x 2 x i64> %i to <vscale x 2 x ptr>
+  %cmp = icmp eq <vscale x 2 x ptr> %p, %Y
+  ret <vscale x 2 x i1> %cmp
+}
+
 define i1 @func_pointer_
diff erent_types(ptr %X, ptr %Y) {
 ; CHECK-LABEL: @func_pointer_
diff erent_types(
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr [[X:%.*]], [[Y:%.*]]


        


More information about the llvm-commits mailing list