Skip to content

Commit cc83418

Browse files
committed
[InstCombine] Preserve return attributes when merging llvm.ptrmask
If we have assosiated attributes i.e `([ret_attrs] (ptrmask (ptrmask p0, m0), m1))` we should preserve `[ret_attrs]` when combining the two `llvm.ptrmask`s. Differential Revision: https://reviews.llvm.org/D156638
1 parent 51abbf9 commit cc83418

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,17 +1968,23 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
19681968
return II;
19691969

19701970
Value *InnerPtr, *InnerMask;
1971+
bool Changed = false;
1972+
// Combine:
1973+
// (ptrmask (ptrmask p, A), B)
1974+
// -> (ptrmask p, (and A, B))
19711975
if (match(II->getArgOperand(0),
19721976
m_OneUse(m_Intrinsic<Intrinsic::ptrmask>(m_Value(InnerPtr),
19731977
m_Value(InnerMask))))) {
19741978
assert(II->getArgOperand(1)->getType() == InnerMask->getType() &&
19751979
"Mask types must match");
1980+
// TODO: If InnerMask == Op1, we could copy attributes from inner
1981+
// callsite -> outer callsite.
19761982
Value *NewMask = Builder.CreateAnd(II->getArgOperand(1), InnerMask);
1977-
return replaceInstUsesWith(
1978-
*II, Builder.CreateIntrinsic(InnerPtr->getType(), Intrinsic::ptrmask,
1979-
{InnerPtr, NewMask}));
1983+
replaceOperand(CI, 0, InnerPtr);
1984+
replaceOperand(CI, 1, NewMask);
1985+
Changed = true;
19801986
}
1981-
bool Changed = false;
1987+
19821988
// See if we can deduce non-null.
19831989
if (!CI.hasRetAttr(Attribute::NonNull) &&
19841990
(Known.isNonZero() ||

llvm/test/Transforms/InstCombine/ptrmask.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ define ptr @ptrmask_combine_consecutive_preserve_attrs(ptr %p0, i64 %m1) {
1212
; CHECK-LABEL: define ptr @ptrmask_combine_consecutive_preserve_attrs
1313
; CHECK-SAME: (ptr [[P0:%.*]], i64 [[M1:%.*]]) {
1414
; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[M1]], 224
15-
; CHECK-NEXT: [[R:%.*]] = call align 32 ptr @llvm.ptrmask.p0.i64(ptr [[P0]], i64 [[TMP1]])
15+
; CHECK-NEXT: [[R:%.*]] = call noalias align 32 ptr @llvm.ptrmask.p0.i64(ptr [[P0]], i64 [[TMP1]])
1616
; CHECK-NEXT: ret ptr [[R]]
1717
;
1818
%pm0 = call ptr @llvm.ptrmask.p0.i64(ptr %p0, i64 224)
@@ -24,7 +24,7 @@ define <2 x ptr> @ptrmask_combine_consecutive_preserve_attrs_vecs(<2 x ptr> %p0,
2424
; CHECK-LABEL: define <2 x ptr> @ptrmask_combine_consecutive_preserve_attrs_vecs
2525
; CHECK-SAME: (<2 x ptr> [[P0:%.*]], <2 x i64> [[M1:%.*]]) {
2626
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i64> [[M1]], <i64 12345, i64 12345>
27-
; CHECK-NEXT: [[R:%.*]] = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> [[P0]], <2 x i64> [[TMP1]])
27+
; CHECK-NEXT: [[R:%.*]] = call align 128 <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> [[P0]], <2 x i64> [[TMP1]])
2828
; CHECK-NEXT: ret <2 x ptr> [[R]]
2929
;
3030
%pm0 = call <2 x ptr> @llvm.ptrmask.v2p0.v2i64(<2 x ptr> %p0, <2 x i64> <i64 12345, i64 12345>)

0 commit comments

Comments
 (0)