--- /dev/null
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -infer-address-spaces -instsimplify %s | FileCheck %s
+
+define i8 @ptrmask_cast_local_to_flat(i8 addrspace(3)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_local_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(3)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast i8 addrspace(3)* %src.ptr to i8*
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load = load i8, i8* %masked
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_private_to_flat(i8 addrspace(5)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_private_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(5)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast i8 addrspace(5)* %src.ptr to i8*
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load = load i8, i8* %masked
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_region_to_flat(i8 addrspace(2)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_region_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(2)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast i8 addrspace(2)* %src.ptr to i8*
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load = load i8, i8* %masked
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_global_to_flat(i8 addrspace(1)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_global_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(1)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast i8 addrspace(1)* %src.ptr to i8*
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load = load i8, i8* %masked
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_999_to_flat(i8 addrspace(999)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_999_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(999)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %cast = addrspacecast i8 addrspace(999)* %src.ptr to i8*
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load = load i8, i8* %masked
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_flat_to_local(i8* %ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_flat_to_local(
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[PTR:%.*]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8* [[MASKED]] to i8 addrspace(3)*
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8 addrspace(3)* [[CAST]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %ptr, i64 %mask)
+ %cast = addrspacecast i8* %masked to i8 addrspace(3)*
+ %load = load i8, i8 addrspace(3)* %cast
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_flat_to_private(i8* %ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_flat_to_private(
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[PTR:%.*]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8* [[MASKED]] to i8 addrspace(5)*
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8 addrspace(5)* [[CAST]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %ptr, i64 %mask)
+ %cast = addrspacecast i8* %masked to i8 addrspace(5)*
+ %load = load i8, i8 addrspace(5)* %cast
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_flat_to_global(i8* %ptr, i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_flat_to_global(
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[PTR:%.*]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8* [[MASKED]] to i8 addrspace(1)*
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8 addrspace(1)* [[CAST]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %ptr, i64 %mask)
+ %cast = addrspacecast i8* %masked to i8 addrspace(1)*
+ %load = load i8, i8 addrspace(1)* %cast
+ ret i8 %load
+}
+
+@lds0 = internal addrspace(3) global i8 123, align 4
+@gv = internal addrspace(1) global i8 123, align 4
+
+define i8 @ptrmask_cast_local_to_flat_global(i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_local_to_flat_global(
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* addrspacecast (i8 addrspace(3)* @lds0 to i8*), i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* addrspacecast (i8 addrspace(3)* @lds0 to i8*), i64 %mask)
+ %load = load i8, i8* %masked, align 1
+ ret i8 %load
+}
+
+define i8 @ptrmask_cast_global_to_flat_global(i64 %mask) {
+; CHECK-LABEL: @ptrmask_cast_global_to_flat_global(
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* addrspacecast (i8 addrspace(1)* @gv to i8*), i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: ret i8 [[LOAD]]
+;
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* addrspacecast (i8 addrspace(1)* @gv to i8*), i64 %mask)
+ %load = load i8, i8* %masked, align 1
+ ret i8 %load
+}
+
+define i8 @multi_ptrmask_cast_global_to_flat(i8 addrspace(1)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @multi_ptrmask_cast_global_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(1)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[LOAD0:%.*]] = load i8, i8 addrspace(1)* [[SRC_PTR]], align 1
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD1:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD0]], [[LOAD1]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %cast = addrspacecast i8 addrspace(1)* %src.ptr to i8*
+ %load0 = load i8, i8* %cast
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load1 = load i8, i8* %masked
+ %add = add i8 %load0, %load1
+ ret i8 %add
+}
+
+; Can't rewrite the ptrmask, but can rewrite other use instructions
+define i8 @multi_ptrmask_cast_local_to_flat(i8 addrspace(3)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @multi_ptrmask_cast_local_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(3)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[LOAD0:%.*]] = load i8, i8 addrspace(3)* [[SRC_PTR]], align 1
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD1:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD0]], [[LOAD1]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %cast = addrspacecast i8 addrspace(3)* %src.ptr to i8*
+ %load0 = load i8, i8* %cast
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load1 = load i8, i8* %masked
+ %add = add i8 %load0, %load1
+ ret i8 %add
+}
+
+define i8 @multi_ptrmask_cast_region_to_flat(i8 addrspace(2)* %src.ptr, i64 %mask) {
+; CHECK-LABEL: @multi_ptrmask_cast_region_to_flat(
+; CHECK-NEXT: [[CAST:%.*]] = addrspacecast i8 addrspace(2)* [[SRC_PTR:%.*]] to i8*
+; CHECK-NEXT: [[LOAD0:%.*]] = load i8, i8 addrspace(2)* [[SRC_PTR]], align 1
+; CHECK-NEXT: [[MASKED:%.*]] = call i8* @llvm.ptrmask.p0i8.i64(i8* [[CAST]], i64 [[MASK:%.*]])
+; CHECK-NEXT: [[LOAD1:%.*]] = load i8, i8* [[MASKED]], align 1
+; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LOAD0]], [[LOAD1]]
+; CHECK-NEXT: ret i8 [[ADD]]
+;
+ %cast = addrspacecast i8 addrspace(2)* %src.ptr to i8*
+ %load0 = load i8, i8* %cast
+ %masked = call i8* @llvm.ptrmask.p0i8.i64(i8* %cast, i64 %mask)
+ %load1 = load i8, i8* %masked
+ %add = add i8 %load0, %load1
+ ret i8 %add
+}
+
+declare i8* @llvm.ptrmask.p0i8.i64(i8*, i64) #0
+declare i8 addrspace(5)* @llvm.ptrmask.p5i8.i32(i8 addrspace(5)*, i32) #0
+declare i8 addrspace(3)* @llvm.ptrmask.p3i8.i32(i8 addrspace(3)*, i32) #0
+declare i8 addrspace(1)* @llvm.ptrmask.p1i8.i64(i8 addrspace(1)*, i64) #0
+
+attributes #0 = { nounwind readnone speculatable willreturn }