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