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