LLVM  7.0.0svn
AMDGPUAliasAnalysis.cpp
Go to the documentation of this file.
1 //===- AMDGPUAliasAnalysis ------------------------------------------------===//
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"
15 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/Argument.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/CallingConv.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/GlobalVariable.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/Casting.h"
29 #include <cassert>
30 
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "amdgpu-aa"
34 
35 // Register this pass...
37 
39  "AMDGPU Address space based Alias Analysis", false, true)
40 
42  return new AMDGPUAAWrapperPass();
43 }
44 
46  AU.setPreservesAll();
47 }
48 
49 // Must match the table in getAliasResult.
50 AMDGPUAAResult::ASAliasRulesTy::ASAliasRulesTy(AMDGPUAS AS_, Triple::ArchType Arch_)
51  : Arch(Arch_), AS(AS_) {
52  // These arrarys are indexed by address space value
53  // enum elements 0 ... to 5
54  static const AliasResult ASAliasRulesPrivIsZero[6][6] = {
55  /* Private Global Constant Group Flat Region*/
56  /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , MayAlias, NoAlias},
57  /* Global */ {NoAlias , MayAlias, NoAlias , NoAlias , MayAlias, NoAlias},
58  /* Constant */ {NoAlias , NoAlias , MayAlias, NoAlias , MayAlias, NoAlias},
59  /* Group */ {NoAlias , NoAlias , NoAlias , MayAlias, MayAlias, NoAlias},
60  /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias},
61  /* Region */ {NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, MayAlias}
62  };
63  static const AliasResult ASAliasRulesGenIsZero[6][6] = {
64  /* Flat Global Region Group Constant Private */
65  /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias},
66  /* Global */ {MayAlias, MayAlias, NoAlias , NoAlias , NoAlias , NoAlias},
67  /* Constant */ {MayAlias, NoAlias , MayAlias, NoAlias , NoAlias, NoAlias},
68  /* Group */ {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias},
69  /* Region */ {MayAlias, NoAlias , NoAlias , NoAlias, MayAlias, NoAlias},
70  /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias}
71  };
73  if (AS.FLAT_ADDRESS == 0) {
74  assert(AS.GLOBAL_ADDRESS == 1 &&
75  AS.REGION_ADDRESS == 2 &&
76  AS.LOCAL_ADDRESS == 3 &&
77  AS.CONSTANT_ADDRESS == 4 &&
78  AS.PRIVATE_ADDRESS == 5);
79  ASAliasRules = &ASAliasRulesGenIsZero;
80  } else {
81  assert(AS.PRIVATE_ADDRESS == 0 &&
82  AS.GLOBAL_ADDRESS == 1 &&
83  AS.CONSTANT_ADDRESS == 2 &&
84  AS.LOCAL_ADDRESS == 3 &&
85  AS.FLAT_ADDRESS == 4 &&
86  AS.REGION_ADDRESS == 5);
87  ASAliasRules = &ASAliasRulesPrivIsZero;
88  }
89 }
90 
91 AliasResult AMDGPUAAResult::ASAliasRulesTy::getAliasResult(unsigned AS1,
92  unsigned AS2) const {
93  if (AS1 > AS.MAX_COMMON_ADDRESS || AS2 > AS.MAX_COMMON_ADDRESS) {
94  if (Arch == Triple::amdgcn)
95  report_fatal_error("Pointer address space out of range");
96  return AS1 == AS2 ? MayAlias : NoAlias;
97  }
98 
99  return (*ASAliasRules)[AS1][AS2];
100 }
101 
103  const MemoryLocation &LocB) {
104  unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
105  unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
106 
107  AliasResult Result = ASAliasRules.getAliasResult(asA, asB);
108  if (Result == NoAlias) return Result;
109 
110  // Forward the query to the next alias analysis.
111  return AAResultBase::alias(LocA, LocB);
112 }
113 
115  bool OrLocal) {
116  const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
117 
120  return true;
121  }
122 
123  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
124  if (GV->isConstant())
125  return true;
126  } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
127  const Function *F = Arg->getParent();
128 
129  // Only assume constant memory for arguments on kernels.
130  switch (F->getCallingConv()) {
131  default:
132  return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
142  break;
143  }
144 
145  unsigned ArgNo = Arg->getArgNo();
146  /* On an argument, ReadOnly attribute indicates that the function does
147  not write through this pointer argument, even though it may write
148  to the memory that the pointer points to.
149  On an argument, ReadNone attribute indicates that the function does
150  not dereference that pointer argument, even though it may read or write
151  the memory that the pointer points to if accessed through other pointers.
152  */
153  if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
154  (F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
155  F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
156  return true;
157  }
158  }
159  return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
160 }
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:115
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
Calling convention used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:208
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal)
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:234
The two locations do not alias at all.
Definition: AliasAnalysis.h:85
Calling convention used for AMDPAL shader stage before geometry shader if geometry is in use...
Definition: CallingConv.h:221
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:201
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
F(f)
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:503
Calling convention used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:198
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
Definition: AMDGPU.h:224
Calling convention used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (ve...
Definition: CallingConv.h:189
This file contains the simple types necessary to represent the attributes associated with functions a...
bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const
check if an attributes is in the list of attributes.
Definition: Function.h:384
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
Address space for 32-bit constant memory.
Definition: AMDGPU.h:238
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:79
Calling convention used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:195
Represent the analysis usage information of a pass.
Legacy wrapper pass to provide the AMDGPUAAResult object.
Calling convention used for AMDPAL vertex shader if tessellation is in use.
Definition: CallingConv.h:216
unsigned REGION_ADDRESS
Address space for region memory.
Definition: AMDGPU.h:228
const AMDGPUAS & AS
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.
The two locations may or may not alias. This is the least precise result.
Definition: AliasAnalysis.h:87
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:256
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:199
ImmutablePass * createAMDGPUAAWrapperPass()
SPIR_KERNEL - Calling convention for SPIR kernel functions.
Definition: CallingConv.h:137
void setPreservesAll()
Set by analyses that do not transform their input at all.
amdgpu Simplify well known AMD library false Value Value * Arg
Calling convention used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:192
This file provides utility analysis objects describing memory locations.
unsigned FLAT_ADDRESS
Address space for flat memory.
Definition: AMDGPU.h:227
INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa", "AMDGPU Address space based Alias Analysis", false, true) ImmutablePass *llvm
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal)
Address space for local memory.
Definition: AMDGPU.h:236
Address space for constant memory (VTX2)
Definition: AMDGPU.h:235
unsigned PRIVATE_ADDRESS
Address space for private memory.
Definition: AMDGPU.h:226