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