1 //===- AMDGPULDSUtils.h - LDS 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 // AMDGPU LDS related helper utility functions.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPULDSUTILS_H
14 #define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPULDSUTILS_H
15 
16 #include "AMDGPU.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/IR/Constants.h"
19 
20 namespace llvm {
21 
22 class ConstantExpr;
23 
24 namespace AMDGPU {
25 
26 /// Collect reachable callees for each kernel defined in the module \p M and
27 /// return collected callees at \p KernelToCallees.
28 void collectReachableCallees(
29     Module &M,
30     DenseMap<Function *, SmallPtrSet<Function *, 8>> &KernelToCallees);
31 
32 /// For the given LDS global \p GV, visit all its users and collect all
33 /// non-kernel functions within which \p GV is used and return collected list of
34 /// such non-kernel functions.
35 SmallPtrSet<Function *, 8> collectNonKernelAccessorsOfLDS(GlobalVariable *GV);
36 
37 /// Collect all the instructions where user \p U belongs to. \p U could be
38 /// instruction itself or it could be a constant expression which is used within
39 /// an instruction. If \p CollectKernelInsts is true, collect instructions only
40 /// from kernels, otherwise collect instructions only from non-kernel functions.
41 DenseMap<Function *, SmallPtrSet<Instruction *, 8>>
42 getFunctionToInstsMap(User *U, bool CollectKernelInsts);
43 
44 bool isKernelCC(const Function *Func);
45 
46 Align getAlign(DataLayout const &DL, const GlobalVariable *GV);
47 
48 /// \returns true if a given global variable \p GV (or its global users) appear
49 /// as an use within some instruction (either from kernel or from non-kernel).
50 bool hasUserInstruction(const GlobalValue *GV);
51 
52 /// \returns true if an LDS global requres lowering to a module LDS structure
53 /// if \p F is not given. If \p F is given it must be a kernel and function
54 /// \returns true if an LDS global is directly used from that kernel and it
55 /// is safe to replace its uses with a kernel LDS structure member.
56 bool shouldLowerLDSToStruct(const GlobalVariable &GV,
57                             const Function *F = nullptr);
58 
59 std::vector<GlobalVariable *> findVariablesToLower(Module &M,
60                                                    const Function *F = nullptr);
61 
62 SmallPtrSet<GlobalValue *, 32> getUsedList(Module &M);
63 
64 /// Replace all uses of constant \p C with instructions in \p F.
65 void replaceConstantUsesInFunction(ConstantExpr *C, const Function *F);
66 } // end namespace AMDGPU
67 
68 } // end namespace llvm
69 
70 #endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPULDSUTILS_H
71