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