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