Index: llvm/include/llvm/Analysis/TargetTransformInfo.h =================================================================== --- llvm/include/llvm/Analysis/TargetTransformInfo.h +++ llvm/include/llvm/Analysis/TargetTransformInfo.h @@ -419,6 +419,9 @@ /// ToAS is valid. bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const; + /// Return false if a \p AS0 address cannot possibly alias a \p AS1 address. + bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const; + /// Returns the address space ID for a target's 'flat' address space. Note /// this is not necessarily the same as addrspace(0), which LLVM sometimes /// refers to as the generic address space. The flat address space is a @@ -1690,6 +1693,7 @@ virtual bool isSourceOfDivergence(const Value *V) = 0; virtual bool isAlwaysUniform(const Value *V) = 0; virtual bool isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const = 0; + virtual bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const = 0; virtual unsigned getFlatAddressSpace() = 0; virtual bool collectFlatAddressOperands(SmallVectorImpl &OpIndexes, Intrinsic::ID IID) const = 0; @@ -2075,6 +2079,10 @@ return Impl.isValidAddrSpaceCast(FromAS, ToAS); } + bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const override { + return Impl.addrspacesMayAlias(AS0, AS1); + } + unsigned getFlatAddressSpace() override { return Impl.getFlatAddressSpace(); } bool collectFlatAddressOperands(SmallVectorImpl &OpIndexes, Index: llvm/include/llvm/Analysis/TargetTransformInfoImpl.h =================================================================== --- llvm/include/llvm/Analysis/TargetTransformInfoImpl.h +++ llvm/include/llvm/Analysis/TargetTransformInfoImpl.h @@ -101,6 +101,10 @@ return false; } + bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const { + return true; + } + unsigned getFlatAddressSpace() const { return -1; } bool collectFlatAddressOperands(SmallVectorImpl &OpIndexes, Index: llvm/include/llvm/CodeGen/BasicTTIImpl.h =================================================================== --- llvm/include/llvm/CodeGen/BasicTTIImpl.h +++ llvm/include/llvm/CodeGen/BasicTTIImpl.h @@ -286,6 +286,10 @@ return false; } + bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const { + return true; + } + unsigned getFlatAddressSpace() { // Return an invalid address space. return -1; Index: llvm/lib/Analysis/TargetTransformInfo.cpp =================================================================== --- llvm/lib/Analysis/TargetTransformInfo.cpp +++ llvm/lib/Analysis/TargetTransformInfo.cpp @@ -276,6 +276,11 @@ return TTIImpl->isValidAddrSpaceCast(FromAS, ToAS); } +bool llvm::TargetTransformInfo::addrspacesMayAlias(unsigned FromAS, + unsigned ToAS) const { + return TTIImpl->addrspacesMayAlias(FromAS, ToAS); +} + unsigned TargetTransformInfo::getFlatAddressSpace() const { return TTIImpl->getFlatAddressSpace(); } Index: llvm/lib/Target/AMDGPU/AMDGPU.h =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPU.h +++ llvm/lib/Target/AMDGPU/AMDGPU.h @@ -451,6 +451,36 @@ AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT || AS > AMDGPUAS::MAX_AMDGPU_ADDRESS; } + +static inline bool addrspacesMayAlias(unsigned AS1, unsigned AS2) { + static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 8, "Addr space out of range"); + + if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS) + return true; + +#define ASMay true +#define ASNo false + // This array is indexed by address space value enum elements 0 ... to 8 + // clang-format off + static const bool ASAliasRules[9][9] = { + /* Flat Global Region Group Constant Private Const32 BufFatPtr BufRsrc */ + /* Flat */ {ASMay, ASMay, ASNo, ASMay, ASMay, ASMay, ASMay, ASMay, ASMay}, + /* Global */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay, ASMay}, + /* Region */ {ASNo, ASNo, ASMay, ASNo, ASNo, ASNo, ASNo, ASNo, ASNo}, + /* Group */ {ASMay, ASNo, ASNo, ASMay, ASNo, ASNo, ASNo, ASNo, ASNo}, + /* Constant */ {ASMay, ASMay, ASNo, ASNo, ASNo, ASNo, ASMay, ASMay, ASMay}, + /* Private */ {ASMay, ASNo, ASNo, ASNo, ASNo, ASMay, ASNo, ASNo, ASNo}, + /* Constant 32-bit */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASNo, ASMay, ASMay}, + /* Buffer Fat Ptr */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay, ASMay}, + /* Buffer Resource */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay, ASMay}, + }; + // clang-format on +#undef ASMay +#undef ASNo + + return ASAliasRules[AS1][AS2]; +} + } } // End namespace llvm Index: llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp +++ llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp @@ -46,44 +46,14 @@ AU.setPreservesAll(); } -static AliasResult getAliasResult(unsigned AS1, unsigned AS2) { - static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 8, "Addr space out of range"); - - if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS) - return AliasResult::MayAlias; - -#define ASMay AliasResult::MayAlias -#define ASNo AliasResult::NoAlias - // This array is indexed by address space value enum elements 0 ... to 8 - // clang-format off - static const AliasResult ASAliasRules[9][9] = { - /* Flat Global Region Group Constant Private Const32 BufFatPtr BufRsrc */ - /* Flat */ {ASMay, ASMay, ASNo, ASMay, ASMay, ASMay, ASMay, ASMay, ASMay}, - /* Global */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay, ASMay}, - /* Region */ {ASNo, ASNo, ASMay, ASNo, ASNo, ASNo, ASNo, ASNo, ASNo}, - /* Group */ {ASMay, ASNo, ASNo, ASMay, ASNo, ASNo, ASNo, ASNo, ASNo}, - /* Constant */ {ASMay, ASMay, ASNo, ASNo, ASNo, ASNo, ASMay, ASMay, ASMay}, - /* Private */ {ASMay, ASNo, ASNo, ASNo, ASNo, ASMay, ASNo, ASNo, ASNo}, - /* Constant 32-bit */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASNo, ASMay, ASMay}, - /* Buffer Fat Ptr */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay, ASMay}, - /* Buffer Resource */ {ASMay, ASMay, ASNo, ASNo, ASMay, ASNo, ASMay, ASMay, ASMay}, - }; - // clang-format on -#undef ASMay -#undef ASNo - - return ASAliasRules[AS1][AS2]; -} - AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA, const MemoryLocation &LocB, AAQueryInfo &AAQI, const Instruction *) { unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace(); unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace(); - AliasResult Result = getAliasResult(asA, asB); - if (Result == AliasResult::NoAlias) - return Result; + if (!AMDGPU::addrspacesMayAlias(asA, asB)) + return AliasResult::NoAlias; // In general, FLAT (generic) pointers could be aliased to LOCAL or PRIVATE // pointers. However, as LOCAL or PRIVATE pointers point to local objects, in Index: llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h =================================================================== --- llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h +++ llvm/lib/Target/AMDGPU/AMDGPUTargetTransformInfo.h @@ -194,6 +194,10 @@ return false; } + bool addrspacesMayAlias(unsigned AS0, unsigned AS1) const { + return AMDGPU::addrspacesMayAlias(AS0, AS1); + } + unsigned getFlatAddressSpace() const { // Don't bother running InferAddressSpaces pass on graphics shaders which // don't use flat addressing.