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" |
Chris Lattner | b8c124c | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/PointerIntPair.h" |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | char LazyValueInfo::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 30 | INITIALIZE_PASS(LazyValueInfo, "lazy-value-info", |
| 31 | "Lazy Value Information Analysis", false, true); |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 32 | |
| 33 | namespace llvm { |
| 34 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); } |
| 35 | } |
| 36 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 37 | |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | // LVILatticeVal |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | /// LVILatticeVal - This is the information tracked by LazyValueInfo for each |
| 43 | /// value. |
| 44 | /// |
| 45 | /// FIXME: This is basically just for bringup, this can be made a lot more rich |
| 46 | /// in the future. |
| 47 | /// |
| 48 | namespace { |
| 49 | class LVILatticeVal { |
| 50 | enum LatticeValueTy { |
| 51 | /// undefined - This LLVM Value has no known value yet. |
| 52 | undefined, |
| 53 | /// constant - This LLVM Value has a specific constant value. |
| 54 | constant, |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 55 | |
| 56 | /// notconstant - This LLVM value is known to not have the specified value. |
| 57 | notconstant, |
| 58 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 59 | /// overdefined - This instruction is not known to be constant, and we know |
| 60 | /// it has a value. |
| 61 | overdefined |
| 62 | }; |
| 63 | |
| 64 | /// Val: This stores the current lattice value along with the Constant* for |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 65 | /// the constant if this is a 'constant' or 'notconstant' value. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 66 | PointerIntPair<Constant *, 2, LatticeValueTy> Val; |
| 67 | |
| 68 | public: |
| 69 | LVILatticeVal() : Val(0, undefined) {} |
| 70 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 71 | static LVILatticeVal get(Constant *C) { |
| 72 | LVILatticeVal Res; |
| 73 | Res.markConstant(C); |
| 74 | return Res; |
| 75 | } |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 76 | static LVILatticeVal getNot(Constant *C) { |
| 77 | LVILatticeVal Res; |
| 78 | Res.markNotConstant(C); |
| 79 | return Res; |
| 80 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 81 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 82 | bool isUndefined() const { return Val.getInt() == undefined; } |
| 83 | bool isConstant() const { return Val.getInt() == constant; } |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 84 | bool isNotConstant() const { return Val.getInt() == notconstant; } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 85 | bool isOverdefined() const { return Val.getInt() == overdefined; } |
| 86 | |
| 87 | Constant *getConstant() const { |
| 88 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
| 89 | return Val.getPointer(); |
| 90 | } |
| 91 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 92 | Constant *getNotConstant() const { |
| 93 | assert(isNotConstant() && "Cannot get the constant of a non-notconstant!"); |
| 94 | return Val.getPointer(); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /// markOverdefined - Return true if this is a change in status. |
| 98 | bool markOverdefined() { |
| 99 | if (isOverdefined()) |
| 100 | return false; |
| 101 | Val.setInt(overdefined); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | /// markConstant - Return true if this is a change in status. |
| 106 | bool markConstant(Constant *V) { |
| 107 | if (isConstant()) { |
| 108 | assert(getConstant() == V && "Marking constant with different value"); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | assert(isUndefined()); |
| 113 | Val.setInt(constant); |
| 114 | assert(V && "Marking constant with NULL"); |
| 115 | Val.setPointer(V); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 119 | /// markNotConstant - Return true if this is a change in status. |
| 120 | bool markNotConstant(Constant *V) { |
| 121 | if (isNotConstant()) { |
| 122 | assert(getNotConstant() == V && "Marking !constant with different value"); |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if (isConstant()) |
| 127 | assert(getConstant() != V && "Marking not constant with different value"); |
| 128 | else |
| 129 | assert(isUndefined()); |
| 130 | |
| 131 | Val.setInt(notconstant); |
| 132 | assert(V && "Marking constant with NULL"); |
| 133 | Val.setPointer(V); |
| 134 | return true; |
| 135 | } |
| 136 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 137 | /// mergeIn - Merge the specified lattice value into this one, updating this |
| 138 | /// one and returning true if anything changed. |
| 139 | bool mergeIn(const LVILatticeVal &RHS) { |
| 140 | if (RHS.isUndefined() || isOverdefined()) return false; |
| 141 | if (RHS.isOverdefined()) return markOverdefined(); |
| 142 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 143 | if (RHS.isNotConstant()) { |
| 144 | if (isNotConstant()) { |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 145 | if (getNotConstant() != RHS.getNotConstant() || |
| 146 | isa<ConstantExpr>(getNotConstant()) || |
| 147 | isa<ConstantExpr>(RHS.getNotConstant())) |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 148 | return markOverdefined(); |
| 149 | return false; |
| 150 | } |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 151 | if (isConstant()) { |
| 152 | if (getConstant() == RHS.getNotConstant() || |
| 153 | isa<ConstantExpr>(RHS.getNotConstant()) || |
| 154 | isa<ConstantExpr>(getConstant())) |
| 155 | return markOverdefined(); |
| 156 | return markNotConstant(RHS.getNotConstant()); |
| 157 | } |
| 158 | |
| 159 | assert(isUndefined() && "Unexpected lattice"); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 160 | return markNotConstant(RHS.getNotConstant()); |
| 161 | } |
| 162 | |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 163 | // RHS must be a constant, we must be undef, constant, or notconstant. |
| 164 | if (isUndefined()) |
| 165 | return markConstant(RHS.getConstant()); |
| 166 | |
| 167 | if (isConstant()) { |
| 168 | if (getConstant() != RHS.getConstant()) |
| 169 | return markOverdefined(); |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // If we are known "!=4" and RHS is "==5", stay at "!=4". |
| 174 | if (getNotConstant() == RHS.getConstant() || |
| 175 | isa<ConstantExpr>(getNotConstant()) || |
| 176 | isa<ConstantExpr>(RHS.getConstant())) |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 177 | return markOverdefined(); |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 178 | return false; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | }; |
| 182 | |
| 183 | } // end anonymous namespace. |
| 184 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 185 | namespace llvm { |
| 186 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) { |
| 187 | if (Val.isUndefined()) |
| 188 | return OS << "undefined"; |
| 189 | if (Val.isOverdefined()) |
| 190 | return OS << "overdefined"; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 191 | |
| 192 | if (Val.isNotConstant()) |
| 193 | return OS << "notconstant<" << *Val.getNotConstant() << '>'; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 194 | return OS << "constant<" << *Val.getConstant() << '>'; |
| 195 | } |
| 196 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 197 | |
| 198 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 199 | // LazyValueInfoCache Decl |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 200 | //===----------------------------------------------------------------------===// |
| 201 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 202 | namespace { |
| 203 | /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which |
| 204 | /// maintains information about queries across the clients' queries. |
| 205 | class LazyValueInfoCache { |
| 206 | public: |
| 207 | /// BlockCacheEntryTy - This is a computed lattice value at the end of the |
| 208 | /// specified basic block for a Value* that depends on context. |
| 209 | typedef std::pair<BasicBlock*, LVILatticeVal> BlockCacheEntryTy; |
| 210 | |
| 211 | /// ValueCacheEntryTy - This is all of the cached block information for |
| 212 | /// exactly one Value*. The entries are sorted by the BasicBlock* of the |
| 213 | /// entries, allowing us to do a lookup with a binary search. |
| 214 | typedef std::vector<BlockCacheEntryTy> ValueCacheEntryTy; |
| 215 | |
| 216 | private: |
| 217 | /// ValueCache - This is all of the cached information for all values, |
| 218 | /// mapped from Value* to key information. |
| 219 | DenseMap<Value*, ValueCacheEntryTy> ValueCache; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 220 | |
| 221 | /// OverDefinedCache - This tracks, on a per-block basis, the set of |
| 222 | /// values that are over-defined at the end of that block. This is required |
| 223 | /// for cache updating. |
| 224 | DenseMap<BasicBlock*, std::set<Value*> > OverDefinedCache; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 225 | public: |
| 226 | |
| 227 | /// getValueInBlock - This is the query interface to determine the lattice |
| 228 | /// value for the specified Value* at the end of the specified block. |
| 229 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB); |
| 230 | |
| 231 | /// getValueOnEdge - This is the query interface to determine the lattice |
| 232 | /// value for the specified Value* that is true on the specified edge. |
| 233 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 234 | |
| 235 | /// threadEdge - This is the update interface to inform the cache that an |
| 236 | /// edge from PredBB to OldSucc has been threaded to be from PredBB to |
| 237 | /// NewSucc. |
| 238 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 239 | }; |
| 240 | } // end anonymous namespace |
| 241 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 242 | namespace { |
| 243 | struct BlockCacheEntryComparator { |
| 244 | static int Compare(const void *LHSv, const void *RHSv) { |
| 245 | const LazyValueInfoCache::BlockCacheEntryTy *LHS = |
| 246 | static_cast<const LazyValueInfoCache::BlockCacheEntryTy *>(LHSv); |
| 247 | const LazyValueInfoCache::BlockCacheEntryTy *RHS = |
| 248 | static_cast<const LazyValueInfoCache::BlockCacheEntryTy *>(RHSv); |
| 249 | if (LHS->first < RHS->first) |
| 250 | return -1; |
| 251 | if (LHS->first > RHS->first) |
| 252 | return 1; |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | bool operator()(const LazyValueInfoCache::BlockCacheEntryTy &LHS, |
| 257 | const LazyValueInfoCache::BlockCacheEntryTy &RHS) const { |
| 258 | return LHS.first < RHS.first; |
| 259 | } |
| 260 | }; |
| 261 | } |
| 262 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 263 | //===----------------------------------------------------------------------===// |
| 264 | // LVIQuery Impl |
| 265 | //===----------------------------------------------------------------------===// |
| 266 | |
| 267 | namespace { |
| 268 | /// LVIQuery - This is a transient object that exists while a query is |
| 269 | /// being performed. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 270 | /// |
| 271 | /// TODO: Reuse LVIQuery instead of recreating it for every query, this avoids |
| 272 | /// reallocation of the densemap on every query. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 273 | class LVIQuery { |
| 274 | typedef LazyValueInfoCache::BlockCacheEntryTy BlockCacheEntryTy; |
| 275 | typedef LazyValueInfoCache::ValueCacheEntryTy ValueCacheEntryTy; |
| 276 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 277 | /// This is the current value being queried for. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 278 | Value *Val; |
| 279 | |
| 280 | /// This is all of the cached information about this value. |
| 281 | ValueCacheEntryTy &Cache; |
| 282 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 283 | /// This tracks, for each block, what values are overdefined. |
| 284 | DenseMap<BasicBlock*, std::set<Value*> > &OverDefinedCache; |
| 285 | |
Chris Lattner | d6add15 | 2009-11-16 03:51:42 +0000 | [diff] [blame] | 286 | /// NewBlocks - This is a mapping of the new BasicBlocks which have been |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 287 | /// added to cache but that are not in sorted order. |
| 288 | DenseMap<BasicBlock*, LVILatticeVal> NewBlockInfo; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 289 | public: |
| 290 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 291 | LVIQuery(Value *V, ValueCacheEntryTy &VC, |
| 292 | DenseMap<BasicBlock*, std::set<Value*> > &ODC) |
| 293 | : Val(V), Cache(VC), OverDefinedCache(ODC) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 296 | ~LVIQuery() { |
| 297 | // When the query is done, insert the newly discovered facts into the |
| 298 | // cache in sorted order. |
| 299 | if (NewBlockInfo.empty()) return; |
| 300 | |
| 301 | // Grow the cache to exactly fit the new data. |
| 302 | Cache.reserve(Cache.size() + NewBlockInfo.size()); |
| 303 | |
| 304 | // If we only have one new entry, insert it instead of doing a full-on |
| 305 | // sort. |
| 306 | if (NewBlockInfo.size() == 1) { |
| 307 | BlockCacheEntryTy Entry = *NewBlockInfo.begin(); |
| 308 | ValueCacheEntryTy::iterator I = |
| 309 | std::lower_bound(Cache.begin(), Cache.end(), Entry, |
| 310 | BlockCacheEntryComparator()); |
| 311 | assert((I == Cache.end() || I->first != Entry.first) && |
| 312 | "Entry already in map!"); |
| 313 | |
| 314 | Cache.insert(I, Entry); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | // TODO: If we only have two new elements, INSERT them both. |
| 319 | |
| 320 | Cache.insert(Cache.end(), NewBlockInfo.begin(), NewBlockInfo.end()); |
| 321 | array_pod_sort(Cache.begin(), Cache.end(), |
| 322 | BlockCacheEntryComparator::Compare); |
| 323 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 324 | for (DenseMap<BasicBlock*, LVILatticeVal>::iterator |
| 325 | I = NewBlockInfo.begin(), E = NewBlockInfo.end(); I != E; ++I) { |
| 326 | if (I->second.isOverdefined()) |
| 327 | OverDefinedCache[I->first].insert(Val); |
| 328 | } |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 331 | LVILatticeVal getBlockValue(BasicBlock *BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 332 | LVILatticeVal getEdgeValue(BasicBlock *FromBB, BasicBlock *ToBB); |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 333 | |
| 334 | private: |
| 335 | LVILatticeVal &getCachedEntryForBlock(BasicBlock *BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 336 | }; |
| 337 | } // end anonymous namespace |
| 338 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 339 | /// getCachedEntryForBlock - See if we already have a value for this block. If |
| 340 | /// so, return it, otherwise create a new entry in the NewBlockInfo map to use. |
| 341 | LVILatticeVal &LVIQuery::getCachedEntryForBlock(BasicBlock *BB) { |
| 342 | |
| 343 | // Do a binary search to see if we already have an entry for this block in |
| 344 | // the cache set. If so, find it. |
| 345 | if (!Cache.empty()) { |
| 346 | ValueCacheEntryTy::iterator Entry = |
| 347 | std::lower_bound(Cache.begin(), Cache.end(), |
| 348 | BlockCacheEntryTy(BB, LVILatticeVal()), |
| 349 | BlockCacheEntryComparator()); |
| 350 | if (Entry != Cache.end() && Entry->first == BB) |
| 351 | return Entry->second; |
| 352 | } |
| 353 | |
| 354 | // Otherwise, check to see if it's in NewBlockInfo or create a new entry if |
| 355 | // not. |
| 356 | return NewBlockInfo[BB]; |
| 357 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 358 | |
| 359 | LVILatticeVal LVIQuery::getBlockValue(BasicBlock *BB) { |
| 360 | // See if we already have a value for this block. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 361 | LVILatticeVal &BBLV = getCachedEntryForBlock(BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 362 | |
| 363 | // If we've already computed this block's value, return it. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 364 | if (!BBLV.isUndefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 365 | DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n'); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 366 | return BBLV; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 369 | // Otherwise, this is the first time we're seeing this block. Reset the |
| 370 | // lattice value to overdefined, so that cycles will terminate and be |
| 371 | // conservatively correct. |
| 372 | BBLV.markOverdefined(); |
| 373 | |
| 374 | // If V is live into BB, see if our predecessors know anything about it. |
| 375 | Instruction *BBI = dyn_cast<Instruction>(Val); |
| 376 | if (BBI == 0 || BBI->getParent() != BB) { |
| 377 | LVILatticeVal Result; // Start Undefined. |
| 378 | unsigned NumPreds = 0; |
| 379 | |
| 380 | // Loop over all of our predecessors, merging what we know from them into |
| 381 | // result. |
| 382 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 383 | Result.mergeIn(getEdgeValue(*PI, BB)); |
| 384 | |
| 385 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 386 | // to overdefined. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 387 | if (Result.isOverdefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 388 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 389 | << "' - overdefined because of pred.\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 390 | return Result; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 391 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 392 | ++NumPreds; |
| 393 | } |
| 394 | |
| 395 | // If this is the entry block, we must be asking about an argument. The |
| 396 | // value is overdefined. |
| 397 | if (NumPreds == 0 && BB == &BB->getParent()->front()) { |
| 398 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 399 | Result.markOverdefined(); |
| 400 | return Result; |
| 401 | } |
| 402 | |
| 403 | // Return the merged value, which is more precise than 'overdefined'. |
| 404 | assert(!Result.isOverdefined()); |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 405 | return getCachedEntryForBlock(BB) = Result; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | // If this value is defined by an instruction in this block, we have to |
| 409 | // process it here somehow or return overdefined. |
| 410 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
| 411 | (void)PN; |
| 412 | // TODO: PHI Translation in preds. |
| 413 | } else { |
| 414 | |
| 415 | } |
| 416 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 417 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 418 | << "' - overdefined because inst def found.\n"); |
| 419 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 420 | LVILatticeVal Result; |
| 421 | Result.markOverdefined(); |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 422 | return getCachedEntryForBlock(BB) = Result; |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 425 | |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 426 | /// getEdgeValue - This method attempts to infer more complex |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 427 | LVILatticeVal LVIQuery::getEdgeValue(BasicBlock *BBFrom, BasicBlock *BBTo) { |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 428 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 429 | // know that v != 0. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 430 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 431 | // If this is a conditional branch and only one successor goes to BBTo, then |
| 432 | // we maybe able to infer something from the condition. |
| 433 | if (BI->isConditional() && |
| 434 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 435 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 436 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 437 | "BBTo isn't a successor of BBFrom"); |
| 438 | |
| 439 | // If V is the condition of the branch itself, then we know exactly what |
| 440 | // it is. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 441 | if (BI->getCondition() == Val) |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 442 | return LVILatticeVal::get(ConstantInt::get( |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 443 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 444 | |
| 445 | // If the condition of the branch is an equality comparison, we may be |
| 446 | // able to infer the value. |
| 447 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 448 | if (ICI->isEquality() && ICI->getOperand(0) == Val && |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 449 | isa<Constant>(ICI->getOperand(1))) { |
| 450 | // We know that V has the RHS constant if this is a true SETEQ or |
| 451 | // false SETNE. |
| 452 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
| 453 | return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 454 | return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | } |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 458 | |
| 459 | // If the edge was formed by a switch on the value, then we may know exactly |
| 460 | // what it is. |
| 461 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
| 462 | // If BBTo is the default destination of the switch, we don't know anything. |
| 463 | // Given a more powerful range analysis we could know stuff. |
| 464 | if (SI->getCondition() == Val && SI->getDefaultDest() != BBTo) { |
| 465 | // We only know something if there is exactly one value that goes from |
| 466 | // BBFrom to BBTo. |
| 467 | unsigned NumEdges = 0; |
| 468 | ConstantInt *EdgeVal = 0; |
| 469 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 470 | if (SI->getSuccessor(i) != BBTo) continue; |
| 471 | if (NumEdges++) break; |
| 472 | EdgeVal = SI->getCaseValue(i); |
| 473 | } |
| 474 | assert(EdgeVal && "Missing successor?"); |
| 475 | if (NumEdges == 1) |
| 476 | return LVILatticeVal::get(EdgeVal); |
| 477 | } |
| 478 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 479 | |
| 480 | // Otherwise see if the value is known in the block. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 481 | return getBlockValue(BBFrom); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 485 | //===----------------------------------------------------------------------===// |
| 486 | // LazyValueInfoCache Impl |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | |
| 489 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) { |
| 490 | // If already a constant, there is nothing to compute. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 491 | if (Constant *VC = dyn_cast<Constant>(V)) |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 492 | return LVILatticeVal::get(VC); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 493 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 494 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 495 | << BB->getName() << "'\n"); |
| 496 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 497 | LVILatticeVal Result = LVIQuery(V, ValueCache[V], |
| 498 | OverDefinedCache).getBlockValue(BB); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 499 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 500 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 501 | return Result; |
| 502 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 504 | LVILatticeVal LazyValueInfoCache:: |
| 505 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) { |
| 506 | // If already a constant, there is nothing to compute. |
| 507 | if (Constant *VC = dyn_cast<Constant>(V)) |
| 508 | return LVILatticeVal::get(VC); |
| 509 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 510 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 511 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 512 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 513 | LVILatticeVal Result = |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 514 | LVIQuery(V, ValueCache[V], |
| 515 | OverDefinedCache).getEdgeValue(FromBB, ToBB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 516 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 517 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 518 | |
| 519 | return Result; |
| 520 | } |
| 521 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 522 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 523 | BasicBlock *NewSucc) { |
| 524 | // When an edge in the graph has been threaded, values that we could not |
| 525 | // determine a value for before (i.e. were marked overdefined) may be possible |
| 526 | // to solve now. We do NOT try to proactively update these values. Instead, |
| 527 | // we clear their entries from the cache, and allow lazy updating to recompute |
| 528 | // them when needed. |
| 529 | |
| 530 | // The updating process is fairly simple: we need to dropped cached info |
| 531 | // for all values that were marked overdefined in OldSucc, and for those same |
| 532 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 533 | // also marked overdefined. |
| 534 | std::vector<BasicBlock*> worklist; |
| 535 | worklist.push_back(OldSucc); |
| 536 | |
| 537 | std::set<Value*> ClearSet = OverDefinedCache[OldSucc]; |
| 538 | LVILatticeVal OverDef; |
| 539 | OverDef.markOverdefined(); |
| 540 | |
| 541 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 542 | // NOTE: We do not need a visited list since any blocks we have already |
| 543 | // visited will have had their overdefined markers cleared already, and we |
| 544 | // thus won't loop to their successors. |
| 545 | while (!worklist.empty()) { |
| 546 | BasicBlock *ToUpdate = worklist.back(); |
| 547 | worklist.pop_back(); |
| 548 | |
| 549 | // Skip blocks only accessible through NewSucc. |
| 550 | if (ToUpdate == NewSucc) continue; |
| 551 | |
| 552 | bool changed = false; |
| 553 | std::set<Value*> &CurrentSet = OverDefinedCache[ToUpdate]; |
| 554 | for (std::set<Value*>::iterator I = ClearSet.begin(),E = ClearSet.end(); |
| 555 | I != E; ++I) { |
| 556 | // If a value was marked overdefined in OldSucc, and is here too... |
| 557 | if (CurrentSet.count(*I)) { |
| 558 | // Remove it from the caches. |
| 559 | ValueCacheEntryTy &Entry = ValueCache[*I]; |
| 560 | ValueCacheEntryTy::iterator CI = |
| 561 | std::lower_bound(Entry.begin(), Entry.end(), |
| 562 | std::make_pair(ToUpdate, OverDef), |
| 563 | BlockCacheEntryComparator()); |
| 564 | assert(CI != Entry.end() && "Couldn't find entry to update?"); |
| 565 | Entry.erase(CI); |
| 566 | |
| 567 | CurrentSet.erase(*I); |
| 568 | |
| 569 | // If we removed anything, then we potentially need to update |
| 570 | // blocks successors too. |
| 571 | changed = true; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | if (!changed) continue; |
| 576 | |
| 577 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 578 | } |
| 579 | } |
| 580 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 581 | //===----------------------------------------------------------------------===// |
| 582 | // LazyValueInfo Impl |
| 583 | //===----------------------------------------------------------------------===// |
| 584 | |
| 585 | bool LazyValueInfo::runOnFunction(Function &F) { |
| 586 | TD = getAnalysisIfAvailable<TargetData>(); |
| 587 | // Fully lazy. |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | /// getCache - This lazily constructs the LazyValueInfoCache. |
| 592 | static LazyValueInfoCache &getCache(void *&PImpl) { |
| 593 | if (!PImpl) |
| 594 | PImpl = new LazyValueInfoCache(); |
| 595 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 596 | } |
| 597 | |
| 598 | void LazyValueInfo::releaseMemory() { |
| 599 | // If the cache was allocated, free it. |
| 600 | if (PImpl) { |
| 601 | delete &getCache(PImpl); |
| 602 | PImpl = 0; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) { |
| 607 | LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB); |
| 608 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 609 | if (Result.isConstant()) |
| 610 | return Result.getConstant(); |
| 611 | return 0; |
| 612 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 613 | |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 614 | /// getConstantOnEdge - Determine whether the specified value is known to be a |
| 615 | /// constant on the specified edge. Return null if not. |
| 616 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
| 617 | BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 618 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 619 | |
| 620 | if (Result.isConstant()) |
| 621 | return Result.getConstant(); |
| 622 | return 0; |
| 623 | } |
| 624 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 625 | /// getPredicateOnEdge - Determine whether the specified value comparison |
| 626 | /// with a constant is known to be true or false on the specified CFG edge. |
| 627 | /// Pred is a CmpInst predicate. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 628 | LazyValueInfo::Tristate |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 629 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 630 | BasicBlock *FromBB, BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 631 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 632 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 633 | // If we know the value is a constant, evaluate the conditional. |
| 634 | Constant *Res = 0; |
| 635 | if (Result.isConstant()) { |
| 636 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD); |
| 637 | if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res)) |
| 638 | return ResCI->isZero() ? False : True; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 639 | return Unknown; |
| 640 | } |
| 641 | |
| 642 | if (Result.isNotConstant()) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 643 | // If this is an equality comparison, we can try to fold it knowing that |
| 644 | // "V != C1". |
| 645 | if (Pred == ICmpInst::ICMP_EQ) { |
| 646 | // !C1 == C -> false iff C1 == C. |
| 647 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
| 648 | Result.getNotConstant(), C, TD); |
| 649 | if (Res->isNullValue()) |
| 650 | return False; |
| 651 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 652 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | 5553a3a | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 653 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 654 | Result.getNotConstant(), C, TD); |
| 655 | if (Res->isNullValue()) |
| 656 | return True; |
| 657 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 658 | return Unknown; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 661 | return Unknown; |
| 662 | } |
| 663 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame^] | 664 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 665 | BasicBlock* NewSucc) { |
| 666 | getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc); |
| 667 | } |