1 //===--------------------- NVPTXAliasAnalysis.cpp--------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 /// This is the NVPTX address space based alias analysis pass.
10 //===----------------------------------------------------------------------===//
12 #include "NVPTXAliasAnalysis.h"
13 #include "MCTargetDesc/NVPTXBaseInfo.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/IR/CallingConv.h"
17 #include "llvm/IR/Instructions.h"
21 #define DEBUG_TYPE "NVPTX-aa"
23 AnalysisKey
NVPTXAA::Key
;
25 char NVPTXAAWrapperPass::ID
= 0;
26 char NVPTXExternalAAWrapper::ID
= 0;
28 INITIALIZE_PASS(NVPTXAAWrapperPass
, "nvptx-aa",
29 "NVPTX Address space based Alias Analysis", false, true)
31 INITIALIZE_PASS(NVPTXExternalAAWrapper
, "nvptx-aa-wrapper",
32 "NVPTX Address space based Alias Analysis Wrapper", false, true)
34 ImmutablePass
*llvm::createNVPTXAAWrapperPass() {
35 return new NVPTXAAWrapperPass();
38 ImmutablePass
*llvm::createNVPTXExternalAAWrapperPass() {
39 return new NVPTXExternalAAWrapper();
42 NVPTXAAWrapperPass::NVPTXAAWrapperPass() : ImmutablePass(ID
) {
43 initializeNVPTXAAWrapperPassPass(*PassRegistry::getPassRegistry());
46 void NVPTXAAWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
50 static AliasResult::Kind
getAliasResult(unsigned AS1
, unsigned AS2
) {
51 if ((AS1
== ADDRESS_SPACE_GENERIC
) || (AS2
== ADDRESS_SPACE_GENERIC
))
52 return AliasResult::MayAlias
;
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
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+).
64 // TODO: cvta.param is not yet supported. We need to change aliasing
65 // rules once it is added.
67 return (AS1
== AS2
? AliasResult::MayAlias
: AliasResult::NoAlias
);
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();
76 return getAliasResult(AS1
, AS2
);
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
;
87 ModRefInfo
NVPTXAAResult::getModRefInfoMask(const MemoryLocation
&Loc
,
90 if (isConstOrParam(Loc
.Ptr
->getType()->getPointerAddressSpace()))
91 return ModRefInfo::NoModRef
;
93 const Value
*Base
= getUnderlyingObject(Loc
.Ptr
);
94 if (isConstOrParam(Base
->getType()->getPointerAddressSpace()))
95 return ModRefInfo::NoModRef
;
97 return ModRefInfo::ModRef
;