Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceOperand.h - High-level operands -----------*- C++ -*-===// |
| 2 | // |
| 3 | // The Subzero Code Generator |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Declares the Operand class and its target-independent subclasses. |
| 12 | /// |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 13 | /// The main classes are Variable, which represents an LLVM variable that is |
| 14 | /// either register- or stack-allocated, and the Constant hierarchy, which |
| 15 | /// represents integer, floating-point, and/or symbolic constants. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 16 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #ifndef SUBZERO_SRC_ICEOPERAND_H |
| 20 | #define SUBZERO_SRC_ICEOPERAND_H |
| 21 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 22 | #include "IceCfg.h" |
Antonio Maiorano | debdfa2 | 2020-11-10 16:28:34 -0500 | [diff] [blame] | 23 | #include "IceDefs.h" |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 24 | #include "IceGlobalContext.h" |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 25 | #include "IceStringPool.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 26 | #include "IceTypes.h" |
| 27 | |
Nicolas Capens | 4d70802 | 2016-09-07 15:03:32 -0400 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 29 | #include "llvm/Support/Format.h" |
| 30 | |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 31 | #include <limits> |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 32 | #include <type_traits> |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 33 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 34 | namespace Ice { |
| 35 | |
| 36 | class Operand { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 37 | Operand() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 38 | Operand(const Operand &) = delete; |
| 39 | Operand &operator=(const Operand &) = delete; |
| 40 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 41 | public: |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 42 | static constexpr size_t MaxTargetKinds = 10; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 43 | enum OperandKind { |
| 44 | kConst_Base, |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 45 | kConstInteger32, |
| 46 | kConstInteger64, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 47 | kConstFloat, |
| 48 | kConstDouble, |
| 49 | kConstRelocatable, |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 50 | kConstUndef, |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 51 | kConst_Target, // leave space for target-specific constant kinds |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 52 | kConst_Max = kConst_Target + MaxTargetKinds, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 53 | kVariable, |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 54 | kVariable64On32, |
Jaydeep Patil | 958ddb7 | 2016-10-03 07:52:48 -0700 | [diff] [blame] | 55 | kVariableVecOn32, |
Eric Holk | 80ee5b3 | 2016-05-06 14:28:04 -0700 | [diff] [blame] | 56 | kVariableBoolean, |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 57 | kVariable_Target, // leave space for target-specific variable kinds |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 58 | kVariable_Max = kVariable_Target + MaxTargetKinds, |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 59 | // Target-specific operand classes use kTarget as the starting point for |
| 60 | // their Kind enum space. Note that the value-spaces are shared across |
| 61 | // targets. To avoid confusion over the definition of shared values, an |
| 62 | // object specific to one target should never be passed to a different |
| 63 | // target. |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 64 | kTarget, |
| 65 | kTarget_Max = std::numeric_limits<uint8_t>::max(), |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 66 | }; |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 67 | static_assert(kTarget <= kTarget_Max, "Must not be above max."); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 68 | OperandKind getKind() const { return Kind; } |
| 69 | Type getType() const { return Ty; } |
| 70 | |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 71 | /// Every Operand keeps an array of the Variables referenced in the operand. |
| 72 | /// This is so that the liveness operations can get quick access to the |
| 73 | /// variables of interest, without having to dig so far into the operand. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 74 | SizeT getNumVars() const { return NumVars; } |
| 75 | Variable *getVar(SizeT I) const { |
| 76 | assert(I < getNumVars()); |
| 77 | return Vars[I]; |
| 78 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 79 | virtual void emit(const Cfg *Func) const = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 80 | |
| 81 | /// \name Dumping functions. |
| 82 | /// @{ |
| 83 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 84 | /// The dump(Func,Str) implementation must be sure to handle the situation |
| 85 | /// where Func==nullptr. |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 86 | virtual void dump(const Cfg *Func, Ostream &Str) const = 0; |
| 87 | void dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 88 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 89 | return; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 90 | assert(Func); |
| 91 | dump(Func, Func->getContext()->getStrDump()); |
| 92 | } |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 93 | void dump(Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 94 | if (BuildDefs::dump()) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 95 | dump(nullptr, Str); |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 96 | } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 97 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 98 | |
Sean Klein | fc707ff | 2016-02-29 16:44:07 -0800 | [diff] [blame] | 99 | virtual ~Operand() = default; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 100 | |
Eric Holk | 80ee5b3 | 2016-05-06 14:28:04 -0700 | [diff] [blame] | 101 | virtual Variable *asBoolean() { return nullptr; } |
| 102 | |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 103 | virtual SizeT hashValue() const { |
| 104 | llvm::report_fatal_error("Tried to hash unsupported operand type : " + |
| 105 | std::to_string(Kind)); |
| 106 | return 0; |
| 107 | } |
| 108 | |
Antonio Maiorano | debdfa2 | 2020-11-10 16:28:34 -0500 | [diff] [blame] | 109 | inline void *getExternalData() const { return externalData; } |
| 110 | inline void setExternalData(void *data) { externalData = data; } |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 111 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 112 | protected: |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 113 | Operand(OperandKind Kind, Type Ty) : Ty(Ty), Kind(Kind) { |
| 114 | // It is undefined behavior to have a larger value in the enum |
| 115 | assert(Kind <= kTarget_Max); |
| 116 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 117 | |
| 118 | const Type Ty; |
| 119 | const OperandKind Kind; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 120 | /// Vars and NumVars are initialized by the derived class. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 121 | SizeT NumVars = 0; |
| 122 | Variable **Vars = nullptr; |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 123 | |
| 124 | /// External data can be set by an optimizer to compute and retain any |
| 125 | /// information related to the current operand. All the memory used to |
| 126 | /// store this information must be managed by the optimizer. |
Antonio Maiorano | debdfa2 | 2020-11-10 16:28:34 -0500 | [diff] [blame] | 127 | void *externalData = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 130 | template <class StreamType> |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 131 | inline StreamType &operator<<(StreamType &Str, const Operand &Op) { |
| 132 | Op.dump(Str); |
| 133 | return Str; |
| 134 | } |
| 135 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 136 | /// Constant is the abstract base class for constants. All constants are |
| 137 | /// allocated from a global arena and are pooled. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 138 | class Constant : public Operand { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 139 | Constant() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 140 | Constant(const Constant &) = delete; |
| 141 | Constant &operator=(const Constant &) = delete; |
| 142 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 143 | public: |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 144 | // Declare the lookup counter to take minimal space in a non-DUMP build. |
| 145 | using CounterType = |
| 146 | std::conditional<BuildDefs::dump(), uint64_t, uint8_t>::type; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 147 | void emit(const Cfg *Func) const override { emit(Func->getTarget()); } |
| 148 | virtual void emit(TargetLowering *Target) const = 0; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 149 | |
| 150 | static bool classof(const Operand *Operand) { |
| 151 | OperandKind Kind = Operand->getKind(); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 152 | return Kind >= kConst_Base && Kind <= kConst_Max; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 155 | const GlobalString getLabelName() const { return LabelName; } |
| 156 | |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 157 | bool getShouldBePooled() const { return ShouldBePooled; } |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 158 | |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 159 | // This should be thread-safe because the constant pool lock is acquired |
| 160 | // before the method is invoked. |
| 161 | void updateLookupCount() { |
| 162 | if (!BuildDefs::dump()) |
| 163 | return; |
| 164 | ++LookupCount; |
| 165 | } |
| 166 | CounterType getLookupCount() const { return LookupCount; } |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 167 | SizeT hashValue() const override { return 0; } |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 168 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 169 | protected: |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 170 | Constant(OperandKind Kind, Type Ty) : Operand(Kind, Ty) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 171 | Vars = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 172 | NumVars = 0; |
| 173 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 174 | /// Set the ShouldBePooled field to the proper value after the object is fully |
| 175 | /// initialized. |
| 176 | void initShouldBePooled(); |
| 177 | GlobalString LabelName; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 178 | /// Whether we should pool this constant. Usually Float/Double and pooled |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 179 | /// Integers should be flagged true. Ideally this field would be const, but |
| 180 | /// it needs to be initialized only after the subclass is fully constructed. |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 181 | bool ShouldBePooled = false; |
| 182 | /// Note: If ShouldBePooled is ever removed from the base class, we will want |
| 183 | /// to completely disable LookupCount in a non-DUMP build to save space. |
| 184 | CounterType LookupCount = 0; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 187 | /// ConstantPrimitive<> wraps a primitive type. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 188 | template <typename T, Operand::OperandKind K> |
| 189 | class ConstantPrimitive : public Constant { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 190 | ConstantPrimitive() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 191 | ConstantPrimitive(const ConstantPrimitive &) = delete; |
| 192 | ConstantPrimitive &operator=(const ConstantPrimitive &) = delete; |
| 193 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 194 | public: |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 195 | using PrimType = T; |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 196 | |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 197 | static ConstantPrimitive *create(GlobalContext *Ctx, Type Ty, |
| 198 | PrimType Value) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 199 | auto *Const = |
| 200 | new (Ctx->allocate<ConstantPrimitive>()) ConstantPrimitive(Ty, Value); |
| 201 | Const->initShouldBePooled(); |
| 202 | if (Const->getShouldBePooled()) |
| 203 | Const->initName(Ctx); |
| 204 | return Const; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 205 | } |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 206 | PrimType getValue() const { return Value; } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 207 | using Constant::emit; |
| 208 | void emit(TargetLowering *Target) const final; |
| 209 | using Constant::dump; |
| 210 | void dump(const Cfg *, Ostream &Str) const override { |
| 211 | if (BuildDefs::dump()) |
| 212 | Str << getValue(); |
| 213 | } |
| 214 | |
| 215 | static bool classof(const Operand *Operand) { |
| 216 | return Operand->getKind() == K; |
| 217 | } |
| 218 | |
Sagar Thakur | e160ed9 | 2016-05-30 07:54:47 -0700 | [diff] [blame] | 219 | SizeT hashValue() const override { return std::hash<PrimType>()(Value); } |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 220 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 221 | private: |
| 222 | ConstantPrimitive(Type Ty, PrimType Value) : Constant(K, Ty), Value(Value) {} |
| 223 | |
| 224 | void initName(GlobalContext *Ctx) { |
| 225 | std::string Buffer; |
| 226 | llvm::raw_string_ostream Str(Buffer); |
Jim Stichnoth | e922c23 | 2016-04-09 08:54:20 -0700 | [diff] [blame] | 227 | constexpr bool IsCompact = !BuildDefs::dump(); |
| 228 | if (IsCompact) { |
| 229 | switch (getType()) { |
| 230 | case IceType_f32: |
| 231 | Str << "$F"; |
| 232 | break; |
| 233 | case IceType_f64: |
| 234 | Str << "$D"; |
| 235 | break; |
| 236 | default: |
| 237 | // For constant pooling diversification |
| 238 | Str << ".L$" << getType() << "$"; |
| 239 | break; |
| 240 | } |
| 241 | } else { |
| 242 | Str << ".L$" << getType() << "$"; |
| 243 | } |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 244 | // Print hex characters byte by byte, starting from the most significant |
| 245 | // byte. NOTE: This ordering assumes Subzero runs on a little-endian |
| 246 | // platform. That means the possibility of different label names depending |
| 247 | // on the endian-ness of the platform where Subzero runs. |
| 248 | for (unsigned i = 0; i < sizeof(Value); ++i) { |
| 249 | constexpr unsigned HexWidthChars = 2; |
| 250 | unsigned Offset = sizeof(Value) - 1 - i; |
| 251 | Str << llvm::format_hex_no_prefix( |
| 252 | *(Offset + (const unsigned char *)&Value), HexWidthChars); |
| 253 | } |
| 254 | // For a floating-point value in DecorateAsm mode, also append the value in |
| 255 | // human-readable sprintf form, changing '+' to 'p' and '-' to 'm' to |
| 256 | // maintain valid asm labels. |
Jim Stichnoth | e922c23 | 2016-04-09 08:54:20 -0700 | [diff] [blame] | 257 | if (BuildDefs::dump() && std::is_floating_point<PrimType>::value && |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 258 | getFlags().getDecorateAsm()) { |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 259 | char Buf[30]; |
| 260 | snprintf(Buf, llvm::array_lengthof(Buf), "$%g", (double)Value); |
| 261 | for (unsigned i = 0; i < llvm::array_lengthof(Buf) && Buf[i]; ++i) { |
| 262 | if (Buf[i] == '-') |
| 263 | Buf[i] = 'm'; |
| 264 | else if (Buf[i] == '+') |
| 265 | Buf[i] = 'p'; |
| 266 | } |
| 267 | Str << Buf; |
| 268 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 269 | LabelName = GlobalString::createWithString(Ctx, Str.str()); |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 270 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 271 | |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 272 | const PrimType Value; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 275 | using ConstantInteger32 = ConstantPrimitive<int32_t, Operand::kConstInteger32>; |
| 276 | using ConstantInteger64 = ConstantPrimitive<int64_t, Operand::kConstInteger64>; |
| 277 | using ConstantFloat = ConstantPrimitive<float, Operand::kConstFloat>; |
| 278 | using ConstantDouble = ConstantPrimitive<double, Operand::kConstDouble>; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 279 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 280 | template <> |
| 281 | inline void ConstantInteger32::dump(const Cfg *, Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 282 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 283 | return; |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 284 | if (getType() == IceType_i1) |
| 285 | Str << (getValue() ? "true" : "false"); |
| 286 | else |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 287 | Str << static_cast<int32_t>(getValue()); |
| 288 | } |
| 289 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 290 | template <> |
| 291 | inline void ConstantInteger64::dump(const Cfg *, Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 292 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 293 | return; |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 294 | assert(getType() == IceType_i64); |
| 295 | Str << static_cast<int64_t>(getValue()); |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 296 | } |
| 297 | |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 298 | /// RelocOffset allows symbolic references in ConstantRelocatables' offsets, |
| 299 | /// e.g., 8 + LabelOffset, where label offset is the location (code or data) |
| 300 | /// of a Label that is only determinable during ELF emission. |
| 301 | class RelocOffset final { |
| 302 | RelocOffset(const RelocOffset &) = delete; |
| 303 | RelocOffset &operator=(const RelocOffset &) = delete; |
| 304 | |
| 305 | public: |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 306 | template <typename T> static RelocOffset *create(T *AllocOwner) { |
| 307 | return new (AllocOwner->template allocate<RelocOffset>()) RelocOffset(); |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static RelocOffset *create(GlobalContext *Ctx, RelocOffsetT Value) { |
| 311 | return new (Ctx->allocate<RelocOffset>()) RelocOffset(Value); |
| 312 | } |
| 313 | |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 314 | void setSubtract(bool Value) { Subtract = Value; } |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 315 | bool hasOffset() const { return HasOffset; } |
| 316 | |
| 317 | RelocOffsetT getOffset() const { |
| 318 | assert(HasOffset); |
| 319 | return Offset; |
| 320 | } |
| 321 | |
| 322 | void setOffset(const RelocOffsetT Value) { |
| 323 | assert(!HasOffset); |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 324 | if (Subtract) { |
| 325 | assert(Value != std::numeric_limits<RelocOffsetT>::lowest()); |
| 326 | Offset = -Value; |
| 327 | } else { |
| 328 | Offset = Value; |
| 329 | } |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 330 | HasOffset = true; |
| 331 | } |
| 332 | |
| 333 | private: |
| 334 | RelocOffset() = default; |
| 335 | explicit RelocOffset(RelocOffsetT Offset) { setOffset(Offset); } |
| 336 | |
John Porto | 6e8d3fa | 2016-02-04 10:35:20 -0800 | [diff] [blame] | 337 | bool Subtract = false; |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 338 | bool HasOffset = false; |
| 339 | RelocOffsetT Offset; |
| 340 | }; |
| 341 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 342 | /// RelocatableTuple bundles the parameters that are used to construct an |
| 343 | /// ConstantRelocatable. It is done this way so that ConstantRelocatable can fit |
| 344 | /// into the global constant pool template mechanism. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 345 | class RelocatableTuple { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 346 | RelocatableTuple() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 347 | RelocatableTuple &operator=(const RelocatableTuple &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 348 | |
| 349 | public: |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 350 | RelocatableTuple(const RelocOffsetT Offset, |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 351 | const RelocOffsetArray &OffsetExpr, GlobalString Name) |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 352 | : Offset(Offset), OffsetExpr(OffsetExpr), Name(Name) {} |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 353 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 354 | RelocatableTuple(const RelocOffsetT Offset, |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 355 | const RelocOffsetArray &OffsetExpr, GlobalString Name, |
| 356 | const std::string &EmitString) |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 357 | : Offset(Offset), OffsetExpr(OffsetExpr), Name(Name), |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 358 | EmitString(EmitString) {} |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 359 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 360 | RelocatableTuple(const RelocatableTuple &) = default; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 361 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 362 | const RelocOffsetT Offset; |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 363 | const RelocOffsetArray OffsetExpr; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 364 | const GlobalString Name; |
| 365 | const std::string EmitString; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 368 | bool operator==(const RelocatableTuple &A, const RelocatableTuple &B); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 369 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 370 | /// ConstantRelocatable represents a symbolic constant combined with a fixed |
| 371 | /// offset. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 372 | class ConstantRelocatable : public Constant { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 373 | ConstantRelocatable() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 374 | ConstantRelocatable(const ConstantRelocatable &) = delete; |
| 375 | ConstantRelocatable &operator=(const ConstantRelocatable &) = delete; |
| 376 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 377 | public: |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 378 | template <typename T> |
| 379 | static ConstantRelocatable *create(T *AllocOwner, Type Ty, |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 380 | const RelocatableTuple &Tuple) { |
Jim Stichnoth | 3e32400 | 2016-03-08 16:18:40 -0800 | [diff] [blame] | 381 | return new (AllocOwner->template allocate<ConstantRelocatable>()) |
| 382 | ConstantRelocatable(Ty, Tuple.Offset, Tuple.OffsetExpr, Tuple.Name, |
| 383 | Tuple.EmitString); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 384 | } |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 385 | |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 386 | RelocOffsetT getOffset() const { |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 387 | RelocOffsetT Ret = Offset; |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 388 | for (const auto *const OffsetReloc : OffsetExpr) { |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 389 | Ret += OffsetReloc->getOffset(); |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 390 | } |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 391 | return Ret; |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 392 | } |
| 393 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 394 | const std::string &getEmitString() const { return EmitString; } |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 395 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 396 | GlobalString getName() const { return Name; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 397 | using Constant::emit; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 398 | void emit(TargetLowering *Target) const final; |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame] | 399 | void emitWithoutPrefix(const TargetLowering *Target, |
| 400 | const char *Suffix = "") const; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 401 | using Constant::dump; |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 402 | void dump(const Cfg *Func, Ostream &Str) const override; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 403 | |
| 404 | static bool classof(const Operand *Operand) { |
| 405 | OperandKind Kind = Operand->getKind(); |
| 406 | return Kind == kConstRelocatable; |
| 407 | } |
| 408 | |
| 409 | private: |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 410 | ConstantRelocatable(Type Ty, const RelocOffsetT Offset, |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 411 | const RelocOffsetArray &OffsetExpr, GlobalString Name, |
| 412 | const std::string &EmitString) |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 413 | : Constant(kConstRelocatable, Ty), Offset(Offset), OffsetExpr(OffsetExpr), |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 414 | Name(Name), EmitString(EmitString) {} |
John Porto | 27fddcc | 2016-02-02 15:06:09 -0800 | [diff] [blame] | 415 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 416 | const RelocOffsetT Offset; /// fixed, known offset to add |
| 417 | const RelocOffsetArray OffsetExpr; /// fixed, unknown offset to add |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 418 | const GlobalString Name; /// optional for debug/dump |
| 419 | const std::string EmitString; /// optional for textual emission |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 420 | }; |
| 421 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 422 | /// ConstantUndef represents an unspecified bit pattern. Although it is legal to |
| 423 | /// lower ConstantUndef to any value, backends should try to make code |
| 424 | /// generation deterministic by lowering ConstantUndefs to 0. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 425 | class ConstantUndef : public Constant { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 426 | ConstantUndef() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 427 | ConstantUndef(const ConstantUndef &) = delete; |
| 428 | ConstantUndef &operator=(const ConstantUndef &) = delete; |
| 429 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 430 | public: |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 431 | static ConstantUndef *create(GlobalContext *Ctx, Type Ty) { |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 432 | return new (Ctx->allocate<ConstantUndef>()) ConstantUndef(Ty); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | using Constant::emit; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 436 | void emit(TargetLowering *Target) const final; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 437 | using Constant::dump; |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 438 | void dump(const Cfg *, Ostream &Str) const override { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 439 | if (BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 440 | Str << "undef"; |
| 441 | } |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 442 | |
| 443 | static bool classof(const Operand *Operand) { |
| 444 | return Operand->getKind() == kConstUndef; |
| 445 | } |
| 446 | |
| 447 | private: |
Jim Stichnoth | b36757e | 2015-10-05 13:55:11 -0700 | [diff] [blame] | 448 | ConstantUndef(Type Ty) : Constant(kConstUndef, Ty) {} |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 449 | }; |
| 450 | |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 451 | /// RegNumT is for holding target-specific register numbers, plus the sentinel |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 452 | /// value if no register is assigned. Its public ctor allows direct use of enum |
| 453 | /// values, such as RegNumT(Reg_eax), but not things like RegNumT(Reg_eax+1). |
| 454 | /// This is to try to prevent inappropriate assumptions about enum ordering. If |
| 455 | /// needed, the fromInt() method can be used, such as when a RegNumT is based |
| 456 | /// on a bitvector index. |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 457 | class RegNumT { |
| 458 | public: |
| 459 | using BaseType = uint32_t; |
| 460 | RegNumT() = default; |
| 461 | RegNumT(const RegNumT &) = default; |
| 462 | template <typename AnyEnum> |
| 463 | RegNumT(AnyEnum Value, |
| 464 | typename std::enable_if<std::is_enum<AnyEnum>::value, int>::type = 0) |
| 465 | : Value(Value) { |
| 466 | validate(Value); |
| 467 | } |
| 468 | RegNumT &operator=(const RegNumT &) = default; |
| 469 | operator unsigned() const { return Value; } |
| 470 | /// Asserts that the register is valid, i.e. not NoRegisterValue. Note that |
| 471 | /// the ctor already does the target-specific limit check. |
| 472 | void assertIsValid() const { assert(Value != NoRegisterValue); } |
| 473 | static RegNumT fromInt(BaseType Value) { return RegNumT(Value); } |
| 474 | /// Marks cases that inappropriately add/subtract RegNumT values, and |
| 475 | /// therefore need to be fixed because they make assumptions about register |
| 476 | /// enum value ordering. TODO(stichnot): Remove fixme() as soon as all |
| 477 | /// current uses are fixed/removed. |
| 478 | static RegNumT fixme(BaseType Value) { return RegNumT(Value); } |
| 479 | /// The target's staticInit() method should call setLimit() to register the |
| 480 | /// upper bound of allowable values. |
| 481 | static void setLimit(BaseType Value) { |
| 482 | // Make sure it's only called once. |
| 483 | assert(Limit == 0); |
| 484 | assert(Value != 0); |
| 485 | Limit = Value; |
| 486 | } |
| 487 | // Define NoRegisterValue as an enum value so that it can be used as an |
| 488 | // argument for the public ctor if desired. |
Nicolas Capens | 5c4d677 | 2017-01-18 16:57:52 -0500 | [diff] [blame] | 489 | enum : BaseType { NoRegisterValue = std::numeric_limits<BaseType>::max() }; |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 490 | |
| 491 | bool hasValue() const { return Value != NoRegisterValue; } |
| 492 | bool hasNoValue() const { return !hasValue(); } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 493 | |
| 494 | private: |
| 495 | BaseType Value = NoRegisterValue; |
| 496 | static BaseType Limit; |
| 497 | /// Private ctor called only by fromInt() and fixme(). |
| 498 | RegNumT(BaseType Value) : Value(Value) { validate(Value); } |
| 499 | /// The ctor calls this to validate against the target-supplied limit. |
| 500 | static void validate(BaseType Value) { |
| 501 | (void)Value; |
| 502 | assert(Value == NoRegisterValue || Value < Limit); |
| 503 | } |
| 504 | /// Disallow operators that inappropriately make assumptions about register |
| 505 | /// enum value ordering. |
| 506 | bool operator<(const RegNumT &) = delete; |
| 507 | bool operator<=(const RegNumT &) = delete; |
| 508 | bool operator>(const RegNumT &) = delete; |
| 509 | bool operator>=(const RegNumT &) = delete; |
| 510 | }; |
| 511 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 512 | /// RegNumBVIter wraps SmallBitVector so that instead of this pattern: |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 513 | /// |
| 514 | /// for (int i = V.find_first(); i != -1; i = V.find_next(i)) { |
| 515 | /// RegNumT RegNum = RegNumT::fromInt(i); |
| 516 | /// ... |
| 517 | /// } |
| 518 | /// |
| 519 | /// this cleaner pattern can be used: |
| 520 | /// |
| 521 | /// for (RegNumT RegNum : RegNumBVIter(V)) { |
| 522 | /// ... |
| 523 | /// } |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 524 | template <class B> class RegNumBVIterImpl { |
| 525 | using T = B; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 526 | static constexpr int Sentinel = -1; |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 527 | RegNumBVIterImpl() = delete; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 528 | |
| 529 | public: |
| 530 | class Iterator { |
| 531 | Iterator() = delete; |
| 532 | Iterator &operator=(const Iterator &) = delete; |
| 533 | |
| 534 | public: |
| 535 | explicit Iterator(const T &V) : V(V), Current(V.find_first()) {} |
| 536 | Iterator(const T &V, int Value) : V(V), Current(Value) {} |
| 537 | Iterator(const Iterator &) = default; |
| 538 | RegNumT operator*() { |
| 539 | assert(Current != Sentinel); |
| 540 | return RegNumT::fromInt(Current); |
| 541 | } |
| 542 | Iterator &operator++() { |
| 543 | assert(Current != Sentinel); |
| 544 | Current = V.find_next(Current); |
| 545 | return *this; |
| 546 | } |
| 547 | bool operator!=(Iterator &Other) { return Current != Other.Current; } |
| 548 | |
| 549 | private: |
| 550 | const T &V; |
| 551 | int Current; |
| 552 | }; |
| 553 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 554 | RegNumBVIterImpl(const RegNumBVIterImpl &) = default; |
| 555 | RegNumBVIterImpl &operator=(const RegNumBVIterImpl &) = delete; |
| 556 | explicit RegNumBVIterImpl(const T &V) : V(V) {} |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 557 | Iterator begin() { return Iterator(V); } |
| 558 | Iterator end() { return Iterator(V, Sentinel); } |
| 559 | |
| 560 | private: |
| 561 | const T &V; |
| 562 | }; |
| 563 | |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 564 | template <class B> RegNumBVIterImpl<B> RegNumBVIter(const B &BV) { |
| 565 | return RegNumBVIterImpl<B>(BV); |
| 566 | } |
| 567 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 568 | /// RegWeight is a wrapper for a uint32_t weight value, with a special value |
| 569 | /// that represents infinite weight, and an addWeight() method that ensures that |
| 570 | /// W+infinity=infinity. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 571 | class RegWeight { |
| 572 | public: |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 573 | using BaseType = uint32_t; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 574 | RegWeight() = default; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 575 | explicit RegWeight(BaseType Weight) : Weight(Weight) {} |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 576 | RegWeight(const RegWeight &) = default; |
| 577 | RegWeight &operator=(const RegWeight &) = default; |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 578 | constexpr static BaseType Inf = ~0; /// Force regalloc to give a register |
| 579 | constexpr static BaseType Zero = 0; /// Force regalloc NOT to give a register |
| 580 | constexpr static BaseType Max = Inf - 1; /// Max natural weight. |
| 581 | void addWeight(BaseType Delta) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 582 | if (Delta == Inf) |
| 583 | Weight = Inf; |
| 584 | else if (Weight != Inf) |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 585 | if (Utils::add_overflow(Weight, Delta, &Weight) || Weight == Inf) |
| 586 | Weight = Max; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 587 | } |
| 588 | void addWeight(const RegWeight &Other) { addWeight(Other.Weight); } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 589 | void setWeight(BaseType Val) { Weight = Val; } |
| 590 | BaseType getWeight() const { return Weight; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 591 | |
| 592 | private: |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 593 | BaseType Weight = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 594 | }; |
| 595 | Ostream &operator<<(Ostream &Str, const RegWeight &W); |
| 596 | bool operator<(const RegWeight &A, const RegWeight &B); |
| 597 | bool operator<=(const RegWeight &A, const RegWeight &B); |
| 598 | bool operator==(const RegWeight &A, const RegWeight &B); |
| 599 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 600 | /// LiveRange is a set of instruction number intervals representing a variable's |
| 601 | /// live range. Generally there is one interval per basic block where the |
| 602 | /// variable is live, but adjacent intervals get coalesced into a single |
| 603 | /// interval. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 604 | class LiveRange { |
| 605 | public: |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 606 | using RangeElementType = std::pair<InstNumberT, InstNumberT>; |
| 607 | /// RangeType is arena-allocated from the Cfg's allocator. |
| 608 | using RangeType = CfgVector<RangeElementType>; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 609 | LiveRange() = default; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 610 | /// Special constructor for building a kill set. The advantage is that we can |
| 611 | /// reserve the right amount of space in advance. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 612 | explicit LiveRange(const CfgVector<InstNumberT> &Kills) { |
Jim Stichnoth | 2a7fcbb | 2014-12-04 11:45:03 -0800 | [diff] [blame] | 613 | Range.reserve(Kills.size()); |
| 614 | for (InstNumberT I : Kills) |
| 615 | addSegment(I, I); |
| 616 | } |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 617 | LiveRange(const LiveRange &) = default; |
| 618 | LiveRange &operator=(const LiveRange &) = default; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 619 | |
| 620 | void reset() { |
| 621 | Range.clear(); |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 622 | untrim(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 623 | } |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 624 | void addSegment(InstNumberT Start, InstNumberT End, CfgNode *Node = nullptr); |
| 625 | void addSegment(RangeElementType Segment, CfgNode *Node = nullptr) { |
| 626 | addSegment(Segment.first, Segment.second, Node); |
| 627 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 628 | |
| 629 | bool endsBefore(const LiveRange &Other) const; |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 630 | bool overlaps(const LiveRange &Other, bool UseTrimmed = false) const; |
| 631 | bool overlapsInst(InstNumberT OtherBegin, bool UseTrimmed = false) const; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 632 | bool containsValue(InstNumberT Value, bool IsDest) const; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 633 | bool isEmpty() const { return Range.empty(); } |
| 634 | InstNumberT getStart() const { |
| 635 | return Range.empty() ? -1 : Range.begin()->first; |
| 636 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 637 | InstNumberT getEnd() const { |
| 638 | return Range.empty() ? -1 : Range.rbegin()->second; |
| 639 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 640 | |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 641 | void untrim() { TrimmedBegin = Range.begin(); } |
| 642 | void trim(InstNumberT Lower); |
| 643 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 644 | void dump(Ostream &Str) const; |
| 645 | |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 646 | SizeT getNumSegments() const { return Range.size(); } |
| 647 | |
| 648 | const RangeType &getSegments() const { return Range; } |
| 649 | CfgNode *getNodeForSegment(InstNumberT Begin) { |
| 650 | auto Iter = NodeMap.find(Begin); |
| 651 | assert(Iter != NodeMap.end()); |
| 652 | return Iter->second; |
| 653 | } |
| 654 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 655 | private: |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 656 | RangeType Range; |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 657 | CfgUnorderedMap<InstNumberT, CfgNode *> NodeMap; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 658 | /// TrimmedBegin is an optimization for the overlaps() computation. Since the |
| 659 | /// linear-scan algorithm always calls it as overlaps(Cur) and Cur advances |
| 660 | /// monotonically according to live range start, we can optimize overlaps() by |
| 661 | /// ignoring all segments that end before the start of Cur's range. The |
| 662 | /// linear-scan code enables this by calling trim() on the ranges of interest |
| 663 | /// as Cur advances. Note that linear-scan also has to initialize TrimmedBegin |
| 664 | /// at the beginning by calling untrim(). |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 665 | RangeType::const_iterator TrimmedBegin; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 666 | }; |
| 667 | |
| 668 | Ostream &operator<<(Ostream &Str, const LiveRange &L); |
| 669 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 670 | /// Variable represents an operand that is register-allocated or |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 671 | /// stack-allocated. If it is register-allocated, it will ultimately have a |
Jim Stichnoth | e343e06 | 2016-06-28 21:40:33 -0700 | [diff] [blame] | 672 | /// valid RegNum field. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 673 | class Variable : public Operand { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 674 | Variable() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 675 | Variable(const Variable &) = delete; |
| 676 | Variable &operator=(const Variable &) = delete; |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 677 | |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 678 | enum RegRequirement : uint8_t { |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 679 | RR_MayHaveRegister, |
| 680 | RR_MustHaveRegister, |
| 681 | RR_MustNotHaveRegister, |
| 682 | }; |
| 683 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 684 | public: |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 685 | static Variable *create(Cfg *Func, Type Ty, SizeT Index) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 686 | return new (Func->allocate<Variable>()) |
| 687 | Variable(Func, kVariable, Ty, Index); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | SizeT getIndex() const { return Number; } |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 691 | std::string getName() const { |
| 692 | if (Name.hasStdString()) |
| 693 | return Name.toString(); |
| 694 | return "__" + std::to_string(getIndex()); |
| 695 | } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 696 | virtual void setName(const Cfg *Func, const std::string &NewName) { |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 697 | if (NewName.empty()) |
| 698 | return; |
| 699 | Name = VariableString::createWithString(Func, NewName); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 700 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 701 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 702 | bool getIsArg() const { return IsArgument; } |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 703 | virtual void setIsArg(bool Val = true) { IsArgument = Val; } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 704 | bool getIsImplicitArg() const { return IsImplicitArgument; } |
| 705 | void setIsImplicitArg(bool Val = true) { IsImplicitArgument = Val; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 706 | |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 707 | void setIgnoreLiveness() { IgnoreLiveness = true; } |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 708 | bool getIgnoreLiveness() const { |
| 709 | return IgnoreLiveness || IsRematerializable; |
| 710 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 711 | |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 712 | /// Returns true if the variable either has a definite stack offset, or has |
| 713 | /// the UndeterminedStackOffset such that it is guaranteed to have a definite |
| 714 | /// stack offset at emission time. |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 715 | bool hasStackOffset() const { return StackOffset != InvalidStackOffset; } |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 716 | /// Returns true if the variable has a stack offset that is known at this |
| 717 | /// time. |
| 718 | bool hasKnownStackOffset() const { |
| 719 | return StackOffset != InvalidStackOffset && |
| 720 | StackOffset != UndeterminedStackOffset; |
| 721 | } |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 722 | int32_t getStackOffset() const { |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 723 | assert(hasKnownStackOffset()); |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 724 | return StackOffset; |
| 725 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 726 | void setStackOffset(int32_t Offset) { StackOffset = Offset; } |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 727 | /// Set a "placeholder" stack offset before its actual offset has been |
| 728 | /// determined. |
| 729 | void setHasStackOffset() { |
| 730 | if (!hasStackOffset()) |
| 731 | StackOffset = UndeterminedStackOffset; |
| 732 | } |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 733 | /// Returns the variable's stack offset in symbolic form, to improve |
| 734 | /// readability in DecorateAsm mode. |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 735 | std::string getSymbolicStackOffset() const { |
Jim Stichnoth | 98ba006 | 2016-03-07 09:26:22 -0800 | [diff] [blame] | 736 | if (!BuildDefs::dump()) |
| 737 | return ""; |
Jim Stichnoth | 00b9edb | 2016-06-25 10:14:39 -0700 | [diff] [blame] | 738 | return ".L$lv$" + getName(); |
Jim Stichnoth | 238b4c1 | 2015-10-01 07:46:38 -0700 | [diff] [blame] | 739 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 740 | |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 741 | bool hasReg() const { return getRegNum().hasValue(); } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 742 | RegNumT getRegNum() const { return RegNum; } |
| 743 | void setRegNum(RegNumT NewRegNum) { |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 744 | // Regnum shouldn't be set more than once. |
| 745 | assert(!hasReg() || RegNum == NewRegNum); |
| 746 | RegNum = NewRegNum; |
| 747 | } |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 748 | bool hasRegTmp() const { return getRegNumTmp().hasValue(); } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 749 | RegNumT getRegNumTmp() const { return RegNumTmp; } |
| 750 | void setRegNumTmp(RegNumT NewRegNum) { RegNumTmp = NewRegNum; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 751 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 752 | RegWeight getWeight(const Cfg *Func) const; |
| 753 | |
| 754 | void setMustHaveReg() { RegRequirement = RR_MustHaveRegister; } |
| 755 | bool mustHaveReg() const { return RegRequirement == RR_MustHaveRegister; } |
| 756 | void setMustNotHaveReg() { RegRequirement = RR_MustNotHaveRegister; } |
| 757 | bool mustNotHaveReg() const { |
| 758 | return RegRequirement == RR_MustNotHaveRegister; |
| 759 | } |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 760 | bool mayHaveReg() const { return RegRequirement == RR_MayHaveRegister; } |
Jim Stichnoth | 8aa3966 | 2016-02-10 11:20:30 -0800 | [diff] [blame] | 761 | void setRematerializable(RegNumT NewRegNum, int32_t NewOffset) { |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 762 | IsRematerializable = true; |
| 763 | setRegNum(NewRegNum); |
| 764 | setStackOffset(NewOffset); |
| 765 | setMustHaveReg(); |
| 766 | } |
| 767 | bool isRematerializable() const { return IsRematerializable; } |
Nicolas Capens | f8c7e51 | 2021-07-13 15:16:40 -0400 | [diff] [blame] | 768 | int32_t getRematerializableOffset(const ::Ice::TargetLowering *Target); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 769 | |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 770 | void setRegClass(uint8_t RC) { RegisterClass = static_cast<RegClass>(RC); } |
| 771 | RegClass getRegClass() const { return RegisterClass; } |
| 772 | |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 773 | LiveRange &getLiveRange() { return Live; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 774 | const LiveRange &getLiveRange() const { return Live; } |
| 775 | void setLiveRange(const LiveRange &Range) { Live = Range; } |
| 776 | void resetLiveRange() { Live.reset(); } |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 777 | void addLiveRange(InstNumberT Start, InstNumberT End, |
| 778 | CfgNode *Node = nullptr) { |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 779 | assert(!getIgnoreLiveness()); |
Manasij Mukherjee | 7cd926d | 2016-08-04 12:33:23 -0700 | [diff] [blame] | 780 | Live.addSegment(Start, End, Node); |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 781 | } |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 782 | void trimLiveRange(InstNumberT Start) { Live.trim(Start); } |
| 783 | void untrimLiveRange() { Live.untrim(); } |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 784 | bool rangeEndsBefore(const Variable *Other) const { |
| 785 | return Live.endsBefore(Other->Live); |
| 786 | } |
| 787 | bool rangeOverlaps(const Variable *Other) const { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 788 | constexpr bool UseTrimmed = true; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 789 | return Live.overlaps(Other->Live, UseTrimmed); |
| 790 | } |
| 791 | bool rangeOverlapsStart(const Variable *Other) const { |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 792 | constexpr bool UseTrimmed = true; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 793 | return Live.overlapsInst(Other->Live.getStart(), UseTrimmed); |
| 794 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 795 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 796 | /// Creates a temporary copy of the variable with a different type. Used |
| 797 | /// primarily for syntactic correctness of textual assembly emission. Note |
| 798 | /// that only basic information is copied, in particular not IsArgument, |
| 799 | /// IsImplicitArgument, IgnoreLiveness, RegNumTmp, Live, LoVar, HiVar, |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 800 | /// VarsReal. If NewRegNum.hasValue(), then that register assignment is made |
Jim Stichnoth | 5bff61c | 2015-10-28 09:26:00 -0700 | [diff] [blame] | 801 | /// instead of copying the existing assignment. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 802 | const Variable *asType(const Cfg *Func, Type Ty, RegNumT NewRegNum) const; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 803 | |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 804 | void emit(const Cfg *Func) const override; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 805 | using Operand::dump; |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 806 | void dump(const Cfg *Func, Ostream &Str) const override; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 807 | |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 808 | /// Return reg num of base register, if different from stack/frame register. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 809 | virtual RegNumT getBaseRegNum() const { return RegNumT(); } |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 810 | |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 811 | /// Access the LinkedTo field. |
| 812 | void setLinkedTo(Variable *Var) { LinkedTo = Var; } |
| 813 | Variable *getLinkedTo() const { return LinkedTo; } |
| 814 | /// Follow the LinkedTo chain up to the furthest ancestor. |
| 815 | Variable *getLinkedToRoot() const { |
| 816 | Variable *Root = LinkedTo; |
| 817 | if (Root == nullptr) |
| 818 | return nullptr; |
| 819 | while (Root->LinkedTo != nullptr) |
| 820 | Root = Root->LinkedTo; |
| 821 | return Root; |
Jim Stichnoth | e343e06 | 2016-06-28 21:40:33 -0700 | [diff] [blame] | 822 | } |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 823 | /// Follow the LinkedTo chain up to the furthest stack-allocated ancestor. |
| 824 | /// This is only certain to be accurate after register allocation and stack |
| 825 | /// slot assignment have completed. |
| 826 | Variable *getLinkedToStackRoot() const { |
| 827 | Variable *FurthestStackVar = nullptr; |
| 828 | for (Variable *Root = LinkedTo; Root != nullptr; Root = Root->LinkedTo) { |
| 829 | if (!Root->hasReg() && Root->hasStackOffset()) { |
| 830 | FurthestStackVar = Root; |
| 831 | } |
| 832 | } |
| 833 | return FurthestStackVar; |
| 834 | } |
Jim Stichnoth | e343e06 | 2016-06-28 21:40:33 -0700 | [diff] [blame] | 835 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 836 | static bool classof(const Operand *Operand) { |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 837 | OperandKind Kind = Operand->getKind(); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 838 | return Kind >= kVariable && Kind <= kVariable_Max; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 839 | } |
| 840 | |
Sagar Thakur | e160ed9 | 2016-05-30 07:54:47 -0700 | [diff] [blame] | 841 | SizeT hashValue() const override { return std::hash<SizeT>()(getIndex()); } |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 842 | |
Antonio Maiorano | debdfa2 | 2020-11-10 16:28:34 -0500 | [diff] [blame] | 843 | inline void *getExternalData() const { return externalData; } |
| 844 | inline void setExternalData(void *data) { externalData = data; } |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 845 | |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 846 | protected: |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 847 | Variable(const Cfg *Func, OperandKind K, Type Ty, SizeT Index) |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 848 | : Operand(K, Ty), Number(Index), |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 849 | Name(VariableString::createWithoutString(Func)), |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 850 | RegisterClass(static_cast<RegClass>(Ty)) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 851 | Vars = VarsReal; |
| 852 | Vars[0] = this; |
| 853 | NumVars = 1; |
| 854 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 855 | /// Number is unique across all variables, and is used as a (bit)vector index |
| 856 | /// for liveness analysis. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 857 | const SizeT Number; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 858 | VariableString Name; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 859 | bool IsArgument = false; |
| 860 | bool IsImplicitArgument = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 861 | /// IgnoreLiveness means that the variable should be ignored when constructing |
| 862 | /// and validating live ranges. This is usually reserved for the stack |
Jim Stichnoth | 6966055 | 2015-09-18 06:41:02 -0700 | [diff] [blame] | 863 | /// pointer and other physical registers specifically referenced by name. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 864 | bool IgnoreLiveness = false; |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 865 | // If IsRematerializable, RegNum keeps track of which register (stack or frame |
| 866 | // pointer), and StackOffset is the known offset from that register. |
| 867 | bool IsRematerializable = false; |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 868 | RegRequirement RegRequirement = RR_MayHaveRegister; |
Jim Stichnoth | c59288b | 2015-11-09 11:38:40 -0800 | [diff] [blame] | 869 | RegClass RegisterClass; |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 870 | /// RegNum is the allocated register, (as long as RegNum.hasValue() is true). |
| 871 | RegNumT RegNum; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 872 | /// RegNumTmp is the tentative assignment during register allocation. |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 873 | RegNumT RegNumTmp; |
Jim Stichnoth | b9a8472 | 2016-08-01 13:18:36 -0700 | [diff] [blame] | 874 | static constexpr int32_t InvalidStackOffset = |
| 875 | std::numeric_limits<int32_t>::min(); |
| 876 | static constexpr int32_t UndeterminedStackOffset = |
| 877 | 1 + std::numeric_limits<int32_t>::min(); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 878 | /// StackOffset is the canonical location on stack (only if |
Reed Kotler | 5fa0a5f | 2016-02-15 20:01:24 -0800 | [diff] [blame] | 879 | /// RegNum.hasNoValue() || IsArgument). |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 880 | int32_t StackOffset = InvalidStackOffset; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 881 | LiveRange Live; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 882 | /// VarsReal (and Operand::Vars) are set up such that Vars[0] == this. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 883 | Variable *VarsReal[1]; |
Jim Stichnoth | e343e06 | 2016-06-28 21:40:33 -0700 | [diff] [blame] | 884 | /// This Variable may be "linked" to another Variable, such that if neither |
| 885 | /// Variable gets a register, they are guaranteed to share a stack location. |
Jim Stichnoth | fe62f0a | 2016-07-10 05:13:18 -0700 | [diff] [blame] | 886 | Variable *LinkedTo = nullptr; |
Alexis Hetu | 932640b | 2018-06-20 15:35:53 -0400 | [diff] [blame] | 887 | /// External data can be set by an optimizer to compute and retain any |
| 888 | /// information related to the current variable. All the memory used to |
| 889 | /// store this information must be managed by the optimizer. |
Antonio Maiorano | debdfa2 | 2020-11-10 16:28:34 -0500 | [diff] [blame] | 890 | void *externalData = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 891 | }; |
| 892 | |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 893 | // Variable64On32 represents a 64-bit variable on a 32-bit architecture. In |
| 894 | // this situation the variable must be split into a low and a high word. |
| 895 | class Variable64On32 : public Variable { |
| 896 | Variable64On32() = delete; |
| 897 | Variable64On32(const Variable64On32 &) = delete; |
| 898 | Variable64On32 &operator=(const Variable64On32 &) = delete; |
| 899 | |
| 900 | public: |
| 901 | static Variable64On32 *create(Cfg *Func, Type Ty, SizeT Index) { |
John Porto | a83bfde | 2015-09-18 08:43:02 -0700 | [diff] [blame] | 902 | return new (Func->allocate<Variable64On32>()) |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 903 | Variable64On32(Func, kVariable64On32, Ty, Index); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 904 | } |
| 905 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 906 | void setName(const Cfg *Func, const std::string &NewName) override { |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 907 | Variable::setName(Func, NewName); |
| 908 | if (LoVar && HiVar) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 909 | LoVar->setName(Func, getName() + "__lo"); |
| 910 | HiVar->setName(Func, getName() + "__hi"); |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | void setIsArg(bool Val = true) override { |
| 915 | Variable::setIsArg(Val); |
| 916 | if (LoVar && HiVar) { |
| 917 | LoVar->setIsArg(Val); |
| 918 | HiVar->setIsArg(Val); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | Variable *getLo() const { |
| 923 | assert(LoVar != nullptr); |
| 924 | return LoVar; |
| 925 | } |
| 926 | Variable *getHi() const { |
| 927 | assert(HiVar != nullptr); |
| 928 | return HiVar; |
| 929 | } |
| 930 | |
| 931 | void initHiLo(Cfg *Func) { |
| 932 | assert(LoVar == nullptr); |
| 933 | assert(HiVar == nullptr); |
| 934 | LoVar = Func->makeVariable(IceType_i32); |
| 935 | HiVar = Func->makeVariable(IceType_i32); |
| 936 | LoVar->setIsArg(getIsArg()); |
| 937 | HiVar->setIsArg(getIsArg()); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 938 | if (BuildDefs::dump()) { |
Jim Stichnoth | a91c341 | 2016-04-05 15:31:43 -0700 | [diff] [blame] | 939 | LoVar->setName(Func, getName() + "__lo"); |
| 940 | HiVar->setName(Func, getName() + "__hi"); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 941 | } |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | static bool classof(const Operand *Operand) { |
| 945 | OperandKind Kind = Operand->getKind(); |
| 946 | return Kind == kVariable64On32; |
| 947 | } |
| 948 | |
| 949 | protected: |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 950 | Variable64On32(const Cfg *Func, OperandKind K, Type Ty, SizeT Index) |
| 951 | : Variable(Func, K, Ty, Index) { |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 952 | assert(typeWidthInBytes(Ty) == 8); |
| 953 | } |
| 954 | |
| 955 | Variable *LoVar = nullptr; |
| 956 | Variable *HiVar = nullptr; |
| 957 | }; |
| 958 | |
Jaydeep Patil | 958ddb7 | 2016-10-03 07:52:48 -0700 | [diff] [blame] | 959 | // VariableVecOn32 represents a 128-bit vector variable on a 32-bit |
| 960 | // architecture. In this case the variable must be split into 4 containers. |
| 961 | class VariableVecOn32 : public Variable { |
| 962 | VariableVecOn32() = delete; |
| 963 | VariableVecOn32(const VariableVecOn32 &) = delete; |
| 964 | VariableVecOn32 &operator=(const VariableVecOn32 &) = delete; |
| 965 | |
| 966 | public: |
| 967 | static VariableVecOn32 *create(Cfg *Func, Type Ty, SizeT Index) { |
| 968 | return new (Func->allocate<VariableVecOn32>()) |
| 969 | VariableVecOn32(Func, kVariableVecOn32, Ty, Index); |
| 970 | } |
| 971 | |
| 972 | void setName(const Cfg *Func, const std::string &NewName) override { |
| 973 | Variable::setName(Func, NewName); |
| 974 | if (!Containers.empty()) { |
Jaydeep Patil | 3a01f33 | 2016-10-17 06:33:50 -0700 | [diff] [blame] | 975 | for (SizeT i = 0; i < ContainersPerVector; ++i) { |
Jaydeep Patil | 958ddb7 | 2016-10-03 07:52:48 -0700 | [diff] [blame] | 976 | Containers[i]->setName(Func, getName() + "__cont" + std::to_string(i)); |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | |
| 981 | void setIsArg(bool Val = true) override { |
| 982 | Variable::setIsArg(Val); |
| 983 | for (Variable *Var : Containers) { |
| 984 | Var->setIsArg(getIsArg()); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | const VarList &getContainers() const { return Containers; } |
| 989 | |
| 990 | void initVecElement(Cfg *Func) { |
Jaydeep Patil | 3a01f33 | 2016-10-17 06:33:50 -0700 | [diff] [blame] | 991 | for (SizeT i = 0; i < ContainersPerVector; ++i) { |
Jaydeep Patil | 958ddb7 | 2016-10-03 07:52:48 -0700 | [diff] [blame] | 992 | Variable *Var = Func->makeVariable(IceType_i32); |
| 993 | Var->setIsArg(getIsArg()); |
| 994 | if (BuildDefs::dump()) { |
| 995 | Var->setName(Func, getName() + "__cont" + std::to_string(i)); |
| 996 | } |
| 997 | Containers.push_back(Var); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | static bool classof(const Operand *Operand) { |
| 1002 | OperandKind Kind = Operand->getKind(); |
| 1003 | return Kind == kVariableVecOn32; |
| 1004 | } |
| 1005 | |
| 1006 | // A 128-bit vector value is mapped onto 4 32-bit register values. |
Jaydeep Patil | 3a01f33 | 2016-10-17 06:33:50 -0700 | [diff] [blame] | 1007 | static constexpr SizeT ContainersPerVector = 4; |
Jaydeep Patil | 958ddb7 | 2016-10-03 07:52:48 -0700 | [diff] [blame] | 1008 | |
| 1009 | protected: |
| 1010 | VariableVecOn32(const Cfg *Func, OperandKind K, Type Ty, SizeT Index) |
| 1011 | : Variable(Func, K, Ty, Index) { |
| 1012 | assert(typeWidthInBytes(Ty) == |
Jaydeep Patil | 3a01f33 | 2016-10-17 06:33:50 -0700 | [diff] [blame] | 1013 | ContainersPerVector * typeWidthInBytes(IceType_i32)); |
Jaydeep Patil | 958ddb7 | 2016-10-03 07:52:48 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | VarList Containers; |
| 1017 | }; |
| 1018 | |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1019 | enum MetadataKind { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1020 | VMK_Uses, /// Track only uses, not defs |
| 1021 | VMK_SingleDefs, /// Track uses+defs, but only record single def |
| 1022 | VMK_All /// Track uses+defs, including full def list |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1023 | }; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 1024 | using InstDefList = CfgVector<const Inst *>; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 1025 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1026 | /// VariableTracking tracks the metadata for a single variable. It is |
| 1027 | /// only meant to be used internally by VariablesMetadata. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1028 | class VariableTracking { |
| 1029 | public: |
| 1030 | enum MultiDefState { |
| 1031 | // TODO(stichnot): Consider using just a simple counter. |
| 1032 | MDS_Unknown, |
| 1033 | MDS_SingleDef, |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 1034 | MDS_MultiDefSingleBlock, |
| 1035 | MDS_MultiDefMultiBlock |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1036 | }; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 1037 | enum MultiBlockState { |
| 1038 | MBS_Unknown, // Not yet initialized, so be conservative |
| 1039 | MBS_NoUses, // Known to have no uses |
| 1040 | MBS_SingleBlock, // All uses in are in a single block |
| 1041 | MBS_MultiBlock // Several uses across several blocks |
| 1042 | }; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1043 | VariableTracking() = default; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 1044 | VariableTracking(const VariableTracking &) = default; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 1045 | VariableTracking &operator=(const VariableTracking &) = default; |
| 1046 | VariableTracking(MultiBlockState MultiBlock) : MultiBlock(MultiBlock) {} |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1047 | MultiDefState getMultiDef() const { return MultiDef; } |
| 1048 | MultiBlockState getMultiBlock() const { return MultiBlock; } |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1049 | const Inst *getFirstDefinitionSingleBlock() const; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 1050 | const Inst *getSingleDefinition() const; |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1051 | const Inst *getFirstDefinition() const; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1052 | const InstDefList &getLatterDefinitions() const { return Definitions; } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 1053 | CfgNode *getNode() const { return SingleUseNode; } |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1054 | RegWeight getUseWeight() const { return UseWeight; } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 1055 | void markUse(MetadataKind TrackingKind, const Inst *Instr, CfgNode *Node, |
| 1056 | bool IsImplicit); |
| 1057 | void markDef(MetadataKind TrackingKind, const Inst *Instr, CfgNode *Node); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1058 | |
| 1059 | private: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 1060 | MultiDefState MultiDef = MDS_Unknown; |
| 1061 | MultiBlockState MultiBlock = MBS_Unknown; |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 1062 | CfgNode *SingleUseNode = nullptr; |
| 1063 | CfgNode *SingleDefNode = nullptr; |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1064 | /// All definitions of the variable are collected in Definitions[] (except for |
| 1065 | /// the earliest definition), in increasing order of instruction number. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1066 | InstDefList Definitions; /// Only used if Kind==VMK_All |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1067 | const Inst *FirstOrSingleDefinition = nullptr; |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1068 | RegWeight UseWeight; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1069 | }; |
| 1070 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 1071 | /// VariablesMetadata analyzes and summarizes the metadata for the complete set |
| 1072 | /// of Variables. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1073 | class VariablesMetadata { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 1074 | VariablesMetadata() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 1075 | VariablesMetadata(const VariablesMetadata &) = delete; |
| 1076 | VariablesMetadata &operator=(const VariablesMetadata &) = delete; |
| 1077 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1078 | public: |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 1079 | explicit VariablesMetadata(const Cfg *Func) : Func(Func) {} |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1080 | /// Initialize the state by traversing all instructions/variables in the CFG. |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1081 | void init(MetadataKind TrackingKind); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1082 | /// Add a single node. This is called by init(), and can be called |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1083 | /// incrementally from elsewhere, e.g. after edge-splitting. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 1084 | void addNode(CfgNode *Node); |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1085 | MetadataKind getKind() const { return Kind; } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1086 | /// Returns whether the given Variable is tracked in this object. It should |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 1087 | /// only return false if changes were made to the CFG after running init(), in |
| 1088 | /// which case the state is stale and the results shouldn't be trusted (but it |
| 1089 | /// may be OK e.g. for dumping). |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1090 | bool isTracked(const Variable *Var) const { |
| 1091 | return Var->getIndex() < Metadata.size(); |
| 1092 | } |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 1093 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1094 | /// Returns whether the given Variable has multiple definitions. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1095 | bool isMultiDef(const Variable *Var) const; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1096 | /// Returns the first definition instruction of the given Variable. This is |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 1097 | /// only valid for variables whose definitions are all within the same block, |
| 1098 | /// e.g. T after the lowered sequence "T=B; T+=C; A=T", for which |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1099 | /// getFirstDefinitionSingleBlock(T) would return the "T=B" instruction. For |
| 1100 | /// variables with definitions span multiple blocks, nullptr is returned. |
| 1101 | const Inst *getFirstDefinitionSingleBlock(const Variable *Var) const; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1102 | /// Returns the definition instruction of the given Variable, when the |
| 1103 | /// variable has exactly one definition. Otherwise, nullptr is returned. |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 1104 | const Inst *getSingleDefinition(const Variable *Var) const; |
Jim Stichnoth | 48e3ae5 | 2015-10-01 13:33:35 -0700 | [diff] [blame] | 1105 | /// getFirstDefinition() and getLatterDefinitions() are used together to |
| 1106 | /// return the complete set of instructions that define the given Variable, |
| 1107 | /// regardless of whether the definitions are within the same block (in |
| 1108 | /// contrast to getFirstDefinitionSingleBlock). |
| 1109 | const Inst *getFirstDefinition(const Variable *Var) const; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1110 | const InstDefList &getLatterDefinitions(const Variable *Var) const; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 1111 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1112 | /// Returns whether the given Variable is live across multiple blocks. Mainly, |
| 1113 | /// this is used to partition Variables into single-block versus multi-block |
| 1114 | /// sets for leveraging sparsity in liveness analysis, and for implementing |
| 1115 | /// simple stack slot coalescing. As a special case, function arguments are |
| 1116 | /// always considered multi-block because they are live coming into the entry |
| 1117 | /// block. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1118 | bool isMultiBlock(const Variable *Var) const; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 1119 | bool isSingleBlock(const Variable *Var) const; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 1120 | /// Returns the node that the given Variable is used in, assuming |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 1121 | /// isMultiBlock() returns false. Otherwise, nullptr is returned. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 1122 | CfgNode *getLocalUseNode(const Variable *Var) const; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1123 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 1124 | /// Returns the total use weight computed as the sum of uses multiplied by a |
| 1125 | /// loop nest depth factor for each use. |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 1126 | RegWeight getUseWeight(const Variable *Var) const; |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 1127 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1128 | private: |
| 1129 | const Cfg *Func; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 1130 | MetadataKind Kind; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 1131 | CfgVector<VariableTracking> Metadata; |
Nicolas Capens | 86e5d88 | 2016-09-08 12:47:42 -0400 | [diff] [blame] | 1132 | static const InstDefList *NoDefinitions; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 1133 | }; |
| 1134 | |
Eric Holk | 80ee5b3 | 2016-05-06 14:28:04 -0700 | [diff] [blame] | 1135 | /// BooleanVariable represents a variable that was the zero-extended result of a |
| 1136 | /// comparison. It maintains a pointer to its original i1 source so that the |
| 1137 | /// WASM frontend can avoid adding needless comparisons. |
| 1138 | class BooleanVariable : public Variable { |
| 1139 | BooleanVariable() = delete; |
| 1140 | BooleanVariable(const BooleanVariable &) = delete; |
| 1141 | BooleanVariable &operator=(const BooleanVariable &) = delete; |
| 1142 | |
| 1143 | BooleanVariable(const Cfg *Func, OperandKind K, Type Ty, SizeT Index) |
| 1144 | : Variable(Func, K, Ty, Index) {} |
| 1145 | |
| 1146 | public: |
| 1147 | static BooleanVariable *create(Cfg *Func, Type Ty, SizeT Index) { |
| 1148 | return new (Func->allocate<BooleanVariable>()) |
| 1149 | BooleanVariable(Func, kVariable, Ty, Index); |
| 1150 | } |
| 1151 | |
| 1152 | virtual Variable *asBoolean() { return BoolSource; } |
| 1153 | |
| 1154 | void setBoolSource(Variable *Src) { BoolSource = Src; } |
| 1155 | |
| 1156 | static bool classof(const Operand *Operand) { |
| 1157 | return Operand->getKind() == kVariableBoolean; |
| 1158 | } |
| 1159 | |
| 1160 | private: |
| 1161 | Variable *BoolSource = nullptr; |
| 1162 | }; |
| 1163 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1164 | } // end of namespace Ice |
| 1165 | |
| 1166 | #endif // SUBZERO_SRC_ICEOPERAND_H |