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