xref: /freebsd/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXAliasAnalysis.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
106c3fb27SDimitry Andric //===--------------------- NVPTXAliasAnalysis.cpp--------------------------===//
206c3fb27SDimitry Andric //
306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606c3fb27SDimitry Andric //
706c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
806c3fb27SDimitry Andric /// \file
906c3fb27SDimitry Andric /// This is the NVPTX address space based alias analysis pass.
1006c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
1106c3fb27SDimitry Andric 
1206c3fb27SDimitry Andric #include "NVPTXAliasAnalysis.h"
1306c3fb27SDimitry Andric #include "MCTargetDesc/NVPTXBaseInfo.h"
1406c3fb27SDimitry Andric #include "NVPTX.h"
1506c3fb27SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
1606c3fb27SDimitry Andric #include "llvm/IR/CallingConv.h"
1706c3fb27SDimitry Andric #include "llvm/IR/Instructions.h"
1806c3fb27SDimitry Andric 
1906c3fb27SDimitry Andric using namespace llvm;
2006c3fb27SDimitry Andric 
2106c3fb27SDimitry Andric #define DEBUG_TYPE "NVPTX-aa"
2206c3fb27SDimitry Andric 
2306c3fb27SDimitry Andric AnalysisKey NVPTXAA::Key;
2406c3fb27SDimitry Andric 
2506c3fb27SDimitry Andric char NVPTXAAWrapperPass::ID = 0;
2606c3fb27SDimitry Andric char NVPTXExternalAAWrapper::ID = 0;
2706c3fb27SDimitry Andric 
2806c3fb27SDimitry Andric INITIALIZE_PASS(NVPTXAAWrapperPass, "nvptx-aa",
2906c3fb27SDimitry Andric                 "NVPTX Address space based Alias Analysis", false, true)
3006c3fb27SDimitry Andric 
3106c3fb27SDimitry Andric INITIALIZE_PASS(NVPTXExternalAAWrapper, "nvptx-aa-wrapper",
3206c3fb27SDimitry Andric                 "NVPTX Address space based Alias Analysis Wrapper", false, true)
3306c3fb27SDimitry Andric 
3406c3fb27SDimitry Andric ImmutablePass *llvm::createNVPTXAAWrapperPass() {
3506c3fb27SDimitry Andric   return new NVPTXAAWrapperPass();
3606c3fb27SDimitry Andric }
3706c3fb27SDimitry Andric 
3806c3fb27SDimitry Andric ImmutablePass *llvm::createNVPTXExternalAAWrapperPass() {
3906c3fb27SDimitry Andric   return new NVPTXExternalAAWrapper();
4006c3fb27SDimitry Andric }
4106c3fb27SDimitry Andric 
4206c3fb27SDimitry Andric NVPTXAAWrapperPass::NVPTXAAWrapperPass() : ImmutablePass(ID) {
4306c3fb27SDimitry Andric   initializeNVPTXAAWrapperPassPass(*PassRegistry::getPassRegistry());
4406c3fb27SDimitry Andric }
4506c3fb27SDimitry Andric 
4606c3fb27SDimitry Andric void NVPTXAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
4706c3fb27SDimitry Andric   AU.setPreservesAll();
4806c3fb27SDimitry Andric }
4906c3fb27SDimitry Andric 
5006c3fb27SDimitry Andric static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) {
5106c3fb27SDimitry Andric   if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC))
5206c3fb27SDimitry Andric     return AliasResult::MayAlias;
5306c3fb27SDimitry Andric 
5406c3fb27SDimitry Andric   // PTX s6.4.1.1. Generic Addressing:
5506c3fb27SDimitry Andric   // A generic address maps to global memory unless it falls within
5606c3fb27SDimitry Andric   // the window for const, local, or shared memory. The Kernel
5706c3fb27SDimitry Andric   // Function Parameters (.param) window is contained within the
5806c3fb27SDimitry Andric   // .global window.
5906c3fb27SDimitry Andric   //
6006c3fb27SDimitry Andric   // Therefore a global pointer may alias with a param pointer on some
6106c3fb27SDimitry Andric   // GPUs via addrspacecast(param->generic->global) when cvta.param
6206c3fb27SDimitry Andric   // instruction is used (PTX 7.7+ and SM_70+).
6306c3fb27SDimitry Andric   //
6406c3fb27SDimitry Andric   // TODO: cvta.param is not yet supported. We need to change aliasing
6506c3fb27SDimitry Andric   // rules once it is added.
6606c3fb27SDimitry Andric 
6706c3fb27SDimitry Andric   return (AS1 == AS2 ? AliasResult::MayAlias : AliasResult::NoAlias);
6806c3fb27SDimitry Andric }
6906c3fb27SDimitry Andric 
7006c3fb27SDimitry Andric AliasResult NVPTXAAResult::alias(const MemoryLocation &Loc1,
7106c3fb27SDimitry Andric                                  const MemoryLocation &Loc2, AAQueryInfo &AAQI,
7206c3fb27SDimitry Andric                                  const Instruction *) {
7306c3fb27SDimitry Andric   unsigned AS1 = Loc1.Ptr->getType()->getPointerAddressSpace();
7406c3fb27SDimitry Andric   unsigned AS2 = Loc2.Ptr->getType()->getPointerAddressSpace();
7506c3fb27SDimitry Andric 
7606c3fb27SDimitry Andric   return getAliasResult(AS1, AS2);
7706c3fb27SDimitry Andric }
7806c3fb27SDimitry Andric 
7906c3fb27SDimitry Andric // TODO: .param address space may be writable in presence of cvta.param, but
8006c3fb27SDimitry Andric // this instruction is currently not supported. NVPTXLowerArgs also does not
8106c3fb27SDimitry Andric // allow any writes to .param pointers.
8206c3fb27SDimitry Andric static bool isConstOrParam(unsigned AS) {
8306c3fb27SDimitry Andric   return AS == AddressSpace::ADDRESS_SPACE_CONST ||
8406c3fb27SDimitry Andric          AS == AddressSpace::ADDRESS_SPACE_PARAM;
8506c3fb27SDimitry Andric }
8606c3fb27SDimitry Andric 
8706c3fb27SDimitry Andric ModRefInfo NVPTXAAResult::getModRefInfoMask(const MemoryLocation &Loc,
8806c3fb27SDimitry Andric                                             AAQueryInfo &AAQI,
8906c3fb27SDimitry Andric                                             bool IgnoreLocals) {
9006c3fb27SDimitry Andric   if (isConstOrParam(Loc.Ptr->getType()->getPointerAddressSpace()))
9106c3fb27SDimitry Andric     return ModRefInfo::NoModRef;
9206c3fb27SDimitry Andric 
9306c3fb27SDimitry Andric   const Value *Base = getUnderlyingObject(Loc.Ptr);
9406c3fb27SDimitry Andric   if (isConstOrParam(Base->getType()->getPointerAddressSpace()))
9506c3fb27SDimitry Andric     return ModRefInfo::NoModRef;
9606c3fb27SDimitry Andric 
97*5f757f3fSDimitry Andric   return ModRefInfo::ModRef;
9806c3fb27SDimitry Andric }
99