Skip to content

Commit bd29197

Browse files
committed
[InstCombine] Fold (ptrtoint (ptrmask p0, m0)) -> (and (ptrtoint p0), m0)
`and` is generally more supported so if we have a `ptrmask` anyways might as well use `and`. Differential Revision: https://reviews.llvm.org/D156640 Closes #67166
1 parent cc83418 commit bd29197

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,15 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
19351935
return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false);
19361936
}
19371937

1938+
// (ptrtoint (ptrmask P, M))
1939+
// -> (and (ptrtoint P), M)
1940+
// This is generally beneficial as `and` is better supported than `ptrmask`.
1941+
Value *Ptr, *Mask;
1942+
if (match(SrcOp, m_OneUse(m_Intrinsic<Intrinsic::ptrmask>(m_Value(Ptr),
1943+
m_Value(Mask)))) &&
1944+
Mask->getType() == Ty)
1945+
return BinaryOperator::CreateAnd(Builder.CreatePtrToInt(Ptr, Ty), Mask);
1946+
19381947
if (auto *GEP = dyn_cast<GetElementPtrInst>(SrcOp)) {
19391948
// Fold ptrtoint(gep null, x) to multiply + constant if the GEP has one use.
19401949
// While this can increase the number of instructions it doesn't actually

llvm/test/Transforms/InstCombine/ptrmask.ll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ define ptr addrspace(1) @ptrmask_combine_improve_alignment_fail(ptr addrspace(1)
154154
define i64 @ptrtoint_of_ptrmask(ptr %p, i64 %m) {
155155
; CHECK-LABEL: define i64 @ptrtoint_of_ptrmask
156156
; CHECK-SAME: (ptr [[P:%.*]], i64 [[M:%.*]]) {
157-
; CHECK-NEXT: [[PM:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 [[M]])
158-
; CHECK-NEXT: [[R:%.*]] = ptrtoint ptr [[PM]] to i64
157+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P]] to i64
158+
; CHECK-NEXT: [[R:%.*]] = and i64 [[TMP1]], [[M]]
159159
; CHECK-NEXT: ret i64 [[R]]
160160
;
161161
%pm = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 %m)
@@ -167,9 +167,9 @@ define i64 @ptrtoint_of_ptrmask(ptr %p, i64 %m) {
167167
define i32 @ptrtoint_of_ptrmask2(ptr %p, i64 %m) {
168168
; CHECK-LABEL: define i32 @ptrtoint_of_ptrmask2
169169
; CHECK-SAME: (ptr [[P:%.*]], i64 [[M:%.*]]) {
170-
; CHECK-NEXT: [[PM:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 [[M]])
171-
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[PM]] to i64
172-
; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP1]] to i32
170+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint ptr [[P]] to i64
171+
; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], [[M]]
172+
; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP2]] to i32
173173
; CHECK-NEXT: ret i32 [[R]]
174174
;
175175
%pm = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 %m)
@@ -180,8 +180,8 @@ define i32 @ptrtoint_of_ptrmask2(ptr %p, i64 %m) {
180180
define <2 x i64> @ptrtoint_of_ptrmask_vec(<2 x ptr> %p, <2 x i64> %m) {
181181
; CHECK-LABEL: define <2 x i64> @ptrtoint_of_ptrmask_vec
182182
; CHECK-SAME: (<2 x ptr> [[P:%.*]], <2 x i64> [[M:%.*]]) {
183-
; CHECK-NEXT: [[PM:%.*]] = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> [[P]], <2 x i64> [[M]])
184-
; CHECK-NEXT: [[R:%.*]] = ptrtoint <2 x ptr> [[PM]] to <2 x i64>
183+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint <2 x ptr> [[P]] to <2 x i64>
184+
; CHECK-NEXT: [[R:%.*]] = and <2 x i64> [[TMP1]], [[M]]
185185
; CHECK-NEXT: ret <2 x i64> [[R]]
186186
;
187187
%pm = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %p, <2 x i64> %m)
@@ -192,9 +192,9 @@ define <2 x i64> @ptrtoint_of_ptrmask_vec(<2 x ptr> %p, <2 x i64> %m) {
192192
define <2 x i32> @ptrtoint_of_ptrmask_vec2(<2 x ptr> %p, <2 x i64> %m) {
193193
; CHECK-LABEL: define <2 x i32> @ptrtoint_of_ptrmask_vec2
194194
; CHECK-SAME: (<2 x ptr> [[P:%.*]], <2 x i64> [[M:%.*]]) {
195-
; CHECK-NEXT: [[PM:%.*]] = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> [[P]], <2 x i64> [[M]])
196-
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint <2 x ptr> [[PM]] to <2 x i64>
197-
; CHECK-NEXT: [[R:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32>
195+
; CHECK-NEXT: [[TMP1:%.*]] = ptrtoint <2 x ptr> [[P]] to <2 x i64>
196+
; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i64> [[TMP1]], [[M]]
197+
; CHECK-NEXT: [[R:%.*]] = trunc <2 x i64> [[TMP2]] to <2 x i32>
198198
; CHECK-NEXT: ret <2 x i32> [[R]]
199199
;
200200
%pm = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %p, <2 x i64> %m)

0 commit comments

Comments
 (0)