Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 1 | //===- AMDGPUAliasAnalysis ------------------------------------------------===// |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This is the AMGPU address space based alias analysis pass. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 13 | #include "AMDGPUAliasAnalysis.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "AMDGPU.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Triple.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/MemoryLocation.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Argument.h" |
| 20 | #include "llvm/IR/Attributes.h" |
| 21 | #include "llvm/IR/CallingConv.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Function.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 23 | #include "llvm/IR/GlobalVariable.h" |
| 24 | #include "llvm/IR/Type.h" |
| 25 | #include "llvm/IR/Value.h" |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Casting.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
| 29 | #include <cassert> |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
| 33 | #define DEBUG_TYPE "amdgpu-aa" |
| 34 | |
| 35 | // Register this pass... |
| 36 | char AMDGPUAAWrapperPass::ID = 0; |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 37 | char AMDGPUExternalAAWrapper::ID = 0; |
Eugene Zelenko | d16eff8 | 2017-08-08 23:53:55 +0000 | [diff] [blame] | 38 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 39 | INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa", |
| 40 | "AMDGPU Address space based Alias Analysis", false, true) |
| 41 | |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 42 | INITIALIZE_PASS(AMDGPUExternalAAWrapper, "amdgpu-aa-wrapper", |
| 43 | "AMDGPU Address space based Alias Analysis Wrapper", false, true) |
| 44 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 45 | ImmutablePass *llvm::createAMDGPUAAWrapperPass() { |
| 46 | return new AMDGPUAAWrapperPass(); |
| 47 | } |
| 48 | |
Matt Arsenault | 8ba740a | 2018-11-07 20:26:42 +0000 | [diff] [blame] | 49 | ImmutablePass *llvm::createAMDGPUExternalAAWrapperPass() { |
| 50 | return new AMDGPUExternalAAWrapper(); |
| 51 | } |
| 52 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 53 | void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
| 54 | AU.setPreservesAll(); |
| 55 | } |
| 56 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 57 | // These arrays are indexed by address space value enum elements 0 ... to 6 |
| 58 | static const AliasResult ASAliasRules[7][7] = { |
Samuel Pitoiset | 7bd9dcf | 2018-08-22 16:08:48 +0000 | [diff] [blame] | 59 | /* Flat Global Region Group Constant Private Constant 32-bit */ |
| 60 | /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias}, |
| 61 | /* Global */ {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias}, |
| 62 | /* Region */ {MayAlias, NoAlias , NoAlias , NoAlias, MayAlias, NoAlias , MayAlias}, |
| 63 | /* Group */ {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias , NoAlias}, |
| 64 | /* Constant */ {MayAlias, MayAlias, MayAlias, NoAlias , NoAlias, NoAlias , MayAlias}, |
| 65 | /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, NoAlias}, |
| 66 | /* Constant 32-bit */ {MayAlias, MayAlias, MayAlias, NoAlias , MayAlias, NoAlias , NoAlias} |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 67 | }; |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 68 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 69 | static AliasResult getAliasResult(unsigned AS1, unsigned AS2) { |
Samuel Pitoiset | 7bd9dcf | 2018-08-22 16:08:48 +0000 | [diff] [blame] | 70 | static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 6, "Addr space out of range"); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 71 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 72 | if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS) |
| 73 | return MayAlias; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 74 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 75 | return ASAliasRules[AS1][AS2]; |
Yaxun Liu | 1a14bfa | 2017-03-27 14:04:01 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 78 | AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA, |
| 79 | const MemoryLocation &LocB) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 80 | unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace(); |
| 81 | unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace(); |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 82 | |
Matt Arsenault | 796b0e7 | 2018-09-11 04:00:49 +0000 | [diff] [blame] | 83 | AliasResult Result = getAliasResult(asA, asB); |
| 84 | if (Result == NoAlias) |
| 85 | return Result; |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 86 | |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 87 | // Forward the query to the next alias analysis. |
| 88 | return AAResultBase::alias(LocA, LocB); |
| 89 | } |
| 90 | |
| 91 | bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc, |
| 92 | bool OrLocal) { |
| 93 | const Value *Base = GetUnderlyingObject(Loc.Ptr, DL); |
Matt Arsenault | 0da6350 | 2018-08-31 05:49:54 +0000 | [diff] [blame] | 94 | unsigned AS = Base->getType()->getPointerAddressSpace(); |
| 95 | if (AS == AMDGPUAS::CONSTANT_ADDRESS || |
| 96 | AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
| 100 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) { |
| 101 | if (GV->isConstant()) |
| 102 | return true; |
| 103 | } else if (const Argument *Arg = dyn_cast<Argument>(Base)) { |
| 104 | const Function *F = Arg->getParent(); |
| 105 | |
| 106 | // Only assume constant memory for arguments on kernels. |
| 107 | switch (F->getCallingConv()) { |
| 108 | default: |
| 109 | return AAResultBase::pointsToConstantMemory(Loc, OrLocal); |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 110 | case CallingConv::AMDGPU_LS: |
| 111 | case CallingConv::AMDGPU_HS: |
| 112 | case CallingConv::AMDGPU_ES: |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 113 | case CallingConv::AMDGPU_GS: |
Tim Renouf | ef1ae8f | 2017-09-29 09:51:22 +0000 | [diff] [blame] | 114 | case CallingConv::AMDGPU_VS: |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 115 | case CallingConv::AMDGPU_PS: |
| 116 | case CallingConv::AMDGPU_CS: |
| 117 | case CallingConv::AMDGPU_KERNEL: |
| 118 | case CallingConv::SPIR_KERNEL: |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | unsigned ArgNo = Arg->getArgNo(); |
| 123 | /* On an argument, ReadOnly attribute indicates that the function does |
| 124 | not write through this pointer argument, even though it may write |
| 125 | to the memory that the pointer points to. |
| 126 | On an argument, ReadNone attribute indicates that the function does |
| 127 | not dereference that pointer argument, even though it may read or write |
| 128 | the memory that the pointer points to if accessed through other pointers. |
| 129 | */ |
Reid Kleckner | f021fab | 2017-04-13 23:12:13 +0000 | [diff] [blame] | 130 | if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) && |
| 131 | (F->hasParamAttribute(ArgNo, Attribute::ReadNone) || |
| 132 | F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) { |
Stanislav Mekhanoshin | 8e45acf | 2017-03-17 23:56:58 +0000 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | } |
| 136 | return AAResultBase::pointsToConstantMemory(Loc, OrLocal); |
| 137 | } |