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