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