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