LLVM  4.0.0
AssumptionCache.h
Go to the documentation of this file.
1 //===- llvm/Analysis/AssumptionCache.h - Track @llvm.assume ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains a pass that keeps track of @llvm.assume intrinsics in
11 // the functions of a module (allowing assumptions within any function to be
12 // found cheaply by other parts of the optimizer).
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_ANALYSIS_ASSUMPTIONCACHE_H
17 #define LLVM_ANALYSIS_ASSUMPTIONCACHE_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallSet.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/Pass.h"
27 #include <memory>
28 
29 namespace llvm {
30 
31 /// \brief A cache of @llvm.assume calls within a function.
32 ///
33 /// This cache provides fast lookup of assumptions within a function by caching
34 /// them and amortizing the cost of scanning for them across all queries. The
35 /// cache is also conservatively self-updating so that it will never return
36 /// incorrect results about a function even as the function is being mutated.
37 /// However, flushing the cache and rebuilding it (or explicitly updating it)
38 /// may allow it to discover new assumptions.
40  /// \brief The function for which this cache is handling assumptions.
41  ///
42  /// We track this to lazily populate our assumptions.
43  Function &F;
44 
45  /// \brief Vector of weak value handles to calls of the @llvm.assume
46  /// intrinsic.
47  SmallVector<WeakVH, 4> AssumeHandles;
48 
49  class AffectedValueCallbackVH final : public CallbackVH {
50  AssumptionCache *AC;
51  void deleted() override;
52  void allUsesReplacedWith(Value *) override;
53 
54  public:
55  using DMI = DenseMapInfo<Value *>;
56 
57  AffectedValueCallbackVH(Value *V, AssumptionCache *AC = nullptr)
58  : CallbackVH(V), AC(AC) {}
59  };
60 
61  friend AffectedValueCallbackVH;
62 
63  /// \brief A map of values about which an assumption might be providing
64  /// information to the relevant set of assumptions.
65  using AffectedValuesMap =
68  AffectedValuesMap AffectedValues;
69 
70  /// Get the vector of assumptions which affect a value from the cache.
71  SmallVector<WeakVH, 1> &getOrInsertAffectedValues(Value *V);
72 
73  /// Copy affected values in the cache for OV to be affected values for NV.
74  void copyAffectedValuesInCache(Value *OV, Value *NV);
75 
76  /// \brief Flag tracking whether we have scanned the function yet.
77  ///
78  /// We want to be as lazy about this as possible, and so we scan the function
79  /// at the last moment.
80  bool Scanned;
81 
82  /// \brief Scan the function for assumptions and add them to the cache.
83  void scanFunction();
84 
85 public:
86  /// \brief Construct an AssumptionCache from a function by scanning all of
87  /// its instructions.
88  AssumptionCache(Function &F) : F(F), Scanned(false) {}
89 
90  /// \brief Add an @llvm.assume intrinsic to this function's cache.
91  ///
92  /// The call passed in must be an instruction within this function and must
93  /// not already be in the cache.
94  void registerAssumption(CallInst *CI);
95 
96  /// \brief Update the cache of values being affected by this assumption (i.e.
97  /// the values about which this assumption provides information).
99 
100  /// \brief Clear the cache of @llvm.assume intrinsics for a function.
101  ///
102  /// It will be re-scanned the next time it is requested.
103  void clear() {
104  AssumeHandles.clear();
105  AffectedValues.clear();
106  Scanned = false;
107  }
108 
109  /// \brief Access the list of assumption handles currently tracked for this
110  /// function.
111  ///
112  /// Note that these produce weak handles that may be null. The caller must
113  /// handle that case.
114  /// FIXME: We should replace this with pointee_iterator<filter_iterator<...>>
115  /// when we can write that to filter out the null values. Then caller code
116  /// will become simpler.
118  if (!Scanned)
119  scanFunction();
120  return AssumeHandles;
121  }
122 
123  /// \brief Access the list of assumptions which affect this value.
125  if (!Scanned)
126  scanFunction();
127 
128  auto AVI = AffectedValues.find_as(const_cast<Value *>(V));
129  if (AVI == AffectedValues.end())
130  return MutableArrayRef<WeakVH>();
131 
132  return AVI->second;
133  }
134 };
135 
136 /// \brief A function analysis which provides an \c AssumptionCache.
137 ///
138 /// This analysis is intended for use with the new pass manager and will vend
139 /// assumption caches for a given function.
140 class AssumptionAnalysis : public AnalysisInfoMixin<AssumptionAnalysis> {
142  static AnalysisKey Key;
143 
144 public:
146 
148  return AssumptionCache(F);
149  }
150 };
151 
152 /// \brief Printer pass for the \c AssumptionAnalysis results.
153 class AssumptionPrinterPass : public PassInfoMixin<AssumptionPrinterPass> {
154  raw_ostream &OS;
155 
156 public:
157  explicit AssumptionPrinterPass(raw_ostream &OS) : OS(OS) {}
159 };
160 
161 /// \brief An immutable pass that tracks lazily created \c AssumptionCache
162 /// objects.
163 ///
164 /// This is essentially a workaround for the legacy pass manager's weaknesses
165 /// which associates each assumption cache with Function and clears it if the
166 /// function is deleted. The nature of the AssumptionCache is that it is not
167 /// invalidated by any changes to the function body and so this is sufficient
168 /// to be conservatively correct.
170  /// A callback value handle applied to function objects, which we use to
171  /// delete our cache of intrinsics for a function when it is deleted.
172  class FunctionCallbackVH final : public CallbackVH {
174  void deleted() override;
175 
176  public:
177  typedef DenseMapInfo<Value *> DMI;
178 
179  FunctionCallbackVH(Value *V, AssumptionCacheTracker *ACT = nullptr)
180  : CallbackVH(V), ACT(ACT) {}
181  };
182 
183  friend FunctionCallbackVH;
184 
187  FunctionCallsMap AssumptionCaches;
188 
189 public:
190  /// \brief Get the cached assumptions for a function.
191  ///
192  /// If no assumptions are cached, this will scan the function. Otherwise, the
193  /// existing cache will be returned.
195 
197  ~AssumptionCacheTracker() override;
198 
199  void releaseMemory() override { AssumptionCaches.shrink_and_clear(); }
200 
201  void verifyAnalysis() const override;
202  bool doFinalization(Module &) override {
203  verifyAnalysis();
204  return false;
205  }
206 
207  static char ID; // Pass identification, replacement for typeid
208 };
209 
210 } // end namespace llvm
211 
212 #endif
DiagnosticInfoOptimizationBase::Argument NV
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
Printer pass for the AssumptionAnalysis results.
This class represents a function call, abstracting a target machine's calling convention.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of .assume calls within a function.
bool doFinalization(Module &) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
#define F(x, y, z)
Definition: MD5.cpp:51
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
Function Alias Analysis false
void clear()
Clear the cache of .assume intrinsics for a function.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:283
AssumptionCache(Function &F)
Construct an AssumptionCache from a function by scanning all of its instructions. ...
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:328
MutableArrayRef< WeakVH > assumptions()
Access the list of assumption handles currently tracked for this function.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
A function analysis which provides an AssumptionCache.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
AssumptionCache run(Function &F, FunctionAnalysisManager &)
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:266
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
void updateAffectedValues(CallInst *CI)
Update the cache of values being affected by this assumption (i.e.
void shrink_and_clear()
Definition: DenseMap.h:682
void registerAssumption(CallInst *CI)
Add an .assume intrinsic to this function's cache.
MutableArrayRef< WeakVH > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
iterator end()
Definition: DenseMap.h:69
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive, key type.
Definition: DenseMap.h:146
LLVM Value Representation.
Definition: Value.h:71
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
AssumptionCache & getAssumptionCache(Function &F)
Get the cached assumptions for a function.
Value handle with callbacks on RAUW and destruction.
Definition: ValueHandle.h:333
A container for analyses that lazily runs them and caches their results.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
This header defines various interfaces for pass management in LLVM.
AssumptionPrinterPass(raw_ostream &OS)
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:64