Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Target / AMDGPU / AMDGPUAliasAnalysis.cpp
blob629332150cc524277a708d4b38d679c43241ccef
1 //===- AMDGPUAliasAnalysis ------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This is the AMGPU address space based alias analysis pass.
10 //===----------------------------------------------------------------------===//
12 #include "AMDGPUAliasAnalysis.h"
13 #include "AMDGPU.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/Analysis/MemoryLocation.h"
17 #include "llvm/Analysis/ValueTracking.h"
18 #include "llvm/IR/Argument.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/CallingConv.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/Value.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include <cassert>
30 using namespace llvm;
32 #define DEBUG_TYPE "amdgpu-aa"
34 // Register this pass...
35 char AMDGPUAAWrapperPass::ID = 0;
36 char AMDGPUExternalAAWrapper::ID = 0;
38 INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa",
39 "AMDGPU Address space based Alias Analysis", false, true)
41 INITIALIZE_PASS(AMDGPUExternalAAWrapper, "amdgpu-aa-wrapper",
42 "AMDGPU Address space based Alias Analysis Wrapper", false, true)
44 ImmutablePass *llvm::createAMDGPUAAWrapperPass() {
45 return new AMDGPUAAWrapperPass();
48 ImmutablePass *llvm::createAMDGPUExternalAAWrapperPass() {
49 return new AMDGPUExternalAAWrapper();
52 void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesAll();
56 // These arrays are indexed by address space value enum elements 0 ... to 6
57 static const AliasResult ASAliasRules[7][7] = {
58 /* Flat Global Region Group Constant Private Constant 32-bit */
59 /* Flat */ {MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias, MayAlias},
60 /* Global */ {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias},
61 /* Region */ {MayAlias, NoAlias , NoAlias , NoAlias, MayAlias, NoAlias , MayAlias},
62 /* Group */ {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias , NoAlias},
63 /* Constant */ {MayAlias, MayAlias, MayAlias, NoAlias , NoAlias, NoAlias , MayAlias},
64 /* Private */ {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, NoAlias},
65 /* Constant 32-bit */ {MayAlias, MayAlias, MayAlias, NoAlias , MayAlias, NoAlias , NoAlias}
68 static AliasResult getAliasResult(unsigned AS1, unsigned AS2) {
69 static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 6, "Addr space out of range");
71 if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS)
72 return MayAlias;
74 return ASAliasRules[AS1][AS2];
77 AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA,
78 const MemoryLocation &LocB) {
79 unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
80 unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
82 AliasResult Result = getAliasResult(asA, asB);
83 if (Result == NoAlias)
84 return Result;
86 // Forward the query to the next alias analysis.
87 return AAResultBase::alias(LocA, LocB);
90 bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
91 bool OrLocal) {
92 const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
93 unsigned AS = Base->getType()->getPointerAddressSpace();
94 if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
95 AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT) {
96 return true;
99 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
100 if (GV->isConstant())
101 return true;
102 } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
103 const Function *F = Arg->getParent();
105 // Only assume constant memory for arguments on kernels.
106 switch (F->getCallingConv()) {
107 default:
108 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);
109 case CallingConv::AMDGPU_LS:
110 case CallingConv::AMDGPU_HS:
111 case CallingConv::AMDGPU_ES:
112 case CallingConv::AMDGPU_GS:
113 case CallingConv::AMDGPU_VS:
114 case CallingConv::AMDGPU_PS:
115 case CallingConv::AMDGPU_CS:
116 case CallingConv::AMDGPU_KERNEL:
117 case CallingConv::SPIR_KERNEL:
118 break;
121 unsigned ArgNo = Arg->getArgNo();
122 /* On an argument, ReadOnly attribute indicates that the function does
123 not write through this pointer argument, even though it may write
124 to the memory that the pointer points to.
125 On an argument, ReadNone attribute indicates that the function does
126 not dereference that pointer argument, even though it may read or write
127 the memory that the pointer points to if accessed through other pointers.
129 if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
130 (F->hasParamAttribute(ArgNo, Attribute::ReadNone) ||
131 F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
132 return true;
135 return AAResultBase::pointsToConstantMemory(Loc, OrLocal);