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; |
| 220 | public: |
| 221 | |
| 222 | /// getValueInBlock - This is the query interface to determine the lattice |
| 223 | /// value for the specified Value* at the end of the specified block. |
| 224 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB); |
| 225 | |
| 226 | /// getValueOnEdge - This is the query interface to determine the lattice |
| 227 | /// value for the specified Value* that is true on the specified edge. |
| 228 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB); |
| 229 | }; |
| 230 | } // end anonymous namespace |
| 231 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 232 | namespace { |
| 233 | struct BlockCacheEntryComparator { |
| 234 | static int Compare(const void *LHSv, const void *RHSv) { |
| 235 | const LazyValueInfoCache::BlockCacheEntryTy *LHS = |
| 236 | static_cast<const LazyValueInfoCache::BlockCacheEntryTy *>(LHSv); |
| 237 | const LazyValueInfoCache::BlockCacheEntryTy *RHS = |
| 238 | static_cast<const LazyValueInfoCache::BlockCacheEntryTy *>(RHSv); |
| 239 | if (LHS->first < RHS->first) |
| 240 | return -1; |
| 241 | if (LHS->first > RHS->first) |
| 242 | return 1; |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | bool operator()(const LazyValueInfoCache::BlockCacheEntryTy &LHS, |
| 247 | const LazyValueInfoCache::BlockCacheEntryTy &RHS) const { |
| 248 | return LHS.first < RHS.first; |
| 249 | } |
| 250 | }; |
| 251 | } |
| 252 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
| 254 | // LVIQuery Impl |
| 255 | //===----------------------------------------------------------------------===// |
| 256 | |
| 257 | namespace { |
| 258 | /// LVIQuery - This is a transient object that exists while a query is |
| 259 | /// being performed. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 260 | /// |
| 261 | /// TODO: Reuse LVIQuery instead of recreating it for every query, this avoids |
| 262 | /// reallocation of the densemap on every query. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 263 | class LVIQuery { |
| 264 | typedef LazyValueInfoCache::BlockCacheEntryTy BlockCacheEntryTy; |
| 265 | typedef LazyValueInfoCache::ValueCacheEntryTy ValueCacheEntryTy; |
| 266 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 267 | /// This is the current value being queried for. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 268 | Value *Val; |
| 269 | |
| 270 | /// This is all of the cached information about this value. |
| 271 | ValueCacheEntryTy &Cache; |
| 272 | |
Chris Lattner | d6add15 | 2009-11-16 03:51:42 +0000 | [diff] [blame] | 273 | /// NewBlocks - This is a mapping of the new BasicBlocks which have been |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 274 | /// added to cache but that are not in sorted order. |
| 275 | DenseMap<BasicBlock*, LVILatticeVal> NewBlockInfo; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 276 | public: |
| 277 | |
| 278 | LVIQuery(Value *V, ValueCacheEntryTy &VC) : Val(V), Cache(VC) { |
| 279 | } |
| 280 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 281 | ~LVIQuery() { |
| 282 | // When the query is done, insert the newly discovered facts into the |
| 283 | // cache in sorted order. |
| 284 | if (NewBlockInfo.empty()) return; |
| 285 | |
| 286 | // Grow the cache to exactly fit the new data. |
| 287 | Cache.reserve(Cache.size() + NewBlockInfo.size()); |
| 288 | |
| 289 | // If we only have one new entry, insert it instead of doing a full-on |
| 290 | // sort. |
| 291 | if (NewBlockInfo.size() == 1) { |
| 292 | BlockCacheEntryTy Entry = *NewBlockInfo.begin(); |
| 293 | ValueCacheEntryTy::iterator I = |
| 294 | std::lower_bound(Cache.begin(), Cache.end(), Entry, |
| 295 | BlockCacheEntryComparator()); |
| 296 | assert((I == Cache.end() || I->first != Entry.first) && |
| 297 | "Entry already in map!"); |
| 298 | |
| 299 | Cache.insert(I, Entry); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | // TODO: If we only have two new elements, INSERT them both. |
| 304 | |
| 305 | Cache.insert(Cache.end(), NewBlockInfo.begin(), NewBlockInfo.end()); |
| 306 | array_pod_sort(Cache.begin(), Cache.end(), |
| 307 | BlockCacheEntryComparator::Compare); |
| 308 | |
| 309 | } |
| 310 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 311 | LVILatticeVal getBlockValue(BasicBlock *BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 312 | LVILatticeVal getEdgeValue(BasicBlock *FromBB, BasicBlock *ToBB); |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 313 | |
| 314 | private: |
| 315 | LVILatticeVal &getCachedEntryForBlock(BasicBlock *BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 316 | }; |
| 317 | } // end anonymous namespace |
| 318 | |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 319 | /// getCachedEntryForBlock - See if we already have a value for this block. If |
| 320 | /// so, return it, otherwise create a new entry in the NewBlockInfo map to use. |
| 321 | LVILatticeVal &LVIQuery::getCachedEntryForBlock(BasicBlock *BB) { |
| 322 | |
| 323 | // Do a binary search to see if we already have an entry for this block in |
| 324 | // the cache set. If so, find it. |
| 325 | if (!Cache.empty()) { |
| 326 | ValueCacheEntryTy::iterator Entry = |
| 327 | std::lower_bound(Cache.begin(), Cache.end(), |
| 328 | BlockCacheEntryTy(BB, LVILatticeVal()), |
| 329 | BlockCacheEntryComparator()); |
| 330 | if (Entry != Cache.end() && Entry->first == BB) |
| 331 | return Entry->second; |
| 332 | } |
| 333 | |
| 334 | // Otherwise, check to see if it's in NewBlockInfo or create a new entry if |
| 335 | // not. |
| 336 | return NewBlockInfo[BB]; |
| 337 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 338 | |
| 339 | LVILatticeVal LVIQuery::getBlockValue(BasicBlock *BB) { |
| 340 | // See if we already have a value for this block. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 341 | LVILatticeVal &BBLV = getCachedEntryForBlock(BB); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 342 | |
| 343 | // If we've already computed this block's value, return it. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 344 | if (!BBLV.isUndefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 345 | DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n'); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 346 | return BBLV; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 349 | // Otherwise, this is the first time we're seeing this block. Reset the |
| 350 | // lattice value to overdefined, so that cycles will terminate and be |
| 351 | // conservatively correct. |
| 352 | BBLV.markOverdefined(); |
| 353 | |
| 354 | // If V is live into BB, see if our predecessors know anything about it. |
| 355 | Instruction *BBI = dyn_cast<Instruction>(Val); |
| 356 | if (BBI == 0 || BBI->getParent() != BB) { |
| 357 | LVILatticeVal Result; // Start Undefined. |
| 358 | unsigned NumPreds = 0; |
| 359 | |
| 360 | // Loop over all of our predecessors, merging what we know from them into |
| 361 | // result. |
| 362 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 363 | Result.mergeIn(getEdgeValue(*PI, BB)); |
| 364 | |
| 365 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 366 | // to overdefined. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 367 | if (Result.isOverdefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 368 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 369 | << "' - overdefined because of pred.\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 370 | return Result; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 371 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 372 | ++NumPreds; |
| 373 | } |
| 374 | |
| 375 | // If this is the entry block, we must be asking about an argument. The |
| 376 | // value is overdefined. |
| 377 | if (NumPreds == 0 && BB == &BB->getParent()->front()) { |
| 378 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 379 | Result.markOverdefined(); |
| 380 | return Result; |
| 381 | } |
| 382 | |
| 383 | // Return the merged value, which is more precise than 'overdefined'. |
| 384 | assert(!Result.isOverdefined()); |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 385 | return getCachedEntryForBlock(BB) = Result; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // If this value is defined by an instruction in this block, we have to |
| 389 | // process it here somehow or return overdefined. |
| 390 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
| 391 | (void)PN; |
| 392 | // TODO: PHI Translation in preds. |
| 393 | } else { |
| 394 | |
| 395 | } |
| 396 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 397 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 398 | << "' - overdefined because inst def found.\n"); |
| 399 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 400 | LVILatticeVal Result; |
| 401 | Result.markOverdefined(); |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 402 | return getCachedEntryForBlock(BB) = Result; |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 406 | /// getEdgeValue - This method attempts to infer more complex |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 407 | LVILatticeVal LVIQuery::getEdgeValue(BasicBlock *BBFrom, BasicBlock *BBTo) { |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 408 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 409 | // know that v != 0. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 410 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 411 | // If this is a conditional branch and only one successor goes to BBTo, then |
| 412 | // we maybe able to infer something from the condition. |
| 413 | if (BI->isConditional() && |
| 414 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 415 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 416 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 417 | "BBTo isn't a successor of BBFrom"); |
| 418 | |
| 419 | // If V is the condition of the branch itself, then we know exactly what |
| 420 | // it is. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 421 | if (BI->getCondition() == Val) |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 422 | return LVILatticeVal::get(ConstantInt::get( |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 423 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 424 | |
| 425 | // If the condition of the branch is an equality comparison, we may be |
| 426 | // able to infer the value. |
| 427 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 428 | if (ICI->isEquality() && ICI->getOperand(0) == Val && |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 429 | isa<Constant>(ICI->getOperand(1))) { |
| 430 | // We know that V has the RHS constant if this is a true SETEQ or |
| 431 | // false SETNE. |
| 432 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
| 433 | return LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 434 | return LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | } |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 438 | |
| 439 | // If the edge was formed by a switch on the value, then we may know exactly |
| 440 | // what it is. |
| 441 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
| 442 | // If BBTo is the default destination of the switch, we don't know anything. |
| 443 | // Given a more powerful range analysis we could know stuff. |
| 444 | if (SI->getCondition() == Val && SI->getDefaultDest() != BBTo) { |
| 445 | // We only know something if there is exactly one value that goes from |
| 446 | // BBFrom to BBTo. |
| 447 | unsigned NumEdges = 0; |
| 448 | ConstantInt *EdgeVal = 0; |
| 449 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 450 | if (SI->getSuccessor(i) != BBTo) continue; |
| 451 | if (NumEdges++) break; |
| 452 | EdgeVal = SI->getCaseValue(i); |
| 453 | } |
| 454 | assert(EdgeVal && "Missing successor?"); |
| 455 | if (NumEdges == 1) |
| 456 | return LVILatticeVal::get(EdgeVal); |
| 457 | } |
| 458 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 459 | |
| 460 | // Otherwise see if the value is known in the block. |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 461 | return getBlockValue(BBFrom); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 465 | //===----------------------------------------------------------------------===// |
| 466 | // LazyValueInfoCache Impl |
| 467 | //===----------------------------------------------------------------------===// |
| 468 | |
| 469 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) { |
| 470 | // If already a constant, there is nothing to compute. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 471 | if (Constant *VC = dyn_cast<Constant>(V)) |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 472 | return LVILatticeVal::get(VC); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 473 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 474 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 475 | << BB->getName() << "'\n"); |
| 476 | |
| 477 | LVILatticeVal Result = LVIQuery(V, ValueCache[V]).getBlockValue(BB); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 478 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 479 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 480 | return Result; |
| 481 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 482 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 483 | LVILatticeVal LazyValueInfoCache:: |
| 484 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) { |
| 485 | // If already a constant, there is nothing to compute. |
| 486 | if (Constant *VC = dyn_cast<Constant>(V)) |
| 487 | return LVILatticeVal::get(VC); |
| 488 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 489 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 490 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
| 491 | LVILatticeVal Result = |
| 492 | LVIQuery(V, ValueCache[V]).getEdgeValue(FromBB, ToBB); |
| 493 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 494 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 495 | |
| 496 | return Result; |
| 497 | } |
| 498 | |
| 499 | //===----------------------------------------------------------------------===// |
| 500 | // LazyValueInfo Impl |
| 501 | //===----------------------------------------------------------------------===// |
| 502 | |
| 503 | bool LazyValueInfo::runOnFunction(Function &F) { |
| 504 | TD = getAnalysisIfAvailable<TargetData>(); |
| 505 | // Fully lazy. |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | /// getCache - This lazily constructs the LazyValueInfoCache. |
| 510 | static LazyValueInfoCache &getCache(void *&PImpl) { |
| 511 | if (!PImpl) |
| 512 | PImpl = new LazyValueInfoCache(); |
| 513 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 514 | } |
| 515 | |
| 516 | void LazyValueInfo::releaseMemory() { |
| 517 | // If the cache was allocated, free it. |
| 518 | if (PImpl) { |
| 519 | delete &getCache(PImpl); |
| 520 | PImpl = 0; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) { |
| 525 | LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB); |
| 526 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 527 | if (Result.isConstant()) |
| 528 | return Result.getConstant(); |
| 529 | return 0; |
| 530 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 531 | |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 532 | /// getConstantOnEdge - Determine whether the specified value is known to be a |
| 533 | /// constant on the specified edge. Return null if not. |
| 534 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
| 535 | BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 536 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 537 | |
| 538 | if (Result.isConstant()) |
| 539 | return Result.getConstant(); |
| 540 | return 0; |
| 541 | } |
| 542 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 543 | /// getPredicateOnEdge - Determine whether the specified value comparison |
| 544 | /// with a constant is known to be true or false on the specified CFG edge. |
| 545 | /// Pred is a CmpInst predicate. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 546 | LazyValueInfo::Tristate |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 547 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 548 | BasicBlock *FromBB, BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 549 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 550 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 551 | // If we know the value is a constant, evaluate the conditional. |
| 552 | Constant *Res = 0; |
| 553 | if (Result.isConstant()) { |
| 554 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD); |
| 555 | if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res)) |
| 556 | return ResCI->isZero() ? False : True; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 557 | return Unknown; |
| 558 | } |
| 559 | |
| 560 | if (Result.isNotConstant()) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 561 | // If this is an equality comparison, we can try to fold it knowing that |
| 562 | // "V != C1". |
| 563 | if (Pred == ICmpInst::ICMP_EQ) { |
| 564 | // !C1 == C -> false iff C1 == C. |
| 565 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
| 566 | Result.getNotConstant(), C, TD); |
| 567 | if (Res->isNullValue()) |
| 568 | return False; |
| 569 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 570 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | 5553a3a | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 571 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 572 | Result.getNotConstant(), C, TD); |
| 573 | if (Res->isNullValue()) |
| 574 | return True; |
| 575 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 576 | return Unknown; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 579 | return Unknown; |
| 580 | } |
| 581 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 582 | |