Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/BasicAliasAnalysis.h
Line
Count
Source (jump to first uncovered line)
1
//===- BasicAliasAnalysis.h - Stateless, local Alias Analysis ---*- C++ -*-===//
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 interface for LLVM's primary stateless and local alias analysis.
10
///
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_ANALYSIS_BASICALIASANALYSIS_H
14
#define LLVM_ANALYSIS_BASICALIASANALYSIS_H
15
16
#include "llvm/ADT/DenseMap.h"
17
#include "llvm/ADT/Optional.h"
18
#include "llvm/ADT/SmallPtrSet.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/Analysis/AliasAnalysis.h"
21
#include "llvm/Analysis/AssumptionCache.h"
22
#include "llvm/Analysis/MemoryLocation.h"
23
#include "llvm/IR/InstrTypes.h"
24
#include "llvm/IR/PassManager.h"
25
#include "llvm/Pass.h"
26
#include <algorithm>
27
#include <cstdint>
28
#include <memory>
29
#include <utility>
30
31
namespace llvm {
32
33
struct AAMDNodes;
34
class APInt;
35
class AssumptionCache;
36
class BasicBlock;
37
class DataLayout;
38
class DominatorTree;
39
class Function;
40
class GEPOperator;
41
class LoopInfo;
42
class PHINode;
43
class SelectInst;
44
class TargetLibraryInfo;
45
class PhiValues;
46
class Value;
47
48
/// This is the AA result object for the basic, local, and stateless alias
49
/// analysis. It implements the AA query interface in an entirely stateless
50
/// manner. As one consequence, it is never invalidated due to IR changes.
51
/// While it does retain some storage, that is used as an optimization and not
52
/// to preserve information from query to query. However it does retain handles
53
/// to various other analyses and must be recomputed when those analyses are.
54
class BasicAAResult : public AAResultBase<BasicAAResult> {
55
  friend AAResultBase<BasicAAResult>;
56
57
  const DataLayout &DL;
58
  const Function &F;
59
  const TargetLibraryInfo &TLI;
60
  AssumptionCache &AC;
61
  DominatorTree *DT;
62
  LoopInfo *LI;
63
  PhiValues *PV;
64
65
public:
66
  BasicAAResult(const DataLayout &DL, const Function &F,
67
                const TargetLibraryInfo &TLI, AssumptionCache &AC,
68
                DominatorTree *DT = nullptr, LoopInfo *LI = nullptr,
69
                PhiValues *PV = nullptr)
70
      : AAResultBase(), DL(DL), F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), PV(PV)
71
10.5M
        {}
72
73
  BasicAAResult(const BasicAAResult &Arg)
74
      : AAResultBase(Arg), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI), AC(Arg.AC),
75
        DT(Arg.DT),  LI(Arg.LI), PV(Arg.PV) {}
76
  BasicAAResult(BasicAAResult &&Arg)
77
      : AAResultBase(std::move(Arg)), DL(Arg.DL), F(Arg.F), TLI(Arg.TLI),
78
1.15M
        AC(Arg.AC), DT(Arg.DT), LI(Arg.LI), PV(Arg.PV) {}
79
80
  /// Handle invalidation events in the new pass manager.
81
  bool invalidate(Function &Fn, const PreservedAnalyses &PA,
82
                  FunctionAnalysisManager::Invalidator &Inv);
83
84
  AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
85
86
  ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc);
87
88
  ModRefInfo getModRefInfo(const CallBase *Call1, const CallBase *Call2);
89
90
  /// Chases pointers until we find a (constant global) or not.
91
  bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal);
92
93
  /// Get the location associated with a pointer argument of a callsite.
94
  ModRefInfo getArgModRefInfo(const CallBase *Call, unsigned ArgIdx);
95
96
  /// Returns the behavior when calling the given call site.
97
  FunctionModRefBehavior getModRefBehavior(const CallBase *Call);
98
99
  /// Returns the behavior when calling the given function. For use when the
100
  /// call site is not known.
101
  FunctionModRefBehavior getModRefBehavior(const Function *Fn);
102
103
private:
104
  // A linear transformation of a Value; this class represents ZExt(SExt(V,
105
  // SExtBits), ZExtBits) * Scale + Offset.
106
  struct VariableGEPIndex {
107
    // An opaque Value - we can't decompose this further.
108
    const Value *V;
109
110
    // We need to track what extensions we've done as we consider the same Value
111
    // with different extensions as different variables in a GEP's linear
112
    // expression;
113
    // e.g.: if V == -1, then sext(x) != zext(x).
114
    unsigned ZExtBits;
115
    unsigned SExtBits;
116
117
    APInt Scale;
118
119
160k
    bool operator==(const VariableGEPIndex &Other) const {
120
160k
      return V == Other.V && 
ZExtBits == Other.ZExtBits50.8k
&&
121
160k
             
SExtBits == Other.SExtBits50.8k
&&
Scale == Other.Scale50.8k
;
122
160k
    }
123
124
0
    bool operator!=(const VariableGEPIndex &Other) const {
125
0
      return !operator==(Other);
126
0
    }
127
  };
128
129
  // Represents the internal structure of a GEP, decomposed into a base pointer,
130
  // constant offsets, and variable scaled indices.
131
  struct DecomposedGEP {
132
    // Base pointer of the GEP
133
    const Value *Base;
134
    // Total constant offset w.r.t the base from indexing into structs
135
    APInt StructOffset;
136
    // Total constant offset w.r.t the base from indexing through
137
    // pointers/arrays/vectors
138
    APInt OtherOffset;
139
    // Scaled variable (non-constant) indices.
140
    SmallVector<VariableGEPIndex, 4> VarIndices;
141
  };
142
143
  /// Track alias queries to guard against recursion.
144
  using LocPair = std::pair<MemoryLocation, MemoryLocation>;
145
  using AliasCacheTy = SmallDenseMap<LocPair, AliasResult, 8>;
146
  AliasCacheTy AliasCache;
147
  using IsCapturedCacheTy = SmallDenseMap<const Value *, bool, 8>;
148
  IsCapturedCacheTy IsCapturedCache;
149
150
  /// Tracks phi nodes we have visited.
151
  ///
152
  /// When interpret "Value" pointer equality as value equality we need to make
153
  /// sure that the "Value" is not part of a cycle. Otherwise, two uses could
154
  /// come from different "iterations" of a cycle and see different values for
155
  /// the same "Value" pointer.
156
  ///
157
  /// The following example shows the problem:
158
  ///   %p = phi(%alloca1, %addr2)
159
  ///   %l = load %ptr
160
  ///   %addr1 = gep, %alloca2, 0, %l
161
  ///   %addr2 = gep  %alloca2, 0, (%l + 1)
162
  ///      alias(%p, %addr1) -> MayAlias !
163
  ///   store %l, ...
164
  SmallPtrSet<const BasicBlock *, 8> VisitedPhiBBs;
165
166
  /// Tracks instructions visited by pointsToConstantMemory.
167
  SmallPtrSet<const Value *, 16> Visited;
168
169
  static const Value *
170
  GetLinearExpression(const Value *V, APInt &Scale, APInt &Offset,
171
                      unsigned &ZExtBits, unsigned &SExtBits,
172
                      const DataLayout &DL, unsigned Depth, AssumptionCache *AC,
173
                      DominatorTree *DT, bool &NSW, bool &NUW);
174
175
  static bool DecomposeGEPExpression(const Value *V, DecomposedGEP &Decomposed,
176
      const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT);
177
178
  static bool isGEPBaseAtNegativeOffset(const GEPOperator *GEPOp,
179
      const DecomposedGEP &DecompGEP, const DecomposedGEP &DecompObject,
180
      LocationSize ObjectAccessSize);
181
182
  /// A Heuristic for aliasGEP that searches for a constant offset
183
  /// between the variables.
184
  ///
185
  /// GetLinearExpression has some limitations, as generally zext(%x + 1)
186
  /// != zext(%x) + zext(1) if the arithmetic overflows. GetLinearExpression
187
  /// will therefore conservatively refuse to decompose these expressions.
188
  /// However, we know that, for all %x, zext(%x) != zext(%x + 1), even if
189
  /// the addition overflows.
190
  bool
191
  constantOffsetHeuristic(const SmallVectorImpl<VariableGEPIndex> &VarIndices,
192
                          LocationSize V1Size, LocationSize V2Size,
193
                          APInt BaseOffset, AssumptionCache *AC,
194
                          DominatorTree *DT);
195
196
  bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
197
198
  void GetIndexDifference(SmallVectorImpl<VariableGEPIndex> &Dest,
199
                          const SmallVectorImpl<VariableGEPIndex> &Src);
200
201
  AliasResult aliasGEP(const GEPOperator *V1, LocationSize V1Size,
202
                       const AAMDNodes &V1AAInfo, const Value *V2,
203
                       LocationSize V2Size, const AAMDNodes &V2AAInfo,
204
                       const Value *UnderlyingV1, const Value *UnderlyingV2);
205
206
  AliasResult aliasPHI(const PHINode *PN, LocationSize PNSize,
207
                       const AAMDNodes &PNAAInfo, const Value *V2,
208
                       LocationSize V2Size, const AAMDNodes &V2AAInfo,
209
                       const Value *UnderV2);
210
211
  AliasResult aliasSelect(const SelectInst *SI, LocationSize SISize,
212
                          const AAMDNodes &SIAAInfo, const Value *V2,
213
                          LocationSize V2Size, const AAMDNodes &V2AAInfo,
214
                          const Value *UnderV2);
215
216
  AliasResult aliasCheck(const Value *V1, LocationSize V1Size,
217
                         AAMDNodes V1AATag, const Value *V2,
218
                         LocationSize V2Size, AAMDNodes V2AATag,
219
                         const Value *O1 = nullptr, const Value *O2 = nullptr);
220
};
221
222
/// Analysis pass providing a never-invalidated alias analysis result.
223
class BasicAA : public AnalysisInfoMixin<BasicAA> {
224
  friend AnalysisInfoMixin<BasicAA>;
225
226
  static AnalysisKey Key;
227
228
public:
229
  using Result = BasicAAResult;
230
231
  BasicAAResult run(Function &F, FunctionAnalysisManager &AM);
232
};
233
234
/// Legacy wrapper pass to provide the BasicAAResult object.
235
class BasicAAWrapperPass : public FunctionPass {
236
  std::unique_ptr<BasicAAResult> Result;
237
238
  virtual void anchor();
239
240
public:
241
  static char ID;
242
243
  BasicAAWrapperPass();
244
245
9.24M
  BasicAAResult &getResult() { return *Result; }
246
0
  const BasicAAResult &getResult() const { return *Result; }
247
248
  bool runOnFunction(Function &F) override;
249
  void getAnalysisUsage(AnalysisUsage &AU) const override;
250
};
251
252
FunctionPass *createBasicAAWrapperPass();
253
254
/// A helper for the legacy pass manager to create a \c BasicAAResult object
255
/// populated to the best of our ability for a particular function when inside
256
/// of a \c ModulePass or a \c CallGraphSCCPass.
257
BasicAAResult createLegacyPMBasicAAResult(Pass &P, Function &F);
258
259
/// This class is a functor to be used in legacy module or SCC passes for
260
/// computing AA results for a function. We store the results in fields so that
261
/// they live long enough to be queried, but we re-use them each time.
262
class LegacyAARGetter {
263
  Pass &P;
264
  Optional<BasicAAResult> BAR;
265
  Optional<AAResults> AAR;
266
267
public:
268
2.08M
  LegacyAARGetter(Pass &P) : P(P) {}
269
1.15M
  AAResults &operator()(Function &F) {
270
1.15M
    BAR.emplace(createLegacyPMBasicAAResult(P, F));
271
1.15M
    AAR.emplace(createLegacyPMAAResults(P, F, *BAR));
272
1.15M
    return *AAR;
273
1.15M
  }
274
};
275
276
} // end namespace llvm
277
278
#endif // LLVM_ANALYSIS_BASICALIASANALYSIS_H