Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 1 | //===- LazyValueInfo.cpp - Value constraint analysis ------------*- C++ -*-===// |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interface for lazy computation of value constraint |
| 11 | // information. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/LazyValueInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ConstantFolding.h" |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ValueLattice.h" |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 24 | #include "llvm/IR/AssemblyAnnotationWriter.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 26 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Constants.h" |
| 28 | #include "llvm/IR/DataLayout.h" |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/IntrinsicInst.h" |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Intrinsics.h" |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 33 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 34 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 35 | #include "llvm/IR/ValueHandle.h" |
Chris Lattner | b584d1e | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 37 | #include "llvm/Support/FormattedStream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 4ec081a | 2012-01-11 23:43:34 +0000 | [diff] [blame] | 39 | #include <map> |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 40 | using namespace llvm; |
Benjamin Kramer | d9d80b1 | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 41 | using namespace PatternMatch; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "lazy-value-info" |
| 44 | |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 45 | // This is the number of worklist items we will process to try to discover an |
| 46 | // answer for a given value. |
| 47 | static const unsigned MaxProcessedPerValue = 500; |
| 48 | |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 49 | char LazyValueInfoWrapperPass::ID = 0; |
| 50 | INITIALIZE_PASS_BEGIN(LazyValueInfoWrapperPass, "lazy-value-info", |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 51 | "Lazy Value Information Analysis", false, true) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 52 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 55 | "Lazy Value Information Analysis", false, true) |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 56 | |
| 57 | namespace llvm { |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 58 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfoWrapperPass(); } |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 61 | AnalysisKey LazyValueAnalysis::Key; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 62 | |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 63 | /// Returns true if this lattice value represents at most one possible value. |
| 64 | /// This is as precise as any lattice value can get while still representing |
| 65 | /// reachable code. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 66 | static bool hasSingleValue(const ValueLatticeElement &Val) { |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 67 | if (Val.isConstantRange() && |
| 68 | Val.getConstantRange().isSingleElement()) |
| 69 | // Integer constants are single element ranges |
| 70 | return true; |
| 71 | if (Val.isConstant()) |
| 72 | // Non integer constants |
| 73 | return true; |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | /// Combine two sets of facts about the same value into a single set of |
| 78 | /// facts. Note that this method is not suitable for merging facts along |
| 79 | /// different paths in a CFG; that's what the mergeIn function is for. This |
| 80 | /// is for merging facts gathered about the same value at the same location |
| 81 | /// through two independent means. |
| 82 | /// Notes: |
| 83 | /// * This method does not promise to return the most precise possible lattice |
| 84 | /// value implied by A and B. It is allowed to return any lattice element |
| 85 | /// which is at least as strong as *either* A or B (unless our facts |
NAKAMURA Takumi | f252951 | 2016-07-04 01:26:27 +0000 | [diff] [blame] | 86 | /// conflict, see below). |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 87 | /// * Due to unreachable code, the intersection of two lattice values could be |
| 88 | /// contradictory. If this happens, we return some valid lattice value so as |
| 89 | /// not confuse the rest of LVI. Ideally, we'd always return Undefined, but |
| 90 | /// we do not make this guarantee. TODO: This would be a useful enhancement. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 91 | static ValueLatticeElement intersect(const ValueLatticeElement &A, |
| 92 | const ValueLatticeElement &B) { |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 93 | // Undefined is the strongest state. It means the value is known to be along |
| 94 | // an unreachable path. |
| 95 | if (A.isUndefined()) |
| 96 | return A; |
| 97 | if (B.isUndefined()) |
| 98 | return B; |
| 99 | |
| 100 | // If we gave up for one, but got a useable fact from the other, use it. |
| 101 | if (A.isOverdefined()) |
| 102 | return B; |
| 103 | if (B.isOverdefined()) |
| 104 | return A; |
| 105 | |
| 106 | // Can't get any more precise than constants. |
| 107 | if (hasSingleValue(A)) |
| 108 | return A; |
| 109 | if (hasSingleValue(B)) |
| 110 | return B; |
| 111 | |
| 112 | // Could be either constant range or not constant here. |
| 113 | if (!A.isConstantRange() || !B.isConstantRange()) { |
| 114 | // TODO: Arbitrary choice, could be improved |
| 115 | return A; |
| 116 | } |
| 117 | |
| 118 | // Intersect two constant ranges |
| 119 | ConstantRange Range = |
| 120 | A.getConstantRange().intersectWith(B.getConstantRange()); |
| 121 | // Note: An empty range is implicitly converted to overdefined internally. |
| 122 | // TODO: We could instead use Undefined here since we've proven a conflict |
NAKAMURA Takumi | f252951 | 2016-07-04 01:26:27 +0000 | [diff] [blame] | 123 | // and thus know this path must be unreachable. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 124 | return ValueLatticeElement::getRange(std::move(Range)); |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 125 | } |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 126 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 127 | //===----------------------------------------------------------------------===// |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 128 | // LazyValueInfoCache Decl |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
| 130 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 131 | namespace { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 132 | /// A callback value handle updates the cache when values are erased. |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 133 | class LazyValueInfoCache; |
David Blaikie | 774b584 | 2015-08-03 22:30:24 +0000 | [diff] [blame] | 134 | struct LVIValueHandle final : public CallbackVH { |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 135 | // Needs to access getValPtr(), which is protected. |
| 136 | friend struct DenseMapInfo<LVIValueHandle>; |
| 137 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 138 | LazyValueInfoCache *Parent; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 139 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 140 | LVIValueHandle(Value *V, LazyValueInfoCache *P) |
| 141 | : CallbackVH(V), Parent(P) { } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 142 | |
| 143 | void deleted() override; |
| 144 | void allUsesReplacedWith(Value *V) override { |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 145 | deleted(); |
| 146 | } |
| 147 | }; |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 148 | } // end anonymous namespace |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 149 | |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 150 | namespace { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 151 | /// This is the cache kept by LazyValueInfo which |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 152 | /// maintains information about queries across the clients' queries. |
| 153 | class LazyValueInfoCache { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 154 | /// This is all of the cached block information for exactly one Value*. |
| 155 | /// The entries are sorted by the BasicBlock* of the |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 156 | /// entries, allowing us to do a lookup with a binary search. |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 157 | /// Over-defined lattice values are recorded in OverDefinedCache to reduce |
| 158 | /// memory overhead. |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 159 | struct ValueCacheEntryTy { |
| 160 | ValueCacheEntryTy(Value *V, LazyValueInfoCache *P) : Handle(V, P) {} |
| 161 | LVIValueHandle Handle; |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 162 | SmallDenseMap<PoisoningVH<BasicBlock>, ValueLatticeElement, 4> BlockVals; |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 163 | }; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 164 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 165 | /// This tracks, on a per-block basis, the set of values that are |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 166 | /// over-defined at the end of that block. |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 167 | typedef DenseMap<PoisoningVH<BasicBlock>, SmallPtrSet<Value *, 4>> |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 168 | OverDefinedCacheTy; |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 169 | /// Keep track of all blocks that we have ever seen, so we |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 170 | /// don't spend time removing unused blocks from our caches. |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 171 | DenseSet<PoisoningVH<BasicBlock> > SeenBlocks; |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 172 | |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 173 | /// This is all of the cached information for all values, |
| 174 | /// mapped from Value* to key information. |
| 175 | DenseMap<Value *, std::unique_ptr<ValueCacheEntryTy>> ValueCache; |
| 176 | OverDefinedCacheTy OverDefinedCache; |
| 177 | |
| 178 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 179 | public: |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 180 | void insertResult(Value *Val, BasicBlock *BB, |
| 181 | const ValueLatticeElement &Result) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 182 | SeenBlocks.insert(BB); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 183 | |
| 184 | // Insert over-defined values into their own cache to reduce memory |
| 185 | // overhead. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 186 | if (Result.isOverdefined()) |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 187 | OverDefinedCache[BB].insert(Val); |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 188 | else { |
| 189 | auto It = ValueCache.find_as(Val); |
| 190 | if (It == ValueCache.end()) { |
| 191 | ValueCache[Val] = make_unique<ValueCacheEntryTy>(Val, this); |
| 192 | It = ValueCache.find_as(Val); |
| 193 | assert(It != ValueCache.end() && "Val was just added to the map!"); |
| 194 | } |
| 195 | It->second->BlockVals[BB] = Result; |
| 196 | } |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 197 | } |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 198 | |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 199 | bool isOverdefined(Value *V, BasicBlock *BB) const { |
| 200 | auto ODI = OverDefinedCache.find(BB); |
| 201 | |
| 202 | if (ODI == OverDefinedCache.end()) |
| 203 | return false; |
| 204 | |
| 205 | return ODI->second.count(V); |
| 206 | } |
| 207 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 208 | bool hasCachedValueInfo(Value *V, BasicBlock *BB) const { |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 209 | if (isOverdefined(V, BB)) |
| 210 | return true; |
| 211 | |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 212 | auto I = ValueCache.find_as(V); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 213 | if (I == ValueCache.end()) |
| 214 | return false; |
| 215 | |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 216 | return I->second->BlockVals.count(BB); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 219 | ValueLatticeElement getCachedValueInfo(Value *V, BasicBlock *BB) const { |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 220 | if (isOverdefined(V, BB)) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 221 | return ValueLatticeElement::getOverdefined(); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 222 | |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 223 | auto I = ValueCache.find_as(V); |
| 224 | if (I == ValueCache.end()) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 225 | return ValueLatticeElement(); |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 226 | auto BBI = I->second->BlockVals.find(BB); |
| 227 | if (BBI == I->second->BlockVals.end()) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 228 | return ValueLatticeElement(); |
Justin Lebar | 58b377e | 2016-07-27 22:33:36 +0000 | [diff] [blame] | 229 | return BBI->second; |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 230 | } |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 231 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 232 | /// clear - Empty the cache. |
| 233 | void clear() { |
| 234 | SeenBlocks.clear(); |
| 235 | ValueCache.clear(); |
| 236 | OverDefinedCache.clear(); |
| 237 | } |
| 238 | |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 239 | /// Inform the cache that a given value has been deleted. |
| 240 | void eraseValue(Value *V); |
| 241 | |
| 242 | /// This is part of the update interface to inform the cache |
| 243 | /// that a block has been deleted. |
| 244 | void eraseBlock(BasicBlock *BB); |
| 245 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 246 | /// Updates the cache to remove any influence an overdefined value in |
| 247 | /// OldSucc might have (unless also overdefined in NewSucc). This just |
| 248 | /// flushes elements from the cache and does not add any. |
| 249 | void threadEdgeImpl(BasicBlock *OldSucc,BasicBlock *NewSucc); |
| 250 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 251 | friend struct LVIValueHandle; |
| 252 | }; |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 253 | } |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 254 | |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 255 | void LazyValueInfoCache::eraseValue(Value *V) { |
Chandler Carruth | 41421df | 2017-01-26 08:31:54 +0000 | [diff] [blame] | 256 | for (auto I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E;) { |
| 257 | // Copy and increment the iterator immediately so we can erase behind |
| 258 | // ourselves. |
| 259 | auto Iter = I++; |
| 260 | SmallPtrSetImpl<Value *> &ValueSet = Iter->second; |
Philip Reames | fdbb05b | 2016-12-30 22:09:10 +0000 | [diff] [blame] | 261 | ValueSet.erase(V); |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 262 | if (ValueSet.empty()) |
Chandler Carruth | 41421df | 2017-01-26 08:31:54 +0000 | [diff] [blame] | 263 | OverDefinedCache.erase(Iter); |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 264 | } |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 265 | |
| 266 | ValueCache.erase(V); |
| 267 | } |
| 268 | |
| 269 | void LVIValueHandle::deleted() { |
| 270 | // This erasure deallocates *this, so it MUST happen after we're done |
| 271 | // using any and all members of *this. |
| 272 | Parent->eraseValue(*this); |
| 273 | } |
| 274 | |
| 275 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
| 276 | // Shortcut if we have never seen this block. |
Chandler Carruth | 6acdca7 | 2017-01-24 12:55:57 +0000 | [diff] [blame] | 277 | DenseSet<PoisoningVH<BasicBlock> >::iterator I = SeenBlocks.find(BB); |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 278 | if (I == SeenBlocks.end()) |
| 279 | return; |
| 280 | SeenBlocks.erase(I); |
| 281 | |
| 282 | auto ODI = OverDefinedCache.find(BB); |
| 283 | if (ODI != OverDefinedCache.end()) |
| 284 | OverDefinedCache.erase(ODI); |
| 285 | |
| 286 | for (auto &I : ValueCache) |
| 287 | I.second->BlockVals.erase(BB); |
| 288 | } |
| 289 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 290 | void LazyValueInfoCache::threadEdgeImpl(BasicBlock *OldSucc, |
| 291 | BasicBlock *NewSucc) { |
| 292 | // When an edge in the graph has been threaded, values that we could not |
| 293 | // determine a value for before (i.e. were marked overdefined) may be |
| 294 | // possible to solve now. We do NOT try to proactively update these values. |
| 295 | // Instead, we clear their entries from the cache, and allow lazy updating to |
| 296 | // recompute them when needed. |
| 297 | |
| 298 | // The updating process is fairly simple: we need to drop cached info |
| 299 | // for all values that were marked overdefined in OldSucc, and for those same |
| 300 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 301 | // also marked overdefined. |
| 302 | std::vector<BasicBlock*> worklist; |
| 303 | worklist.push_back(OldSucc); |
| 304 | |
| 305 | auto I = OverDefinedCache.find(OldSucc); |
| 306 | if (I == OverDefinedCache.end()) |
| 307 | return; // Nothing to process here. |
| 308 | SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end()); |
| 309 | |
| 310 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 311 | // NOTE: We do not need a visited list since any blocks we have already |
| 312 | // visited will have had their overdefined markers cleared already, and we |
| 313 | // thus won't loop to their successors. |
| 314 | while (!worklist.empty()) { |
| 315 | BasicBlock *ToUpdate = worklist.back(); |
| 316 | worklist.pop_back(); |
| 317 | |
| 318 | // Skip blocks only accessible through NewSucc. |
| 319 | if (ToUpdate == NewSucc) continue; |
| 320 | |
Philip Reames | 1e48efc | 2016-12-30 17:56:47 +0000 | [diff] [blame] | 321 | // If a value was marked overdefined in OldSucc, and is here too... |
| 322 | auto OI = OverDefinedCache.find(ToUpdate); |
| 323 | if (OI == OverDefinedCache.end()) |
| 324 | continue; |
| 325 | SmallPtrSetImpl<Value *> &ValueSet = OI->second; |
| 326 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 327 | bool changed = false; |
| 328 | for (Value *V : ValsToClear) { |
Philip Reames | fdbb05b | 2016-12-30 22:09:10 +0000 | [diff] [blame] | 329 | if (!ValueSet.erase(V)) |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 330 | continue; |
| 331 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 332 | // If we removed anything, then we potentially need to update |
| 333 | // blocks successors too. |
| 334 | changed = true; |
Philip Reames | 1e48efc | 2016-12-30 17:56:47 +0000 | [diff] [blame] | 335 | |
| 336 | if (ValueSet.empty()) { |
| 337 | OverDefinedCache.erase(OI); |
| 338 | break; |
| 339 | } |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (!changed) continue; |
| 343 | |
| 344 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 345 | } |
| 346 | } |
| 347 | |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 348 | |
| 349 | namespace { |
| 350 | /// An assembly annotator class to print LazyValueCache information in |
| 351 | /// comments. |
| 352 | class LazyValueInfoImpl; |
| 353 | class LazyValueInfoAnnotatedWriter : public AssemblyAnnotationWriter { |
| 354 | LazyValueInfoImpl *LVIImpl; |
| 355 | // While analyzing which blocks we can solve values for, we need the dominator |
| 356 | // information. Since this is an optional parameter in LVI, we require this |
| 357 | // DomTreeAnalysis pass in the printer pass, and pass the dominator |
| 358 | // tree to the LazyValueInfoAnnotatedWriter. |
| 359 | DominatorTree &DT; |
| 360 | |
| 361 | public: |
| 362 | LazyValueInfoAnnotatedWriter(LazyValueInfoImpl *L, DominatorTree &DTree) |
| 363 | : LVIImpl(L), DT(DTree) {} |
| 364 | |
| 365 | virtual void emitBasicBlockStartAnnot(const BasicBlock *BB, |
| 366 | formatted_raw_ostream &OS); |
| 367 | |
| 368 | virtual void emitInstructionAnnot(const Instruction *I, |
| 369 | formatted_raw_ostream &OS); |
| 370 | }; |
| 371 | } |
Philip Reames | b627aec | 2016-09-12 22:03:36 +0000 | [diff] [blame] | 372 | namespace { |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 373 | // The actual implementation of the lazy analysis and update. Note that the |
| 374 | // inheritance from LazyValueInfoCache is intended to be temporary while |
| 375 | // splitting the code and then transitioning to a has-a relationship. |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 376 | class LazyValueInfoImpl { |
| 377 | |
| 378 | /// Cached results from previous queries |
| 379 | LazyValueInfoCache TheCache; |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 380 | |
| 381 | /// This stack holds the state of the value solver during a query. |
| 382 | /// It basically emulates the callstack of the naive |
| 383 | /// recursive value lookup process. |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 384 | SmallVector<std::pair<BasicBlock*, Value*>, 8> BlockValueStack; |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 385 | |
| 386 | /// Keeps track of which block-value pairs are in BlockValueStack. |
| 387 | DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet; |
| 388 | |
| 389 | /// Push BV onto BlockValueStack unless it's already in there. |
| 390 | /// Returns true on success. |
| 391 | bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) { |
| 392 | if (!BlockValueSet.insert(BV).second) |
| 393 | return false; // It's already in the stack. |
| 394 | |
| 395 | DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName() |
| 396 | << "\n"); |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 397 | BlockValueStack.push_back(BV); |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 398 | return true; |
| 399 | } |
| 400 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 401 | AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls. |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 402 | const DataLayout &DL; ///< A mandatory DataLayout |
| 403 | DominatorTree *DT; ///< An optional DT pointer. |
Brian M. Rzycki | f1a7df5 | 2018-02-16 16:35:17 +0000 | [diff] [blame] | 404 | DominatorTree *DisabledDT; ///< Stores DT if it's disabled. |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 405 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 406 | ValueLatticeElement getBlockValue(Value *Val, BasicBlock *BB); |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 407 | bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 408 | ValueLatticeElement &Result, Instruction *CxtI = nullptr); |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 409 | bool hasBlockValue(Value *Val, BasicBlock *BB); |
| 410 | |
| 411 | // These methods process one work item and may add more. A false value |
| 412 | // returned means that the work item was not completely processed and must |
| 413 | // be revisited after going through the new items. |
| 414 | bool solveBlockValue(Value *Val, BasicBlock *BB); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 415 | bool solveBlockValueImpl(ValueLatticeElement &Res, Value *Val, |
| 416 | BasicBlock *BB); |
| 417 | bool solveBlockValueNonLocal(ValueLatticeElement &BBLV, Value *Val, |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 418 | BasicBlock *BB); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 419 | bool solveBlockValuePHINode(ValueLatticeElement &BBLV, PHINode *PN, |
| 420 | BasicBlock *BB); |
| 421 | bool solveBlockValueSelect(ValueLatticeElement &BBLV, SelectInst *S, |
| 422 | BasicBlock *BB); |
| 423 | bool solveBlockValueBinaryOp(ValueLatticeElement &BBLV, BinaryOperator *BBI, |
| 424 | BasicBlock *BB); |
| 425 | bool solveBlockValueCast(ValueLatticeElement &BBLV, CastInst *CI, |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 426 | BasicBlock *BB); |
| 427 | void intersectAssumeOrGuardBlockValueConstantRange(Value *Val, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 428 | ValueLatticeElement &BBLV, |
Craig Topper | 9277a86 | 2017-06-02 17:28:12 +0000 | [diff] [blame] | 429 | Instruction *BBI); |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 430 | |
| 431 | void solve(); |
| 432 | |
| 433 | public: |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 434 | /// This is the query interface to determine the lattice |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 435 | /// value for the specified Value* at the end of the specified block. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 436 | ValueLatticeElement getValueInBlock(Value *V, BasicBlock *BB, |
| 437 | Instruction *CxtI = nullptr); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 438 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 439 | /// This is the query interface to determine the lattice |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 440 | /// value for the specified Value* at the specified instruction (generally |
| 441 | /// from an assume intrinsic). |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 442 | ValueLatticeElement getValueAt(Value *V, Instruction *CxtI); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 443 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 444 | /// This is the query interface to determine the lattice |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 445 | /// value for the specified Value* that is true on the specified edge. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 446 | ValueLatticeElement getValueOnEdge(Value *V, BasicBlock *FromBB, |
| 447 | BasicBlock *ToBB, |
| 448 | Instruction *CxtI = nullptr); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 449 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 450 | /// Complete flush all previously computed values |
| 451 | void clear() { |
| 452 | TheCache.clear(); |
| 453 | } |
| 454 | |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 455 | /// Printing the LazyValueInfo Analysis. |
| 456 | void printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) { |
| 457 | LazyValueInfoAnnotatedWriter Writer(this, DTree); |
| 458 | F.print(OS, &Writer); |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 461 | /// This is part of the update interface to inform the cache |
| 462 | /// that a block has been deleted. |
| 463 | void eraseBlock(BasicBlock *BB) { |
| 464 | TheCache.eraseBlock(BB); |
| 465 | } |
| 466 | |
Brian M. Rzycki | f1a7df5 | 2018-02-16 16:35:17 +0000 | [diff] [blame] | 467 | /// Disables use of the DominatorTree within LVI. |
| 468 | void disableDT() { |
| 469 | if (DT) { |
| 470 | assert(!DisabledDT && "Both DT and DisabledDT are not nullptr!"); |
| 471 | std::swap(DT, DisabledDT); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /// Enables use of the DominatorTree within LVI. Does nothing if the class |
| 476 | /// instance was initialized without a DT pointer. |
| 477 | void enableDT() { |
| 478 | if (DisabledDT) { |
| 479 | assert(!DT && "Both DT and DisabledDT are not nullptr!"); |
| 480 | std::swap(DT, DisabledDT); |
| 481 | } |
| 482 | } |
| 483 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 484 | /// This is the update interface to inform the cache that an edge from |
| 485 | /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc. |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 486 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 487 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 488 | LazyValueInfoImpl(AssumptionCache *AC, const DataLayout &DL, |
| 489 | DominatorTree *DT = nullptr) |
Brian M. Rzycki | f1a7df5 | 2018-02-16 16:35:17 +0000 | [diff] [blame] | 490 | : AC(AC), DL(DL), DT(DT), DisabledDT(nullptr) {} |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 491 | }; |
| 492 | } // end anonymous namespace |
| 493 | |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 494 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 495 | void LazyValueInfoImpl::solve() { |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 496 | SmallVector<std::pair<BasicBlock *, Value *>, 8> StartingStack( |
| 497 | BlockValueStack.begin(), BlockValueStack.end()); |
| 498 | |
| 499 | unsigned processedCount = 0; |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 500 | while (!BlockValueStack.empty()) { |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 501 | processedCount++; |
| 502 | // Abort if we have to process too many values to get a result for this one. |
| 503 | // Because of the design of the overdefined cache currently being per-block |
| 504 | // to avoid naming-related issues (IE it wants to try to give different |
| 505 | // results for the same name in different blocks), overdefined results don't |
| 506 | // get cached globally, which in turn means we will often try to rediscover |
| 507 | // the same overdefined result again and again. Once something like |
| 508 | // PredicateInfo is used in LVI or CVP, we should be able to make the |
| 509 | // overdefined cache global, and remove this throttle. |
| 510 | if (processedCount > MaxProcessedPerValue) { |
| 511 | DEBUG(dbgs() << "Giving up on stack because we are getting too deep\n"); |
| 512 | // Fill in the original values |
| 513 | while (!StartingStack.empty()) { |
| 514 | std::pair<BasicBlock *, Value *> &e = StartingStack.back(); |
| 515 | TheCache.insertResult(e.second, e.first, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 516 | ValueLatticeElement::getOverdefined()); |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 517 | StartingStack.pop_back(); |
| 518 | } |
| 519 | BlockValueSet.clear(); |
| 520 | BlockValueStack.clear(); |
| 521 | return; |
| 522 | } |
Vitaly Buka | 9987d98 | 2017-02-09 09:28:05 +0000 | [diff] [blame] | 523 | std::pair<BasicBlock *, Value *> e = BlockValueStack.back(); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 524 | assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!"); |
| 525 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 526 | if (solveBlockValue(e.second, e.first)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 527 | // The work item was completely processed. |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 528 | assert(BlockValueStack.back() == e && "Nothing should have been pushed!"); |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 529 | assert(TheCache.hasCachedValueInfo(e.second, e.first) && |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 530 | "Result should be in cache!"); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 531 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 532 | DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName() |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 533 | << " = " << TheCache.getCachedValueInfo(e.second, e.first) << "\n"); |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 534 | |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 535 | BlockValueStack.pop_back(); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 536 | BlockValueSet.erase(e); |
| 537 | } else { |
| 538 | // More work needs to be done before revisiting. |
Daniel Berlin | 9c92a46 | 2017-02-08 15:22:52 +0000 | [diff] [blame] | 539 | assert(BlockValueStack.back() != e && "Stack should have been pushed!"); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 540 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 544 | bool LazyValueInfoImpl::hasBlockValue(Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 545 | // If already a constant, there is nothing to compute. |
| 546 | if (isa<Constant>(Val)) |
| 547 | return true; |
| 548 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 549 | return TheCache.hasCachedValueInfo(Val, BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 552 | ValueLatticeElement LazyValueInfoImpl::getBlockValue(Value *Val, |
| 553 | BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 554 | // If already a constant, there is nothing to compute. |
| 555 | if (Constant *VC = dyn_cast<Constant>(Val)) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 556 | return ValueLatticeElement::get(VC); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 557 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 558 | return TheCache.getCachedValueInfo(Val, BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 561 | static ValueLatticeElement getFromRangeMetadata(Instruction *BBI) { |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 562 | switch (BBI->getOpcode()) { |
| 563 | default: break; |
| 564 | case Instruction::Load: |
| 565 | case Instruction::Call: |
| 566 | case Instruction::Invoke: |
NAKAMURA Takumi | bd072a9 | 2016-07-25 00:59:46 +0000 | [diff] [blame] | 567 | if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range)) |
Philip Reames | 70efccd | 2015-10-29 04:21:49 +0000 | [diff] [blame] | 568 | if (isa<IntegerType>(BBI->getType())) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 569 | return ValueLatticeElement::getRange( |
| 570 | getConstantRangeFromMetadata(*Ranges)); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 571 | } |
| 572 | break; |
| 573 | }; |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 574 | // Nothing known - will be intersected with other facts |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 575 | return ValueLatticeElement::getOverdefined(); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 578 | bool LazyValueInfoImpl::solveBlockValue(Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 579 | if (isa<Constant>(Val)) |
| 580 | return true; |
| 581 | |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 582 | if (TheCache.hasCachedValueInfo(Val, BB)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 583 | // If we have a cached value, use that. |
| 584 | DEBUG(dbgs() << " reuse BB '" << BB->getName() |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 585 | << "' val=" << TheCache.getCachedValueInfo(Val, BB) << '\n'); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 586 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 587 | // Since we're reusing a cached value, we don't need to update the |
| 588 | // OverDefinedCache. The cache will have been properly updated whenever the |
| 589 | // cached value was inserted. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 590 | return true; |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 593 | // Hold off inserting this value into the Cache in case we have to return |
| 594 | // false and come back later. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 595 | ValueLatticeElement Res; |
Philip Reames | 05c435e | 2016-12-06 03:22:03 +0000 | [diff] [blame] | 596 | if (!solveBlockValueImpl(Res, Val, BB)) |
| 597 | // Work pushed, will revisit |
| 598 | return false; |
| 599 | |
| 600 | TheCache.insertResult(Val, BB, Res); |
| 601 | return true; |
| 602 | } |
| 603 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 604 | bool LazyValueInfoImpl::solveBlockValueImpl(ValueLatticeElement &Res, |
Philip Reames | 05c435e | 2016-12-06 03:22:03 +0000 | [diff] [blame] | 605 | Value *Val, BasicBlock *BB) { |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 606 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 607 | Instruction *BBI = dyn_cast<Instruction>(Val); |
Philip Reames | 05c435e | 2016-12-06 03:22:03 +0000 | [diff] [blame] | 608 | if (!BBI || BBI->getParent() != BB) |
| 609 | return solveBlockValueNonLocal(Res, Val, BB); |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 610 | |
Philip Reames | 05c435e | 2016-12-06 03:22:03 +0000 | [diff] [blame] | 611 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) |
| 612 | return solveBlockValuePHINode(Res, PN, BB); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 613 | |
Philip Reames | 05c435e | 2016-12-06 03:22:03 +0000 | [diff] [blame] | 614 | if (auto *SI = dyn_cast<SelectInst>(BBI)) |
| 615 | return solveBlockValueSelect(Res, SI, BB); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 616 | |
Philip Reames | 2ab964e | 2016-04-27 01:02:25 +0000 | [diff] [blame] | 617 | // If this value is a nonnull pointer, record it's range and bailout. Note |
| 618 | // that for all other pointer typed values, we terminate the search at the |
| 619 | // definition. We could easily extend this to look through geps, bitcasts, |
| 620 | // and the like to prove non-nullness, but it's not clear that's worth it |
Craig Topper | 7ad13f2 | 2017-06-09 21:21:17 +0000 | [diff] [blame] | 621 | // compile time wise. The context-insensitive value walk done inside |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 622 | // isKnownNonZero gets most of the profitable cases at much less expense. |
Philip Reames | 2ab964e | 2016-04-27 01:02:25 +0000 | [diff] [blame] | 623 | // This does mean that we have a sensativity to where the defining |
| 624 | // instruction is placed, even if it could legally be hoisted much higher. |
| 625 | // That is unfortunate. |
Igor Laevsky | 0fa4819 | 2015-09-18 13:01:48 +0000 | [diff] [blame] | 626 | PointerType *PT = dyn_cast<PointerType>(BBI->getType()); |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 627 | if (PT && isKnownNonZero(BBI, DL)) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 628 | Res = ValueLatticeElement::getNot(ConstantPointerNull::get(PT)); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 629 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 630 | } |
Davide Italiano | bd543d0 | 2016-05-25 22:29:34 +0000 | [diff] [blame] | 631 | if (BBI->getType()->isIntegerTy()) { |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 632 | if (auto *CI = dyn_cast<CastInst>(BBI)) |
| 633 | return solveBlockValueCast(Res, CI, BB); |
| 634 | |
Philip Reames | 2ab964e | 2016-04-27 01:02:25 +0000 | [diff] [blame] | 635 | BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI); |
Philip Reames | 05c435e | 2016-12-06 03:22:03 +0000 | [diff] [blame] | 636 | if (BO && isa<ConstantInt>(BO->getOperand(1))) |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 637 | return solveBlockValueBinaryOp(Res, BO, BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 638 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 639 | |
Philip Reames | a0c9f6e | 2016-03-04 22:27:39 +0000 | [diff] [blame] | 640 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 641 | << "' - unknown inst def found.\n"); |
| 642 | Res = getFromRangeMetadata(BBI); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 643 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) { |
| 647 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 648 | return L->getPointerAddressSpace() == 0 && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 649 | GetUnderlyingObject(L->getPointerOperand(), |
| 650 | L->getModule()->getDataLayout()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 651 | } |
| 652 | if (StoreInst *S = dyn_cast<StoreInst>(I)) { |
| 653 | return S->getPointerAddressSpace() == 0 && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 654 | GetUnderlyingObject(S->getPointerOperand(), |
| 655 | S->getModule()->getDataLayout()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 656 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 657 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 658 | if (MI->isVolatile()) return false; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 659 | |
| 660 | // FIXME: check whether it has a valuerange that excludes zero? |
| 661 | ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 662 | if (!Len || Len->isZero()) return false; |
| 663 | |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 664 | if (MI->getDestAddressSpace() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 665 | if (GetUnderlyingObject(MI->getRawDest(), |
| 666 | MI->getModule()->getDataLayout()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 667 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 668 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 669 | if (MTI->getSourceAddressSpace() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 670 | if (GetUnderlyingObject(MTI->getRawSource(), |
| 671 | MTI->getModule()->getDataLayout()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 672 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 673 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 674 | return false; |
| 675 | } |
| 676 | |
Philip Reames | 3f83dbe | 2016-04-27 00:30:55 +0000 | [diff] [blame] | 677 | /// Return true if the allocation associated with Val is ever dereferenced |
| 678 | /// within the given basic block. This establishes the fact Val is not null, |
| 679 | /// but does not imply that the memory at Val is dereferenceable. (Val may |
| 680 | /// point off the end of the dereferenceable part of the object.) |
| 681 | static bool isObjectDereferencedInBlock(Value *Val, BasicBlock *BB) { |
| 682 | assert(Val->getType()->isPointerTy()); |
| 683 | |
| 684 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 685 | Value *UnderlyingVal = GetUnderlyingObject(Val, DL); |
| 686 | // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge |
| 687 | // inside InstructionDereferencesPointer either. |
| 688 | if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1)) |
| 689 | for (Instruction &I : *BB) |
| 690 | if (InstructionDereferencesPointer(&I, UnderlyingVal)) |
| 691 | return true; |
| 692 | return false; |
| 693 | } |
| 694 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 695 | bool LazyValueInfoImpl::solveBlockValueNonLocal(ValueLatticeElement &BBLV, |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 696 | Value *Val, BasicBlock *BB) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 697 | ValueLatticeElement Result; // Start Undefined. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 698 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 699 | // If this is the entry block, we must be asking about an argument. The |
| 700 | // value is overdefined. |
| 701 | if (BB == &BB->getParent()->getEntryBlock()) { |
| 702 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
Craig Topper | 96d6ee85 | 2017-04-28 16:57:59 +0000 | [diff] [blame] | 703 | // Before giving up, see if we can prove the pointer non-null local to |
Philip Reames | 3f83dbe | 2016-04-27 00:30:55 +0000 | [diff] [blame] | 704 | // this particular block. |
| 705 | if (Val->getType()->isPointerTy() && |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 706 | (isKnownNonZero(Val, DL) || isObjectDereferencedInBlock(Val, BB))) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 707 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 708 | Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 709 | } else { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 710 | Result = ValueLatticeElement::getOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 711 | } |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 712 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 713 | return true; |
| 714 | } |
| 715 | |
| 716 | // Loop over all of our predecessors, merging what we know from them into |
Philip Reames | c80bd04 | 2017-02-07 00:25:24 +0000 | [diff] [blame] | 717 | // result. If we encounter an unexplored predecessor, we eagerly explore it |
| 718 | // in a depth first manner. In practice, this has the effect of discovering |
| 719 | // paths we can't analyze eagerly without spending compile times analyzing |
| 720 | // other paths. This heuristic benefits from the fact that predecessors are |
| 721 | // frequently arranged such that dominating ones come first and we quickly |
| 722 | // find a path to function entry. TODO: We should consider explicitly |
| 723 | // canonicalizing to make this true rather than relying on this happy |
| 724 | // accident. |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 725 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 726 | ValueLatticeElement EdgeResult; |
Philip Reames | c80bd04 | 2017-02-07 00:25:24 +0000 | [diff] [blame] | 727 | if (!getEdgeValue(Val, *PI, BB, EdgeResult)) |
| 728 | // Explore that input, then return here |
| 729 | return false; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 730 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 731 | Result.mergeIn(EdgeResult, DL); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 732 | |
| 733 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 734 | // to overdefined. |
| 735 | if (Result.isOverdefined()) { |
| 736 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Philip Reames | b757104 | 2016-02-02 22:43:08 +0000 | [diff] [blame] | 737 | << "' - overdefined because of pred (non local).\n"); |
Artur Pilipenko | adcd01f | 2016-08-09 09:14:29 +0000 | [diff] [blame] | 738 | // Before giving up, see if we can prove the pointer non-null local to |
Philip Reames | 3f83dbe | 2016-04-27 00:30:55 +0000 | [diff] [blame] | 739 | // this particular block. |
| 740 | if (Val->getType()->isPointerTy() && |
| 741 | isObjectDereferencedInBlock(Val, BB)) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 742 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 743 | Result = ValueLatticeElement::getNot(ConstantPointerNull::get(PTy)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 744 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 745 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 746 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 750 | |
| 751 | // Return the merged value, which is more precise than 'overdefined'. |
| 752 | assert(!Result.isOverdefined()); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 753 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 754 | return true; |
| 755 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 756 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 757 | bool LazyValueInfoImpl::solveBlockValuePHINode(ValueLatticeElement &BBLV, |
| 758 | PHINode *PN, BasicBlock *BB) { |
| 759 | ValueLatticeElement Result; // Start Undefined. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 760 | |
| 761 | // Loop over all of our predecessors, merging what we know from them into |
Philip Reames | c80bd04 | 2017-02-07 00:25:24 +0000 | [diff] [blame] | 762 | // result. See the comment about the chosen traversal order in |
| 763 | // solveBlockValueNonLocal; the same reasoning applies here. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 764 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 765 | BasicBlock *PhiBB = PN->getIncomingBlock(i); |
| 766 | Value *PhiVal = PN->getIncomingValue(i); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 767 | ValueLatticeElement EdgeResult; |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 768 | // Note that we can provide PN as the context value to getEdgeValue, even |
| 769 | // though the results will be cached, because PN is the value being used as |
| 770 | // the cache key in the caller. |
Philip Reames | c80bd04 | 2017-02-07 00:25:24 +0000 | [diff] [blame] | 771 | if (!getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN)) |
| 772 | // Explore that input, then return here |
| 773 | return false; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 774 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 775 | Result.mergeIn(EdgeResult, DL); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 776 | |
| 777 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 778 | // to overdefined. |
| 779 | if (Result.isOverdefined()) { |
| 780 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Philip Reames | b757104 | 2016-02-02 22:43:08 +0000 | [diff] [blame] | 781 | << "' - overdefined because of pred (local).\n"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 782 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 783 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 784 | return true; |
| 785 | } |
| 786 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 787 | |
| 788 | // Return the merged value, which is more precise than 'overdefined'. |
| 789 | assert(!Result.isOverdefined() && "Possible PHI in entry block?"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 790 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 791 | return true; |
| 792 | } |
| 793 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 794 | static ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond, |
| 795 | bool isTrueDest = true); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 796 | |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 797 | // If we can determine a constraint on the value given conditions assumed by |
| 798 | // the program, intersect those constraints with BBLV |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 799 | void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange( |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 800 | Value *Val, ValueLatticeElement &BBLV, Instruction *BBI) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 801 | BBI = BBI ? BBI : dyn_cast<Instruction>(Val); |
| 802 | if (!BBI) |
| 803 | return; |
| 804 | |
Hal Finkel | 8a9a783 | 2017-01-11 13:24:24 +0000 | [diff] [blame] | 805 | for (auto &AssumeVH : AC->assumptionsFor(Val)) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 806 | if (!AssumeVH) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 807 | continue; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 808 | auto *I = cast<CallInst>(AssumeVH); |
| 809 | if (!isValidAssumeForContext(I, BBI, DT)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 810 | continue; |
| 811 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 812 | BBLV = intersect(BBLV, getValueFromCondition(Val, I->getArgOperand(0))); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 813 | } |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 814 | |
| 815 | // If guards are not used in the module, don't spend time looking for them |
| 816 | auto *GuardDecl = BBI->getModule()->getFunction( |
| 817 | Intrinsic::getName(Intrinsic::experimental_guard)); |
| 818 | if (!GuardDecl || GuardDecl->use_empty()) |
| 819 | return; |
| 820 | |
Artur Pilipenko | 47dc098 | 2016-10-21 15:02:21 +0000 | [diff] [blame] | 821 | for (Instruction &I : make_range(BBI->getIterator().getReverse(), |
| 822 | BBI->getParent()->rend())) { |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 823 | Value *Cond = nullptr; |
Artur Pilipenko | 47dc098 | 2016-10-21 15:02:21 +0000 | [diff] [blame] | 824 | if (match(&I, m_Intrinsic<Intrinsic::experimental_guard>(m_Value(Cond)))) |
| 825 | BBLV = intersect(BBLV, getValueFromCondition(Val, Cond)); |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 826 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 829 | bool LazyValueInfoImpl::solveBlockValueSelect(ValueLatticeElement &BBLV, |
| 830 | SelectInst *SI, BasicBlock *BB) { |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 831 | |
| 832 | // Recurse on our inputs if needed |
| 833 | if (!hasBlockValue(SI->getTrueValue(), BB)) { |
| 834 | if (pushBlockValue(std::make_pair(BB, SI->getTrueValue()))) |
| 835 | return false; |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 836 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 837 | return true; |
| 838 | } |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 839 | ValueLatticeElement TrueVal = getBlockValue(SI->getTrueValue(), BB); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 840 | // If we hit overdefined, don't ask more queries. We want to avoid poisoning |
| 841 | // extra slots in the table if we can. |
| 842 | if (TrueVal.isOverdefined()) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 843 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 844 | return true; |
| 845 | } |
| 846 | |
| 847 | if (!hasBlockValue(SI->getFalseValue(), BB)) { |
| 848 | if (pushBlockValue(std::make_pair(BB, SI->getFalseValue()))) |
| 849 | return false; |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 850 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 851 | return true; |
| 852 | } |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 853 | ValueLatticeElement FalseVal = getBlockValue(SI->getFalseValue(), BB); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 854 | // If we hit overdefined, don't ask more queries. We want to avoid poisoning |
| 855 | // extra slots in the table if we can. |
| 856 | if (FalseVal.isOverdefined()) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 857 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 858 | return true; |
| 859 | } |
| 860 | |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 861 | if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) { |
Craig Topper | 2b195fd | 2017-05-06 03:35:15 +0000 | [diff] [blame] | 862 | const ConstantRange &TrueCR = TrueVal.getConstantRange(); |
| 863 | const ConstantRange &FalseCR = FalseVal.getConstantRange(); |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 864 | Value *LHS = nullptr; |
| 865 | Value *RHS = nullptr; |
| 866 | SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS); |
| 867 | // Is this a min specifically of our two inputs? (Avoid the risk of |
| 868 | // ValueTracking getting smarter looking back past our immediate inputs.) |
| 869 | if (SelectPatternResult::isMinOrMax(SPR.Flavor) && |
| 870 | LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) { |
Philip Reames | b294962 | 2016-12-06 02:54:16 +0000 | [diff] [blame] | 871 | ConstantRange ResultCR = [&]() { |
| 872 | switch (SPR.Flavor) { |
| 873 | default: |
| 874 | llvm_unreachable("unexpected minmax type!"); |
| 875 | case SPF_SMIN: /// Signed minimum |
| 876 | return TrueCR.smin(FalseCR); |
| 877 | case SPF_UMIN: /// Unsigned minimum |
| 878 | return TrueCR.umin(FalseCR); |
| 879 | case SPF_SMAX: /// Signed maximum |
| 880 | return TrueCR.smax(FalseCR); |
| 881 | case SPF_UMAX: /// Unsigned maximum |
| 882 | return TrueCR.umax(FalseCR); |
| 883 | }; |
| 884 | }(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 885 | BBLV = ValueLatticeElement::getRange(ResultCR); |
Philip Reames | b294962 | 2016-12-06 02:54:16 +0000 | [diff] [blame] | 886 | return true; |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 887 | } |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 888 | |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 889 | // TODO: ABS, NABS from the SelectPatternResult |
| 890 | } |
| 891 | |
Philip Reames | 854a84c | 2016-02-12 00:09:18 +0000 | [diff] [blame] | 892 | // Can we constrain the facts about the true and false values by using the |
| 893 | // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5). |
| 894 | // TODO: We could potentially refine an overdefined true value above. |
Artur Pilipenko | 2e19f59 | 2016-08-02 16:20:48 +0000 | [diff] [blame] | 895 | Value *Cond = SI->getCondition(); |
Artur Pilipenko | 933c07a | 2016-08-10 13:38:07 +0000 | [diff] [blame] | 896 | TrueVal = intersect(TrueVal, |
| 897 | getValueFromCondition(SI->getTrueValue(), Cond, true)); |
| 898 | FalseVal = intersect(FalseVal, |
| 899 | getValueFromCondition(SI->getFalseValue(), Cond, false)); |
Philip Reames | 854a84c | 2016-02-12 00:09:18 +0000 | [diff] [blame] | 900 | |
Artur Pilipenko | 2e19f59 | 2016-08-02 16:20:48 +0000 | [diff] [blame] | 901 | // Handle clamp idioms such as: |
| 902 | // %24 = constantrange<0, 17> |
| 903 | // %39 = icmp eq i32 %24, 0 |
| 904 | // %40 = add i32 %24, -1 |
| 905 | // %siv.next = select i1 %39, i32 16, i32 %40 |
| 906 | // %siv.next = constantrange<0, 17> not <-1, 17> |
| 907 | // In general, this can handle any clamp idiom which tests the edge |
| 908 | // condition via an equality or inequality. |
| 909 | if (auto *ICI = dyn_cast<ICmpInst>(Cond)) { |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 910 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 911 | Value *A = ICI->getOperand(0); |
| 912 | if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 913 | auto addConstants = [](ConstantInt *A, ConstantInt *B) { |
| 914 | assert(A->getType() == B->getType()); |
| 915 | return ConstantInt::get(A->getType(), A->getValue() + B->getValue()); |
| 916 | }; |
| 917 | // See if either input is A + C2, subject to the constraint from the |
| 918 | // condition that A != C when that input is used. We can assume that |
| 919 | // that input doesn't include C + C2. |
| 920 | ConstantInt *CIAdded; |
| 921 | switch (Pred) { |
Philip Reames | 70b3918 | 2016-02-27 05:18:30 +0000 | [diff] [blame] | 922 | default: break; |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 923 | case ICmpInst::ICMP_EQ: |
| 924 | if (match(SI->getFalseValue(), m_Add(m_Specific(A), |
| 925 | m_ConstantInt(CIAdded)))) { |
| 926 | auto ResNot = addConstants(CIBase, CIAdded); |
| 927 | FalseVal = intersect(FalseVal, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 928 | ValueLatticeElement::getNot(ResNot)); |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 929 | } |
| 930 | break; |
| 931 | case ICmpInst::ICMP_NE: |
| 932 | if (match(SI->getTrueValue(), m_Add(m_Specific(A), |
| 933 | m_ConstantInt(CIAdded)))) { |
| 934 | auto ResNot = addConstants(CIBase, CIAdded); |
| 935 | TrueVal = intersect(TrueVal, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 936 | ValueLatticeElement::getNot(ResNot)); |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 937 | } |
| 938 | break; |
| 939 | }; |
| 940 | } |
| 941 | } |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 942 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 943 | ValueLatticeElement Result; // Start Undefined. |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 944 | Result.mergeIn(TrueVal, DL); |
| 945 | Result.mergeIn(FalseVal, DL); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 946 | BBLV = Result; |
| 947 | return true; |
| 948 | } |
| 949 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 950 | bool LazyValueInfoImpl::solveBlockValueCast(ValueLatticeElement &BBLV, |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 951 | CastInst *CI, |
| 952 | BasicBlock *BB) { |
| 953 | if (!CI->getOperand(0)->getType()->isSized()) { |
Philip Reames | e5030e8 | 2016-04-26 22:52:30 +0000 | [diff] [blame] | 954 | // Without knowing how wide the input is, we can't analyze it in any useful |
| 955 | // way. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 956 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | e5030e8 | 2016-04-26 22:52:30 +0000 | [diff] [blame] | 957 | return true; |
| 958 | } |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 959 | |
| 960 | // Filter out casts we don't know how to reason about before attempting to |
| 961 | // recurse on our operand. This can cut a long search short if we know we're |
| 962 | // not going to be able to get any useful information anways. |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 963 | switch (CI->getOpcode()) { |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 964 | case Instruction::Trunc: |
| 965 | case Instruction::SExt: |
| 966 | case Instruction::ZExt: |
| 967 | case Instruction::BitCast: |
| 968 | break; |
| 969 | default: |
| 970 | // Unhandled instructions are overdefined. |
| 971 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 972 | << "' - overdefined (unknown cast).\n"); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 973 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 974 | return true; |
| 975 | } |
| 976 | |
Philip Reames | 38c87c2 | 2016-04-26 21:48:16 +0000 | [diff] [blame] | 977 | // Figure out the range of the LHS. If that fails, we still apply the |
| 978 | // transfer rule on the full set since we may be able to locally infer |
| 979 | // interesting facts. |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 980 | if (!hasBlockValue(CI->getOperand(0), BB)) |
| 981 | if (pushBlockValue(std::make_pair(BB, CI->getOperand(0)))) |
Philip Reames | 38c87c2 | 2016-04-26 21:48:16 +0000 | [diff] [blame] | 982 | // More work to do before applying this transfer rule. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 983 | return false; |
Philip Reames | 38c87c2 | 2016-04-26 21:48:16 +0000 | [diff] [blame] | 984 | |
| 985 | const unsigned OperandBitWidth = |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 986 | DL.getTypeSizeInBits(CI->getOperand(0)->getType()); |
Philip Reames | 38c87c2 | 2016-04-26 21:48:16 +0000 | [diff] [blame] | 987 | ConstantRange LHSRange = ConstantRange(OperandBitWidth); |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 988 | if (hasBlockValue(CI->getOperand(0), BB)) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 989 | ValueLatticeElement LHSVal = getBlockValue(CI->getOperand(0), BB); |
Craig Topper | 0e5f109 | 2017-06-03 07:47:08 +0000 | [diff] [blame] | 990 | intersectAssumeOrGuardBlockValueConstantRange(CI->getOperand(0), LHSVal, |
| 991 | CI); |
Philip Reames | 38c87c2 | 2016-04-26 21:48:16 +0000 | [diff] [blame] | 992 | if (LHSVal.isConstantRange()) |
| 993 | LHSRange = LHSVal.getConstantRange(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Craig Topper | a803d5b | 2017-06-03 07:47:14 +0000 | [diff] [blame] | 996 | const unsigned ResultBitWidth = CI->getType()->getIntegerBitWidth(); |
Philip Reames | 6671577 | 2016-04-25 18:30:31 +0000 | [diff] [blame] | 997 | |
| 998 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 999 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 1000 | // more definitions. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1001 | BBLV = ValueLatticeElement::getRange(LHSRange.castOp(CI->getOpcode(), |
| 1002 | ResultBitWidth)); |
Philip Reames | 6671577 | 2016-04-25 18:30:31 +0000 | [diff] [blame] | 1003 | return true; |
| 1004 | } |
| 1005 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1006 | bool LazyValueInfoImpl::solveBlockValueBinaryOp(ValueLatticeElement &BBLV, |
| 1007 | BinaryOperator *BO, |
| 1008 | BasicBlock *BB) { |
Philip Reames | 6671577 | 2016-04-25 18:30:31 +0000 | [diff] [blame] | 1009 | |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1010 | assert(BO->getOperand(0)->getType()->isSized() && |
Philip Reames | 053c2a6 | 2016-04-26 23:10:35 +0000 | [diff] [blame] | 1011 | "all operands to binary operators are sized"); |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 1012 | |
| 1013 | // Filter out operators we don't know how to reason about before attempting to |
| 1014 | // recurse on our operand(s). This can cut a long search short if we know |
Craig Topper | 84a9f16 | 2017-06-02 16:21:13 +0000 | [diff] [blame] | 1015 | // we're not going to be able to get any useful information anyways. |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1016 | switch (BO->getOpcode()) { |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 1017 | case Instruction::Add: |
| 1018 | case Instruction::Sub: |
| 1019 | case Instruction::Mul: |
| 1020 | case Instruction::UDiv: |
| 1021 | case Instruction::Shl: |
| 1022 | case Instruction::LShr: |
Max Kazantsev | 1acab00 | 2017-12-18 14:23:30 +0000 | [diff] [blame] | 1023 | case Instruction::AShr: |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 1024 | case Instruction::And: |
| 1025 | case Instruction::Or: |
| 1026 | // continue into the code below |
| 1027 | break; |
| 1028 | default: |
| 1029 | // Unhandled instructions are overdefined. |
| 1030 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 1031 | << "' - overdefined (unknown binary operator).\n"); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1032 | BBLV = ValueLatticeElement::getOverdefined(); |
Philip Reames | f105db4 | 2016-04-26 23:27:33 +0000 | [diff] [blame] | 1033 | return true; |
| 1034 | }; |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 1035 | |
Philip Reames | 053c2a6 | 2016-04-26 23:10:35 +0000 | [diff] [blame] | 1036 | // Figure out the range of the LHS. If that fails, use a conservative range, |
| 1037 | // but apply the transfer rule anyways. This lets us pick up facts from |
| 1038 | // expressions like "and i32 (call i32 @foo()), 32" |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1039 | if (!hasBlockValue(BO->getOperand(0), BB)) |
| 1040 | if (pushBlockValue(std::make_pair(BB, BO->getOperand(0)))) |
Philip Reames | 053c2a6 | 2016-04-26 23:10:35 +0000 | [diff] [blame] | 1041 | // More work to do before applying this transfer rule. |
| 1042 | return false; |
| 1043 | |
| 1044 | const unsigned OperandBitWidth = |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1045 | DL.getTypeSizeInBits(BO->getOperand(0)->getType()); |
Philip Reames | 053c2a6 | 2016-04-26 23:10:35 +0000 | [diff] [blame] | 1046 | ConstantRange LHSRange = ConstantRange(OperandBitWidth); |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1047 | if (hasBlockValue(BO->getOperand(0), BB)) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1048 | ValueLatticeElement LHSVal = getBlockValue(BO->getOperand(0), BB); |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1049 | intersectAssumeOrGuardBlockValueConstantRange(BO->getOperand(0), LHSVal, |
| 1050 | BO); |
Philip Reames | 053c2a6 | 2016-04-26 23:10:35 +0000 | [diff] [blame] | 1051 | if (LHSVal.isConstantRange()) |
| 1052 | LHSRange = LHSVal.getConstantRange(); |
Philip Reames | 6671577 | 2016-04-25 18:30:31 +0000 | [diff] [blame] | 1053 | } |
Philip Reames | 6671577 | 2016-04-25 18:30:31 +0000 | [diff] [blame] | 1054 | |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1055 | ConstantInt *RHS = cast<ConstantInt>(BO->getOperand(1)); |
Philip Reames | 6671577 | 2016-04-25 18:30:31 +0000 | [diff] [blame] | 1056 | ConstantRange RHSRange = ConstantRange(RHS->getValue()); |
| 1057 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1058 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 1059 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 1060 | // more definitions. |
Craig Topper | 3778c89 | 2017-06-02 16:33:13 +0000 | [diff] [blame] | 1061 | Instruction::BinaryOps BinOp = BO->getOpcode(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1062 | BBLV = ValueLatticeElement::getRange(LHSRange.binaryOp(BinOp, RHSRange)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1063 | return true; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1066 | static ValueLatticeElement getValueFromICmpCondition(Value *Val, ICmpInst *ICI, |
| 1067 | bool isTrueDest) { |
Artur Pilipenko | 2147291 | 2016-08-08 14:08:37 +0000 | [diff] [blame] | 1068 | Value *LHS = ICI->getOperand(0); |
| 1069 | Value *RHS = ICI->getOperand(1); |
| 1070 | CmpInst::Predicate Predicate = ICI->getPredicate(); |
| 1071 | |
| 1072 | if (isa<Constant>(RHS)) { |
| 1073 | if (ICI->isEquality() && LHS == Val) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1074 | // We know that V has the RHS constant if this is a true SETEQ or |
NAKAMURA Takumi | f252951 | 2016-07-04 01:26:27 +0000 | [diff] [blame] | 1075 | // false SETNE. |
Artur Pilipenko | 2147291 | 2016-08-08 14:08:37 +0000 | [diff] [blame] | 1076 | if (isTrueDest == (Predicate == ICmpInst::ICMP_EQ)) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1077 | return ValueLatticeElement::get(cast<Constant>(RHS)); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1078 | else |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1079 | return ValueLatticeElement::getNot(cast<Constant>(RHS)); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1080 | } |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1081 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1082 | |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1083 | if (!Val->getType()->isIntegerTy()) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1084 | return ValueLatticeElement::getOverdefined(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1085 | |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1086 | // Use ConstantRange::makeAllowedICmpRegion in order to determine the possible |
| 1087 | // range of Val guaranteed by the condition. Recognize comparisons in the from |
| 1088 | // of: |
| 1089 | // icmp <pred> Val, ... |
Artur Pilipenko | 6356258 | 2016-08-12 10:05:11 +0000 | [diff] [blame] | 1090 | // icmp <pred> (add Val, Offset), ... |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1091 | // The latter is the range checking idiom that InstCombine produces. Subtract |
| 1092 | // the offset from the allowed range for RHS in this case. |
Artur Pilipenko | eed618d | 2016-08-08 14:33:11 +0000 | [diff] [blame] | 1093 | |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1094 | // Val or (add Val, Offset) can be on either hand of the comparison |
| 1095 | if (LHS != Val && !match(LHS, m_Add(m_Specific(Val), m_ConstantInt()))) { |
| 1096 | std::swap(LHS, RHS); |
| 1097 | Predicate = CmpInst::getSwappedPredicate(Predicate); |
| 1098 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1099 | |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1100 | ConstantInt *Offset = nullptr; |
Artur Pilipenko | 6356258 | 2016-08-12 10:05:11 +0000 | [diff] [blame] | 1101 | if (LHS != Val) |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1102 | match(LHS, m_Add(m_Specific(Val), m_ConstantInt(Offset))); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1103 | |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1104 | if (LHS == Val || Offset) { |
| 1105 | // Calculate the range of values that are allowed by the comparison |
| 1106 | ConstantRange RHSRange(RHS->getType()->getIntegerBitWidth(), |
| 1107 | /*isFullSet=*/true); |
| 1108 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) |
| 1109 | RHSRange = ConstantRange(CI->getValue()); |
Artur Pilipenko | 6669f25 | 2016-08-12 10:14:11 +0000 | [diff] [blame] | 1110 | else if (Instruction *I = dyn_cast<Instruction>(RHS)) |
| 1111 | if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) |
| 1112 | RHSRange = getConstantRangeFromMetadata(*Ranges); |
Artur Pilipenko | c710a46 | 2016-08-09 14:50:08 +0000 | [diff] [blame] | 1113 | |
| 1114 | // If we're interested in the false dest, invert the condition |
| 1115 | CmpInst::Predicate Pred = |
| 1116 | isTrueDest ? Predicate : CmpInst::getInversePredicate(Predicate); |
| 1117 | ConstantRange TrueValues = |
| 1118 | ConstantRange::makeAllowedICmpRegion(Pred, RHSRange); |
| 1119 | |
| 1120 | if (Offset) // Apply the offset from above. |
| 1121 | TrueValues = TrueValues.subtract(Offset->getValue()); |
| 1122 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1123 | return ValueLatticeElement::getRange(std::move(TrueValues)); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1124 | } |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 1125 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1126 | return ValueLatticeElement::getOverdefined(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1129 | static ValueLatticeElement |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1130 | getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1131 | DenseMap<Value*, ValueLatticeElement> &Visited); |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1132 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1133 | static ValueLatticeElement |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1134 | getValueFromConditionImpl(Value *Val, Value *Cond, bool isTrueDest, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1135 | DenseMap<Value*, ValueLatticeElement> &Visited) { |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1136 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(Cond)) |
| 1137 | return getValueFromICmpCondition(Val, ICI, isTrueDest); |
| 1138 | |
| 1139 | // Handle conditions in the form of (cond1 && cond2), we know that on the |
Craig Topper | b60f866 | 2017-06-23 01:08:16 +0000 | [diff] [blame] | 1140 | // true dest path both of the conditions hold. Similarly for conditions of |
| 1141 | // the form (cond1 || cond2), we know that on the false dest path neither |
| 1142 | // condition holds. |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1143 | BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond); |
Craig Topper | b60f866 | 2017-06-23 01:08:16 +0000 | [diff] [blame] | 1144 | if (!BO || (isTrueDest && BO->getOpcode() != BinaryOperator::And) || |
| 1145 | (!isTrueDest && BO->getOpcode() != BinaryOperator::Or)) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1146 | return ValueLatticeElement::getOverdefined(); |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1147 | |
Brian M. Rzycki | 252165b | 2018-03-13 18:14:10 +0000 | [diff] [blame] | 1148 | // Prevent infinite recursion if Cond references itself as in this example: |
| 1149 | // Cond: "%tmp4 = and i1 %tmp4, undef" |
| 1150 | // BL: "%tmp4 = and i1 %tmp4, undef" |
| 1151 | // BR: "i1 undef" |
| 1152 | Value *BL = BO->getOperand(0); |
| 1153 | Value *BR = BO->getOperand(1); |
| 1154 | if (BL == Cond || BR == Cond) |
| 1155 | return ValueLatticeElement::getOverdefined(); |
| 1156 | |
| 1157 | return intersect(getValueFromCondition(Val, BL, isTrueDest, Visited), |
| 1158 | getValueFromCondition(Val, BR, isTrueDest, Visited)); |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1161 | static ValueLatticeElement |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1162 | getValueFromCondition(Value *Val, Value *Cond, bool isTrueDest, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1163 | DenseMap<Value*, ValueLatticeElement> &Visited) { |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1164 | auto I = Visited.find(Cond); |
| 1165 | if (I != Visited.end()) |
| 1166 | return I->second; |
Artur Pilipenko | b623088 | 2016-08-12 15:08:15 +0000 | [diff] [blame] | 1167 | |
| 1168 | auto Result = getValueFromConditionImpl(Val, Cond, isTrueDest, Visited); |
| 1169 | Visited[Cond] = Result; |
| 1170 | return Result; |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1173 | ValueLatticeElement getValueFromCondition(Value *Val, Value *Cond, |
| 1174 | bool isTrueDest) { |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1175 | assert(Cond && "precondition"); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1176 | DenseMap<Value*, ValueLatticeElement> Visited; |
Artur Pilipenko | fd223d5 | 2016-08-10 15:13:15 +0000 | [diff] [blame] | 1177 | return getValueFromCondition(Val, Cond, isTrueDest, Visited); |
| 1178 | } |
| 1179 | |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1180 | // Return true if Usr has Op as an operand, otherwise false. |
| 1181 | static bool usesOperand(User *Usr, Value *Op) { |
| 1182 | return find(Usr->operands(), Op) != Usr->op_end(); |
| 1183 | } |
| 1184 | |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1185 | // Return true if the instruction type of Val is supported by |
| 1186 | // constantFoldUser(). Currently CastInst and BinaryOperator only. Call this |
| 1187 | // before calling constantFoldUser() to find out if it's even worth attempting |
| 1188 | // to call it. |
| 1189 | static bool isOperationFoldable(User *Usr) { |
| 1190 | return isa<CastInst>(Usr) || isa<BinaryOperator>(Usr); |
| 1191 | } |
| 1192 | |
| 1193 | // Check if Usr can be simplified to an integer constant when the value of one |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1194 | // of its operands Op is an integer constant OpConstVal. If so, return it as an |
| 1195 | // lattice value range with a single element or otherwise return an overdefined |
| 1196 | // lattice value. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1197 | static ValueLatticeElement constantFoldUser(User *Usr, Value *Op, |
| 1198 | const APInt &OpConstVal, |
| 1199 | const DataLayout &DL) { |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1200 | assert(isOperationFoldable(Usr) && "Precondition"); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1201 | Constant* OpConst = Constant::getIntegerValue(Op->getType(), OpConstVal); |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1202 | // Check if Usr can be simplified to a constant. |
| 1203 | if (auto *CI = dyn_cast<CastInst>(Usr)) { |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1204 | assert(CI->getOperand(0) == Op && "Operand 0 isn't Op"); |
| 1205 | if (auto *C = dyn_cast_or_null<ConstantInt>( |
| 1206 | SimplifyCastInst(CI->getOpcode(), OpConst, |
| 1207 | CI->getDestTy(), DL))) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1208 | return ValueLatticeElement::getRange(ConstantRange(C->getValue())); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1209 | } |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1210 | } else if (auto *BO = dyn_cast<BinaryOperator>(Usr)) { |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1211 | bool Op0Match = BO->getOperand(0) == Op; |
| 1212 | bool Op1Match = BO->getOperand(1) == Op; |
| 1213 | assert((Op0Match || Op1Match) && |
| 1214 | "Operand 0 nor Operand 1 isn't a match"); |
| 1215 | Value *LHS = Op0Match ? OpConst : BO->getOperand(0); |
| 1216 | Value *RHS = Op1Match ? OpConst : BO->getOperand(1); |
| 1217 | if (auto *C = dyn_cast_or_null<ConstantInt>( |
| 1218 | SimplifyBinOp(BO->getOpcode(), LHS, RHS, DL))) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1219 | return ValueLatticeElement::getRange(ConstantRange(C->getValue())); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1222 | return ValueLatticeElement::getOverdefined(); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1225 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if |
Philip Reames | 13f7324 | 2016-02-01 23:21:11 +0000 | [diff] [blame] | 1226 | /// Val is not constrained on the edge. Result is unspecified if return value |
| 1227 | /// is false. |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1228 | static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1229 | BasicBlock *BBTo, ValueLatticeElement &Result) { |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1230 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1231 | // know that v != 0. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1232 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 1233 | // If this is a conditional branch and only one successor goes to BBTo, then |
Sanjay Patel | 938e279 | 2015-01-09 16:35:37 +0000 | [diff] [blame] | 1234 | // we may be able to infer something from the condition. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1235 | if (BI->isConditional() && |
| 1236 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 1237 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 1238 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 1239 | "BBTo isn't a successor of BBFrom"); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1240 | Value *Condition = BI->getCondition(); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1242 | // If V is the condition of the branch itself, then we know exactly what |
| 1243 | // it is. |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1244 | if (Condition == Val) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1245 | Result = ValueLatticeElement::get(ConstantInt::get( |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1246 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1247 | return true; |
| 1248 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1249 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1250 | // If the condition of the branch is an equality comparison, we may be |
| 1251 | // able to infer the value. |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1252 | Result = getValueFromCondition(Val, Condition, isTrueDest); |
| 1253 | if (!Result.isOverdefined()) |
| 1254 | return true; |
| 1255 | |
| 1256 | if (User *Usr = dyn_cast<User>(Val)) { |
| 1257 | assert(Result.isOverdefined() && "Result isn't overdefined"); |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1258 | // Check with isOperationFoldable() first to avoid linearly iterating |
| 1259 | // over the operands unnecessarily which can be expensive for |
| 1260 | // instructions with many operands. |
| 1261 | if (isa<IntegerType>(Usr->getType()) && isOperationFoldable(Usr)) { |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1262 | const DataLayout &DL = BBTo->getModule()->getDataLayout(); |
| 1263 | if (usesOperand(Usr, Condition)) { |
| 1264 | // If Val has Condition as an operand and Val can be folded into a |
| 1265 | // constant with either Condition == true or Condition == false, |
| 1266 | // propagate the constant. |
| 1267 | // eg. |
| 1268 | // ; %Val is true on the edge to %then. |
| 1269 | // %Val = and i1 %Condition, true. |
| 1270 | // br %Condition, label %then, label %else |
| 1271 | APInt ConditionVal(1, isTrueDest ? 1 : 0); |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1272 | Result = constantFoldUser(Usr, Condition, ConditionVal, DL); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1273 | } else { |
| 1274 | // If one of Val's operand has an inferred value, we may be able to |
| 1275 | // infer the value of Val. |
| 1276 | // eg. |
| 1277 | // ; %Val is 94 on the edge to %then. |
| 1278 | // %Val = add i8 %Op, 1 |
| 1279 | // %Condition = icmp eq i8 %Op, 93 |
| 1280 | // br i1 %Condition, label %then, label %else |
| 1281 | for (unsigned i = 0; i < Usr->getNumOperands(); ++i) { |
| 1282 | Value *Op = Usr->getOperand(i); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1283 | ValueLatticeElement OpLatticeVal = |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1284 | getValueFromCondition(Op, Condition, isTrueDest); |
| 1285 | if (Optional<APInt> OpConst = OpLatticeVal.asConstantInteger()) { |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1286 | Result = constantFoldUser(Usr, Op, OpConst.getValue(), DL); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | } |
Artur Pilipenko | 933c07a | 2016-08-10 13:38:07 +0000 | [diff] [blame] | 1293 | if (!Result.isOverdefined()) |
Artur Pilipenko | 2e19f59 | 2016-08-02 16:20:48 +0000 | [diff] [blame] | 1294 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1297 | |
| 1298 | // If the edge was formed by a switch on the value, then we may know exactly |
| 1299 | // what it is. |
| 1300 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1301 | Value *Condition = SI->getCondition(); |
| 1302 | if (!isa<IntegerType>(Val->getType())) |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 1303 | return false; |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1304 | bool ValUsesConditionAndMayBeFoldable = false; |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1305 | if (Condition != Val) { |
| 1306 | // Check if Val has Condition as an operand. |
| 1307 | if (User *Usr = dyn_cast<User>(Val)) |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1308 | ValUsesConditionAndMayBeFoldable = isOperationFoldable(Usr) && |
| 1309 | usesOperand(Usr, Condition); |
| 1310 | if (!ValUsesConditionAndMayBeFoldable) |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1313 | assert((Condition == Val || ValUsesConditionAndMayBeFoldable) && |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1314 | "Condition != Val nor Val doesn't use Condition"); |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 1315 | |
| 1316 | bool DefaultCase = SI->getDefaultDest() == BBTo; |
| 1317 | unsigned BitWidth = Val->getType()->getIntegerBitWidth(); |
| 1318 | ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/); |
| 1319 | |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 1320 | for (auto Case : SI->cases()) { |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1321 | APInt CaseValue = Case.getCaseValue()->getValue(); |
| 1322 | ConstantRange EdgeVal(CaseValue); |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1323 | if (ValUsesConditionAndMayBeFoldable) { |
| 1324 | User *Usr = cast<User>(Val); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1325 | const DataLayout &DL = BBTo->getModule()->getDataLayout(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1326 | ValueLatticeElement EdgeLatticeVal = |
Hiroshi Yamauchi | ccd412f | 2017-08-10 02:23:14 +0000 | [diff] [blame] | 1327 | constantFoldUser(Usr, Condition, CaseValue, DL); |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1328 | if (EdgeLatticeVal.isOverdefined()) |
| 1329 | return false; |
| 1330 | EdgeVal = EdgeLatticeVal.getConstantRange(); |
| 1331 | } |
Manman Ren | f3fedb6 | 2012-09-05 23:45:58 +0000 | [diff] [blame] | 1332 | if (DefaultCase) { |
| 1333 | // It is possible that the default destination is the destination of |
Hiroshi Yamauchi | 144ee2b | 2017-08-03 21:11:30 +0000 | [diff] [blame] | 1334 | // some cases. We cannot perform difference for those cases. |
| 1335 | // We know Condition != CaseValue in BBTo. In some cases we can use |
| 1336 | // this to infer Val == f(Condition) is != f(CaseValue). For now, we |
| 1337 | // only do this when f is identity (i.e. Val == Condition), but we |
| 1338 | // should be able to do this for any injective f. |
| 1339 | if (Case.getCaseSuccessor() != BBTo && Condition == Val) |
Manman Ren | f3fedb6 | 2012-09-05 23:45:58 +0000 | [diff] [blame] | 1340 | EdgesVals = EdgesVals.difference(EdgeVal); |
Chandler Carruth | 927d8e6 | 2017-04-12 07:27:28 +0000 | [diff] [blame] | 1341 | } else if (Case.getCaseSuccessor() == BBTo) |
Nuno Lopes | ac59380 | 2012-05-18 21:02:10 +0000 | [diff] [blame] | 1342 | EdgesVals = EdgesVals.unionWith(EdgeVal); |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1343 | } |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1344 | Result = ValueLatticeElement::getRange(std::move(EdgesVals)); |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 1345 | return true; |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1346 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1347 | return false; |
| 1348 | } |
| 1349 | |
Sanjay Patel | 938e279 | 2015-01-09 16:35:37 +0000 | [diff] [blame] | 1350 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at |
| 1351 | /// the basic block if the edge does not constrain Val. |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 1352 | bool LazyValueInfoImpl::getEdgeValue(Value *Val, BasicBlock *BBFrom, |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1353 | BasicBlock *BBTo, |
| 1354 | ValueLatticeElement &Result, |
Xin Tong | 68ea9aa | 2017-02-24 20:59:26 +0000 | [diff] [blame] | 1355 | Instruction *CxtI) { |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1356 | // If already a constant, there is nothing to compute. |
| 1357 | if (Constant *VC = dyn_cast<Constant>(Val)) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1358 | Result = ValueLatticeElement::get(VC); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1359 | return true; |
| 1360 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1361 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1362 | ValueLatticeElement LocalResult; |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1363 | if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult)) |
| 1364 | // If we couldn't constrain the value on the edge, LocalResult doesn't |
| 1365 | // provide any information. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1366 | LocalResult = ValueLatticeElement::getOverdefined(); |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 1367 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1368 | if (hasSingleValue(LocalResult)) { |
| 1369 | // Can't get any more precise here |
| 1370 | Result = LocalResult; |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1371 | return true; |
| 1372 | } |
| 1373 | |
| 1374 | if (!hasBlockValue(Val, BBFrom)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1375 | if (pushBlockValue(std::make_pair(BBFrom, Val))) |
| 1376 | return false; |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1377 | // No new information. |
| 1378 | Result = LocalResult; |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1379 | return true; |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1382 | // Try to intersect ranges of the BB and the constraint on the edge. |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1383 | ValueLatticeElement InBlock = getBlockValue(Val, BBFrom); |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 1384 | intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, |
| 1385 | BBFrom->getTerminator()); |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 1386 | // We can use the context instruction (generically the ultimate instruction |
| 1387 | // the calling pass is trying to simplify) here, even though the result of |
| 1388 | // this function is generally cached when called from the solve* functions |
| 1389 | // (and that cached result might be used with queries using a different |
| 1390 | // context instruction), because when this function is called from the solve* |
| 1391 | // functions, the context instruction is not provided. When called from |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 1392 | // LazyValueInfoImpl::getValueOnEdge, the context instruction is provided, |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 1393 | // but then the result is not cached. |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 1394 | intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock, CxtI); |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1395 | |
| 1396 | Result = intersect(LocalResult, InBlock); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1397 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1400 | ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB, |
| 1401 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1402 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1403 | << BB->getName() << "'\n"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1404 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1405 | assert(BlockValueStack.empty() && BlockValueSet.empty()); |
Philip Reames | bb781b4 | 2016-02-10 21:46:32 +0000 | [diff] [blame] | 1406 | if (!hasBlockValue(V, BB)) { |
NAKAMURA Takumi | bd072a9 | 2016-07-25 00:59:46 +0000 | [diff] [blame] | 1407 | pushBlockValue(std::make_pair(BB, V)); |
Philip Reames | bb781b4 | 2016-02-10 21:46:32 +0000 | [diff] [blame] | 1408 | solve(); |
| 1409 | } |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1410 | ValueLatticeElement Result = getBlockValue(V, BB); |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 1411 | intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1412 | |
| 1413 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
| 1414 | return Result; |
| 1415 | } |
| 1416 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1417 | ValueLatticeElement LazyValueInfoImpl::getValueAt(Value *V, Instruction *CxtI) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1418 | DEBUG(dbgs() << "LVI Getting value " << *V << " at '" |
| 1419 | << CxtI->getName() << "'\n"); |
| 1420 | |
Philip Reames | bb781b4 | 2016-02-10 21:46:32 +0000 | [diff] [blame] | 1421 | if (auto *C = dyn_cast<Constant>(V)) |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1422 | return ValueLatticeElement::get(C); |
Philip Reames | bb781b4 | 2016-02-10 21:46:32 +0000 | [diff] [blame] | 1423 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1424 | ValueLatticeElement Result = ValueLatticeElement::getOverdefined(); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 1425 | if (auto *I = dyn_cast<Instruction>(V)) |
| 1426 | Result = getFromRangeMetadata(I); |
Artur Pilipenko | 2e8f82d | 2016-08-12 15:52:23 +0000 | [diff] [blame] | 1427 | intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI); |
Philip Reames | 2c275cc | 2016-02-02 00:45:30 +0000 | [diff] [blame] | 1428 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1429 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1430 | return Result; |
| 1431 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1432 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1433 | ValueLatticeElement LazyValueInfoImpl:: |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1434 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, |
| 1435 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1436 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1437 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1438 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1439 | ValueLatticeElement Result; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1440 | if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1441 | solve(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1442 | bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1443 | (void)WasFastQuery; |
| 1444 | assert(WasFastQuery && "More work to do after problem solved?"); |
| 1445 | } |
| 1446 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1447 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1448 | return Result; |
| 1449 | } |
| 1450 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 1451 | void LazyValueInfoImpl::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Philip Reames | 9db7948 | 2016-09-12 22:38:44 +0000 | [diff] [blame] | 1452 | BasicBlock *NewSucc) { |
| 1453 | TheCache.threadEdgeImpl(OldSucc, NewSucc); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1456 | //===----------------------------------------------------------------------===// |
| 1457 | // LazyValueInfo Impl |
| 1458 | //===----------------------------------------------------------------------===// |
| 1459 | |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 1460 | /// This lazily constructs the LazyValueInfoImpl. |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1461 | static LazyValueInfoImpl &getImpl(void *&PImpl, AssumptionCache *AC, |
| 1462 | const DataLayout *DL, |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 1463 | DominatorTree *DT = nullptr) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1464 | if (!PImpl) { |
| 1465 | assert(DL && "getCache() called with a null DataLayout"); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1466 | PImpl = new LazyValueInfoImpl(AC, *DL, DT); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1467 | } |
Philip Reames | 92e5e1b | 2016-09-12 21:46:58 +0000 | [diff] [blame] | 1468 | return *static_cast<LazyValueInfoImpl*>(PImpl); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1471 | bool LazyValueInfoWrapperPass::runOnFunction(Function &F) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1472 | Info.AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1473 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1474 | |
| 1475 | DominatorTreeWrapperPass *DTWP = |
| 1476 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1477 | Info.DT = DTWP ? &DTWP->getDomTree() : nullptr; |
| 1478 | Info.TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1479 | |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1480 | if (Info.PImpl) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1481 | getImpl(Info.PImpl, Info.AC, &DL, Info.DT).clear(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1482 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1483 | // Fully lazy. |
| 1484 | return false; |
| 1485 | } |
| 1486 | |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1487 | void LazyValueInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1488 | AU.setPreservesAll(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1489 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1490 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1493 | LazyValueInfo &LazyValueInfoWrapperPass::getLVI() { return Info; } |
| 1494 | |
| 1495 | LazyValueInfo::~LazyValueInfo() { releaseMemory(); } |
| 1496 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1497 | void LazyValueInfo::releaseMemory() { |
| 1498 | // If the cache was allocated, free it. |
| 1499 | if (PImpl) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1500 | delete &getImpl(PImpl, AC, nullptr); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1501 | PImpl = nullptr; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | |
Chandler Carruth | a504f2b | 2017-01-23 06:35:12 +0000 | [diff] [blame] | 1505 | bool LazyValueInfo::invalidate(Function &F, const PreservedAnalyses &PA, |
| 1506 | FunctionAnalysisManager::Invalidator &Inv) { |
| 1507 | // We need to invalidate if we have either failed to preserve this analyses |
| 1508 | // result directly or if any of its dependencies have been invalidated. |
| 1509 | auto PAC = PA.getChecker<LazyValueAnalysis>(); |
| 1510 | if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || |
| 1511 | (DT && Inv.invalidate<DominatorTreeAnalysis>(F, PA))) |
| 1512 | return true; |
| 1513 | |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1517 | void LazyValueInfoWrapperPass::releaseMemory() { Info.releaseMemory(); } |
| 1518 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1519 | LazyValueInfo LazyValueAnalysis::run(Function &F, |
| 1520 | FunctionAnalysisManager &FAM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1521 | auto &AC = FAM.getResult<AssumptionAnalysis>(F); |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1522 | auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F); |
| 1523 | auto *DT = FAM.getCachedResult<DominatorTreeAnalysis>(F); |
| 1524 | |
Anna Thomas | a10e3e4 | 2017-03-12 14:06:41 +0000 | [diff] [blame] | 1525 | return LazyValueInfo(&AC, &F.getParent()->getDataLayout(), &TLI, DT); |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Wei Mi | f160e34 | 2016-09-15 06:28:34 +0000 | [diff] [blame] | 1528 | /// Returns true if we can statically tell that this value will never be a |
| 1529 | /// "useful" constant. In practice, this means we've got something like an |
| 1530 | /// alloca or a malloc call for which a comparison against a constant can |
| 1531 | /// only be guarding dead code. Note that we are potentially giving up some |
| 1532 | /// precision in dead code (a constant result) in favour of avoiding a |
| 1533 | /// expensive search for a easily answered common query. |
| 1534 | static bool isKnownNonConstant(Value *V) { |
| 1535 | V = V->stripPointerCasts(); |
| 1536 | // The return val of alloc cannot be a Constant. |
| 1537 | if (isa<AllocaInst>(V)) |
| 1538 | return true; |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1542 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB, |
| 1543 | Instruction *CxtI) { |
Wei Mi | f160e34 | 2016-09-15 06:28:34 +0000 | [diff] [blame] | 1544 | // Bail out early if V is known not to be a Constant. |
| 1545 | if (isKnownNonConstant(V)) |
| 1546 | return nullptr; |
| 1547 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1548 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1549 | ValueLatticeElement Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1550 | getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1551 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1552 | if (Result.isConstant()) |
| 1553 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1554 | if (Result.isConstantRange()) { |
Craig Topper | 2b195fd | 2017-05-06 03:35:15 +0000 | [diff] [blame] | 1555 | const ConstantRange &CR = Result.getConstantRange(); |
Owen Anderson | 38f6b7f | 2010-08-27 23:29:38 +0000 | [diff] [blame] | 1556 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1557 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1558 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1559 | return nullptr; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1560 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1561 | |
John Regehr | e1c481d | 2016-05-02 19:58:00 +0000 | [diff] [blame] | 1562 | ConstantRange LazyValueInfo::getConstantRange(Value *V, BasicBlock *BB, |
NAKAMURA Takumi | 940cd93 | 2016-07-04 01:26:21 +0000 | [diff] [blame] | 1563 | Instruction *CxtI) { |
John Regehr | e1c481d | 2016-05-02 19:58:00 +0000 | [diff] [blame] | 1564 | assert(V->getType()->isIntegerTy()); |
| 1565 | unsigned Width = V->getType()->getIntegerBitWidth(); |
| 1566 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1567 | ValueLatticeElement Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1568 | getImpl(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI); |
John Regehr | e1c481d | 2016-05-02 19:58:00 +0000 | [diff] [blame] | 1569 | if (Result.isUndefined()) |
| 1570 | return ConstantRange(Width, /*isFullSet=*/false); |
| 1571 | if (Result.isConstantRange()) |
| 1572 | return Result.getConstantRange(); |
Artur Pilipenko | a4b6a70 | 2016-08-10 12:54:54 +0000 | [diff] [blame] | 1573 | // We represent ConstantInt constants as constant ranges but other kinds |
| 1574 | // of integer constants, i.e. ConstantExpr will be tagged as constants |
| 1575 | assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) && |
| 1576 | "ConstantInt value must be represented as constantrange"); |
Davide Italiano | bd543d0 | 2016-05-25 22:29:34 +0000 | [diff] [blame] | 1577 | return ConstantRange(Width, /*isFullSet=*/true); |
John Regehr | e1c481d | 2016-05-02 19:58:00 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1580 | /// Determine whether the specified value is known to be a |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1581 | /// constant on the specified edge. Return null if not. |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1582 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1583 | BasicBlock *ToBB, |
| 1584 | Instruction *CxtI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1585 | const DataLayout &DL = FromBB->getModule()->getDataLayout(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1586 | ValueLatticeElement Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1587 | getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1589 | if (Result.isConstant()) |
| 1590 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1591 | if (Result.isConstantRange()) { |
Craig Topper | 2b195fd | 2017-05-06 03:35:15 +0000 | [diff] [blame] | 1592 | const ConstantRange &CR = Result.getConstantRange(); |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1593 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1594 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1595 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1596 | return nullptr; |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Craig Topper | 2c20c42 | 2017-06-23 05:41:35 +0000 | [diff] [blame] | 1599 | ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V, |
| 1600 | BasicBlock *FromBB, |
| 1601 | BasicBlock *ToBB, |
| 1602 | Instruction *CxtI) { |
| 1603 | unsigned Width = V->getType()->getIntegerBitWidth(); |
| 1604 | const DataLayout &DL = FromBB->getModule()->getDataLayout(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1605 | ValueLatticeElement Result = |
Craig Topper | 2c20c42 | 2017-06-23 05:41:35 +0000 | [diff] [blame] | 1606 | getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
| 1607 | |
| 1608 | if (Result.isUndefined()) |
| 1609 | return ConstantRange(Width, /*isFullSet=*/false); |
| 1610 | if (Result.isConstantRange()) |
| 1611 | return Result.getConstantRange(); |
| 1612 | // We represent ConstantInt constants as constant ranges but other kinds |
| 1613 | // of integer constants, i.e. ConstantExpr will be tagged as constants |
| 1614 | assert(!(Result.isConstant() && isa<ConstantInt>(Result.getConstant())) && |
| 1615 | "ConstantInt value must be represented as constantrange"); |
| 1616 | return ConstantRange(Width, /*isFullSet=*/true); |
| 1617 | } |
| 1618 | |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1619 | static LazyValueInfo::Tristate |
| 1620 | getPredicateResult(unsigned Pred, Constant *C, const ValueLatticeElement &Val, |
| 1621 | const DataLayout &DL, TargetLibraryInfo *TLI) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1622 | // If we know the value is a constant, evaluate the conditional. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1623 | Constant *Res = nullptr; |
Craig Topper | 6dd9dcf | 2017-06-09 21:18:16 +0000 | [diff] [blame] | 1624 | if (Val.isConstant()) { |
| 1625 | Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL, TLI); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1626 | if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1627 | return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True; |
| 1628 | return LazyValueInfo::Unknown; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1629 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1630 | |
Craig Topper | 6dd9dcf | 2017-06-09 21:18:16 +0000 | [diff] [blame] | 1631 | if (Val.isConstantRange()) { |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1632 | ConstantInt *CI = dyn_cast<ConstantInt>(C); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1633 | if (!CI) return LazyValueInfo::Unknown; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1634 | |
Craig Topper | 6dd9dcf | 2017-06-09 21:18:16 +0000 | [diff] [blame] | 1635 | const ConstantRange &CR = Val.getConstantRange(); |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1636 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1637 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1638 | return LazyValueInfo::False; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1639 | |
Craig Topper | 7945248 | 2017-06-07 00:58:09 +0000 | [diff] [blame] | 1640 | if (CR.isSingleElement()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1641 | return LazyValueInfo::True; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1642 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1643 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1644 | return LazyValueInfo::True; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1645 | |
Craig Topper | 7945248 | 2017-06-07 00:58:09 +0000 | [diff] [blame] | 1646 | if (CR.isSingleElement()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1647 | return LazyValueInfo::False; |
Craig Topper | 31ce4ec | 2017-06-09 16:16:20 +0000 | [diff] [blame] | 1648 | } else { |
| 1649 | // Handle more complex predicates. |
| 1650 | ConstantRange TrueValues = ConstantRange::makeExactICmpRegion( |
| 1651 | (ICmpInst::Predicate)Pred, CI->getValue()); |
| 1652 | if (TrueValues.contains(CR)) |
| 1653 | return LazyValueInfo::True; |
| 1654 | if (TrueValues.inverse().contains(CR)) |
| 1655 | return LazyValueInfo::False; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1656 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1657 | return LazyValueInfo::Unknown; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1658 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1659 | |
Craig Topper | 6dd9dcf | 2017-06-09 21:18:16 +0000 | [diff] [blame] | 1660 | if (Val.isNotConstant()) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1661 | // If this is an equality comparison, we can try to fold it knowing that |
| 1662 | // "V != C1". |
| 1663 | if (Pred == ICmpInst::ICMP_EQ) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1664 | // !C1 == C -> false iff C1 == C. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1665 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Craig Topper | 6dd9dcf | 2017-06-09 21:18:16 +0000 | [diff] [blame] | 1666 | Val.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1667 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1668 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1669 | return LazyValueInfo::False; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1670 | } else if (Pred == ICmpInst::ICMP_NE) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1671 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | b0c0a0d | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 1672 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Craig Topper | 6dd9dcf | 2017-06-09 21:18:16 +0000 | [diff] [blame] | 1673 | Val.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1674 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1675 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1676 | return LazyValueInfo::True; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1677 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1678 | return LazyValueInfo::Unknown; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1679 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1680 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1681 | return LazyValueInfo::Unknown; |
| 1682 | } |
| 1683 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1684 | /// Determine whether the specified value comparison with a constant is known to |
| 1685 | /// be true or false on the specified CFG edge. Pred is a CmpInst predicate. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1686 | LazyValueInfo::Tristate |
| 1687 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 1688 | BasicBlock *FromBB, BasicBlock *ToBB, |
| 1689 | Instruction *CxtI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1690 | const DataLayout &DL = FromBB->getModule()->getDataLayout(); |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1691 | ValueLatticeElement Result = |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1692 | getImpl(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1693 | |
| 1694 | return getPredicateResult(Pred, C, Result, DL, TLI); |
| 1695 | } |
| 1696 | |
| 1697 | LazyValueInfo::Tristate |
| 1698 | LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C, |
| 1699 | Instruction *CxtI) { |
Wei Mi | f160e34 | 2016-09-15 06:28:34 +0000 | [diff] [blame] | 1700 | // Is or is not NonNull are common predicates being queried. If |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1701 | // isKnownNonZero can tell us the result of the predicate, we can |
Wei Mi | f160e34 | 2016-09-15 06:28:34 +0000 | [diff] [blame] | 1702 | // return it quickly. But this is only a fastpath, and falling |
| 1703 | // through would still be correct. |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1704 | const DataLayout &DL = CxtI->getModule()->getDataLayout(); |
Wei Mi | f160e34 | 2016-09-15 06:28:34 +0000 | [diff] [blame] | 1705 | if (V->getType()->isPointerTy() && C->isNullValue() && |
Nuno Lopes | 404f106 | 2017-09-09 18:23:11 +0000 | [diff] [blame] | 1706 | isKnownNonZero(V->stripPointerCasts(), DL)) { |
Wei Mi | f160e34 | 2016-09-15 06:28:34 +0000 | [diff] [blame] | 1707 | if (Pred == ICmpInst::ICMP_EQ) |
| 1708 | return LazyValueInfo::False; |
| 1709 | else if (Pred == ICmpInst::ICMP_NE) |
| 1710 | return LazyValueInfo::True; |
| 1711 | } |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1712 | ValueLatticeElement Result = getImpl(PImpl, AC, &DL, DT).getValueAt(V, CxtI); |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1713 | Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI); |
| 1714 | if (Ret != Unknown) |
| 1715 | return Ret; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1716 | |
Philip Reames | aeefae0 | 2015-11-04 01:47:04 +0000 | [diff] [blame] | 1717 | // Note: The following bit of code is somewhat distinct from the rest of LVI; |
| 1718 | // LVI as a whole tries to compute a lattice value which is conservatively |
| 1719 | // correct at a given location. In this case, we have a predicate which we |
| 1720 | // weren't able to prove about the merged result, and we're pushing that |
| 1721 | // predicate back along each incoming edge to see if we can prove it |
| 1722 | // separately for each input. As a motivating example, consider: |
| 1723 | // bb1: |
| 1724 | // %v1 = ... ; constantrange<1, 5> |
| 1725 | // br label %merge |
| 1726 | // bb2: |
| 1727 | // %v2 = ... ; constantrange<10, 20> |
| 1728 | // br label %merge |
| 1729 | // merge: |
| 1730 | // %phi = phi [%v1, %v2] ; constantrange<1,20> |
| 1731 | // %pred = icmp eq i32 %phi, 8 |
| 1732 | // We can't tell from the lattice value for '%phi' that '%pred' is false |
| 1733 | // along each path, but by checking the predicate over each input separately, |
| 1734 | // we can. |
| 1735 | // We limit the search to one step backwards from the current BB and value. |
| 1736 | // We could consider extending this to search further backwards through the |
| 1737 | // CFG and/or value graph, but there are non-obvious compile time vs quality |
NAKAMURA Takumi | f252951 | 2016-07-04 01:26:27 +0000 | [diff] [blame] | 1738 | // tradeoffs. |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1739 | if (CxtI) { |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1740 | BasicBlock *BB = CxtI->getParent(); |
| 1741 | |
| 1742 | // Function entry or an unreachable block. Bail to avoid confusing |
| 1743 | // analysis below. |
| 1744 | pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 1745 | if (PI == PE) |
| 1746 | return Unknown; |
| 1747 | |
| 1748 | // If V is a PHI node in the same block as the context, we need to ask |
| 1749 | // questions about the predicate as applied to the incoming value along |
| 1750 | // each edge. This is useful for eliminating cases where the predicate is |
| 1751 | // known along all incoming edges. |
| 1752 | if (auto *PHI = dyn_cast<PHINode>(V)) |
| 1753 | if (PHI->getParent() == BB) { |
| 1754 | Tristate Baseline = Unknown; |
| 1755 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) { |
| 1756 | Value *Incoming = PHI->getIncomingValue(i); |
| 1757 | BasicBlock *PredBB = PHI->getIncomingBlock(i); |
NAKAMURA Takumi | f252951 | 2016-07-04 01:26:27 +0000 | [diff] [blame] | 1758 | // Note that PredBB may be BB itself. |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1759 | Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB, |
| 1760 | CxtI); |
NAKAMURA Takumi | 4cb46e6 | 2016-07-04 01:26:33 +0000 | [diff] [blame] | 1761 | |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1762 | // Keep going as long as we've seen a consistent known result for |
| 1763 | // all inputs. |
| 1764 | Baseline = (i == 0) ? Result /* First iteration */ |
| 1765 | : (Baseline == Result ? Baseline : Unknown); /* All others */ |
| 1766 | if (Baseline == Unknown) |
| 1767 | break; |
| 1768 | } |
| 1769 | if (Baseline != Unknown) |
| 1770 | return Baseline; |
NAKAMURA Takumi | bd072a9 | 2016-07-25 00:59:46 +0000 | [diff] [blame] | 1771 | } |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1772 | |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1773 | // For a comparison where the V is outside this block, it's possible |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1774 | // that we've branched on it before. Look to see if the value is known |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1775 | // on all incoming edges. |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1776 | if (!isa<Instruction>(V) || |
| 1777 | cast<Instruction>(V)->getParent() != BB) { |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1778 | // For predecessor edge, determine if the comparison is true or false |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1779 | // on that edge. If they're all true or all false, we can conclude |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1780 | // the value of the comparison in this block. |
| 1781 | Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); |
| 1782 | if (Baseline != Unknown) { |
| 1783 | // Check that all remaining incoming values match the first one. |
| 1784 | while (++PI != PE) { |
| 1785 | Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); |
| 1786 | if (Ret != Baseline) break; |
| 1787 | } |
| 1788 | // If we terminated early, then one of the values didn't match. |
| 1789 | if (PI == PE) { |
| 1790 | return Baseline; |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | } |
| 1795 | return Unknown; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1798 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1799 | BasicBlock *NewSucc) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1800 | if (PImpl) { |
| 1801 | const DataLayout &DL = PredBB->getModule()->getDataLayout(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1802 | getImpl(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1803 | } |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1807 | if (PImpl) { |
| 1808 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1809 | getImpl(PImpl, AC, &DL, DT).eraseBlock(BB); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1810 | } |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1811 | } |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1812 | |
| 1813 | |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1814 | void LazyValueInfo::printLVI(Function &F, DominatorTree &DTree, raw_ostream &OS) { |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1815 | if (PImpl) { |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1816 | getImpl(PImpl, AC, DL, DT).printLVI(F, DTree, OS); |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1817 | } |
| 1818 | } |
| 1819 | |
Brian M. Rzycki | f1a7df5 | 2018-02-16 16:35:17 +0000 | [diff] [blame] | 1820 | void LazyValueInfo::disableDT() { |
| 1821 | if (PImpl) |
| 1822 | getImpl(PImpl, AC, DL, DT).disableDT(); |
| 1823 | } |
| 1824 | |
| 1825 | void LazyValueInfo::enableDT() { |
| 1826 | if (PImpl) |
| 1827 | getImpl(PImpl, AC, DL, DT).enableDT(); |
| 1828 | } |
| 1829 | |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1830 | // Print the LVI for the function arguments at the start of each basic block. |
| 1831 | void LazyValueInfoAnnotatedWriter::emitBasicBlockStartAnnot( |
| 1832 | const BasicBlock *BB, formatted_raw_ostream &OS) { |
| 1833 | // Find if there are latticevalues defined for arguments of the function. |
| 1834 | auto *F = BB->getParent(); |
| 1835 | for (auto &Arg : F->args()) { |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1836 | ValueLatticeElement Result = LVIImpl->getValueInBlock( |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1837 | const_cast<Argument *>(&Arg), const_cast<BasicBlock *>(BB)); |
| 1838 | if (Result.isUndefined()) |
| 1839 | continue; |
| 1840 | OS << "; LatticeVal for: '" << Arg << "' is: " << Result << "\n"; |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | // This function prints the LVI analysis for the instruction I at the beginning |
| 1845 | // of various basic blocks. It relies on calculated values that are stored in |
Xin Tong | adb5bfe | 2018-04-24 07:38:07 +0000 | [diff] [blame^] | 1846 | // the LazyValueInfoCache, and in the absence of cached values, recalculate the |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1847 | // LazyValueInfo for `I`, and print that info. |
| 1848 | void LazyValueInfoAnnotatedWriter::emitInstructionAnnot( |
| 1849 | const Instruction *I, formatted_raw_ostream &OS) { |
| 1850 | |
| 1851 | auto *ParentBB = I->getParent(); |
| 1852 | SmallPtrSet<const BasicBlock*, 16> BlocksContainingLVI; |
| 1853 | // We can generate (solve) LVI values only for blocks that are dominated by |
| 1854 | // the I's parent. However, to avoid generating LVI for all dominating blocks, |
| 1855 | // that contain redundant/uninteresting information, we print LVI for |
| 1856 | // blocks that may use this LVI information (such as immediate successor |
| 1857 | // blocks, and blocks that contain uses of `I`). |
| 1858 | auto printResult = [&](const BasicBlock *BB) { |
| 1859 | if (!BlocksContainingLVI.insert(BB).second) |
| 1860 | return; |
Florian Hahn | 8af0157 | 2017-09-28 11:09:22 +0000 | [diff] [blame] | 1861 | ValueLatticeElement Result = LVIImpl->getValueInBlock( |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1862 | const_cast<Instruction *>(I), const_cast<BasicBlock *>(BB)); |
| 1863 | OS << "; LatticeVal for: '" << *I << "' in BB: '"; |
| 1864 | BB->printAsOperand(OS, false); |
| 1865 | OS << "' is: " << Result << "\n"; |
| 1866 | }; |
| 1867 | |
| 1868 | printResult(ParentBB); |
Hiroshi Inoue | 8f976ba | 2018-01-17 12:29:38 +0000 | [diff] [blame] | 1869 | // Print the LVI analysis results for the immediate successor blocks, that |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1870 | // are dominated by `ParentBB`. |
| 1871 | for (auto *BBSucc : successors(ParentBB)) |
| 1872 | if (DT.dominates(ParentBB, BBSucc)) |
| 1873 | printResult(BBSucc); |
| 1874 | |
| 1875 | // Print LVI in blocks where `I` is used. |
| 1876 | for (auto *U : I->users()) |
| 1877 | if (auto *UseI = dyn_cast<Instruction>(U)) |
| 1878 | if (!isa<PHINode>(UseI) || DT.dominates(ParentBB, UseI->getParent())) |
| 1879 | printResult(UseI->getParent()); |
| 1880 | |
| 1881 | } |
| 1882 | |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1883 | namespace { |
| 1884 | // Printer class for LazyValueInfo results. |
| 1885 | class LazyValueInfoPrinter : public FunctionPass { |
| 1886 | public: |
| 1887 | static char ID; // Pass identification, replacement for typeid |
| 1888 | LazyValueInfoPrinter() : FunctionPass(ID) { |
| 1889 | initializeLazyValueInfoPrinterPass(*PassRegistry::getPassRegistry()); |
| 1890 | } |
| 1891 | |
| 1892 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 1893 | AU.setPreservesAll(); |
| 1894 | AU.addRequired<LazyValueInfoWrapperPass>(); |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1895 | AU.addRequired<DominatorTreeWrapperPass>(); |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1898 | // Get the mandatory dominator tree analysis and pass this in to the |
| 1899 | // LVIPrinter. We cannot rely on the LVI's DT, since it's optional. |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1900 | bool runOnFunction(Function &F) override { |
| 1901 | dbgs() << "LVI for function '" << F.getName() << "':\n"; |
| 1902 | auto &LVI = getAnalysis<LazyValueInfoWrapperPass>().getLVI(); |
Anna Thomas | 4acfc7e | 2017-06-06 19:25:31 +0000 | [diff] [blame] | 1903 | auto &DTree = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1904 | LVI.printLVI(F, DTree, dbgs()); |
Anna Thomas | e27b39a | 2017-03-22 19:27:12 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | } |
| 1907 | }; |
| 1908 | } |
| 1909 | |
| 1910 | char LazyValueInfoPrinter::ID = 0; |
| 1911 | INITIALIZE_PASS_BEGIN(LazyValueInfoPrinter, "print-lazy-value-info", |
| 1912 | "Lazy Value Info Printer Pass", false, false) |
| 1913 | INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass) |
| 1914 | INITIALIZE_PASS_END(LazyValueInfoPrinter, "print-lazy-value-info", |
| 1915 | "Lazy Value Info Printer Pass", false, false) |