Index: lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp =================================================================== --- lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp +++ lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp @@ -134,13 +134,17 @@ // // TODO: Check isTriviallyVectorizable for calls and handle other // instructions. -static bool canVectorizeInst(Instruction *Inst) { +static bool canVectorizeInst(Instruction *Inst, User *User) { switch (Inst->getOpcode()) { case Instruction::Load: - case Instruction::Store: case Instruction::BitCast: case Instruction::AddrSpaceCast: return true; + case Instruction::Store: { + // Must be the stored pointer operand, not a stored value. + StoreInst *SI = cast(Inst); + return SI->getPointerOperand() == User; + } default: return false; } @@ -166,7 +170,7 @@ for (User *AllocaUser : Alloca->users()) { GetElementPtrInst *GEP = dyn_cast(AllocaUser); if (!GEP) { - if (!canVectorizeInst(cast(AllocaUser))) + if (!canVectorizeInst(cast(AllocaUser), Alloca)) return false; WorkList.push_back(AllocaUser); @@ -184,7 +188,7 @@ GEPVectorIdx[GEP] = Index; for (User *GEPUser : AllocaUser->users()) { - if (!canVectorizeInst(cast(GEPUser))) + if (!canVectorizeInst(cast(GEPUser), AllocaUser)) return false; WorkList.push_back(GEPUser); @@ -255,6 +259,12 @@ if (UseInst && UseInst->getOpcode() == Instruction::PtrToInt) return false; + if (StoreInst *SI = dyn_cast_or_null(UseInst)) { + // Reject if the stored value is not the pointer operand. + if (SI->getPointerOperand() != User) + return false; + } + if (!User->getType()->isPointerTy()) continue; Index: test/CodeGen/AMDGPU/promote-alloca-stored-pointer-value.ll =================================================================== --- /dev/null +++ test/CodeGen/AMDGPU/promote-alloca-stored-pointer-value.ll @@ -0,0 +1,45 @@ +; RUN: llc -march=amdgcn < %s | FileCheck -check-prefix=GCN %s + +; Pointer value is stored in a candidate for LDS usage. + +; GCN-LABEL: {{^}}stored_lds_pointer_value: +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD0 +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD1 +; GCN: buffer_load_dwordx2 +; GCN: buffer_store_dwordx2 +define void @stored_lds_pointer_value(half addrspace(1)* addrspace(1)* %capture) #0 { +bb: + %tmp = alloca float addrspace(1)* + %tmp2 = getelementptr inbounds float addrspace(1)*, float addrspace(1)** %tmp, i32 undef + %tmp3 = load float addrspace(1)*, float addrspace(1)** %tmp2 + %tmp4 = getelementptr inbounds float, float addrspace(1)* %tmp3, i64 undef + %tmp5 = bitcast float addrspace(1)* %tmp4 to half addrspace(1)* + store half addrspace(1)* %tmp5, half addrspace(1)* addrspace(1)* %capture + ret void +} + +; Pointer value is stored in a candidate for vector usage +; GCN-LABEL: {{^}}stored_vector_pointer_value: +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD0 +; GCN-DAG: s_mov_b32 s{{[0-9]+}}, SCRATCH_RSRC_DWORD1 +; GCN: buffer_store_dword +; GCN: buffer_store_dword +; GCN: buffer_store_dword +; GCN: buffer_store_dword +define void @stored_vector_pointer_value(i32* addrspace(1)* %out, i32 %index) { +entry: + %tmp0 = alloca [4 x i32] + %x = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 0 + %y = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 1 + %z = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 2 + %w = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 3 + store i32 0, i32* %x + store i32 1, i32* %y + store i32 2, i32* %z + store i32 3, i32* %w + %tmp1 = getelementptr [4 x i32], [4 x i32]* %tmp0, i32 0, i32 %index + store i32* %tmp1, i32* addrspace(1)* %out + ret void +} + +attributes #0 = { nounwind }