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