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/Instructions.h" 17 #include "llvm/Support/CommandLine.h" 18 19 using namespace llvm; 20 21 #define DEBUG_TYPE "NVPTX-aa" 22 23 static cl::opt<unsigned> TraverseAddressSpacesLimit( 24 "nvptx-traverse-address-aliasing-limit", cl::Hidden, 25 cl::desc("Depth limit for finding address space through traversal"), 26 cl::init(6)); 27 28 AnalysisKey NVPTXAA::Key; 29 30 char NVPTXAAWrapperPass::ID = 0; 31 char NVPTXExternalAAWrapper::ID = 0; 32 33 INITIALIZE_PASS(NVPTXAAWrapperPass, "nvptx-aa", 34 "NVPTX Address space based Alias Analysis", false, true) 35 36 INITIALIZE_PASS(NVPTXExternalAAWrapper, "nvptx-aa-wrapper", 37 "NVPTX Address space based Alias Analysis Wrapper", false, true) 38 39 ImmutablePass *llvm::createNVPTXAAWrapperPass() { 40 return new NVPTXAAWrapperPass(); 41 } 42 43 ImmutablePass *llvm::createNVPTXExternalAAWrapperPass() { 44 return new NVPTXExternalAAWrapper(); 45 } 46 47 NVPTXAAWrapperPass::NVPTXAAWrapperPass() : ImmutablePass(ID) { 48 initializeNVPTXAAWrapperPassPass(*PassRegistry::getPassRegistry()); 49 } 50 51 void NVPTXAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 52 AU.setPreservesAll(); 53 } 54 55 static unsigned getAddressSpace(const Value *V, unsigned MaxLookup) { 56 // Find the first non-generic address space traversing the UD chain. 57 // It is undefined behaviour if a pointer belongs to more than one 58 // non-overlapping address spaces along a valid execution path. 59 auto GetAS = [](const Value *V) -> unsigned { 60 if (const auto *PTy = dyn_cast<PointerType>(V->getType())) 61 return PTy->getAddressSpace(); 62 return ADDRESS_SPACE_GENERIC; 63 }; 64 while (MaxLookup-- && GetAS(V) == ADDRESS_SPACE_GENERIC) { 65 const Value *NewV = getUnderlyingObject(V, 1); 66 if (NewV == V) 67 break; 68 V = NewV; 69 } 70 return GetAS(V); 71 } 72 73 static AliasResult::Kind getAliasResult(unsigned AS1, unsigned AS2) { 74 if ((AS1 == ADDRESS_SPACE_GENERIC) || (AS2 == ADDRESS_SPACE_GENERIC)) 75 return AliasResult::MayAlias; 76 77 // PTX s6.4.1.1. Generic Addressing: 78 // A generic address maps to global memory unless it falls within 79 // the window for const, local, or shared memory. The Kernel 80 // Function Parameters (.param) window is contained within the 81 // .global window. 82 // 83 // Therefore a global pointer may alias with a param pointer on some 84 // GPUs via addrspacecast(param->generic->global) when cvta.param 85 // instruction is used (PTX 7.7+ and SM_70+). 86 // 87 // TODO: cvta.param is not yet supported. We need to change aliasing 88 // rules once it is added. 89 90 return (AS1 == AS2 ? AliasResult::MayAlias : AliasResult::NoAlias); 91 } 92 93 AliasResult NVPTXAAResult::alias(const MemoryLocation &Loc1, 94 const MemoryLocation &Loc2, AAQueryInfo &AAQI, 95 const Instruction *) { 96 unsigned AS1 = getAddressSpace(Loc1.Ptr, TraverseAddressSpacesLimit); 97 unsigned AS2 = getAddressSpace(Loc2.Ptr, TraverseAddressSpacesLimit); 98 99 return getAliasResult(AS1, AS2); 100 } 101 102 // TODO: .param address space may be writable in presence of cvta.param, but 103 // this instruction is currently not supported. NVPTXLowerArgs also does not 104 // allow any writes to .param pointers. 105 static bool isConstOrParam(unsigned AS) { 106 return AS == AddressSpace::ADDRESS_SPACE_CONST || 107 AS == AddressSpace::ADDRESS_SPACE_PARAM; 108 } 109 110 ModRefInfo NVPTXAAResult::getModRefInfoMask(const MemoryLocation &Loc, 111 AAQueryInfo &AAQI, 112 bool IgnoreLocals) { 113 if (isConstOrParam(getAddressSpace(Loc.Ptr, TraverseAddressSpacesLimit))) 114 return ModRefInfo::NoModRef; 115 116 return ModRefInfo::ModRef; 117 } 118