LLVM  5.0.0
AMDGPUAliasAnalysis.cpp
Go to the documentation of this file.
1 //===- 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 "AMDGPUAliasAnalysis.h"
14 #include "AMDGPU.h"
16 #include "llvm/Analysis/Passes.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "amdgpu-aa"
26 
27 // Register this pass...
30  "AMDGPU Address space based Alias Analysis", false, true)
31 
33  return new AMDGPUAAWrapperPass();
34 }
35 
37  AU.setPreservesAll();
38 }
39 
40 // Must match the table in getAliasResult.
41 AMDGPUAAResult::ASAliasRulesTy::ASAliasRulesTy(AMDGPUAS AS_, Triple::ArchType Arch_)
42  : Arch(Arch_), AS(AS_) {
43  // 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] = {
55  /* Flat Global Constant Group Region Private */
56  /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias},
57  /* Global */ {MayAlias, MayAlias, NoAlias , NoAlias , NoAlias , NoAlias},
58  /* Constant */ {MayAlias, NoAlias , MayAlias, NoAlias , NoAlias, NoAlias},
59  /* Group */ {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias},
60  /* Region */ {MayAlias, NoAlias , NoAlias , NoAlias, MayAlias, NoAlias},
61  /* 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 &&
66  AS.REGION_ADDRESS == 4 &&
67  AS.LOCAL_ADDRESS == 3 &&
68  AS.CONSTANT_ADDRESS == 2 &&
69  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 
82 AliasResult AMDGPUAAResult::ASAliasRulesTy::getAliasResult(unsigned AS1,
83  unsigned AS2) const {
84  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 
90  return (*ASAliasRules)[AS1][AS2];
91 }
92 
94  const MemoryLocation &LocB) {
95  unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
96  unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
97 
98  AliasResult Result = ASAliasRules.getAliasResult(asA, asB);
99  if (Result == NoAlias) return Result;
100 
101  // Forward the query to the next alias analysis.
102  return AAResultBase::alias(LocA, LocB);
103 }
104 
106  bool OrLocal) {
107  const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
108 
109  if (Base->getType()->getPointerAddressSpace() == AS.CONSTANT_ADDRESS) {
110  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);
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  */
140  if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
141  (F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
142  F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
143  return true;
144  }
145  }
146  return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
147 }
Calling convention used for Mesa compute shaders.
Definition: CallingConv.h:196
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
The two locations do not alias at all.
Definition: AliasAnalysis.h:79
Calling convention used for Mesa geometry shaders.
Definition: CallingConv.h:190
INITIALIZE_PASS(AMDGPUAAWrapperPass,"amdgpu-aa","AMDGPU Address space based Alias Analysis", false, true) ImmutablePass *llvm
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:81
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:186
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
Definition: AMDGPU.h:164
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
#define F(x, y, z)
Definition: MD5.cpp:55
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition: Function.h:327
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:137
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:73
Represent the analysis usage information of a pass.
Legacy wrapper pass to provide the AMDGPUAAResult object.
Calling convention used for Mesa vertex shaders.
Definition: CallingConv.h:187
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
This is the AMGPU address space based alias analysis pass.
const Value * Ptr
The address of the start of the location.
Representation for a specific memory location.
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
Calling convention used for Mesa pixel shaders.
Definition: CallingConv.h:193
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
ImmutablePass * createAMDGPUAAWrapperPass()
void setPreservesAll()
Set by analyses that do not transform their input at all.
static const unsigned CONSTANT_ADDRESS
Address space for constant memory (VTX2)
Definition: AMDGPU.h:174
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:503
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:199