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