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