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