1 //===- AMDGPUAliasAnalysis --------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// \file 10 /// This is the AMGPU address space based alias analysis pass. 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUALIASANALYSIS_H 14 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUALIASANALYSIS_H 15 16 #include "AMDGPU.h" 17 #include "llvm/ADT/Triple.h" 18 #include "llvm/Analysis/AliasAnalysis.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/Pass.h" 22 #include <algorithm> 23 #include <memory> 24 25 namespace llvm { 26 27 class DataLayout; 28 class MDNode; 29 class MemoryLocation; 30 31 /// A simple AA result that uses TBAA metadata to answer queries. 32 class AMDGPUAAResult : public AAResultBase<AMDGPUAAResult> { 33 friend AAResultBase<AMDGPUAAResult>; 34 35 const DataLayout &DL; 36 AMDGPUAS AS; 37 38 public: AMDGPUAAResult(const DataLayout & DL,Triple T)39 explicit AMDGPUAAResult(const DataLayout &DL, Triple T) : AAResultBase(), 40 DL(DL), AS(AMDGPU::getAMDGPUAS(T)), ASAliasRules(AS, T.getArch()) {} AMDGPUAAResult(AMDGPUAAResult && Arg)41 AMDGPUAAResult(AMDGPUAAResult &&Arg) 42 : AAResultBase(std::move(Arg)), DL(Arg.DL), AS(Arg.AS), 43 ASAliasRules(Arg.ASAliasRules){} 44 45 /// Handle invalidation events from the new pass manager. 46 /// 47 /// By definition, this result is stateless and so remains valid. invalidate(Function &,const PreservedAnalyses &)48 bool invalidate(Function &, const PreservedAnalyses &) { return false; } 49 50 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB); 51 bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal); 52 53 private: 54 bool Aliases(const MDNode *A, const MDNode *B) const; 55 bool PathAliases(const MDNode *A, const MDNode *B) const; 56 57 class ASAliasRulesTy { 58 public: 59 ASAliasRulesTy(AMDGPUAS AS_, Triple::ArchType Arch_); 60 61 AliasResult getAliasResult(unsigned AS1, unsigned AS2) const; 62 63 private: 64 Triple::ArchType Arch; 65 AMDGPUAS AS; 66 const AliasResult (*ASAliasRules)[7][7]; 67 } ASAliasRules; 68 }; 69 70 /// Analysis pass providing a never-invalidated alias analysis result. 71 class AMDGPUAA : public AnalysisInfoMixin<AMDGPUAA> { 72 friend AnalysisInfoMixin<AMDGPUAA>; 73 74 static char PassID; 75 76 public: 77 using Result = AMDGPUAAResult; 78 run(Function & F,AnalysisManager<Function> & AM)79 AMDGPUAAResult run(Function &F, AnalysisManager<Function> &AM) { 80 return AMDGPUAAResult(F.getParent()->getDataLayout(), 81 Triple(F.getParent()->getTargetTriple())); 82 } 83 }; 84 85 /// Legacy wrapper pass to provide the AMDGPUAAResult object. 86 class AMDGPUAAWrapperPass : public ImmutablePass { 87 std::unique_ptr<AMDGPUAAResult> Result; 88 89 public: 90 static char ID; 91 AMDGPUAAWrapperPass()92 AMDGPUAAWrapperPass() : ImmutablePass(ID) { 93 initializeAMDGPUAAWrapperPassPass(*PassRegistry::getPassRegistry()); 94 } 95 getResult()96 AMDGPUAAResult &getResult() { return *Result; } getResult()97 const AMDGPUAAResult &getResult() const { return *Result; } 98 doInitialization(Module & M)99 bool doInitialization(Module &M) override { 100 Result.reset(new AMDGPUAAResult(M.getDataLayout(), 101 Triple(M.getTargetTriple()))); 102 return false; 103 } 104 doFinalization(Module & M)105 bool doFinalization(Module &M) override { 106 Result.reset(); 107 return false; 108 } 109 110 void getAnalysisUsage(AnalysisUsage &AU) const override; 111 }; 112 113 } // end namespace llvm 114 115 #endif // LLVM_LIB_TARGET_AMDGPU_AMDGPUALIASANALYSIS_H 116