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" |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Instructions.h" |
| 19 | #include "llvm/Analysis/ConstantFolding.h" |
| 20 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CFG.h" |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | b8c124c | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseMap.h" |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/DenseSet.h" |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | char LazyValueInfo::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 32 | INITIALIZE_PASS(LazyValueInfo, "lazy-value-info", |
| 33 | "Lazy Value Information Analysis", false, true); |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 34 | |
| 35 | namespace llvm { |
| 36 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); } |
| 37 | } |
| 38 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // LVILatticeVal |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | /// LVILatticeVal - This is the information tracked by LazyValueInfo for each |
| 45 | /// value. |
| 46 | /// |
| 47 | /// FIXME: This is basically just for bringup, this can be made a lot more rich |
| 48 | /// in the future. |
| 49 | /// |
| 50 | namespace { |
| 51 | class LVILatticeVal { |
| 52 | enum LatticeValueTy { |
| 53 | /// undefined - This LLVM Value has no known value yet. |
| 54 | undefined, |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 55 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 56 | /// constant - This LLVM Value has a specific constant value. |
| 57 | constant, |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 58 | /// notconstant - This LLVM value is known to not have the specified value. |
| 59 | notconstant, |
| 60 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 61 | /// constantrange |
| 62 | constantrange, |
| 63 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 64 | /// overdefined - This instruction is not known to be constant, and we know |
| 65 | /// it has a value. |
| 66 | overdefined |
| 67 | }; |
| 68 | |
| 69 | /// Val: This stores the current lattice value along with the Constant* for |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 70 | /// the constant if this is a 'constant' or 'notconstant' value. |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 71 | LatticeValueTy Tag; |
| 72 | Constant *Val; |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 73 | ConstantRange Range; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 74 | |
| 75 | public: |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 76 | LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {} |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 78 | static LVILatticeVal get(Constant *C) { |
| 79 | LVILatticeVal Res; |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 80 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 81 | Res.markConstantRange(ConstantRange(CI->getValue(), CI->getValue()+1)); |
| 82 | else if (!isa<UndefValue>(C)) |
| 83 | Res.markConstant(C); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 84 | return Res; |
| 85 | } |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 86 | static LVILatticeVal getNot(Constant *C) { |
| 87 | LVILatticeVal Res; |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 88 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) |
| 89 | Res.markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue())); |
| 90 | else |
| 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) { |
| 132 | if (isConstant()) { |
| 133 | assert(getConstant() == V && "Marking constant with different value"); |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | assert(isUndefined()); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 138 | Tag = constant; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 139 | assert(V && "Marking constant with NULL"); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 140 | Val = V; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 144 | /// markNotConstant - Return true if this is a change in status. |
| 145 | bool markNotConstant(Constant *V) { |
| 146 | if (isNotConstant()) { |
| 147 | assert(getNotConstant() == V && "Marking !constant with different value"); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | if (isConstant()) |
| 152 | assert(getConstant() != V && "Marking not constant with different value"); |
| 153 | else |
| 154 | assert(isUndefined()); |
| 155 | |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 156 | Tag = notconstant; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 157 | assert(V && "Marking constant with NULL"); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 158 | Val = V; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 162 | /// markConstantRange - Return true if this is a change in status. |
| 163 | bool markConstantRange(const ConstantRange NewR) { |
| 164 | if (isConstantRange()) { |
| 165 | if (NewR.isEmptySet()) |
| 166 | return markOverdefined(); |
| 167 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 168 | bool changed = Range == NewR; |
| 169 | Range = NewR; |
| 170 | return changed; |
| 171 | } |
| 172 | |
| 173 | assert(isUndefined()); |
| 174 | if (NewR.isEmptySet()) |
| 175 | return markOverdefined(); |
| 176 | else if (NewR.isFullSet()) { |
| 177 | Tag = undefined; |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | Tag = constantrange; |
| 182 | Range = NewR; |
| 183 | return true; |
| 184 | } |
| 185 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 186 | /// mergeIn - Merge the specified lattice value into this one, updating this |
| 187 | /// one and returning true if anything changed. |
| 188 | bool mergeIn(const LVILatticeVal &RHS) { |
| 189 | if (RHS.isUndefined() || isOverdefined()) return false; |
| 190 | if (RHS.isOverdefined()) return markOverdefined(); |
| 191 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 192 | if (RHS.isNotConstant()) { |
| 193 | if (isNotConstant()) { |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 194 | if (getNotConstant() != RHS.getNotConstant() || |
| 195 | isa<ConstantExpr>(getNotConstant()) || |
| 196 | isa<ConstantExpr>(RHS.getNotConstant())) |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 197 | return markOverdefined(); |
| 198 | return false; |
| 199 | } |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 200 | if (isConstant()) { |
| 201 | if (getConstant() == RHS.getNotConstant() || |
| 202 | isa<ConstantExpr>(RHS.getNotConstant()) || |
| 203 | isa<ConstantExpr>(getConstant())) |
| 204 | return markOverdefined(); |
| 205 | return markNotConstant(RHS.getNotConstant()); |
| 206 | } |
| 207 | |
| 208 | assert(isUndefined() && "Unexpected lattice"); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 209 | return markNotConstant(RHS.getNotConstant()); |
| 210 | } |
| 211 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 212 | if (RHS.isConstantRange()) { |
| 213 | if (isConstantRange()) { |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 214 | ConstantRange NewR = Range.unionWith(RHS.getConstantRange()); |
| 215 | if (NewR.isFullSet()) |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 216 | return markOverdefined(); |
| 217 | else |
| 218 | return markConstantRange(NewR); |
| 219 | } |
| 220 | |
| 221 | assert(isUndefined() && "Unexpected lattice"); |
| 222 | return markConstantRange(RHS.getConstantRange()); |
| 223 | } |
| 224 | |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 225 | // RHS must be a constant, we must be undef, constant, or notconstant. |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 226 | assert(!isConstantRange() && |
| 227 | "Constant and ConstantRange cannot be merged."); |
| 228 | |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 229 | if (isUndefined()) |
| 230 | return markConstant(RHS.getConstant()); |
| 231 | |
| 232 | if (isConstant()) { |
| 233 | if (getConstant() != RHS.getConstant()) |
| 234 | return markOverdefined(); |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // If we are known "!=4" and RHS is "==5", stay at "!=4". |
| 239 | if (getNotConstant() == RHS.getConstant() || |
| 240 | isa<ConstantExpr>(getNotConstant()) || |
| 241 | isa<ConstantExpr>(RHS.getConstant())) |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 242 | return markOverdefined(); |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 243 | return false; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | }; |
| 247 | |
| 248 | } // end anonymous namespace. |
| 249 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 250 | namespace llvm { |
| 251 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) { |
| 252 | if (Val.isUndefined()) |
| 253 | return OS << "undefined"; |
| 254 | if (Val.isOverdefined()) |
| 255 | return OS << "overdefined"; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 256 | |
| 257 | if (Val.isNotConstant()) |
| 258 | return OS << "notconstant<" << *Val.getNotConstant() << '>'; |
Owen Anderson | 2f3ffb8 | 2010-08-09 20:50:46 +0000 | [diff] [blame] | 259 | else if (Val.isConstantRange()) |
| 260 | return OS << "constantrange<" << Val.getConstantRange().getLower() << ", " |
| 261 | << Val.getConstantRange().getUpper() << '>'; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 262 | return OS << "constant<" << *Val.getConstant() << '>'; |
| 263 | } |
| 264 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 265 | |
| 266 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 267 | // LazyValueInfoCache Decl |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 270 | namespace { |
| 271 | /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which |
| 272 | /// maintains information about queries across the clients' queries. |
| 273 | class LazyValueInfoCache { |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 274 | public: |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 275 | /// BlockCacheEntryTy - This is a computed lattice value at the end of the |
| 276 | /// specified basic block for a Value* that depends on context. |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 277 | typedef std::pair<AssertingVH<BasicBlock>, LVILatticeVal> BlockCacheEntryTy; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 278 | |
| 279 | /// ValueCacheEntryTy - This is all of the cached block information for |
| 280 | /// exactly one Value*. The entries are sorted by the BasicBlock* of the |
| 281 | /// entries, allowing us to do a lookup with a binary search. |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 282 | typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 283 | |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 284 | private: |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 285 | /// LVIValueHandle - A callback value handle update the cache when |
| 286 | /// values are erased. |
| 287 | struct LVIValueHandle : public CallbackVH { |
| 288 | LazyValueInfoCache *Parent; |
| 289 | |
| 290 | LVIValueHandle(Value *V, LazyValueInfoCache *P) |
| 291 | : CallbackVH(V), Parent(P) { } |
| 292 | |
| 293 | void deleted(); |
| 294 | void allUsesReplacedWith(Value* V) { |
| 295 | deleted(); |
| 296 | } |
| 297 | |
| 298 | LVIValueHandle &operator=(Value *V) { |
| 299 | return *this = LVIValueHandle(V, Parent); |
| 300 | } |
| 301 | }; |
| 302 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 303 | /// ValueCache - This is all of the cached information for all values, |
| 304 | /// mapped from Value* to key information. |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 305 | std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 306 | |
| 307 | /// OverDefinedCache - This tracks, on a per-block basis, the set of |
| 308 | /// values that are over-defined at the end of that block. This is required |
| 309 | /// for cache updating. |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 310 | std::set<std::pair<AssertingVH<BasicBlock>, Value*> > OverDefinedCache; |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 312 | public: |
| 313 | |
| 314 | /// getValueInBlock - This is the query interface to determine the lattice |
| 315 | /// value for the specified Value* at the end of the specified block. |
| 316 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB); |
| 317 | |
| 318 | /// getValueOnEdge - This is the query interface to determine the lattice |
| 319 | /// value for the specified Value* that is true on the specified edge. |
| 320 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 321 | |
| 322 | /// threadEdge - This is the update interface to inform the cache that an |
| 323 | /// edge from PredBB to OldSucc has been threaded to be from PredBB to |
| 324 | /// NewSucc. |
| 325 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 326 | |
| 327 | /// eraseBlock - This is part of the update interface to inform the cache |
| 328 | /// that a block has been deleted. |
| 329 | void eraseBlock(BasicBlock *BB); |
| 330 | |
| 331 | /// clear - Empty the cache. |
| 332 | void clear() { |
| 333 | ValueCache.clear(); |
| 334 | OverDefinedCache.clear(); |
| 335 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 336 | }; |
| 337 | } // end anonymous namespace |
| 338 | |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 339 | //===----------------------------------------------------------------------===// |
| 340 | // LVIQuery Impl |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
| 343 | namespace { |
| 344 | /// LVIQuery - This is a transient object that exists while a query is |
| 345 | /// being performed. |
| 346 | /// |
| 347 | /// TODO: Reuse LVIQuery instead of recreating it for every query, this avoids |
| 348 | /// reallocation of the densemap on every query. |
| 349 | class LVIQuery { |
| 350 | typedef LazyValueInfoCache::BlockCacheEntryTy BlockCacheEntryTy; |
| 351 | typedef LazyValueInfoCache::ValueCacheEntryTy ValueCacheEntryTy; |
| 352 | |
| 353 | /// This is the current value being queried for. |
| 354 | Value *Val; |
| 355 | |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 356 | /// This is a pointer to the owning cache, for recursive queries. |
| 357 | LazyValueInfoCache &Parent; |
| 358 | |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 359 | /// This is all of the cached information about this value. |
| 360 | ValueCacheEntryTy &Cache; |
| 361 | |
| 362 | /// This tracks, for each block, what values are overdefined. |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 363 | std::set<std::pair<AssertingVH<BasicBlock>, Value*> > &OverDefinedCache; |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 364 | |
| 365 | /// NewBlocks - This is a mapping of the new BasicBlocks which have been |
| 366 | /// added to cache but that are not in sorted order. |
| 367 | DenseSet<BasicBlock*> NewBlockInfo; |
| 368 | public: |
| 369 | |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 370 | LVIQuery(Value *V, LazyValueInfoCache &P, |
| 371 | ValueCacheEntryTy &VC, |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 372 | std::set<std::pair<AssertingVH<BasicBlock>, Value*> > &ODC) |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 373 | : Val(V), Parent(P), Cache(VC), OverDefinedCache(ODC) { |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | ~LVIQuery() { |
| 377 | // When the query is done, insert the newly discovered facts into the |
| 378 | // cache in sorted order. |
| 379 | if (NewBlockInfo.empty()) return; |
| 380 | |
| 381 | for (DenseSet<BasicBlock*>::iterator I = NewBlockInfo.begin(), |
| 382 | E = NewBlockInfo.end(); I != E; ++I) { |
| 383 | if (Cache[*I].isOverdefined()) |
| 384 | OverDefinedCache.insert(std::make_pair(*I, Val)); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | LVILatticeVal getBlockValue(BasicBlock *BB); |
| 389 | LVILatticeVal getEdgeValue(BasicBlock *FromBB, BasicBlock *ToBB); |
| 390 | |
| 391 | private: |
Owen Anderson | 4bb3eaf | 2010-08-16 23:42:33 +0000 | [diff] [blame] | 392 | LVILatticeVal getCachedEntryForBlock(BasicBlock *BB); |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 393 | }; |
| 394 | } // end anonymous namespace |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 395 | |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 396 | void LazyValueInfoCache::LVIValueHandle::deleted() { |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 397 | for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 398 | I = Parent->OverDefinedCache.begin(), |
| 399 | E = Parent->OverDefinedCache.end(); |
| 400 | I != E; ) { |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 401 | std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I; |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 402 | ++I; |
| 403 | if (tmp->second == getValPtr()) |
| 404 | Parent->OverDefinedCache.erase(tmp); |
| 405 | } |
Owen Anderson | cf6abd2 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 406 | |
| 407 | // This erasure deallocates *this, so it MUST happen after we're done |
| 408 | // using any and all members of *this. |
| 409 | Parent->ValueCache.erase(*this); |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 412 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
| 413 | for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator |
| 414 | I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ) { |
| 415 | std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator tmp = I; |
| 416 | ++I; |
| 417 | if (tmp->first == BB) |
| 418 | OverDefinedCache.erase(tmp); |
| 419 | } |
| 420 | |
| 421 | for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator |
| 422 | I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I) |
| 423 | I->second.erase(BB); |
| 424 | } |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 425 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 426 | /// getCachedEntryForBlock - See if we already have a value for this block. If |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 427 | /// so, return it, otherwise create a new entry in the Cache map to use. |
Owen Anderson | 4bb3eaf | 2010-08-16 23:42:33 +0000 | [diff] [blame] | 428 | LVILatticeVal LVIQuery::getCachedEntryForBlock(BasicBlock *BB) { |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 429 | NewBlockInfo.insert(BB); |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 430 | return Cache[BB]; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 431 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 432 | |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 433 | LVILatticeVal LVIQuery::getBlockValue(BasicBlock *BB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 434 | // See if we already have a value for this block. |
Owen Anderson | 4bb3eaf | 2010-08-16 23:42:33 +0000 | [diff] [blame] | 435 | LVILatticeVal BBLV = getCachedEntryForBlock(BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 436 | |
| 437 | // If we've already computed this block's value, return it. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 438 | if (!BBLV.isUndefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 439 | DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n'); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 440 | return BBLV; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 443 | // Otherwise, this is the first time we're seeing this block. Reset the |
| 444 | // lattice value to overdefined, so that cycles will terminate and be |
| 445 | // conservatively correct. |
| 446 | BBLV.markOverdefined(); |
Owen Anderson | 4bb3eaf | 2010-08-16 23:42:33 +0000 | [diff] [blame] | 447 | Cache[BB] = BBLV; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 448 | |
| 449 | // If V is live into BB, see if our predecessors know anything about it. |
| 450 | Instruction *BBI = dyn_cast<Instruction>(Val); |
| 451 | if (BBI == 0 || BBI->getParent() != BB) { |
| 452 | LVILatticeVal Result; // Start Undefined. |
| 453 | unsigned NumPreds = 0; |
| 454 | |
| 455 | // Loop over all of our predecessors, merging what we know from them into |
| 456 | // result. |
| 457 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 458 | Result.mergeIn(getEdgeValue(*PI, BB)); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 459 | |
| 460 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 461 | // to overdefined. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 462 | if (Result.isOverdefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 463 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 464 | << "' - overdefined because of pred.\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 465 | return Result; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 466 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 467 | ++NumPreds; |
| 468 | } |
| 469 | |
| 470 | // If this is the entry block, we must be asking about an argument. The |
| 471 | // value is overdefined. |
| 472 | if (NumPreds == 0 && BB == &BB->getParent()->front()) { |
| 473 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 474 | Result.markOverdefined(); |
| 475 | return Result; |
| 476 | } |
| 477 | |
| 478 | // Return the merged value, which is more precise than 'overdefined'. |
| 479 | assert(!Result.isOverdefined()); |
Owen Anderson | 4bb3eaf | 2010-08-16 23:42:33 +0000 | [diff] [blame] | 480 | return Cache[BB] = Result; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // If this value is defined by an instruction in this block, we have to |
| 484 | // process it here somehow or return overdefined. |
| 485 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 486 | LVILatticeVal Result; // Start Undefined. |
| 487 | |
| 488 | // Loop over all of our predecessors, merging what we know from them into |
| 489 | // result. |
| 490 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 491 | Value* PhiVal = PN->getIncomingValueForBlock(*PI); |
| 492 | Result.mergeIn(Parent.getValueOnEdge(PhiVal, *PI, BB)); |
| 493 | |
| 494 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 495 | // to overdefined. |
| 496 | if (Result.isOverdefined()) { |
| 497 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 498 | << "' - overdefined because of pred.\n"); |
| 499 | return Result; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Return the merged value, which is more precise than 'overdefined'. |
| 504 | assert(!Result.isOverdefined()); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 505 | return Cache[BB] = Result; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 506 | } else { |
| 507 | |
| 508 | } |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 510 | LVILatticeVal Result; |
| 511 | Result.markOverdefined(); |
Owen Anderson | 4bb3eaf | 2010-08-16 23:42:33 +0000 | [diff] [blame] | 512 | return Result; |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 515 | |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 516 | /// getEdgeValue - This method attempts to infer more complex |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 517 | LVILatticeVal LVIQuery::getEdgeValue(BasicBlock *BBFrom, BasicBlock *BBTo) { |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 518 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 519 | // know that v != 0. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 520 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 521 | // If this is a conditional branch and only one successor goes to BBTo, then |
| 522 | // we maybe able to infer something from the condition. |
| 523 | if (BI->isConditional() && |
| 524 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 525 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 526 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 527 | "BBTo isn't a successor of BBFrom"); |
| 528 | |
| 529 | // If V is the condition of the branch itself, then we know exactly what |
| 530 | // it is. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 531 | if (BI->getCondition() == Val) |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 532 | return LVILatticeVal::get(ConstantInt::get( |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 533 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 534 | |
| 535 | // If the condition of the branch is an equality comparison, we may be |
| 536 | // able to infer the value. |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 537 | ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()); |
| 538 | if (ICI && ICI->getOperand(0) == Val && |
| 539 | isa<Constant>(ICI->getOperand(1))) { |
| 540 | if (ICI->isEquality()) { |
| 541 | // We know that V has the RHS constant if this is a true SETEQ or |
| 542 | // false SETNE. |
| 543 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
| 544 | return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
| 545 | return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 546 | } |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 547 | |
| 548 | if (ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 549 | // Calculate the range of values that would satisfy the comparison. |
| 550 | ConstantRange CmpRange(CI->getValue(), CI->getValue()+1); |
| 551 | ConstantRange TrueValues = |
| 552 | ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange); |
| 553 | |
| 554 | // If we're interested in the false dest, invert the condition. |
| 555 | if (!isTrueDest) TrueValues = TrueValues.inverse(); |
| 556 | |
| 557 | // Figure out the possible values of the query BEFORE this branch. |
| 558 | LVILatticeVal InBlock = getBlockValue(BBFrom); |
| 559 | if (!InBlock.isConstantRange()) return InBlock; |
| 560 | |
| 561 | // Find all potential values that satisfy both the input and output |
| 562 | // conditions. |
| 563 | ConstantRange PossibleValues = |
| 564 | TrueValues.intersectWith(InBlock.getConstantRange()); |
| 565 | |
| 566 | return LVILatticeVal::getRange(PossibleValues); |
| 567 | } |
| 568 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 571 | |
| 572 | // If the edge was formed by a switch on the value, then we may know exactly |
| 573 | // what it is. |
| 574 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
| 575 | // If BBTo is the default destination of the switch, we don't know anything. |
| 576 | // Given a more powerful range analysis we could know stuff. |
| 577 | if (SI->getCondition() == Val && SI->getDefaultDest() != BBTo) { |
| 578 | // We only know something if there is exactly one value that goes from |
| 579 | // BBFrom to BBTo. |
| 580 | unsigned NumEdges = 0; |
| 581 | ConstantInt *EdgeVal = 0; |
| 582 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 583 | if (SI->getSuccessor(i) != BBTo) continue; |
| 584 | if (NumEdges++) break; |
| 585 | EdgeVal = SI->getCaseValue(i); |
| 586 | } |
| 587 | assert(EdgeVal && "Missing successor?"); |
| 588 | if (NumEdges == 1) |
| 589 | return LVILatticeVal::get(EdgeVal); |
| 590 | } |
| 591 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 592 | |
| 593 | // Otherwise see if the value is known in the block. |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 594 | return getBlockValue(BBFrom); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 597 | |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | // LazyValueInfoCache Impl |
| 600 | //===----------------------------------------------------------------------===// |
| 601 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 602 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) { |
| 603 | // If already a constant, there is nothing to compute. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 604 | if (Constant *VC = dyn_cast<Constant>(V)) |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 605 | return LVILatticeVal::get(VC); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 606 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 607 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 608 | << BB->getName() << "'\n"); |
| 609 | |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 610 | LVILatticeVal Result = LVIQuery(V, *this, |
| 611 | ValueCache[LVIValueHandle(V, this)], |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 612 | OverDefinedCache).getBlockValue(BB); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 613 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 614 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 615 | return Result; |
| 616 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 618 | LVILatticeVal LazyValueInfoCache:: |
| 619 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) { |
| 620 | // If already a constant, there is nothing to compute. |
| 621 | if (Constant *VC = dyn_cast<Constant>(V)) |
| 622 | return LVILatticeVal::get(VC); |
| 623 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 624 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 625 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 626 | |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 627 | LVILatticeVal Result = |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 628 | LVIQuery(V, *this, ValueCache[LVIValueHandle(V, this)], |
Owen Anderson | 81881bc | 2010-07-30 20:56:07 +0000 | [diff] [blame] | 629 | OverDefinedCache).getEdgeValue(FromBB, ToBB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 630 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 631 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 632 | |
| 633 | return Result; |
| 634 | } |
| 635 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 636 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 637 | BasicBlock *NewSucc) { |
| 638 | // When an edge in the graph has been threaded, values that we could not |
| 639 | // determine a value for before (i.e. were marked overdefined) may be possible |
| 640 | // to solve now. We do NOT try to proactively update these values. Instead, |
| 641 | // we clear their entries from the cache, and allow lazy updating to recompute |
| 642 | // them when needed. |
| 643 | |
| 644 | // The updating process is fairly simple: we need to dropped cached info |
| 645 | // for all values that were marked overdefined in OldSucc, and for those same |
| 646 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 647 | // also marked overdefined. |
| 648 | std::vector<BasicBlock*> worklist; |
| 649 | worklist.push_back(OldSucc); |
| 650 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 651 | DenseSet<Value*> ClearSet; |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 652 | for (std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 653 | I = OverDefinedCache.begin(), E = OverDefinedCache.end(); I != E; ++I) { |
| 654 | if (I->first == OldSucc) |
| 655 | ClearSet.insert(I->second); |
| 656 | } |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 657 | |
| 658 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 659 | // NOTE: We do not need a visited list since any blocks we have already |
| 660 | // visited will have had their overdefined markers cleared already, and we |
| 661 | // thus won't loop to their successors. |
| 662 | while (!worklist.empty()) { |
| 663 | BasicBlock *ToUpdate = worklist.back(); |
| 664 | worklist.pop_back(); |
| 665 | |
| 666 | // Skip blocks only accessible through NewSucc. |
| 667 | if (ToUpdate == NewSucc) continue; |
| 668 | |
| 669 | bool changed = false; |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 670 | for (DenseSet<Value*>::iterator I = ClearSet.begin(),E = ClearSet.end(); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 671 | I != E; ++I) { |
| 672 | // If a value was marked overdefined in OldSucc, and is here too... |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 673 | std::set<std::pair<AssertingVH<BasicBlock>, Value*> >::iterator OI = |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 674 | OverDefinedCache.find(std::make_pair(ToUpdate, *I)); |
| 675 | if (OI == OverDefinedCache.end()) continue; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 676 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 677 | // Remove it from the caches. |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 678 | ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)]; |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 679 | ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate); |
| 680 | |
| 681 | assert(CI != Entry.end() && "Couldn't find entry to update?"); |
| 682 | Entry.erase(CI); |
| 683 | OverDefinedCache.erase(OI); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 684 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 685 | // If we removed anything, then we potentially need to update |
| 686 | // blocks successors too. |
| 687 | changed = true; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | if (!changed) continue; |
| 691 | |
| 692 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 693 | } |
| 694 | } |
| 695 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 696 | //===----------------------------------------------------------------------===// |
| 697 | // LazyValueInfo Impl |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 700 | /// getCache - This lazily constructs the LazyValueInfoCache. |
| 701 | static LazyValueInfoCache &getCache(void *&PImpl) { |
| 702 | if (!PImpl) |
| 703 | PImpl = new LazyValueInfoCache(); |
| 704 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 705 | } |
| 706 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 707 | bool LazyValueInfo::runOnFunction(Function &F) { |
| 708 | if (PImpl) |
| 709 | getCache(PImpl).clear(); |
| 710 | |
| 711 | TD = getAnalysisIfAvailable<TargetData>(); |
| 712 | // Fully lazy. |
| 713 | return false; |
| 714 | } |
| 715 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 716 | void LazyValueInfo::releaseMemory() { |
| 717 | // If the cache was allocated, free it. |
| 718 | if (PImpl) { |
| 719 | delete &getCache(PImpl); |
| 720 | PImpl = 0; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) { |
| 725 | LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB); |
| 726 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 727 | if (Result.isConstant()) |
| 728 | return Result.getConstant(); |
| 729 | return 0; |
| 730 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 731 | |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 732 | /// getConstantOnEdge - Determine whether the specified value is known to be a |
| 733 | /// constant on the specified edge. Return null if not. |
| 734 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
| 735 | BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 736 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 737 | |
| 738 | if (Result.isConstant()) |
| 739 | return Result.getConstant(); |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 740 | else if (Result.isConstantRange()) { |
| 741 | ConstantRange CR = Result.getConstantRange(); |
| 742 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 743 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 744 | } |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 748 | /// getPredicateOnEdge - Determine whether the specified value comparison |
| 749 | /// with a constant is known to be true or false on the specified CFG edge. |
| 750 | /// Pred is a CmpInst predicate. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 751 | LazyValueInfo::Tristate |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 752 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 753 | BasicBlock *FromBB, BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 754 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 755 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 756 | // If we know the value is a constant, evaluate the conditional. |
| 757 | Constant *Res = 0; |
| 758 | if (Result.isConstant()) { |
| 759 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD); |
| 760 | if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res)) |
| 761 | return ResCI->isZero() ? False : True; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 762 | return Unknown; |
| 763 | } |
| 764 | |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 765 | if (Result.isConstantRange()) { |
| 766 | ConstantInt *CI = cast<ConstantInt>(C); |
| 767 | ConstantRange CR = Result.getConstantRange(); |
| 768 | if (Pred == ICmpInst::ICMP_EQ) { |
| 769 | if (!CR.contains(CI->getValue())) |
| 770 | return False; |
| 771 | |
| 772 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
| 773 | return True; |
| 774 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 775 | if (!CR.contains(CI->getValue())) |
| 776 | return True; |
| 777 | |
| 778 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
| 779 | return False; |
| 780 | } |
| 781 | |
| 782 | // Handle more complex predicates. |
| 783 | ConstantRange RHS(CI->getValue(), CI->getValue()+1); |
| 784 | ConstantRange TrueValues = ConstantRange::makeICmpRegion(Pred, RHS); |
| 785 | if (CR.intersectWith(TrueValues).isEmptySet()) |
| 786 | return False; |
Owen Anderson | 625051b | 2010-08-10 23:20:01 +0000 | [diff] [blame] | 787 | else if (TrueValues.contains(CR)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 788 | return True; |
| 789 | |
| 790 | return Unknown; |
| 791 | } |
| 792 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 793 | if (Result.isNotConstant()) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 794 | // If this is an equality comparison, we can try to fold it knowing that |
| 795 | // "V != C1". |
| 796 | if (Pred == ICmpInst::ICMP_EQ) { |
| 797 | // !C1 == C -> false iff C1 == C. |
| 798 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
| 799 | Result.getNotConstant(), C, TD); |
| 800 | if (Res->isNullValue()) |
| 801 | return False; |
| 802 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 803 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | 5553a3a | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 804 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 805 | Result.getNotConstant(), C, TD); |
| 806 | if (Res->isNullValue()) |
| 807 | return True; |
| 808 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 809 | return Unknown; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 810 | } |
| 811 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 812 | return Unknown; |
| 813 | } |
| 814 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 815 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 816 | BasicBlock* NewSucc) { |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame^] | 817 | if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc); |
| 818 | } |
| 819 | |
| 820 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
| 821 | if (PImpl) getCache(PImpl).eraseBlock(BB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 822 | } |