Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- AMDGPUAliasAnalysis ------------------------------------------------===//
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 AMGPU address space based alias analysis pass.
10
//===----------------------------------------------------------------------===//
11
12
#include "AMDGPUAliasAnalysis.h"
13
#include "AMDGPU.h"
14
#include "llvm/ADT/Triple.h"
15
#include "llvm/Analysis/AliasAnalysis.h"
16
#include "llvm/Analysis/MemoryLocation.h"
17
#include "llvm/Analysis/ValueTracking.h"
18
#include "llvm/IR/Argument.h"
19
#include "llvm/IR/Attributes.h"
20
#include "llvm/IR/CallingConv.h"
21
#include "llvm/IR/Function.h"
22
#include "llvm/IR/GlobalVariable.h"
23
#include "llvm/IR/Type.h"
24
#include "llvm/IR/Value.h"
25
#include "llvm/Pass.h"
26
#include "llvm/Support/Casting.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include <cassert>
29
30
using namespace llvm;
31
32
#define DEBUG_TYPE "amdgpu-aa"
33
34
// Register this pass...
35
char AMDGPUAAWrapperPass::ID = 0;
36
char AMDGPUExternalAAWrapper::ID = 0;
37
38
INITIALIZE_PASS(AMDGPUAAWrapperPass, "amdgpu-aa",
39
                "AMDGPU Address space based Alias Analysis", false, true)
40
41
INITIALIZE_PASS(AMDGPUExternalAAWrapper, "amdgpu-aa-wrapper",
42
                "AMDGPU Address space based Alias Analysis Wrapper", false, true)
43
44
2.84k
ImmutablePass *llvm::createAMDGPUAAWrapperPass() {
45
2.84k
  return new AMDGPUAAWrapperPass();
46
2.84k
}
47
48
194
ImmutablePass *llvm::createAMDGPUExternalAAWrapperPass() {
49
194
  return new AMDGPUExternalAAWrapper();
50
194
}
51
52
2.82k
void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
53
2.82k
  AU.setPreservesAll();
54
2.82k
}
55
56
// These arrays are indexed by address space value enum elements 0 ... to 7
57
static const AliasResult ASAliasRules[8][8] = {
58
  /*                    Flat       Global    Region    Group     Constant  Private   Constant 32-bit  Buffer Fat Ptr */
59
  /* Flat     */        {MayAlias, MayAlias, NoAlias,  MayAlias, MayAlias, MayAlias, MayAlias,        MayAlias},
60
  /* Global   */        {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias,        MayAlias},
61
  /* Region   */        {NoAlias,  NoAlias , MayAlias, NoAlias , NoAlias,  NoAlias , NoAlias,         NoAlias},
62
  /* Group    */        {MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , NoAlias , NoAlias ,        NoAlias},
63
  /* Constant */        {MayAlias, MayAlias, NoAlias,  NoAlias , NoAlias , NoAlias , MayAlias,        MayAlias},
64
  /* Private  */        {MayAlias, NoAlias , NoAlias , NoAlias , NoAlias , MayAlias, NoAlias ,        NoAlias},
65
  /* Constant 32-bit */ {MayAlias, MayAlias, NoAlias,  NoAlias , MayAlias, NoAlias , NoAlias ,        MayAlias},
66
  /* Buffer Fat Ptr  */ {MayAlias, MayAlias, NoAlias , NoAlias , MayAlias, NoAlias , MayAlias,        MayAlias}
67
};
68
69
91.0k
static AliasResult getAliasResult(unsigned AS1, unsigned AS2) {
70
91.0k
  static_assert(AMDGPUAS::MAX_AMDGPU_ADDRESS <= 7, "Addr space out of range");
71
91.0k
72
91.0k
  if (AS1 > AMDGPUAS::MAX_AMDGPU_ADDRESS || 
AS2 > AMDGPUAS::MAX_AMDGPU_ADDRESS91.0k
)
73
10
    return MayAlias;
74
91.0k
75
91.0k
  return ASAliasRules[AS1][AS2];
76
91.0k
}
77
78
AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA,
79
                                  const MemoryLocation &LocB,
80
91.0k
                                  AAQueryInfo &AAQI) {
81
91.0k
  unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
82
91.0k
  unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
83
91.0k
84
91.0k
  AliasResult Result = getAliasResult(asA, asB);
85
91.0k
  if (Result == NoAlias)
86
12.1k
    return Result;
87
78.9k
88
78.9k
  // Forward the query to the next alias analysis.
89
78.9k
  return AAResultBase::alias(LocA, LocB, AAQI);
90
78.9k
}
91
92
bool AMDGPUAAResult::pointsToConstantMemory(const MemoryLocation &Loc,
93
142k
                                            AAQueryInfo &AAQI, bool OrLocal) {
94
142k
  const Value *Base = GetUnderlyingObject(Loc.Ptr, DL);
95
142k
  unsigned AS = Base->getType()->getPointerAddressSpace();
96
142k
  if (AS == AMDGPUAS::CONSTANT_ADDRESS ||
97
142k
      
AS == AMDGPUAS::CONSTANT_ADDRESS_32BIT110k
) {
98
33.8k
    return true;
99
33.8k
  }
100
109k
101
109k
  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
102
3.22k
    if (GV->isConstant())
103
0
      return true;
104
105k
  } else if (const Argument *Arg = dyn_cast<Argument>(Base)) {
105
22.8k
    const Function *F = Arg->getParent();
106
22.8k
107
22.8k
    // Only assume constant memory for arguments on kernels.
108
22.8k
    switch (F->getCallingConv()) {
109
22.8k
    default:
110
4.95k
      return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
111
22.8k
    case CallingConv::AMDGPU_LS:
112
17.9k
    case CallingConv::AMDGPU_HS:
113
17.9k
    case CallingConv::AMDGPU_ES:
114
17.9k
    case CallingConv::AMDGPU_GS:
115
17.9k
    case CallingConv::AMDGPU_VS:
116
17.9k
    case CallingConv::AMDGPU_PS:
117
17.9k
    case CallingConv::AMDGPU_CS:
118
17.9k
    case CallingConv::AMDGPU_KERNEL:
119
17.9k
    case CallingConv::SPIR_KERNEL:
120
17.9k
      break;
121
17.9k
    }
122
17.9k
123
17.9k
    unsigned ArgNo = Arg->getArgNo();
124
17.9k
    /* On an argument, ReadOnly attribute indicates that the function does
125
17.9k
       not write through this pointer argument, even though it may write
126
17.9k
       to the memory that the pointer points to.
127
17.9k
       On an argument, ReadNone attribute indicates that the function does
128
17.9k
       not dereference that pointer argument, even though it may read or write
129
17.9k
       the memory that the pointer points to if accessed through other pointers.
130
17.9k
     */
131
17.9k
    if (F->hasParamAttribute(ArgNo, Attribute::NoAlias) &&
132
17.9k
        
(4.56k
F->hasParamAttribute(ArgNo, Attribute::ReadNone)4.56k
||
133
4.56k
         F->hasParamAttribute(ArgNo, Attribute::ReadOnly))) {
134
334
      return true;
135
334
    }
136
103k
  }
137
103k
  return AAResultBase::pointsToConstantMemory(Loc, AAQI, OrLocal);
138
103k
}