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" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 29 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 30 | #include "llvm/IR/ValueHandle.h" |
Chris Lattner | b584d1e | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 4ec081a | 2012-01-11 23:43:34 +0000 | [diff] [blame] | 33 | #include <map> |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 34 | #include <stack> |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Benjamin Kramer | d9d80b1 | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 36 | using namespace PatternMatch; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 37 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "lazy-value-info" |
| 39 | |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 40 | char LazyValueInfo::ID = 0; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 41 | INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info", |
| 42 | "Lazy Value Information Analysis", false, true) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 43 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 44 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 45 | INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 46 | "Lazy Value Information Analysis", false, true) |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 47 | |
| 48 | namespace llvm { |
| 49 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); } |
| 50 | } |
| 51 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // LVILatticeVal |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 57 | /// This is the information tracked by LazyValueInfo for each value. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 58 | /// |
| 59 | /// FIXME: This is basically just for bringup, this can be made a lot more rich |
| 60 | /// in the future. |
| 61 | /// |
| 62 | namespace { |
| 63 | class LVILatticeVal { |
| 64 | enum LatticeValueTy { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 65 | /// This Value has no known value yet. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 66 | undefined, |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 67 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 68 | /// This Value has a specific constant value. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 69 | constant, |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 70 | |
| 71 | /// This Value is known to not have the specified value. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 72 | notconstant, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 73 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 74 | /// The Value falls within this range. |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 75 | constantrange, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 76 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 77 | /// This value is not known to be constant, and we know that it has a value. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 78 | overdefined |
| 79 | }; |
| 80 | |
| 81 | /// Val: This stores the current lattice value along with the Constant* for |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 82 | /// the constant if this is a 'constant' or 'notconstant' value. |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 83 | LatticeValueTy Tag; |
| 84 | Constant *Val; |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 85 | ConstantRange Range; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 86 | |
| 87 | public: |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 88 | LVILatticeVal() : Tag(undefined), Val(nullptr), Range(1, true) {} |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 90 | static LVILatticeVal get(Constant *C) { |
| 91 | LVILatticeVal Res; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 92 | if (!isa<UndefValue>(C)) |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 93 | Res.markConstant(C); |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 94 | return Res; |
| 95 | } |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 96 | static LVILatticeVal getNot(Constant *C) { |
| 97 | LVILatticeVal Res; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 98 | if (!isa<UndefValue>(C)) |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 99 | Res.markNotConstant(C); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 100 | return Res; |
| 101 | } |
Owen Anderson | 5f1dd09 | 2010-08-10 23:20:01 +0000 | [diff] [blame] | 102 | static LVILatticeVal getRange(ConstantRange CR) { |
| 103 | LVILatticeVal Res; |
| 104 | Res.markConstantRange(CR); |
| 105 | return Res; |
| 106 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 107 | |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 108 | bool isUndefined() const { return Tag == undefined; } |
| 109 | bool isConstant() const { return Tag == constant; } |
| 110 | bool isNotConstant() const { return Tag == notconstant; } |
| 111 | bool isConstantRange() const { return Tag == constantrange; } |
| 112 | bool isOverdefined() const { return Tag == overdefined; } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 113 | |
| 114 | Constant *getConstant() const { |
| 115 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 116 | return Val; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 119 | Constant *getNotConstant() const { |
| 120 | assert(isNotConstant() && "Cannot get the constant of a non-notconstant!"); |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 121 | return Val; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 124 | ConstantRange getConstantRange() const { |
| 125 | assert(isConstantRange() && |
| 126 | "Cannot get the constant-range of a non-constant-range!"); |
| 127 | return Range; |
| 128 | } |
| 129 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 130 | /// Return true if this is a change in status. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 131 | bool markOverdefined() { |
| 132 | if (isOverdefined()) |
| 133 | return false; |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 134 | Tag = overdefined; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 138 | /// Return true if this is a change in status. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 139 | bool markConstant(Constant *V) { |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 140 | assert(V && "Marking constant with NULL"); |
| 141 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 142 | return markConstantRange(ConstantRange(CI->getValue())); |
| 143 | if (isa<UndefValue>(V)) |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 144 | return false; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 145 | |
| 146 | assert((!isConstant() || getConstant() == V) && |
| 147 | "Marking constant with different value"); |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 148 | assert(isUndefined()); |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 149 | Tag = constant; |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 150 | Val = V; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 154 | /// Return true if this is a change in status. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 155 | bool markNotConstant(Constant *V) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 156 | assert(V && "Marking constant with NULL"); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 157 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 158 | return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue())); |
| 159 | if (isa<UndefValue>(V)) |
| 160 | return false; |
| 161 | |
| 162 | assert((!isConstant() || getConstant() != V) && |
| 163 | "Marking constant !constant with same value"); |
| 164 | assert((!isNotConstant() || getNotConstant() == V) && |
| 165 | "Marking !constant with different value"); |
| 166 | assert(isUndefined() || isConstant()); |
| 167 | Tag = notconstant; |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 168 | Val = V; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 172 | /// Return true if this is a change in status. |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 173 | bool markConstantRange(const ConstantRange NewR) { |
| 174 | if (isConstantRange()) { |
| 175 | if (NewR.isEmptySet()) |
| 176 | return markOverdefined(); |
| 177 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 178 | bool changed = Range != NewR; |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 179 | Range = NewR; |
| 180 | return changed; |
| 181 | } |
| 182 | |
| 183 | assert(isUndefined()); |
| 184 | if (NewR.isEmptySet()) |
| 185 | return markOverdefined(); |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 186 | |
| 187 | Tag = constantrange; |
| 188 | Range = NewR; |
| 189 | return true; |
| 190 | } |
| 191 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 192 | /// Merge the specified lattice value into this one, updating this |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 193 | /// one and returning true if anything changed. |
| 194 | bool mergeIn(const LVILatticeVal &RHS) { |
| 195 | if (RHS.isUndefined() || isOverdefined()) return false; |
| 196 | if (RHS.isOverdefined()) return markOverdefined(); |
| 197 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 198 | if (isUndefined()) { |
| 199 | Tag = RHS.Tag; |
| 200 | Val = RHS.Val; |
| 201 | Range = RHS.Range; |
| 202 | return true; |
Chris Lattner | 22db4b5 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 205 | if (isConstant()) { |
| 206 | if (RHS.isConstant()) { |
| 207 | if (Val == RHS.Val) |
| 208 | return false; |
| 209 | return markOverdefined(); |
| 210 | } |
| 211 | |
| 212 | if (RHS.isNotConstant()) { |
| 213 | if (Val == RHS.Val) |
| 214 | return markOverdefined(); |
| 215 | |
| 216 | // Unless we can prove that the two Constants are different, we must |
| 217 | // move to overdefined. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 218 | // FIXME: use DataLayout/TargetLibraryInfo for smarter constant folding. |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 219 | if (ConstantInt *Res = dyn_cast<ConstantInt>( |
| 220 | ConstantFoldCompareInstOperands(CmpInst::ICMP_NE, |
| 221 | getConstant(), |
| 222 | RHS.getNotConstant()))) |
| 223 | if (Res->isOne()) |
| 224 | return markNotConstant(RHS.getNotConstant()); |
| 225 | |
| 226 | return markOverdefined(); |
| 227 | } |
| 228 | |
| 229 | // RHS is a ConstantRange, LHS is a non-integer Constant. |
| 230 | |
| 231 | // FIXME: consider the case where RHS is a range [1, 0) and LHS is |
| 232 | // a function. The correct result is to pick up RHS. |
| 233 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 234 | return markOverdefined(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (isNotConstant()) { |
| 238 | if (RHS.isConstant()) { |
| 239 | if (Val == RHS.Val) |
| 240 | return markOverdefined(); |
| 241 | |
| 242 | // Unless we can prove that the two Constants are different, we must |
| 243 | // move to overdefined. |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 244 | // FIXME: use DataLayout/TargetLibraryInfo for smarter constant folding. |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 245 | if (ConstantInt *Res = dyn_cast<ConstantInt>( |
| 246 | ConstantFoldCompareInstOperands(CmpInst::ICMP_NE, |
| 247 | getNotConstant(), |
| 248 | RHS.getConstant()))) |
| 249 | if (Res->isOne()) |
| 250 | return false; |
| 251 | |
| 252 | return markOverdefined(); |
| 253 | } |
| 254 | |
| 255 | if (RHS.isNotConstant()) { |
| 256 | if (Val == RHS.Val) |
| 257 | return false; |
| 258 | return markOverdefined(); |
| 259 | } |
| 260 | |
| 261 | return markOverdefined(); |
| 262 | } |
| 263 | |
| 264 | assert(isConstantRange() && "New LVILattice type?"); |
| 265 | if (!RHS.isConstantRange()) |
| 266 | return markOverdefined(); |
| 267 | |
| 268 | ConstantRange NewR = Range.unionWith(RHS.getConstantRange()); |
| 269 | if (NewR.isFullSet()) |
| 270 | return markOverdefined(); |
| 271 | return markConstantRange(NewR); |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | } // end anonymous namespace. |
| 276 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 277 | namespace llvm { |
Chandler Carruth | 2b1ba48 | 2011-04-18 18:49:44 +0000 | [diff] [blame] | 278 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) |
| 279 | LLVM_ATTRIBUTE_USED; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 280 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) { |
| 281 | if (Val.isUndefined()) |
| 282 | return OS << "undefined"; |
| 283 | if (Val.isOverdefined()) |
| 284 | return OS << "overdefined"; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 285 | |
| 286 | if (Val.isNotConstant()) |
| 287 | return OS << "notconstant<" << *Val.getNotConstant() << '>'; |
Owen Anderson | 8afac04 | 2010-08-09 20:50:46 +0000 | [diff] [blame] | 288 | else if (Val.isConstantRange()) |
| 289 | return OS << "constantrange<" << Val.getConstantRange().getLower() << ", " |
| 290 | << Val.getConstantRange().getUpper() << '>'; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 291 | return OS << "constant<" << *Val.getConstant() << '>'; |
| 292 | } |
| 293 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 294 | |
| 295 | //===----------------------------------------------------------------------===// |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 296 | // LazyValueInfoCache Decl |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 297 | //===----------------------------------------------------------------------===// |
| 298 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 299 | namespace { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 300 | /// A callback value handle updates the cache when values are erased. |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 301 | class LazyValueInfoCache; |
| 302 | struct LVIValueHandle : public CallbackVH { |
| 303 | LazyValueInfoCache *Parent; |
| 304 | |
| 305 | LVIValueHandle(Value *V, LazyValueInfoCache *P) |
| 306 | : CallbackVH(V), Parent(P) { } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 307 | |
| 308 | void deleted() override; |
| 309 | void allUsesReplacedWith(Value *V) override { |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 310 | deleted(); |
| 311 | } |
| 312 | }; |
| 313 | } |
| 314 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 315 | namespace { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 316 | /// This is the cache kept by LazyValueInfo which |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 317 | /// maintains information about queries across the clients' queries. |
| 318 | class LazyValueInfoCache { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 319 | /// This is all of the cached block information for exactly one Value*. |
| 320 | /// The entries are sorted by the BasicBlock* of the |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 321 | /// entries, allowing us to do a lookup with a binary search. |
Bill Wendling | 4ec081a | 2012-01-11 23:43:34 +0000 | [diff] [blame] | 322 | typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 323 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 324 | /// This is all of the cached information for all values, |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 325 | /// mapped from Value* to key information. |
Bill Wendling | 58c7569 | 2012-01-12 01:41:03 +0000 | [diff] [blame] | 326 | std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache; |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 327 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 328 | /// This tracks, on a per-block basis, the set of values that are |
| 329 | /// over-defined at the end of that block. This is required |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 330 | /// for cache updating. |
| 331 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 332 | DenseSet<OverDefinedPairTy> OverDefinedCache; |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 333 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 334 | /// Keep track of all blocks that we have ever seen, so we |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 335 | /// don't spend time removing unused blocks from our caches. |
| 336 | DenseSet<AssertingVH<BasicBlock> > SeenBlocks; |
| 337 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 338 | /// This stack holds the state of the value solver during a query. |
| 339 | /// It basically emulates the callstack of the naive |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 340 | /// recursive value lookup process. |
| 341 | std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 342 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 343 | /// Keeps track of which block-value pairs are in BlockValueStack. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 344 | DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet; |
| 345 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 346 | /// Push BV onto BlockValueStack unless it's already in there. |
| 347 | /// Returns true on success. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 348 | bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) { |
| 349 | if (BlockValueSet.count(BV)) |
| 350 | return false; // It's already in the stack. |
| 351 | |
| 352 | BlockValueStack.push(BV); |
| 353 | BlockValueSet.insert(BV); |
| 354 | return true; |
| 355 | } |
| 356 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 357 | /// A pointer to the cache of @llvm.assume calls. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 358 | AssumptionCache *AC; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 359 | /// An optional DL pointer. |
| 360 | const DataLayout *DL; |
| 361 | /// An optional DT pointer. |
| 362 | DominatorTree *DT; |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 363 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 364 | friend struct LVIValueHandle; |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 365 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 366 | void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) { |
| 367 | SeenBlocks.insert(BB); |
| 368 | lookup(Val)[BB] = Result; |
| 369 | if (Result.isOverdefined()) |
| 370 | OverDefinedCache.insert(std::make_pair(BB, Val)); |
| 371 | } |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 372 | |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 373 | LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 374 | bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 375 | LVILatticeVal &Result, |
| 376 | Instruction *CxtI = nullptr); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 377 | bool hasBlockValue(Value *Val, BasicBlock *BB); |
| 378 | |
| 379 | // These methods process one work item and may add more. A false value |
| 380 | // returned means that the work item was not completely processed and must |
| 381 | // be revisited after going through the new items. |
| 382 | bool solveBlockValue(Value *Val, BasicBlock *BB); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 383 | bool solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 384 | Value *Val, BasicBlock *BB); |
| 385 | bool solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 386 | PHINode *PN, BasicBlock *BB); |
| 387 | bool solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 388 | Instruction *BBI, BasicBlock *BB); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 389 | void mergeAssumeBlockValueConstantRange(Value *Val, LVILatticeVal &BBLV, |
| 390 | Instruction *BBI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 391 | |
| 392 | void solve(); |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 393 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 394 | ValueCacheEntryTy &lookup(Value *V) { |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 395 | return ValueCache[LVIValueHandle(V, this)]; |
| 396 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 397 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 398 | public: |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 399 | /// This is the query interface to determine the lattice |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 400 | /// value for the specified Value* at the end of the specified block. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 401 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB, |
| 402 | Instruction *CxtI = nullptr); |
| 403 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 404 | /// This is the query interface to determine the lattice |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 405 | /// value for the specified Value* at the specified instruction (generally |
| 406 | /// from an assume intrinsic). |
| 407 | LVILatticeVal getValueAt(Value *V, Instruction *CxtI); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 408 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 409 | /// This is the query interface to determine the lattice |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 410 | /// value for the specified Value* that is true on the specified edge. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 411 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB, |
| 412 | Instruction *CxtI = nullptr); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 413 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 414 | /// This is the update interface to inform the cache that an edge from |
| 415 | /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc. |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 416 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 417 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 418 | /// This is part of the update interface to inform the cache |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 419 | /// that a block has been deleted. |
| 420 | void eraseBlock(BasicBlock *BB); |
| 421 | |
| 422 | /// clear - Empty the cache. |
| 423 | void clear() { |
Benjamin Kramer | bbf3c60 | 2011-12-03 15:19:55 +0000 | [diff] [blame] | 424 | SeenBlocks.clear(); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 425 | ValueCache.clear(); |
| 426 | OverDefinedCache.clear(); |
| 427 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 428 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 429 | LazyValueInfoCache(AssumptionCache *AC, const DataLayout *DL = nullptr, |
| 430 | DominatorTree *DT = nullptr) |
| 431 | : AC(AC), DL(DL), DT(DT) {} |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 432 | }; |
| 433 | } // end anonymous namespace |
| 434 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 435 | void LVIValueHandle::deleted() { |
| 436 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 437 | |
| 438 | SmallVector<OverDefinedPairTy, 4> ToErase; |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 439 | for (const OverDefinedPairTy &P : Parent->OverDefinedCache) |
| 440 | if (P.second == getValPtr()) |
| 441 | ToErase.push_back(P); |
| 442 | for (const OverDefinedPairTy &P : ToErase) |
| 443 | Parent->OverDefinedCache.erase(P); |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 444 | |
Owen Anderson | 7b974a4 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 445 | // This erasure deallocates *this, so it MUST happen after we're done |
| 446 | // using any and all members of *this. |
| 447 | Parent->ValueCache.erase(*this); |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 450 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 451 | // Shortcut if we have never seen this block. |
| 452 | DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB); |
| 453 | if (I == SeenBlocks.end()) |
| 454 | return; |
| 455 | SeenBlocks.erase(I); |
| 456 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 457 | SmallVector<OverDefinedPairTy, 4> ToErase; |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 458 | for (const OverDefinedPairTy& P : OverDefinedCache) |
| 459 | if (P.first == BB) |
| 460 | ToErase.push_back(P); |
| 461 | for (const OverDefinedPairTy &P : ToErase) |
| 462 | OverDefinedCache.erase(P); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 463 | |
Bill Wendling | 58c7569 | 2012-01-12 01:41:03 +0000 | [diff] [blame] | 464 | for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 465 | I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I) |
| 466 | I->second.erase(BB); |
| 467 | } |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 468 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 469 | void LazyValueInfoCache::solve() { |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 470 | while (!BlockValueStack.empty()) { |
| 471 | std::pair<BasicBlock*, Value*> &e = BlockValueStack.top(); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 472 | assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!"); |
| 473 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 474 | if (solveBlockValue(e.second, e.first)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 475 | // The work item was completely processed. |
| 476 | assert(BlockValueStack.top() == e && "Nothing should have been pushed!"); |
| 477 | assert(lookup(e.second).count(e.first) && "Result should be in cache!"); |
| 478 | |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 479 | BlockValueStack.pop(); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 480 | BlockValueSet.erase(e); |
| 481 | } else { |
| 482 | // More work needs to be done before revisiting. |
| 483 | assert(BlockValueStack.top() != e && "Stack should have been pushed!"); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 484 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) { |
| 489 | // If already a constant, there is nothing to compute. |
| 490 | if (isa<Constant>(Val)) |
| 491 | return true; |
| 492 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 493 | LVIValueHandle ValHandle(Val, this); |
Benjamin Kramer | f29db27 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 494 | std::map<LVIValueHandle, ValueCacheEntryTy>::iterator I = |
| 495 | ValueCache.find(ValHandle); |
| 496 | if (I == ValueCache.end()) return false; |
| 497 | return I->second.count(BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 500 | LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 501 | // If already a constant, there is nothing to compute. |
| 502 | if (Constant *VC = dyn_cast<Constant>(Val)) |
| 503 | return LVILatticeVal::get(VC); |
| 504 | |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 505 | SeenBlocks.insert(BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 506 | return lookup(Val)[BB]; |
| 507 | } |
| 508 | |
| 509 | bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) { |
| 510 | if (isa<Constant>(Val)) |
| 511 | return true; |
| 512 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 513 | if (lookup(Val).count(BB)) { |
| 514 | // If we have a cached value, use that. |
| 515 | DEBUG(dbgs() << " reuse BB '" << BB->getName() |
| 516 | << "' val=" << lookup(Val)[BB] << '\n'); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 517 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 518 | // Since we're reusing a cached value, we don't need to update the |
| 519 | // OverDefinedCache. The cache will have been properly updated whenever the |
| 520 | // cached value was inserted. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 521 | return true; |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 524 | // Hold off inserting this value into the Cache in case we have to return |
| 525 | // false and come back later. |
| 526 | LVILatticeVal Res; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 527 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 528 | Instruction *BBI = dyn_cast<Instruction>(Val); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 529 | if (!BBI || BBI->getParent() != BB) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 530 | if (!solveBlockValueNonLocal(Res, Val, BB)) |
| 531 | return false; |
| 532 | insertResult(Val, BB, Res); |
| 533 | return true; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 534 | } |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 535 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 536 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 537 | if (!solveBlockValuePHINode(Res, PN, BB)) |
| 538 | return false; |
| 539 | insertResult(Val, BB, Res); |
| 540 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 541 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 542 | |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 543 | if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 544 | Res = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType())); |
| 545 | insertResult(Val, BB, Res); |
| 546 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 549 | // We can only analyze the definitions of certain classes of instructions |
| 550 | // (integral binops and casts at the moment), so bail if this isn't one. |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 551 | LVILatticeVal Result; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 552 | if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) || |
| 553 | !BBI->getType()->isIntegerTy()) { |
| 554 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 555 | << "' - overdefined because inst def found.\n"); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 556 | Res.markOverdefined(); |
| 557 | insertResult(Val, BB, Res); |
| 558 | return true; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 559 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 560 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 561 | // FIXME: We're currently limited to binops with a constant RHS. This should |
| 562 | // be improved. |
| 563 | BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI); |
| 564 | if (BO && !isa<ConstantInt>(BO->getOperand(1))) { |
| 565 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 566 | << "' - overdefined because inst def found.\n"); |
| 567 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 568 | Res.markOverdefined(); |
| 569 | insertResult(Val, BB, Res); |
| 570 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 571 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 572 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 573 | if (!solveBlockValueConstantRange(Res, BBI, BB)) |
| 574 | return false; |
| 575 | insertResult(Val, BB, Res); |
| 576 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) { |
| 580 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 581 | return L->getPointerAddressSpace() == 0 && |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 582 | GetUnderlyingObject(L->getPointerOperand()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 583 | } |
| 584 | if (StoreInst *S = dyn_cast<StoreInst>(I)) { |
| 585 | return S->getPointerAddressSpace() == 0 && |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 586 | GetUnderlyingObject(S->getPointerOperand()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 587 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 588 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 589 | if (MI->isVolatile()) return false; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 590 | |
| 591 | // FIXME: check whether it has a valuerange that excludes zero? |
| 592 | ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 593 | if (!Len || Len->isZero()) return false; |
| 594 | |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 595 | if (MI->getDestAddressSpace() == 0) |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 596 | if (GetUnderlyingObject(MI->getRawDest()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 597 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 598 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 599 | if (MTI->getSourceAddressSpace() == 0) |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 600 | if (GetUnderlyingObject(MTI->getRawSource()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 601 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 602 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 603 | return false; |
| 604 | } |
| 605 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 606 | bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 607 | Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 608 | LVILatticeVal Result; // Start Undefined. |
| 609 | |
| 610 | // If this is a pointer, and there's a load from that pointer in this BB, |
| 611 | // then we know that the pointer can't be NULL. |
| 612 | bool NotNull = false; |
| 613 | if (Val->getType()->isPointerTy()) { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 614 | if (isKnownNonNull(Val)) { |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 615 | NotNull = true; |
| 616 | } else { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 617 | Value *UnderlyingVal = GetUnderlyingObject(Val); |
| 618 | // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge |
| 619 | // inside InstructionDereferencesPointer either. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 620 | if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, nullptr, 1)) { |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 621 | for (Instruction &I : *BB) { |
| 622 | if (InstructionDereferencesPointer(&I, UnderlyingVal)) { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 623 | NotNull = true; |
| 624 | break; |
| 625 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 626 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | // If this is the entry block, we must be asking about an argument. The |
| 632 | // value is overdefined. |
| 633 | if (BB == &BB->getParent()->getEntryBlock()) { |
| 634 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 635 | if (NotNull) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 636 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 637 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 638 | } else { |
| 639 | Result.markOverdefined(); |
| 640 | } |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 641 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 642 | return true; |
| 643 | } |
| 644 | |
| 645 | // Loop over all of our predecessors, merging what we know from them into |
| 646 | // result. |
| 647 | bool EdgesMissing = false; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 648 | 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] | 649 | LVILatticeVal EdgeResult; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 650 | EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 651 | if (EdgesMissing) |
| 652 | continue; |
| 653 | |
| 654 | Result.mergeIn(EdgeResult); |
| 655 | |
| 656 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 657 | // to overdefined. |
| 658 | if (Result.isOverdefined()) { |
| 659 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 660 | << "' - overdefined because of pred.\n"); |
| 661 | // If we previously determined that this is a pointer that can't be null |
| 662 | // then return that rather than giving up entirely. |
| 663 | if (NotNull) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 664 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 665 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 666 | } |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 667 | |
| 668 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 669 | return true; |
| 670 | } |
| 671 | } |
| 672 | if (EdgesMissing) |
| 673 | return false; |
| 674 | |
| 675 | // Return the merged value, which is more precise than 'overdefined'. |
| 676 | assert(!Result.isOverdefined()); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 677 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 678 | return true; |
| 679 | } |
| 680 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 681 | bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 682 | PHINode *PN, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 683 | LVILatticeVal Result; // Start Undefined. |
| 684 | |
| 685 | // Loop over all of our predecessors, merging what we know from them into |
| 686 | // result. |
| 687 | bool EdgesMissing = false; |
| 688 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 689 | BasicBlock *PhiBB = PN->getIncomingBlock(i); |
| 690 | Value *PhiVal = PN->getIncomingValue(i); |
| 691 | LVILatticeVal EdgeResult; |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 692 | // Note that we can provide PN as the context value to getEdgeValue, even |
| 693 | // though the results will be cached, because PN is the value being used as |
| 694 | // the cache key in the caller. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 695 | EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 696 | if (EdgesMissing) |
| 697 | continue; |
| 698 | |
| 699 | Result.mergeIn(EdgeResult); |
| 700 | |
| 701 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 702 | // to overdefined. |
| 703 | if (Result.isOverdefined()) { |
| 704 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 705 | << "' - overdefined because of pred.\n"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 706 | |
| 707 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 708 | return true; |
| 709 | } |
| 710 | } |
| 711 | if (EdgesMissing) |
| 712 | return false; |
| 713 | |
| 714 | // Return the merged value, which is more precise than 'overdefined'. |
| 715 | assert(!Result.isOverdefined() && "Possible PHI in entry block?"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 716 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 717 | return true; |
| 718 | } |
| 719 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 720 | static bool getValueFromFromCondition(Value *Val, ICmpInst *ICI, |
| 721 | LVILatticeVal &Result, |
| 722 | bool isTrueDest = true); |
| 723 | |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 724 | // If we can determine a constant range for the value Val in the context |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 725 | // provided by the instruction BBI, then merge it into BBLV. If we did find a |
| 726 | // constant range, return true. |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 727 | void LazyValueInfoCache::mergeAssumeBlockValueConstantRange(Value *Val, |
| 728 | LVILatticeVal &BBLV, |
| 729 | Instruction *BBI) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 730 | BBI = BBI ? BBI : dyn_cast<Instruction>(Val); |
| 731 | if (!BBI) |
| 732 | return; |
| 733 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 734 | for (auto &AssumeVH : AC->assumptions()) { |
| 735 | if (!AssumeVH) |
| 736 | continue; |
| 737 | auto *I = cast<CallInst>(AssumeVH); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 738 | if (!isValidAssumeForContext(I, BBI, DL, DT)) |
| 739 | continue; |
| 740 | |
| 741 | Value *C = I->getArgOperand(0); |
| 742 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(C)) { |
| 743 | LVILatticeVal Result; |
| 744 | if (getValueFromFromCondition(Val, ICI, Result)) { |
| 745 | if (BBLV.isOverdefined()) |
| 746 | BBLV = Result; |
| 747 | else |
| 748 | BBLV.mergeIn(Result); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 754 | bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 755 | Instruction *BBI, |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 756 | BasicBlock *BB) { |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 757 | // Figure out the range of the LHS. If that fails, bail. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 758 | if (!hasBlockValue(BBI->getOperand(0), BB)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 759 | if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0)))) |
| 760 | return false; |
| 761 | BBLV.markOverdefined(); |
| 762 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 765 | LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 766 | mergeAssumeBlockValueConstantRange(BBI->getOperand(0), LHSVal, BBI); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 767 | if (!LHSVal.isConstantRange()) { |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 768 | BBLV.markOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 769 | return true; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 772 | ConstantRange LHSRange = LHSVal.getConstantRange(); |
| 773 | ConstantRange RHSRange(1); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 774 | IntegerType *ResultTy = cast<IntegerType>(BBI->getType()); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 775 | if (isa<BinaryOperator>(BBI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 776 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) { |
| 777 | RHSRange = ConstantRange(RHS->getValue()); |
| 778 | } else { |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 779 | BBLV.markOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 780 | return true; |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 781 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 782 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 783 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 784 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 785 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 786 | // more definitions. |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 787 | LVILatticeVal Result; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 788 | switch (BBI->getOpcode()) { |
| 789 | case Instruction::Add: |
| 790 | Result.markConstantRange(LHSRange.add(RHSRange)); |
| 791 | break; |
| 792 | case Instruction::Sub: |
| 793 | Result.markConstantRange(LHSRange.sub(RHSRange)); |
| 794 | break; |
| 795 | case Instruction::Mul: |
| 796 | Result.markConstantRange(LHSRange.multiply(RHSRange)); |
| 797 | break; |
| 798 | case Instruction::UDiv: |
| 799 | Result.markConstantRange(LHSRange.udiv(RHSRange)); |
| 800 | break; |
| 801 | case Instruction::Shl: |
| 802 | Result.markConstantRange(LHSRange.shl(RHSRange)); |
| 803 | break; |
| 804 | case Instruction::LShr: |
| 805 | Result.markConstantRange(LHSRange.lshr(RHSRange)); |
| 806 | break; |
| 807 | case Instruction::Trunc: |
| 808 | Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth())); |
| 809 | break; |
| 810 | case Instruction::SExt: |
| 811 | Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth())); |
| 812 | break; |
| 813 | case Instruction::ZExt: |
| 814 | Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth())); |
| 815 | break; |
| 816 | case Instruction::BitCast: |
| 817 | Result.markConstantRange(LHSRange); |
| 818 | break; |
Nick Lewycky | ad48e01 | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 819 | case Instruction::And: |
| 820 | Result.markConstantRange(LHSRange.binaryAnd(RHSRange)); |
| 821 | break; |
| 822 | case Instruction::Or: |
| 823 | Result.markConstantRange(LHSRange.binaryOr(RHSRange)); |
| 824 | break; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 825 | |
| 826 | // Unhandled instructions are overdefined. |
| 827 | default: |
| 828 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 829 | << "' - overdefined because inst def found.\n"); |
| 830 | Result.markOverdefined(); |
| 831 | break; |
| 832 | } |
| 833 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 834 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 835 | return true; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 838 | bool getValueFromFromCondition(Value *Val, ICmpInst *ICI, |
| 839 | LVILatticeVal &Result, bool isTrueDest) { |
| 840 | if (ICI && isa<Constant>(ICI->getOperand(1))) { |
| 841 | if (ICI->isEquality() && ICI->getOperand(0) == Val) { |
| 842 | // We know that V has the RHS constant if this is a true SETEQ or |
| 843 | // false SETNE. |
| 844 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
| 845 | Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
| 846 | else |
| 847 | Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
| 848 | return true; |
| 849 | } |
| 850 | |
| 851 | // Recognize the range checking idiom that InstCombine produces. |
| 852 | // (X-C1) u< C2 --> [C1, C1+C2) |
| 853 | ConstantInt *NegOffset = nullptr; |
| 854 | if (ICI->getPredicate() == ICmpInst::ICMP_ULT) |
| 855 | match(ICI->getOperand(0), m_Add(m_Specific(Val), |
| 856 | m_ConstantInt(NegOffset))); |
| 857 | |
| 858 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1)); |
| 859 | if (CI && (ICI->getOperand(0) == Val || NegOffset)) { |
| 860 | // Calculate the range of values that would satisfy the comparison. |
| 861 | ConstantRange CmpRange(CI->getValue()); |
| 862 | ConstantRange TrueValues = |
| 863 | ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange); |
| 864 | |
| 865 | if (NegOffset) // Apply the offset from above. |
| 866 | TrueValues = TrueValues.subtract(NegOffset->getValue()); |
| 867 | |
| 868 | // If we're interested in the false dest, invert the condition. |
| 869 | if (!isTrueDest) TrueValues = TrueValues.inverse(); |
| 870 | |
| 871 | Result = LVILatticeVal::getRange(TrueValues); |
| 872 | return true; |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | return false; |
| 877 | } |
| 878 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 879 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if |
| 880 | /// Val is not constrained on the edge. |
| 881 | static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, |
| 882 | BasicBlock *BBTo, LVILatticeVal &Result) { |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 883 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 884 | // know that v != 0. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 885 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 886 | // 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] | 887 | // we may be able to infer something from the condition. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 888 | if (BI->isConditional() && |
| 889 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 890 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 891 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 892 | "BBTo isn't a successor of BBFrom"); |
| 893 | |
| 894 | // If V is the condition of the branch itself, then we know exactly what |
| 895 | // it is. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 896 | if (BI->getCondition() == Val) { |
| 897 | Result = LVILatticeVal::get(ConstantInt::get( |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 898 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 899 | return true; |
| 900 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 901 | |
| 902 | // If the condition of the branch is an equality comparison, we may be |
| 903 | // able to infer the value. |
Sanjay Patel | d729115 | 2015-01-09 16:28:15 +0000 | [diff] [blame] | 904 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) |
| 905 | if (getValueFromFromCondition(Val, ICI, Result, isTrueDest)) |
| 906 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 909 | |
| 910 | // If the edge was formed by a switch on the value, then we may know exactly |
| 911 | // what it is. |
| 912 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 913 | if (SI->getCondition() != Val) |
| 914 | return false; |
| 915 | |
| 916 | bool DefaultCase = SI->getDefaultDest() == BBTo; |
| 917 | unsigned BitWidth = Val->getType()->getIntegerBitWidth(); |
| 918 | ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/); |
| 919 | |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 920 | for (SwitchInst::CaseIt i : SI->cases()) { |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 921 | ConstantRange EdgeVal(i.getCaseValue()->getValue()); |
Manman Ren | f3fedb6 | 2012-09-05 23:45:58 +0000 | [diff] [blame] | 922 | if (DefaultCase) { |
| 923 | // It is possible that the default destination is the destination of |
| 924 | // some cases. There is no need to perform difference for those cases. |
| 925 | if (i.getCaseSuccessor() != BBTo) |
| 926 | EdgesVals = EdgesVals.difference(EdgeVal); |
| 927 | } else if (i.getCaseSuccessor() == BBTo) |
Nuno Lopes | ac59380 | 2012-05-18 21:02:10 +0000 | [diff] [blame] | 928 | EdgesVals = EdgesVals.unionWith(EdgeVal); |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 929 | } |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 930 | Result = LVILatticeVal::getRange(EdgesVals); |
| 931 | return true; |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 932 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 933 | return false; |
| 934 | } |
| 935 | |
Sanjay Patel | 938e279 | 2015-01-09 16:35:37 +0000 | [diff] [blame] | 936 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at |
| 937 | /// the basic block if the edge does not constrain Val. |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 938 | bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 939 | BasicBlock *BBTo, LVILatticeVal &Result, |
| 940 | Instruction *CxtI) { |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 941 | // If already a constant, there is nothing to compute. |
| 942 | if (Constant *VC = dyn_cast<Constant>(Val)) { |
| 943 | Result = LVILatticeVal::get(VC); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 944 | return true; |
| 945 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 946 | |
| 947 | if (getEdgeValueLocal(Val, BBFrom, BBTo, Result)) { |
| 948 | if (!Result.isConstantRange() || |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 949 | Result.getConstantRange().getSingleElement()) |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 950 | return true; |
| 951 | |
| 952 | // FIXME: this check should be moved to the beginning of the function when |
| 953 | // LVI better supports recursive values. Even for the single value case, we |
| 954 | // can intersect to detect dead code (an empty range). |
| 955 | if (!hasBlockValue(Val, BBFrom)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 956 | if (pushBlockValue(std::make_pair(BBFrom, Val))) |
| 957 | return false; |
| 958 | Result.markOverdefined(); |
| 959 | return true; |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | // Try to intersect ranges of the BB and the constraint on the edge. |
| 963 | LVILatticeVal InBlock = getBlockValue(Val, BBFrom); |
Hal Finkel | a3f23e3 | 2014-10-14 16:04:49 +0000 | [diff] [blame] | 964 | mergeAssumeBlockValueConstantRange(Val, InBlock, BBFrom->getTerminator()); |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 965 | // See note on the use of the CxtI with mergeAssumeBlockValueConstantRange, |
| 966 | // and caching, below. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 967 | mergeAssumeBlockValueConstantRange(Val, InBlock, CxtI); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 968 | if (!InBlock.isConstantRange()) |
| 969 | return true; |
| 970 | |
| 971 | ConstantRange Range = |
| 972 | Result.getConstantRange().intersectWith(InBlock.getConstantRange()); |
| 973 | Result = LVILatticeVal::getRange(Range); |
| 974 | return true; |
| 975 | } |
| 976 | |
| 977 | if (!hasBlockValue(Val, BBFrom)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 978 | if (pushBlockValue(std::make_pair(BBFrom, Val))) |
| 979 | return false; |
| 980 | Result.markOverdefined(); |
| 981 | return true; |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 984 | // If we couldn't compute the value on the edge, use the value from the BB. |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 985 | Result = getBlockValue(Val, BBFrom); |
Hal Finkel | a3f23e3 | 2014-10-14 16:04:49 +0000 | [diff] [blame] | 986 | mergeAssumeBlockValueConstantRange(Val, Result, BBFrom->getTerminator()); |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 987 | // We can use the context instruction (generically the ultimate instruction |
| 988 | // the calling pass is trying to simplify) here, even though the result of |
| 989 | // this function is generally cached when called from the solve* functions |
| 990 | // (and that cached result might be used with queries using a different |
| 991 | // context instruction), because when this function is called from the solve* |
| 992 | // functions, the context instruction is not provided. When called from |
| 993 | // LazyValueInfoCache::getValueOnEdge, the context instruction is provided, |
| 994 | // but then the result is not cached. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 995 | mergeAssumeBlockValueConstantRange(Val, Result, CxtI); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 996 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 999 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB, |
| 1000 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1001 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1002 | << BB->getName() << "'\n"); |
| 1003 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1004 | assert(BlockValueStack.empty() && BlockValueSet.empty()); |
| 1005 | pushBlockValue(std::make_pair(BB, V)); |
| 1006 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1007 | solve(); |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 1008 | LVILatticeVal Result = getBlockValue(V, BB); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1009 | mergeAssumeBlockValueConstantRange(V, Result, CxtI); |
| 1010 | |
| 1011 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
| 1012 | return Result; |
| 1013 | } |
| 1014 | |
| 1015 | LVILatticeVal LazyValueInfoCache::getValueAt(Value *V, Instruction *CxtI) { |
| 1016 | DEBUG(dbgs() << "LVI Getting value " << *V << " at '" |
| 1017 | << CxtI->getName() << "'\n"); |
| 1018 | |
| 1019 | LVILatticeVal Result; |
| 1020 | mergeAssumeBlockValueConstantRange(V, Result, CxtI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1021 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1022 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1023 | return Result; |
| 1024 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1025 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1026 | LVILatticeVal LazyValueInfoCache:: |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1027 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, |
| 1028 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1029 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1030 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1031 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1032 | LVILatticeVal Result; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1033 | if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1034 | solve(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1035 | bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1036 | (void)WasFastQuery; |
| 1037 | assert(WasFastQuery && "More work to do after problem solved?"); |
| 1038 | } |
| 1039 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1040 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1041 | return Result; |
| 1042 | } |
| 1043 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1044 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 1045 | BasicBlock *NewSucc) { |
| 1046 | // When an edge in the graph has been threaded, values that we could not |
| 1047 | // determine a value for before (i.e. were marked overdefined) may be possible |
| 1048 | // to solve now. We do NOT try to proactively update these values. Instead, |
| 1049 | // we clear their entries from the cache, and allow lazy updating to recompute |
| 1050 | // them when needed. |
| 1051 | |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 1052 | // The updating process is fairly simple: we need to drop cached info |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1053 | // for all values that were marked overdefined in OldSucc, and for those same |
| 1054 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 1055 | // also marked overdefined. |
| 1056 | std::vector<BasicBlock*> worklist; |
| 1057 | worklist.push_back(OldSucc); |
| 1058 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1059 | DenseSet<Value*> ClearSet; |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 1060 | for (OverDefinedPairTy &P : OverDefinedCache) |
| 1061 | if (P.first == OldSucc) |
| 1062 | ClearSet.insert(P.second); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1063 | |
| 1064 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 1065 | // NOTE: We do not need a visited list since any blocks we have already |
| 1066 | // visited will have had their overdefined markers cleared already, and we |
| 1067 | // thus won't loop to their successors. |
| 1068 | while (!worklist.empty()) { |
| 1069 | BasicBlock *ToUpdate = worklist.back(); |
| 1070 | worklist.pop_back(); |
| 1071 | |
| 1072 | // Skip blocks only accessible through NewSucc. |
| 1073 | if (ToUpdate == NewSucc) continue; |
| 1074 | |
| 1075 | bool changed = false; |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 1076 | for (Value *V : ClearSet) { |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1077 | // If a value was marked overdefined in OldSucc, and is here too... |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 1078 | DenseSet<OverDefinedPairTy>::iterator OI = |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 1079 | OverDefinedCache.find(std::make_pair(ToUpdate, V)); |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1080 | if (OI == OverDefinedCache.end()) continue; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1081 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1082 | // Remove it from the caches. |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 1083 | ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(V, this)]; |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1084 | ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1085 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1086 | assert(CI != Entry.end() && "Couldn't find entry to update?"); |
| 1087 | Entry.erase(CI); |
| 1088 | OverDefinedCache.erase(OI); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1089 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1090 | // If we removed anything, then we potentially need to update |
| 1091 | // blocks successors too. |
| 1092 | changed = true; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1093 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1094 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1095 | if (!changed) continue; |
| 1096 | |
| 1097 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 1098 | } |
| 1099 | } |
| 1100 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1101 | //===----------------------------------------------------------------------===// |
| 1102 | // LazyValueInfo Impl |
| 1103 | //===----------------------------------------------------------------------===// |
| 1104 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1105 | /// This lazily constructs the LazyValueInfoCache. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1106 | static LazyValueInfoCache &getCache(void *&PImpl, AssumptionCache *AC, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1107 | const DataLayout *DL = nullptr, |
| 1108 | DominatorTree *DT = nullptr) { |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1109 | if (!PImpl) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1110 | PImpl = new LazyValueInfoCache(AC, DL, DT); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1111 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 1112 | } |
| 1113 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1114 | bool LazyValueInfo::runOnFunction(Function &F) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1115 | AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1116 | |
| 1117 | DominatorTreeWrapperPass *DTWP = |
| 1118 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 1119 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1120 | |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 1121 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1122 | DL = DLP ? &DLP->getDataLayout() : nullptr; |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 1123 | |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1124 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1125 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1126 | if (PImpl) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1127 | getCache(PImpl, AC, DL, DT).clear(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1128 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1129 | // Fully lazy. |
| 1130 | return false; |
| 1131 | } |
| 1132 | |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1133 | void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1134 | AU.setPreservesAll(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1135 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1136 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1139 | void LazyValueInfo::releaseMemory() { |
| 1140 | // If the cache was allocated, free it. |
| 1141 | if (PImpl) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1142 | delete &getCache(PImpl, AC); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1143 | PImpl = nullptr; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1147 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB, |
| 1148 | Instruction *CxtI) { |
| 1149 | LVILatticeVal Result = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1150 | getCache(PImpl, AC, DL, DT).getValueInBlock(V, BB, CxtI); |
| 1151 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1152 | if (Result.isConstant()) |
| 1153 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1154 | if (Result.isConstantRange()) { |
Owen Anderson | 38f6b7f | 2010-08-27 23:29:38 +0000 | [diff] [blame] | 1155 | ConstantRange CR = Result.getConstantRange(); |
| 1156 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1157 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1158 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1159 | return nullptr; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1160 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1161 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1162 | /// Determine whether the specified value is known to be a |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1163 | /// constant on the specified edge. Return null if not. |
| 1164 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1165 | BasicBlock *ToBB, |
| 1166 | Instruction *CxtI) { |
| 1167 | LVILatticeVal Result = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1168 | getCache(PImpl, AC, DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
| 1169 | |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1170 | if (Result.isConstant()) |
| 1171 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1172 | if (Result.isConstantRange()) { |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1173 | ConstantRange CR = Result.getConstantRange(); |
| 1174 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1175 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1176 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1177 | return nullptr; |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1180 | static LazyValueInfo::Tristate |
| 1181 | getPredicateResult(unsigned Pred, Constant *C, LVILatticeVal &Result, |
| 1182 | const DataLayout *DL, TargetLibraryInfo *TLI) { |
| 1183 | |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1184 | // If we know the value is a constant, evaluate the conditional. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1185 | Constant *Res = nullptr; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1186 | if (Result.isConstant()) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1187 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1188 | TLI); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1189 | if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1190 | return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True; |
| 1191 | return LazyValueInfo::Unknown; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1194 | if (Result.isConstantRange()) { |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1195 | ConstantInt *CI = dyn_cast<ConstantInt>(C); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1196 | if (!CI) return LazyValueInfo::Unknown; |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1197 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1198 | ConstantRange CR = Result.getConstantRange(); |
| 1199 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1200 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1201 | return LazyValueInfo::False; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1204 | return LazyValueInfo::True; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1205 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1206 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1207 | return LazyValueInfo::True; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1210 | return LazyValueInfo::False; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | // Handle more complex predicates. |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1214 | ConstantRange TrueValues = |
| 1215 | ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue()); |
| 1216 | if (TrueValues.contains(CR)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1217 | return LazyValueInfo::True; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1218 | if (TrueValues.inverse().contains(CR)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1219 | return LazyValueInfo::False; |
| 1220 | return LazyValueInfo::Unknown; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1223 | if (Result.isNotConstant()) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1224 | // If this is an equality comparison, we can try to fold it knowing that |
| 1225 | // "V != C1". |
| 1226 | if (Pred == ICmpInst::ICMP_EQ) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1227 | // !C1 == C -> false iff C1 == C. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1228 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1229 | Result.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1230 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1231 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1232 | return LazyValueInfo::False; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1233 | } else if (Pred == ICmpInst::ICMP_NE) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1234 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | b0c0a0d | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 1235 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1236 | Result.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1237 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1238 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1239 | return LazyValueInfo::True; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1240 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1241 | return LazyValueInfo::Unknown; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1244 | return LazyValueInfo::Unknown; |
| 1245 | } |
| 1246 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1247 | /// Determine whether the specified value comparison with a constant is known to |
| 1248 | /// 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] | 1249 | LazyValueInfo::Tristate |
| 1250 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 1251 | BasicBlock *FromBB, BasicBlock *ToBB, |
| 1252 | Instruction *CxtI) { |
| 1253 | LVILatticeVal Result = |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1254 | getCache(PImpl, AC, DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1255 | |
| 1256 | return getPredicateResult(Pred, C, Result, DL, TLI); |
| 1257 | } |
| 1258 | |
| 1259 | LazyValueInfo::Tristate |
| 1260 | LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C, |
| 1261 | Instruction *CxtI) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1262 | LVILatticeVal Result = getCache(PImpl, AC, DL, DT).getValueAt(V, CxtI); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1263 | |
| 1264 | return getPredicateResult(Pred, C, Result, DL, TLI); |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1267 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1268 | BasicBlock *NewSucc) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1269 | if (PImpl) |
| 1270 | getCache(PImpl, AC, DL, DT).threadEdge(PredBB, OldSucc, NewSucc); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1274 | if (PImpl) |
| 1275 | getCache(PImpl, AC, DL, DT).eraseBlock(BB); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1276 | } |