Chris Lattner | 741c94c | 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 | |
| 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" |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AssumptionTracker.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) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 43 | INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) |
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 | |
| 345 | /// A pointer to the cache of @llvm.assume calls. |
| 346 | AssumptionTracker *AT; |
| 347 | /// An optional DL pointer. |
| 348 | const DataLayout *DL; |
| 349 | /// An optional DT pointer. |
| 350 | DominatorTree *DT; |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 351 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 352 | friend struct LVIValueHandle; |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 353 | |
| 354 | /// OverDefinedCacheUpdater - A helper object that ensures that the |
| 355 | /// OverDefinedCache is updated whenever solveBlockValue returns. |
| 356 | struct OverDefinedCacheUpdater { |
| 357 | LazyValueInfoCache *Parent; |
| 358 | Value *Val; |
| 359 | BasicBlock *BB; |
| 360 | LVILatticeVal &BBLV; |
| 361 | |
| 362 | OverDefinedCacheUpdater(Value *V, BasicBlock *B, LVILatticeVal &LV, |
| 363 | LazyValueInfoCache *P) |
| 364 | : Parent(P), Val(V), BB(B), BBLV(LV) { } |
| 365 | |
| 366 | bool markResult(bool changed) { |
| 367 | if (changed && BBLV.isOverdefined()) |
| 368 | Parent->OverDefinedCache.insert(std::make_pair(BB, Val)); |
| 369 | return changed; |
| 370 | } |
| 371 | }; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 372 | |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 373 | |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 374 | |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 375 | LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 376 | bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 377 | LVILatticeVal &Result, |
| 378 | Instruction *CxtI = nullptr); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 379 | bool hasBlockValue(Value *Val, BasicBlock *BB); |
| 380 | |
| 381 | // These methods process one work item and may add more. A false value |
| 382 | // returned means that the work item was not completely processed and must |
| 383 | // be revisited after going through the new items. |
| 384 | bool solveBlockValue(Value *Val, BasicBlock *BB); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 385 | bool solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 386 | Value *Val, BasicBlock *BB); |
| 387 | bool solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 388 | PHINode *PN, BasicBlock *BB); |
| 389 | bool solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 390 | Instruction *BBI, BasicBlock *BB); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 391 | void mergeAssumeBlockValueConstantRange(Value *Val, LVILatticeVal &BBLV, |
| 392 | Instruction *BBI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 393 | |
| 394 | void solve(); |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 395 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 396 | ValueCacheEntryTy &lookup(Value *V) { |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 397 | return ValueCache[LVIValueHandle(V, this)]; |
| 398 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 399 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 400 | public: |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 401 | /// getValueInBlock - This is the query interface to determine the lattice |
| 402 | /// value for the specified Value* at the end of the specified block. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 403 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB, |
| 404 | Instruction *CxtI = nullptr); |
| 405 | |
| 406 | /// getValueAt - This is the query interface to determine the lattice |
| 407 | /// value for the specified Value* at the specified instruction (generally |
| 408 | /// from an assume intrinsic). |
| 409 | LVILatticeVal getValueAt(Value *V, Instruction *CxtI); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 410 | |
| 411 | /// getValueOnEdge - This is the query interface to determine the lattice |
| 412 | /// value for the specified Value* that is true on the specified edge. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 413 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB, |
| 414 | Instruction *CxtI = nullptr); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 415 | |
| 416 | /// threadEdge - This is the update interface to inform the cache that an |
| 417 | /// edge from PredBB to OldSucc has been threaded to be from PredBB to |
| 418 | /// NewSucc. |
| 419 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 420 | |
| 421 | /// eraseBlock - This is part of the update interface to inform the cache |
| 422 | /// that a block has been deleted. |
| 423 | void eraseBlock(BasicBlock *BB); |
| 424 | |
| 425 | /// clear - Empty the cache. |
| 426 | void clear() { |
Benjamin Kramer | bbf3c60 | 2011-12-03 15:19:55 +0000 | [diff] [blame] | 427 | SeenBlocks.clear(); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 428 | ValueCache.clear(); |
| 429 | OverDefinedCache.clear(); |
| 430 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 431 | |
| 432 | LazyValueInfoCache(AssumptionTracker *AT, |
| 433 | const DataLayout *DL = nullptr, |
| 434 | DominatorTree *DT = nullptr) : AT(AT), DL(DL), DT(DT) {} |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 435 | }; |
| 436 | } // end anonymous namespace |
| 437 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 438 | void LVIValueHandle::deleted() { |
| 439 | typedef std::pair<AssertingVH<BasicBlock>, Value*> OverDefinedPairTy; |
| 440 | |
| 441 | SmallVector<OverDefinedPairTy, 4> ToErase; |
| 442 | for (DenseSet<OverDefinedPairTy>::iterator |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 443 | I = Parent->OverDefinedCache.begin(), |
| 444 | E = Parent->OverDefinedCache.end(); |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 445 | I != E; ++I) { |
| 446 | if (I->second == getValPtr()) |
| 447 | ToErase.push_back(*I); |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 448 | } |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 449 | |
| 450 | for (SmallVectorImpl<OverDefinedPairTy>::iterator I = ToErase.begin(), |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 451 | E = ToErase.end(); I != E; ++I) |
| 452 | Parent->OverDefinedCache.erase(*I); |
| 453 | |
Owen Anderson | 7b974a4 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 454 | // This erasure deallocates *this, so it MUST happen after we're done |
| 455 | // using any and all members of *this. |
| 456 | Parent->ValueCache.erase(*this); |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 459 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 460 | // Shortcut if we have never seen this block. |
| 461 | DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB); |
| 462 | if (I == SeenBlocks.end()) |
| 463 | return; |
| 464 | SeenBlocks.erase(I); |
| 465 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 466 | SmallVector<OverDefinedPairTy, 4> ToErase; |
| 467 | for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(), |
| 468 | E = OverDefinedCache.end(); I != E; ++I) { |
| 469 | if (I->first == BB) |
| 470 | ToErase.push_back(*I); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 471 | } |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 472 | |
| 473 | for (SmallVectorImpl<OverDefinedPairTy>::iterator I = ToErase.begin(), |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 474 | E = ToErase.end(); I != E; ++I) |
| 475 | OverDefinedCache.erase(*I); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 476 | |
Bill Wendling | 58c7569 | 2012-01-12 01:41:03 +0000 | [diff] [blame] | 477 | for (std::map<LVIValueHandle, ValueCacheEntryTy>::iterator |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 478 | I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I) |
| 479 | I->second.erase(BB); |
| 480 | } |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 481 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 482 | void LazyValueInfoCache::solve() { |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 483 | while (!BlockValueStack.empty()) { |
| 484 | std::pair<BasicBlock*, Value*> &e = BlockValueStack.top(); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 485 | if (solveBlockValue(e.second, e.first)) { |
| 486 | assert(BlockValueStack.top() == e); |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 487 | BlockValueStack.pop(); |
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 | |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 517 | ValueCacheEntryTy &Cache = lookup(Val); |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 518 | SeenBlocks.insert(BB); |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 519 | LVILatticeVal &BBLV = Cache[BB]; |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 520 | |
| 521 | // OverDefinedCacheUpdater is a helper object that will update |
| 522 | // the OverDefinedCache for us when this method exits. Make sure to |
| 523 | // call markResult on it as we exist, passing a bool to indicate if the |
| 524 | // cache needs updating, i.e. if we have solve a new value or not. |
| 525 | OverDefinedCacheUpdater ODCacheUpdater(Val, BB, BBLV, this); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 526 | |
Jiangning Liu | 40b04fd | 2014-08-11 05:02:04 +0000 | [diff] [blame] | 527 | // Once this BB is encountered, Val's value for this BB will not be Undefined |
| 528 | // any longer. When we encounter this BB again, if Val's value is Overdefined, |
| 529 | // we need to compute its value again. |
| 530 | // |
| 531 | // For example, considering this control flow, |
| 532 | // BB1->BB2, BB1->BB3, BB2->BB3, BB2->BB4 |
| 533 | // |
| 534 | // Suppose we have "icmp slt %v, 0" in BB1, and "icmp sgt %v, 0" in BB3. At |
| 535 | // the very beginning, when analyzing edge BB2->BB3, we don't know %v's value |
| 536 | // in BB2, and the data flow algorithm tries to compute BB2's predecessors, so |
| 537 | // then we know %v has negative value on edge BB1->BB2. And then we return to |
| 538 | // check BB2 again, and at this moment BB2 has Overdefined value for %v in |
| 539 | // BB2. So we should have to follow data flow propagation algorithm to get the |
| 540 | // value on edge BB1->BB2 propagated to BB2, and finally %v on BB2 has a |
| 541 | // constant range describing a negative value. |
| 542 | |
| 543 | if (!BBLV.isUndefined() && !BBLV.isOverdefined()) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 544 | DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n'); |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 545 | |
| 546 | // Since we're reusing a cached value here, we don't need to update the |
| 547 | // OverDefinedCahce. The cache will have been properly updated |
| 548 | // whenever the cached value was inserted. |
| 549 | ODCacheUpdater.markResult(false); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 550 | return true; |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 553 | // Otherwise, this is the first time we're seeing this block. Reset the |
| 554 | // lattice value to overdefined, so that cycles will terminate and be |
| 555 | // conservatively correct. |
| 556 | BBLV.markOverdefined(); |
| 557 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 558 | Instruction *BBI = dyn_cast<Instruction>(Val); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 559 | if (!BBI || BBI->getParent() != BB) { |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 560 | return ODCacheUpdater.markResult(solveBlockValueNonLocal(BBLV, Val, BB)); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 561 | } |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 562 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 563 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 564 | return ODCacheUpdater.markResult(solveBlockValuePHINode(BBLV, PN, BB)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 565 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 566 | |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 567 | if (AllocaInst *AI = dyn_cast<AllocaInst>(BBI)) { |
| 568 | BBLV = LVILatticeVal::getNot(ConstantPointerNull::get(AI->getType())); |
| 569 | return ODCacheUpdater.markResult(true); |
| 570 | } |
| 571 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 572 | // We can only analyze the definitions of certain classes of instructions |
| 573 | // (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] | 574 | LVILatticeVal Result; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 575 | if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) || |
| 576 | !BBI->getType()->isIntegerTy()) { |
| 577 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 578 | << "' - overdefined because inst def found.\n"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 579 | BBLV.markOverdefined(); |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 580 | return ODCacheUpdater.markResult(true); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 581 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 582 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 583 | // FIXME: We're currently limited to binops with a constant RHS. This should |
| 584 | // be improved. |
| 585 | BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI); |
| 586 | if (BO && !isa<ConstantInt>(BO->getOperand(1))) { |
| 587 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 588 | << "' - overdefined because inst def found.\n"); |
| 589 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 590 | BBLV.markOverdefined(); |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 591 | return ODCacheUpdater.markResult(true); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 592 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 593 | |
Owen Anderson | d83f98a | 2010-12-20 19:33:41 +0000 | [diff] [blame] | 594 | return ODCacheUpdater.markResult(solveBlockValueConstantRange(BBLV, BBI, BB)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) { |
| 598 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 599 | return L->getPointerAddressSpace() == 0 && |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 600 | GetUnderlyingObject(L->getPointerOperand()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 601 | } |
| 602 | if (StoreInst *S = dyn_cast<StoreInst>(I)) { |
| 603 | return S->getPointerAddressSpace() == 0 && |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 604 | GetUnderlyingObject(S->getPointerOperand()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 605 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 606 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 607 | if (MI->isVolatile()) return false; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 608 | |
| 609 | // FIXME: check whether it has a valuerange that excludes zero? |
| 610 | ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 611 | if (!Len || Len->isZero()) return false; |
| 612 | |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 613 | if (MI->getDestAddressSpace() == 0) |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 614 | if (GetUnderlyingObject(MI->getRawDest()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 615 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 616 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 617 | if (MTI->getSourceAddressSpace() == 0) |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 618 | if (GetUnderlyingObject(MTI->getRawSource()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 619 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 620 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 624 | bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 625 | Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 626 | LVILatticeVal Result; // Start Undefined. |
| 627 | |
| 628 | // If this is a pointer, and there's a load from that pointer in this BB, |
| 629 | // then we know that the pointer can't be NULL. |
| 630 | bool NotNull = false; |
| 631 | if (Val->getType()->isPointerTy()) { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 632 | if (isKnownNonNull(Val)) { |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 633 | NotNull = true; |
| 634 | } else { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 635 | Value *UnderlyingVal = GetUnderlyingObject(Val); |
| 636 | // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge |
| 637 | // inside InstructionDereferencesPointer either. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 638 | if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, nullptr, 1)) { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 639 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); |
| 640 | BI != BE; ++BI) { |
| 641 | if (InstructionDereferencesPointer(BI, UnderlyingVal)) { |
| 642 | NotNull = true; |
| 643 | break; |
| 644 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 645 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | // If this is the entry block, we must be asking about an argument. The |
| 651 | // value is overdefined. |
| 652 | if (BB == &BB->getParent()->getEntryBlock()) { |
| 653 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 654 | if (NotNull) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 655 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 656 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 657 | } else { |
| 658 | Result.markOverdefined(); |
| 659 | } |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 660 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 661 | return true; |
| 662 | } |
| 663 | |
| 664 | // Loop over all of our predecessors, merging what we know from them into |
| 665 | // result. |
| 666 | bool EdgesMissing = false; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 667 | 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] | 668 | LVILatticeVal EdgeResult; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 669 | EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 670 | if (EdgesMissing) |
| 671 | continue; |
| 672 | |
| 673 | Result.mergeIn(EdgeResult); |
| 674 | |
| 675 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 676 | // to overdefined. |
| 677 | if (Result.isOverdefined()) { |
| 678 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 679 | << "' - overdefined because of pred.\n"); |
| 680 | // If we previously determined that this is a pointer that can't be null |
| 681 | // then return that rather than giving up entirely. |
| 682 | if (NotNull) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 683 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 684 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 685 | } |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 686 | |
| 687 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 688 | return true; |
| 689 | } |
| 690 | } |
| 691 | if (EdgesMissing) |
| 692 | return false; |
| 693 | |
| 694 | // Return the merged value, which is more precise than 'overdefined'. |
| 695 | assert(!Result.isOverdefined()); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 696 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 697 | return true; |
| 698 | } |
| 699 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 700 | bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 701 | PHINode *PN, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 702 | LVILatticeVal Result; // Start Undefined. |
| 703 | |
| 704 | // Loop over all of our predecessors, merging what we know from them into |
| 705 | // result. |
| 706 | bool EdgesMissing = false; |
| 707 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 708 | BasicBlock *PhiBB = PN->getIncomingBlock(i); |
| 709 | Value *PhiVal = PN->getIncomingValue(i); |
| 710 | LVILatticeVal EdgeResult; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 711 | EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 712 | if (EdgesMissing) |
| 713 | continue; |
| 714 | |
| 715 | Result.mergeIn(EdgeResult); |
| 716 | |
| 717 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 718 | // to overdefined. |
| 719 | if (Result.isOverdefined()) { |
| 720 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 721 | << "' - overdefined because of pred.\n"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 722 | |
| 723 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 724 | return true; |
| 725 | } |
| 726 | } |
| 727 | if (EdgesMissing) |
| 728 | return false; |
| 729 | |
| 730 | // Return the merged value, which is more precise than 'overdefined'. |
| 731 | assert(!Result.isOverdefined() && "Possible PHI in entry block?"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 732 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 733 | return true; |
| 734 | } |
| 735 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 736 | static bool getValueFromFromCondition(Value *Val, ICmpInst *ICI, |
| 737 | LVILatticeVal &Result, |
| 738 | bool isTrueDest = true); |
| 739 | |
| 740 | // If we can determine a constant range for the value Val at the context |
| 741 | // provided by the instruction BBI, then merge it into BBLV. If we did find a |
| 742 | // constant range, return true. |
| 743 | void LazyValueInfoCache::mergeAssumeBlockValueConstantRange( |
| 744 | Value *Val, LVILatticeVal &BBLV, Instruction *BBI) { |
| 745 | BBI = BBI ? BBI : dyn_cast<Instruction>(Val); |
| 746 | if (!BBI) |
| 747 | return; |
| 748 | |
| 749 | for (auto &I : AT->assumptions(BBI->getParent()->getParent())) { |
| 750 | if (!isValidAssumeForContext(I, BBI, DL, DT)) |
| 751 | continue; |
| 752 | |
| 753 | Value *C = I->getArgOperand(0); |
| 754 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(C)) { |
| 755 | LVILatticeVal Result; |
| 756 | if (getValueFromFromCondition(Val, ICI, Result)) { |
| 757 | if (BBLV.isOverdefined()) |
| 758 | BBLV = Result; |
| 759 | else |
| 760 | BBLV.mergeIn(Result); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 766 | bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 767 | Instruction *BBI, |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 768 | BasicBlock *BB) { |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 769 | // Figure out the range of the LHS. If that fails, bail. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 770 | if (!hasBlockValue(BBI->getOperand(0), BB)) { |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 771 | BlockValueStack.push(std::make_pair(BB, BBI->getOperand(0))); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 772 | return false; |
| 773 | } |
| 774 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 775 | LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 776 | mergeAssumeBlockValueConstantRange(BBI->getOperand(0), LHSVal, BBI); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 777 | if (!LHSVal.isConstantRange()) { |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 778 | BBLV.markOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 779 | return true; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 782 | ConstantRange LHSRange = LHSVal.getConstantRange(); |
| 783 | ConstantRange RHSRange(1); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 784 | IntegerType *ResultTy = cast<IntegerType>(BBI->getType()); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 785 | if (isa<BinaryOperator>(BBI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 786 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) { |
| 787 | RHSRange = ConstantRange(RHS->getValue()); |
| 788 | } else { |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 789 | BBLV.markOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 790 | return true; |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 791 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 792 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 793 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 794 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 795 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 796 | // more definitions. |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 797 | LVILatticeVal Result; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 798 | switch (BBI->getOpcode()) { |
| 799 | case Instruction::Add: |
| 800 | Result.markConstantRange(LHSRange.add(RHSRange)); |
| 801 | break; |
| 802 | case Instruction::Sub: |
| 803 | Result.markConstantRange(LHSRange.sub(RHSRange)); |
| 804 | break; |
| 805 | case Instruction::Mul: |
| 806 | Result.markConstantRange(LHSRange.multiply(RHSRange)); |
| 807 | break; |
| 808 | case Instruction::UDiv: |
| 809 | Result.markConstantRange(LHSRange.udiv(RHSRange)); |
| 810 | break; |
| 811 | case Instruction::Shl: |
| 812 | Result.markConstantRange(LHSRange.shl(RHSRange)); |
| 813 | break; |
| 814 | case Instruction::LShr: |
| 815 | Result.markConstantRange(LHSRange.lshr(RHSRange)); |
| 816 | break; |
| 817 | case Instruction::Trunc: |
| 818 | Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth())); |
| 819 | break; |
| 820 | case Instruction::SExt: |
| 821 | Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth())); |
| 822 | break; |
| 823 | case Instruction::ZExt: |
| 824 | Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth())); |
| 825 | break; |
| 826 | case Instruction::BitCast: |
| 827 | Result.markConstantRange(LHSRange); |
| 828 | break; |
Nick Lewycky | ad48e01 | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 829 | case Instruction::And: |
| 830 | Result.markConstantRange(LHSRange.binaryAnd(RHSRange)); |
| 831 | break; |
| 832 | case Instruction::Or: |
| 833 | Result.markConstantRange(LHSRange.binaryOr(RHSRange)); |
| 834 | break; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 835 | |
| 836 | // Unhandled instructions are overdefined. |
| 837 | default: |
| 838 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 839 | << "' - overdefined because inst def found.\n"); |
| 840 | Result.markOverdefined(); |
| 841 | break; |
| 842 | } |
| 843 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 844 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 845 | return true; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 848 | bool getValueFromFromCondition(Value *Val, ICmpInst *ICI, |
| 849 | LVILatticeVal &Result, bool isTrueDest) { |
| 850 | if (ICI && isa<Constant>(ICI->getOperand(1))) { |
| 851 | if (ICI->isEquality() && ICI->getOperand(0) == Val) { |
| 852 | // We know that V has the RHS constant if this is a true SETEQ or |
| 853 | // false SETNE. |
| 854 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
| 855 | Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
| 856 | else |
| 857 | Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
| 858 | return true; |
| 859 | } |
| 860 | |
| 861 | // Recognize the range checking idiom that InstCombine produces. |
| 862 | // (X-C1) u< C2 --> [C1, C1+C2) |
| 863 | ConstantInt *NegOffset = nullptr; |
| 864 | if (ICI->getPredicate() == ICmpInst::ICMP_ULT) |
| 865 | match(ICI->getOperand(0), m_Add(m_Specific(Val), |
| 866 | m_ConstantInt(NegOffset))); |
| 867 | |
| 868 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1)); |
| 869 | if (CI && (ICI->getOperand(0) == Val || NegOffset)) { |
| 870 | // Calculate the range of values that would satisfy the comparison. |
| 871 | ConstantRange CmpRange(CI->getValue()); |
| 872 | ConstantRange TrueValues = |
| 873 | ConstantRange::makeICmpRegion(ICI->getPredicate(), CmpRange); |
| 874 | |
| 875 | if (NegOffset) // Apply the offset from above. |
| 876 | TrueValues = TrueValues.subtract(NegOffset->getValue()); |
| 877 | |
| 878 | // If we're interested in the false dest, invert the condition. |
| 879 | if (!isTrueDest) TrueValues = TrueValues.inverse(); |
| 880 | |
| 881 | Result = LVILatticeVal::getRange(TrueValues); |
| 882 | return true; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | return false; |
| 887 | } |
| 888 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 889 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if |
| 890 | /// Val is not constrained on the edge. |
| 891 | static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, |
| 892 | BasicBlock *BBTo, LVILatticeVal &Result) { |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 893 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
| 894 | // know that v != 0. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 895 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 896 | // If this is a conditional branch and only one successor goes to BBTo, then |
| 897 | // we maybe able to infer something from the condition. |
| 898 | if (BI->isConditional() && |
| 899 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 900 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 901 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 902 | "BBTo isn't a successor of BBFrom"); |
| 903 | |
| 904 | // If V is the condition of the branch itself, then we know exactly what |
| 905 | // it is. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 906 | if (BI->getCondition() == Val) { |
| 907 | Result = LVILatticeVal::get(ConstantInt::get( |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 908 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 909 | return true; |
| 910 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 911 | |
| 912 | // If the condition of the branch is an equality comparison, we may be |
| 913 | // able to infer the value. |
Owen Anderson | 0bd6124 | 2010-08-11 04:24:25 +0000 | [diff] [blame] | 914 | ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 915 | if (getValueFromFromCondition(Val, ICI, Result, isTrueDest)) |
| 916 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 917 | } |
| 918 | } |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 919 | |
| 920 | // If the edge was formed by a switch on the value, then we may know exactly |
| 921 | // what it is. |
| 922 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 923 | if (SI->getCondition() != Val) |
| 924 | return false; |
| 925 | |
| 926 | bool DefaultCase = SI->getDefaultDest() == BBTo; |
| 927 | unsigned BitWidth = Val->getType()->getIntegerBitWidth(); |
| 928 | ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/); |
| 929 | |
| 930 | for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); |
| 931 | i != e; ++i) { |
| 932 | ConstantRange EdgeVal(i.getCaseValue()->getValue()); |
Manman Ren | f3fedb6 | 2012-09-05 23:45:58 +0000 | [diff] [blame] | 933 | if (DefaultCase) { |
| 934 | // It is possible that the default destination is the destination of |
| 935 | // some cases. There is no need to perform difference for those cases. |
| 936 | if (i.getCaseSuccessor() != BBTo) |
| 937 | EdgesVals = EdgesVals.difference(EdgeVal); |
| 938 | } else if (i.getCaseSuccessor() == BBTo) |
Nuno Lopes | ac59380 | 2012-05-18 21:02:10 +0000 | [diff] [blame] | 939 | EdgesVals = EdgesVals.unionWith(EdgeVal); |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 940 | } |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 941 | Result = LVILatticeVal::getRange(EdgesVals); |
| 942 | return true; |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 943 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | |
| 947 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo, or the value at |
| 948 | /// the basic block if the edge does not constraint Val. |
| 949 | bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 950 | BasicBlock *BBTo, LVILatticeVal &Result, |
| 951 | Instruction *CxtI) { |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 952 | // If already a constant, there is nothing to compute. |
| 953 | if (Constant *VC = dyn_cast<Constant>(Val)) { |
| 954 | Result = LVILatticeVal::get(VC); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 955 | return true; |
| 956 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 957 | |
| 958 | if (getEdgeValueLocal(Val, BBFrom, BBTo, Result)) { |
| 959 | if (!Result.isConstantRange() || |
| 960 | Result.getConstantRange().getSingleElement()) |
| 961 | return true; |
| 962 | |
| 963 | // FIXME: this check should be moved to the beginning of the function when |
| 964 | // LVI better supports recursive values. Even for the single value case, we |
| 965 | // can intersect to detect dead code (an empty range). |
| 966 | if (!hasBlockValue(Val, BBFrom)) { |
| 967 | BlockValueStack.push(std::make_pair(BBFrom, Val)); |
| 968 | return false; |
| 969 | } |
| 970 | |
| 971 | // Try to intersect ranges of the BB and the constraint on the edge. |
| 972 | LVILatticeVal InBlock = getBlockValue(Val, BBFrom); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 973 | mergeAssumeBlockValueConstantRange(Val, InBlock, CxtI); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 974 | if (!InBlock.isConstantRange()) |
| 975 | return true; |
| 976 | |
| 977 | ConstantRange Range = |
| 978 | Result.getConstantRange().intersectWith(InBlock.getConstantRange()); |
| 979 | Result = LVILatticeVal::getRange(Range); |
| 980 | return true; |
| 981 | } |
| 982 | |
| 983 | if (!hasBlockValue(Val, BBFrom)) { |
| 984 | BlockValueStack.push(std::make_pair(BBFrom, Val)); |
| 985 | return false; |
| 986 | } |
| 987 | |
| 988 | // if we couldn't compute the value on the edge, use the value from the BB |
| 989 | Result = getBlockValue(Val, BBFrom); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 990 | mergeAssumeBlockValueConstantRange(Val, Result, CxtI); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 991 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 994 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB, |
| 995 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 996 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 997 | << BB->getName() << "'\n"); |
| 998 | |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 999 | BlockValueStack.push(std::make_pair(BB, V)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1000 | solve(); |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 1001 | LVILatticeVal Result = getBlockValue(V, BB); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1002 | mergeAssumeBlockValueConstantRange(V, Result, CxtI); |
| 1003 | |
| 1004 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
| 1005 | return Result; |
| 1006 | } |
| 1007 | |
| 1008 | LVILatticeVal LazyValueInfoCache::getValueAt(Value *V, Instruction *CxtI) { |
| 1009 | DEBUG(dbgs() << "LVI Getting value " << *V << " at '" |
| 1010 | << CxtI->getName() << "'\n"); |
| 1011 | |
| 1012 | LVILatticeVal Result; |
| 1013 | mergeAssumeBlockValueConstantRange(V, Result, CxtI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1014 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1015 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1016 | return Result; |
| 1017 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1018 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1019 | LVILatticeVal LazyValueInfoCache:: |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1020 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, |
| 1021 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1022 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1023 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1024 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1025 | LVILatticeVal Result; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1026 | if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1027 | solve(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1028 | bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1029 | (void)WasFastQuery; |
| 1030 | assert(WasFastQuery && "More work to do after problem solved?"); |
| 1031 | } |
| 1032 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1033 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1034 | return Result; |
| 1035 | } |
| 1036 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1037 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 1038 | BasicBlock *NewSucc) { |
| 1039 | // When an edge in the graph has been threaded, values that we could not |
| 1040 | // determine a value for before (i.e. were marked overdefined) may be possible |
| 1041 | // to solve now. We do NOT try to proactively update these values. Instead, |
| 1042 | // we clear their entries from the cache, and allow lazy updating to recompute |
| 1043 | // them when needed. |
| 1044 | |
| 1045 | // The updating process is fairly simple: we need to dropped cached info |
| 1046 | // for all values that were marked overdefined in OldSucc, and for those same |
| 1047 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 1048 | // also marked overdefined. |
| 1049 | std::vector<BasicBlock*> worklist; |
| 1050 | worklist.push_back(OldSucc); |
| 1051 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1052 | DenseSet<Value*> ClearSet; |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 1053 | for (DenseSet<OverDefinedPairTy>::iterator I = OverDefinedCache.begin(), |
| 1054 | E = OverDefinedCache.end(); I != E; ++I) { |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1055 | if (I->first == OldSucc) |
| 1056 | ClearSet.insert(I->second); |
| 1057 | } |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 1060 | // NOTE: We do not need a visited list since any blocks we have already |
| 1061 | // visited will have had their overdefined markers cleared already, and we |
| 1062 | // thus won't loop to their successors. |
| 1063 | while (!worklist.empty()) { |
| 1064 | BasicBlock *ToUpdate = worklist.back(); |
| 1065 | worklist.pop_back(); |
| 1066 | |
| 1067 | // Skip blocks only accessible through NewSucc. |
| 1068 | if (ToUpdate == NewSucc) continue; |
| 1069 | |
| 1070 | bool changed = false; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1071 | for (DenseSet<Value*>::iterator I = ClearSet.begin(), E = ClearSet.end(); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1072 | I != E; ++I) { |
| 1073 | // If a value was marked overdefined in OldSucc, and is here too... |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 1074 | DenseSet<OverDefinedPairTy>::iterator OI = |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1075 | OverDefinedCache.find(std::make_pair(ToUpdate, *I)); |
| 1076 | if (OI == OverDefinedCache.end()) continue; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1077 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1078 | // Remove it from the caches. |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 1079 | ValueCacheEntryTy &Entry = ValueCache[LVIValueHandle(*I, this)]; |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1080 | ValueCacheEntryTy::iterator CI = Entry.find(ToUpdate); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1081 | |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1082 | assert(CI != Entry.end() && "Couldn't find entry to update?"); |
| 1083 | Entry.erase(CI); |
| 1084 | OverDefinedCache.erase(OI); |
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 | // If we removed anything, then we potentially need to update |
| 1087 | // blocks successors too. |
| 1088 | changed = true; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1089 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1090 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1091 | if (!changed) continue; |
| 1092 | |
| 1093 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 1094 | } |
| 1095 | } |
| 1096 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1097 | //===----------------------------------------------------------------------===// |
| 1098 | // LazyValueInfo Impl |
| 1099 | //===----------------------------------------------------------------------===// |
| 1100 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1101 | /// getCache - This lazily constructs the LazyValueInfoCache. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1102 | static LazyValueInfoCache &getCache(void *&PImpl, |
| 1103 | AssumptionTracker *AT, |
| 1104 | const DataLayout *DL = nullptr, |
| 1105 | DominatorTree *DT = nullptr) { |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1106 | if (!PImpl) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1107 | PImpl = new LazyValueInfoCache(AT, DL, DT); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1108 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 1109 | } |
| 1110 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1111 | bool LazyValueInfo::runOnFunction(Function &F) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1112 | AT = &getAnalysis<AssumptionTracker>(); |
| 1113 | |
| 1114 | DominatorTreeWrapperPass *DTWP = |
| 1115 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 1116 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1117 | |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 1118 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1119 | DL = DLP ? &DLP->getDataLayout() : nullptr; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1120 | TLI = &getAnalysis<TargetLibraryInfo>(); |
| 1121 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1122 | if (PImpl) |
| 1123 | getCache(PImpl, AT, DL, DT).clear(); |
| 1124 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1125 | // Fully lazy. |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1129 | void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1130 | AU.setPreservesAll(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1131 | AU.addRequired<AssumptionTracker>(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1132 | AU.addRequired<TargetLibraryInfo>(); |
| 1133 | } |
| 1134 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1135 | void LazyValueInfo::releaseMemory() { |
| 1136 | // If the cache was allocated, free it. |
| 1137 | if (PImpl) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1138 | delete &getCache(PImpl, AT); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1139 | PImpl = nullptr; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1143 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB, |
| 1144 | Instruction *CxtI) { |
| 1145 | LVILatticeVal Result = |
| 1146 | getCache(PImpl, AT, DL, DT).getValueInBlock(V, BB, CxtI); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1148 | if (Result.isConstant()) |
| 1149 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1150 | if (Result.isConstantRange()) { |
Owen Anderson | 38f6b7f | 2010-08-27 23:29:38 +0000 | [diff] [blame] | 1151 | ConstantRange CR = Result.getConstantRange(); |
| 1152 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1153 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1154 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1155 | return nullptr; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1156 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1157 | |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1158 | /// getConstantOnEdge - Determine whether the specified value is known to be a |
| 1159 | /// constant on the specified edge. Return null if not. |
| 1160 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1161 | BasicBlock *ToBB, |
| 1162 | Instruction *CxtI) { |
| 1163 | LVILatticeVal Result = |
| 1164 | getCache(PImpl, AT, DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (Result.isConstant()) |
| 1167 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1168 | if (Result.isConstantRange()) { |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1169 | ConstantRange CR = Result.getConstantRange(); |
| 1170 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1171 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1172 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1173 | return nullptr; |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1176 | static LazyValueInfo::Tristate |
| 1177 | getPredicateResult(unsigned Pred, Constant *C, LVILatticeVal &Result, |
| 1178 | const DataLayout *DL, TargetLibraryInfo *TLI) { |
| 1179 | |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1180 | // If we know the value is a constant, evaluate the conditional. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1181 | Constant *Res = nullptr; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1182 | if (Result.isConstant()) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1183 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1184 | TLI); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1185 | if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1186 | return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True; |
| 1187 | return LazyValueInfo::Unknown; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1190 | if (Result.isConstantRange()) { |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1191 | ConstantInt *CI = dyn_cast<ConstantInt>(C); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1192 | if (!CI) return LazyValueInfo::Unknown; |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1193 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1194 | ConstantRange CR = Result.getConstantRange(); |
| 1195 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1196 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1197 | return LazyValueInfo::False; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1200 | return LazyValueInfo::True; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1201 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1202 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1203 | return LazyValueInfo::True; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1204 | |
| 1205 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1206 | return LazyValueInfo::False; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | // Handle more complex predicates. |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1210 | ConstantRange TrueValues = |
| 1211 | ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue()); |
| 1212 | if (TrueValues.contains(CR)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1213 | return LazyValueInfo::True; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1214 | if (TrueValues.inverse().contains(CR)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1215 | return LazyValueInfo::False; |
| 1216 | return LazyValueInfo::Unknown; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1219 | if (Result.isNotConstant()) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1220 | // If this is an equality comparison, we can try to fold it knowing that |
| 1221 | // "V != C1". |
| 1222 | if (Pred == ICmpInst::ICMP_EQ) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1223 | // !C1 == C -> false iff C1 == C. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1224 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1225 | Result.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1226 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1227 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1228 | return LazyValueInfo::False; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1229 | } else if (Pred == ICmpInst::ICMP_NE) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1230 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | b0c0a0d | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 1231 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1232 | Result.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1233 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1234 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1235 | return LazyValueInfo::True; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1236 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1237 | return LazyValueInfo::Unknown; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1240 | return LazyValueInfo::Unknown; |
| 1241 | } |
| 1242 | |
| 1243 | /// getPredicateOnEdge - Determine whether the specified value comparison |
| 1244 | /// with a constant is known to be true or false on the specified CFG edge. |
| 1245 | /// Pred is a CmpInst predicate. |
| 1246 | LazyValueInfo::Tristate |
| 1247 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 1248 | BasicBlock *FromBB, BasicBlock *ToBB, |
| 1249 | Instruction *CxtI) { |
| 1250 | LVILatticeVal Result = |
| 1251 | getCache(PImpl, AT, DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
| 1252 | |
| 1253 | return getPredicateResult(Pred, C, Result, DL, TLI); |
| 1254 | } |
| 1255 | |
| 1256 | LazyValueInfo::Tristate |
| 1257 | LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C, |
| 1258 | Instruction *CxtI) { |
| 1259 | LVILatticeVal Result = |
| 1260 | getCache(PImpl, AT, DL, DT).getValueAt(V, CxtI); |
| 1261 | |
| 1262 | return getPredicateResult(Pred, C, Result, DL, TLI); |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1265 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1266 | BasicBlock *NewSucc) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1267 | if (PImpl) getCache(PImpl, AT, DL, DT).threadEdge(PredBB, OldSucc, NewSucc); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1271 | if (PImpl) getCache(PImpl, AT, DL, DT).eraseBlock(BB); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1272 | } |