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