Chris Lattner | 10f2d13 | 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 | |
Chris Lattner | b8c124c | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "lazy-value-info" |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LazyValueInfo.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Instructions.h" |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ConstantFolding.h" |
| 22 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CFG.h" |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | b8c124c | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseMap.h" |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 6bcd3a0 | 2010-09-07 19:16:25 +0000 | [diff] [blame] | 31 | #include <map> |
| 32 | #include <set> |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 33 | #include <stack> |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
| 36 | char LazyValueInfo::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 37 | INITIALIZE_PASS(LazyValueInfo, "lazy-value-info", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 38 | "Lazy Value Information Analysis", false, true) |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 39 | |
| 40 | namespace llvm { |
| 41 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); } |
| 42 | } |
| 43 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 44 | |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | // LVILatticeVal |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | /// LVILatticeVal - This is the information tracked by LazyValueInfo for each |
| 50 | /// value. |
| 51 | /// |
| 52 | /// FIXME: This is basically just for bringup, this can be made a lot more rich |
| 53 | /// in the future. |
| 54 | /// |
| 55 | namespace { |
| 56 | class LVILatticeVal { |
| 57 | enum LatticeValueTy { |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 58 | /// undefined - This Value has no known value yet. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 59 | undefined, |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 60 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 61 | /// constant - This Value has a specific constant value. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 62 | constant, |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 63 | /// notconstant - This Value is known to not have the specified value. |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 64 | notconstant, |
| 65 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 66 | /// constantrange - The Value falls within this range. |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 67 | constantrange, |
| 68 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 69 | /// overdefined - This value is not known to be constant, and we know that |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 70 | /// it has a value. |
| 71 | overdefined |
| 72 | }; |
| 73 | |
| 74 | /// Val: This stores the current lattice value along with the Constant* for |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 75 | /// the constant if this is a 'constant' or 'notconstant' value. |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 76 | LatticeValueTy Tag; |
| 77 | Constant *Val; |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 78 | ConstantRange Range; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 79 | |
| 80 | public: |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 81 | LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {} |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 83 | static LVILatticeVal get(Constant *C) { |
| 84 | LVILatticeVal Res; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 85 | if (!isa<UndefValue>(C)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 86 | Res.markConstant(C); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 87 | return Res; |
| 88 | } |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 89 | static LVILatticeVal getNot(Constant *C) { |
| 90 | LVILatticeVal Res; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 91 | if (!isa<UndefValue>(C)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 92 | Res.markNotConstant(C); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 93 | return Res; |
| 94 | } |
Owen Anderson | 625051b | 2010-08-10 23:20:01 +0000 | [diff] [blame] | 95 | static LVILatticeVal getRange(ConstantRange CR) { |
| 96 | LVILatticeVal Res; |
| 97 | Res.markConstantRange(CR); |
| 98 | return Res; |
| 99 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 100 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 101 | bool isUndefined() const { return Tag == undefined; } |
| 102 | bool isConstant() const { return Tag == constant; } |
| 103 | bool isNotConstant() const { return Tag == notconstant; } |
| 104 | bool isConstantRange() const { return Tag == constantrange; } |
| 105 | bool isOverdefined() const { return Tag == overdefined; } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 106 | |
| 107 | Constant *getConstant() const { |
| 108 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 109 | return Val; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 112 | Constant *getNotConstant() const { |
| 113 | assert(isNotConstant() && "Cannot get the constant of a non-notconstant!"); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 114 | return Val; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 117 | ConstantRange getConstantRange() const { |
| 118 | assert(isConstantRange() && |
| 119 | "Cannot get the constant-range of a non-constant-range!"); |
| 120 | return Range; |
| 121 | } |
| 122 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 123 | /// markOverdefined - Return true if this is a change in status. |
| 124 | bool markOverdefined() { |
| 125 | if (isOverdefined()) |
| 126 | return false; |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 127 | Tag = overdefined; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
| 131 | /// markConstant - Return true if this is a change in status. |
| 132 | bool markConstant(Constant *V) { |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 133 | assert(V && "Marking constant with NULL"); |
| 134 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 135 | return markConstantRange(ConstantRange(CI->getValue())); |
| 136 | if (isa<UndefValue>(V)) |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 137 | return false; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 138 | |
| 139 | assert((!isConstant() || getConstant() == V) && |
| 140 | "Marking constant with different value"); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 141 | assert(isUndefined()); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 142 | Tag = constant; |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 143 | Val = V; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 147 | /// markNotConstant - Return true if this is a change in status. |
| 148 | bool markNotConstant(Constant *V) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 149 | assert(V && "Marking constant with NULL"); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 150 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 151 | return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue())); |
| 152 | if (isa<UndefValue>(V)) |
| 153 | return false; |
| 154 | |
| 155 | assert((!isConstant() || getConstant() != V) && |
| 156 | "Marking constant !constant with same value"); |
| 157 | assert((!isNotConstant() || getNotConstant() == V) && |
| 158 | "Marking !constant with different value"); |
| 159 | assert(isUndefined() || isConstant()); |
| 160 | Tag = notconstant; |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 161 | Val = V; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 162 | return true; |
| 163 | } |
| 164 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 165 | /// markConstantRange - Return true if this is a change in status. |
| 166 | bool markConstantRange(const ConstantRange NewR) { |
| 167 | if (isConstantRange()) { |
| 168 | if (NewR.isEmptySet()) |
| 169 | return markOverdefined(); |
| 170 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 171 | bool changed = Range == NewR; |
| 172 | Range = NewR; |
| 173 | return changed; |
| 174 | } |
| 175 | |
| 176 | assert(isUndefined()); |
| 177 | if (NewR.isEmptySet()) |
| 178 | return markOverdefined(); |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 179 | |
| 180 | Tag = constantrange; |
| 181 | Range = NewR; |
| 182 | return true; |
| 183 | } |
| 184 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 185 | /// mergeIn - Merge the specified lattice value into this one, updating this |
| 186 | /// one and returning true if anything changed. |
| 187 | bool mergeIn(const LVILatticeVal &RHS) { |
| 188 | if (RHS.isUndefined() || isOverdefined()) return false; |
| 189 | if (RHS.isOverdefined()) return markOverdefined(); |
| 190 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 191 | if (isUndefined()) { |
| 192 | Tag = RHS.Tag; |
| 193 | Val = RHS.Val; |
| 194 | Range = RHS.Range; |
| 195 | return true; |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 198 | if (isConstant()) { |
| 199 | if (RHS.isConstant()) { |
| 200 | if (Val == RHS.Val) |
| 201 | return false; |
| 202 | return markOverdefined(); |
| 203 | } |
| 204 | |
| 205 | if (RHS.isNotConstant()) { |
| 206 | if (Val == RHS.Val) |
| 207 | return markOverdefined(); |
| 208 | |
| 209 | // Unless we can prove that the two Constants are different, we must |
| 210 | // move to overdefined. |
| 211 | // FIXME: use TargetData for smarter constant folding. |
| 212 | if (ConstantInt *Res = dyn_cast<ConstantInt>( |
| 213 | ConstantFoldCompareInstOperands(CmpInst::ICMP_NE, |
| 214 | getConstant(), |
| 215 | RHS.getNotConstant()))) |
| 216 | if (Res->isOne()) |
| 217 | return markNotConstant(RHS.getNotConstant()); |
| 218 | |
| 219 | return markOverdefined(); |
| 220 | } |
| 221 | |
| 222 | // RHS is a ConstantRange, LHS is a non-integer Constant. |
| 223 | |
| 224 | // FIXME: consider the case where RHS is a range [1, 0) and LHS is |
| 225 | // a function. The correct result is to pick up RHS. |
| 226 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 227 | return markOverdefined(); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (isNotConstant()) { |
| 231 | if (RHS.isConstant()) { |
| 232 | if (Val == RHS.Val) |
| 233 | return markOverdefined(); |
| 234 | |
| 235 | // Unless we can prove that the two Constants are different, we must |
| 236 | // move to overdefined. |
| 237 | // FIXME: use TargetData for smarter constant folding. |
| 238 | if (ConstantInt *Res = dyn_cast<ConstantInt>( |
| 239 | ConstantFoldCompareInstOperands(CmpInst::ICMP_NE, |
| 240 | getNotConstant(), |
| 241 | RHS.getConstant()))) |
| 242 | if (Res->isOne()) |
| 243 | return false; |
| 244 | |
| 245 | return markOverdefined(); |
| 246 | } |
| 247 | |
| 248 | if (RHS.isNotConstant()) { |
| 249 | if (Val == RHS.Val) |
| 250 | return false; |
| 251 | return markOverdefined(); |
| 252 | } |
| 253 | |
| 254 | return markOverdefined(); |
| 255 | } |
| 256 | |
| 257 | assert(isConstantRange() && "New LVILattice type?"); |
| 258 | if (!RHS.isConstantRange()) |
| 259 | return markOverdefined(); |
| 260 | |
| 261 | ConstantRange NewR = Range.unionWith(RHS.getConstantRange()); |
| 262 | if (NewR.isFullSet()) |
| 263 | return markOverdefined(); |
| 264 | return markConstantRange(NewR); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 265 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 266 | }; |
| 267 | |
| 268 | } // end anonymous namespace. |
| 269 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 270 | namespace llvm { |
Chandler Carruth | 3b55a37 | 2011-04-18 18:49:44 +0000 | [diff] [blame^] | 271 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) |
| 272 | LLVM_ATTRIBUTE_USED; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 273 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) { |
| 274 | if (Val.isUndefined()) |
| 275 | return OS << "undefined"; |
| 276 | if (Val.isOverdefined()) |
| 277 | return OS << "overdefined"; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 278 | |
| 279 | if (Val.isNotConstant()) |
| 280 | return OS << "notconstant<" << *Val.getNotConstant() << '>'; |
Owen Anderson | 2f3ffb8 | 2010-08-09 20:50:46 +0000 | [diff] [blame] | 281 | else if (Val.isConstantRange()) |
| 282 | return OS << "constantrange<" << Val.getConstantRange().getLower() << ", " |
| 283 | << Val.getConstantRange().getUpper() << '>'; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 284 | return OS << "constant<" << *Val.getConstant() << '>'; |
| 285 | } |
| 286 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 287 | |
| 288 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 289 | // LazyValueInfoCache Decl |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 290 | //===----------------------------------------------------------------------===// |
| 291 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 292 | namespace { |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 293 | /// LVIValueHandle - A callback value handle update the cache when |
| 294 | /// values are erased. |
| 295 | class LazyValueInfoCache; |
| 296 | struct LVIValueHandle : public CallbackVH { |
| 297 | LazyValueInfoCache *Parent; |
| 298 | |
| 299 | LVIValueHandle(Value *V, LazyValueInfoCache *P) |
| 300 | : CallbackVH(V), Parent(P) { } |
| 301 | |
| 302 | void deleted(); |
| 303 | void allUsesReplacedWith(Value *V) { |
| 304 | deleted(); |
| 305 | } |
| 306 | }; |
| 307 | } |
| 308 | |
| 309 | namespace llvm { |
| 310 | template<> |
| 311 | struct DenseMapInfo<LVIValueHandle> { |
| 312 | typedef DenseMapInfo<Value*> PointerInfo; |
| 313 | static inline LVIValueHandle getEmptyKey() { |
| 314 | return LVIValueHandle(PointerInfo::getEmptyKey(), |
| 315 | static_cast<LazyValueInfoCache*>(0)); |
| 316 | } |
| 317 | static inline LVIValueHandle getTombstoneKey() { |
| 318 | return LVIValueHandle(PointerInfo::getTombstoneKey(), |
| 319 | static_cast<LazyValueInfoCache*>(0)); |
| 320 | } |
| 321 | static unsigned getHashValue(const LVIValueHandle &Val) { |
| 322 | return PointerInfo::getHashValue(Val); |
| 323 | } |
| 324 | static bool isEqual(const LVIValueHandle &LHS, const LVIValueHandle &RHS) { |
| 325 | return LHS == RHS; |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | template<> |
| 330 | struct DenseMapInfo<std::pair<AssertingVH<BasicBlock>, Value*> > { |
| 331 | typedef std::pair<AssertingVH<BasicBlock>, Value*> PairTy; |
| 332 | typedef DenseMapInfo<AssertingVH<BasicBlock> > APointerInfo; |
| 333 | typedef DenseMapInfo<Value*> BPointerInfo; |
| 334 | static inline PairTy getEmptyKey() { |
| 335 | return std::make_pair(APointerInfo::getEmptyKey(), |
| 336 | BPointerInfo::getEmptyKey()); |
| 337 | } |
| 338 | static inline PairTy getTombstoneKey() { |
| 339 | return std::make_pair(APointerInfo::getTombstoneKey(), |
| 340 | BPointerInfo::getTombstoneKey()); |
| 341 | } |
| 342 | static unsigned getHashValue( const PairTy &Val) { |
| 343 | return APointerInfo::getHashValue(Val.first) ^ |
| 344 | BPointerInfo::getHashValue(Val.second); |
| 345 | } |
| 346 | static bool isEqual(const PairTy &LHS, const PairTy &RHS) { |
| 347 | return APointerInfo::isEqual(LHS.first, RHS.first) && |
| 348 | BPointerInfo::isEqual(LHS.second, RHS.second); |
| 349 | } |
| 350 | }; |
| 351 | } |
| 352 | |
| 353 | namespace { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 354 | /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which |
| 355 | /// maintains information about queries across the clients' queries. |
| 356 | class LazyValueInfoCache { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 357 | /// ValueCacheEntryTy - This is all of the cached block information for |
| 358 | /// exactly one Value*. The entries are sorted by the BasicBlock* of the |
| 359 | /// entries, allowing us to do a lookup with a binary search. |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 360 | typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 361 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 362 | /// ValueCache - This is all of the cached information for all values, |
| 363 | /// mapped from Value* to key information. |
| 364 | DenseMap<LVIValueHandle, ValueCacheEntryTy> ValueCache; |
| 365 | |
| 366 | /// OverDefinedCache - This tracks, on a per-block basis, the set of |
| 367 | /// values that are over-defined at the end of that block. This is required |
| 368 | /// for cache updating. |
| 369 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 370 | DenseSet<OverDefinedPairTy> OverDefinedCache; |
| 371 | |
| 372 | /// BlockValueStack - This stack holds the state of the value solver |
| 373 | /// during a query. It basically emulates the callstack of the naive |
| 374 | /// recursive value lookup process. |
| 375 | std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack; |
| 376 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 377 | friend struct LVIValueHandle; |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 378 | |
| 379 | /// OverDefinedCacheUpdater - A helper object that ensures that the |
| 380 | /// OverDefinedCache is updated whenever solveBlockValue returns. |
| 381 | struct OverDefinedCacheUpdater { |
| 382 | LazyValueInfoCache *Parent; |
| 383 | Value *Val; |
| 384 | BasicBlock *BB; |
| 385 | LVILatticeVal &BBLV; |
| 386 | |
| 387 | OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV, |
| 388 | LazyValueInfoCache *P) |
| 389 | : Parent(P), Val(V), BB(B), BBLV(LV) { } |
| 390 | |
| 391 | bool markResult(bool changed) { |
| 392 | if (changed && BBLV.isOverdefined()) |
| 393 | Parent->OverDefinedCache.insert(std::make_pair(BB, Val)); |
| 394 | return changed; |
| 395 | } |
| 396 | }; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 397 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 398 | |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 399 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 400 | LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 401 | bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T, |
| 402 | LVILatticeVal &Result); |
| 403 | bool hasBlockValue(Value *Val, BasicBlock *BB); |
| 404 | |
| 405 | // These methods process one work item and may add more. A false value |
| 406 | // returned means that the work item was not completely processed and must |
| 407 | // be revisited after going through the new items. |
| 408 | bool solveBlockValue(Value *Val, BasicBlock *BB); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 409 | bool solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 410 | Value *Val, BasicBlock *BB); |
| 411 | bool solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 412 | PHINode *PN, BasicBlock *BB); |
| 413 | bool solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 414 | Instruction *BBI, BasicBlock *BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 415 | |
| 416 | void solve(); |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 417 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 418 | ValueCacheEntryTy &lookup(Value *V) { |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 419 | return ValueCache[LVIValueHandle(V, this)]; |
| 420 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 422 | public: |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 423 | /// getValueInBlock - This is the query interface to determine the lattice |
| 424 | /// value for the specified Value* at the end of the specified block. |
| 425 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB); |
| 426 | |
| 427 | /// getValueOnEdge - This is the query interface to determine the lattice |
| 428 | /// value for the specified Value* that is true on the specified edge. |
| 429 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 430 | |
| 431 | /// threadEdge - This is the update interface to inform the cache that an |
| 432 | /// edge from PredBB to OldSucc has been threaded to be from PredBB to |
| 433 | /// NewSucc. |
| 434 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 435 | |
| 436 | /// eraseBlock - This is part of the update interface to inform the cache |
| 437 | /// that a block has been deleted. |
| 438 | void eraseBlock(BasicBlock *BB); |
| 439 | |
| 440 | /// clear - Empty the cache. |
| 441 | void clear() { |
| 442 | ValueCache.clear(); |
| 443 | OverDefinedCache.clear(); |
| 444 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 445 | }; |
| 446 | } // end anonymous namespace |
| 447 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 448 | void LVIValueHandle::deleted() { |
| 449 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 450 | |
| 451 | SmallVector<OverDefinedPairTy, 4> ToErase; |
| 452 | for (DenseSet<OverDefinedPairTy>::iterator |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 453 | I = Parent->OverDefinedCache.begin(), |
| 454 | E = Parent->OverDefinedCache.end(); |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 455 | I != E; ++I) { |
| 456 | if (I->second == getValPtr()) |
| 457 | ToErase.push_back(*I); |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 458 | } |
Owen Anderson | cf6abd2 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 459 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 460 | for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(), |
| 461 | E = ToErase.end(); I != E; ++I) |
| 462 | Parent->OverDefinedCache.erase(*I); |
| 463 | |
Owen Anderson | cf6abd2 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 464 | // This erasure deallocates *this, so it MUST happen after we're done |
| 465 | // using any and all members of *this. |
| 466 | Parent->ValueCache.erase(*this); |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 469 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 470 | SmallVector<OverDefinedPairTy, 4> ToErase; |
| 471 | for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(), |
| 472 | E = OverDefinedCache.end(); I != E; ++I) { |
| 473 | if (I->first == BB) |
| 474 | ToErase.push_back(*I); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 475 | } |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 476 | |
| 477 | for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(), |
| 478 | E = ToErase.end(); I != E; ++I) |
| 479 | OverDefinedCache.erase(*I); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 480 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 481 | for (DenseMap<LVIValueHandle, ValueCacheEntryTy>::iterator |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 482 | I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I) |
| 483 | I->second.erase(BB); |
| 484 | } |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 485 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 486 | void LazyValueInfoCache::solve() { |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 487 | while (!BlockValueStack.empty()) { |
| 488 | std::pair<BasicBlock*, Value*> &e = BlockValueStack.top(); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 489 | if (solveBlockValue(e.second, e.first)) |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 490 | BlockValueStack.pop(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
| 494 | bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) { |
| 495 | // If already a constant, there is nothing to compute. |
| 496 | if (isa<Constant>(Val)) |
| 497 | return true; |
| 498 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 499 | LVIValueHandle ValHandle(Val, this); |
| 500 | if (!ValueCache.count(ValHandle)) return false; |
| 501 | return ValueCache[ValHandle].count(BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 504 | LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 505 | // If already a constant, there is nothing to compute. |
| 506 | if (Constant *VC = dyn_cast<Constant>(Val)) |
| 507 | return LVILatticeVal::get(VC); |
| 508 | |
| 509 | return lookup(Val)[BB]; |
| 510 | } |
| 511 | |
| 512 | bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) { |
| 513 | if (isa<Constant>(Val)) |
| 514 | return true; |
| 515 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 516 | ValueCacheEntryTy &Cache = lookup(Val); |
| 517 | LVILatticeVal &BBLV = Cache[BB]; |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 518 | |
| 519 | // OverDefinedCacheUpdater is a helper object that will update |
| 520 | // the OverDefinedCache for us when this method exits. Make sure to |
| 521 | // call markResult on it as we exist, passing a bool to indicate if the |
| 522 | // cache needs updating, i.e. if we have solve a new value or not. |
| 523 | OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV, this); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 525 | // If we've already computed this block's value, return it. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 526 | if (!BBLV.isUndefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 527 | DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n'); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 528 | |
| 529 | // Since we're reusing a cached value here, we don't need to update the |
| 530 | // OverDefinedCahce. The cache will have been properly updated |
| 531 | // whenever the cached value was inserted. |
| 532 | ODCacheUpdater.markResult(false); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 533 | return true; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 536 | // Otherwise, this is the first time we're seeing this block. Reset the |
| 537 | // lattice value to overdefined, so that cycles will terminate and be |
| 538 | // conservatively correct. |
| 539 | BBLV.markOverdefined(); |
| 540 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 541 | Instruction *BBI = dyn_cast<Instruction>(Val); |
| 542 | if (BBI == 0 || BBI->getParent() != BB) { |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 543 | return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB)); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 544 | } |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 545 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 546 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 547 | return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 548 | } |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 549 | |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 550 | if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) { |
| 551 | BBLV = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType())); |
| 552 | return ODCacheUpdater.markResult(true); |
| 553 | } |
| 554 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 555 | // We can only analyze the definitions of certain classes of instructions |
| 556 | // (integral binops and casts at the moment), so bail if this isn't one. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 557 | LVILatticeVal Result; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 558 | if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) || |
| 559 | !BBI->getType()->isIntegerTy()) { |
| 560 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 561 | << "' - overdefined because inst def found.\n"); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 562 | BBLV.markOverdefined(); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 563 | return ODCacheUpdater.markResult(true); |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 564 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 565 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 566 | // FIXME: We're currently limited to binops with a constant RHS. This should |
| 567 | // be improved. |
| 568 | BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI); |
| 569 | if (BO && !isa<ConstantInt>(BO->getOperand(1))) { |
| 570 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 571 | << "' - overdefined because inst def found.\n"); |
| 572 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 573 | BBLV.markOverdefined(); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 574 | return ODCacheUpdater.markResult(true); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 575 | } |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 576 | |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 577 | return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) { |
| 581 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 582 | return L->getPointerAddressSpace() == 0 && |
| 583 | GetUnderlyingObject(L->getPointerOperand()) == |
| 584 | GetUnderlyingObject(Ptr); |
| 585 | } |
| 586 | if (StoreInst *S = dyn_cast<StoreInst>(I)) { |
| 587 | return S->getPointerAddressSpace() == 0 && |
| 588 | GetUnderlyingObject(S->getPointerOperand()) == |
| 589 | GetUnderlyingObject(Ptr); |
| 590 | } |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 591 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 592 | if (MI->isVolatile()) return false; |
| 593 | if (MI->getAddressSpace() != 0) return false; |
| 594 | |
| 595 | // FIXME: check whether it has a valuerange that excludes zero? |
| 596 | ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 597 | if (!Len || Len->isZero()) return false; |
| 598 | |
| 599 | if (MI->getRawDest() == Ptr || MI->getDest() == Ptr) |
| 600 | return true; |
| 601 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
| 602 | return MTI->getRawSource() == Ptr || MTI->getSource() == Ptr; |
| 603 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 607 | bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 608 | Value *Val, BasicBlock *BB) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 609 | LVILatticeVal Result; // Start Undefined. |
| 610 | |
| 611 | // If this is a pointer, and there's a load from that pointer in this BB, |
| 612 | // then we know that the pointer can't be NULL. |
| 613 | bool NotNull = false; |
| 614 | if (Val->getType()->isPointerTy()) { |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 615 | if (isa<AllocaInst>(Val)) { |
| 616 | NotNull = true; |
| 617 | } else { |
| 618 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();BI != BE;++BI){ |
| 619 | if (InstructionDereferencesPointer(BI, Val)) { |
| 620 | NotNull = true; |
| 621 | break; |
| 622 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | // If this is the entry block, we must be asking about an argument. The |
| 628 | // value is overdefined. |
| 629 | if (BB == &BB->getParent()->getEntryBlock()) { |
| 630 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 631 | if (NotNull) { |
| 632 | const PointerType *PTy = cast<PointerType>(Val->getType()); |
| 633 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 634 | } else { |
| 635 | Result.markOverdefined(); |
| 636 | } |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 637 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 638 | return true; |
| 639 | } |
| 640 | |
| 641 | // Loop over all of our predecessors, merging what we know from them into |
| 642 | // result. |
| 643 | bool EdgesMissing = false; |
| 644 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 645 | LVILatticeVal EdgeResult; |
| 646 | EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult); |
| 647 | if (EdgesMissing) |
| 648 | continue; |
| 649 | |
| 650 | Result.mergeIn(EdgeResult); |
| 651 | |
| 652 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 653 | // to overdefined. |
| 654 | if (Result.isOverdefined()) { |
| 655 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 656 | << "' - overdefined because of pred.\n"); |
| 657 | // If we previously determined that this is a pointer that can't be null |
| 658 | // then return that rather than giving up entirely. |
| 659 | if (NotNull) { |
| 660 | const PointerType *PTy = cast<PointerType>(Val->getType()); |
| 661 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 662 | } |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 663 | |
| 664 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 665 | return true; |
| 666 | } |
| 667 | } |
| 668 | if (EdgesMissing) |
| 669 | return false; |
| 670 | |
| 671 | // Return the merged value, which is more precise than 'overdefined'. |
| 672 | assert(!Result.isOverdefined()); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 673 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 674 | return true; |
| 675 | } |
| 676 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 677 | bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 678 | PHINode *PN, BasicBlock *BB) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 679 | LVILatticeVal Result; // Start Undefined. |
| 680 | |
| 681 | // Loop over all of our predecessors, merging what we know from them into |
| 682 | // result. |
| 683 | bool EdgesMissing = false; |
| 684 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 685 | BasicBlock *PhiBB = PN->getIncomingBlock(i); |
| 686 | Value *PhiVal = PN->getIncomingValue(i); |
| 687 | LVILatticeVal EdgeResult; |
| 688 | EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult); |
| 689 | if (EdgesMissing) |
| 690 | continue; |
| 691 | |
| 692 | Result.mergeIn(EdgeResult); |
| 693 | |
| 694 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 695 | // to overdefined. |
| 696 | if (Result.isOverdefined()) { |
| 697 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 698 | << "' - overdefined because of pred.\n"); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 699 | |
| 700 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 701 | return true; |
| 702 | } |
| 703 | } |
| 704 | if (EdgesMissing) |
| 705 | return false; |
| 706 | |
| 707 | // Return the merged value, which is more precise than 'overdefined'. |
| 708 | assert(!Result.isOverdefined() && "Possible PHI in entry block?"); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 709 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 710 | return true; |
| 711 | } |
| 712 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 713 | bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 714 | Instruction *BBI, |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 715 | BasicBlock *BB) { |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 716 | // Figure out the range of the LHS. If that fails, bail. |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 717 | if (!hasBlockValue(BBI->getOperand(0), BB)) { |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 718 | BlockValueStack.push(std::make_pair(BB, BBI->getOperand(0))); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 719 | return false; |
| 720 | } |
| 721 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 722 | LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB); |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 723 | if (!LHSVal.isConstantRange()) { |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 724 | BBLV.markOverdefined(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 725 | return true; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 728 | ConstantRange LHSRange = LHSVal.getConstantRange(); |
| 729 | ConstantRange RHSRange(1); |
| 730 | const IntegerType *ResultTy = cast<IntegerType>(BBI->getType()); |
| 731 | if (isa<BinaryOperator>(BBI)) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 732 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) { |
| 733 | RHSRange = ConstantRange(RHS->getValue()); |
| 734 | } else { |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 735 | BBLV.markOverdefined(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 736 | return true; |
Owen Anderson | 59b06dc | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 737 | } |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 738 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 739 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 740 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 741 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 742 | // more definitions. |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 743 | LVILatticeVal Result; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 744 | switch (BBI->getOpcode()) { |
| 745 | case Instruction::Add: |
| 746 | Result.markConstantRange(LHSRange.add(RHSRange)); |
| 747 | break; |
| 748 | case Instruction::Sub: |
| 749 | Result.markConstantRange(LHSRange.sub(RHSRange)); |
| 750 | break; |
| 751 | case Instruction::Mul: |
| 752 | Result.markConstantRange(LHSRange.multiply(RHSRange)); |
| 753 | break; |
| 754 | case Instruction::UDiv: |
| 755 | Result.markConstantRange(LHSRange.udiv(RHSRange)); |
| 756 | break; |
| 757 | case Instruction::Shl: |
| 758 | Result.markConstantRange(LHSRange.shl(RHSRange)); |
| 759 | break; |
| 760 | case Instruction::LShr: |
| 761 | Result.markConstantRange(LHSRange.lshr(RHSRange)); |
| 762 | break; |
| 763 | case Instruction::Trunc: |
| 764 | Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth())); |
| 765 | break; |
| 766 | case Instruction::SExt: |
| 767 | Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth())); |
| 768 | break; |
| 769 | case Instruction::ZExt: |
| 770 | Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth())); |
| 771 | break; |
| 772 | case Instruction::BitCast: |
| 773 | Result.markConstantRange(LHSRange); |
| 774 | break; |
Nick Lewycky | 198381e | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 775 | case Instruction::And: |
| 776 | Result.markConstantRange(LHSRange.binaryAnd(RHSRange)); |
| 777 | break; |
| 778 | case Instruction::Or: |
| 779 | Result.markConstantRange(LHSRange.binaryOr(RHSRange)); |
| 780 | break; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 781 | |
| 782 | // Unhandled instructions are overdefined. |
| 783 | default: |
| 784 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 785 | << "' - overdefined because inst def found.\n"); |
| 786 | Result.markOverdefined(); |
| 787 | break; |
| 788 | } |
| 789 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 790 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 791 | return true; |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 794 | /// getEdgeValue - This method attempts to infer more complex |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 795 | bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom, |
| 796 | BasicBlock *BBTo, LVILatticeVal &Result) { |
| 797 | // If already a constant, there is nothing to compute. |
| 798 | if (Constant *VC = dyn_cast<Constant>(Val)) { |
| 799 | Result = LVILatticeVal::get(VC); |
| 800 | return true; |
| 801 | } |
| 802 | |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 803 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 804 | // know that v != 0. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 805 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 806 | // If this is a conditional branch and only one successor goes to BBTo, then |
| 807 | // we maybe able to infer something from the condition. |
| 808 | if (BI->isConditional() && |
| 809 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 810 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 811 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 812 | "BBTo isn't a successor of BBFrom"); |
| 813 | |
| 814 | // If V is the condition of the branch itself, then we know exactly what |
| 815 | // it is. |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 816 | if (BI->getCondition() == Val) { |
| 817 | Result = LVILatticeVal::get(ConstantInt::get( |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 818 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 819 | return true; |
| 820 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 821 | |
| 822 | // If the condition of the branch is an equality comparison, we may be |
| 823 | // able to infer the value. |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 824 | ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()); |
| 825 | if (ICI && ICI->getOperand(0) == Val && |
| 826 | isa<Constant>(ICI->getOperand(1))) { |
| 827 | if (ICI->isEquality()) { |
| 828 | // We know that V has the RHS constant if this is a true SETEQ or |
| 829 | // false SETNE. |
| 830 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 831 | Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
| 832 | else |
| 833 | Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
| 834 | return true; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 835 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 836 | |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 837 | if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 838 | // Calculate the range of values that would satisfy the comparison. |
| 839 | ConstantRange CmpRange(CI->getValue(), CI->getValue()+1); |
| 840 | ConstantRange TrueValues = |
| 841 | ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 842 | |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 843 | // If we're interested in the false dest, invert the condition. |
| 844 | if (!isTrueDest) TrueValues = TrueValues.inverse(); |
| 845 | |
| 846 | // Figure out the possible values of the query BEFORE this branch. |
Owen Anderson | be41901 | 2011-01-05 21:37:18 +0000 | [diff] [blame] | 847 | if (!hasBlockValue(Val, BBFrom)) { |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 848 | BlockValueStack.push(std::make_pair(BBFrom, Val)); |
Owen Anderson | be41901 | 2011-01-05 21:37:18 +0000 | [diff] [blame] | 849 | return false; |
| 850 | } |
| 851 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 852 | LVILatticeVal InBlock = getBlockValue(Val, BBFrom); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 853 | if (!InBlock.isConstantRange()) { |
| 854 | Result = LVILatticeVal::getRange(TrueValues); |
| 855 | return true; |
| 856 | } |
| 857 | |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 858 | // Find all potential values that satisfy both the input and output |
| 859 | // conditions. |
| 860 | ConstantRange PossibleValues = |
| 861 | TrueValues.intersectWith(InBlock.getConstantRange()); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 862 | |
| 863 | Result = LVILatticeVal::getRange(PossibleValues); |
| 864 | return true; |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 869 | |
| 870 | // If the edge was formed by a switch on the value, then we may know exactly |
| 871 | // what it is. |
| 872 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
Owen Anderson | dae90c6 | 2010-08-24 21:59:42 +0000 | [diff] [blame] | 873 | if (SI->getCondition() == Val) { |
Owen Anderson | 4caef60 | 2010-09-02 22:16:52 +0000 | [diff] [blame] | 874 | // We don't know anything in the default case. |
Owen Anderson | dae90c6 | 2010-08-24 21:59:42 +0000 | [diff] [blame] | 875 | if (SI->getDefaultDest() == BBTo) { |
Owen Anderson | 4caef60 | 2010-09-02 22:16:52 +0000 | [diff] [blame] | 876 | Result.markOverdefined(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 877 | return true; |
Owen Anderson | dae90c6 | 2010-08-24 21:59:42 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 880 | // We only know something if there is exactly one value that goes from |
| 881 | // BBFrom to BBTo. |
| 882 | unsigned NumEdges = 0; |
| 883 | ConstantInt *EdgeVal = 0; |
| 884 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 885 | if (SI->getSuccessor(i) != BBTo) continue; |
| 886 | if (NumEdges++) break; |
| 887 | EdgeVal = SI->getCaseValue(i); |
| 888 | } |
| 889 | assert(EdgeVal && "Missing successor?"); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 890 | if (NumEdges == 1) { |
| 891 | Result = LVILatticeVal::get(EdgeVal); |
| 892 | return true; |
| 893 | } |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 894 | } |
| 895 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 896 | |
| 897 | // Otherwise see if the value is known in the block. |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 898 | if (hasBlockValue(Val, BBFrom)) { |
| 899 | Result = getBlockValue(Val, BBFrom); |
| 900 | return true; |
| 901 | } |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 902 | BlockValueStack.push(std::make_pair(BBFrom, Val)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 903 | return false; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 906 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 907 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 908 | << BB->getName() << "'\n"); |
| 909 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 910 | BlockValueStack.push(std::make_pair(BB, V)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 911 | solve(); |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 912 | LVILatticeVal Result = getBlockValue(V, BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 913 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 914 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 915 | return Result; |
| 916 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 918 | LVILatticeVal LazyValueInfoCache:: |
| 919 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 920 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 921 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 922 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 923 | LVILatticeVal Result; |
| 924 | if (!getEdgeValue(V, FromBB, ToBB, Result)) { |
| 925 | solve(); |
| 926 | bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result); |
| 927 | (void)WasFastQuery; |
| 928 | assert(WasFastQuery && "More work to do after problem solved?"); |
| 929 | } |
| 930 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 931 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 932 | return Result; |
| 933 | } |
| 934 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 935 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 936 | BasicBlock *NewSucc) { |
| 937 | // When an edge in the graph has been threaded, values that we could not |
| 938 | // determine a value for before (i.e. were marked overdefined) may be possible |
| 939 | // to solve now. We do NOT try to proactively update these values. Instead, |
| 940 | // we clear their entries from the cache, and allow lazy updating to recompute |
| 941 | // them when needed. |
| 942 | |
| 943 | // The updating process is fairly simple: we need to dropped cached info |
| 944 | // for all values that were marked overdefined in OldSucc, and for those same |
| 945 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 946 | // also marked overdefined. |
| 947 | std::vector<BasicBlock*> worklist; |
| 948 | worklist.push_back(OldSucc); |
| 949 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 950 | DenseSet<Value*> ClearSet; |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 951 | for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(), |
| 952 | E = OverDefinedCache.end(); I != E; ++I) { |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 953 | if (I->first == OldSucc) |
| 954 | ClearSet.insert(I->second); |
| 955 | } |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 956 | |
| 957 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 958 | // NOTE: We do not need a visited list since any blocks we have already |
| 959 | // visited will have had their overdefined markers cleared already, and we |
| 960 | // thus won't loop to their successors. |
| 961 | while (!worklist.empty()) { |
| 962 | BasicBlock *ToUpdate = worklist.back(); |
| 963 | worklist.pop_back(); |
| 964 | |
| 965 | // Skip blocks only accessible through NewSucc. |
| 966 | if (ToUpdate == NewSucc) continue; |
| 967 | |
| 968 | bool changed = false; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 969 | for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end(); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 970 | I != E; ++I) { |
| 971 | // If a value was marked overdefined in OldSucc, and is here too... |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 972 | DenseSet<OverDefinedPairTy>::iterator OI = |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 973 | OverDefinedCache.find(std::make_pair(ToUpdate, *I)); |
| 974 | if (OI == OverDefinedCache.end()) continue; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 975 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 976 | // Remove it from the caches. |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 977 | ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)]; |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 978 | ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 979 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 980 | assert(CI != Entry.end() && "Couldn't find entry to update?"); |
| 981 | Entry.erase(CI); |
| 982 | OverDefinedCache.erase(OI); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 983 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 984 | // If we removed anything, then we potentially need to update |
| 985 | // blocks successors too. |
| 986 | changed = true; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 987 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 988 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 989 | if (!changed) continue; |
| 990 | |
| 991 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 992 | } |
| 993 | } |
| 994 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 995 | //===----------------------------------------------------------------------===// |
| 996 | // LazyValueInfo Impl |
| 997 | //===----------------------------------------------------------------------===// |
| 998 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 999 | /// getCache - This lazily constructs the LazyValueInfoCache. |
| 1000 | static LazyValueInfoCache &getCache(void *&PImpl) { |
| 1001 | if (!PImpl) |
| 1002 | PImpl = new LazyValueInfoCache(); |
| 1003 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 1004 | } |
| 1005 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1006 | bool LazyValueInfo::runOnFunction(Function &F) { |
| 1007 | if (PImpl) |
| 1008 | getCache(PImpl).clear(); |
| 1009 | |
| 1010 | TD = getAnalysisIfAvailable<TargetData>(); |
| 1011 | // Fully lazy. |
| 1012 | return false; |
| 1013 | } |
| 1014 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1015 | void LazyValueInfo::releaseMemory() { |
| 1016 | // If the cache was allocated, free it. |
| 1017 | if (PImpl) { |
| 1018 | delete &getCache(PImpl); |
| 1019 | PImpl = 0; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) { |
| 1024 | LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB); |
| 1025 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1026 | if (Result.isConstant()) |
| 1027 | return Result.getConstant(); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1028 | if (Result.isConstantRange()) { |
Owen Anderson | ee61fcf | 2010-08-27 23:29:38 +0000 | [diff] [blame] | 1029 | ConstantRange CR = Result.getConstantRange(); |
| 1030 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1031 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1032 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1033 | return 0; |
| 1034 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1035 | |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1036 | /// getConstantOnEdge - Determine whether the specified value is known to be a |
| 1037 | /// constant on the specified edge. Return null if not. |
| 1038 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
| 1039 | BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1040 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1041 | |
| 1042 | if (Result.isConstant()) |
| 1043 | return Result.getConstant(); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1044 | if (Result.isConstantRange()) { |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1045 | ConstantRange CR = Result.getConstantRange(); |
| 1046 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1047 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1048 | } |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
| 1051 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1052 | /// getPredicateOnEdge - Determine whether the specified value comparison |
| 1053 | /// with a constant is known to be true or false on the specified CFG edge. |
| 1054 | /// Pred is a CmpInst predicate. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1055 | LazyValueInfo::Tristate |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1056 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 1057 | BasicBlock *FromBB, BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1058 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1060 | // If we know the value is a constant, evaluate the conditional. |
| 1061 | Constant *Res = 0; |
| 1062 | if (Result.isConstant()) { |
| 1063 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1064 | if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res)) |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1065 | return ResCI->isZero() ? False : True; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1066 | return Unknown; |
| 1067 | } |
| 1068 | |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1069 | if (Result.isConstantRange()) { |
Owen Anderson | 59b06dc | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1070 | ConstantInt *CI = dyn_cast<ConstantInt>(C); |
| 1071 | if (!CI) return Unknown; |
| 1072 | |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1073 | ConstantRange CR = Result.getConstantRange(); |
| 1074 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1075 | if (!CR.contains(CI->getValue())) |
| 1076 | return False; |
| 1077 | |
| 1078 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
| 1079 | return True; |
| 1080 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1081 | if (!CR.contains(CI->getValue())) |
| 1082 | return True; |
| 1083 | |
| 1084 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
| 1085 | return False; |
| 1086 | } |
| 1087 | |
| 1088 | // Handle more complex predicates. |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1089 | ConstantRange TrueValues = |
| 1090 | ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue()); |
| 1091 | if (TrueValues.contains(CR)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1092 | return True; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1093 | if (TrueValues.inverse().contains(CR)) |
| 1094 | return False; |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1095 | return Unknown; |
| 1096 | } |
| 1097 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1098 | if (Result.isNotConstant()) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1099 | // If this is an equality comparison, we can try to fold it knowing that |
| 1100 | // "V != C1". |
| 1101 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1102 | // !C1 == C -> false iff C1 == C. |
| 1103 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
| 1104 | Result.getNotConstant(), C, TD); |
| 1105 | if (Res->isNullValue()) |
| 1106 | return False; |
| 1107 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1108 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | 5553a3a | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 1109 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1110 | Result.getNotConstant(), C, TD); |
| 1111 | if (Res->isNullValue()) |
| 1112 | return True; |
| 1113 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1114 | return Unknown; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1117 | return Unknown; |
| 1118 | } |
| 1119 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1120 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1121 | BasicBlock *NewSucc) { |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1122 | if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc); |
| 1123 | } |
| 1124 | |
| 1125 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
| 1126 | if (PImpl) getCache(PImpl).eraseBlock(BB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1127 | } |