1//===- AMDGPUMemoryUtils.h - Memory related helper functions -*- C++ -*----===//
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
9#ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUMEMORYUTILS_H
10#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUMEMORYUTILS_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/ADT/DenseSet.h"
15
16namespace llvm {
17
18struct Align;
19class AAResults;
20class DataLayout;
21class GlobalVariable;
22class LoadInst;
23class MemoryDef;
24class MemorySSA;
25class Value;
26class Function;
27class CallGraph;
28class Module;
29class TargetExtType;
30
31namespace AMDGPU {
32
33static constexpr unsigned NamedBarrierTypeSizeInBytes = 16;
34
35using FunctionVariableMap = DenseMap<Function *, DenseSet<GlobalVariable *>>;
36using VariableFunctionMap = DenseMap<GlobalVariable *, DenseSet<Function *>>;
37
38Align getAlign(const DataLayout &DL, const GlobalVariable *GV);
39
40// Get the synthetic aperture number for the given address space, or None (0)
41// if the address space does not have one.
42unsigned getSyntheticApertureNumber(unsigned AS);
43
44// Copy metadata onto a load widened to read a superset of Source's bytes. Only
45// value-independent metadata is copied; metadata describing the loaded value
46// (!range, !noundef, !nofpclass, !tbaa, ...) is dropped.
47void copyMetadataForWidenedLoad(LoadInst &Dest, const LoadInst &Source);
48
49// If GV is a named-barrier return its type. Otherwise return nullptr.
50TargetExtType *isNamedBarrier(const GlobalVariable &GV);
51
52/// \returns how many named barriers are declared by \p GV.
53unsigned getNumNamedBarriersDeclared(const DataLayout &DL,
54 const GlobalVariable &GV);
55
56bool isDynamicLDS(const GlobalVariable &GV);
57bool isLDSVariableToLower(const GlobalVariable &GV);
58
59struct GVUsesInfoTy {
60 FunctionVariableMap DirectAccess;
61 FunctionVariableMap IndirectAccess;
62};
63
64/// Iterates over all GlobalVariables in \p M, and whenever \p Filter returns
65/// true, replace all constant users of the GV with instructions.
66bool eliminateGVConstantExprUsesFromAllInstructions(
67 Module &M, function_ref<bool(const GlobalVariable &)> Filter);
68
69/// Finds uses of Global Variables on a per-function basis.
70/// \param CG \p M Call Graph
71/// \param M Module
72/// \param Filter Function that returns true for GVs that need to be considered.
73/// \param Kernels[out] Maps kernels to global variables used by that kernel.
74/// \param Functions[out] Maps functions to global variables used by that
75/// function.
76void getUsesOfGVByFunction(const CallGraph &CG, Module &M,
77 function_ref<bool(const GlobalVariable &)> Filter,
78 FunctionVariableMap &Kernels,
79 FunctionVariableMap &Functions);
80
81/// Collects all uses of Global Variables in \p M using
82/// \ref getUsesOfGVByFunction.
83/// \param CG \p M Call Graph
84/// \param M Module
85/// \param Filter Filter for \ref getUsesOfGVByFunction - only GVs for which the
86/// filter returns true will be considered.
87/// \returns Uses of GVs that were found within each function, sorted by
88/// direct and indirect accesses.
89GVUsesInfoTy
90getTransitiveUsesOfGV(const CallGraph &CG, Module &M,
91 function_ref<bool(const GlobalVariable &)> Filter);
92
93/// Collects all uses of LDS Global Variables in \p M using
94/// \ref getUsesOfGVByFunction, with \ref isLDSVariableToLower as the filter.
95/// \param CG \p M Call Graph
96/// \param M Module
97/// \returns Uses of LDS GVs that need lowering that were found within each
98/// function, sorted by direct and indirect accesses.
99GVUsesInfoTy getTransitiveUsesOfLDSForLowering(const CallGraph &CG, Module &M);
100
101/// Strip FnAttr attribute from any functions where we may have
102/// introduced its use.
103void removeFnAttrFromReachable(CallGraph &CG, Function *KernelRoot,
104 ArrayRef<StringRef> FnAttrs);
105
106/// Given a \p Def clobbering a load from \p Ptr according to the MSSA check
107/// if this is actually a memory update or an artificial clobber to facilitate
108/// ordering constraints.
109bool isReallyAClobber(const Value *Ptr, MemoryDef *Def, AAResults *AA);
110
111/// Check is a \p Load is clobbered in its function.
112bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA,
113 AAResults *AA);
114
115} // end namespace AMDGPU
116
117} // end namespace llvm
118
119#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUMEMORYUTILS_H
120