xref: /freebsd/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===--------------------- NVPTXAliasAnalysis.cpp--------------------------===//
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 NVPTX address space based alias analysis pass.
10 //===----------------------------------------------------------------------===//
11 
12 #include "NVPTXAliasAnalysis.h"
13 #include "MCTargetDesc/NVPTXBaseInfo.h"
14 #include "NVPTX.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/IR/CallingConv.h"
17 #include "llvm/IR/Instructions.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "NVPTX-aa"
22 
23 AnalysisKey NVPTXAA::Key;
24 
25 char NVPTXAAWrapperPass::ID = 0;
26 char NVPTXExternalAAWrapper::ID = 0;
27 
28 INITIALIZE_PASS(NVPTXAAWrapperPass, "nvptx-aa",
29                 "NVPTX Address space based Alias Analysis", false, true)
30 
31 INITIALIZE_PASS(NVPTXExternalAAWrapper, "nvptx-aa-wrapper",
32                 "NVPTX Address space based Alias Analysis Wrapper", false, true)
33 
34 ImmutablePass *llvm::createNVPTXAAWrapperPass() {
35   return new NVPTXAAWrapperPass();
36 }
37 
38 ImmutablePass *llvm::createNVPTXExternalAAWrapperPass() {
39   return new NVPTXExternalAAWrapper();
40 }
41 
42 NVPTXAAWrapperPass::NVPTXAAWrapperPass() : ImmutablePass(ID) {
43   initializeNVPTXAAWrapperPassPass(*PassRegistry::getPassRegistry());
44 }
45 
46 void NVPTXAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
47   AU.setPreservesAll();
48 }
49 
50 static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
51   if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC))
52     return AliasResult::MayAlias;
53 
54   // PTX s6.4.1.1. Generic Addressing:
55   // A generic address maps to global memory unless it falls within
56   // the window for const, local, or shared memory. The Kernel
57   // Function Parameters (.param) window is contained within the
58   // .global window.
59   //
60   // Therefore a global pointer may alias with a param pointer on some
61   // GPUs via addrspacecast(param->generic->global) when cvta.param
62   // instruction is used (PTX 7.7+ and SM_70+).
63   //
64   // TODO: cvta.param is not yet supported. We need to change aliasing
65   // rules once it is added.
66 
67   return (AS1 == AS2 ? AliasResult::MayAlias : AliasResult::NoAlias);
68 }
69 
70 AliasResult NVPTXAAResult::alias(const MemoryLocation &Loc1,
71                                  const MemoryLocation &Loc2, AAQueryInfo &AAQI,
72                                  const Instruction *) {
73   unsigned AS1 = Loc1.Ptr->getType()->getPointerAddressSpace();
74   unsigned AS2 = Loc2.Ptr->getType()->getPointerAddressSpace();
75 
76   return getAliasResult(AS1, AS2);
77 }
78 
79 // TODO: .param address space may be writable in presence of cvta.param, but
80 // this instruction is currently not supported. NVPTXLowerArgs also does not
81 // allow any writes to .param pointers.
82 static bool isConstOrParam(unsigned AS) {
83   return AS == AddressSpace::ADDRESS_SPACE_CONST ||
84          AS == AddressSpace::ADDRESS_SPACE_PARAM;
85 }
86 
87 ModRefInfo NVPTXAAResult::getModRefInfoMask(const MemoryLocation &Loc,
88                                             AAQueryInfo &AAQI,
89                                             bool IgnoreLocals) {
90   if (isConstOrParam(Loc.Ptr->getType()->getPointerAddressSpace()))
91     return ModRefInfo::NoModRef;
92 
93   const Value *Base = getUnderlyingObject(Loc.Ptr);
94   if (isConstOrParam(Base->getType()->getPointerAddressSpace()))
95     return ModRefInfo::NoModRef;
96 
97   return ModRefInfo::ModRef;
98 }
99