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