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