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