[llvm] 912c09e - [InstCombine] eliminate a pointer cast around insertelement
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 12 06:10:13 PDT 2020
Author: Sanjay Patel
Date: 2020-08-12T09:08:17-04:00
New Revision: 912c09e845cb1907bc44664495fc69925a1bd2a9
URL: https://github.com/llvm/llvm-project/commit/912c09e845cb1907bc44664495fc69925a1bd2a9
DIFF: https://github.com/llvm/llvm-project/commit/912c09e845cb1907bc44664495fc69925a1bd2a9.diff
LOG: [InstCombine] eliminate a pointer cast around insertelement
I'm not sure if this solves PR46839 completely, but reducing the casting should help:
https://bugs.llvm.org/show_bug.cgi?id=46839
Differential Revision: https://reviews.llvm.org/D85647
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/cast_ptr.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index d6d63a59754b..b9bcd39a83e7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1935,7 +1935,9 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
Value *SrcOp = CI.getPointerOperand();
Type *Ty = CI.getType();
unsigned AS = CI.getPointerAddressSpace();
- if (Ty->getScalarSizeInBits() != DL.getPointerSizeInBits(AS)) {
+ unsigned TySize = Ty->getScalarSizeInBits();
+ unsigned PtrSize = DL.getPointerSizeInBits(AS);
+ if (TySize != PtrSize) {
Type *IntPtrTy = DL.getIntPtrType(CI.getContext(), AS);
if (auto *VecTy = dyn_cast<VectorType>(Ty)) {
// Handle vectors of pointers.
@@ -1947,6 +1949,17 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
}
+ Value *Vec, *Scalar, *Index;
+ if (match(SrcOp, m_OneUse(m_InsertElt(m_IntToPtr(m_Value(Vec)),
+ m_Value(Scalar), m_Value(Index)))) &&
+ Vec->getType() == Ty) {
+ assert(Vec->getType()->getScalarSizeInBits() == PtrSize && "Wrong type");
+ // Convert the scalar to int followed by insert to eliminate one cast:
+ // p2i (ins (i2p Vec), Scalar, Index --> ins Vec, (p2i Scalar), Index
+ Value *NewCast = Builder.CreatePtrToInt(Scalar, Ty->getScalarType());
+ return InsertElementInst::Create(Vec, NewCast, Index);
+ }
+
return commonPointerCastTransforms(CI);
}
diff --git a/llvm/test/Transforms/InstCombine/cast_ptr.ll b/llvm/test/Transforms/InstCombine/cast_ptr.ll
index 3ac9acd0322b..831d1b4de2f5 100644
--- a/llvm/test/Transforms/InstCombine/cast_ptr.ll
+++ b/llvm/test/Transforms/InstCombine/cast_ptr.ll
@@ -164,9 +164,8 @@ entry:
define <2 x i32> @insertelt(<2 x i32> %x, i32* %p, i133 %index) {
; CHECK-LABEL: @insertelt(
-; CHECK-NEXT: [[V:%.*]] = inttoptr <2 x i32> [[X:%.*]] to <2 x i32*>
-; CHECK-NEXT: [[I:%.*]] = insertelement <2 x i32*> [[V]], i32* [[P:%.*]], i133 [[INDEX:%.*]]
-; CHECK-NEXT: [[R:%.*]] = ptrtoint <2 x i32*> [[I]] to <2 x i32>
+; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint i32* [[P:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = insertelement <2 x i32> [[X:%.*]], i32 [[TMP1]], i133 [[INDEX:%.*]]
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%v = inttoptr <2 x i32> %x to <2 x i32*>
@@ -178,9 +177,8 @@ define <2 x i32> @insertelt(<2 x i32> %x, i32* %p, i133 %index) {
define <2 x i32> @insertelt_intptr_trunc(<2 x i64> %x, i32* %p) {
; CHECK-LABEL: @insertelt_intptr_trunc(
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i64> [[X:%.*]] to <2 x i32>
-; CHECK-NEXT: [[V:%.*]] = inttoptr <2 x i32> [[TMP1]] to <2 x i32*>
-; CHECK-NEXT: [[I:%.*]] = insertelement <2 x i32*> [[V]], i32* [[P:%.*]], i32 0
-; CHECK-NEXT: [[R:%.*]] = ptrtoint <2 x i32*> [[I]] to <2 x i32>
+; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint i32* [[P:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = insertelement <2 x i32> [[TMP1]], i32 [[TMP2]], i32 0
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%v = inttoptr <2 x i64> %x to <2 x i32*>
@@ -192,9 +190,8 @@ define <2 x i32> @insertelt_intptr_trunc(<2 x i64> %x, i32* %p) {
define <2 x i32> @insertelt_intptr_zext(<2 x i8> %x, i32* %p) {
; CHECK-LABEL: @insertelt_intptr_zext(
; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>
-; CHECK-NEXT: [[V:%.*]] = inttoptr <2 x i32> [[TMP1]] to <2 x i32*>
-; CHECK-NEXT: [[I:%.*]] = insertelement <2 x i32*> [[V]], i32* [[P:%.*]], i32 1
-; CHECK-NEXT: [[R:%.*]] = ptrtoint <2 x i32*> [[I]] to <2 x i32>
+; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint i32* [[P:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = insertelement <2 x i32> [[TMP1]], i32 [[TMP2]], i32 1
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%v = inttoptr <2 x i8> %x to <2 x i32*>
@@ -206,10 +203,9 @@ define <2 x i32> @insertelt_intptr_zext(<2 x i8> %x, i32* %p) {
define <2 x i64> @insertelt_intptr_zext_zext(<2 x i8> %x, i32* %p) {
; CHECK-LABEL: @insertelt_intptr_zext_zext(
; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i8> [[X:%.*]] to <2 x i32>
-; CHECK-NEXT: [[V:%.*]] = inttoptr <2 x i32> [[TMP1]] to <2 x i32*>
-; CHECK-NEXT: [[I:%.*]] = insertelement <2 x i32*> [[V]], i32* [[P:%.*]], i32 0
-; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint <2 x i32*> [[I]] to <2 x i32>
-; CHECK-NEXT: [[R:%.*]] = zext <2 x i32> [[TMP2]] to <2 x i64>
+; CHECK-NEXT: [[TMP2:%.*]] = ptrtoint i32* [[P:%.*]] to i32
+; CHECK-NEXT: [[TMP3:%.*]] = insertelement <2 x i32> [[TMP1]], i32 [[TMP2]], i32 0
+; CHECK-NEXT: [[R:%.*]] = zext <2 x i32> [[TMP3]] to <2 x i64>
; CHECK-NEXT: ret <2 x i64> [[R]]
;
%v = inttoptr <2 x i8> %x to <2 x i32*>
@@ -224,8 +220,8 @@ define <2 x i32> @insertelt_extra_use1(<2 x i32> %x, i32* %p) {
; CHECK-LABEL: @insertelt_extra_use1(
; CHECK-NEXT: [[V:%.*]] = inttoptr <2 x i32> [[X:%.*]] to <2 x i32*>
; CHECK-NEXT: call void @use(<2 x i32*> [[V]])
-; CHECK-NEXT: [[I:%.*]] = insertelement <2 x i32*> [[V]], i32* [[P:%.*]], i32 0
-; CHECK-NEXT: [[R:%.*]] = ptrtoint <2 x i32*> [[I]] to <2 x i32>
+; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint i32* [[P:%.*]] to i32
+; CHECK-NEXT: [[R:%.*]] = insertelement <2 x i32> [[X]], i32 [[TMP1]], i32 0
; CHECK-NEXT: ret <2 x i32> [[R]]
;
%v = inttoptr <2 x i32> %x to <2 x i32*>
More information about the llvm-commits
mailing list