1 //===- AMDGPUAliasAnalysis ---------------------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This is the AMGPU address space based alias analysis pass.
11 //===----------------------------------------------------------------------===//
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"
25 #define DEBUG_TYPE "amdgpu-aa"
27 // Register this pass...
28 char AMDGPUAAWrapperPass::ID = 0;
29 INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa",
30 "AMDGPU Address space based Alias Analysis", false, true)
32 ImmutablePass *llvm::createAMDGPUAAWrapperPass() {
33 return new AMDGPUAAWrapperPass();
36 void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
40 AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA,
41 const MemoryLocation &LocB) {
42 // This array is indexed by the AMDGPUAS::AddressSpaces
43 // enum elements PRIVATE_ADDRESS ... to FLAT_ADDRESS
44 // see "llvm/Transforms/AMDSPIRUtils.h"
45 static const AliasResult ASAliasRules[5][5] = {
46 /* Private Global Constant Group Flat */
47 /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , MayAlias},
48 /* Global */ {NoAlias , MayAlias, NoAlias , NoAlias , MayAlias},
49 /* Constant */ {NoAlias , NoAlias , MayAlias, NoAlias , MayAlias},
50 /* Group */ {NoAlias , NoAlias , NoAlias , MayAlias, MayAlias},
51 /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias}
53 unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
54 unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
55 if (asA > AMDGPUAS::AddressSpaces::FLAT_ADDRESS ||
56 asB > AMDGPUAS::AddressSpaces::FLAT_ADDRESS)
57 report_fatal_error("Pointer address space out of range");
59 AliasResult Result = ASAliasRules[asA][asB];
60 if (Result == NoAlias) return Result;
62 if (isa<Argument>(LocA.Ptr) && isa<Argument>(LocB.Ptr)) {
63 Type *T1 = cast<PointerType>(LocA.Ptr->getType())->getElementType();
64 Type *T2 = cast<PointerType>(LocB.Ptr->getType())->getElementType();
66 if ((T1->isVectorTy() && !T2->isVectorTy()) ||
67 (T2->isVectorTy() && !T1->isVectorTy()))
70 // Forward the query to the next alias analysis.
71 return AAResultBase::alias(LocA, LocB);
74 bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
76 const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
78 if (Base->getType()->getPointerAddressSpace() ==
79 AMDGPUAS::AddressSpaces::CONSTANT_ADDRESS) {
83 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
86 } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
87 const Function *F = Arg->getParent();
89 // Only assume constant memory for arguments on kernels.
90 switch (F->getCallingConv()) {
92 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
93 case CallingConv::AMDGPU_VS:
94 case CallingConv::AMDGPU_GS:
95 case CallingConv::AMDGPU_PS:
96 case CallingConv::AMDGPU_CS:
97 case CallingConv::AMDGPU_KERNEL:
98 case CallingConv::SPIR_KERNEL:
102 unsigned ArgNo = Arg->getArgNo();
103 /* On an argument, ReadOnly attribute indicates that the function does
104 not write through this pointer argument, even though it may write
105 to the memory that the pointer points to.
106 On an argument, ReadNone attribute indicates that the function does
107 not dereference that pointer argument, even though it may read or write
108 the memory that the pointer points to if accessed through other pointers.
110 if (F->getAttributes().hasAttribute(ArgNo + 1, Attribute::NoAlias) &&
111 (F->getAttributes().hasAttribute(ArgNo + 1, Attribute::ReadNone) ||
112 F->getAttributes().hasAttribute(ArgNo + 1, Attribute::ReadOnly))) {
116 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);