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