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