llvm.org GIT mirror llvm / bffeba4
Remove the AssumptionCache After r289755, the AssumptionCache is no longer needed. Variables affected by assumptions are now found by using the new operand-bundle-based scheme. This new scheme is more computationally efficient, and also we need much less code... git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289756 91177308-0d34-0410-b5e6-96231b3b80d8 Hal Finkel 2 years ago
99 changed file(s) with 589 addition(s) and 1375 deletion(s). Raw diff Collapse all Expand all
+0
-168
include/llvm/Analysis/AssumptionCache.h less more
None //===- llvm/Analysis/AssumptionCache.h - Track @llvm.assume ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a pass that keeps track of @llvm.assume intrinsics in
10 // the functions of a module (allowing assumptions within any function to be
11 // found cheaply by other parts of the optimizer).
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_ASSUMPTIONCACHE_H
16 #define LLVM_ANALYSIS_ASSUMPTIONCACHE_H
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/Pass.h"
26 #include
27
28 namespace llvm {
29
30 /// \brief A cache of @llvm.assume calls within a function.
31 ///
32 /// This cache provides fast lookup of assumptions within a function by caching
33 /// them and amortizing the cost of scanning for them across all queries. The
34 /// cache is also conservatively self-updating so that it will never return
35 /// incorrect results about a function even as the function is being mutated.
36 /// However, flushing the cache and rebuilding it (or explicitly updating it)
37 /// may allow it to discover new assumptions.
38 class AssumptionCache {
39 /// \brief The function for which this cache is handling assumptions.
40 ///
41 /// We track this to lazily populate our assumptions.
42 Function &F;
43
44 /// \brief Vector of weak value handles to calls of the @llvm.assume
45 /// intrinsic.
46 SmallVector AssumeHandles;
47
48 /// \brief Flag tracking whether we have scanned the function yet.
49 ///
50 /// We want to be as lazy about this as possible, and so we scan the function
51 /// at the last moment.
52 bool Scanned;
53
54 /// \brief Scan the function for assumptions and add them to the cache.
55 void scanFunction();
56
57 public:
58 /// \brief Construct an AssumptionCache from a function by scanning all of
59 /// its instructions.
60 AssumptionCache(Function &F) : F(F), Scanned(false) {}
61
62 /// \brief Add an @llvm.assume intrinsic to this function's cache.
63 ///
64 /// The call passed in must be an instruction within this function and must
65 /// not already be in the cache.
66 void registerAssumption(CallInst *CI);
67
68 /// \brief Clear the cache of @llvm.assume intrinsics for a function.
69 ///
70 /// It will be re-scanned the next time it is requested.
71 void clear() {
72 AssumeHandles.clear();
73 Scanned = false;
74 }
75
76 /// \brief Access the list of assumption handles currently tracked for this
77 /// function.
78 ///
79 /// Note that these produce weak handles that may be null. The caller must
80 /// handle that case.
81 /// FIXME: We should replace this with pointee_iterator>
82 /// when we can write that to filter out the null values. Then caller code
83 /// will become simpler.
84 MutableArrayRef assumptions() {
85 if (!Scanned)
86 scanFunction();
87 return AssumeHandles;
88 }
89 };
90
91 /// \brief A function analysis which provides an \c AssumptionCache.
92 ///
93 /// This analysis is intended for use with the new pass manager and will vend
94 /// assumption caches for a given function.
95 class AssumptionAnalysis : public AnalysisInfoMixin {
96 friend AnalysisInfoMixin;
97 static AnalysisKey Key;
98
99 public:
100 typedef AssumptionCache Result;
101
102 AssumptionCache run(Function &F, FunctionAnalysisManager &) {
103 return AssumptionCache(F);
104 }
105 };
106
107 /// \brief Printer pass for the \c AssumptionAnalysis results.
108 class AssumptionPrinterPass : public PassInfoMixin {
109 raw_ostream &OS;
110
111 public:
112 explicit AssumptionPrinterPass(raw_ostream &OS) : OS(OS) {}
113 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
114 };
115
116 /// \brief An immutable pass that tracks lazily created \c AssumptionCache
117 /// objects.
118 ///
119 /// This is essentially a workaround for the legacy pass manager's weaknesses
120 /// which associates each assumption cache with Function and clears it if the
121 /// function is deleted. The nature of the AssumptionCache is that it is not
122 /// invalidated by any changes to the function body and so this is sufficient
123 /// to be conservatively correct.
124 class AssumptionCacheTracker : public ImmutablePass {
125 /// A callback value handle applied to function objects, which we use to
126 /// delete our cache of intrinsics for a function when it is deleted.
127 class FunctionCallbackVH final : public CallbackVH {
128 AssumptionCacheTracker *ACT;
129 void deleted() override;
130
131 public:
132 typedef DenseMapInfo DMI;
133
134 FunctionCallbackVH(Value *V, AssumptionCacheTracker *ACT = nullptr)
135 : CallbackVH(V), ACT(ACT) {}
136 };
137
138 friend FunctionCallbackVH;
139
140 typedef DenseMap,
141 FunctionCallbackVH::DMI> FunctionCallsMap;
142 FunctionCallsMap AssumptionCaches;
143
144 public:
145 /// \brief Get the cached assumptions for a function.
146 ///
147 /// If no assumptions are cached, this will scan the function. Otherwise, the
148 /// existing cache will be returned.
149 AssumptionCache &getAssumptionCache(Function &F);
150
151 AssumptionCacheTracker();
152 ~AssumptionCacheTracker() override;
153
154 void releaseMemory() override { AssumptionCaches.shrink_and_clear(); }
155
156 void verifyAnalysis() const override;
157 bool doFinalization(Module &) override {
158 verifyAnalysis();
159 return false;
160 }
161
162 static char ID; // Pass identification, replacement for typeid
163 };
164
165 } // end namespace llvm
166
167 #endif
1515
1616 #include "llvm/ADT/SmallPtrSet.h"
1717 #include "llvm/Analysis/AliasAnalysis.h"
18 #include "llvm/Analysis/AssumptionCache.h"
1918 #include "llvm/Analysis/TargetLibraryInfo.h"
2019 #include "llvm/IR/Function.h"
2120 #include "llvm/IR/GetElementPtrTypeIterator.h"
2625 #include "llvm/Support/ErrorHandling.h"
2726
2827 namespace llvm {
29 class AssumptionCache;
3028 class DominatorTree;
3129 class LoopInfo;
3230
4038
4139 const DataLayout &DL;
4240 const TargetLibraryInfo &TLI;
43 AssumptionCache ∾
4441 DominatorTree *DT;
4542 LoopInfo *LI;
4643
4744 public:
4845 BasicAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI,
49 AssumptionCache &AC, DominatorTree *DT = nullptr,
46 DominatorTree *DT = nullptr,
5047 LoopInfo *LI = nullptr)
51 : AAResultBase(), DL(DL), TLI(TLI), AC(AC), DT(DT), LI(LI) {}
48 : AAResultBase(), DL(DL), TLI(TLI), DT(DT), LI(LI) {}
5249
5350 BasicAAResult(const BasicAAResult &Arg)
54 : AAResultBase(Arg), DL(Arg.DL), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
51 : AAResultBase(Arg), DL(Arg.DL), TLI(Arg.TLI), DT(Arg.DT),
5552 LI(Arg.LI) {}
5653 BasicAAResult(BasicAAResult &&Arg)
57 : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI), AC(Arg.AC),
54 : AAResultBase(std::move(Arg)), DL(Arg.DL), TLI(Arg.TLI),
5855 DT(Arg.DT), LI(Arg.LI) {}
5956
6057 AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
144141 static const Value *
145142 GetLinearExpression(const Value *V, APInt &Scale, APInt &Offset,
146143 unsigned &ZExtBits, unsigned &SExtBits,
147 const DataLayout &DL, unsigned Depth, AssumptionCache *AC,
144 const DataLayout &DL, unsigned Depth,
148145 DominatorTree *DT, bool &NSW, bool &NUW);
149146
150147 static bool DecomposeGEPExpression(const Value *V, DecomposedGEP &Decomposed,
151 const DataLayout &DL, AssumptionCache *AC, DominatorTree *DT);
148 const DataLayout &DL, DominatorTree *DT);
152149
153150 static bool isGEPBaseAtNegativeOffset(const GEPOperator *GEPOp,
154151 const DecomposedGEP &DecompGEP, const DecomposedGEP &DecompObject,
165162 bool
166163 constantOffsetHeuristic(const SmallVectorImpl &VarIndices,
167164 uint64_t V1Size, uint64_t V2Size, int64_t BaseOffset,
168 AssumptionCache *AC, DominatorTree *DT);
165 DominatorTree *DT);
169166
170167 bool isValueEqualInPotentialCycles(const Value *V1, const Value *V2);
171168
1919 #include "llvm/IR/CallSite.h"
2020
2121 namespace llvm {
22 class AssumptionCache;
2322 class BasicBlock;
2423 class Loop;
2524 class Function;
9089
9190 /// \brief Collect a loop's ephemeral values (those used only by an assume
9291 /// or similar intrinsics in the loop).
93 static void collectEphemeralValues(const Loop *L, AssumptionCache *AC,
92 static void collectEphemeralValues(const Loop *L,
9493 SmallPtrSetImpl &EphValues);
9594
9695 /// \brief Collect a functions's ephemeral values (those used only by an
9796 /// assume or similar intrinsics in the function).
98 static void collectEphemeralValues(const Function *L, AssumptionCache *AC,
97 static void collectEphemeralValues(const Function *L,
9998 SmallPtrSetImpl &EphValues);
10099 };
101100
3333 class Function;
3434 class Instruction;
3535 class DominatorTree;
36 class AssumptionCache;
3736
3837 class DemandedBits {
3938 public:
40 DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
41 F(F), AC(AC), DT(DT), Analyzed(false) {}
39 DemandedBits(Function &F, DominatorTree &DT) :
40 F(F), DT(DT), Analyzed(false) {}
4241
4342 /// Return the bits demanded from instruction I.
4443 APInt getDemandedBits(Instruction *I);
5049
5150 private:
5251 Function &F;
53 AssumptionCache ∾
5452 DominatorTree &DT;
5553
5654 void performAnalysis();
2121
2222 namespace llvm {
2323
24 class AssumptionCache;
2524 class DominatorTree;
2625 class Instruction;
2726 class Value;
9392 class IVUsers {
9493 friend class IVStrideUse;
9594 Loop *L;
96 AssumptionCache *AC;
9795 LoopInfo *LI;
9896 DominatorTree *DT;
9997 ScalarEvolution *SE;
107105 SmallPtrSet EphValues;
108106
109107 public:
110 IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
108 IVUsers(Loop *L, LoopInfo *LI, DominatorTree *DT,
111109 ScalarEvolution *SE);
112110
113111 IVUsers(IVUsers &&X)
114 : L(std::move(X.L)), AC(std::move(X.AC)), DT(std::move(X.DT)),
112 : L(std::move(X.L)), DT(std::move(X.DT)),
115113 SE(std::move(X.SE)), Processed(std::move(X.Processed)),
116114 IVUses(std::move(X.IVUses)), EphValues(std::move(X.EphValues)) {
117115 for (IVStrideUse &U : IVUses)
1414 #define LLVM_ANALYSIS_INLINECOST_H
1515
1616 #include "llvm/Analysis/CallGraphSCCPass.h"
17 #include "llvm/Analysis/AssumptionCache.h"
1817 #include
1918 #include
2019
2120 namespace llvm {
22 class AssumptionCacheTracker;
2321 class CallSite;
2422 class DataLayout;
2523 class Function;
169167 InlineCost
170168 getInlineCost(CallSite CS, const InlineParams &Params,
171169 TargetTransformInfo &CalleeTTI,
172 std::function &GetAssumptionCache,
173170 ProfileSummaryInfo *PSI);
174171
175172 /// \brief Get an InlineCost with the callee explicitly specified.
180177 InlineCost
181178 getInlineCost(CallSite CS, Function *Callee, const InlineParams &Params,
182179 TargetTransformInfo &CalleeTTI,
183 std::function &GetAssumptionCache,
184180 ProfileSummaryInfo *PSI);
185181
186182 /// \brief Minimal filter to detect invalid constructs for inlining.
3636 namespace llvm {
3737 template
3838 class ArrayRef;
39 class AssumptionCache;
4039 class DominatorTree;
4140 class Instruction;
4241 class DataLayout;
5049 const DataLayout &DL,
5150 const TargetLibraryInfo *TLI = nullptr,
5251 const DominatorTree *DT = nullptr,
53 AssumptionCache *AC = nullptr,
5452 const Instruction *CxtI = nullptr);
5553
5654 /// Given operands for a Sub, fold the result or return null.
5856 const DataLayout &DL,
5957 const TargetLibraryInfo *TLI = nullptr,
6058 const DominatorTree *DT = nullptr,
61 AssumptionCache *AC = nullptr,
6259 const Instruction *CxtI = nullptr);
6360
6461 /// Given operands for an FAdd, fold the result or return null.
6663 const DataLayout &DL,
6764 const TargetLibraryInfo *TLI = nullptr,
6865 const DominatorTree *DT = nullptr,
69 AssumptionCache *AC = nullptr,
7066 const Instruction *CxtI = nullptr);
7167
7268 /// Given operands for an FSub, fold the result or return null.
7470 const DataLayout &DL,
7571 const TargetLibraryInfo *TLI = nullptr,
7672 const DominatorTree *DT = nullptr,
77 AssumptionCache *AC = nullptr,
7873 const Instruction *CxtI = nullptr);
7974
8075 /// Given operands for an FMul, fold the result or return null.
8277 const DataLayout &DL,
8378 const TargetLibraryInfo *TLI = nullptr,
8479 const DominatorTree *DT = nullptr,
85 AssumptionCache *AC = nullptr,
8680 const Instruction *CxtI = nullptr);
8781
8882 /// Given operands for a Mul, fold the result or return null.
8983 Value *SimplifyMulInst(Value *LHS, Value *RHS, const DataLayout &DL,
9084 const TargetLibraryInfo *TLI = nullptr,
9185 const DominatorTree *DT = nullptr,
92 AssumptionCache *AC = nullptr,
9386 const Instruction *CxtI = nullptr);
9487
9588 /// Given operands for an SDiv, fold the result or return null.
9689 Value *SimplifySDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
9790 const TargetLibraryInfo *TLI = nullptr,
9891 const DominatorTree *DT = nullptr,
99 AssumptionCache *AC = nullptr,
10092 const Instruction *CxtI = nullptr);
10193
10294 /// Given operands for a UDiv, fold the result or return null.
10395 Value *SimplifyUDivInst(Value *LHS, Value *RHS, const DataLayout &DL,
10496 const TargetLibraryInfo *TLI = nullptr,
10597 const DominatorTree *DT = nullptr,
106 AssumptionCache *AC = nullptr,
10798 const Instruction *CxtI = nullptr);
10899
109100 /// Given operands for an FDiv, fold the result or return null.
111102 const DataLayout &DL,
112103 const TargetLibraryInfo *TLI = nullptr,
113104 const DominatorTree *DT = nullptr,
114 AssumptionCache *AC = nullptr,
115105 const Instruction *CxtI = nullptr);
116106
117107 /// Given operands for an SRem, fold the result or return null.
118108 Value *SimplifySRemInst(Value *LHS, Value *RHS, const DataLayout &DL,
119109 const TargetLibraryInfo *TLI = nullptr,
120110 const DominatorTree *DT = nullptr,
121 AssumptionCache *AC = nullptr,
122111 const Instruction *CxtI = nullptr);
123112
124113 /// Given operands for a URem, fold the result or return null.
125114 Value *SimplifyURemInst(Value *LHS, Value *RHS, const DataLayout &DL,
126115 const TargetLibraryInfo *TLI = nullptr,
127116 const DominatorTree *DT = nullptr,
128 AssumptionCache *AC = nullptr,
129117 const Instruction *CxtI = nullptr);
130118
131119 /// Given operands for an FRem, fold the result or return null.
133121 const DataLayout &DL,
134122 const TargetLibraryInfo *TLI = nullptr,
135123 const DominatorTree *DT = nullptr,
136 AssumptionCache *AC = nullptr,
137124 const Instruction *CxtI = nullptr);
138125
139126 /// Given operands for a Shl, fold the result or return null.
141128 const DataLayout &DL,
142129 const TargetLibraryInfo *TLI = nullptr,
143130 const DominatorTree *DT = nullptr,
144 AssumptionCache *AC = nullptr,
145131 const Instruction *CxtI = nullptr);
146132
147133 /// Given operands for a LShr, fold the result or return null.
149135 const DataLayout &DL,
150136 const TargetLibraryInfo *TLI = nullptr,
151137 const DominatorTree *DT = nullptr,
152 AssumptionCache *AC = nullptr,
153138 const Instruction *CxtI = nullptr);
154139
155140 /// Given operands for a AShr, fold the result or return nulll.
157142 const DataLayout &DL,
158143 const TargetLibraryInfo *TLI = nullptr,
159144 const DominatorTree *DT = nullptr,
160 AssumptionCache *AC = nullptr,
161145 const Instruction *CxtI = nullptr);
162146
163147 /// Given operands for an And, fold the result or return null.
164148 Value *SimplifyAndInst(Value *LHS, Value *RHS, const DataLayout &DL,
165149 const TargetLibraryInfo *TLI = nullptr,
166150 const DominatorTree *DT = nullptr,
167 AssumptionCache *AC = nullptr,
168151 const Instruction *CxtI = nullptr);
169152
170153 /// Given operands for an Or, fold the result or return null.
171154 Value *SimplifyOrInst(Value *LHS, Value *RHS, const DataLayout &DL,
172155 const TargetLibraryInfo *TLI = nullptr,
173156 const DominatorTree *DT = nullptr,
174 AssumptionCache *AC = nullptr,
175157 const Instruction *CxtI = nullptr);
176158
177159 /// Given operands for an Xor, fold the result or return null.
178160 Value *SimplifyXorInst(Value *LHS, Value *RHS, const DataLayout &DL,
179161 const TargetLibraryInfo *TLI = nullptr,
180162 const DominatorTree *DT = nullptr,
181 AssumptionCache *AC = nullptr,
182163 const Instruction *CxtI = nullptr);
183164
184165 /// Given operands for an ICmpInst, fold the result or return null.
186167 const DataLayout &DL,
187168 const TargetLibraryInfo *TLI = nullptr,
188169 const DominatorTree *DT = nullptr,
189 AssumptionCache *AC = nullptr,
190170 const Instruction *CxtI = nullptr);
191171
192172 /// Given operands for an FCmpInst, fold the result or return null.
194174 FastMathFlags FMF, const DataLayout &DL,
195175 const TargetLibraryInfo *TLI = nullptr,
196176 const DominatorTree *DT = nullptr,
197 AssumptionCache *AC = nullptr,
198177 const Instruction *CxtI = nullptr);
199178
200179 /// Given operands for a SelectInst, fold the result or return null.
202181 const DataLayout &DL,
203182 const TargetLibraryInfo *TLI = nullptr,
204183 const DominatorTree *DT = nullptr,
205 AssumptionCache *AC = nullptr,
206184 const Instruction *CxtI = nullptr);
207185
208186 /// Given operands for a GetElementPtrInst, fold the result or return null.
210188 const DataLayout &DL,
211189 const TargetLibraryInfo *TLI = nullptr,
212190 const DominatorTree *DT = nullptr,
213 AssumptionCache *AC = nullptr,
214191 const Instruction *CxtI = nullptr);
215192
216193 /// Given operands for an InsertValueInst, fold the result or return null.
218195 ArrayRef Idxs, const DataLayout &DL,
219196 const TargetLibraryInfo *TLI = nullptr,
220197 const DominatorTree *DT = nullptr,
221 AssumptionCache *AC = nullptr,
222198 const Instruction *CxtI = nullptr);
223199
224200 /// Given operands for an ExtractValueInst, fold the result or return null.
226202 const DataLayout &DL,
227203 const TargetLibraryInfo *TLI = nullptr,
228204 const DominatorTree *DT = nullptr,
229 AssumptionCache *AC = nullptr,
230205 const Instruction *CxtI = nullptr);
231206
232207 /// Given operands for an ExtractElementInst, fold the result or return null.
234209 const DataLayout &DL,
235210 const TargetLibraryInfo *TLI = nullptr,
236211 const DominatorTree *DT = nullptr,
237 AssumptionCache *AC = nullptr,
238212 const Instruction *CxtI = nullptr);
239213
240214 /// Given operands for a CastInst, fold the result or return null.
242216 const DataLayout &DL,
243217 const TargetLibraryInfo *TLI = nullptr,
244218 const DominatorTree *DT = nullptr,
245 AssumptionCache *AC = nullptr,
246219 const Instruction *CxtI = nullptr);
247220
248221 //=== Helper functions for higher up the class hierarchy.
253226 const DataLayout &DL,
254227 const TargetLibraryInfo *TLI = nullptr,
255228 const DominatorTree *DT = nullptr,
256 AssumptionCache *AC = nullptr,
257229 const Instruction *CxtI = nullptr);
258230
259231 /// Given operands for a BinaryOperator, fold the result or return null.
261233 const DataLayout &DL,
262234 const TargetLibraryInfo *TLI = nullptr,
263235 const DominatorTree *DT = nullptr,
264 AssumptionCache *AC = nullptr,
265236 const Instruction *CxtI = nullptr);
266237
267238 /// Given operands for an FP BinaryOperator, fold the result or return null.
271242 const FastMathFlags &FMF, const DataLayout &DL,
272243 const TargetLibraryInfo *TLI = nullptr,
273244 const DominatorTree *DT = nullptr,
274 AssumptionCache *AC = nullptr,
275245 const Instruction *CxtI = nullptr);
276246
277247 /// Given a function and iterators over arguments, fold the result or return
280250 User::op_iterator ArgEnd, const DataLayout &DL,
281251 const TargetLibraryInfo *TLI = nullptr,
282252 const DominatorTree *DT = nullptr,
283 AssumptionCache *AC = nullptr,
284253 const Instruction *CxtI = nullptr);
285254
286255 /// Given a function and set of arguments, fold the result or return null.
287256 Value *SimplifyCall(Value *V, ArrayRef Args, const DataLayout &DL,
288257 const TargetLibraryInfo *TLI = nullptr,
289258 const DominatorTree *DT = nullptr,
290 AssumptionCache *AC = nullptr,
291259 const Instruction *CxtI = nullptr);
292260
293261 /// See if we can compute a simplified version of this instruction. If not,
294262 /// return null.
295263 Value *SimplifyInstruction(Instruction *I, const DataLayout &DL,
296264 const TargetLibraryInfo *TLI = nullptr,
297 const DominatorTree *DT = nullptr,
298 AssumptionCache *AC = nullptr);
265 const DominatorTree *DT = nullptr);
299266
300267 /// Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
301268 ///
306273 /// The function returns true if any simplifications were performed.
307274 bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
308275 const TargetLibraryInfo *TLI = nullptr,
309 const DominatorTree *DT = nullptr,
310 AssumptionCache *AC = nullptr);
276 const DominatorTree *DT = nullptr);
311277
312278 /// Recursively attempt to simplify an instruction.
313279 ///
317283 /// performed.
318284 bool recursivelySimplifyInstruction(Instruction *I,
319285 const TargetLibraryInfo *TLI = nullptr,
320 const DominatorTree *DT = nullptr,
321 AssumptionCache *AC = nullptr);
286 const DominatorTree *DT = nullptr);
322287 } // end namespace llvm
323288
324289 #endif
1818 #include "llvm/Pass.h"
1919
2020 namespace llvm {
21 class AssumptionCache;
2221 class Constant;
2322 class ConstantRange;
2423 class DataLayout;
3029 /// This pass computes, caches, and vends lazy value constraint information.
3130 class LazyValueInfo {
3231 friend class LazyValueInfoWrapperPass;
33 AssumptionCache *AC = nullptr;
3432 class TargetLibraryInfo *TLI = nullptr;
3533 DominatorTree *DT = nullptr;
3634 void *PImpl = nullptr;
3937 public:
4038 ~LazyValueInfo();
4139 LazyValueInfo() {}
42 LazyValueInfo(AssumptionCache *AC_, TargetLibraryInfo *TLI_,
40 LazyValueInfo(TargetLibraryInfo *TLI_,
4341 DominatorTree *DT_)
44 : AC(AC_), TLI(TLI_), DT(DT_) {}
42 : TLI(TLI_), DT(DT_) {}
4543 LazyValueInfo(LazyValueInfo &&Arg)
46 : AC(Arg.AC), TLI(Arg.TLI), DT(Arg.DT), PImpl(Arg.PImpl) {
44 : TLI(Arg.TLI), DT(Arg.DT), PImpl(Arg.PImpl) {
4745 Arg.PImpl = nullptr;
4846 }
4947 LazyValueInfo &operator=(LazyValueInfo &&Arg) {
5048 releaseMemory();
51 AC = Arg.AC;
5249 TLI = Arg.TLI;
5350 DT = Arg.DT;
5451 PImpl = Arg.PImpl;
2929 class FunctionPass;
3030 class Instruction;
3131 class CallSite;
32 class AssumptionCache;
3332 class MemoryDependenceResults;
3433 class PredIteratorCache;
3534 class DominatorTree;
338337
339338 /// Current AA implementation, just a cache.
340339 AliasAnalysis &AA;
341 AssumptionCache ∾
342340 const TargetLibraryInfo &TLI;
343341 DominatorTree &DT;
344342 PredIteratorCache PredCache;
345343
346344 public:
347 MemoryDependenceResults(AliasAnalysis &AA, AssumptionCache &AC,
345 MemoryDependenceResults(AliasAnalysis &AA,
348346 const TargetLibraryInfo &TLI,
349347 DominatorTree &DT)
350 : AA(AA), AC(AC), TLI(TLI), DT(DT) {}
348 : AA(AA), TLI(TLI), DT(DT) {}
351349
352350 /// Some methods limit the number of instructions they will examine.
353351 /// The return value of this method is the default limit that will be
1717 #include "llvm/IR/Instruction.h"
1818
1919 namespace llvm {
20 class AssumptionCache;
2120 class DominatorTree;
2221 class DataLayout;
2322 class TargetLibraryInfo;
4241 /// TLI - The target library info if known, otherwise null.
4342 const TargetLibraryInfo *TLI;
4443
45 /// A cache of @llvm.assume calls used by SimplifyInstruction.
46 AssumptionCache *AC;
47
4844 /// InstInputs - The inputs for our symbolic address.
4945 SmallVector InstInputs;
5046
5147 public:
52 PHITransAddr(Value *addr, const DataLayout &DL, AssumptionCache *AC)
53 : Addr(addr), DL(DL), TLI(nullptr), AC(AC) {
48 PHITransAddr(Value *addr, const DataLayout &DL)
49 : Addr(addr), DL(DL), TLI(nullptr) {
5450 // If the address is an instruction, the whole thing is considered an input.
5551 if (Instruction *I = dyn_cast(Addr))
5652 InstInputs.push_back(I);
3636
3737 namespace llvm {
3838 class APInt;
39 class AssumptionCache;
4039 class Constant;
4140 class ConstantInt;
4241 class DominatorTree;
473472 /// The target library information for the target we are targeting.
474473 ///
475474 TargetLibraryInfo &TLI;
476
477 /// The tracker for @llvm.assume intrinsics in this function.
478 AssumptionCache ∾
479475
480476 /// The dominator tree.
481477 ///
11131109 bool isAddRecNeverPoison(const Instruction *I, const Loop *L);
11141110
11151111 public:
1116 ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC,
1112 ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
11171113 DominatorTree &DT, LoopInfo &LI);
11181114 ~ScalarEvolution();
11191115 ScalarEvolution(ScalarEvolution &&Arg);
2323 template class ArrayRef;
2424 class APInt;
2525 class AddOperator;
26 class AssumptionCache;
2726 class DataLayout;
2827 class DominatorTree;
2928 class GEPOperator;
4948 /// for all of the elements in the vector.
5049 void computeKnownBits(const Value *V, APInt &KnownZero, APInt &KnownOne,
5150 const DataLayout &DL, unsigned Depth = 0,
52 AssumptionCache *AC = nullptr,
5351 const Instruction *CxtI = nullptr,
5452 const DominatorTree *DT = nullptr);
5553 /// Compute known bits from the range metadata.
6058 /// Return true if LHS and RHS have no common bits set.
6159 bool haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
6260 const DataLayout &DL,
63 AssumptionCache *AC = nullptr,
6461 const Instruction *CxtI = nullptr,
6562 const DominatorTree *DT = nullptr);
6663
6865 /// wrapper around computeKnownBits.
6966 void ComputeSignBit(const Value *V, bool &KnownZero, bool &KnownOne,
7067 const DataLayout &DL, unsigned Depth = 0,
71 AssumptionCache *AC = nullptr,
7268 const Instruction *CxtI = nullptr,
7369 const DominatorTree *DT = nullptr);
7470
7975 /// value is either a power of two or zero.
8076 bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
8177 bool OrZero = false, unsigned Depth = 0,
82 AssumptionCache *AC = nullptr,
8378 const Instruction *CxtI = nullptr,
8479 const DominatorTree *DT = nullptr);
8580
8883 /// defined. Supports values with integer or pointer type and vectors of
8984 /// integers.
9085 bool isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth = 0,
91 AssumptionCache *AC = nullptr,
9286 const Instruction *CxtI = nullptr,
9387 const DominatorTree *DT = nullptr);
9488
9589 /// Returns true if the give value is known to be non-negative.
9690 bool isKnownNonNegative(const Value *V, const DataLayout &DL,
9791 unsigned Depth = 0,
98 AssumptionCache *AC = nullptr,
9992 const Instruction *CxtI = nullptr,
10093 const DominatorTree *DT = nullptr);
10194
10295 /// Returns true if the given value is known be positive (i.e. non-negative
10396 /// and non-zero).
10497 bool isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth = 0,
105 AssumptionCache *AC = nullptr,
10698 const Instruction *CxtI = nullptr,
10799 const DominatorTree *DT = nullptr);
108100
109101 /// Returns true if the given value is known be negative (i.e. non-positive
110102 /// and non-zero).
111103 bool isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth = 0,
112 AssumptionCache *AC = nullptr,
113104 const Instruction *CxtI = nullptr,
114105 const DominatorTree *DT = nullptr);
115106
116107 /// Return true if the given values are known to be non-equal when defined.
117108 /// Supports scalar integer types only.
118109 bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL,
119 AssumptionCache *AC = nullptr,
120110 const Instruction *CxtI = nullptr,
121111 const DominatorTree *DT = nullptr);
122112
131121 /// for all of the elements in the vector.
132122 bool MaskedValueIsZero(const Value *V, const APInt &Mask,
133123 const DataLayout &DL,
134 unsigned Depth = 0, AssumptionCache *AC = nullptr,
124 unsigned Depth = 0,
135125 const Instruction *CxtI = nullptr,
136126 const DominatorTree *DT = nullptr);
137127
143133 /// sign bits for the vector element with the mininum number of known sign
144134 /// bits.
145135 unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL,
146 unsigned Depth = 0, AssumptionCache *AC = nullptr,
136 unsigned Depth = 0,
147137 const Instruction *CxtI = nullptr,
148138 const DominatorTree *DT = nullptr);
149139
324314 OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
325315 const Value *RHS,
326316 const DataLayout &DL,
327 AssumptionCache *AC,
328317 const Instruction *CxtI,
329318 const DominatorTree *DT);
330319 OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
331320 const Value *RHS,
332321 const DataLayout &DL,
333 AssumptionCache *AC,
334322 const Instruction *CxtI,
335323 const DominatorTree *DT);
336324 OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS,
337325 const DataLayout &DL,
338 AssumptionCache *AC = nullptr,
339326 const Instruction *CxtI = nullptr,
340327 const DominatorTree *DT = nullptr);
341328 /// This version also leverages the sign bit of Add if known.
342329 OverflowResult computeOverflowForSignedAdd(const AddOperator *Add,
343330 const DataLayout &DL,
344 AssumptionCache *AC = nullptr,
345331 const Instruction *CxtI = nullptr,
346332 const DominatorTree *DT = nullptr);
347333
474460 const DataLayout &DL,
475461 bool InvertAPred = false,
476462 unsigned Depth = 0,
477 AssumptionCache *AC = nullptr,
478463 const Instruction *CxtI = nullptr,
479464 const DominatorTree *DT = nullptr);
480465 } // end namespace llvm
6767 void initializeAlignmentFromAssumptionsPass(PassRegistry&);
6868 void initializeAlwaysInlinerLegacyPassPass(PassRegistry&);
6969 void initializeArgPromotionPass(PassRegistry&);
70 void initializeAssumptionCacheTrackerPass(PassRegistry &);
7170 void initializeAtomicExpandPass(PassRegistry&);
7271 void initializeBBVectorizePass(PassRegistry&);
7372 void initializeBDCELegacyPassPass(PassRegistry &);
2222 #include "llvm/Transforms/Utils/ImportedFunctionsInliningStatistics.h"
2323
2424 namespace llvm {
25 class AssumptionCacheTracker;
2625 class CallSite;
2726 class DataLayout;
2827 class InlineCost;
7675 bool InsertLifetime;
7776
7877 protected:
79 AssumptionCacheTracker *ACT;
8078 ProfileSummaryInfo *PSI;
8179 ImportedFunctionsInliningStatistics ImportedFunctionsStats;
8280 };
2929 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
3030
3131 // Glue for old PM.
32 bool runImpl(Function &F, AssumptionCache &AC, ScalarEvolution *SE_,
33 DominatorTree *DT_);
32 bool runImpl(Function &F, ScalarEvolution *SE_, DominatorTree *DT_);
3433
3534 // For memory transfers, we need a common alignment for both the source and
3635 // destination. If we have a new alignment for only one operand of a transfer
2020 #include "llvm/ADT/SetVector.h"
2121 #include "llvm/ADT/SmallPtrSet.h"
2222 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AssumptionCache.h"
2423 #include "llvm/Analysis/LoopInfo.h"
2524 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
2625 #include "llvm/IR/Dominators.h"
108107 MemoryDependenceResults *MD;
109108 DominatorTree *DT;
110109 const TargetLibraryInfo *TLI;
111 AssumptionCache *AC;
112110 SetVector DeadBlocks;
113111 OptimizationRemarkEmitter *ORE;
114112
134132 typedef SmallVector AvailValInBlkVect;
135133 typedef SmallVector UnavailBlkVect;
136134
137 bool runImpl(Function &F, AssumptionCache &RunAC, DominatorTree &RunDT,
135 bool runImpl(Function &F, DominatorTree &RunDT,
138136 const TargetLibraryInfo &RunTLI, AAResults &RunAA,
139137 MemoryDependenceResults *RunMD, LoopInfo *LI,
140138 OptimizationRemarkEmitter *ORE);
1616
1717 #include "llvm/ADT/STLExtras.h"
1818 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/AssumptionCache.h"
2019 #include "llvm/Analysis/GlobalsModRef.h"
2120 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
2221 #include "llvm/Analysis/TargetLibraryInfo.h"
3231 MemoryDependenceResults *MD = nullptr;
3332 TargetLibraryInfo *TLI = nullptr;
3433 std::function LookupAliasAnalysis;
35 std::function LookupAssumptionCache;
3634 std::function LookupDomTree;
3735
3836 public:
4240 bool runImpl(Function &F, MemoryDependenceResults *MD_,
4341 TargetLibraryInfo *TLI_,
4442 std::function LookupAliasAnalysis_,
45 std::function LookupAssumptionCache_,
4643 std::function LookupDomTree_);
4744
4845 private:
8080
8181 #include "llvm/ADT/DenseMap.h"
8282 #include "llvm/ADT/SmallVector.h"
83 #include "llvm/Analysis/AssumptionCache.h"
8483 #include "llvm/Analysis/ScalarEvolution.h"
8584 #include "llvm/Analysis/TargetLibraryInfo.h"
8685 #include "llvm/Analysis/TargetTransformInfo.h"
9493 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
9594
9695 // Glue for old PM.
97 bool runImpl(Function &F, AssumptionCache *AC_, DominatorTree *DT_,
96 bool runImpl(Function &F, DominatorTree *DT_,
9897 ScalarEvolution *SE_, TargetLibraryInfo *TLI_,
9998 TargetTransformInfo *TTI_);
10099
151150 // to be an index of GEP.
152151 bool requiresSignExtension(Value *Index, GetElementPtrInst *GEP);
153152
154 AssumptionCache *AC;
155153 const DataLayout *DL;
156154 DominatorTree *DT;
157155 ScalarEvolution *SE;
1616 #define LLVM_TRANSFORMS_SCALAR_SROA_H
1717
1818 #include "llvm/ADT/SetVector.h"
19 #include "llvm/Analysis/AssumptionCache.h"
2019 #include "llvm/IR/Dominators.h"
2120 #include "llvm/IR/Function.h"
2221 #include "llvm/IR/PassManager.h"
2322
2423 namespace llvm {
24 class AllocaInst;
25 class SelectInst;
26 class PHINode;
2527
2628 /// A private "module" namespace for types and utilities used by SROA. These
2729 /// are implementation details and should not be used by clients.
5355 class SROA : public PassInfoMixin {
5456 LLVMContext *C;
5557 DominatorTree *DT;
56 AssumptionCache *AC;
5758
5859 /// \brief Worklist of alloca instructions to simplify.
5960 ///
9899 SetVector> SpeculatableSelects;
99100
100101 public:
101 SROA() : C(nullptr), DT(nullptr), AC(nullptr) {}
102 SROA() : C(nullptr), DT(nullptr) {}
102103
103104 /// \brief Run the pass over the function.
104105 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
108109 friend class sroa::SROALegacyPass;
109110
110111 /// Helper used by both the public run method and by the legacy pass.
111 PreservedAnalyses runImpl(Function &F, DominatorTree &RunDT,
112 AssumptionCache &RunAC);
112 PreservedAnalyses runImpl(Function &F, DominatorTree &RunDT);
113113
114114 bool presplitLoadsAndStores(AllocaInst &AI, sroa::AllocaSlices &AS);
115115 AllocaInst *rewritePartition(AllocaInst &AI, sroa::AllocaSlices &AS,
2020 #include "llvm/ADT/SmallVector.h"
2121 #include "llvm/ADT/Twine.h"
2222 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AssumptionCache.h"
2423 #include "llvm/IR/ValueHandle.h"
2524 #include "llvm/IR/ValueMap.h"
2625 #include "llvm/Transforms/Utils/ValueMapper.h"
4544 class Loop;
4645 class LoopInfo;
4746 class AllocaInst;
48 class AssumptionCacheTracker;
4947 class DominatorTree;
5048
5149 /// Return an exact copy of the specified module
175173 /// InlineFunction call, and records the auxiliary results produced by it.
176174 class InlineFunctionInfo {
177175 public:
178 explicit InlineFunctionInfo(CallGraph *cg = nullptr,
179 std::function
180 *GetAssumptionCache = nullptr)
181 : CG(cg), GetAssumptionCache(GetAssumptionCache) {}
176 explicit InlineFunctionInfo(CallGraph *cg = nullptr)
177 : CG(cg) {}
182178
183179 /// CG - If non-null, InlineFunction will update the callgraph to reflect the
184180 /// changes it makes.
185181 CallGraph *CG;
186 std::function *GetAssumptionCache;
187182
188183 /// StaticAllocas - InlineFunction fills this in with all static allocas that
189184 /// get copied into the caller.
3737 class Value;
3838 class PHINode;
3939 class AllocaInst;
40 class AssumptionCache;
4140 class ConstantExpr;
4241 class DataLayout;
4342 class TargetLibraryInfo;
136135 /// parameter, providing the set of loop header that SimplifyCFG should not
137136 /// eliminate.
138137 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
139 unsigned BonusInstThreshold, AssumptionCache *AC = nullptr,
138 unsigned BonusInstThreshold,
140139 SmallPtrSetImpl *LoopHeaders = nullptr);
141140
142141 /// This function is used to flatten a CFG. For example, it uses parallel-and
175174 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
176175 const DataLayout &DL,
177176 const Instruction *CxtI = nullptr,
178 AssumptionCache *AC = nullptr,
179177 const DominatorTree *DT = nullptr);
180178
181179 /// Try to infer an alignment for the specified pointer.
182180 static inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
183181 const Instruction *CxtI = nullptr,
184 AssumptionCache *AC = nullptr,
185182 const DominatorTree *DT = nullptr) {
186 return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
183 return getOrEnforceKnownAlignment(V, 0, DL, CxtI, DT);
187184 }
188185
189186 /// Given a getelementptr instruction/constantexpr, emit the code necessary to
3838 #ifndef LLVM_TRANSFORMS_UTILS_LOOPSIMPLIFY_H
3939 #define LLVM_TRANSFORMS_UTILS_LOOPSIMPLIFY_H
4040
41 #include "llvm/Analysis/AssumptionCache.h"
4241 #include "llvm/Analysis/ScalarEvolution.h"
4342 #include "llvm/IR/Dominators.h"
4443 #include "llvm/IR/PassManager.h"
5756 /// it into a simplified loop nest with preheaders and single backedges. It will
5857 /// update \c AliasAnalysis and \c ScalarEvolution analyses if they're non-null.
5958 bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE,
60 AssumptionCache *AC, bool PreserveLCSSA);
59 bool PreserveLCSSA);
6160
6261 } // end namespace llvm
6362
2222 namespace llvm {
2323 class AliasSet;
2424 class AliasSetTracker;
25 class AssumptionCache;
2625 class BasicBlock;
2726 class DataLayout;
2827 class DominatorTree;
962962 if (WalkingPhi && Location.Ptr) {
963963 PHITransAddr Translator(
964964 const_cast(Location.Ptr),
965 OriginalAccess->getBlock()->getModule()->getDataLayout(), nullptr);
965 OriginalAccess->getBlock()->getModule()->getDataLayout());
966966 if (!Translator.PHITranslateValue(OriginalAccess->getBlock(),
967967 DefIterator.getPhiArgBlock(), nullptr,
968968 false))
2020 class AllocaInst;
2121 class DominatorTree;
2222 class AliasSetTracker;
23 class AssumptionCache;
2423
2524 /// \brief Return true if this alloca is legal for promotion.
2625 ///
4039 /// If AST is specified, the specified tracker is updated to reflect changes
4140 /// made to the IR.
4241 void PromoteMemToReg(ArrayRef Allocas, DominatorTree &DT,
43 AliasSetTracker *AST = nullptr,
44 AssumptionCache *AC = nullptr);
42 AliasSetTracker *AST = nullptr);
4543
4644 } // End llvm namespace
4745
2222 namespace llvm {
2323
2424 class StringRef;
25 class AssumptionCache;
2625 class DominatorTree;
2726 class Loop;
2827 class LoopInfo;
3635 bool AllowRuntime, bool AllowExpensiveTripCount,
3736 bool PreserveCondBr, bool PreserveOnlyFirst,
3837 unsigned TripMultiple, unsigned PeelCount, LoopInfo *LI,
39 ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
38 ScalarEvolution *SE, DominatorTree *DT,
4039 OptimizationRemarkEmitter *ORE, bool PreserveLCSSA);
4140
4241 bool UnrollRuntimeLoopRemainder(Loop *L, unsigned Count,
5050
5151 #include "llvm/ADT/MapVector.h"
5252 #include "llvm/Analysis/AliasAnalysis.h"
53 #include "llvm/Analysis/AssumptionCache.h"
5453 #include "llvm/Analysis/BasicAliasAnalysis.h"
5554 #include "llvm/Analysis/BlockFrequencyInfo.h"
5655 #include "llvm/Analysis/DemandedBits.h"
8281 TargetLibraryInfo *TLI;
8382 DemandedBits *DB;
8483 AliasAnalysis *AA;
85 AssumptionCache *AC;
8684 std::function *GetLAA;
8785 OptimizationRemarkEmitter *ORE;
8886
9492 bool runImpl(Function &F, ScalarEvolution &SE_, LoopInfo &LI_,
9593 TargetTransformInfo &TTI_, DominatorTree &DT_,
9694 BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_,
97 DemandedBits &DB_, AliasAnalysis &AA_, AssumptionCache &AC_,
95 DemandedBits &DB_, AliasAnalysis &AA_,
9896 std::function &GetLAA_,
9997 OptimizationRemarkEmitter &ORE);
10098
2020
2121 #include "llvm/ADT/MapVector.h"
2222 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/AssumptionCache.h"
2423 #include "llvm/Analysis/DemandedBits.h"
2524 #include "llvm/Analysis/LoopInfo.h"
2625 #include "llvm/Analysis/ScalarEvolution.h"
4847 AliasAnalysis *AA = nullptr;
4948 LoopInfo *LI = nullptr;
5049 DominatorTree *DT = nullptr;
51 AssumptionCache *AC = nullptr;
5250 DemandedBits *DB = nullptr;
5351 const DataLayout *DL = nullptr;
5452
5856 // Glue for old PM.
5957 bool runImpl(Function &F, ScalarEvolution *SE_, TargetTransformInfo *TTI_,
6058 TargetLibraryInfo *TLI_, AliasAnalysis *AA_, LoopInfo *LI_,
61 DominatorTree *DT_, AssumptionCache *AC_, DemandedBits *DB_);
59 DominatorTree *DT_, DemandedBits *DB_);
6260
6361 private:
6462 /// \brief Collect store and getelementptr instructions and organize them
+0
-140
lib/Analysis/AssumptionCache.cpp less more
None //===- AssumptionCache.cpp - Cache finding @llvm.assume calls -------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a pass that keeps track of @llvm.assume intrinsics in
10 // the functions of a module.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Analysis/AssumptionCache.h"
15 #include "llvm/IR/CallSite.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/IntrinsicInst.h"
20 #include "llvm/IR/PassManager.h"
21 #include "llvm/IR/PatternMatch.h"
22 #include "llvm/Support/Debug.h"
23 using namespace llvm;
24 using namespace llvm::PatternMatch;
25
26 void AssumptionCache::scanFunction() {
27 assert(!Scanned && "Tried to scan the function twice!");
28 assert(AssumeHandles.empty() && "Already have assumes when scanning!");
29
30 // Go through all instructions in all blocks, add all calls to @llvm.assume
31 // to this cache.
32 for (BasicBlock &B : F)
33 for (Instruction &II : B)
34 if (match(&II, m_Intrinsic()))
35 AssumeHandles.push_back(&II);
36
37 // Mark the scan as complete.
38 Scanned = true;
39 }
40
41 void AssumptionCache::registerAssumption(CallInst *CI) {
42 assert(match(CI, m_Intrinsic()) &&
43 "Registered call does not call @llvm.assume");
44
45 // If we haven't scanned the function yet, just drop this assumption. It will
46 // be found when we scan later.
47 if (!Scanned)
48 return;
49
50 AssumeHandles.push_back(CI);
51
52 #ifndef NDEBUG
53 assert(CI->getParent() &&
54 "Cannot register @llvm.assume call not in a basic block");
55 assert(&F == CI->getParent()->getParent() &&
56 "Cannot register @llvm.assume call not in this function");
57
58 // We expect the number of assumptions to be small, so in an asserts build
59 // check that we don't accumulate duplicates and that all assumptions point
60 // to the same function.
61 SmallPtrSet AssumptionSet;
62 for (auto &VH : AssumeHandles) {
63 if (!VH)
64 continue;
65
66 assert(&F == cast(VH)->getParent()->getParent() &&
67 "Cached assumption not inside this function!");
68 assert(match(cast(VH), m_Intrinsic()) &&
69 "Cached something other than a call to @llvm.assume!");
70 assert(AssumptionSet.insert(VH).second &&
71 "Cache contains multiple copies of a call!");
72 }
73 #endif
74 }
75
76 AnalysisKey AssumptionAnalysis::Key;
77
78 PreservedAnalyses AssumptionPrinterPass::run(Function &F,
79 FunctionAnalysisManager &AM) {
80 AssumptionCache &AC = AM.getResult(F);
81
82 OS << "Cached assumptions for function: " << F.getName() << "\n";
83 for (auto &VH : AC.assumptions())
84 if (VH)
85 OS << " " << *cast(VH)->getArgOperand(0) << "\n";
86
87 return PreservedAnalyses::all();
88 }
89
90 void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
91 auto I = ACT->AssumptionCaches.find_as(cast(getValPtr()));
92 if (I != ACT->AssumptionCaches.end())
93 ACT->AssumptionCaches.erase(I);
94 // 'this' now dangles!
95 }
96
97 AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
98 // We probe the function map twice to try and avoid creating a value handle
99 // around the function in common cases. This makes insertion a bit slower,
100 // but if we have to insert we're going to scan the whole function so that
101 // shouldn't matter.
102 auto I = AssumptionCaches.find_as(&F);
103 if (I != AssumptionCaches.end())
104 return *I->second;
105
106 // Ok, build a new cache by scanning the function, insert it and the value
107 // handle into our map, and return the newly populated cache.
108 auto IP = AssumptionCaches.insert(std::make_pair(
109 FunctionCallbackVH(&F, this), llvm::make_unique(F)));
110 assert(IP.second && "Scanning function already in the map?");
111 return *IP.first->second;
112 }
113
114 void AssumptionCacheTracker::verifyAnalysis() const {
115 #ifndef NDEBUG
116 SmallPtrSet AssumptionSet;
117 for (const auto &I : AssumptionCaches) {
118 for (auto &VH : I.second->assumptions())
119 if (VH)
120 AssumptionSet.insert(cast(VH));
121
122 for (const BasicBlock &B : cast(*I.first))
123 for (const Instruction &II : B)
124 if (match(&II, m_Intrinsic()))
125 assert(AssumptionSet.count(cast(&II)) &&
126 "Assumption in scanned function not in cache");
127 }
128 #endif
129 }
130
131 AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
132 initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
133 }
134
135 AssumptionCacheTracker::~AssumptionCacheTracker() {}
136
137 INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
138 "Assumption Cache Tracker", false, true)
139 char AssumptionCacheTracker::ID = 0;
2222 #include "llvm/Analysis/LoopInfo.h"
2323 #include "llvm/Analysis/MemoryBuiltins.h"
2424 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Analysis/AssumptionCache.h"
2625 #include "llvm/IR/Constants.h"
2726 #include "llvm/IR/DataLayout.h"
2827 #include "llvm/IR/DerivedTypes.h"
181180 /*static*/ const Value *BasicAAResult::GetLinearExpression(
182181 const Value *V, APInt &Scale, APInt &Offset, unsigned &ZExtBits,
183182 unsigned &SExtBits, const DataLayout &DL, unsigned Depth,
184 AssumptionCache *AC, DominatorTree *DT, bool &NSW, bool &NUW) {
183 DominatorTree *DT, bool &NSW, bool &NUW) {
185184 assert(V->getType()->isIntegerTy() && "Not an integer value");
186185
187186 // Limit our recursion depth.
220219 case Instruction::Or:
221220 // X|C == X+C if all the bits in C are unset in X. Otherwise we can't
222221 // analyze it.
223 if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0, AC,
222 if (!MaskedValueIsZero(BOp->getOperand(0), RHSC->getValue(), DL, 0,
224223 BOp, DT)) {
225224 Scale = 1;
226225 Offset = 0;
229228 LLVM_FALLTHROUGH;
230229 case Instruction::Add:
231230 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
232 SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
231 SExtBits, DL, Depth + 1, DT, NSW, NUW);
233232 Offset += RHS;
234233 break;
235234 case Instruction::Sub:
236235 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
237 SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
236 SExtBits, DL, Depth + 1, DT, NSW, NUW);
238237 Offset -= RHS;
239238 break;
240239 case Instruction::Mul:
241240 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
242 SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
241 SExtBits, DL, Depth + 1, DT, NSW, NUW);
243242 Offset *= RHS;
244243 Scale *= RHS;
245244 break;
246245 case Instruction::Shl:
247246 V = GetLinearExpression(BOp->getOperand(0), Scale, Offset, ZExtBits,
248 SExtBits, DL, Depth + 1, AC, DT, NSW, NUW);
247 SExtBits, DL, Depth + 1, DT, NSW, NUW);
249248 Offset <<= RHS.getLimitedValue();
250249 Scale <<= RHS.getLimitedValue();
251250 // the semantics of nsw and nuw for left shifts don't match those of
272271 unsigned OldZExtBits = ZExtBits, OldSExtBits = SExtBits;
273272 const Value *Result =
274273 GetLinearExpression(CastOp, Scale, Offset, ZExtBits, SExtBits, DL,
275 Depth + 1, AC, DT, NSW, NUW);
274 Depth + 1, DT, NSW, NUW);
276275
277276 // zext(zext(%x)) == zext(%x), and similarly for sext; we'll handle this
278277 // by just incrementing the number of bits we've extended by.
343342 /// depth (MaxLookupSearchDepth). When DataLayout not is around, it just looks
344343 /// through pointer casts.
345344 bool BasicAAResult::DecomposeGEPExpression(const Value *V,
346 DecomposedGEP &Decomposed, const DataLayout &DL, AssumptionCache *AC,
345 DecomposedGEP &Decomposed, const DataLayout &DL,
347346 DominatorTree *DT) {
348347 // Limit recursion depth to limit compile time in crazy cases.
349348 unsigned MaxLookup = MaxLookupSearchDepth;
384383 // If it's not a GEP, hand it off to SimplifyInstruction to see if it
385384 // can come up with something. This matches what GetUnderlyingObject does.
386385 if (const Instruction *I = dyn_cast(V))
387 // TODO: Get a DominatorTree and AssumptionCache and use them here
388 // (these are both now available in this function, but this should be
389 // updated when GetUnderlyingObject is updated). TLI should be
390 // provided also.
386 // TODO: Get a DominatorTree and use it here (it is now available in
387 // this function, but this should be updated when GetUnderlyingObject
388 // is updated). TLI should be provided also.
391389 if (const Value *Simplified =
392390 SimplifyInstruction(const_cast(I), DL)) {
393391 V = Simplified;
449447 APInt IndexScale(Width, 0), IndexOffset(Width, 0);
450448 bool NSW = true, NUW = true;
451449 Index = GetLinearExpression(Index, IndexScale, IndexOffset, ZExtBits,
452 SExtBits, DL, 0, AC, DT, NSW, NUW);
450 SExtBits, DL, 0, DT, NSW, NUW);
453451
454452 // The GEP index scale ("Scale") scales C1*V+C2, yielding (C1*V+C2)*Scale.
455453 // This gives us an aggregate computation of (C1*Scale)*V + C2*Scale.
10571055 const Value *UnderlyingV2) {
10581056 DecomposedGEP DecompGEP1, DecompGEP2;
10591057 bool GEP1MaxLookupReached =
1060 DecomposeGEPExpression(GEP1, DecompGEP1, DL, &AC, DT);
1058 DecomposeGEPExpression(GEP1, DecompGEP1, DL, DT);
10611059 bool GEP2MaxLookupReached =
1062 DecomposeGEPExpression(V2, DecompGEP2, DL, &AC, DT);
1060 DecomposeGEPExpression(V2, DecompGEP2, DL, DT);
10631061
10641062 int64_t GEP1BaseOffset = DecompGEP1.StructOffset + DecompGEP1.OtherOffset;
10651063 int64_t GEP2BaseOffset = DecompGEP2.StructOffset + DecompGEP2.OtherOffset;
12211219
12221220 bool SignKnownZero, SignKnownOne;
12231221 ComputeSignBit(const_cast(V), SignKnownZero, SignKnownOne, DL,
1224 0, &AC, nullptr, DT);
1222 0, nullptr, DT);
12251223
12261224 // Zero-extension widens the variable, and so forces the sign
12271225 // bit to zero.
12561254 return NoAlias;
12571255
12581256 if (constantOffsetHeuristic(DecompGEP1.VarIndices, V1Size, V2Size,
1259 GEP1BaseOffset, &AC, DT))
1257 GEP1BaseOffset, DT))
12601258 return NoAlias;
12611259 }
12621260
16581656
16591657 bool BasicAAResult::constantOffsetHeuristic(
16601658 const SmallVectorImpl &VarIndices, uint64_t V1Size,
1661 uint64_t V2Size, int64_t BaseOffset, AssumptionCache *AC,
1659 uint64_t V2Size, int64_t BaseOffset,
16621660 DominatorTree *DT) {
16631661 if (VarIndices.size() != 2 || V1Size == MemoryLocation::UnknownSize ||
16641662 V2Size == MemoryLocation::UnknownSize)
16811679 bool NSW = true, NUW = true;
16821680 unsigned V0ZExtBits = 0, V0SExtBits = 0, V1ZExtBits = 0, V1SExtBits = 0;
16831681 const Value *V0 = GetLinearExpression(Var0.V, V0Scale, V0Offset, V0ZExtBits,
1684 V0SExtBits, DL, 0, AC, DT, NSW, NUW);
1682 V0SExtBits, DL, 0, DT, NSW, NUW);
16851683 NSW = true;
16861684 NUW = true;
16871685 const Value *V1 = GetLinearExpression(Var1.V, V1Scale, V1Offset, V1ZExtBits,
1688 V1SExtBits, DL, 0, AC, DT, NSW, NUW);
1686 V1SExtBits, DL, 0, DT, NSW, NUW);
16891687
16901688 if (V0Scale != V1Scale || V0ZExtBits != V1ZExtBits ||
16911689 V0SExtBits != V1SExtBits || !isValueEqualInPotentialCycles(V0, V1))
17191717 BasicAAResult BasicAA::run(Function &F, FunctionAnalysisManager &AM) {
17201718 return BasicAAResult(F.getParent()->getDataLayout(),
17211719 AM.getResult(F),
1722 AM.getResult(F),
17231720 &AM.getResult(F),
17241721 AM.getCachedResult(F));
17251722 }
17331730
17341731 INITIALIZE_PASS_BEGIN(BasicAAWrapperPass, "basicaa",
17351732 "Basic Alias Analysis (stateless AA impl)", true, true)
1736 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
17371733 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
17381734 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
17391735 INITIALIZE_PASS_END(BasicAAWrapperPass, "basicaa",
17441740 }
17451741
17461742 bool BasicAAWrapperPass::runOnFunction(Function &F) {
1747 auto &ACT = getAnalysis();
17481743 auto &TLIWP = getAnalysis();
17491744 auto &DTWP = getAnalysis();
17501745 auto *LIWP = getAnalysisIfAvailable();
17511746
17521747 Result.reset(new BasicAAResult(F.getParent()->getDataLayout(), TLIWP.getTLI(),
1753 ACT.getAssumptionCache(F), &DTWP.getDomTree(),
1748 &DTWP.getDomTree(),
17541749 LIWP ? &LIWP->getLoopInfo() : nullptr));
17551750
17561751 return false;
17581753
17591754 void BasicAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
17601755 AU.setPreservesAll();
1761 AU.addRequired();
17621756 AU.addRequired();
17631757 AU.addRequired();
17641758 }
17661760 BasicAAResult llvm::createLegacyPMBasicAAResult(Pass &P, Function &F) {
17671761 return BasicAAResult(
17681762 F.getParent()->getDataLayout(),
1769 P.getAnalysis().getTLI(),
1770 P.getAnalysis().getAssumptionCache(F));
1771 }
1763 P.getAnalysis().getTLI());
1764 }
33 AliasAnalysisSummary.cpp
44 AliasSetTracker.cpp
55 Analysis.cpp
6 AssumptionCache.cpp
76 BasicAliasAnalysis.cpp
87 BlockFrequencyInfo.cpp
98 BlockFrequencyInfoImpl.cpp
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/Analysis/AssumptionCache.h"
1413 #include "llvm/Analysis/CodeMetrics.h"
1514 #include "llvm/Analysis/LoopInfo.h"
1615 #include "llvm/Analysis/TargetTransformInfo.h"
7069
7170 // Find all ephemeral values.
7271 void CodeMetrics::collectEphemeralValues(
73 const Loop *L, AssumptionCache *AC,
74 SmallPtrSetImpl &EphValues) {
72 const Loop *L, SmallPtrSetImpl &EphValues) {
7573 SmallPtrSet Visited;
7674 SmallVector Worklist;
7775
8684 }
8785
8886 void CodeMetrics::collectEphemeralValues(
89 const Function *F, AssumptionCache *AC,
90 SmallPtrSetImpl &EphValues) {
87 const Function *F, SmallPtrSetImpl &EphValues) {
9188 SmallPtrSet Visited;
9289 SmallVector Worklist;
9390
2323 #include "llvm/ADT/SmallPtrSet.h"
2424 #include "llvm/ADT/SmallVector.h"
2525 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Analysis/AssumptionCache.h"
2726 #include "llvm/Analysis/ValueTracking.h"
2827 #include "llvm/IR/BasicBlock.h"
2928 #include "llvm/IR/CFG.h"
4443 char DemandedBitsWrapperPass::ID = 0;
4544 INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
4645 "Demanded bits analysis", false, false)
47 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
4846 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
4947 INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
5048 "Demanded bits analysis", false, false)
5553
5654 void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
5755 AU.setPreservesCFG();
58 AU.addRequired();
5956 AU.addRequired();
6057 AU.setPreservesAll();
6158 }
8784 KnownZero = APInt(BitWidth, 0);
8885 KnownOne = APInt(BitWidth, 0);
8986 computeKnownBits(const_cast(V1), KnownZero, KnownOne, DL, 0,
90 &AC, UserI, &DT);
87 UserI, &DT);
9188
9289 if (V2) {
9390 KnownZero2 = APInt(BitWidth, 0);
9491 KnownOne2 = APInt(BitWidth, 0);
9592 computeKnownBits(const_cast(V2), KnownZero2, KnownOne2, DL,
96 0, &AC, UserI, &DT);
93 0, UserI, &DT);
9794 }
9895 };
9996
247244 }
248245
249246 bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
250 auto &AC = getAnalysis().getAssumptionCache(F);
251247 auto &DT = getAnalysis().getDomTree();
252 DB.emplace(F, AC, DT);
248 DB.emplace(F, DT);
253249 return false;
254250 }
255251
389385
390386 DemandedBits DemandedBitsAnalysis::run(Function &F,
391387 FunctionAnalysisManager &AM) {
392 auto &AC = AM.getResult(F);
393388 auto &DT = AM.getResult(F);
394 return DemandedBits(F, AC, DT);
389 return DemandedBits(F, DT);
395390 }
396391
397392 PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
1313
1414 #include "llvm/Analysis/IVUsers.h"
1515 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Analysis/AssumptionCache.h"
1716 #include "llvm/Analysis/CodeMetrics.h"
1817 #include "llvm/Analysis/LoopPass.h"
1918 #include "llvm/Analysis/LoopPassManager.h"
4039 AM.getResult(L).getManager();
4140 Function *F = L.getHeader()->getParent();
4241
43 return IVUsers(&L, FAM.getCachedResult(*F),
44 FAM.getCachedResult(*F),
42 return IVUsers(&L, FAM.getCachedResult(*F),
4543 FAM.getCachedResult(*F),
4644 FAM.getCachedResult(*F));
4745 }
5452 char IVUsersWrapperPass::ID = 0;
5553 INITIALIZE_PASS_BEGIN(IVUsersWrapperPass, "iv-users",
5654 "Induction Variable Users", false, true)
57 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
5855 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
5956 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
6057 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
262259 return IVUses.back();
263260 }
264261
265 IVUsers::IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
266 ScalarEvolution *SE)
267 : L(L), AC(AC), LI(LI), DT(DT), SE(SE), IVUses() {
262 IVUsers::IVUsers(Loop *L, LoopInfo *LI, DominatorTree *DT, ScalarEvolution *SE)
263 : L(L), LI(LI), DT(DT), SE(SE), IVUses() {
268264 // Collect ephemeral values so that AddUsersIfInteresting skips them.
269265 EphValues.clear();
270 CodeMetrics::collectEphemeralValues(L, AC, EphValues);
266 CodeMetrics::collectEphemeralValues(L, EphValues);
271267
272268 // Find all uses of induction variables in this loop, and categorize
273269 // them by stride. Start by finding all of the PHI nodes in the header for
316312 }
317313
318314 void IVUsersWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
319 AU.addRequired();
320315 AU.addRequired();
321316 AU.addRequired();
322317 AU.addRequired();
324319 }
325320
326321 bool IVUsersWrapperPass::runOnLoop(Loop *L, LPPassManager &LPM) {
327 auto *AC = &getAnalysis().getAssumptionCache(
328 *L->getHeader()->getParent());
329322 auto *LI = &getAnalysis().getLoopInfo();
330323 auto *DT = &getAnalysis().getDomTree();
331324 auto *SE = &getAnalysis().getSE();
332325
333 IU.reset(new IVUsers(L, AC, LI, DT, SE));
326 IU.reset(new IVUsers(L, LI, DT, SE));
334327 return false;
335328 }
336329
1616 #include "llvm/ADT/SmallPtrSet.h"
1717 #include "llvm/ADT/SmallVector.h"
1818 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/AssumptionCache.h"
2019 #include "llvm/Analysis/CodeMetrics.h"
2120 #include "llvm/Analysis/ConstantFolding.h"
2221 #include "llvm/Analysis/InstructionSimplify.h"
6766
6867 /// The TargetTransformInfo available for this compilation.
6968 const TargetTransformInfo &TTI;
70
71 /// Getter for the cache of @llvm.assume intrinsics.
72 std::function &GetAssumptionCache;
7369
7470 /// Profile summary information.
7571 ProfileSummaryInfo *PSI;
200196
201197 public:
202198 CallAnalyzer(const TargetTransformInfo &TTI,
203 std::function &GetAssumptionCache,
204199 ProfileSummaryInfo *PSI, Function &Callee, CallSite CSArg,
205200 const InlineParams &Params)
206 : TTI(TTI), GetAssumptionCache(GetAssumptionCache), PSI(PSI), F(Callee),
207 CandidateCS(CSArg), Params(Params), Threshold(Params.DefaultThreshold),
208 Cost(0), IsCallerRecursive(false), IsRecursiveCall(false),
209 ExposesReturnsTwice(false), HasDynamicAlloca(false),
210 ContainsNoDuplicateCall(false), HasReturn(false), HasIndirectBr(false),
211 HasFrameEscape(false), AllocatedSize(0), NumInstructions(0),
212 NumVectorInstructions(0), FiftyPercentVectorBonus(0),
213 TenPercentVectorBonus(0), VectorBonus(0), NumConstantArgs(0),
214 NumConstantOffsetPtrArgs(0), NumAllocaArgs(0), NumConstantPtrCmps(0),
215 NumConstantPtrDiffs(0), NumInstructionsSimplified(0),
216 SROACostSavings(0), SROACostSavingsLost(0) {}
201 : TTI(TTI), PSI(PSI), F(Callee), CandidateCS(CSArg), Params(Params),
202 Threshold(Params.DefaultThreshold), Cost(0), IsCallerRecursive(false),
203 IsRecursiveCall(false), ExposesReturnsTwice(false),
204 HasDynamicAlloca(false), ContainsNoDuplicateCall(false),
205 HasReturn(false), HasIndirectBr(false), HasFrameEscape(false),
206 AllocatedSize(0), NumInstructions(0), NumVectorInstructions(0),
207 FiftyPercentVectorBonus(0), TenPercentVectorBonus(0), VectorBonus(0),
208 NumConstantArgs(0), NumConstantOffsetPtrArgs(0), NumAllocaArgs(0),
209 NumConstantPtrCmps(0), NumConstantPtrDiffs(0),
210 NumInstructionsSimplified(0), SROACostSavings(0),
211 SROACostSavingsLost(0) {}
217212
218213 bool analyzeCall(CallSite CS);
219214
961956 // out. Pretend to inline the function, with a custom threshold.
962957 auto IndirectCallParams = Params;
963958 IndirectCallParams.DefaultThreshold = InlineConstants::IndirectCallThreshold;
964 CallAnalyzer CA(TTI, GetAssumptionCache, PSI, *F, CS, IndirectCallParams);
959 CallAnalyzer CA(TTI, PSI, *F, CS, IndirectCallParams);
965960 if (CA.analyzeCall(CS)) {
966961 // We were able to inline the indirect call! Subtract the cost from the
967962 // threshold to get the bonus we want to apply, but don't go below zero.
13171312 // the ephemeral values multiple times (and they're completely determined by
13181313 // the callee, so this is purely duplicate work).
13191314 SmallPtrSet EphValues;
1320 CodeMetrics::collectEphemeralValues(&F, &GetAssumptionCache(F), EphValues);
1315 CodeMetrics::collectEphemeralValues(&F, EphValues);
13211316
13221317 // The worklist of live basic blocks in the callee *after* inlining. We avoid
13231318 // adding basic blocks of the callee which can be proven to be dead for this
14501445
14511446 InlineCost llvm::getInlineCost(
14521447 CallSite CS, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
1453 std::function &GetAssumptionCache,
14541448 ProfileSummaryInfo *PSI) {
1455 return getInlineCost(CS, CS.getCalledFunction(), Params, CalleeTTI,
1456 GetAssumptionCache, PSI);
1449 return getInlineCost(CS, CS.getCalledFunction(), Params, CalleeTTI, PSI);
14571450 }
14581451
14591452 InlineCost llvm::getInlineCost(
14601453 CallSite CS, Function *Callee, const InlineParams &Params,
1461 TargetTransformInfo &CalleeTTI,
1462 std::function &GetAssumptionCache,
1463 ProfileSummaryInfo *PSI) {
1454 TargetTransformInfo &CalleeTTI, ProfileSummaryInfo *PSI) {
14641455
14651456 // Cannot inline indirect calls.
14661457 if (!Callee)
14941485 DEBUG(llvm::dbgs() << " Analyzing call of " << Callee->getName()
14951486 << "...\n");
14961487
1497 CallAnalyzer CA(CalleeTTI, GetAssumptionCache, PSI, *Callee, CS, Params);
1488 CallAnalyzer CA(CalleeTTI, PSI, *Callee, CS, Params);
14981489 bool ShouldInline = CA.analyzeCall(CS);
14991490
15001491 DEBUG(CA.dump());
4949 const DataLayout &DL;
5050 const TargetLibraryInfo *TLI;
5151 const DominatorTree *DT;
52 AssumptionCache *AC;
5352 const Instruction *CxtI;
5453
5554 Query(const DataLayout &DL, const TargetLibraryInfo *tli,
56 const DominatorTree *dt, AssumptionCache *ac = nullptr,
57 const Instruction *cxti = nullptr)
58 : DL(DL), TLI(tli), DT(dt), AC(ac), CxtI(cxti) {}
55 const DominatorTree *dt, const Instruction *cxti = nullptr)
56 : DL(DL), TLI(tli), DT(dt), CxtI(cxti) {}
5957 };
6058 } // end anonymous namespace
6159
583581
584582 Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
585583 const DataLayout &DL, const TargetLibraryInfo *TLI,
586 const DominatorTree *DT, AssumptionCache *AC,
587 const Instruction *CxtI) {
588 return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI),
584 const DominatorTree *DT, const Instruction *CxtI) {
585 return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, CxtI),
589586 RecursionLimit);
590587 }
591588
690687 unsigned BitWidth = Op1->getType()->getScalarSizeInBits();
691688 APInt KnownZero(BitWidth, 0);
692689 APInt KnownOne(BitWidth, 0);
693 computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
690 computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.CxtI, Q.DT);
694691 if (KnownZero == ~APInt::getSignBit(BitWidth)) {
695692 // Op1 is either 0 or the minimum signed value. If the sub is NSW, then
696693 // Op1 must be 0 because negating the minimum signed value is undefined.
796793
797794 Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
798795 const DataLayout &DL, const TargetLibraryInfo *TLI,
799 const DominatorTree *DT, AssumptionCache *AC,
800 const Instruction *CxtI) {
801 return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI),
796 const DominatorTree *DT, const Instruction *CxtI) {
797 return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, CxtI),
802798 RecursionLimit);
803799 }
804800
965961 Value *llvm::SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
966962 const DataLayout &DL,
967963 const TargetLibraryInfo *TLI,
968 const DominatorTree *DT, AssumptionCache *AC,
964 const DominatorTree *DT,
969965 const Instruction *CxtI) {
970 return ::SimplifyFAddInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
966 return ::SimplifyFAddInst(Op0, Op1, FMF, Query(DL, TLI, DT, CxtI),
971967 RecursionLimit);
972968 }
973969
974970 Value *llvm::SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
975971 const DataLayout &DL,
976972 const TargetLibraryInfo *TLI,
977 const DominatorTree *DT, AssumptionCache *AC,
973 const DominatorTree *DT,
978974 const Instruction *CxtI) {
979 return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
975 return ::SimplifyFSubInst(Op0, Op1, FMF, Query(DL, TLI, DT, CxtI),
980976 RecursionLimit);
981977 }
982978
983979 Value *llvm::SimplifyFMulInst(Value *Op0, Value *Op1, FastMathFlags FMF,
984980 const DataLayout &DL,
985981 const TargetLibraryInfo *TLI,
986 const DominatorTree *DT, AssumptionCache *AC,
982 const DominatorTree *DT,
987983 const Instruction *CxtI) {
988 return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
984 return ::SimplifyFMulInst(Op0, Op1, FMF, Query(DL, TLI, DT, CxtI),
989985 RecursionLimit);
990986 }
991987
992988 Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const DataLayout &DL,
993989 const TargetLibraryInfo *TLI,
994 const DominatorTree *DT, AssumptionCache *AC,
990 const DominatorTree *DT,
995991 const Instruction *CxtI) {
996 return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
992 return ::SimplifyMulInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
997993 RecursionLimit);
998994 }
999995
10921088
10931089 Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const DataLayout &DL,
10941090 const TargetLibraryInfo *TLI,
1095 const DominatorTree *DT, AssumptionCache *AC,
1091 const DominatorTree *DT,
10961092 const Instruction *CxtI) {
1097 return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1093 return ::SimplifySDivInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
10981094 RecursionLimit);
10991095 }
11001096
11101106
11111107 Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const DataLayout &DL,
11121108 const TargetLibraryInfo *TLI,
1113 const DominatorTree *DT, AssumptionCache *AC,
1109 const DominatorTree *DT,
11141110 const Instruction *CxtI) {
1115 return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1111 return ::SimplifyUDivInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
11161112 RecursionLimit);
11171113 }
11181114
11571153 Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, FastMathFlags FMF,
11581154 const DataLayout &DL,
11591155 const TargetLibraryInfo *TLI,
1160 const DominatorTree *DT, AssumptionCache *AC,
1156 const DominatorTree *DT,
11611157 const Instruction *CxtI) {
1162 return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
1158 return ::SimplifyFDivInst(Op0, Op1, FMF, Query(DL, TLI, DT, CxtI),
11631159 RecursionLimit);
11641160 }
11651161
12331229
12341230 Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const DataLayout &DL,
12351231 const TargetLibraryInfo *TLI,
1236 const DominatorTree *DT, AssumptionCache *AC,
1232 const DominatorTree *DT,
12371233 const Instruction *CxtI) {
1238 return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1234 return ::SimplifySRemInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
12391235 RecursionLimit);
12401236 }
12411237
12511247
12521248 Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const DataLayout &DL,
12531249 const TargetLibraryInfo *TLI,
1254 const DominatorTree *DT, AssumptionCache *AC,
1250 const DominatorTree *DT,
12551251 const Instruction *CxtI) {
1256 return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1252 return ::SimplifyURemInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
12571253 RecursionLimit);
12581254 }
12591255
12791275 Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, FastMathFlags FMF,
12801276 const DataLayout &DL,
12811277 const TargetLibraryInfo *TLI,
1282 const DominatorTree *DT, AssumptionCache *AC,
1278 const DominatorTree *DT,
12831279 const Instruction *CxtI) {
1284 return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, AC, CxtI),
1280 return ::SimplifyFRemInst(Op0, Op1, FMF, Query(DL, TLI, DT, CxtI),
12851281 RecursionLimit);
12861282 }
12871283
13491345 unsigned BitWidth = Op1->getType()->getScalarSizeInBits();
13501346 APInt KnownZero(BitWidth, 0);
13511347 APInt KnownOne(BitWidth, 0);
1352 computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1348 computeKnownBits(Op1, KnownZero, KnownOne, Q.DL, 0, Q.CxtI, Q.DT);
13531349 if (KnownOne.getLimitedValue() >= BitWidth)
13541350 return UndefValue::get(Op0->getType());
13551351
13851381 unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
13861382 APInt Op0KnownZero(BitWidth, 0);
13871383 APInt Op0KnownOne(BitWidth, 0);
1388 computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.AC,
1389 Q.CxtI, Q.DT);
1384 computeKnownBits(Op0, Op0KnownZero, Op0KnownOne, Q.DL, /*Depth=*/0, Q.CxtI,
1385 Q.DT);
13901386 if (Op0KnownOne[0])
13911387 return Op0;
13921388 }
14151411
14161412 Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
14171413 const DataLayout &DL, const TargetLibraryInfo *TLI,
1418 const DominatorTree *DT, AssumptionCache *AC,
1414 const DominatorTree *DT,
14191415 const Instruction *CxtI) {
1420 return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, AC, CxtI),
1416 return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, Query(DL, TLI, DT, CxtI),
14211417 RecursionLimit);
14221418 }
14231419
14401436 Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
14411437 const DataLayout &DL,
14421438 const TargetLibraryInfo *TLI,
1443 const DominatorTree *DT, AssumptionCache *AC,
1439 const DominatorTree *DT,
14441440 const Instruction *CxtI) {
1445 return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI),
1441 return ::SimplifyLShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, CxtI),
14461442 RecursionLimit);
14471443 }
14481444
14641460 return X;
14651461
14661462 // Arithmetic shifting an all-sign-bit value is a no-op.
1467 unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.AC, Q.CxtI, Q.DT);
1463 unsigned NumSignBits = ComputeNumSignBits(Op0, Q.DL, 0, Q.CxtI, Q.DT);
14681464 if (NumSignBits == Op0->getType()->getScalarSizeInBits())
14691465 return Op0;
14701466
14741470 Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
14751471 const DataLayout &DL,
14761472 const TargetLibraryInfo *TLI,
1477 const DominatorTree *DT, AssumptionCache *AC,
1473 const DominatorTree *DT,
14781474 const Instruction *CxtI) {
1479 return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, AC, CxtI),
1475 return ::SimplifyAShrInst(Op0, Op1, isExact, Query(DL, TLI, DT, CxtI),
14801476 RecursionLimit);
14811477 }
14821478
16581654 // A & (-A) = A if A is a power of two or zero.
16591655 if (match(Op0, m_Neg(m_Specific(Op1))) ||
16601656 match(Op1, m_Neg(m_Specific(Op0)))) {
1661 if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
1662 Q.DT))
1657 if (isKnownToBeAPowerOfTwo(Op0, Q.DL, /*OrZero*/ true, 0, Q.CxtI, Q.DT))
16631658 return Op0;
1664 if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.AC, Q.CxtI,
1665 Q.DT))
1659 if (isKnownToBeAPowerOfTwo(Op1, Q.DL, /*OrZero*/ true, 0, Q.CxtI, Q.DT))
16661660 return Op1;
16671661 }
16681662
17271721
17281722 Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const DataLayout &DL,
17291723 const TargetLibraryInfo *TLI,
1730 const DominatorTree *DT, AssumptionCache *AC,
1724 const DominatorTree *DT,
17311725 const Instruction *CxtI) {
1732 return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1726 return ::SimplifyAndInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
17331727 RecursionLimit);
17341728 }
17351729
19091903 match(A, m_Add(m_Value(V1), m_Value(V2)))) {
19101904 // Add commutes, try both ways.
19111905 if (V1 == B &&
1912 MaskedValueIsZero(V2, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1906 MaskedValueIsZero(V2, C2->getValue(), Q.DL, 0, Q.CxtI, Q.DT))
19131907 return A;
19141908 if (V2 == B &&
1915 MaskedValueIsZero(V1, C2->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1909 MaskedValueIsZero(V1, C2->getValue(), Q.DL, 0, Q.CxtI, Q.DT))
19161910 return A;
19171911 }
19181912 // Or commutes, try both ways.
19201914 match(B, m_Add(m_Value(V1), m_Value(V2)))) {
19211915 // Add commutes, try both ways.
19221916 if (V1 == A &&
1923 MaskedValueIsZero(V2, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1917 MaskedValueIsZero(V2, C1->getValue(), Q.DL, 0, Q.CxtI, Q.DT))
19241918 return B;
19251919 if (V2 == A &&
1926 MaskedValueIsZero(V1, C1->getValue(), Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
1920 MaskedValueIsZero(V1, C1->getValue(), Q.DL, 0, Q.CxtI, Q.DT))
19271921 return B;
19281922 }
19291923 }
19401934
19411935 Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const DataLayout &DL,
19421936 const TargetLibraryInfo *TLI,
1943 const DominatorTree *DT, AssumptionCache *AC,
1937 const DominatorTree *DT,
19441938 const Instruction *CxtI) {
1945 return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1939 return ::SimplifyOrInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
19461940 RecursionLimit);
19471941 }
19481942
19941988
19951989 Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const DataLayout &DL,
19961990 const TargetLibraryInfo *TLI,
1997 const DominatorTree *DT, AssumptionCache *AC,
1991 const DominatorTree *DT,
19981992 const Instruction *CxtI) {
1999 return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, AC, CxtI),
1993 return ::SimplifyXorInst(Op0, Op1, Query(DL, TLI, DT, CxtI),
20001994 RecursionLimit);
20011995 }
20021996
23112305 return getTrue(ITy);
23122306 case ICmpInst::ICMP_EQ:
23132307 case ICmpInst::ICMP_ULE:
2314 if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2308 if (isKnownNonZero(LHS, Q.DL, 0, Q.CxtI, Q.DT))
23152309 return getFalse(ITy);
23162310 break;
23172311 case ICmpInst::ICMP_NE:
23182312 case ICmpInst::ICMP_UGT:
2319 if (isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2313 if (isKnownNonZero(LHS, Q.DL, 0, Q.CxtI, Q.DT))
23202314 return getTrue(ITy);
23212315 break;
23222316 case ICmpInst::ICMP_SLT:
2323 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2324 Q.CxtI, Q.DT);
2317 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.CxtI,
2318 Q.DT);
23252319 if (LHSKnownNegative)
23262320 return getTrue(ITy);
23272321 if (LHSKnownNonNegative)
23282322 return getFalse(ITy);
23292323 break;
23302324 case ICmpInst::ICMP_SLE:
2331 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2332 Q.CxtI, Q.DT);
2325 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.CxtI,
2326 Q.DT);
23332327 if (LHSKnownNegative)
23342328 return getTrue(ITy);
2335 if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2329 if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.CxtI, Q.DT))
23362330 return getFalse(ITy);
23372331 break;
23382332 case ICmpInst::ICMP_SGE:
2339 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2340 Q.CxtI, Q.DT);
2333 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.CxtI,
2334 Q.DT);
23412335 if (LHSKnownNegative)
23422336 return getFalse(ITy);
23432337 if (LHSKnownNonNegative)
23442338 return getTrue(ITy);
23452339 break;
23462340 case ICmpInst::ICMP_SGT:
2347 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.AC,
2348 Q.CxtI, Q.DT);
2341 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0, Q.CxtI,
2342 Q.DT);
23492343 if (LHSKnownNegative)
23502344 return getFalse(ITy);
2351 if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.AC, Q.CxtI, Q.DT))
2345 if (LHSKnownNonNegative && isKnownNonZero(LHS, Q.DL, 0, Q.CxtI, Q.DT))
23522346 return getTrue(ITy);
23532347 break;
23542348 }
25792573 bool RHSKnownNonNegative, RHSKnownNegative;
25802574 bool YKnownNonNegative, YKnownNegative;
25812575 ComputeSignBit(RHS, RHSKnownNonNegative, RHSKnownNegative, Q.DL, 0,
2582 Q.AC, Q.CxtI, Q.DT);
2583 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC,
25842576 Q.CxtI, Q.DT);
2577 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.CxtI,
2578 Q.DT);
25852579 if (RHSKnownNonNegative && YKnownNegative)
25862580 return Pred == ICmpInst::ICMP_SLT ? getTrue(ITy) : getFalse(ITy);
25872581 if (RHSKnownNegative || YKnownNonNegative)
25992593 bool LHSKnownNonNegative, LHSKnownNegative;
26002594 bool YKnownNonNegative, YKnownNegative;
26012595 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, Q.DL, 0,
2602 Q.AC, Q.CxtI, Q.DT);
2603 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.AC,
26042596 Q.CxtI, Q.DT);
2597 ComputeSignBit(Y, YKnownNonNegative, YKnownNegative, Q.DL, 0, Q.CxtI,
2598 Q.DT);
26052599 if (LHSKnownNonNegative && YKnownNegative)
26062600 return Pred == ICmpInst::ICMP_SGT ? getTrue(ITy) : getFalse(ITy);
26072601 if (LHSKnownNegative || YKnownNonNegative)
26572651 break;
26582652 case ICmpInst::ICMP_SGT:
26592653 case ICmpInst::ICMP_SGE:
2660 ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2661 Q.CxtI, Q.DT);
2654 ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.CxtI,
2655 Q.DT);
26622656 if (!KnownNonNegative)
26632657 break;
26642658 LLVM_FALLTHROUGH;
26682662 return getFalse(ITy);
26692663 case ICmpInst::ICMP_SLT:
26702664 case ICmpInst::ICMP_SLE:
2671 ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2672 Q.CxtI, Q.DT);
2665 ComputeSignBit(RHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.CxtI,
2666 Q.DT);
26732667 if (!KnownNonNegative)
26742668 break;
26752669 LLVM_FALLTHROUGH;
26882682 break;
26892683 case ICmpInst::ICMP_SGT:
26902684 case ICmpInst::ICMP_SGE:
2691 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2692 Q.CxtI, Q.DT);
2685 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.CxtI,
2686 Q.DT);
26932687 if (!KnownNonNegative)
26942688 break;
26952689 LLVM_FALLTHROUGH;
26992693 return getTrue(ITy);
27002694 case ICmpInst::ICMP_SLT:
27012695 case ICmpInst::ICMP_SLE:
2702 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.AC,
2703 Q.CxtI, Q.DT);
2696 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, Q.DL, 0, Q.CxtI,
2697 Q.DT);
27042698 if (!KnownNonNegative)
27052699 break;
27062700 LLVM_FALLTHROUGH;
32253219
32263220 // icmp eq|ne X, Y -> false|true if X != Y
32273221 if ((Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) &&
3228 isKnownNonEqual(LHS, RHS, Q.DL, Q.AC, Q.CxtI, Q.DT)) {
3222 isKnownNonEqual(LHS, RHS, Q.DL, Q.CxtI, Q.DT)) {
32293223 LLVMContext &Ctx = LHS->getType()->getContext();
32303224 return Pred == ICmpInst::ICMP_NE ?
32313225 ConstantInt::getTrue(Ctx) : ConstantInt::getFalse(Ctx);
32843278 unsigned BitWidth = RHSVal->getBitWidth();
32853279 APInt LHSKnownZero(BitWidth, 0);
32863280 APInt LHSKnownOne(BitWidth, 0);
3287 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC,
3281 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0,
32883282 Q.CxtI, Q.DT);
32893283 if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0))
32903284 return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy)
33103304 Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
33113305 const DataLayout &DL,
33123306 const TargetLibraryInfo *TLI,
3313 const DominatorTree *DT, AssumptionCache *AC,
3307 const DominatorTree *DT,
33143308 const Instruction *CxtI) {
3315 return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI),
3309 return ::SimplifyICmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, CxtI),
33163310 RecursionLimit);
33173311 }
33183312
34433437 Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
34443438 FastMathFlags FMF, const DataLayout &DL,
34453439 const TargetLibraryInfo *TLI,
3446 const DominatorTree *DT, AssumptionCache *AC,
3440 const DominatorTree *DT,
34473441 const Instruction *CxtI) {
34483442 return ::SimplifyFCmpInst(Predicate, LHS, RHS, FMF,
3449 Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
3443 Query(DL, TLI, DT, CxtI), RecursionLimit);
34503444 }
34513445
34523446 /// See if V simplifies when its operand Op is replaced with RepOp.
37133707 Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
37143708 const DataLayout &DL,
37153709 const TargetLibraryInfo *TLI,
3716 const DominatorTree *DT, AssumptionCache *AC,
3710 const DominatorTree *DT,
37173711 const Instruction *CxtI) {
37183712 return ::SimplifySelectInst(Cond, TrueVal, FalseVal,
3719 Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
3713 Query(DL, TLI, DT, CxtI), RecursionLimit);
37203714 }
37213715
37223716 /// Given operands for an GetElementPtrInst, see if we can fold the result.
38323826 Value *llvm::SimplifyGEPInst(Type *SrcTy, ArrayRef Ops,
38333827 const DataLayout &DL,
38343828 const TargetLibraryInfo *TLI,
3835 const DominatorTree *DT, AssumptionCache *AC,
3829 const DominatorTree *DT,
38363830 const Instruction *CxtI) {
38373831 return ::SimplifyGEPInst(SrcTy, Ops,
3838 Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
3832 Query(DL, TLI, DT, CxtI), RecursionLimit);
38393833 }
38403834
38413835 /// Given operands for an InsertValueInst, see if we can fold the result.
38693863
38703864 Value *llvm::SimplifyInsertValueInst(
38713865 Value *Agg, Value *Val, ArrayRef Idxs, const DataLayout &DL,
3872 const TargetLibraryInfo *TLI, const DominatorTree *DT, AssumptionCache *AC,
3866 const TargetLibraryInfo *TLI, const DominatorTree *DT,
38733867 const Instruction *CxtI) {
3874 return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, AC, CxtI),
3868 return ::SimplifyInsertValueInst(Agg, Val, Idxs, Query(DL, TLI, DT, CxtI),
38753869 RecursionLimit);
38763870 }
38773871
39043898 const DataLayout &DL,
39053899 const TargetLibraryInfo *TLI,
39063900 const DominatorTree *DT,
3907 AssumptionCache *AC,
39083901 const Instruction *CxtI) {
3909 return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, AC, CxtI),
3902 return ::SimplifyExtractValueInst(Agg, Idxs, Query(DL, TLI, DT, CxtI),
39103903 RecursionLimit);
39113904 }
39123905
39373930
39383931 Value *llvm::SimplifyExtractElementInst(
39393932 Value *Vec, Value *Idx, const DataLayout &DL, const TargetLibraryInfo *TLI,
3940 const DominatorTree *DT, AssumptionCache *AC, const Instruction *CxtI) {
3941 return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, AC, CxtI),
3933 const DominatorTree *DT, const Instruction *CxtI) {
3934 return ::SimplifyExtractElementInst(Vec, Idx, Query(DL, TLI, DT, CxtI),
39423935 RecursionLimit);
39433936 }
39443937
40124005 Value *llvm::SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
40134006 const DataLayout &DL,
40144007 const TargetLibraryInfo *TLI,
4015 const DominatorTree *DT, AssumptionCache *AC,
4008 const DominatorTree *DT,
40164009 const Instruction *CxtI) {
4017 return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, AC, CxtI),
4010 return ::SimplifyCastInst(CastOpc, Op, Ty, Query(DL, TLI, DT, CxtI),
40184011 RecursionLimit);
40194012 }
40204013
41074100
41084101 Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
41094102 const DataLayout &DL, const TargetLibraryInfo *TLI,
4110 const DominatorTree *DT, AssumptionCache *AC,
4103 const DominatorTree *DT,
41114104 const Instruction *CxtI) {
4112 return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, AC, CxtI),
4105 return ::SimplifyBinOp(Opcode, LHS, RHS, Query(DL, TLI, DT, CxtI),
41134106 RecursionLimit);
41144107 }
41154108
41164109 Value *llvm::SimplifyFPBinOp(unsigned Opcode, Value *LHS, Value *RHS,
41174110 const FastMathFlags &FMF, const DataLayout &DL,
41184111 const TargetLibraryInfo *TLI,
4119 const DominatorTree *DT, AssumptionCache *AC,
4112 const DominatorTree *DT,
41204113 const Instruction *CxtI) {
4121 return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, AC, CxtI),
4114 return ::SimplifyFPBinOp(Opcode, LHS, RHS, FMF, Query(DL, TLI, DT, CxtI),
41224115 RecursionLimit);
41234116 }
41244117
41324125
41334126 Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
41344127 const DataLayout &DL, const TargetLibraryInfo *TLI,
4135 const DominatorTree *DT, AssumptionCache *AC,
4128 const DominatorTree *DT,
41364129 const Instruction *CxtI) {
4137 return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, AC, CxtI),
4130 return ::SimplifyCmpInst(Predicate, LHS, RHS, Query(DL, TLI, DT, CxtI),
41384131 RecursionLimit);
41394132 }
41404133
43344327 Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin,
43354328 User::op_iterator ArgEnd, const DataLayout &DL,
43364329 const TargetLibraryInfo *TLI, const DominatorTree *DT,
4337 AssumptionCache *AC, const Instruction *CxtI) {
4338 return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, AC, CxtI),
4330 const Instruction *CxtI) {
4331 return ::SimplifyCall(V, ArgBegin, ArgEnd, Query(DL, TLI, DT, CxtI),
43394332 RecursionLimit);
43404333 }
43414334
43424335 Value *llvm::SimplifyCall(Value *V, ArrayRef Args,
43434336 const DataLayout &DL, const TargetLibraryInfo *TLI,
4344 const DominatorTree *DT, AssumptionCache *AC,
4337 const DominatorTree *DT,
43454338 const Instruction *CxtI) {
43464339 return ::SimplifyCall(V, Args.begin(), Args.end(),
4347 Query(DL, TLI, DT, AC, CxtI), RecursionLimit);
4340 Query(DL, TLI, DT, CxtI), RecursionLimit);
43484341 }
43494342
43504343 /// See if we can compute a simplified version of this instruction.
43514344 /// If not, this returns null.
43524345 Value *llvm::SimplifyInstruction(Instruction *I, const DataLayout &DL,
43534346 const TargetLibraryInfo *TLI,
4354 const DominatorTree *DT, AssumptionCache *AC) {
4347 const DominatorTree *DT) {
43554348 Value *Result;
43564349
43574350 switch (I->getOpcode()) {
43604353 break;
43614354 case Instruction::FAdd:
43624355 Result = SimplifyFAddInst(I->getOperand(0), I->getOperand(1),
4363 I->getFastMathFlags(), DL, TLI, DT, AC, I);
4356 I->getFastMathFlags(), DL, TLI, DT, I);
43644357 break;
43654358 case Instruction::Add:
43664359 Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
43674360 cast(I)->hasNoSignedWrap(),
43684361 cast(I)->hasNoUnsignedWrap(), DL,
4369 TLI, DT, AC, I);
4362 TLI, DT, I);
43704363 break;
43714364 case Instruction::FSub:
43724365 Result = SimplifyFSubInst(I->getOperand(0), I->getOperand(1),
4373 I->getFastMathFlags(), DL, TLI, DT, AC, I);
4366 I->getFastMathFlags(), DL, TLI, DT, I);
43744367 break;
43754368 case Instruction::Sub:
43764369 Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
43774370 cast(I)->hasNoSignedWrap(),
43784371 cast(I)->hasNoUnsignedWrap(), DL,
4379 TLI, DT, AC, I);
4372 TLI, DT, I);
43804373 break;
43814374 case Instruction::FMul:
43824375 Result = SimplifyFMulInst(I->getOperand(0), I->getOperand(1),
4383 I->getFastMathFlags(), DL, TLI, DT, AC, I);
4376 I->getFastMathFlags(), DL, TLI, DT, I);
43844377 break;
43854378 case Instruction::Mul:
43864379 Result =
4387 SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4380 SimplifyMulInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, I);
43884381 break;
43894382 case Instruction::SDiv:
43904383 Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4391 AC, I);
4384 I);
43924385 break;
43934386 case Instruction::UDiv:
43944387 Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4395 AC, I);
4388 I);
43964389 break;
43974390 case Instruction::FDiv:
43984391 Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1),
4399 I->getFastMathFlags(), DL, TLI, DT, AC, I);
4392 I->getFastMathFlags(), DL, TLI, DT, I);
44004393 break;
44014394 case Instruction::SRem:
44024395 Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4403 AC, I);
4396 I);
44044397 break;
44054398 case Instruction::URem:
44064399 Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT,
4407 AC, I);
4400 I);
44084401 break;
44094402 case Instruction::FRem:
44104403 Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1),
4411 I->getFastMathFlags(), DL, TLI, DT, AC, I);
4404 I->getFastMathFlags(), DL, TLI, DT, I);
44124405 break;
44134406 case Instruction::Shl:
44144407 Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1),
44154408 cast(I)->hasNoSignedWrap(),
44164409 cast(I)->hasNoUnsignedWrap(), DL,
4417 TLI, DT, AC, I);
4410 TLI, DT, I);
44184411 break;
44194412 case Instruction::LShr:
44204413 Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1),
44214414 cast(I)->isExact(), DL, TLI, DT,
4422 AC, I);
4415 I);
44234416 break;
44244417 case Instruction::AShr:
44254418 Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1),
44264419 cast(I)->isExact(), DL, TLI, DT,
4427 AC, I);
4420 I);
44284421 break;
44294422 case Instruction::And:
44304423 Result =
4431 SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4424 SimplifyAndInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, I);
44324425 break;
44334426 case Instruction::Or:
44344427 Result =
4435 SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4428 SimplifyOrInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, I);
44364429 break;
44374430 case Instruction::Xor:
44384431 Result =
4439 SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, AC, I);
4432 SimplifyXorInst(I->getOperand(0), I->getOperand(1), DL, TLI, DT, I);
44404433 break;
44414434 case Instruction::ICmp:
44424435 Result =
44434436 SimplifyICmpInst(cast(I)->getPredicate(), I->getOperand(0),
4444 I->getOperand(1), DL, TLI, DT, AC, I);
4437 I->getOperand(1), DL, TLI, DT, I);
44454438 break;
44464439 case Instruction::FCmp:
44474440 Result = SimplifyFCmpInst(cast(I)->getPredicate(),
44484441 I->getOperand(0), I->getOperand(1),
4449 I->getFastMathFlags(), DL, TLI, DT, AC, I);
4442 I->getFastMathFlags(), DL, TLI, DT, I);
44504443 break;
44514444 case Instruction::Select:
44524445 Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
4453 I->getOperand(2), DL, TLI, DT, AC, I);
4446 I->getOperand(2), DL, TLI, DT, I);
44544447 break;
44554448 case Instruction::GetElementPtr: {
44564449 SmallVector Ops(I->op_begin(), I->op_end());
44574450 Result = SimplifyGEPInst(cast(I)->getSourceElementType(),
4458 Ops, DL, TLI, DT, AC, I);
4451 Ops, DL, TLI, DT, I);
44594452 break;
44604453 }
44614454 case Instruction::InsertValue: {
44624455 InsertValueInst *IV = cast(I);
44634456 Result = SimplifyInsertValueInst(IV->getAggregateOperand(),
44644457 IV->getInsertedValueOperand(),
4465 IV->getIndices(), DL, TLI, DT, AC, I);
4458 IV->getIndices(), DL, TLI, DT, I);
44664459 break;
44674460 }
44684461 case Instruction::ExtractValue: {
44694462 auto *EVI = cast(I);
44704463 Result = SimplifyExtractValueInst(EVI->getAggregateOperand(),
4471 EVI->getIndices(), DL, TLI, DT, AC, I);
4464 EVI->getIndices(), DL, TLI, DT, I);
44724465 break;
44734466 }
44744467 case Instruction::ExtractElement: {
44754468 auto *EEI = cast(I);
44764469 Result = SimplifyExtractElementInst(
4477 EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, AC, I);
4470 EEI->getVectorOperand(), EEI->getIndexOperand(), DL, TLI, DT, I);
44784471 break;
44794472 }
44804473 case Instruction::PHI:
4481 Result = SimplifyPHINode(cast(I), Query(DL, TLI, DT, AC, I));
4474 Result = SimplifyPHINode(cast(I), Query(DL, TLI, DT, I));
44824475 break;
44834476 case Instruction::Call: {
44844477 CallSite CS(cast(I));
44854478 Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), DL,
4486 TLI, DT, AC, I);
4479 TLI, DT, I);
44874480 break;
44884481 }
44894482 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
44904483 #include "llvm/IR/Instruction.def"
44914484 #undef HANDLE_CAST_INST
44924485 Result = SimplifyCastInst(I->getOpcode(), I->getOperand(0), I->getType(),
4493 DL, TLI, DT, AC, I);
4486 DL, TLI, DT, I);
44944487 break;
44954488 }
44964489
45004493 unsigned BitWidth = I->getType()->getScalarSizeInBits();
45014494 APInt KnownZero(BitWidth, 0);
45024495 APInt KnownOne(BitWidth, 0);
4503 computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, AC, I, DT);
4496 computeKnownBits(I, KnownZero, KnownOne, DL, /*Depth*/0, I, DT);
45044497 if ((KnownZero | KnownOne).isAllOnesValue())
45054498 Result = ConstantInt::get(I->getType(), KnownOne);
45064499 }
45244517 /// in simplified value does not count toward this.
45254518 static bool replaceAndRecursivelySimplifyImpl(Instruction *I, Value *SimpleV,
45264519 const TargetLibraryInfo *TLI,
4527 const DominatorTree *DT,
4528 AssumptionCache *AC) {
4520 const DominatorTree *DT) {
45294521 bool Simplified = false;
45304522 SmallSetVector Worklist;
45314523 const DataLayout &DL = I->getModule()->getDataLayout();
45544546 I = Worklist[Idx];
45554547
45564548 // See if this instruction simplifies.
4557 SimpleV = SimplifyInstruction(I, DL, TLI, DT, AC);
4549 SimpleV = SimplifyInstruction(I, DL, TLI, DT);
45584550 if (!SimpleV)
45594551 continue;
45604552
45804572
45814573 bool llvm::recursivelySimplifyInstruction(Instruction *I,
45824574 const TargetLibraryInfo *TLI,
4583 const DominatorTree *DT,
4584 AssumptionCache *AC) {
4585 return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT, AC);
4575 const DominatorTree *DT) {
4576 return replaceAndRecursivelySimplifyImpl(I, nullptr, TLI, DT);
45864577 }
45874578
45884579 bool llvm::replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV,
45894580 const TargetLibraryInfo *TLI,
4590 const DominatorTree *DT,
4591 AssumptionCache *AC) {
4581 const DominatorTree *DT) {
45924582 assert(I != SimpleV && "replaceAndRecursivelySimplify(X,X) is not valid!");
45934583 assert(SimpleV && "Must provide a simplified value.");
4594 return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT, AC);
4595 }
4584 return replaceAndRecursivelySimplifyImpl(I, SimpleV, TLI, DT);
4585 }
1414 #include "llvm/Analysis/LazyValueInfo.h"
1515 #include "llvm/ADT/DenseSet.h"
1616 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Analysis/AssumptionCache.h"
1817 #include "llvm/Analysis/ConstantFolding.h"
1918 #include "llvm/Analysis/TargetLibraryInfo.h"
2019 #include "llvm/Analysis/ValueTracking.h"
4140 char LazyValueInfoWrapperPass::ID = 0;
4241 INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info",
4342 "Lazy Value Information Analysis", false, true)
44 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
4543 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
4644 INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
4745 "Lazy Value Information Analysis", false, true)
578576 return true;
579577 }
580578
581 AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls.
582579 const DataLayout &DL; ///< A mandatory DataLayout
583580 DominatorTree *DT; ///< An optional DT pointer.
584581
637634 /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc.
638635 void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc);
639636
640 LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL,
641 DominatorTree *DT = nullptr)
642 : AC(AC), DL(DL), DT(DT) {}
637 LazyValueInfoImpl(const DataLayout &DL, DominatorTree *DT = nullptr)
638 : DL(DL), DT(DT) {}
643639 };
644640 } // end anonymous namespace
645641
14591455 //===----------------------------------------------------------------------===//
14601456
14611457 /// This lazily constructs the LazyValueInfoImpl.
1462 static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC,
1463 const DataLayout *DL,
1458 static LazyValueInfoImpl &getImpl(void *&PImpl, const DataLayout *DL,
14641459 DominatorTree *DT = nullptr) {
14651460 if (!PImpl) {
14661461 assert(DL && "getCache() called with a null DataLayout");
1467 PImpl = new LazyValueInfoImpl(AC, *DL, DT);
1462 PImpl = new LazyValueInfoImpl(*DL, DT);
14681463 }
14691464 return *static_cast(PImpl);
14701465 }
14711466
14721467 bool LazyValueInfoWrapperPass::runOnFunction(Function &F) {
1473 Info.AC = &getAnalysis().getAssumptionCache(F);
14741468 const DataLayout &DL = F.getParent()->getDataLayout();
14751469
14761470 DominatorTreeWrapperPass *DTWP =
14791473 Info.TLI = &getAnalysis().getTLI();
14801474
14811475 if (Info.PImpl)
1482 getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear();
1476 getImpl(Info.PImpl, &DL, Info.DT).clear();
14831477
14841478 // Fully lazy.
14851479 return false;
14871481
14881482 void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
14891483 AU.setPreservesAll();
1490 AU.addRequired();
14911484 AU.addRequired();
14921485 }
14931486
14981491 void LazyValueInfo::releaseMemory() {
14991492 // If the cache was allocated, free it.
15001493 if (PImpl) {
1501 delete &getImpl(PImpl, AC, nullptr);
1494 delete &getImpl(PImpl, nullptr);
15021495 PImpl = nullptr;
15031496 }
15041497 }
15061499 void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); }
15071500
15081501 LazyValueInfo LazyValueAnalysis::run(Function &F, FunctionAnalysisManager &FAM) {
1509 auto &AC = FAM.getResult(F);
15101502 auto &TLI = FAM.getResult(F);
15111503 auto *DT = FAM.getCachedResult(F);
15121504
1513 return LazyValueInfo(&AC, &TLI, DT);
1505 return LazyValueInfo(&TLI, DT);
15141506 }
15151507
15161508 /// Returns true if we can statically tell that this value will never be a
15351527
15361528 const DataLayout &DL = BB->getModule()->getDataLayout();
15371529 LVILatticeVal Result =
1538 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
1530 getImpl(PImpl, &DL, DT).getValueInBlock(V, BB, CxtI);
15391531
15401532 if (Result.isConstant())
15411533 return Result.getConstant();
15531545 unsigned Width = V->getType()->getIntegerBitWidth();
15541546 const DataLayout &DL = BB->getModule()->getDataLayout();
15551547 LVILatticeVal Result =
1556 getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI);
1548 getImpl(PImpl, &DL, DT).getValueInBlock(V, BB, CxtI);
15571549 if (Result.isUndefined())
15581550 return ConstantRange(Width, /*isFullSet=*/false);
15591551 if (Result.isConstantRange())
15721564 Instruction *CxtI) {
15731565 const DataLayout &DL = FromBB->getModule()->getDataLayout();
15741566 LVILatticeVal Result =
1575 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1567 getImpl(PImpl, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
15761568
15771569 if (Result.isConstant())
15781570 return Result.getConstant();
16601652 Instruction *CxtI) {
16611653 const DataLayout &DL = FromBB->getModule()->getDataLayout();
16621654 LVILatticeVal Result =
1663 getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
1655 getImpl(PImpl, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI);
16641656
16651657 return getPredicateResult(Pred, C, Result, DL, TLI);
16661658 }
16801672 return LazyValueInfo::True;
16811673 }
16821674 const DataLayout &DL = CxtI->getModule()->getDataLayout();
1683 LVILatticeVal Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI);
1675 LVILatticeVal Result = getImpl(PImpl, &DL, DT).getValueAt(V, CxtI);
16841676 Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI);
16851677 if (Ret != Unknown)
16861678 return Ret;
17701762 BasicBlock *NewSucc) {
17711763 if (PImpl) {
17721764 const DataLayout &DL = PredBB->getModule()->getDataLayout();
1773 getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
1765 getImpl(PImpl, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc);
17741766 }
17751767 }
17761768
17771769 void LazyValueInfo::eraseBlock(BasicBlock *BB) {
17781770 if (PImpl) {
17791771 const DataLayout &DL = BB->getModule()->getDataLayout();
1780 getImpl(PImpl, AC, &DL, DT).eraseBlock(BB);
1781 }
1782 }
1772 getImpl(PImpl, &DL, DT).eraseBlock(BB);
1773 }
1774 }
3939 #include "llvm/ADT/SmallPtrSet.h"
4040 #include "llvm/ADT/Twine.h"
4141 #include "llvm/Analysis/AliasAnalysis.h"
42 #include "llvm/Analysis/AssumptionCache.h"
4342 #include "llvm/Analysis/ConstantFolding.h"
4443 #include "llvm/Analysis/InstructionSimplify.h"
4544 #include "llvm/Analysis/Loads.h"
127126 Module *Mod;
128127 const DataLayout *DL;
129128 AliasAnalysis *AA;
130 AssumptionCache *AC;
131129 DominatorTree *DT;
132130 TargetLibraryInfo *TLI;
133131
144142 void getAnalysisUsage(AnalysisUsage &AU) const override {
145143 AU.setPreservesAll();
146144 AU.addRequired();
147 AU.addRequired();
148145 AU.addRequired();
149146 AU.addRequired();
150147 }
184181 char Lint::ID = 0;
185182 INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR",
186183 false, true)
187 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
188184 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
189185 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
190186 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
202198 Mod = F.getParent();
203199 DL = &F.getParent()->getDataLayout();
204200 AA = &getAnalysis().getAAResults();
205 AC = &getAnalysis().getAssumptionCache(F);
206201 DT = &getAnalysis().getDomTree();
207202 TLI = &getAnalysis().getTLI();
208203 visit(F);
524519 "Undefined result: Shift count out of range", &I);
525520 }
526521
527 static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT,
528 AssumptionCache *AC) {
522 static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT) {
529523 // Assume undef could be zero.
530524 if (isa(V))
531525 return true;
534528 if (!VecTy) {
535529 unsigned BitWidth = V->getType()->getIntegerBitWidth();
536530 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
537 computeKnownBits(V, KnownZero, KnownOne, DL, 0, AC,
531 computeKnownBits(V, KnownZero, KnownOne, DL, 0,
538532 dyn_cast(V), DT);
539533 return KnownZero.isAllOnesValue();
540534 }
565559 }
566560
567561 void Lint::visitSDiv(BinaryOperator &I) {
568 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
562 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT),
569563 "Undefined behavior: Division by zero", &I);
570564 }
571565
572566 void Lint::visitUDiv(BinaryOperator &I) {
573 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
567 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT),
574568 "Undefined behavior: Division by zero", &I);
575569 }
576570
577571 void Lint::visitSRem(BinaryOperator &I) {
578 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
572 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT),
579573 "Undefined behavior: Division by zero", &I);
580574 }
581575
582576 void Lint::visitURem(BinaryOperator &I) {
583 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
577 Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT),
584578 "Undefined behavior: Division by zero", &I);
585579 }
586580
698692
699693 // As a last resort, try SimplifyInstruction or constant folding.
700694 if (Instruction *Inst = dyn_cast(V)) {
701 if (Value *W = SimplifyInstruction(Inst, *DL, TLI, DT, AC))
695 if (Value *W = SimplifyInstruction(Inst, *DL, TLI, DT))
702696 return findValueImpl(W, OffsetOk, Visited);
703697 } else if (auto *C = dyn_cast(V)) {
704698 if (Value *W = ConstantFoldConstant(C, *DL, TLI))
1919 #include "llvm/ADT/STLExtras.h"
2020 #include "llvm/ADT/Statistic.h"
2121 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/AssumptionCache.h"
2322 #include "llvm/Analysis/MemoryBuiltins.h"
2423 #include "llvm/Analysis/PHITransAddr.h"
2524 #include "llvm/Analysis/OrderedBasicBlock.h"
890889 return;
891890 }
892891 const DataLayout &DL = FromBB->getModule()->getDataLayout();
893 PHITransAddr Address(const_cast(Loc.Ptr), DL, &AC);
892 PHITransAddr Address(const_cast(Loc.Ptr), DL);
894893
895894 // This is the set of blocks we've inspected, and the pointer we consider in
896895 // each block. Because of critical edges, we currently bail out if querying
16471646 MemoryDependenceResults
16481647 MemoryDependenceAnalysis::run(Function &F, FunctionAnalysisManager &AM) {
16491648 auto &AA = AM.getResult(F);
1650 auto &AC = AM.getResult(F);
16511649 auto &TLI = AM.getResult(F);
16521650 auto &DT = AM.getResult(F);
1653 return MemoryDependenceResults(AA, AC, TLI, DT);
1651 return MemoryDependenceResults(AA, TLI, DT);
16541652 }
16551653
16561654 char MemoryDependenceWrapperPass::ID = 0;
16571655
16581656 INITIALIZE_PASS_BEGIN(MemoryDependenceWrapperPass, "memdep",
16591657 "Memory Dependence Analysis", false, true)
1660 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
16611658 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
16621659 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
16631660 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
16761673
16771674 void MemoryDependenceWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
16781675 AU.setPreservesAll();
1679 AU.addRequired();
16801676 AU.addRequired();
16811677 AU.addRequiredTransitive();
16821678 AU.addRequiredTransitive();
16881684
16891685 bool MemoryDependenceWrapperPass::runOnFunction(Function &F) {
16901686 auto &AA = getAnalysis().getAAResults();
1691 auto &AC = getAnalysis().getAssumptionCache(F);
16921687 auto &TLI = getAnalysis().getTLI();
16931688 auto &DT = getAnalysis().getDomTree();
1694 MemDep.emplace(AA, AC, TLI, DT);
1689 MemDep.emplace(AA, TLI, DT);
16951690 return false;
16961691 }
226226
227227 // Simplify the GEP to handle 'gep x, 0' -> x etc.
228228 if (Value *V = SimplifyGEPInst(GEP->getSourceElementType(),
229 GEPOps, DL, TLI, DT, AC)) {
229 GEPOps, DL, TLI, DT)) {
230230 for (unsigned i = 0, e = GEPOps.size(); i != e; ++i)
231231 RemoveInstInputs(GEPOps[i], InstInputs);
232232
275275 }
276276
277277 // See if the add simplifies away.
278 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, DL, TLI, DT, AC)) {
278 if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, DL, TLI, DT)) {
279279 // If we simplified the operands, the LHS is no longer an input, but Res
280280 // is.
281281 RemoveInstInputs(LHS, InstInputs);
366366 SmallVectorImpl &NewInsts) {
367367 // See if we have a version of this value already available and dominating
368368 // PredBB. If so, there is no need to insert a new instance of it.
369 PHITransAddr Tmp(InVal, DL, AC);
369 PHITransAddr Tmp(InVal, DL);
370370 if (!Tmp.PHITranslateValue(CurBB, PredBB, &DT, /*MustDominate=*/true))
371371 return Tmp.getAddr();
372372
6363 #include "llvm/ADT/ScopeExit.h"
6464 #include "llvm/ADT/Sequence.h"
6565 #include "llvm/ADT/SmallPtrSet.h"
66 #include "llvm/ADT/SmallSet.h"
6667 #include "llvm/ADT/Statistic.h"
67 #include "llvm/Analysis/AssumptionCache.h"
6868 #include "llvm/Analysis/ConstantFolding.h"
6969 #include "llvm/Analysis/InstructionSimplify.h"
7070 #include "llvm/Analysis/LoopInfo.h"
43214321 // PHI's incoming blocks are in a different loop, in which case doing so
43224322 // risks breaking LCSSA form. Instcombine would normally zap these, but
43234323 // it doesn't have DominatorTree information, so it may miss cases.
4324 if (Value *V = SimplifyInstruction(PN, getDataLayout(), &TLI, &DT, &AC))
4324 if (Value *V = SimplifyInstruction(PN, getDataLayout(), &TLI, &DT))
43254325 if (LI.replacementPreservesLCSSAForm(PN, V))
43264326 return getSCEV(V);
43274327
45094509 // For a SCEVUnknown, ask ValueTracking.
45104510 unsigned BitWidth = getTypeSizeInBits(U->getType());
45114511 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
4512 computeKnownBits(U->getValue(), Zeros, Ones, getDataLayout(), 0, &AC,
4512 computeKnownBits(U->getValue(), Zeros, Ones, getDataLayout(), 0,
45134513 nullptr, &DT);
45144514 return Zeros.countTrailingOnes();
45154515 }
46804680 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
46814681 // For a SCEVUnknown, ask ValueTracking.
46824682 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
4683 computeKnownBits(U->getValue(), Zeros, Ones, DL, 0, &AC, nullptr, &DT);
4683 computeKnownBits(U->getValue(), Zeros, Ones, DL, 0, nullptr, &DT);
46844684 if (Ones != ~Zeros + 1)
46854685 ConservativeResult =
46864686 ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1));
46874687 } else {
46884688 assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
46894689 "generalize as needed!");
4690 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
4690 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, nullptr, &DT);
46914691 if (NS > 1)
46924692 ConservativeResult = ConservativeResult.intersectWith(
46934693 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
51765176 unsigned BitWidth = A.getBitWidth();
51775177 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
51785178 computeKnownBits(BO->LHS, KnownZero, KnownOne, getDataLayout(),
5179 0, &AC, nullptr, &DT);
5179 0, nullptr, &DT);
51805180
51815181 APInt EffectiveMask =
51825182 APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);
63716371 // bitwidth(K) iterations.
63726372 Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
63736373 bool KnownZero, KnownOne;
6374 ComputeSignBit(FirstValue, KnownZero, KnownOne, DL, 0, nullptr,
6374 ComputeSignBit(FirstValue, KnownZero, KnownOne, DL, 0,
63756375 Predecessor->getTerminator(), &DT);
63766376 auto *Ty = cast(RHS->getType());
63776377 if (KnownZero)
95339533 //===----------------------------------------------------------------------===//
95349534
95359535 ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
9536 AssumptionCache &AC, DominatorTree &DT,
9537 LoopInfo &LI)
9538 : F(F), TLI(TLI), AC(AC), DT(DT), LI(LI),
9536 DominatorTree &DT, LoopInfo &LI)
9537 : F(F), TLI(TLI), DT(DT), LI(LI),
95399538 CouldNotCompute(new SCEVCouldNotCompute()),
95409539 WalkingBEDominatingConds(false), ProvingSplitPredicate(false),
95419540 ValuesAtScopes(64), LoopDispositions(64), BlockDispositions(64),
95579556 }
95589557
95599558 ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
9560 : F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
9559 : F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), DT(Arg.DT),
95619560 LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
95629561 ValueExprMap(std::move(Arg.ValueExprMap)),
95639562 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
1002810027
1002910028 // Gather stringified backedge taken counts for all loops using a fresh
1003010029 // ScalarEvolution object.
10031 ScalarEvolution SE2(F, TLI, AC, DT, LI);
10030 ScalarEvolution SE2(F, TLI, DT, LI);
1003210031 for (LoopInfo::reverse_iterator I = LI.rbegin(), E = LI.rend(); I != E; ++I)
1003310032 getLoopBackedgeTakenCounts(*I, BackedgeDumpsNew, SE2);
1003410033
1006910068 ScalarEvolution ScalarEvolutionAnalysis::run(Function &F,
1007010069 FunctionAnalysisManager &AM) {
1007110070 return ScalarEvolution(F, AM.getResult(F),
10072 AM.getResult(F),
1007310071 AM.getResult(F),
1007410072 AM.getResult(F));
1007510073 }
1008210080
1008310081 INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution",
1008410082 "Scalar Evolution Analysis", false, true)
10085 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1008610083 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
1008710084 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1008810085 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
1009710094 bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) {
1009810095 SE.reset(new ScalarEvolution(
1009910096 F, getAnalysis().getTLI(),
10100 getAnalysis().getAssumptionCache(F),
1010110097 getAnalysis().getDomTree(),
1010210098 getAnalysis().getLoopInfo()));
1010310099 return false;
1011810114
1011910115 void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1012010116 AU.setPreservesAll();
10121 AU.addRequiredTransitive();
1012210117 AU.addRequiredTransitive();
1012310118 AU.addRequiredTransitive();
1012410119 AU.addRequiredTransitive();
17991799 // so narrow phis can reuse them.
18001800 for (PHINode *Phi : Phis) {
18011801 auto SimplifyPHINode = [&](PHINode *PN) -> Value * {
1802 if (Value *V = SimplifyInstruction(PN, DL, &SE.TLI, &SE.DT, &SE.AC))
1802 if (Value *V = SimplifyInstruction(PN, DL, &SE.TLI, &SE.DT))
18031803 return V;
18041804 if (!SE.isSCEVable(PN->getType()))
18051805 return nullptr;
1414 #include "llvm/Analysis/ValueTracking.h"
1515 #include "llvm/ADT/Optional.h"
1616 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/Analysis/AssumptionCache.h"
17 #include "llvm/ADT/SmallSet.h"
1818 #include "llvm/Analysis/InstructionSimplify.h"
1919 #include "llvm/Analysis/MemoryBuiltins.h"
2020 #include "llvm/Analysis/Loads.h"
7272 // figuring out if we can use it.
7373 struct Query {
7474 const DataLayout &DL;
75 AssumptionCache *AC;
7675 const Instruction *CxtI;
7776 const DominatorTree *DT;
7877
8887 std::array Excluded;
8988 unsigned NumExcluded;
9089
91 Query(const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI,
92 const DominatorTree *DT)
93 : DL(DL), AC(AC), CxtI(CxtI), DT(DT), NumExcluded(0) {}
90 Query(const DataLayout &DL, const Instruction *CxtI, const DominatorTree *DT)
91 : DL(DL), CxtI(CxtI), DT(DT), NumExcluded(0) {}
9492
9593 Query(const Query &Q, const Value *NewExcl)
96 : DL(Q.DL), AC(Q.AC), CxtI(Q.CxtI), DT(Q.DT), NumExcluded(Q.NumExcluded) {
94 : DL(Q.DL), CxtI(Q.CxtI), DT(Q.DT), NumExcluded(Q.NumExcluded) {
9795 Excluded = Q.Excluded;
9896 Excluded[NumExcluded++] = NewExcl;
9997 assert(NumExcluded <= Excluded.size());
129127
130128 void llvm::computeKnownBits(const Value *V, APInt &KnownZero, APInt &KnownOne,
131129 const DataLayout &DL, unsigned Depth,
132 AssumptionCache *AC, const Instruction *CxtI,
130 const Instruction *CxtI,
133131 const DominatorTree *DT) {
134132 ::computeKnownBits(V, KnownZero, KnownOne, Depth,
135 Query(DL, AC, safeCxtI(V, CxtI), DT));
133 Query(DL, safeCxtI(V, CxtI), DT));
136134 }
137135
138136 bool llvm::haveNoCommonBitsSet(const Value *LHS, const Value *RHS,
139137 const DataLayout &DL,
140 AssumptionCache *AC, const Instruction *CxtI,
138 const Instruction *CxtI,
141139 const DominatorTree *DT) {
142140 assert(LHS->getType() == RHS->getType() &&
143141 "LHS and RHS should have the same type");
146144 IntegerType *IT = cast(LHS->getType()->getScalarType());
147145 APInt LHSKnownZero(IT->getBitWidth(), 0), LHSKnownOne(IT->getBitWidth(), 0);
148146 APInt RHSKnownZero(IT->getBitWidth(), 0), RHSKnownOne(IT->getBitWidth(), 0);
149 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, 0, AC, CxtI, DT);
150 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, 0, AC, CxtI, DT);
147 computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, DL, 0, CxtI, DT);
148 computeKnownBits(RHS, RHSKnownZero, RHSKnownOne, DL, 0, CxtI, DT);
151149 return (LHSKnownZero | RHSKnownZero).isAllOnesValue();
152150 }
153151
156154
157155 void llvm::ComputeSignBit(const Value *V, bool &KnownZero, bool &KnownOne,
158156 const DataLayout &DL, unsigned Depth,
159 AssumptionCache *AC, const Instruction *CxtI,
157 const Instruction *CxtI,
160158 const DominatorTree *DT) {
161159 ::ComputeSignBit(V, KnownZero, KnownOne, Depth,
162 Query(DL, AC, safeCxtI(V, CxtI), DT));
160 Query(DL, safeCxtI(V, CxtI), DT));
163161 }
164162
165163 static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
167165
168166 bool llvm::isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL,
169167 bool OrZero,
170 unsigned Depth, AssumptionCache *AC,
171 const Instruction *CxtI,
168 unsigned Depth, const Instruction *CxtI,
172169 const DominatorTree *DT) {
173170 return ::isKnownToBeAPowerOfTwo(V, OrZero, Depth,
174 Query(DL, AC, safeCxtI(V, CxtI), DT));
171 Query(DL, safeCxtI(V, CxtI), DT));
175172 }
176173
177174 static bool isKnownNonZero(const Value *V, unsigned Depth, const Query &Q);
178175
179176 bool llvm::isKnownNonZero(const Value *V, const DataLayout &DL, unsigned Depth,
180 AssumptionCache *AC, const Instruction *CxtI,
181 const DominatorTree *DT) {
182 return ::isKnownNonZero(V, Depth, Query(DL, AC, safeCxtI(V, CxtI), DT));
177 const Instruction *CxtI, const DominatorTree *DT) {
178 return ::isKnownNonZero(V, Depth, Query(DL, safeCxtI(V, CxtI), DT));
183179 }
184180
185181 bool llvm::isKnownNonNegative(const Value *V, const DataLayout &DL,
186 unsigned Depth,
187 AssumptionCache *AC, const Instruction *CxtI,
182 unsigned Depth, const Instruction *CxtI,
188183 const DominatorTree *DT) {
189184 bool NonNegative, Negative;
190 ComputeSignBit(V, NonNegative, Negative, DL, Depth, AC, CxtI, DT);
185 ComputeSignBit(V, NonNegative, Negative, DL, Depth, CxtI, DT);
191186 return NonNegative;
192187 }
193188
194189 bool llvm::isKnownPositive(const Value *V, const DataLayout &DL, unsigned Depth,
195 AssumptionCache *AC, const Instruction *CxtI,
196 const DominatorTree *DT) {
190 const Instruction *CxtI, const DominatorTree *DT) {
197191 if (auto *CI = dyn_cast(V))
198192 return CI->getValue().isStrictlyPositive();
199193
200194 // TODO: We'd doing two recursive queries here. We should factor this such
201195 // that only a single query is needed.
202 return isKnownNonNegative(V, DL, Depth, AC, CxtI, DT) &&
203 isKnownNonZero(V, DL, Depth, AC, CxtI, DT);
196 return isKnownNonNegative(V, DL, Depth, CxtI, DT) &&
197 isKnownNonZero(V, DL, Depth, CxtI, DT);
204198 }
205199
206200 bool llvm::isKnownNegative(const Value *V, const DataLayout &DL, unsigned Depth,
207 AssumptionCache *AC, const Instruction *CxtI,
201 const Instruction *CxtI, const DominatorTree *DT) {
202 bool NonNegative, Negative;
203 ComputeSignBit(V, NonNegative, Negative, DL, Depth, CxtI, DT);
204 return Negative;
205 }
206
207 static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q);
208
209 bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
210 const DataLayout &DL, const Instruction *CxtI,
208211 const DominatorTree *DT) {
209 bool NonNegative, Negative;
210 ComputeSignBit(V, NonNegative, Negative, DL, Depth, AC, CxtI, DT);
211 return Negative;
212 }
213
214 static bool isKnownNonEqual(const Value *V1, const Value *V2, const Query &Q);
215
216 bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
217 const DataLayout &DL,
218 AssumptionCache *AC, const Instruction *CxtI,
219 const DominatorTree *DT) {
220 return ::isKnownNonEqual(V1, V2, Query(DL, AC,
221 safeCxtI(V1, safeCxtI(V2, CxtI)),
212 return ::isKnownNonEqual(V1, V2, Query(DL, safeCxtI(V1, safeCxtI(V2, CxtI)),
222213 DT));
223214 }
224215
227218
228219 bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
229220 const DataLayout &DL,
230 unsigned Depth, AssumptionCache *AC,
231