blob: 3c99f48e818ae449d43f085c359e694bb93ef37f [file] [log] [blame]
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +00001//===- AMDGPUAliasAnalysis ---------------------------------------*- C++ -*-==//
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
13#include "AMDGPU.h"
14#include "AMDGPUAliasAnalysis.h"
15#include "llvm/Analysis/AliasAnalysis.h"
16#include "llvm/Analysis/ValueTracking.h"
17#include "llvm/Analysis/Passes.h"
18#include "llvm/Support/raw_ostream.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Pass.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "amdgpu-aa"
26
27// Register this pass...
28char AMDGPUAAWrapperPass::ID = 0;
29INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa",
30 "AMDGPU Address space based Alias Analysis", false, true)
31
32ImmutablePass *llvm::createAMDGPUAAWrapperPass() {
33 return new AMDGPUAAWrapperPass();
34}
35
36void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
37 AU.setPreservesAll();
38}
39
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000040// Must match the table in getAliasResult.
Jan Vesely3c994412017-03-31 19:26:23 +000041AMDGPUAAResult::ASAliasRulesTy::ASAliasRulesTy(AMDGPUAS AS_, Triple::ArchType Arch_)
42 : Arch(Arch_), AS(AS_) {
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000043 // These arrarys are indexed by address space value
44 // enum elements 0 ... to 5
45 static const AliasResult ASAliasRulesPrivIsZero[6][6] = {
46 /* Private Global Constant Group Flat Region*/
47 /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , MayAlias, NoAlias},
48 /* Global */ {NoAlias , MayAlias, NoAlias , NoAlias , MayAlias, NoAlias},
49 /* Constant */ {NoAlias , NoAlias , MayAlias, NoAlias , MayAlias, NoAlias},
50 /* Group */ {NoAlias , NoAlias , NoAlias , MayAlias, MayAlias, NoAlias},
51 /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias},
52 /* Region */ {NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, MayAlias}
53 };
54 static const AliasResult ASAliasRulesGenIsZero[6][6] = {
Yaxun Liu76ae47c2017-04-06 19:17:32 +000055 /* Flat Global Constant Group Region Private */
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000056 /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias},
57 /* Global */ {MayAlias, MayAlias, NoAlias , NoAlias , NoAlias , NoAlias},
Yaxun Liu76ae47c2017-04-06 19:17:32 +000058 /* Constant */ {MayAlias, NoAlias , MayAlias, NoAlias , NoAlias, NoAlias},
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000059 /* Group */ {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias},
Yaxun Liu76ae47c2017-04-06 19:17:32 +000060 /* Region */ {MayAlias, NoAlias , NoAlias , NoAlias, MayAlias, NoAlias},
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000061 /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias}
62 };
63 assert(AS.MAX_COMMON_ADDRESS <= 5);
64 if (AS.FLAT_ADDRESS == 0) {
65 assert(AS.GLOBAL_ADDRESS == 1 &&
Yaxun Liu76ae47c2017-04-06 19:17:32 +000066 AS.REGION_ADDRESS == 4 &&
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000067 AS.LOCAL_ADDRESS == 3 &&
Yaxun Liu76ae47c2017-04-06 19:17:32 +000068 AS.CONSTANT_ADDRESS == 2 &&
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000069 AS.PRIVATE_ADDRESS == 5);
70 ASAliasRules = &ASAliasRulesGenIsZero;
71 } else {
72 assert(AS.PRIVATE_ADDRESS == 0 &&
73 AS.GLOBAL_ADDRESS == 1 &&
74 AS.CONSTANT_ADDRESS == 2 &&
75 AS.LOCAL_ADDRESS == 3 &&
76 AS.FLAT_ADDRESS == 4 &&
77 AS.REGION_ADDRESS == 5);
78 ASAliasRules = &ASAliasRulesPrivIsZero;
79 }
80}
81
82AliasResult AMDGPUAAResult::ASAliasRulesTy::getAliasResult(unsigned AS1,
83 unsigned AS2) const {
Jan Vesely3c994412017-03-31 19:26:23 +000084 if (AS1 > AS.MAX_COMMON_ADDRESS || AS2 > AS.MAX_COMMON_ADDRESS) {
85 if (Arch == Triple::amdgcn)
86 report_fatal_error("Pointer address space out of range");
87 return AS1 == AS2 ? MayAlias : NoAlias;
88 }
89
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000090 return (*ASAliasRules)[AS1][AS2];
91}
92
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +000093AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA,
94 const MemoryLocation &LocB) {
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +000095 unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
96 unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +000097
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000098 AliasResult Result = ASAliasRules.getAliasResult(asA, asB);
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +000099 if (Result == NoAlias) return Result;
100
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +0000101 // Forward the query to the next alias analysis.
102 return AAResultBase::alias(LocA, LocB);
103}
104
105bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
106 bool OrLocal) {
107 const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
108
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000109 if (Base->getType()->getPointerAddressSpace() == AS.CONSTANT_ADDRESS) {
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +0000110 return true;
111 }
112
113 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
114 if (GV->isConstant())
115 return true;
116 } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
117 const Function *F = Arg->getParent();
118
119 // Only assume constant memory for arguments on kernels.
120 switch (F->getCallingConv()) {
121 default:
122 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
123 case CallingConv::AMDGPU_VS:
124 case CallingConv::AMDGPU_GS:
125 case CallingConv::AMDGPU_PS:
126 case CallingConv::AMDGPU_CS:
127 case CallingConv::AMDGPU_KERNEL:
128 case CallingConv::SPIR_KERNEL:
129 break;
130 }
131
132 unsigned ArgNo = Arg->getArgNo();
133 /* On an argument, ReadOnly attribute indicates that the function does
134 not write through this pointer argument, even though it may write
135 to the memory that the pointer points to.
136 On an argument, ReadNone attribute indicates that the function does
137 not dereference that pointer argument, even though it may read or write
138 the memory that the pointer points to if accessed through other pointers.
139 */
Reid Klecknerf021fab2017-04-13 23:12:13 +0000140 if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
141 (F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
142 F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
Stanislav Mekhanoshin8e45acf2017-03-17 23:56:58 +0000143 return true;
144 }
145 }
146 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
147}