blob: 127b26397946bb972026d7cd5d14b24e062826d3 [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
40AliasResult 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}
52 };
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");
58
59 AliasResult Result = ASAliasRules[asA][asB];
60 if (Result == NoAlias) return Result;
61
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();
65
66 if ((T1->isVectorTy() && !T2->isVectorTy()) ||
67 (T2->isVectorTy() && !T1->isVectorTy()))
68 return NoAlias;
69 }
70 // Forward the query to the next alias analysis.
71 return AAResultBase::alias(LocA, LocB);
72}
73
74bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
75 bool OrLocal) {
76 const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
77
78 if (Base->getType()->getPointerAddressSpace() ==
79 AMDGPUAS::AddressSpaces::CONSTANT_ADDRESS) {
80 return true;
81 }
82
83 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
84 if (GV->isConstant())
85 return true;
86 } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
87 const Function *F = Arg->getParent();
88
89 // Only assume constant memory for arguments on kernels.
90 switch (F->getCallingConv()) {
91 default:
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:
99 break;
100 }
101
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.
109 */
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))) {
113 return true;
114 }
115 }
116 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
117}