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" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Dan Gohman | a4fcd24 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 23 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Constants.h" |
| 25 | #include "llvm/IR/DataLayout.h" |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Instructions.h" |
| 28 | #include "llvm/IR/IntrinsicInst.h" |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LLVMContext.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 30 | #include "llvm/IR/PatternMatch.h" |
Chandler Carruth | 4220e9c | 2014-03-04 11:17:44 +0000 | [diff] [blame] | 31 | #include "llvm/IR/ValueHandle.h" |
Chris Lattner | b584d1e | 2009-11-12 01:22:16 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | 4ec081a | 2012-01-11 23:43:34 +0000 | [diff] [blame] | 34 | #include <map> |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 35 | #include <stack> |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 36 | using namespace llvm; |
Benjamin Kramer | d9d80b1 | 2012-03-02 15:34:43 +0000 | [diff] [blame] | 37 | using namespace PatternMatch; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 38 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 39 | #define DEBUG_TYPE "lazy-value-info" |
| 40 | |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 41 | char LazyValueInfo::ID = 0; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 42 | INITIALIZE_PASS_BEGIN(LazyValueInfo, "lazy-value-info", |
| 43 | "Lazy Value Information Analysis", false, true) |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 44 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 45 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS_END(LazyValueInfo, "lazy-value-info", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 47 | "Lazy Value Information Analysis", false, true) |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 48 | |
| 49 | namespace llvm { |
| 50 | FunctionPass *createLazyValueInfoPass() { return new LazyValueInfo(); } |
| 51 | } |
| 52 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 53 | |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | // LVILatticeVal |
| 56 | //===----------------------------------------------------------------------===// |
| 57 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 58 | /// This is the information tracked by LazyValueInfo for each value. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 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 { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 66 | /// This Value has no known value yet. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 67 | undefined, |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 68 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 69 | /// This Value has a specific constant value. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 70 | constant, |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 71 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 72 | /// This Value is known to not have the specified value. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 73 | notconstant, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 74 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 75 | /// The Value falls within this range. |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 76 | constantrange, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 77 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 78 | /// This value is not known to be constant, and we know that it has a value. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 79 | overdefined |
| 80 | }; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 81 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 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; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 87 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 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; |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 105 | Res.markConstantRange(std::move(CR)); |
Owen Anderson | 5f1dd09 | 2010-08-10 23:20:01 +0000 | [diff] [blame] | 106 | return Res; |
| 107 | } |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 108 | static LVILatticeVal getOverdefined() { |
| 109 | LVILatticeVal Res; |
| 110 | Res.markOverdefined(); |
| 111 | return Res; |
| 112 | } |
| 113 | |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 114 | bool isUndefined() const { return Tag == undefined; } |
| 115 | bool isConstant() const { return Tag == constant; } |
| 116 | bool isNotConstant() const { return Tag == notconstant; } |
| 117 | bool isConstantRange() const { return Tag == constantrange; } |
| 118 | bool isOverdefined() const { return Tag == overdefined; } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 119 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 120 | Constant *getConstant() const { |
| 121 | assert(isConstant() && "Cannot get the constant of a non-constant!"); |
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 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 125 | Constant *getNotConstant() const { |
| 126 | assert(isNotConstant() && "Cannot get the constant of a non-notconstant!"); |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 127 | return Val; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 128 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 129 | |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 130 | ConstantRange getConstantRange() const { |
| 131 | assert(isConstantRange() && |
| 132 | "Cannot get the constant-range of a non-constant-range!"); |
| 133 | return Range; |
| 134 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 135 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 136 | /// Return true if this is a change in status. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 137 | bool markOverdefined() { |
| 138 | if (isOverdefined()) |
| 139 | return false; |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 140 | Tag = overdefined; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 144 | /// Return true if this is a change in status. |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 145 | bool markConstant(Constant *V) { |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 146 | assert(V && "Marking constant with NULL"); |
| 147 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 148 | return markConstantRange(ConstantRange(CI->getValue())); |
| 149 | if (isa<UndefValue>(V)) |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 150 | return false; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 151 | |
| 152 | assert((!isConstant() || getConstant() == V) && |
| 153 | "Marking constant with different value"); |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 154 | assert(isUndefined()); |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 155 | Tag = constant; |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 156 | Val = V; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 159 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 160 | /// Return true if this is a change in status. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 161 | bool markNotConstant(Constant *V) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 162 | assert(V && "Marking constant with NULL"); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 163 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 164 | return markConstantRange(ConstantRange(CI->getValue()+1, CI->getValue())); |
| 165 | if (isa<UndefValue>(V)) |
| 166 | return false; |
| 167 | |
| 168 | assert((!isConstant() || getConstant() != V) && |
| 169 | "Marking constant !constant with same value"); |
| 170 | assert((!isNotConstant() || getNotConstant() == V) && |
| 171 | "Marking !constant with different value"); |
| 172 | assert(isUndefined() || isConstant()); |
| 173 | Tag = notconstant; |
Owen Anderson | c3a1413 | 2010-08-05 22:10:46 +0000 | [diff] [blame] | 174 | Val = V; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 175 | return true; |
| 176 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 177 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 178 | /// Return true if this is a change in status. |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 179 | bool markConstantRange(ConstantRange NewR) { |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 180 | if (isConstantRange()) { |
| 181 | if (NewR.isEmptySet()) |
| 182 | return markOverdefined(); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 183 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 184 | bool changed = Range != NewR; |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 185 | Range = std::move(NewR); |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 186 | return changed; |
| 187 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 188 | |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 189 | assert(isUndefined()); |
| 190 | if (NewR.isEmptySet()) |
| 191 | return markOverdefined(); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 192 | |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 193 | Tag = constantrange; |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 194 | Range = std::move(NewR); |
Owen Anderson | 0f306a4 | 2010-08-05 22:59:19 +0000 | [diff] [blame] | 195 | return true; |
| 196 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 197 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 198 | /// Merge the specified lattice value into this one, updating this |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 199 | /// one and returning true if anything changed. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 200 | bool mergeIn(const LVILatticeVal &RHS, const DataLayout &DL) { |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 201 | if (RHS.isUndefined() || isOverdefined()) return false; |
| 202 | if (RHS.isOverdefined()) return markOverdefined(); |
| 203 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 204 | if (isUndefined()) { |
| 205 | Tag = RHS.Tag; |
| 206 | Val = RHS.Val; |
| 207 | Range = RHS.Range; |
| 208 | return true; |
Chris Lattner | 22db4b5 | 2009-11-12 04:57:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 211 | if (isConstant()) { |
| 212 | if (RHS.isConstant()) { |
| 213 | if (Val == RHS.Val) |
| 214 | return false; |
| 215 | return markOverdefined(); |
| 216 | } |
| 217 | |
| 218 | if (RHS.isNotConstant()) { |
| 219 | if (Val == RHS.Val) |
| 220 | return markOverdefined(); |
| 221 | |
| 222 | // Unless we can prove that the two Constants are different, we must |
| 223 | // move to overdefined. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 224 | if (ConstantInt *Res = |
| 225 | dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands( |
| 226 | CmpInst::ICMP_NE, getConstant(), RHS.getNotConstant(), DL))) |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 227 | if (Res->isOne()) |
| 228 | return markNotConstant(RHS.getNotConstant()); |
| 229 | |
| 230 | return markOverdefined(); |
| 231 | } |
| 232 | |
| 233 | // RHS is a ConstantRange, LHS is a non-integer Constant. |
| 234 | |
| 235 | // FIXME: consider the case where RHS is a range [1, 0) and LHS is |
| 236 | // a function. The correct result is to pick up RHS. |
| 237 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 238 | return markOverdefined(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | if (isNotConstant()) { |
| 242 | if (RHS.isConstant()) { |
| 243 | if (Val == RHS.Val) |
| 244 | return markOverdefined(); |
| 245 | |
| 246 | // Unless we can prove that the two Constants are different, we must |
| 247 | // move to overdefined. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 248 | if (ConstantInt *Res = |
| 249 | dyn_cast<ConstantInt>(ConstantFoldCompareInstOperands( |
| 250 | CmpInst::ICMP_NE, getNotConstant(), RHS.getConstant(), DL))) |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 251 | if (Res->isOne()) |
| 252 | return false; |
| 253 | |
| 254 | return markOverdefined(); |
| 255 | } |
| 256 | |
| 257 | if (RHS.isNotConstant()) { |
| 258 | if (Val == RHS.Val) |
| 259 | return false; |
| 260 | return markOverdefined(); |
| 261 | } |
| 262 | |
| 263 | return markOverdefined(); |
| 264 | } |
| 265 | |
| 266 | assert(isConstantRange() && "New LVILattice type?"); |
| 267 | if (!RHS.isConstantRange()) |
| 268 | return markOverdefined(); |
| 269 | |
| 270 | ConstantRange NewR = Range.unionWith(RHS.getConstantRange()); |
| 271 | if (NewR.isFullSet()) |
| 272 | return markOverdefined(); |
| 273 | return markConstantRange(NewR); |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 274 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 275 | }; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 276 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 277 | } // end anonymous namespace. |
| 278 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 279 | namespace llvm { |
Chandler Carruth | 2b1ba48 | 2011-04-18 18:49:44 +0000 | [diff] [blame] | 280 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) |
| 281 | LLVM_ATTRIBUTE_USED; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 282 | raw_ostream &operator<<(raw_ostream &OS, const LVILatticeVal &Val) { |
| 283 | if (Val.isUndefined()) |
| 284 | return OS << "undefined"; |
| 285 | if (Val.isOverdefined()) |
| 286 | return OS << "overdefined"; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 287 | |
| 288 | if (Val.isNotConstant()) |
| 289 | return OS << "notconstant<" << *Val.getNotConstant() << '>'; |
Owen Anderson | 8afac04 | 2010-08-09 20:50:46 +0000 | [diff] [blame] | 290 | else if (Val.isConstantRange()) |
| 291 | return OS << "constantrange<" << Val.getConstantRange().getLower() << ", " |
| 292 | << Val.getConstantRange().getUpper() << '>'; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 293 | return OS << "constant<" << *Val.getConstant() << '>'; |
| 294 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 295 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 296 | |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 297 | /// Returns true if this lattice value represents at most one possible value. |
| 298 | /// This is as precise as any lattice value can get while still representing |
| 299 | /// reachable code. |
| 300 | static bool hasSingleValue(LVILatticeVal Val) { |
| 301 | if (Val.isConstantRange() && |
| 302 | Val.getConstantRange().isSingleElement()) |
| 303 | // Integer constants are single element ranges |
| 304 | return true; |
| 305 | if (Val.isConstant()) |
| 306 | // Non integer constants |
| 307 | return true; |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | /// Combine two sets of facts about the same value into a single set of |
| 312 | /// facts. Note that this method is not suitable for merging facts along |
| 313 | /// different paths in a CFG; that's what the mergeIn function is for. This |
| 314 | /// is for merging facts gathered about the same value at the same location |
| 315 | /// through two independent means. |
| 316 | /// Notes: |
| 317 | /// * This method does not promise to return the most precise possible lattice |
| 318 | /// value implied by A and B. It is allowed to return any lattice element |
| 319 | /// which is at least as strong as *either* A or B (unless our facts |
| 320 | /// conflict, see below). |
| 321 | /// * Due to unreachable code, the intersection of two lattice values could be |
| 322 | /// contradictory. If this happens, we return some valid lattice value so as |
| 323 | /// not confuse the rest of LVI. Ideally, we'd always return Undefined, but |
| 324 | /// we do not make this guarantee. TODO: This would be a useful enhancement. |
| 325 | static LVILatticeVal intersect(LVILatticeVal A, LVILatticeVal B) { |
| 326 | // Undefined is the strongest state. It means the value is known to be along |
| 327 | // an unreachable path. |
| 328 | if (A.isUndefined()) |
| 329 | return A; |
| 330 | if (B.isUndefined()) |
| 331 | return B; |
| 332 | |
| 333 | // If we gave up for one, but got a useable fact from the other, use it. |
| 334 | if (A.isOverdefined()) |
| 335 | return B; |
| 336 | if (B.isOverdefined()) |
| 337 | return A; |
| 338 | |
| 339 | // Can't get any more precise than constants. |
| 340 | if (hasSingleValue(A)) |
| 341 | return A; |
| 342 | if (hasSingleValue(B)) |
| 343 | return B; |
| 344 | |
| 345 | // Could be either constant range or not constant here. |
| 346 | if (!A.isConstantRange() || !B.isConstantRange()) { |
| 347 | // TODO: Arbitrary choice, could be improved |
| 348 | return A; |
| 349 | } |
| 350 | |
| 351 | // Intersect two constant ranges |
| 352 | ConstantRange Range = |
| 353 | A.getConstantRange().intersectWith(B.getConstantRange()); |
| 354 | // Note: An empty range is implicitly converted to overdefined internally. |
| 355 | // TODO: We could instead use Undefined here since we've proven a conflict |
| 356 | // and thus know this path must be unreachable. |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 357 | return LVILatticeVal::getRange(std::move(Range)); |
Philip Reames | ed8cd0d | 2016-02-02 22:03:19 +0000 | [diff] [blame] | 358 | } |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 359 | |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 361 | // LazyValueInfoCache Decl |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 362 | //===----------------------------------------------------------------------===// |
| 363 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 364 | namespace { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 365 | /// A callback value handle updates the cache when values are erased. |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 366 | class LazyValueInfoCache; |
David Blaikie | 774b584 | 2015-08-03 22:30:24 +0000 | [diff] [blame] | 367 | struct LVIValueHandle final : public CallbackVH { |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 368 | LazyValueInfoCache *Parent; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 369 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 370 | LVIValueHandle(Value *V, LazyValueInfoCache *P) |
| 371 | : CallbackVH(V), Parent(P) { } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 372 | |
| 373 | void deleted() override; |
| 374 | void allUsesReplacedWith(Value *V) override { |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 375 | deleted(); |
| 376 | } |
| 377 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 378 | } |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 379 | |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 380 | namespace { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 381 | /// This is the cache kept by LazyValueInfo which |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 382 | /// maintains information about queries across the clients' queries. |
| 383 | class LazyValueInfoCache { |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 384 | /// This is all of the cached block information for exactly one Value*. |
| 385 | /// The entries are sorted by the BasicBlock* of the |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 386 | /// entries, allowing us to do a lookup with a binary search. |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 387 | /// Over-defined lattice values are recorded in OverDefinedCache to reduce |
| 388 | /// memory overhead. |
Bruno Cardoso Lopes | 1846ea3 | 2015-08-18 16:54:36 +0000 | [diff] [blame] | 389 | typedef SmallDenseMap<AssertingVH<BasicBlock>, LVILatticeVal, 4> |
| 390 | ValueCacheEntryTy; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 391 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 392 | /// This is all of the cached information for all values, |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 393 | /// mapped from Value* to key information. |
Bill Wendling | 58c7569 | 2012-01-12 01:41:03 +0000 | [diff] [blame] | 394 | std::map<LVIValueHandle, ValueCacheEntryTy> ValueCache; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 395 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 396 | /// This tracks, on a per-block basis, the set of values that are |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 397 | /// over-defined at the end of that block. |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 398 | typedef DenseMap<AssertingVH<BasicBlock>, SmallPtrSet<Value *, 4>> |
| 399 | OverDefinedCacheTy; |
| 400 | OverDefinedCacheTy OverDefinedCache; |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 401 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 402 | /// Keep track of all blocks that we have ever seen, so we |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 403 | /// don't spend time removing unused blocks from our caches. |
| 404 | DenseSet<AssertingVH<BasicBlock> > SeenBlocks; |
| 405 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 406 | /// This stack holds the state of the value solver during a query. |
| 407 | /// It basically emulates the callstack of the naive |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 408 | /// recursive value lookup process. |
| 409 | std::stack<std::pair<BasicBlock*, Value*> > BlockValueStack; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 410 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 411 | /// Keeps track of which block-value pairs are in BlockValueStack. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 412 | DenseSet<std::pair<BasicBlock*, Value*> > BlockValueSet; |
| 413 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 414 | /// Push BV onto BlockValueStack unless it's already in there. |
| 415 | /// Returns true on success. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 416 | bool pushBlockValue(const std::pair<BasicBlock *, Value *> &BV) { |
Benjamin Kramer | 4e3b903 | 2015-02-27 21:43:14 +0000 | [diff] [blame] | 417 | if (!BlockValueSet.insert(BV).second) |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 418 | return false; // It's already in the stack. |
| 419 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 420 | DEBUG(dbgs() << "PUSH: " << *BV.second << " in " << BV.first->getName() |
| 421 | << "\n"); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 422 | BlockValueStack.push(BV); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 423 | return true; |
| 424 | } |
| 425 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 426 | AssumptionCache *AC; ///< A pointer to the cache of @llvm.assume calls. |
| 427 | const DataLayout &DL; ///< A mandatory DataLayout |
| 428 | DominatorTree *DT; ///< An optional DT pointer. |
| 429 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 430 | friend struct LVIValueHandle; |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 431 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 432 | void insertResult(Value *Val, BasicBlock *BB, const LVILatticeVal &Result) { |
| 433 | SeenBlocks.insert(BB); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 434 | |
| 435 | // Insert over-defined values into their own cache to reduce memory |
| 436 | // overhead. |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 437 | if (Result.isOverdefined()) |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 438 | OverDefinedCache[BB].insert(Val); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 439 | else |
| 440 | lookup(Val)[BB] = Result; |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 441 | } |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 442 | |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 443 | LVILatticeVal getBlockValue(Value *Val, BasicBlock *BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 444 | bool getEdgeValue(Value *V, BasicBlock *F, BasicBlock *T, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 445 | LVILatticeVal &Result, |
| 446 | Instruction *CxtI = nullptr); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 447 | bool hasBlockValue(Value *Val, BasicBlock *BB); |
| 448 | |
| 449 | // These methods process one work item and may add more. A false value |
| 450 | // returned means that the work item was not completely processed and must |
| 451 | // be revisited after going through the new items. |
| 452 | bool solveBlockValue(Value *Val, BasicBlock *BB); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 453 | bool solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 454 | Value *Val, BasicBlock *BB); |
| 455 | bool solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 456 | PHINode *PN, BasicBlock *BB); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 457 | bool solveBlockValueSelect(LVILatticeVal &BBLV, |
| 458 | SelectInst *S, BasicBlock *BB); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 459 | bool solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 460 | Instruction *BBI, BasicBlock *BB); |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 461 | void intersectAssumeBlockValueConstantRange(Value *Val, LVILatticeVal &BBLV, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 462 | Instruction *BBI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 463 | |
| 464 | void solve(); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 465 | |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 466 | ValueCacheEntryTy &lookup(Value *V) { |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 467 | return ValueCache[LVIValueHandle(V, this)]; |
| 468 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 469 | |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 470 | bool isOverdefined(Value *V, BasicBlock *BB) const { |
| 471 | auto ODI = OverDefinedCache.find(BB); |
| 472 | |
| 473 | if (ODI == OverDefinedCache.end()) |
| 474 | return false; |
| 475 | |
| 476 | return ODI->second.count(V); |
| 477 | } |
| 478 | |
| 479 | bool hasCachedValueInfo(Value *V, BasicBlock *BB) { |
| 480 | if (isOverdefined(V, BB)) |
| 481 | return true; |
| 482 | |
| 483 | LVIValueHandle ValHandle(V, this); |
| 484 | auto I = ValueCache.find(ValHandle); |
| 485 | if (I == ValueCache.end()) |
| 486 | return false; |
| 487 | |
| 488 | return I->second.count(BB); |
| 489 | } |
| 490 | |
| 491 | LVILatticeVal getCachedValueInfo(Value *V, BasicBlock *BB) { |
| 492 | if (isOverdefined(V, BB)) |
| 493 | return LVILatticeVal::getOverdefined(); |
| 494 | |
| 495 | return lookup(V)[BB]; |
| 496 | } |
| 497 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 498 | public: |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 499 | /// This is the query interface to determine the lattice |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 500 | /// value for the specified Value* at the end of the specified block. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 501 | LVILatticeVal getValueInBlock(Value *V, BasicBlock *BB, |
| 502 | Instruction *CxtI = nullptr); |
| 503 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 504 | /// This is the query interface to determine the lattice |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 505 | /// value for the specified Value* at the specified instruction (generally |
| 506 | /// from an assume intrinsic). |
| 507 | LVILatticeVal getValueAt(Value *V, Instruction *CxtI); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 508 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 509 | /// This is the query interface to determine the lattice |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 510 | /// value for the specified Value* that is true on the specified edge. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 511 | LVILatticeVal getValueOnEdge(Value *V, BasicBlock *FromBB,BasicBlock *ToBB, |
| 512 | Instruction *CxtI = nullptr); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 513 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 514 | /// This is the update interface to inform the cache that an edge from |
| 515 | /// PredBB to OldSucc has been threaded to be from PredBB to NewSucc. |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 516 | void threadEdge(BasicBlock *PredBB,BasicBlock *OldSucc,BasicBlock *NewSucc); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 517 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 518 | /// This is part of the update interface to inform the cache |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 519 | /// that a block has been deleted. |
| 520 | void eraseBlock(BasicBlock *BB); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 521 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 522 | /// clear - Empty the cache. |
| 523 | void clear() { |
Benjamin Kramer | bbf3c60 | 2011-12-03 15:19:55 +0000 | [diff] [blame] | 524 | SeenBlocks.clear(); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 525 | ValueCache.clear(); |
| 526 | OverDefinedCache.clear(); |
| 527 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 528 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 529 | LazyValueInfoCache(AssumptionCache *AC, const DataLayout &DL, |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 530 | DominatorTree *DT = nullptr) |
| 531 | : AC(AC), DL(DL), DT(DT) {} |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 532 | }; |
| 533 | } // end anonymous namespace |
| 534 | |
Owen Anderson | 118ac80 | 2011-01-05 21:15:29 +0000 | [diff] [blame] | 535 | void LVIValueHandle::deleted() { |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 536 | SmallVector<AssertingVH<BasicBlock>, 4> ToErase; |
| 537 | for (auto &I : Parent->OverDefinedCache) { |
| 538 | SmallPtrSetImpl<Value *> &ValueSet = I.second; |
| 539 | if (ValueSet.count(getValPtr())) |
| 540 | ValueSet.erase(getValPtr()); |
| 541 | if (ValueSet.empty()) |
| 542 | ToErase.push_back(I.first); |
| 543 | } |
| 544 | for (auto &BB : ToErase) |
| 545 | Parent->OverDefinedCache.erase(BB); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 546 | |
Owen Anderson | 7b974a4 | 2010-08-11 22:36:04 +0000 | [diff] [blame] | 547 | // This erasure deallocates *this, so it MUST happen after we're done |
| 548 | // using any and all members of *this. |
| 549 | Parent->ValueCache.erase(*this); |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 552 | void LazyValueInfoCache::eraseBlock(BasicBlock *BB) { |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 553 | // Shortcut if we have never seen this block. |
| 554 | DenseSet<AssertingVH<BasicBlock> >::iterator I = SeenBlocks.find(BB); |
| 555 | if (I == SeenBlocks.end()) |
| 556 | return; |
| 557 | SeenBlocks.erase(I); |
| 558 | |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 559 | auto ODI = OverDefinedCache.find(BB); |
| 560 | if (ODI != OverDefinedCache.end()) |
| 561 | OverDefinedCache.erase(ODI); |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 562 | |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 563 | for (auto I = ValueCache.begin(), E = ValueCache.end(); I != E; ++I) |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 564 | I->second.erase(BB); |
| 565 | } |
Owen Anderson | c1561b8 | 2010-07-30 23:59:40 +0000 | [diff] [blame] | 566 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 567 | void LazyValueInfoCache::solve() { |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 568 | while (!BlockValueStack.empty()) { |
| 569 | std::pair<BasicBlock*, Value*> &e = BlockValueStack.top(); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 570 | assert(BlockValueSet.count(e) && "Stack value should be in BlockValueSet!"); |
| 571 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 572 | if (solveBlockValue(e.second, e.first)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 573 | // The work item was completely processed. |
| 574 | assert(BlockValueStack.top() == e && "Nothing should have been pushed!"); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 575 | assert(hasCachedValueInfo(e.second, e.first) && |
| 576 | "Result should be in cache!"); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 577 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 578 | DEBUG(dbgs() << "POP " << *e.second << " in " << e.first->getName() |
Philip Reames | b757104 | 2016-02-02 22:43:08 +0000 | [diff] [blame] | 579 | << " = " << getCachedValueInfo(e.second, e.first) << "\n"); |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 580 | |
Owen Anderson | 6f060af | 2011-01-05 23:26:22 +0000 | [diff] [blame] | 581 | BlockValueStack.pop(); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 582 | BlockValueSet.erase(e); |
| 583 | } else { |
| 584 | // More work needs to be done before revisiting. |
| 585 | assert(BlockValueStack.top() != e && "Stack should have been pushed!"); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 586 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| 590 | bool LazyValueInfoCache::hasBlockValue(Value *Val, BasicBlock *BB) { |
| 591 | // If already a constant, there is nothing to compute. |
| 592 | if (isa<Constant>(Val)) |
| 593 | return true; |
| 594 | |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 595 | return hasCachedValueInfo(Val, BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 598 | LVILatticeVal LazyValueInfoCache::getBlockValue(Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 599 | // If already a constant, there is nothing to compute. |
| 600 | if (Constant *VC = dyn_cast<Constant>(Val)) |
| 601 | return LVILatticeVal::get(VC); |
| 602 | |
Benjamin Kramer | 3664708 | 2011-12-03 15:16:45 +0000 | [diff] [blame] | 603 | SeenBlocks.insert(BB); |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 604 | return getCachedValueInfo(Val, BB); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 607 | static LVILatticeVal getFromRangeMetadata(Instruction *BBI) { |
| 608 | switch (BBI->getOpcode()) { |
| 609 | default: break; |
| 610 | case Instruction::Load: |
| 611 | case Instruction::Call: |
| 612 | case Instruction::Invoke: |
| 613 | if (MDNode *Ranges = BBI->getMetadata(LLVMContext::MD_range)) |
Philip Reames | 70efccd | 2015-10-29 04:21:49 +0000 | [diff] [blame] | 614 | if (isa<IntegerType>(BBI->getType())) { |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 615 | return LVILatticeVal::getRange(getConstantRangeFromMetadata(*Ranges)); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 616 | } |
| 617 | break; |
| 618 | }; |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 619 | // Nothing known - will be intersected with other facts |
| 620 | return LVILatticeVal::getOverdefined(); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 623 | bool LazyValueInfoCache::solveBlockValue(Value *Val, BasicBlock *BB) { |
| 624 | if (isa<Constant>(Val)) |
| 625 | return true; |
| 626 | |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 627 | if (hasCachedValueInfo(Val, BB)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 628 | // If we have a cached value, use that. |
| 629 | DEBUG(dbgs() << " reuse BB '" << BB->getName() |
Akira Hatanaka | 2992bee | 2015-12-11 00:49:47 +0000 | [diff] [blame] | 630 | << "' val=" << getCachedValueInfo(Val, BB) << '\n'); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 631 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 632 | // Since we're reusing a cached value, we don't need to update the |
| 633 | // OverDefinedCache. The cache will have been properly updated whenever the |
| 634 | // cached value was inserted. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 635 | return true; |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 638 | // Hold off inserting this value into the Cache in case we have to return |
| 639 | // false and come back later. |
| 640 | LVILatticeVal Res; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 641 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 642 | Instruction *BBI = dyn_cast<Instruction>(Val); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 643 | if (!BBI || BBI->getParent() != BB) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 644 | if (!solveBlockValueNonLocal(Res, Val, BB)) |
| 645 | return false; |
| 646 | insertResult(Val, BB, Res); |
| 647 | return true; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | 2c70856 | 2009-11-15 20:00:52 +0000 | [diff] [blame] | 649 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 650 | if (PHINode *PN = dyn_cast<PHINode>(BBI)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 651 | if (!solveBlockValuePHINode(Res, PN, BB)) |
| 652 | return false; |
| 653 | insertResult(Val, BB, Res); |
| 654 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 655 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 656 | |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 657 | if (auto *SI = dyn_cast<SelectInst>(BBI)) { |
| 658 | if (!solveBlockValueSelect(Res, SI, BB)) |
| 659 | return false; |
| 660 | insertResult(Val, BB, Res); |
| 661 | return true; |
| 662 | } |
| 663 | |
Igor Laevsky | 0fa4819 | 2015-09-18 13:01:48 +0000 | [diff] [blame] | 664 | // If this value is a nonnull pointer, record it's range and bailout. |
| 665 | PointerType *PT = dyn_cast<PointerType>(BBI->getType()); |
| 666 | if (PT && isKnownNonNull(BBI)) { |
| 667 | Res = LVILatticeVal::getNot(ConstantPointerNull::get(PT)); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 668 | insertResult(Val, BB, Res); |
| 669 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 672 | // If this is an instruction which supports range metadata, intersect the |
| 673 | // implied range. |
| 674 | Res = intersect(Res, getFromRangeMetadata(BBI)); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 675 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 676 | // We can only analyze the definitions of certain classes of instructions |
| 677 | // (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] | 678 | LVILatticeVal Result; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 679 | if ((!isa<BinaryOperator>(BBI) && !isa<CastInst>(BBI)) || |
| 680 | !BBI->getType()->isIntegerTy()) { |
| 681 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 682 | << "' - overdefined because inst def found.\n"); |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 683 | Res.markOverdefined(); |
| 684 | insertResult(Val, BB, Res); |
| 685 | return true; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 686 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 687 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 688 | // FIXME: We're currently limited to binops with a constant RHS. This should |
| 689 | // be improved. |
| 690 | BinaryOperator *BO = dyn_cast<BinaryOperator>(BBI); |
| 691 | if (BO && !isa<ConstantInt>(BO->getOperand(1))) { |
| 692 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 693 | << "' - overdefined because inst def found.\n"); |
| 694 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 695 | Res.markOverdefined(); |
| 696 | insertResult(Val, BB, Res); |
| 697 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 698 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 699 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 700 | if (!solveBlockValueConstantRange(Res, BBI, BB)) |
| 701 | return false; |
| 702 | insertResult(Val, BB, Res); |
| 703 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static bool InstructionDereferencesPointer(Instruction *I, Value *Ptr) { |
| 707 | if (LoadInst *L = dyn_cast<LoadInst>(I)) { |
| 708 | return L->getPointerAddressSpace() == 0 && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 709 | GetUnderlyingObject(L->getPointerOperand(), |
| 710 | L->getModule()->getDataLayout()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 711 | } |
| 712 | if (StoreInst *S = dyn_cast<StoreInst>(I)) { |
| 713 | return S->getPointerAddressSpace() == 0 && |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 714 | GetUnderlyingObject(S->getPointerOperand(), |
| 715 | S->getModule()->getDataLayout()) == Ptr; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 716 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 717 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(I)) { |
| 718 | if (MI->isVolatile()) return false; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 719 | |
| 720 | // FIXME: check whether it has a valuerange that excludes zero? |
| 721 | ConstantInt *Len = dyn_cast<ConstantInt>(MI->getLength()); |
| 722 | if (!Len || Len->isZero()) return false; |
| 723 | |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 724 | if (MI->getDestAddressSpace() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 725 | if (GetUnderlyingObject(MI->getRawDest(), |
| 726 | MI->getModule()->getDataLayout()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 727 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 728 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 729 | if (MTI->getSourceAddressSpace() == 0) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 730 | if (GetUnderlyingObject(MTI->getRawSource(), |
| 731 | MTI->getModule()->getDataLayout()) == Ptr) |
Eli Friedman | 7a5fc69 | 2011-05-31 20:40:16 +0000 | [diff] [blame] | 732 | return true; |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 733 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 734 | return false; |
| 735 | } |
| 736 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 737 | bool LazyValueInfoCache::solveBlockValueNonLocal(LVILatticeVal &BBLV, |
| 738 | Value *Val, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 739 | LVILatticeVal Result; // Start Undefined. |
| 740 | |
| 741 | // If this is a pointer, and there's a load from that pointer in this BB, |
| 742 | // then we know that the pointer can't be NULL. |
| 743 | bool NotNull = false; |
| 744 | if (Val->getType()->isPointerTy()) { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 745 | if (isKnownNonNull(Val)) { |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 746 | NotNull = true; |
| 747 | } else { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 748 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 749 | Value *UnderlyingVal = GetUnderlyingObject(Val, DL); |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 750 | // If 'GetUnderlyingObject' didn't converge, skip it. It won't converge |
| 751 | // inside InstructionDereferencesPointer either. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 752 | if (UnderlyingVal == GetUnderlyingObject(UnderlyingVal, DL, 1)) { |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 753 | for (Instruction &I : *BB) { |
| 754 | if (InstructionDereferencesPointer(&I, UnderlyingVal)) { |
Nick Lewycky | c86037f | 2012-10-26 04:43:47 +0000 | [diff] [blame] | 755 | NotNull = true; |
| 756 | break; |
| 757 | } |
Nick Lewycky | 367f98f | 2011-01-15 09:16:12 +0000 | [diff] [blame] | 758 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | // If this is the entry block, we must be asking about an argument. The |
| 764 | // value is overdefined. |
| 765 | if (BB == &BB->getParent()->getEntryBlock()) { |
| 766 | assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); |
| 767 | if (NotNull) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 768 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 769 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 770 | } else { |
| 771 | Result.markOverdefined(); |
| 772 | } |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 773 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 774 | return true; |
| 775 | } |
| 776 | |
| 777 | // Loop over all of our predecessors, merging what we know from them into |
| 778 | // result. |
| 779 | bool EdgesMissing = false; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 780 | 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] | 781 | LVILatticeVal EdgeResult; |
Duncan P. N. Exon Smith | 6c99015 | 2014-07-21 17:06:51 +0000 | [diff] [blame] | 782 | EdgesMissing |= !getEdgeValue(Val, *PI, BB, EdgeResult); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 783 | if (EdgesMissing) |
| 784 | continue; |
| 785 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 786 | Result.mergeIn(EdgeResult, DL); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 787 | |
| 788 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 789 | // to overdefined. |
| 790 | if (Result.isOverdefined()) { |
| 791 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Philip Reames | b757104 | 2016-02-02 22:43:08 +0000 | [diff] [blame] | 792 | << "' - overdefined because of pred (non local).\n"); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 793 | // If we previously determined that this is a pointer that can't be null |
| 794 | // then return that rather than giving up entirely. |
| 795 | if (NotNull) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 796 | PointerType *PTy = cast<PointerType>(Val->getType()); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 797 | Result = LVILatticeVal::getNot(ConstantPointerNull::get(PTy)); |
| 798 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 799 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 800 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 801 | return true; |
| 802 | } |
| 803 | } |
| 804 | if (EdgesMissing) |
| 805 | return false; |
| 806 | |
| 807 | // Return the merged value, which is more precise than 'overdefined'. |
| 808 | assert(!Result.isOverdefined()); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 809 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 810 | return true; |
| 811 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 812 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 813 | bool LazyValueInfoCache::solveBlockValuePHINode(LVILatticeVal &BBLV, |
| 814 | PHINode *PN, BasicBlock *BB) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 815 | LVILatticeVal Result; // Start Undefined. |
| 816 | |
| 817 | // Loop over all of our predecessors, merging what we know from them into |
| 818 | // result. |
| 819 | bool EdgesMissing = false; |
| 820 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 821 | BasicBlock *PhiBB = PN->getIncomingBlock(i); |
| 822 | Value *PhiVal = PN->getIncomingValue(i); |
| 823 | LVILatticeVal EdgeResult; |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 824 | // Note that we can provide PN as the context value to getEdgeValue, even |
| 825 | // though the results will be cached, because PN is the value being used as |
| 826 | // the cache key in the caller. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 827 | EdgesMissing |= !getEdgeValue(PhiVal, PhiBB, BB, EdgeResult, PN); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 828 | if (EdgesMissing) |
| 829 | continue; |
| 830 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 831 | Result.mergeIn(EdgeResult, DL); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 832 | |
| 833 | // If we hit overdefined, exit early. The BlockVals entry is already set |
| 834 | // to overdefined. |
| 835 | if (Result.isOverdefined()) { |
| 836 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
Philip Reames | b757104 | 2016-02-02 22:43:08 +0000 | [diff] [blame] | 837 | << "' - overdefined because of pred (local).\n"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 838 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 839 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 840 | return true; |
| 841 | } |
| 842 | } |
| 843 | if (EdgesMissing) |
| 844 | return false; |
| 845 | |
| 846 | // Return the merged value, which is more precise than 'overdefined'. |
| 847 | assert(!Result.isOverdefined() && "Possible PHI in entry block?"); |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 848 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 852 | static bool getValueFromFromCondition(Value *Val, ICmpInst *ICI, |
| 853 | LVILatticeVal &Result, |
| 854 | bool isTrueDest = true); |
| 855 | |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 856 | // If we can determine a constraint on the value given conditions assumed by |
| 857 | // the program, intersect those constraints with BBLV |
| 858 | void LazyValueInfoCache::intersectAssumeBlockValueConstantRange(Value *Val, |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 859 | LVILatticeVal &BBLV, |
| 860 | Instruction *BBI) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 861 | BBI = BBI ? BBI : dyn_cast<Instruction>(Val); |
| 862 | if (!BBI) |
| 863 | return; |
| 864 | |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 865 | for (auto &AssumeVH : AC->assumptions()) { |
| 866 | if (!AssumeVH) |
| 867 | continue; |
| 868 | auto *I = cast<CallInst>(AssumeVH); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 869 | if (!isValidAssumeForContext(I, BBI, DT)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 870 | continue; |
| 871 | |
| 872 | Value *C = I->getArgOperand(0); |
| 873 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(C)) { |
| 874 | LVILatticeVal Result; |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 875 | if (getValueFromFromCondition(Val, ICI, Result)) |
| 876 | BBLV = intersect(BBLV, Result); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 881 | bool LazyValueInfoCache::solveBlockValueSelect(LVILatticeVal &BBLV, |
| 882 | SelectInst *SI, BasicBlock *BB) { |
| 883 | |
| 884 | // Recurse on our inputs if needed |
| 885 | if (!hasBlockValue(SI->getTrueValue(), BB)) { |
| 886 | if (pushBlockValue(std::make_pair(BB, SI->getTrueValue()))) |
| 887 | return false; |
| 888 | BBLV.markOverdefined(); |
| 889 | return true; |
| 890 | } |
| 891 | LVILatticeVal TrueVal = getBlockValue(SI->getTrueValue(), BB); |
| 892 | // If we hit overdefined, don't ask more queries. We want to avoid poisoning |
| 893 | // extra slots in the table if we can. |
| 894 | if (TrueVal.isOverdefined()) { |
| 895 | BBLV.markOverdefined(); |
| 896 | return true; |
| 897 | } |
| 898 | |
| 899 | if (!hasBlockValue(SI->getFalseValue(), BB)) { |
| 900 | if (pushBlockValue(std::make_pair(BB, SI->getFalseValue()))) |
| 901 | return false; |
| 902 | BBLV.markOverdefined(); |
| 903 | return true; |
| 904 | } |
| 905 | LVILatticeVal FalseVal = getBlockValue(SI->getFalseValue(), BB); |
| 906 | // If we hit overdefined, don't ask more queries. We want to avoid poisoning |
| 907 | // extra slots in the table if we can. |
| 908 | if (FalseVal.isOverdefined()) { |
| 909 | BBLV.markOverdefined(); |
| 910 | return true; |
| 911 | } |
| 912 | |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 913 | if (TrueVal.isConstantRange() && FalseVal.isConstantRange()) { |
| 914 | ConstantRange TrueCR = TrueVal.getConstantRange(); |
| 915 | ConstantRange FalseCR = FalseVal.getConstantRange(); |
| 916 | Value *LHS = nullptr; |
| 917 | Value *RHS = nullptr; |
| 918 | SelectPatternResult SPR = matchSelectPattern(SI, LHS, RHS); |
| 919 | // Is this a min specifically of our two inputs? (Avoid the risk of |
| 920 | // ValueTracking getting smarter looking back past our immediate inputs.) |
| 921 | if (SelectPatternResult::isMinOrMax(SPR.Flavor) && |
| 922 | LHS == SI->getTrueValue() && RHS == SI->getFalseValue()) { |
| 923 | switch (SPR.Flavor) { |
| 924 | default: |
| 925 | llvm_unreachable("unexpected minmax type!"); |
| 926 | case SPF_SMIN: /// Signed minimum |
| 927 | BBLV.markConstantRange(TrueCR.smin(FalseCR)); |
| 928 | return true; |
| 929 | case SPF_UMIN: /// Unsigned minimum |
| 930 | BBLV.markConstantRange(TrueCR.umin(FalseCR)); |
| 931 | return true; |
| 932 | case SPF_SMAX: /// Signed maximum |
| 933 | BBLV.markConstantRange(TrueCR.smax(FalseCR)); |
| 934 | return true; |
| 935 | case SPF_UMAX: /// Unsigned maximum |
| 936 | BBLV.markConstantRange(TrueCR.umax(FalseCR)); |
| 937 | return true; |
| 938 | }; |
| 939 | } |
| 940 | |
| 941 | // TODO: ABS, NABS from the SelectPatternResult |
| 942 | } |
| 943 | |
Philip Reames | 854a84c | 2016-02-12 00:09:18 +0000 | [diff] [blame] | 944 | // Can we constrain the facts about the true and false values by using the |
| 945 | // condition itself? This shows up with idioms like e.g. select(a > 5, a, 5). |
| 946 | // TODO: We could potentially refine an overdefined true value above. |
| 947 | if (auto *ICI = dyn_cast<ICmpInst>(SI->getCondition())) { |
| 948 | LVILatticeVal TrueValTaken, FalseValTaken; |
| 949 | if (!getValueFromFromCondition(SI->getTrueValue(), ICI, |
| 950 | TrueValTaken, true)) |
| 951 | TrueValTaken.markOverdefined(); |
| 952 | if (!getValueFromFromCondition(SI->getFalseValue(), ICI, |
| 953 | FalseValTaken, false)) |
| 954 | FalseValTaken.markOverdefined(); |
| 955 | |
| 956 | TrueVal = intersect(TrueVal, TrueValTaken); |
| 957 | FalseVal = intersect(FalseVal, FalseValTaken); |
Philip Reames | 854a84c | 2016-02-12 00:09:18 +0000 | [diff] [blame] | 958 | |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 959 | |
| 960 | // Handle clamp idioms such as: |
| 961 | // %24 = constantrange<0, 17> |
| 962 | // %39 = icmp eq i32 %24, 0 |
| 963 | // %40 = add i32 %24, -1 |
| 964 | // %siv.next = select i1 %39, i32 16, i32 %40 |
| 965 | // %siv.next = constantrange<0, 17> not <-1, 17> |
| 966 | // In general, this can handle any clamp idiom which tests the edge |
| 967 | // condition via an equality or inequality. |
| 968 | ICmpInst::Predicate Pred = ICI->getPredicate(); |
| 969 | Value *A = ICI->getOperand(0); |
| 970 | if (ConstantInt *CIBase = dyn_cast<ConstantInt>(ICI->getOperand(1))) { |
| 971 | auto addConstants = [](ConstantInt *A, ConstantInt *B) { |
| 972 | assert(A->getType() == B->getType()); |
| 973 | return ConstantInt::get(A->getType(), A->getValue() + B->getValue()); |
| 974 | }; |
| 975 | // See if either input is A + C2, subject to the constraint from the |
| 976 | // condition that A != C when that input is used. We can assume that |
| 977 | // that input doesn't include C + C2. |
| 978 | ConstantInt *CIAdded; |
| 979 | switch (Pred) { |
Philip Reames | 70b3918 | 2016-02-27 05:18:30 +0000 | [diff] [blame] | 980 | default: break; |
Philip Reames | adf0e35 | 2016-02-26 22:53:59 +0000 | [diff] [blame] | 981 | case ICmpInst::ICMP_EQ: |
| 982 | if (match(SI->getFalseValue(), m_Add(m_Specific(A), |
| 983 | m_ConstantInt(CIAdded)))) { |
| 984 | auto ResNot = addConstants(CIBase, CIAdded); |
| 985 | FalseVal = intersect(FalseVal, |
| 986 | LVILatticeVal::getNot(ResNot)); |
| 987 | } |
| 988 | break; |
| 989 | case ICmpInst::ICMP_NE: |
| 990 | if (match(SI->getTrueValue(), m_Add(m_Specific(A), |
| 991 | m_ConstantInt(CIAdded)))) { |
| 992 | auto ResNot = addConstants(CIBase, CIAdded); |
| 993 | TrueVal = intersect(TrueVal, |
| 994 | LVILatticeVal::getNot(ResNot)); |
| 995 | } |
| 996 | break; |
| 997 | }; |
| 998 | } |
| 999 | } |
Philip Reames | 854a84c | 2016-02-12 00:09:18 +0000 | [diff] [blame] | 1000 | |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 1001 | LVILatticeVal Result; // Start Undefined. |
| 1002 | Result.mergeIn(TrueVal, DL); |
| 1003 | Result.mergeIn(FalseVal, DL); |
Philip Reames | c0bdb0c | 2016-02-01 22:57:53 +0000 | [diff] [blame] | 1004 | BBLV = Result; |
| 1005 | return true; |
| 1006 | } |
| 1007 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 1008 | bool LazyValueInfoCache::solveBlockValueConstantRange(LVILatticeVal &BBLV, |
| 1009 | Instruction *BBI, |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1010 | BasicBlock *BB) { |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1011 | // Figure out the range of the LHS. If that fails, bail. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1012 | if (!hasBlockValue(BBI->getOperand(0), BB)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1013 | if (pushBlockValue(std::make_pair(BB, BBI->getOperand(0)))) |
| 1014 | return false; |
| 1015 | BBLV.markOverdefined(); |
| 1016 | return true; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1019 | LVILatticeVal LHSVal = getBlockValue(BBI->getOperand(0), BB); |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 1020 | intersectAssumeBlockValueConstantRange(BBI->getOperand(0), LHSVal, BBI); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1021 | if (!LHSVal.isConstantRange()) { |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 1022 | BBLV.markOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1023 | return true; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1024 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1025 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1026 | ConstantRange LHSRange = LHSVal.getConstantRange(); |
| 1027 | ConstantRange RHSRange(1); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1028 | IntegerType *ResultTy = cast<IntegerType>(BBI->getType()); |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1029 | if (isa<BinaryOperator>(BBI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1030 | if (ConstantInt *RHS = dyn_cast<ConstantInt>(BBI->getOperand(1))) { |
| 1031 | RHSRange = ConstantRange(RHS->getValue()); |
| 1032 | } else { |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 1033 | BBLV.markOverdefined(); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1034 | return true; |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1035 | } |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1036 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1037 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1038 | // NOTE: We're currently limited by the set of operations that ConstantRange |
| 1039 | // can evaluate symbolically. Enhancing that set will allows us to analyze |
| 1040 | // more definitions. |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 1041 | LVILatticeVal Result; |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1042 | switch (BBI->getOpcode()) { |
| 1043 | case Instruction::Add: |
| 1044 | Result.markConstantRange(LHSRange.add(RHSRange)); |
| 1045 | break; |
| 1046 | case Instruction::Sub: |
| 1047 | Result.markConstantRange(LHSRange.sub(RHSRange)); |
| 1048 | break; |
| 1049 | case Instruction::Mul: |
| 1050 | Result.markConstantRange(LHSRange.multiply(RHSRange)); |
| 1051 | break; |
| 1052 | case Instruction::UDiv: |
| 1053 | Result.markConstantRange(LHSRange.udiv(RHSRange)); |
| 1054 | break; |
| 1055 | case Instruction::Shl: |
| 1056 | Result.markConstantRange(LHSRange.shl(RHSRange)); |
| 1057 | break; |
| 1058 | case Instruction::LShr: |
| 1059 | Result.markConstantRange(LHSRange.lshr(RHSRange)); |
| 1060 | break; |
| 1061 | case Instruction::Trunc: |
| 1062 | Result.markConstantRange(LHSRange.truncate(ResultTy->getBitWidth())); |
| 1063 | break; |
| 1064 | case Instruction::SExt: |
| 1065 | Result.markConstantRange(LHSRange.signExtend(ResultTy->getBitWidth())); |
| 1066 | break; |
| 1067 | case Instruction::ZExt: |
| 1068 | Result.markConstantRange(LHSRange.zeroExtend(ResultTy->getBitWidth())); |
| 1069 | break; |
| 1070 | case Instruction::BitCast: |
| 1071 | Result.markConstantRange(LHSRange); |
| 1072 | break; |
Nick Lewycky | ad48e01 | 2010-09-07 05:39:02 +0000 | [diff] [blame] | 1073 | case Instruction::And: |
| 1074 | Result.markConstantRange(LHSRange.binaryAnd(RHSRange)); |
| 1075 | break; |
| 1076 | case Instruction::Or: |
| 1077 | Result.markConstantRange(LHSRange.binaryOr(RHSRange)); |
| 1078 | break; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1079 | |
Owen Anderson | 80d19f0 | 2010-08-18 21:11:37 +0000 | [diff] [blame] | 1080 | // Unhandled instructions are overdefined. |
| 1081 | default: |
| 1082 | DEBUG(dbgs() << " compute BB '" << BB->getName() |
| 1083 | << "' - overdefined because inst def found.\n"); |
| 1084 | Result.markOverdefined(); |
| 1085 | break; |
| 1086 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1087 | |
Owen Anderson | 64c2c57 | 2010-12-20 18:18:16 +0000 | [diff] [blame] | 1088 | BBLV = Result; |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1089 | return true; |
Chris Lattner | 741c94c | 2009-11-11 00:22:30 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1092 | bool getValueFromFromCondition(Value *Val, ICmpInst *ICI, |
| 1093 | LVILatticeVal &Result, bool isTrueDest) { |
| 1094 | if (ICI && isa<Constant>(ICI->getOperand(1))) { |
| 1095 | if (ICI->isEquality() && ICI->getOperand(0) == Val) { |
| 1096 | // We know that V has the RHS constant if this is a true SETEQ or |
| 1097 | // false SETNE. |
| 1098 | if (isTrueDest == (ICI->getPredicate() == ICmpInst::ICMP_EQ)) |
| 1099 | Result = LVILatticeVal::get(cast<Constant>(ICI->getOperand(1))); |
| 1100 | else |
| 1101 | Result = LVILatticeVal::getNot(cast<Constant>(ICI->getOperand(1))); |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
| 1105 | // Recognize the range checking idiom that InstCombine produces. |
| 1106 | // (X-C1) u< C2 --> [C1, C1+C2) |
| 1107 | ConstantInt *NegOffset = nullptr; |
| 1108 | if (ICI->getPredicate() == ICmpInst::ICMP_ULT) |
| 1109 | match(ICI->getOperand(0), m_Add(m_Specific(Val), |
| 1110 | m_ConstantInt(NegOffset))); |
| 1111 | |
| 1112 | ConstantInt *CI = dyn_cast<ConstantInt>(ICI->getOperand(1)); |
| 1113 | if (CI && (ICI->getOperand(0) == Val || NegOffset)) { |
Sanjoy Das | 7182d36 | 2015-03-18 00:41:24 +0000 | [diff] [blame] | 1114 | // Calculate the range of values that are allowed by the comparison |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1115 | ConstantRange CmpRange(CI->getValue()); |
| 1116 | ConstantRange TrueValues = |
Sanjoy Das | 7182d36 | 2015-03-18 00:41:24 +0000 | [diff] [blame] | 1117 | ConstantRange::makeAllowedICmpRegion(ICI->getPredicate(), CmpRange); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (NegOffset) // Apply the offset from above. |
| 1120 | TrueValues = TrueValues.subtract(NegOffset->getValue()); |
| 1121 | |
| 1122 | // If we're interested in the false dest, invert the condition. |
| 1123 | if (!isTrueDest) TrueValues = TrueValues.inverse(); |
| 1124 | |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 1125 | Result = LVILatticeVal::getRange(std::move(TrueValues)); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1126 | return true; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | return false; |
| 1131 | } |
| 1132 | |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1133 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo. Returns false if |
Philip Reames | 13f7324 | 2016-02-01 23:21:11 +0000 | [diff] [blame] | 1134 | /// Val is not constrained on the edge. Result is unspecified if return value |
| 1135 | /// is false. |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1136 | static bool getEdgeValueLocal(Value *Val, BasicBlock *BBFrom, |
| 1137 | BasicBlock *BBTo, LVILatticeVal &Result) { |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1138 | // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1139 | // know that v != 0. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1140 | if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) { |
| 1141 | // 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] | 1142 | // we may be able to infer something from the condition. |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1143 | if (BI->isConditional() && |
| 1144 | BI->getSuccessor(0) != BI->getSuccessor(1)) { |
| 1145 | bool isTrueDest = BI->getSuccessor(0) == BBTo; |
| 1146 | assert(BI->getSuccessor(!isTrueDest) == BBTo && |
| 1147 | "BBTo isn't a successor of BBFrom"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1148 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1149 | // If V is the condition of the branch itself, then we know exactly what |
| 1150 | // it is. |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1151 | if (BI->getCondition() == Val) { |
| 1152 | Result = LVILatticeVal::get(ConstantInt::get( |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1153 | Type::getInt1Ty(Val->getContext()), isTrueDest)); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1154 | return true; |
| 1155 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1156 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1157 | // If the condition of the branch is an equality comparison, we may be |
| 1158 | // able to infer the value. |
Sanjay Patel | d729115 | 2015-01-09 16:28:15 +0000 | [diff] [blame] | 1159 | if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) |
| 1160 | if (getValueFromFromCondition(Val, ICI, Result, isTrueDest)) |
| 1161 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1162 | } |
| 1163 | } |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1164 | |
| 1165 | // If the edge was formed by a switch on the value, then we may know exactly |
| 1166 | // what it is. |
| 1167 | if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) { |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 1168 | if (SI->getCondition() != Val) |
| 1169 | return false; |
| 1170 | |
| 1171 | bool DefaultCase = SI->getDefaultDest() == BBTo; |
| 1172 | unsigned BitWidth = Val->getType()->getIntegerBitWidth(); |
| 1173 | ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/); |
| 1174 | |
Hans Wennborg | cbb18e3 | 2014-11-21 19:07:46 +0000 | [diff] [blame] | 1175 | for (SwitchInst::CaseIt i : SI->cases()) { |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 1176 | ConstantRange EdgeVal(i.getCaseValue()->getValue()); |
Manman Ren | f3fedb6 | 2012-09-05 23:45:58 +0000 | [diff] [blame] | 1177 | if (DefaultCase) { |
| 1178 | // It is possible that the default destination is the destination of |
| 1179 | // some cases. There is no need to perform difference for those cases. |
| 1180 | if (i.getCaseSuccessor() != BBTo) |
| 1181 | EdgesVals = EdgesVals.difference(EdgeVal); |
| 1182 | } else if (i.getCaseSuccessor() == BBTo) |
Nuno Lopes | ac59380 | 2012-05-18 21:02:10 +0000 | [diff] [blame] | 1183 | EdgesVals = EdgesVals.unionWith(EdgeVal); |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1184 | } |
Benjamin Kramer | 2337c1f | 2016-02-20 10:40:34 +0000 | [diff] [blame] | 1185 | Result = LVILatticeVal::getRange(std::move(EdgesVals)); |
Nuno Lopes | 8650fb8 | 2012-06-28 16:13:37 +0000 | [diff] [blame] | 1186 | return true; |
Chris Lattner | 7735878 | 2009-11-15 20:02:12 +0000 | [diff] [blame] | 1187 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1188 | return false; |
| 1189 | } |
| 1190 | |
Sanjay Patel | 938e279 | 2015-01-09 16:35:37 +0000 | [diff] [blame] | 1191 | /// \brief Compute the value of Val on the edge BBFrom -> BBTo or the value at |
| 1192 | /// the basic block if the edge does not constrain Val. |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1193 | bool LazyValueInfoCache::getEdgeValue(Value *Val, BasicBlock *BBFrom, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1194 | BasicBlock *BBTo, LVILatticeVal &Result, |
| 1195 | Instruction *CxtI) { |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1196 | // If already a constant, there is nothing to compute. |
| 1197 | if (Constant *VC = dyn_cast<Constant>(Val)) { |
| 1198 | Result = LVILatticeVal::get(VC); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1199 | return true; |
| 1200 | } |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1201 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1202 | LVILatticeVal LocalResult; |
| 1203 | if (!getEdgeValueLocal(Val, BBFrom, BBTo, LocalResult)) |
| 1204 | // If we couldn't constrain the value on the edge, LocalResult doesn't |
| 1205 | // provide any information. |
| 1206 | LocalResult.markOverdefined(); |
| 1207 | |
| 1208 | if (hasSingleValue(LocalResult)) { |
| 1209 | // Can't get any more precise here |
| 1210 | Result = LocalResult; |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1211 | return true; |
| 1212 | } |
| 1213 | |
| 1214 | if (!hasBlockValue(Val, BBFrom)) { |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1215 | if (pushBlockValue(std::make_pair(BBFrom, Val))) |
| 1216 | return false; |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1217 | // No new information. |
| 1218 | Result = LocalResult; |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1219 | return true; |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1222 | // Try to intersect ranges of the BB and the constraint on the edge. |
| 1223 | LVILatticeVal InBlock = getBlockValue(Val, BBFrom); |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 1224 | intersectAssumeBlockValueConstantRange(Val, InBlock, BBFrom->getTerminator()); |
Hal Finkel | 2400c96 | 2014-10-16 00:40:05 +0000 | [diff] [blame] | 1225 | // We can use the context instruction (generically the ultimate instruction |
| 1226 | // the calling pass is trying to simplify) here, even though the result of |
| 1227 | // this function is generally cached when called from the solve* functions |
| 1228 | // (and that cached result might be used with queries using a different |
| 1229 | // context instruction), because when this function is called from the solve* |
| 1230 | // functions, the context instruction is not provided. When called from |
| 1231 | // LazyValueInfoCache::getValueOnEdge, the context instruction is provided, |
| 1232 | // but then the result is not cached. |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 1233 | intersectAssumeBlockValueConstantRange(Val, InBlock, CxtI); |
Philip Reames | 44456b8 | 2016-02-02 03:15:40 +0000 | [diff] [blame] | 1234 | |
| 1235 | Result = intersect(LocalResult, InBlock); |
Nuno Lopes | e6e0490 | 2012-06-28 01:16:18 +0000 | [diff] [blame] | 1236 | return true; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1239 | LVILatticeVal LazyValueInfoCache::getValueInBlock(Value *V, BasicBlock *BB, |
| 1240 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1241 | DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1242 | << BB->getName() << "'\n"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1243 | |
Hans Wennborg | 45172ac | 2014-11-25 17:23:05 +0000 | [diff] [blame] | 1244 | assert(BlockValueStack.empty() && BlockValueSet.empty()); |
Philip Reames | bb781b4 | 2016-02-10 21:46:32 +0000 | [diff] [blame] | 1245 | if (!hasBlockValue(V, BB)) { |
| 1246 | pushBlockValue(std::make_pair(BB, V)); |
| 1247 | solve(); |
| 1248 | } |
Owen Anderson | c7ed4dc | 2010-12-09 06:14:58 +0000 | [diff] [blame] | 1249 | LVILatticeVal Result = getBlockValue(V, BB); |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 1250 | intersectAssumeBlockValueConstantRange(V, Result, CxtI); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1251 | |
| 1252 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
| 1253 | return Result; |
| 1254 | } |
| 1255 | |
| 1256 | LVILatticeVal LazyValueInfoCache::getValueAt(Value *V, Instruction *CxtI) { |
| 1257 | DEBUG(dbgs() << "LVI Getting value " << *V << " at '" |
| 1258 | << CxtI->getName() << "'\n"); |
| 1259 | |
Philip Reames | bb781b4 | 2016-02-10 21:46:32 +0000 | [diff] [blame] | 1260 | if (auto *C = dyn_cast<Constant>(V)) |
| 1261 | return LVILatticeVal::get(C); |
| 1262 | |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 1263 | LVILatticeVal Result = LVILatticeVal::getOverdefined(); |
Philip Reames | eb3e9da | 2015-10-29 03:57:17 +0000 | [diff] [blame] | 1264 | if (auto *I = dyn_cast<Instruction>(V)) |
| 1265 | Result = getFromRangeMetadata(I); |
Philip Reames | d1f829d | 2016-02-02 21:57:37 +0000 | [diff] [blame] | 1266 | intersectAssumeBlockValueConstantRange(V, Result, CxtI); |
Philip Reames | 2c275cc | 2016-02-02 00:45:30 +0000 | [diff] [blame] | 1267 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1268 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1269 | return Result; |
| 1270 | } |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1272 | LVILatticeVal LazyValueInfoCache:: |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1273 | getValueOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, |
| 1274 | Instruction *CxtI) { |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1275 | DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '" |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1276 | << FromBB->getName() << "' to '" << ToBB->getName() << "'\n"); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1277 | |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1278 | LVILatticeVal Result; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1279 | if (!getEdgeValue(V, FromBB, ToBB, Result, CxtI)) { |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1280 | solve(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1281 | bool WasFastQuery = getEdgeValue(V, FromBB, ToBB, Result, CxtI); |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1282 | (void)WasFastQuery; |
| 1283 | assert(WasFastQuery && "More work to do after problem solved?"); |
| 1284 | } |
| 1285 | |
David Greene | 37e9809 | 2009-12-23 20:43:58 +0000 | [diff] [blame] | 1286 | DEBUG(dbgs() << " Result = " << Result << "\n"); |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1287 | return Result; |
| 1288 | } |
| 1289 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1290 | void LazyValueInfoCache::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
| 1291 | BasicBlock *NewSucc) { |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1292 | // When an edge in the graph has been threaded, values that we could not |
| 1293 | // determine a value for before (i.e. were marked overdefined) may be |
| 1294 | // possible to solve now. We do NOT try to proactively update these values. |
| 1295 | // Instead, we clear their entries from the cache, and allow lazy updating to |
| 1296 | // recompute them when needed. |
| 1297 | |
Hans Wennborg | c5ec73d | 2014-11-21 18:58:23 +0000 | [diff] [blame] | 1298 | // The updating process is fairly simple: we need to drop cached info |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1299 | // for all values that were marked overdefined in OldSucc, and for those same |
| 1300 | // values in any successor of OldSucc (except NewSucc) in which they were |
| 1301 | // also marked overdefined. |
| 1302 | std::vector<BasicBlock*> worklist; |
| 1303 | worklist.push_back(OldSucc); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1304 | |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 1305 | auto I = OverDefinedCache.find(OldSucc); |
| 1306 | if (I == OverDefinedCache.end()) |
| 1307 | return; // Nothing to process here. |
Bruno Cardoso Lopes | 7a1483e | 2015-08-21 21:18:26 +0000 | [diff] [blame] | 1308 | SmallVector<Value *, 4> ValsToClear(I->second.begin(), I->second.end()); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1309 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1310 | // Use a worklist to perform a depth-first search of OldSucc's successors. |
| 1311 | // NOTE: We do not need a visited list since any blocks we have already |
| 1312 | // visited will have had their overdefined markers cleared already, and we |
| 1313 | // thus won't loop to their successors. |
| 1314 | while (!worklist.empty()) { |
| 1315 | BasicBlock *ToUpdate = worklist.back(); |
| 1316 | worklist.pop_back(); |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1317 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1318 | // Skip blocks only accessible through NewSucc. |
| 1319 | if (ToUpdate == NewSucc) continue; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1320 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1321 | bool changed = false; |
Bruno Cardoso Lopes | 7a1483e | 2015-08-21 21:18:26 +0000 | [diff] [blame] | 1322 | for (Value *V : ValsToClear) { |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1323 | // If a value was marked overdefined in OldSucc, and is here too... |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 1324 | auto OI = OverDefinedCache.find(ToUpdate); |
| 1325 | if (OI == OverDefinedCache.end()) |
| 1326 | continue; |
| 1327 | SmallPtrSetImpl<Value *> &ValueSet = OI->second; |
| 1328 | if (!ValueSet.count(V)) |
| 1329 | continue; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1330 | |
Bruno Cardoso Lopes | 6ac4ea4 | 2015-08-18 16:34:27 +0000 | [diff] [blame] | 1331 | ValueSet.erase(V); |
| 1332 | if (ValueSet.empty()) |
| 1333 | OverDefinedCache.erase(OI); |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1334 | |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1335 | // If we removed anything, then we potentially need to update |
Owen Anderson | aac5a72 | 2010-07-27 23:58:11 +0000 | [diff] [blame] | 1336 | // blocks successors too. |
| 1337 | changed = true; |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1338 | } |
Nick Lewycky | 55a700b | 2010-12-18 01:00:40 +0000 | [diff] [blame] | 1339 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1340 | if (!changed) continue; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1341 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1342 | worklist.insert(worklist.end(), succ_begin(ToUpdate), succ_end(ToUpdate)); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1346 | //===----------------------------------------------------------------------===// |
| 1347 | // LazyValueInfo Impl |
| 1348 | //===----------------------------------------------------------------------===// |
| 1349 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1350 | /// This lazily constructs the LazyValueInfoCache. |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1351 | static LazyValueInfoCache &getCache(void *&PImpl, AssumptionCache *AC, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1352 | const DataLayout *DL, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1353 | DominatorTree *DT = nullptr) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1354 | if (!PImpl) { |
| 1355 | assert(DL && "getCache() called with a null DataLayout"); |
| 1356 | PImpl = new LazyValueInfoCache(AC, *DL, DT); |
| 1357 | } |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1358 | return *static_cast<LazyValueInfoCache*>(PImpl); |
| 1359 | } |
| 1360 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1361 | bool LazyValueInfo::runOnFunction(Function &F) { |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1362 | AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1363 | const DataLayout &DL = F.getParent()->getDataLayout(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1364 | |
| 1365 | DominatorTreeWrapperPass *DTWP = |
| 1366 | getAnalysisIfAvailable<DominatorTreeWrapperPass>(); |
| 1367 | DT = DTWP ? &DTWP->getDomTree() : nullptr; |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1368 | |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1369 | TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1370 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1371 | if (PImpl) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1372 | getCache(PImpl, AC, &DL, DT).clear(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1373 | |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1374 | // Fully lazy. |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1378 | void LazyValueInfo::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1379 | AU.setPreservesAll(); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1380 | AU.addRequired<AssumptionCacheTracker>(); |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 1381 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1384 | void LazyValueInfo::releaseMemory() { |
| 1385 | // If the cache was allocated, free it. |
| 1386 | if (PImpl) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1387 | delete &getCache(PImpl, AC, nullptr); |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1388 | PImpl = nullptr; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1392 | Constant *LazyValueInfo::getConstant(Value *V, BasicBlock *BB, |
| 1393 | Instruction *CxtI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1394 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1395 | LVILatticeVal Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1396 | getCache(PImpl, AC, &DL, DT).getValueInBlock(V, BB, CxtI); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1398 | if (Result.isConstant()) |
| 1399 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1400 | if (Result.isConstantRange()) { |
Owen Anderson | 38f6b7f | 2010-08-27 23:29:38 +0000 | [diff] [blame] | 1401 | ConstantRange CR = Result.getConstantRange(); |
| 1402 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1403 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1404 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1405 | return nullptr; |
Chris Lattner | 19019ea | 2009-11-11 22:48:44 +0000 | [diff] [blame] | 1406 | } |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1407 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1408 | /// Determine whether the specified value is known to be a |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1409 | /// constant on the specified edge. Return null if not. |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1410 | Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB, |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1411 | BasicBlock *ToBB, |
| 1412 | Instruction *CxtI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1413 | const DataLayout &DL = FromBB->getModule()->getDataLayout(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1414 | LVILatticeVal Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1415 | getCache(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
Chandler Carruth | 66b3130 | 2015-01-04 12:03:27 +0000 | [diff] [blame] | 1416 | |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1417 | if (Result.isConstant()) |
| 1418 | return Result.getConstant(); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1419 | if (Result.isConstantRange()) { |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1420 | ConstantRange CR = Result.getConstantRange(); |
| 1421 | if (const APInt *SingleVal = CR.getSingleElement()) |
| 1422 | return ConstantInt::get(V->getContext(), *SingleVal); |
| 1423 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1424 | return nullptr; |
Chris Lattner | d5e2543 | 2009-11-12 01:29:10 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1427 | static LazyValueInfo::Tristate getPredicateResult(unsigned Pred, Constant *C, |
| 1428 | LVILatticeVal &Result, |
| 1429 | const DataLayout &DL, |
| 1430 | TargetLibraryInfo *TLI) { |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1431 | |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1432 | // If we know the value is a constant, evaluate the conditional. |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 1433 | Constant *Res = nullptr; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1434 | if (Result.isConstant()) { |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1435 | Res = ConstantFoldCompareInstOperands(Pred, Result.getConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1436 | TLI); |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1437 | if (ConstantInt *ResCI = dyn_cast<ConstantInt>(Res)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1438 | return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True; |
| 1439 | return LazyValueInfo::Unknown; |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1440 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1441 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1442 | if (Result.isConstantRange()) { |
Owen Anderson | c62f704 | 2010-08-24 07:55:44 +0000 | [diff] [blame] | 1443 | ConstantInt *CI = dyn_cast<ConstantInt>(C); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1444 | if (!CI) return LazyValueInfo::Unknown; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1445 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1446 | ConstantRange CR = Result.getConstantRange(); |
| 1447 | if (Pred == ICmpInst::ICMP_EQ) { |
| 1448 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1449 | return LazyValueInfo::False; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1450 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1451 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1452 | return LazyValueInfo::True; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1453 | } else if (Pred == ICmpInst::ICMP_NE) { |
| 1454 | if (!CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1455 | return LazyValueInfo::True; |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1456 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1457 | if (CR.isSingleElement() && CR.contains(CI->getValue())) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1458 | return LazyValueInfo::False; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1459 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1460 | |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1461 | // Handle more complex predicates. |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1462 | ConstantRange TrueValues = |
| 1463 | ICmpInst::makeConstantRange((ICmpInst::Predicate)Pred, CI->getValue()); |
| 1464 | if (TrueValues.contains(CR)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1465 | return LazyValueInfo::True; |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1466 | if (TrueValues.inverse().contains(CR)) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1467 | return LazyValueInfo::False; |
| 1468 | return LazyValueInfo::Unknown; |
Owen Anderson | 185fe00 | 2010-08-10 20:03:09 +0000 | [diff] [blame] | 1469 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | af025d3 | 2009-11-15 19:59:49 +0000 | [diff] [blame] | 1471 | if (Result.isNotConstant()) { |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1472 | // If this is an equality comparison, we can try to fold it knowing that |
| 1473 | // "V != C1". |
| 1474 | if (Pred == ICmpInst::ICMP_EQ) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1475 | // !C1 == C -> false iff C1 == C. |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1476 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1477 | Result.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1478 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1479 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1480 | return LazyValueInfo::False; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1481 | } else if (Pred == ICmpInst::ICMP_NE) { |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1482 | // !C1 != C -> true iff C1 == C. |
Chris Lattner | b0c0a0d | 2009-11-15 20:01:24 +0000 | [diff] [blame] | 1483 | Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE, |
Rafael Espindola | 7c68beb | 2014-02-18 15:33:12 +0000 | [diff] [blame] | 1484 | Result.getNotConstant(), C, DL, |
Chad Rosier | 43a3306 | 2011-12-02 01:26:24 +0000 | [diff] [blame] | 1485 | TLI); |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1486 | if (Res->isNullValue()) |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1487 | return LazyValueInfo::True; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1488 | } |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1489 | return LazyValueInfo::Unknown; |
Chris Lattner | 565ee2f | 2009-11-12 04:36:58 +0000 | [diff] [blame] | 1490 | } |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1491 | |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1492 | return LazyValueInfo::Unknown; |
| 1493 | } |
| 1494 | |
Sanjay Patel | 2a385e2 | 2015-01-09 16:47:20 +0000 | [diff] [blame] | 1495 | /// Determine whether the specified value comparison with a constant is known to |
| 1496 | /// be true or false on the specified CFG edge. Pred is a CmpInst predicate. |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1497 | LazyValueInfo::Tristate |
| 1498 | LazyValueInfo::getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, |
| 1499 | BasicBlock *FromBB, BasicBlock *ToBB, |
| 1500 | Instruction *CxtI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1501 | const DataLayout &DL = FromBB->getModule()->getDataLayout(); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1502 | LVILatticeVal Result = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1503 | getCache(PImpl, AC, &DL, DT).getValueOnEdge(V, FromBB, ToBB, CxtI); |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1504 | |
| 1505 | return getPredicateResult(Pred, C, Result, DL, TLI); |
| 1506 | } |
| 1507 | |
| 1508 | LazyValueInfo::Tristate |
| 1509 | LazyValueInfo::getPredicateAt(unsigned Pred, Value *V, Constant *C, |
| 1510 | Instruction *CxtI) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1511 | const DataLayout &DL = CxtI->getModule()->getDataLayout(); |
| 1512 | LVILatticeVal Result = getCache(PImpl, AC, &DL, DT).getValueAt(V, CxtI); |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1513 | Tristate Ret = getPredicateResult(Pred, C, Result, DL, TLI); |
| 1514 | if (Ret != Unknown) |
| 1515 | return Ret; |
Hal Finkel | 7e18449 | 2014-09-07 20:29:59 +0000 | [diff] [blame] | 1516 | |
Philip Reames | aeefae0 | 2015-11-04 01:47:04 +0000 | [diff] [blame] | 1517 | // Note: The following bit of code is somewhat distinct from the rest of LVI; |
| 1518 | // LVI as a whole tries to compute a lattice value which is conservatively |
| 1519 | // correct at a given location. In this case, we have a predicate which we |
| 1520 | // weren't able to prove about the merged result, and we're pushing that |
| 1521 | // predicate back along each incoming edge to see if we can prove it |
| 1522 | // separately for each input. As a motivating example, consider: |
| 1523 | // bb1: |
| 1524 | // %v1 = ... ; constantrange<1, 5> |
| 1525 | // br label %merge |
| 1526 | // bb2: |
| 1527 | // %v2 = ... ; constantrange<10, 20> |
| 1528 | // br label %merge |
| 1529 | // merge: |
| 1530 | // %phi = phi [%v1, %v2] ; constantrange<1,20> |
| 1531 | // %pred = icmp eq i32 %phi, 8 |
| 1532 | // We can't tell from the lattice value for '%phi' that '%pred' is false |
| 1533 | // along each path, but by checking the predicate over each input separately, |
| 1534 | // we can. |
| 1535 | // We limit the search to one step backwards from the current BB and value. |
| 1536 | // We could consider extending this to search further backwards through the |
| 1537 | // CFG and/or value graph, but there are non-obvious compile time vs quality |
| 1538 | // tradeoffs. |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1539 | if (CxtI) { |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1540 | BasicBlock *BB = CxtI->getParent(); |
| 1541 | |
| 1542 | // Function entry or an unreachable block. Bail to avoid confusing |
| 1543 | // analysis below. |
| 1544 | pred_iterator PI = pred_begin(BB), PE = pred_end(BB); |
| 1545 | if (PI == PE) |
| 1546 | return Unknown; |
| 1547 | |
| 1548 | // If V is a PHI node in the same block as the context, we need to ask |
| 1549 | // questions about the predicate as applied to the incoming value along |
| 1550 | // each edge. This is useful for eliminating cases where the predicate is |
| 1551 | // known along all incoming edges. |
| 1552 | if (auto *PHI = dyn_cast<PHINode>(V)) |
| 1553 | if (PHI->getParent() == BB) { |
| 1554 | Tristate Baseline = Unknown; |
| 1555 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) { |
| 1556 | Value *Incoming = PHI->getIncomingValue(i); |
| 1557 | BasicBlock *PredBB = PHI->getIncomingBlock(i); |
| 1558 | // Note that PredBB may be BB itself. |
| 1559 | Tristate Result = getPredicateOnEdge(Pred, Incoming, C, PredBB, BB, |
| 1560 | CxtI); |
| 1561 | |
| 1562 | // Keep going as long as we've seen a consistent known result for |
| 1563 | // all inputs. |
| 1564 | Baseline = (i == 0) ? Result /* First iteration */ |
| 1565 | : (Baseline == Result ? Baseline : Unknown); /* All others */ |
| 1566 | if (Baseline == Unknown) |
| 1567 | break; |
| 1568 | } |
| 1569 | if (Baseline != Unknown) |
| 1570 | return Baseline; |
| 1571 | } |
| 1572 | |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1573 | // For a comparison where the V is outside this block, it's possible |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1574 | // that we've branched on it before. Look to see if the value is known |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1575 | // on all incoming edges. |
Philip Reames | bb11d62 | 2015-08-31 18:31:48 +0000 | [diff] [blame] | 1576 | if (!isa<Instruction>(V) || |
| 1577 | cast<Instruction>(V)->getParent() != BB) { |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1578 | // For predecessor edge, determine if the comparison is true or false |
Bruno Cardoso Lopes | 51fd242 | 2015-07-28 15:53:21 +0000 | [diff] [blame] | 1579 | // on that edge. If they're all true or all false, we can conclude |
Philip Reames | 66ab0f0 | 2015-06-16 00:49:59 +0000 | [diff] [blame] | 1580 | // the value of the comparison in this block. |
| 1581 | Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); |
| 1582 | if (Baseline != Unknown) { |
| 1583 | // Check that all remaining incoming values match the first one. |
| 1584 | while (++PI != PE) { |
| 1585 | Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI); |
| 1586 | if (Ret != Baseline) break; |
| 1587 | } |
| 1588 | // If we terminated early, then one of the values didn't match. |
| 1589 | if (PI == PE) { |
| 1590 | return Baseline; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | return Unknown; |
Chris Lattner | fde1f8d | 2009-11-11 02:08:33 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1598 | void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, |
Nick Lewycky | 11678bd | 2010-12-15 18:57:18 +0000 | [diff] [blame] | 1599 | BasicBlock *NewSucc) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1600 | if (PImpl) { |
| 1601 | const DataLayout &DL = PredBB->getModule()->getDataLayout(); |
| 1602 | getCache(PImpl, AC, &DL, DT).threadEdge(PredBB, OldSucc, NewSucc); |
| 1603 | } |
Owen Anderson | 208636f | 2010-08-18 18:39:01 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | void LazyValueInfo::eraseBlock(BasicBlock *BB) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1607 | if (PImpl) { |
| 1608 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 1609 | getCache(PImpl, AC, &DL, DT).eraseBlock(BB); |
| 1610 | } |
Owen Anderson | aa7f66b | 2010-07-26 18:48:03 +0000 | [diff] [blame] | 1611 | } |