xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.h (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
1 //===- AMDGPUAliasAnalysis --------------------------------------*- 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 /// \file
9 /// This is the AMGPU address space based alias analysis pass.
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUALIASANALYSIS_H
13 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUALIASANALYSIS_H
14 
15 #include "AMDGPU.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Pass.h"
21 #include <algorithm>
22 #include <memory>
23 
24 namespace llvm {
25 
26 class DataLayout;
27 class MDNode;
28 class MemoryLocation;
29 
30 /// A simple AA result that uses TBAA metadata to answer queries.
31 class AMDGPUAAResult : public AAResultBase<AMDGPUAAResult> {
32   friend AAResultBase<AMDGPUAAResult>;
33 
34   const DataLayout &DL;
35 
36 public:
37   explicit AMDGPUAAResult(const DataLayout &DL, Triple T) : AAResultBase(),
38     DL(DL) {}
39   AMDGPUAAResult(AMDGPUAAResult &&Arg)
40       : AAResultBase(std::move(Arg)), DL(Arg.DL) {}
41 
42   /// Handle invalidation events from the new pass manager.
43   ///
44   /// By definition, this result is stateless and so remains valid.
45   bool invalidate(Function &, const PreservedAnalyses &) { return false; }
46 
47   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB,
48                     AAQueryInfo &AAQI);
49   bool pointsToConstantMemory(const MemoryLocation &Loc, AAQueryInfo &AAQI,
50                               bool OrLocal);
51 };
52 
53 /// Analysis pass providing a never-invalidated alias analysis result.
54 class AMDGPUAA : public AnalysisInfoMixin<AMDGPUAA> {
55   friend AnalysisInfoMixin<AMDGPUAA>;
56 
57   static char PassID;
58 
59 public:
60   using Result = AMDGPUAAResult;
61 
62   AMDGPUAAResult run(Function &F, AnalysisManager<Function> &AM) {
63     return AMDGPUAAResult(F.getParent()->getDataLayout(),
64         Triple(F.getParent()->getTargetTriple()));
65   }
66 };
67 
68 /// Legacy wrapper pass to provide the AMDGPUAAResult object.
69 class AMDGPUAAWrapperPass : public ImmutablePass {
70   std::unique_ptr<AMDGPUAAResult> Result;
71 
72 public:
73   static char ID;
74 
75   AMDGPUAAWrapperPass() : ImmutablePass(ID) {
76     initializeAMDGPUAAWrapperPassPass(*PassRegistry::getPassRegistry());
77   }
78 
79   AMDGPUAAResult &getResult() { return *Result; }
80   const AMDGPUAAResult &getResult() const { return *Result; }
81 
82   bool doInitialization(Module &M) override {
83     Result.reset(new AMDGPUAAResult(M.getDataLayout(),
84         Triple(M.getTargetTriple())));
85     return false;
86   }
87 
88   bool doFinalization(Module &M) override {
89     Result.reset();
90     return false;
91   }
92 
93   void getAnalysisUsage(AnalysisUsage &AU) const override;
94 };
95 
96 // Wrapper around ExternalAAWrapperPass so that the default constructor gets the
97 // callback.
98 class AMDGPUExternalAAWrapper : public ExternalAAWrapperPass {
99 public:
100   static char ID;
101 
102   AMDGPUExternalAAWrapper() : ExternalAAWrapperPass(
103     [](Pass &P, Function &, AAResults &AAR) {
104       if (auto *WrapperPass = P.getAnalysisIfAvailable<AMDGPUAAWrapperPass>())
105         AAR.addAAResult(WrapperPass->getResult());
106     }) {}
107 };
108 
109 } // end namespace llvm
110 
111 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUALIASANALYSIS_H
112