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" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseSet.h" |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | #include "llvm/Analysis/AliasAnalysis.h" |
| 20 | #include "llvm/Analysis/ConstantFolding.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/DataLayout.h" |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 25 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CFG.h" |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ConstantRange.h" |
Chris Lattner | b8c124c | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 8979e5f | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 29 | #include "llvm/Support/PatternMatch.h" |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ValueHandle.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include "llvm/Target/TargetLibraryInfo.h" |
Bill Wendling | aa8b994 | 2012-01-11 23:43:34 +0000 | [diff] [blame] | 33 | #include <map> |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 34 | #include <stack> |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Benjamin Kramer | 8979e5f | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 36 | using namespace PatternMatch; |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 37 | |
| 38 | char LazyValueInfo::ID = 0; |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 39 | INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info", |
| 40 | "Lazy Value Information Analysis", false, true) |
| 41 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) |
| 42 | INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 43 | "Lazy Value Information Analysis", false, true) |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 44 | |
| 45 | namespace llvm { |
| 46 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); } |
| 47 | } |
| 48 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 49 | |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | // LVILatticeVal |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
| 54 | /// LVILatticeVal - This is the information tracked by LazyValueInfo for each |
| 55 | /// value. |
| 56 | /// |
| 57 | /// FIXME: This is basically just for bringup, this can be made a lot more rich |
| 58 | /// in the future. |
| 59 | /// |
| 60 | namespace { |
| 61 | class LVILatticeVal { |
| 62 | enum LatticeValueTy { |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 63 | /// undefined - This Value has no known value yet. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 64 | undefined, |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 65 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 66 | /// constant - This Value has a specific constant value. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 67 | constant, |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 68 | /// notconstant - This Value is known to not have the specified value. |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 69 | notconstant, |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 70 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 71 | /// constantrange - The Value falls within this range. |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 72 | constantrange, |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 73 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 74 | /// 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] | 75 | /// it has a value. |
| 76 | overdefined |
| 77 | }; |
| 78 | |
| 79 | /// Val: This stores the current lattice value along with the Constant* for |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 80 | /// the constant if this is a 'constant' or 'notconstant' value. |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 81 | LatticeValueTy Tag; |
| 82 | Constant *Val; |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 83 | ConstantRange Range; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 84 | |
| 85 | public: |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 86 | LVILatticeVal() : Tag(undefined), Val(0), Range(1, true) {} |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 88 | static LVILatticeVal get(Constant *C) { |
| 89 | LVILatticeVal Res; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 90 | if (!isa<UndefValue>(C)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 91 | Res.markConstant(C); |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 92 | return Res; |
| 93 | } |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 94 | static LVILatticeVal getNot(Constant *C) { |
| 95 | LVILatticeVal Res; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 96 | if (!isa<UndefValue>(C)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 97 | Res.markNotConstant(C); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 98 | return Res; |
| 99 | } |
Owen Anderson | 625051b | 2010-08-10 23:20:01 +0000 | [diff] [blame] | 100 | static LVILatticeVal getRange(ConstantRange CR) { |
| 101 | LVILatticeVal Res; |
| 102 | Res.markConstantRange(CR); |
| 103 | return Res; |
| 104 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 105 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 106 | bool isUndefined() const { return Tag == undefined; } |
| 107 | bool isConstant() const { return Tag == constant; } |
| 108 | bool isNotConstant() const { return Tag == notconstant; } |
| 109 | bool isConstantRange() const { return Tag == constantrange; } |
| 110 | bool isOverdefined() const { return Tag == overdefined; } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 111 | |
| 112 | Constant *getConstant() const { |
| 113 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 114 | return Val; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 117 | Constant *getNotConstant() const { |
| 118 | assert(isNotConstant() && "Cannot get the constant of a non-notconstant!"); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 119 | return Val; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 122 | ConstantRange getConstantRange() const { |
| 123 | assert(isConstantRange() && |
| 124 | "Cannot get the constant-range of a non-constant-range!"); |
| 125 | return Range; |
| 126 | } |
| 127 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 128 | /// markOverdefined - Return true if this is a change in status. |
| 129 | bool markOverdefined() { |
| 130 | if (isOverdefined()) |
| 131 | return false; |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 132 | Tag = overdefined; |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | /// markConstant - Return true if this is a change in status. |
| 137 | bool markConstant(Constant *V) { |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 138 | assert(V && "Marking constant with NULL"); |
| 139 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 140 | return markConstantRange(ConstantRange(CI->getValue())); |
| 141 | if (isa<UndefValue>(V)) |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 142 | return false; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 143 | |
| 144 | assert((!isConstant() || getConstant() == V) && |
| 145 | "Marking constant with different value"); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 146 | assert(isUndefined()); |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 147 | Tag = constant; |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 148 | Val = V; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 152 | /// markNotConstant - Return true if this is a change in status. |
| 153 | bool markNotConstant(Constant *V) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 154 | assert(V && "Marking constant with NULL"); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 155 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 156 | return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue())); |
| 157 | if (isa<UndefValue>(V)) |
| 158 | return false; |
| 159 | |
| 160 | assert((!isConstant() || getConstant() != V) && |
| 161 | "Marking constant !constant with same value"); |
| 162 | assert((!isNotConstant() || getNotConstant() == V) && |
| 163 | "Marking !constant with different value"); |
| 164 | assert(isUndefined() || isConstant()); |
| 165 | Tag = notconstant; |
Owen Anderson | db78d73 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 166 | Val = V; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 170 | /// markConstantRange - Return true if this is a change in status. |
| 171 | bool markConstantRange(const ConstantRange NewR) { |
| 172 | if (isConstantRange()) { |
| 173 | if (NewR.isEmptySet()) |
| 174 | return markOverdefined(); |
| 175 | |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 176 | bool changed = Range != NewR; |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 177 | Range = NewR; |
| 178 | return changed; |
| 179 | } |
| 180 | |
| 181 | assert(isUndefined()); |
| 182 | if (NewR.isEmptySet()) |
| 183 | return markOverdefined(); |
Owen Anderson | 5be2e78 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 184 | |
| 185 | Tag = constantrange; |
| 186 | Range = NewR; |
| 187 | return true; |
| 188 | } |
| 189 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 190 | /// mergeIn - Merge the specified lattice value into this one, updating this |
| 191 | /// one and returning true if anything changed. |
| 192 | bool mergeIn(const LVILatticeVal &RHS) { |
| 193 | if (RHS.isUndefined() || isOverdefined()) return false; |
| 194 | if (RHS.isOverdefined()) return markOverdefined(); |
| 195 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 196 | if (isUndefined()) { |
| 197 | Tag = RHS.Tag; |
| 198 | Val = RHS.Val; |
| 199 | Range = RHS.Range; |
| 200 | return true; |
Chris Lattner | f496e79 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 203 | if (isConstant()) { |
| 204 | if (RHS.isConstant()) { |
| 205 | if (Val == RHS.Val) |
| 206 | return false; |
| 207 | return markOverdefined(); |
| 208 | } |
| 209 | |
| 210 | if (RHS.isNotConstant()) { |
| 211 | if (Val == RHS.Val) |
| 212 | return markOverdefined(); |
| 213 | |
| 214 | // Unless we can prove that the two Constants are different, we must |
| 215 | // move to overdefined. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 216 | // FIXME: use DataLayout/TargetLibraryInfo for smarter constant folding. |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 217 | if (ConstantInt *Res = dyn_cast<ConstantInt>( |
| 218 | ConstantFoldCompareInstOperands(CmpInst::ICMP_NE, |
| 219 | getConstant(), |
| 220 | RHS.getNotConstant()))) |
| 221 | if (Res->isOne()) |
| 222 | return markNotConstant(RHS.getNotConstant()); |
| 223 | |
| 224 | return markOverdefined(); |
| 225 | } |
| 226 | |
| 227 | // RHS is a ConstantRange, LHS is a non-integer Constant. |
| 228 | |
| 229 | // FIXME: consider the case where RHS is a range [1, 0) and LHS is |
| 230 | // a function. The correct result is to pick up RHS. |
| 231 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 232 | return markOverdefined(); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (isNotConstant()) { |
| 236 | if (RHS.isConstant()) { |
| 237 | if (Val == RHS.Val) |
| 238 | return markOverdefined(); |
| 239 | |
| 240 | // Unless we can prove that the two Constants are different, we must |
| 241 | // move to overdefined. |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 242 | // FIXME: use DataLayout/TargetLibraryInfo for smarter constant folding. |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 243 | if (ConstantInt *Res = dyn_cast<ConstantInt>( |
| 244 | ConstantFoldCompareInstOperands(CmpInst::ICMP_NE, |
| 245 | getNotConstant(), |
| 246 | RHS.getConstant()))) |
| 247 | if (Res->isOne()) |
| 248 | return false; |
| 249 | |
| 250 | return markOverdefined(); |
| 251 | } |
| 252 | |
| 253 | if (RHS.isNotConstant()) { |
| 254 | if (Val == RHS.Val) |
| 255 | return false; |
| 256 | return markOverdefined(); |
| 257 | } |
| 258 | |
| 259 | return markOverdefined(); |
| 260 | } |
| 261 | |
| 262 | assert(isConstantRange() && "New LVILattice type?"); |
| 263 | if (!RHS.isConstantRange()) |
| 264 | return markOverdefined(); |
| 265 | |
| 266 | ConstantRange NewR = Range.unionWith(RHS.getConstantRange()); |
| 267 | if (NewR.isFullSet()) |
| 268 | return markOverdefined(); |
| 269 | return markConstantRange(NewR); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | } // end anonymous namespace. |
| 274 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 275 | namespace llvm { |
Chandler Carruth | 3b55a37 | 2011-04-18 18:49:44 +0000 | [diff] [blame] | 276 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) |
| 277 | LLVM_ATTRIBUTE_USED; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 278 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) { |
| 279 | if (Val.isUndefined()) |
| 280 | return OS << "undefined"; |
| 281 | if (Val.isOverdefined()) |
| 282 | return OS << "overdefined"; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 283 | |
| 284 | if (Val.isNotConstant()) |
| 285 | return OS << "notconstant<" << *Val.getNotConstant() << '>'; |
Owen Anderson | 2f3ffb8 | 2010-08-09 20:50:46 +0000 | [diff] [blame] | 286 | else if (Val.isConstantRange()) |
| 287 | return OS << "constantrange<" << Val.getConstantRange().getLower() << ", " |
| 288 | << Val.getConstantRange().getUpper() << '>'; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 289 | return OS << "constant<" << *Val.getConstant() << '>'; |
| 290 | } |
| 291 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 292 | |
| 293 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 294 | // LazyValueInfoCache Decl |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 295 | //===----------------------------------------------------------------------===// |
| 296 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 297 | namespace { |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 298 | /// LVIValueHandle - A callback value handle updates the cache when |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 299 | /// values are erased. |
| 300 | class LazyValueInfoCache; |
| 301 | struct LVIValueHandle : public CallbackVH { |
| 302 | LazyValueInfoCache *Parent; |
| 303 | |
| 304 | LVIValueHandle(Value *V, LazyValueInfoCache *P) |
| 305 | : CallbackVH(V), Parent(P) { } |
| 306 | |
| 307 | void deleted(); |
| 308 | void allUsesReplacedWith(Value *V) { |
| 309 | deleted(); |
| 310 | } |
| 311 | }; |
| 312 | } |
| 313 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 314 | namespace { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 315 | /// LazyValueInfoCache - This is the cache kept by LazyValueInfo which |
| 316 | /// maintains information about queries across the clients' queries. |
| 317 | class LazyValueInfoCache { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 318 | /// ValueCacheEntryTy - This is all of the cached block information for |
| 319 | /// exactly one Value*. The entries are sorted by the BasicBlock* of the |
| 320 | /// entries, allowing us to do a lookup with a binary search. |
Bill Wendling | aa8b994 | 2012-01-11 23:43:34 +0000 | [diff] [blame] | 321 | typedef std::map<AssertingVH<BasicBlock>, LVILatticeVal> ValueCacheEntryTy; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 322 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 323 | /// ValueCache - This is all of the cached information for all values, |
| 324 | /// mapped from Value* to key information. |
Bill Wendling | 805e224 | 2012-01-12 01:41:03 +0000 | [diff] [blame] | 325 | std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache; |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 326 | |
| 327 | /// OverDefinedCache - This tracks, on a per-block basis, the set of |
| 328 | /// values that are over-defined at the end of that block. This is required |
| 329 | /// for cache updating. |
| 330 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 331 | DenseSet<OverDefinedPairTy> OverDefinedCache; |
Benjamin Kramer | feb9b4b | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 332 | |
| 333 | /// SeenBlocks - Keep track of all blocks that we have ever seen, so we |
| 334 | /// don't spend time removing unused blocks from our caches. |
| 335 | DenseSet<AssertingVH<BasicBlock> > SeenBlocks; |
| 336 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 337 | /// BlockValueStack - This stack holds the state of the value solver |
| 338 | /// during a query. It basically emulates the callstack of the naive |
| 339 | /// recursive value lookup process. |
| 340 | std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack; |
| 341 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 342 | friend struct LVIValueHandle; |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 343 | |
| 344 | /// OverDefinedCacheUpdater - A helper object that ensures that the |
| 345 | /// OverDefinedCache is updated whenever solveBlockValue returns. |
| 346 | struct OverDefinedCacheUpdater { |
| 347 | LazyValueInfoCache *Parent; |
| 348 | Value *Val; |
| 349 | BasicBlock *BB; |
| 350 | LVILatticeVal &BBLV; |
| 351 | |
| 352 | OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV, |
| 353 | LazyValueInfoCache *P) |
| 354 | : Parent(P), Val(V), BB(B), BBLV(LV) { } |
| 355 | |
| 356 | bool markResult(bool changed) { |
| 357 | if (changed && BBLV.isOverdefined()) |
| 358 | Parent->OverDefinedCache.insert(std::make_pair(BB, Val)); |
| 359 | return changed; |
| 360 | } |
| 361 | }; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 362 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 363 | |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 364 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 365 | LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 366 | bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T, |
| 367 | LVILatticeVal &Result); |
| 368 | bool hasBlockValue(Value *Val, BasicBlock *BB); |
| 369 | |
| 370 | // These methods process one work item and may add more. A false value |
| 371 | // returned means that the work item was not completely processed and must |
| 372 | // be revisited after going through the new items. |
| 373 | bool solveBlockValue(Value *Val, BasicBlock *BB); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 374 | bool solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 375 | Value *Val, BasicBlock *BB); |
| 376 | bool solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 377 | PHINode *PN, BasicBlock *BB); |
| 378 | bool solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 379 | Instruction *BBI, BasicBlock *BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 380 | |
| 381 | void solve(); |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 382 | |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 383 | ValueCacheEntryTy &lookup(Value *V) { |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 384 | return ValueCache[LVIValueHandle(V, this)]; |
| 385 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 387 | public: |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 388 | /// getValueInBlock - This is the query interface to determine the lattice |
| 389 | /// value for the specified Value* at the end of the specified block. |
| 390 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB); |
| 391 | |
| 392 | /// getValueOnEdge - This is the query interface to determine the lattice |
| 393 | /// value for the specified Value* that is true on the specified edge. |
| 394 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 395 | |
| 396 | /// threadEdge - This is the update interface to inform the cache that an |
| 397 | /// edge from PredBB to OldSucc has been threaded to be from PredBB to |
| 398 | /// NewSucc. |
| 399 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 400 | |
| 401 | /// eraseBlock - This is part of the update interface to inform the cache |
| 402 | /// that a block has been deleted. |
| 403 | void eraseBlock(BasicBlock *BB); |
| 404 | |
| 405 | /// clear - Empty the cache. |
| 406 | void clear() { |
Benjamin Kramer | c00c05f | 2011-12-03 15:19:55 +0000 | [diff] [blame] | 407 | SeenBlocks.clear(); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 408 | ValueCache.clear(); |
| 409 | OverDefinedCache.clear(); |
| 410 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 411 | }; |
| 412 | } // end anonymous namespace |
| 413 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 414 | void LVIValueHandle::deleted() { |
| 415 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 416 | |
| 417 | SmallVector<OverDefinedPairTy, 4> ToErase; |
| 418 | for (DenseSet<OverDefinedPairTy>::iterator |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 419 | I = Parent->OverDefinedCache.begin(), |
| 420 | E = Parent->OverDefinedCache.end(); |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 421 | I != E; ++I) { |
| 422 | if (I->second == getValPtr()) |
| 423 | ToErase.push_back(*I); |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 424 | } |
Owen Anderson | cf6abd2 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 425 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 426 | for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(), |
| 427 | E = ToErase.end(); I != E; ++I) |
| 428 | Parent->OverDefinedCache.erase(*I); |
| 429 | |
Owen Anderson | cf6abd2 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 430 | // This erasure deallocates *this, so it MUST happen after we're done |
| 431 | // using any and all members of *this. |
| 432 | Parent->ValueCache.erase(*this); |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 435 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
Benjamin Kramer | feb9b4b | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 436 | // Shortcut if we have never seen this block. |
| 437 | DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB); |
| 438 | if (I == SeenBlocks.end()) |
| 439 | return; |
| 440 | SeenBlocks.erase(I); |
| 441 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 442 | SmallVector<OverDefinedPairTy, 4> ToErase; |
| 443 | for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(), |
| 444 | E = OverDefinedCache.end(); I != E; ++I) { |
| 445 | if (I->first == BB) |
| 446 | ToErase.push_back(*I); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 447 | } |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 448 | |
| 449 | for (SmallVector<OverDefinedPairTy, 4>::iterator I = ToErase.begin(), |
| 450 | E = ToErase.end(); I != E; ++I) |
| 451 | OverDefinedCache.erase(*I); |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 452 | |
Bill Wendling | 805e224 | 2012-01-12 01:41:03 +0000 | [diff] [blame] | 453 | for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 454 | I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I) |
| 455 | I->second.erase(BB); |
| 456 | } |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 457 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 458 | void LazyValueInfoCache::solve() { |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 459 | while (!BlockValueStack.empty()) { |
| 460 | std::pair<BasicBlock*, Value*> &e = BlockValueStack.top(); |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 461 | if (solveBlockValue(e.second, e.first)) { |
| 462 | assert(BlockValueStack.top() == e); |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 463 | BlockValueStack.pop(); |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 464 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) { |
| 469 | // If already a constant, there is nothing to compute. |
| 470 | if (isa<Constant>(Val)) |
| 471 | return true; |
| 472 | |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 473 | LVIValueHandle ValHandle(Val, this); |
Benjamin Kramer | 05d96f9 | 2012-08-22 15:37:57 +0000 | [diff] [blame] | 474 | std::map<LVIValueHandle, ValueCacheEntryTy>::iterator I = |
| 475 | ValueCache.find(ValHandle); |
| 476 | if (I == ValueCache.end()) return false; |
| 477 | return I->second.count(BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 480 | LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 481 | // If already a constant, there is nothing to compute. |
| 482 | if (Constant *VC = dyn_cast<Constant>(Val)) |
| 483 | return LVILatticeVal::get(VC); |
| 484 | |
Benjamin Kramer | feb9b4b | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 485 | SeenBlocks.insert(BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 486 | return lookup(Val)[BB]; |
| 487 | } |
| 488 | |
| 489 | bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) { |
| 490 | if (isa<Constant>(Val)) |
| 491 | return true; |
| 492 | |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 493 | ValueCacheEntryTy &Cache = lookup(Val); |
Benjamin Kramer | feb9b4b | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 494 | SeenBlocks.insert(BB); |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 495 | LVILatticeVal &BBLV = Cache[BB]; |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 496 | |
| 497 | // OverDefinedCacheUpdater is a helper object that will update |
| 498 | // the OverDefinedCache for us when this method exits. Make sure to |
| 499 | // call markResult on it as we exist, passing a bool to indicate if the |
| 500 | // cache needs updating, i.e. if we have solve a new value or not. |
| 501 | OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV, this); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 502 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 503 | // If we've already computed this block's value, return it. |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 504 | if (!BBLV.isUndefined()) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 505 | DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n'); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 506 | |
| 507 | // Since we're reusing a cached value here, we don't need to update the |
| 508 | // OverDefinedCahce. The cache will have been properly updated |
| 509 | // whenever the cached value was inserted. |
| 510 | ODCacheUpdater.markResult(false); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 511 | return true; |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 514 | // Otherwise, this is the first time we're seeing this block. Reset the |
| 515 | // lattice value to overdefined, so that cycles will terminate and be |
| 516 | // conservatively correct. |
| 517 | BBLV.markOverdefined(); |
| 518 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 519 | Instruction *BBI = dyn_cast<Instruction>(Val); |
| 520 | if (BBI == 0 || BBI->getParent() != BB) { |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 521 | return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB)); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 522 | } |
Chris Lattner | e564281 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 523 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 524 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 525 | return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 526 | } |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 527 | |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 528 | if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) { |
| 529 | BBLV = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType())); |
| 530 | return ODCacheUpdater.markResult(true); |
| 531 | } |
| 532 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 533 | // We can only analyze the definitions of certain classes of instructions |
| 534 | // (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] | 535 | LVILatticeVal Result; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 536 | if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) || |
| 537 | !BBI->getType()->isIntegerTy()) { |
| 538 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 539 | << "' - overdefined because inst def found.\n"); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 540 | BBLV.markOverdefined(); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 541 | return ODCacheUpdater.markResult(true); |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 542 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 543 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 544 | // FIXME: We're currently limited to binops with a constant RHS. This should |
| 545 | // be improved. |
| 546 | BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI); |
| 547 | if (BO && !isa<ConstantInt>(BO->getOperand(1))) { |
| 548 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 549 | << "' - overdefined because inst def found.\n"); |
| 550 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 551 | BBLV.markOverdefined(); |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 552 | return ODCacheUpdater.markResult(true); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 553 | } |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 554 | |
Owen Anderson | 87790ab | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 555 | return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) { |
| 559 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 560 | return L->getPointerAddressSpace() == 0 && |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 561 | GetUnderlyingObject(L->getPointerOperand()) == Ptr; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 562 | } |
| 563 | if (StoreInst *S = dyn_cast<StoreInst>(I)) { |
| 564 | return S->getPointerAddressSpace() == 0 && |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 565 | GetUnderlyingObject(S->getPointerOperand()) == Ptr; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 566 | } |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 567 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 568 | if (MI->isVolatile()) return false; |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 569 | |
| 570 | // FIXME: check whether it has a valuerange that excludes zero? |
| 571 | ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 572 | if (!Len || Len->isZero()) return false; |
| 573 | |
Eli Friedman | 69388e5 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 574 | if (MI->getDestAddressSpace() == 0) |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 575 | if (GetUnderlyingObject(MI->getRawDest()) == Ptr) |
Eli Friedman | 69388e5 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 576 | return true; |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 577 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
Eli Friedman | 69388e5 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 578 | if (MTI->getSourceAddressSpace() == 0) |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 579 | if (GetUnderlyingObject(MTI->getRawSource()) == Ptr) |
Eli Friedman | 69388e5 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 580 | return true; |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 581 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 582 | return false; |
| 583 | } |
| 584 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 585 | bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 586 | Value *Val, BasicBlock *BB) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 587 | LVILatticeVal Result; // Start Undefined. |
| 588 | |
| 589 | // If this is a pointer, and there's a load from that pointer in this BB, |
| 590 | // then we know that the pointer can't be NULL. |
| 591 | bool NotNull = false; |
| 592 | if (Val->getType()->isPointerTy()) { |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 593 | if (isKnownNonNull(Val)) { |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 594 | NotNull = true; |
| 595 | } else { |
Nick Lewycky | 4734fe1 | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 596 | Value *UnderlyingVal = GetUnderlyingObject(Val); |
| 597 | // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge |
| 598 | // inside InstructionDereferencesPointer either. |
| 599 | if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, NULL, 1)) { |
| 600 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); |
| 601 | BI != BE; ++BI) { |
| 602 | if (InstructionDereferencesPointer(BI, UnderlyingVal)) { |
| 603 | NotNull = true; |
| 604 | break; |
| 605 | } |
Nick Lewycky | 786c7cd | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 606 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // If this is the entry block, we must be asking about an argument. The |
| 612 | // value is overdefined. |
| 613 | if (BB == &BB->getParent()->getEntryBlock()) { |
| 614 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 615 | if (NotNull) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 616 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 617 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 618 | } else { |
| 619 | Result.markOverdefined(); |
| 620 | } |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 621 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | |
| 625 | // Loop over all of our predecessors, merging what we know from them into |
| 626 | // result. |
| 627 | bool EdgesMissing = false; |
| 628 | for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { |
| 629 | LVILatticeVal EdgeResult; |
| 630 | EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult); |
| 631 | if (EdgesMissing) |
| 632 | continue; |
| 633 | |
| 634 | Result.mergeIn(EdgeResult); |
| 635 | |
| 636 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 637 | // to overdefined. |
| 638 | if (Result.isOverdefined()) { |
| 639 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 640 | << "' - overdefined because of pred.\n"); |
| 641 | // If we previously determined that this is a pointer that can't be null |
| 642 | // then return that rather than giving up entirely. |
| 643 | if (NotNull) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 644 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 645 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 646 | } |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 647 | |
| 648 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 649 | return true; |
| 650 | } |
| 651 | } |
| 652 | if (EdgesMissing) |
| 653 | return false; |
| 654 | |
| 655 | // Return the merged value, which is more precise than 'overdefined'. |
| 656 | assert(!Result.isOverdefined()); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 657 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 658 | return true; |
| 659 | } |
| 660 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 661 | bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 662 | PHINode *PN, BasicBlock *BB) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 663 | LVILatticeVal Result; // Start Undefined. |
| 664 | |
| 665 | // Loop over all of our predecessors, merging what we know from them into |
| 666 | // result. |
| 667 | bool EdgesMissing = false; |
| 668 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 669 | BasicBlock *PhiBB = PN->getIncomingBlock(i); |
| 670 | Value *PhiVal = PN->getIncomingValue(i); |
| 671 | LVILatticeVal EdgeResult; |
| 672 | EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult); |
| 673 | if (EdgesMissing) |
| 674 | continue; |
| 675 | |
| 676 | Result.mergeIn(EdgeResult); |
| 677 | |
| 678 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 679 | // to overdefined. |
| 680 | if (Result.isOverdefined()) { |
| 681 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 682 | << "' - overdefined because of pred.\n"); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 683 | |
| 684 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 685 | return true; |
| 686 | } |
| 687 | } |
| 688 | if (EdgesMissing) |
| 689 | return false; |
| 690 | |
| 691 | // Return the merged value, which is more precise than 'overdefined'. |
| 692 | assert(!Result.isOverdefined() && "Possible PHI in entry block?"); |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 693 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 694 | return true; |
| 695 | } |
| 696 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 697 | bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 698 | Instruction *BBI, |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 699 | BasicBlock *BB) { |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 700 | // Figure out the range of the LHS. If that fails, bail. |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 701 | if (!hasBlockValue(BBI->getOperand(0), BB)) { |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 702 | BlockValueStack.push(std::make_pair(BB, BBI->getOperand(0))); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 703 | return false; |
| 704 | } |
| 705 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 706 | LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB); |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 707 | if (!LHSVal.isConstantRange()) { |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 708 | BBLV.markOverdefined(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 709 | return true; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 712 | ConstantRange LHSRange = LHSVal.getConstantRange(); |
| 713 | ConstantRange RHSRange(1); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 714 | IntegerType *ResultTy = cast<IntegerType>(BBI->getType()); |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 715 | if (isa<BinaryOperator>(BBI)) { |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 716 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) { |
| 717 | RHSRange = ConstantRange(RHS->getValue()); |
| 718 | } else { |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 719 | BBLV.markOverdefined(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 720 | return true; |
Owen Anderson | 59b06dc | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 721 | } |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 722 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 723 | |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 724 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 725 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 726 | // more definitions. |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 727 | LVILatticeVal Result; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 728 | switch (BBI->getOpcode()) { |
| 729 | case Instruction::Add: |
| 730 | Result.markConstantRange(LHSRange.add(RHSRange)); |
| 731 | break; |
| 732 | case Instruction::Sub: |
| 733 | Result.markConstantRange(LHSRange.sub(RHSRange)); |
| 734 | break; |
| 735 | case Instruction::Mul: |
| 736 | Result.markConstantRange(LHSRange.multiply(RHSRange)); |
| 737 | break; |
| 738 | case Instruction::UDiv: |
| 739 | Result.markConstantRange(LHSRange.udiv(RHSRange)); |
| 740 | break; |
| 741 | case Instruction::Shl: |
| 742 | Result.markConstantRange(LHSRange.shl(RHSRange)); |
| 743 | break; |
| 744 | case Instruction::LShr: |
| 745 | Result.markConstantRange(LHSRange.lshr(RHSRange)); |
| 746 | break; |
| 747 | case Instruction::Trunc: |
| 748 | Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth())); |
| 749 | break; |
| 750 | case Instruction::SExt: |
| 751 | Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth())); |
| 752 | break; |
| 753 | case Instruction::ZExt: |
| 754 | Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth())); |
| 755 | break; |
| 756 | case Instruction::BitCast: |
| 757 | Result.markConstantRange(LHSRange); |
| 758 | break; |
Nick Lewycky | 198381e | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 759 | case Instruction::And: |
| 760 | Result.markConstantRange(LHSRange.binaryAnd(RHSRange)); |
| 761 | break; |
| 762 | case Instruction::Or: |
| 763 | Result.markConstantRange(LHSRange.binaryOr(RHSRange)); |
| 764 | break; |
Owen Anderson | b81fd62 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 765 | |
| 766 | // Unhandled instructions are overdefined. |
| 767 | default: |
| 768 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 769 | << "' - overdefined because inst def found.\n"); |
| 770 | Result.markOverdefined(); |
| 771 | break; |
| 772 | } |
| 773 | |
Owen Anderson | 6186394 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 774 | BBLV = Result; |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 775 | return true; |
Chris Lattner | 10f2d13 | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 778 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if |
| 779 | /// Val is not constrained on the edge. |
| 780 | static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, |
| 781 | BasicBlock *BBTo, LVILatticeVal &Result) { |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 782 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 783 | // know that v != 0. |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 784 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 785 | // If this is a conditional branch and only one successor goes to BBTo, then |
| 786 | // we maybe able to infer something from the condition. |
| 787 | if (BI->isConditional() && |
| 788 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 789 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 790 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 791 | "BBTo isn't a successor of BBFrom"); |
| 792 | |
| 793 | // If V is the condition of the branch itself, then we know exactly what |
| 794 | // it is. |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 795 | if (BI->getCondition() == Val) { |
| 796 | Result = LVILatticeVal::get(ConstantInt::get( |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 797 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 798 | return true; |
| 799 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 800 | |
| 801 | // If the condition of the branch is an equality comparison, we may be |
| 802 | // able to infer the value. |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 803 | ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()); |
Benjamin Kramer | 8979e5f | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 804 | if (ICI && isa<Constant>(ICI->getOperand(1))) { |
| 805 | if (ICI->isEquality() && ICI->getOperand(0) == Val) { |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 806 | // We know that V has the RHS constant if this is a true SETEQ or |
| 807 | // false SETNE. |
| 808 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 809 | Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
| 810 | else |
| 811 | Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
| 812 | return true; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 813 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 814 | |
Benjamin Kramer | 8979e5f | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 815 | // Recognize the range checking idiom that InstCombine produces. |
| 816 | // (X-C1) u< C2 --> [C1, C1+C2) |
| 817 | ConstantInt *NegOffset = 0; |
| 818 | if (ICI->getPredicate() == ICmpInst::ICMP_ULT) |
| 819 | match(ICI->getOperand(0), m_Add(m_Specific(Val), |
| 820 | m_ConstantInt(NegOffset))); |
| 821 | |
| 822 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1)); |
| 823 | if (CI && (ICI->getOperand(0) == Val || NegOffset)) { |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 824 | // Calculate the range of values that would satisfy the comparison. |
Nuno Lopes | 97f87ab | 2012-05-17 23:04:08 +0000 | [diff] [blame] | 825 | ConstantRange CmpRange(CI->getValue()); |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 826 | ConstantRange TrueValues = |
| 827 | ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 828 | |
Benjamin Kramer | 8979e5f | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 829 | if (NegOffset) // Apply the offset from above. |
| 830 | TrueValues = TrueValues.subtract(NegOffset->getValue()); |
| 831 | |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 832 | // If we're interested in the false dest, invert the condition. |
| 833 | if (!isTrueDest) TrueValues = TrueValues.inverse(); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 834 | |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 835 | Result = LVILatticeVal::getRange(TrueValues); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 836 | return true; |
Owen Anderson | 2d0f247 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 841 | |
| 842 | // If the edge was formed by a switch on the value, then we may know exactly |
| 843 | // what it is. |
| 844 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
Nuno Lopes | e504877 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 845 | if (SI->getCondition() != Val) |
| 846 | return false; |
| 847 | |
| 848 | bool DefaultCase = SI->getDefaultDest() == BBTo; |
| 849 | unsigned BitWidth = Val->getType()->getIntegerBitWidth(); |
| 850 | ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/); |
| 851 | |
| 852 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
| 853 | i != e; ++i) { |
| 854 | ConstantRange EdgeVal(i.getCaseValue()->getValue()); |
Manman Ren | 408853e | 2012-09-05 23:45:58 +0000 | [diff] [blame] | 855 | if (DefaultCase) { |
| 856 | // It is possible that the default destination is the destination of |
| 857 | // some cases. There is no need to perform difference for those cases. |
| 858 | if (i.getCaseSuccessor() != BBTo) |
| 859 | EdgesVals = EdgesVals.difference(EdgeVal); |
| 860 | } else if (i.getCaseSuccessor() == BBTo) |
Nuno Lopes | 90255a8 | 2012-05-18 21:02:10 +0000 | [diff] [blame] | 861 | EdgesVals = EdgesVals.unionWith(EdgeVal); |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 862 | } |
Nuno Lopes | e504877 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 863 | Result = LVILatticeVal::getRange(EdgesVals); |
| 864 | return true; |
Chris Lattner | 800c47e | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 865 | } |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 866 | return false; |
| 867 | } |
| 868 | |
| 869 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo, or the value at |
| 870 | /// the basic block if the edge does not constraint Val. |
| 871 | bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom, |
| 872 | BasicBlock *BBTo, LVILatticeVal &Result) { |
| 873 | // If already a constant, there is nothing to compute. |
| 874 | if (Constant *VC = dyn_cast<Constant>(Val)) { |
| 875 | Result = LVILatticeVal::get(VC); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 876 | return true; |
| 877 | } |
Nuno Lopes | e441394 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 878 | |
| 879 | if (getEdgeValueLocal(Val, BBFrom, BBTo, Result)) { |
| 880 | if (!Result.isConstantRange() || |
| 881 | Result.getConstantRange().getSingleElement()) |
| 882 | return true; |
| 883 | |
| 884 | // FIXME: this check should be moved to the beginning of the function when |
| 885 | // LVI better supports recursive values. Even for the single value case, we |
| 886 | // can intersect to detect dead code (an empty range). |
| 887 | if (!hasBlockValue(Val, BBFrom)) { |
| 888 | BlockValueStack.push(std::make_pair(BBFrom, Val)); |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | // Try to intersect ranges of the BB and the constraint on the edge. |
| 893 | LVILatticeVal InBlock = getBlockValue(Val, BBFrom); |
| 894 | if (!InBlock.isConstantRange()) |
| 895 | return true; |
| 896 | |
| 897 | ConstantRange Range = |
| 898 | Result.getConstantRange().intersectWith(InBlock.getConstantRange()); |
| 899 | Result = LVILatticeVal::getRange(Range); |
| 900 | return true; |
| 901 | } |
| 902 | |
| 903 | if (!hasBlockValue(Val, BBFrom)) { |
| 904 | BlockValueStack.push(std::make_pair(BBFrom, Val)); |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | // if we couldn't compute the value on the edge, use the value from the BB |
| 909 | Result = getBlockValue(Val, BBFrom); |
| 910 | return true; |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 913 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 914 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 915 | << BB->getName() << "'\n"); |
| 916 | |
Owen Anderson | e68713a | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 917 | BlockValueStack.push(std::make_pair(BB, V)); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 918 | solve(); |
Owen Anderson | f33b302 | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 919 | LVILatticeVal Result = getBlockValue(V, BB); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 920 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 921 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 922 | return Result; |
| 923 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 924 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 925 | LVILatticeVal LazyValueInfoCache:: |
| 926 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB) { |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 927 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 928 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 929 | |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 930 | LVILatticeVal Result; |
| 931 | if (!getEdgeValue(V, FromBB, ToBB, Result)) { |
| 932 | solve(); |
| 933 | bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result); |
| 934 | (void)WasFastQuery; |
| 935 | assert(WasFastQuery && "More work to do after problem solved?"); |
| 936 | } |
| 937 | |
David Greene | 5d93a1f | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 938 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 939 | return Result; |
| 940 | } |
| 941 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 942 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 943 | BasicBlock *NewSucc) { |
| 944 | // When an edge in the graph has been threaded, values that we could not |
| 945 | // determine a value for before (i.e. were marked overdefined) may be possible |
| 946 | // to solve now. We do NOT try to proactively update these values. Instead, |
| 947 | // we clear their entries from the cache, and allow lazy updating to recompute |
| 948 | // them when needed. |
| 949 | |
| 950 | // The updating process is fairly simple: we need to dropped cached info |
| 951 | // for all values that were marked overdefined in OldSucc, and for those same |
| 952 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 953 | // also marked overdefined. |
| 954 | std::vector<BasicBlock*> worklist; |
| 955 | worklist.push_back(OldSucc); |
| 956 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 957 | DenseSet<Value*> ClearSet; |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 958 | for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(), |
| 959 | E = OverDefinedCache.end(); I != E; ++I) { |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 960 | if (I->first == OldSucc) |
| 961 | ClearSet.insert(I->second); |
| 962 | } |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 963 | |
| 964 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 965 | // NOTE: We do not need a visited list since any blocks we have already |
| 966 | // visited will have had their overdefined markers cleared already, and we |
| 967 | // thus won't loop to their successors. |
| 968 | while (!worklist.empty()) { |
| 969 | BasicBlock *ToUpdate = worklist.back(); |
| 970 | worklist.pop_back(); |
| 971 | |
| 972 | // Skip blocks only accessible through NewSucc. |
| 973 | if (ToUpdate == NewSucc) continue; |
| 974 | |
| 975 | bool changed = false; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 976 | for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end(); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 977 | I != E; ++I) { |
| 978 | // If a value was marked overdefined in OldSucc, and is here too... |
Owen Anderson | 8977846 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 979 | DenseSet<OverDefinedPairTy>::iterator OI = |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 980 | OverDefinedCache.find(std::make_pair(ToUpdate, *I)); |
| 981 | if (OI == OverDefinedCache.end()) continue; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 982 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 983 | // Remove it from the caches. |
Owen Anderson | 7f9cb74 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 984 | ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)]; |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 985 | ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate); |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 986 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 987 | assert(CI != Entry.end() && "Couldn't find entry to update?"); |
| 988 | Entry.erase(CI); |
| 989 | OverDefinedCache.erase(OI); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 990 | |
Owen Anderson | 9a65dc9 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 991 | // If we removed anything, then we potentially need to update |
| 992 | // blocks successors too. |
| 993 | changed = true; |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 994 | } |
Nick Lewycky | 90862ee | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 995 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 996 | if (!changed) continue; |
| 997 | |
| 998 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 999 | } |
| 1000 | } |
| 1001 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1002 | //===----------------------------------------------------------------------===// |
| 1003 | // LazyValueInfo Impl |
| 1004 | //===----------------------------------------------------------------------===// |
| 1005 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1006 | /// getCache - This lazily constructs the LazyValueInfoCache. |
| 1007 | static LazyValueInfoCache &getCache(void *&PImpl) { |
| 1008 | if (!PImpl) |
| 1009 | PImpl = new LazyValueInfoCache(); |
| 1010 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 1011 | } |
| 1012 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1013 | bool LazyValueInfo::runOnFunction(Function &F) { |
| 1014 | if (PImpl) |
| 1015 | getCache(PImpl).clear(); |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1016 | |
Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 1017 | TD = getAnalysisIfAvailable<DataLayout>(); |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1018 | TLI = &getAnalysis<TargetLibraryInfo>(); |
| 1019 | |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1020 | // Fully lazy. |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1024 | void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1025 | AU.setPreservesAll(); |
| 1026 | AU.addRequired<TargetLibraryInfo>(); |
| 1027 | } |
| 1028 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1029 | void LazyValueInfo::releaseMemory() { |
| 1030 | // If the cache was allocated, free it. |
| 1031 | if (PImpl) { |
| 1032 | delete &getCache(PImpl); |
| 1033 | PImpl = 0; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB) { |
| 1038 | LVILatticeVal Result = getCache(PImpl).getValueInBlock(V, BB); |
| 1039 | |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1040 | if (Result.isConstant()) |
| 1041 | return Result.getConstant(); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1042 | if (Result.isConstantRange()) { |
Owen Anderson | ee61fcf | 2010-08-27 23:29:38 +0000 | [diff] [blame] | 1043 | ConstantRange CR = Result.getConstantRange(); |
| 1044 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1045 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1046 | } |
Chris Lattner | 1697652 | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1050 | /// getConstantOnEdge - Determine whether the specified value is known to be a |
| 1051 | /// constant on the specified edge. Return null if not. |
| 1052 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
| 1053 | BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1054 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1055 | |
| 1056 | if (Result.isConstant()) |
| 1057 | return Result.getConstant(); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1058 | if (Result.isConstantRange()) { |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1059 | ConstantRange CR = Result.getConstantRange(); |
| 1060 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1061 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1062 | } |
Chris Lattner | 38392bb | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1063 | return 0; |
| 1064 | } |
| 1065 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1066 | /// getPredicateOnEdge - Determine whether the specified value comparison |
| 1067 | /// with a constant is known to be true or false on the specified CFG edge. |
| 1068 | /// Pred is a CmpInst predicate. |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1069 | LazyValueInfo::Tristate |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1070 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 1071 | BasicBlock *FromBB, BasicBlock *ToBB) { |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1072 | LVILatticeVal Result = getCache(PImpl).getValueOnEdge(V, FromBB, ToBB); |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1073 | |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1074 | // If we know the value is a constant, evaluate the conditional. |
| 1075 | Constant *Res = 0; |
| 1076 | if (Result.isConstant()) { |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1077 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, TD, |
| 1078 | TLI); |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1079 | if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res)) |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1080 | return ResCI->isZero() ? False : True; |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1081 | return Unknown; |
| 1082 | } |
| 1083 | |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1084 | if (Result.isConstantRange()) { |
Owen Anderson | 59b06dc | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1085 | ConstantInt *CI = dyn_cast<ConstantInt>(C); |
| 1086 | if (!CI) return Unknown; |
| 1087 | |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1088 | ConstantRange CR = Result.getConstantRange(); |
| 1089 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1090 | if (!CR.contains(CI->getValue())) |
| 1091 | return False; |
| 1092 | |
| 1093 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
| 1094 | return True; |
| 1095 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1096 | if (!CR.contains(CI->getValue())) |
| 1097 | return True; |
| 1098 | |
| 1099 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
| 1100 | return False; |
| 1101 | } |
| 1102 | |
| 1103 | // Handle more complex predicates. |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1104 | ConstantRange TrueValues = |
| 1105 | ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue()); |
| 1106 | if (TrueValues.contains(CR)) |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1107 | return True; |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1108 | if (TrueValues.inverse().contains(CR)) |
| 1109 | return False; |
Owen Anderson | 9f01406 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1110 | return Unknown; |
| 1111 | } |
| 1112 | |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1113 | if (Result.isNotConstant()) { |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1114 | // If this is an equality comparison, we can try to fold it knowing that |
| 1115 | // "V != C1". |
| 1116 | if (Pred == ICmpInst::ICMP_EQ) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1117 | // !C1 == C -> false iff C1 == C. |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1118 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1119 | Result.getNotConstant(), C, TD, |
| 1120 | TLI); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1121 | if (Res->isNullValue()) |
| 1122 | return False; |
| 1123 | } else if (Pred == ICmpInst::ICMP_NE) { |
Sylvestre Ledru | 94c2271 | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1124 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | 5553a3a | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 1125 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Chad Rosier | aab8e28 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1126 | Result.getNotConstant(), C, TD, |
| 1127 | TLI); |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1128 | if (Res->isNullValue()) |
| 1129 | return True; |
| 1130 | } |
Chris Lattner | 2c5adf8 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1131 | return Unknown; |
Chris Lattner | b52675b | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Chris Lattner | cc4d3b2 | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1134 | return Unknown; |
| 1135 | } |
| 1136 | |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1137 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Nick Lewycky | 69bfdf5 | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1138 | BasicBlock *NewSucc) { |
Owen Anderson | 00ac77e | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1139 | if (PImpl) getCache(PImpl).threadEdge(PredBB, OldSucc, NewSucc); |
| 1140 | } |
| 1141 | |
| 1142 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
| 1143 | if (PImpl) getCache(PImpl).eraseBlock(BB); |
Owen Anderson | cfa7fb6 | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1144 | } |