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