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