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