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