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 |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 11 | /// This file declares the Operand class and its target-independent subclasses. |
| 12 | /// The main classes are Variable, which represents an LLVM variable that is |
| 13 | /// either register- or stack-allocated, and the Constant hierarchy, which |
| 14 | /// represents integer, floating-point, and/or symbolic constants. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 15 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #ifndef SUBZERO_SRC_ICEOPERAND_H |
| 19 | #define SUBZERO_SRC_ICEOPERAND_H |
| 20 | |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 21 | #include "IceCfg.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 22 | #include "IceDefs.h" |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 23 | #include "IceGlobalContext.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 24 | #include "IceTypes.h" |
| 25 | |
| 26 | namespace Ice { |
| 27 | |
| 28 | class Operand { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 29 | Operand() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 30 | Operand(const Operand &) = delete; |
| 31 | Operand &operator=(const Operand &) = delete; |
| 32 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 33 | public: |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 34 | static constexpr size_t MaxTargetKinds = 10; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 35 | enum OperandKind { |
| 36 | kConst_Base, |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 37 | kConstInteger32, |
| 38 | kConstInteger64, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 39 | kConstFloat, |
| 40 | kConstDouble, |
| 41 | kConstRelocatable, |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 42 | kConstUndef, |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 43 | kConst_Target, // leave space for target-specific constant kinds |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 44 | kConst_Max = kConst_Target + MaxTargetKinds, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 45 | kVariable, |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 46 | kVariable_Target, // leave space for target-specific variable kinds |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 47 | kVariable_Max = kVariable_Target + MaxTargetKinds, |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 48 | // Target-specific operand classes use kTarget as the starting point for |
| 49 | // their Kind enum space. Note that the value-spaces are shared across |
| 50 | // targets. To avoid confusion over the definition of shared values, an |
| 51 | // object specific to one target should never be passed to a different |
| 52 | // target. |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 53 | kTarget, |
| 54 | kTarget_Max = std::numeric_limits<uint8_t>::max(), |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 55 | }; |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 56 | static_assert(kTarget <= kTarget_Max, "Must not be above max."); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 57 | OperandKind getKind() const { return Kind; } |
| 58 | Type getType() const { return Ty; } |
| 59 | |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 60 | /// Every Operand keeps an array of the Variables referenced in the operand. |
| 61 | /// This is so that the liveness operations can get quick access to the |
| 62 | /// variables of interest, without having to dig so far into the operand. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 63 | SizeT getNumVars() const { return NumVars; } |
| 64 | Variable *getVar(SizeT I) const { |
| 65 | assert(I < getNumVars()); |
| 66 | return Vars[I]; |
| 67 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 68 | virtual void emit(const Cfg *Func) const = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 69 | |
| 70 | /// \name Dumping functions. |
| 71 | /// @{ |
| 72 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 73 | /// The dump(Func,Str) implementation must be sure to handle the situation |
| 74 | /// where Func==nullptr. |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 75 | virtual void dump(const Cfg *Func, Ostream &Str) const = 0; |
| 76 | void dump(const Cfg *Func) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 77 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 78 | return; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 79 | assert(Func); |
| 80 | dump(Func, Func->getContext()->getStrDump()); |
| 81 | } |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 82 | void dump(Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 83 | if (BuildDefs::dump()) |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 84 | dump(nullptr, Str); |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 85 | } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 86 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 87 | |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 88 | ~Operand() = default; |
| 89 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 90 | protected: |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 91 | Operand(OperandKind Kind, Type Ty) : Ty(Ty), Kind(Kind) { |
| 92 | // It is undefined behavior to have a larger value in the enum |
| 93 | assert(Kind <= kTarget_Max); |
| 94 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 95 | |
| 96 | const Type Ty; |
| 97 | const OperandKind Kind; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 98 | /// Vars and NumVars are initialized by the derived class. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 99 | SizeT NumVars = 0; |
| 100 | Variable **Vars = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 103 | template <class StreamType> |
Karl Schimpf | 9750183 | 2014-09-16 13:35:32 -0700 | [diff] [blame] | 104 | inline StreamType &operator<<(StreamType &Str, const Operand &Op) { |
| 105 | Op.dump(Str); |
| 106 | return Str; |
| 107 | } |
| 108 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 109 | /// Constant is the abstract base class for constants. All constants are |
| 110 | /// allocated from a global arena and are pooled. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 111 | class Constant : public Operand { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 112 | Constant() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 113 | Constant(const Constant &) = delete; |
| 114 | Constant &operator=(const Constant &) = delete; |
| 115 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 116 | public: |
Jan Voung | 1d62cf0 | 2015-01-09 14:57:32 -0800 | [diff] [blame] | 117 | void emitPoolLabel(Ostream &Str) const { |
| 118 | Str << ".L$" << getType() << "$" << PoolEntryID; |
| 119 | } |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 120 | void emit(const Cfg *Func) const override { emit(Func->getTarget()); } |
| 121 | virtual void emit(TargetLowering *Target) const = 0; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 122 | |
| 123 | static bool classof(const Operand *Operand) { |
| 124 | OperandKind Kind = Operand->getKind(); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 125 | return Kind >= kConst_Base && Kind <= kConst_Max; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 128 | /// Judge if this given immediate should be randomized or pooled By default |
| 129 | /// should return false, only constant integers should truly go through this |
| 130 | /// method. |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 131 | virtual bool shouldBeRandomizedOrPooled(const GlobalContext *Ctx) { |
| 132 | (void)Ctx; |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | void setShouldBePooled(bool R) { shouldBePooled = R; } |
| 137 | |
| 138 | bool getShouldBePooled() const { return shouldBePooled; } |
| 139 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 140 | protected: |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 141 | Constant(OperandKind Kind, Type Ty, uint32_t PoolEntryID) |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 142 | : Operand(Kind, Ty), PoolEntryID(PoolEntryID), shouldBePooled(false) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 143 | Vars = nullptr; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 144 | NumVars = 0; |
| 145 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 146 | /// PoolEntryID is an integer that uniquely identifies the constant within its |
| 147 | /// constant pool. It is used for building the constant pool in the object |
| 148 | /// code and for referencing its entries. |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 149 | const uint32_t PoolEntryID; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 150 | /// Whether we should pool this constant. Usually Float/Double and pooled |
| 151 | /// Integers should be flagged true. |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 152 | bool shouldBePooled; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 155 | /// ConstantPrimitive<> wraps a primitive type. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 156 | template <typename T, Operand::OperandKind K> |
| 157 | class ConstantPrimitive : public Constant { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 158 | ConstantPrimitive() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 159 | ConstantPrimitive(const ConstantPrimitive &) = delete; |
| 160 | ConstantPrimitive &operator=(const ConstantPrimitive &) = delete; |
| 161 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 162 | public: |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 163 | using PrimType = T; |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 164 | |
| 165 | static ConstantPrimitive *create(GlobalContext *Ctx, Type Ty, PrimType Value, |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 166 | uint32_t PoolEntryID) { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 167 | assert(!Ctx->isIRGenerationDisabled() && |
| 168 | "Attempt to build primitive constant when IR generation disabled"); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 169 | return new (Ctx->allocate<ConstantPrimitive>()) |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 170 | ConstantPrimitive(Ty, Value, PoolEntryID); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 171 | } |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 172 | PrimType getValue() const { return Value; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 173 | using Constant::emit; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 174 | void emit(TargetLowering *Target) const final; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 175 | using Constant::dump; |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 176 | void dump(const Cfg *, Ostream &Str) const override { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 177 | if (BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 178 | Str << getValue(); |
| 179 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 180 | |
| 181 | static bool classof(const Operand *Operand) { |
| 182 | return Operand->getKind() == K; |
| 183 | } |
| 184 | |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 185 | virtual bool shouldBeRandomizedOrPooled(const GlobalContext *Ctx) override { |
| 186 | (void)Ctx; |
| 187 | return false; |
| 188 | } |
| 189 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 190 | private: |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 191 | ConstantPrimitive(Type Ty, PrimType Value, uint32_t PoolEntryID) |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 192 | : Constant(K, Ty, PoolEntryID), Value(Value) {} |
Jan Voung | 91a3e2c | 2015-01-09 13:01:42 -0800 | [diff] [blame] | 193 | const PrimType Value; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 194 | }; |
| 195 | |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 196 | using ConstantInteger32 = ConstantPrimitive<int32_t, Operand::kConstInteger32>; |
| 197 | using ConstantInteger64 = ConstantPrimitive<int64_t, Operand::kConstInteger64>; |
| 198 | using ConstantFloat = ConstantPrimitive<float, Operand::kConstFloat>; |
| 199 | using ConstantDouble = ConstantPrimitive<double, Operand::kConstDouble>; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 200 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 201 | template <> |
| 202 | inline void ConstantInteger32::dump(const Cfg *, Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 203 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 204 | return; |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 205 | if (getType() == IceType_i1) |
| 206 | Str << (getValue() ? "true" : "false"); |
| 207 | else |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 208 | Str << static_cast<int32_t>(getValue()); |
| 209 | } |
| 210 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 211 | /// Specialization of the template member function for ConstantInteger32 |
Qining Lu | 253dc8a | 2015-06-22 10:10:23 -0700 | [diff] [blame] | 212 | template <> |
| 213 | bool ConstantInteger32::shouldBeRandomizedOrPooled(const GlobalContext *Ctx); |
| 214 | |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 215 | template <> |
| 216 | inline void ConstantInteger64::dump(const Cfg *, Ostream &Str) const { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 217 | if (!BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 218 | return; |
Jan Voung | bc00463 | 2014-09-16 15:09:10 -0700 | [diff] [blame] | 219 | assert(getType() == IceType_i64); |
| 220 | Str << static_cast<int64_t>(getValue()); |
Jim Stichnoth | cabfa30 | 2014-09-03 15:19:12 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 223 | /// RelocatableTuple bundles the parameters that are used to construct an |
| 224 | /// ConstantRelocatable. It is done this way so that ConstantRelocatable can fit |
| 225 | /// into the global constant pool template mechanism. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 226 | class RelocatableTuple { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 227 | RelocatableTuple() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 228 | RelocatableTuple &operator=(const RelocatableTuple &) = delete; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 229 | |
| 230 | public: |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 231 | RelocatableTuple(const RelocOffsetT Offset, const IceString &Name, |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 232 | bool SuppressMangling) |
| 233 | : Offset(Offset), Name(Name), SuppressMangling(SuppressMangling) {} |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 234 | RelocatableTuple(const RelocatableTuple &) = default; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 235 | |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 236 | const RelocOffsetT Offset; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 237 | const IceString Name; |
| 238 | bool SuppressMangling; |
| 239 | }; |
| 240 | |
Jim Stichnoth | d2cb436 | 2014-11-20 11:24:42 -0800 | [diff] [blame] | 241 | bool operator==(const RelocatableTuple &A, const RelocatableTuple &B); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 242 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 243 | /// ConstantRelocatable represents a symbolic constant combined with a fixed |
| 244 | /// offset. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 245 | class ConstantRelocatable : public Constant { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 246 | ConstantRelocatable() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 247 | ConstantRelocatable(const ConstantRelocatable &) = delete; |
| 248 | ConstantRelocatable &operator=(const ConstantRelocatable &) = delete; |
| 249 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 250 | public: |
| 251 | static ConstantRelocatable *create(GlobalContext *Ctx, Type Ty, |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 252 | const RelocatableTuple &Tuple, |
| 253 | uint32_t PoolEntryID) { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 254 | assert(!Ctx->isIRGenerationDisabled() && |
| 255 | "Attempt to build relocatable constant when IR generation disabled"); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 256 | return new (Ctx->allocate<ConstantRelocatable>()) ConstantRelocatable( |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 257 | Ty, Tuple.Offset, Tuple.Name, Tuple.SuppressMangling, PoolEntryID); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 258 | } |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 259 | |
| 260 | RelocOffsetT getOffset() const { return Offset; } |
Jan Voung | c9ec579 | 2015-02-05 17:31:28 -0800 | [diff] [blame] | 261 | const IceString &getName() const { return Name; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 262 | void setSuppressMangling(bool Value) { SuppressMangling = Value; } |
| 263 | bool getSuppressMangling() const { return SuppressMangling; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 264 | using Constant::emit; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 265 | void emit(TargetLowering *Target) const final; |
| 266 | void emitWithoutPrefix(TargetLowering *Target) const; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 267 | using Constant::dump; |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 268 | void dump(const Cfg *Func, Ostream &Str) const override; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 269 | |
| 270 | static bool classof(const Operand *Operand) { |
| 271 | OperandKind Kind = Operand->getKind(); |
| 272 | return Kind == kConstRelocatable; |
| 273 | } |
| 274 | |
| 275 | private: |
Jan Voung | fe14fb8 | 2014-10-13 15:56:32 -0700 | [diff] [blame] | 276 | ConstantRelocatable(Type Ty, RelocOffsetT Offset, const IceString &Name, |
Jim Stichnoth | f61d5b2 | 2014-05-23 13:31:24 -0700 | [diff] [blame] | 277 | bool SuppressMangling, uint32_t PoolEntryID) |
| 278 | : Constant(kConstRelocatable, Ty, PoolEntryID), Offset(Offset), |
| 279 | Name(Name), SuppressMangling(SuppressMangling) {} |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 280 | const RelocOffsetT Offset; /// fixed offset to add |
| 281 | const IceString Name; /// optional for debug/dump |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 282 | bool SuppressMangling; |
| 283 | }; |
| 284 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 285 | /// ConstantUndef represents an unspecified bit pattern. Although it is legal to |
| 286 | /// lower ConstantUndef to any value, backends should try to make code |
| 287 | /// generation deterministic by lowering ConstantUndefs to 0. |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 288 | class ConstantUndef : public Constant { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 289 | ConstantUndef() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 290 | ConstantUndef(const ConstantUndef &) = delete; |
| 291 | ConstantUndef &operator=(const ConstantUndef &) = delete; |
| 292 | |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 293 | public: |
| 294 | static ConstantUndef *create(GlobalContext *Ctx, Type Ty, |
| 295 | uint32_t PoolEntryID) { |
Karl Schimpf | 6fcbddd | 2014-11-06 09:49:24 -0800 | [diff] [blame] | 296 | assert(!Ctx->isIRGenerationDisabled() && |
| 297 | "Attempt to build undefined constant when IR generation disabled"); |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 298 | return new (Ctx->allocate<ConstantUndef>()) ConstantUndef(Ty, PoolEntryID); |
| 299 | } |
| 300 | |
| 301 | using Constant::emit; |
Jan Voung | 76bb0be | 2015-05-14 09:26:19 -0700 | [diff] [blame] | 302 | void emit(TargetLowering *Target) const final; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 303 | using Constant::dump; |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 304 | void dump(const Cfg *, Ostream &Str) const override { |
Jim Stichnoth | 20b71f5 | 2015-06-24 15:52:24 -0700 | [diff] [blame] | 305 | if (BuildDefs::dump()) |
Karl Schimpf | b6c96af | 2014-11-17 10:58:39 -0800 | [diff] [blame] | 306 | Str << "undef"; |
| 307 | } |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 308 | |
| 309 | static bool classof(const Operand *Operand) { |
| 310 | return Operand->getKind() == kConstUndef; |
| 311 | } |
| 312 | |
| 313 | private: |
| 314 | ConstantUndef(Type Ty, uint32_t PoolEntryID) |
| 315 | : Constant(kConstUndef, Ty, PoolEntryID) {} |
Matt Wala | d8f4a7d | 2014-06-18 09:55:03 -0700 | [diff] [blame] | 316 | }; |
| 317 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 318 | /// RegWeight is a wrapper for a uint32_t weight value, with a special value |
| 319 | /// that represents infinite weight, and an addWeight() method that ensures that |
| 320 | /// W+infinity=infinity. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 321 | class RegWeight { |
| 322 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 323 | RegWeight() = default; |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 324 | explicit RegWeight(uint32_t Weight) : Weight(Weight) {} |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 325 | RegWeight(const RegWeight &) = default; |
| 326 | RegWeight &operator=(const RegWeight &) = default; |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 327 | const static uint32_t Inf = ~0; /// Force regalloc to give a register |
| 328 | const static uint32_t Zero = 0; /// Force regalloc NOT to give a register |
| 329 | const static uint32_t Max = Inf - 1; /// Max natural weight. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 330 | void addWeight(uint32_t Delta) { |
| 331 | if (Delta == Inf) |
| 332 | Weight = Inf; |
| 333 | else if (Weight != Inf) |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 334 | if (Utils::add_overflow(Weight, Delta, &Weight) || Weight == Inf) |
| 335 | Weight = Max; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 336 | } |
| 337 | void addWeight(const RegWeight &Other) { addWeight(Other.Weight); } |
| 338 | void setWeight(uint32_t Val) { Weight = Val; } |
| 339 | uint32_t getWeight() const { return Weight; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 340 | |
| 341 | private: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 342 | uint32_t Weight = 0; |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 343 | }; |
| 344 | Ostream &operator<<(Ostream &Str, const RegWeight &W); |
| 345 | bool operator<(const RegWeight &A, const RegWeight &B); |
| 346 | bool operator<=(const RegWeight &A, const RegWeight &B); |
| 347 | bool operator==(const RegWeight &A, const RegWeight &B); |
| 348 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 349 | /// LiveRange is a set of instruction number intervals representing a variable's |
| 350 | /// live range. Generally there is one interval per basic block where the |
| 351 | /// variable is live, but adjacent intervals get coalesced into a single |
| 352 | /// interval. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 353 | class LiveRange { |
| 354 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 355 | LiveRange() = default; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 356 | /// Special constructor for building a kill set. The advantage is that we can |
| 357 | /// reserve the right amount of space in advance. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 358 | explicit LiveRange(const CfgVector<InstNumberT> &Kills) { |
Jim Stichnoth | 2a7fcbb | 2014-12-04 11:45:03 -0800 | [diff] [blame] | 359 | Range.reserve(Kills.size()); |
| 360 | for (InstNumberT I : Kills) |
| 361 | addSegment(I, I); |
| 362 | } |
Jim Stichnoth | 87ff3a1 | 2014-11-14 10:27:29 -0800 | [diff] [blame] | 363 | LiveRange(const LiveRange &) = default; |
| 364 | LiveRange &operator=(const LiveRange &) = default; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 365 | |
| 366 | void reset() { |
| 367 | Range.clear(); |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 368 | untrim(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 369 | } |
| 370 | void addSegment(InstNumberT Start, InstNumberT End); |
| 371 | |
| 372 | bool endsBefore(const LiveRange &Other) const; |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 373 | bool overlaps(const LiveRange &Other, bool UseTrimmed = false) const; |
| 374 | bool overlapsInst(InstNumberT OtherBegin, bool UseTrimmed = false) const; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 375 | bool containsValue(InstNumberT Value, bool IsDest) const; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 376 | bool isEmpty() const { return Range.empty(); } |
| 377 | InstNumberT getStart() const { |
| 378 | return Range.empty() ? -1 : Range.begin()->first; |
| 379 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 380 | InstNumberT getEnd() const { |
| 381 | return Range.empty() ? -1 : Range.rbegin()->second; |
| 382 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 383 | |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 384 | void untrim() { TrimmedBegin = Range.begin(); } |
| 385 | void trim(InstNumberT Lower); |
| 386 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 387 | void dump(Ostream &Str) const; |
| 388 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 389 | private: |
Andrew Scull | 8072bae | 2015-09-14 16:01:26 -0700 | [diff] [blame] | 390 | using RangeElementType = std::pair<InstNumberT, InstNumberT>; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 391 | /// RangeType is arena-allocated from the Cfg's allocator. |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 392 | using RangeType = CfgVector<RangeElementType>; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 393 | RangeType Range; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 394 | /// TrimmedBegin is an optimization for the overlaps() computation. Since the |
| 395 | /// linear-scan algorithm always calls it as overlaps(Cur) and Cur advances |
| 396 | /// monotonically according to live range start, we can optimize overlaps() by |
| 397 | /// ignoring all segments that end before the start of Cur's range. The |
| 398 | /// linear-scan code enables this by calling trim() on the ranges of interest |
| 399 | /// as Cur advances. Note that linear-scan also has to initialize TrimmedBegin |
| 400 | /// at the beginning by calling untrim(). |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 401 | RangeType::const_iterator TrimmedBegin; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
| 404 | Ostream &operator<<(Ostream &Str, const LiveRange &L); |
| 405 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 406 | /// Variable represents an operand that is register-allocated or |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 407 | /// stack-allocated. If it is register-allocated, it will ultimately have a |
| 408 | /// non-negative RegNum field. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 409 | class Variable : public Operand { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 410 | Variable() = delete; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 411 | Variable(const Variable &) = delete; |
| 412 | Variable &operator=(const Variable &) = delete; |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 413 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 414 | enum RegRequirement { |
| 415 | RR_MayHaveRegister, |
| 416 | RR_MustHaveRegister, |
| 417 | RR_MustNotHaveRegister, |
| 418 | }; |
| 419 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 420 | public: |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 421 | static Variable *create(Cfg *Func, Type Ty, SizeT Index) { |
| 422 | return new (Func->allocate<Variable>()) Variable(kVariable, Ty, Index); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | SizeT getIndex() const { return Number; } |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 426 | IceString getName(const Cfg *Func) const; |
| 427 | void setName(Cfg *Func, const IceString &NewName) { |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 428 | // Make sure that the name can only be set once. |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 429 | assert(NameIndex == Cfg::IdentifierIndexInvalid); |
| 430 | if (!NewName.empty()) |
| 431 | NameIndex = Func->addIdentifierName(NewName); |
Karl Schimpf | c132b76 | 2014-09-11 09:43:47 -0700 | [diff] [blame] | 432 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 433 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 434 | bool getIsArg() const { return IsArgument; } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 435 | void setIsArg(bool Val = true) { IsArgument = Val; } |
| 436 | bool getIsImplicitArg() const { return IsImplicitArgument; } |
| 437 | void setIsImplicitArg(bool Val = true) { IsImplicitArgument = Val; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 438 | |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 439 | void setIgnoreLiveness() { IgnoreLiveness = true; } |
| 440 | bool getIgnoreLiveness() const { return IgnoreLiveness; } |
| 441 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 442 | int32_t getStackOffset() const { return StackOffset; } |
| 443 | void setStackOffset(int32_t Offset) { StackOffset = Offset; } |
| 444 | |
| 445 | static const int32_t NoRegister = -1; |
| 446 | bool hasReg() const { return getRegNum() != NoRegister; } |
| 447 | int32_t getRegNum() const { return RegNum; } |
| 448 | void setRegNum(int32_t NewRegNum) { |
| 449 | // Regnum shouldn't be set more than once. |
| 450 | assert(!hasReg() || RegNum == NewRegNum); |
| 451 | RegNum = NewRegNum; |
| 452 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 453 | bool hasRegTmp() const { return getRegNumTmp() != NoRegister; } |
| 454 | int32_t getRegNumTmp() const { return RegNumTmp; } |
| 455 | void setRegNumTmp(int32_t NewRegNum) { RegNumTmp = NewRegNum; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 456 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 457 | RegWeight getWeight(const Cfg *Func) const; |
| 458 | |
| 459 | void setMustHaveReg() { RegRequirement = RR_MustHaveRegister; } |
| 460 | bool mustHaveReg() const { return RegRequirement == RR_MustHaveRegister; } |
| 461 | void setMustNotHaveReg() { RegRequirement = RR_MustNotHaveRegister; } |
| 462 | bool mustNotHaveReg() const { |
| 463 | return RegRequirement == RR_MustNotHaveRegister; |
| 464 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 465 | |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 466 | LiveRange &getLiveRange() { return Live; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 467 | const LiveRange &getLiveRange() const { return Live; } |
| 468 | void setLiveRange(const LiveRange &Range) { Live = Range; } |
| 469 | void resetLiveRange() { Live.reset(); } |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 470 | void addLiveRange(InstNumberT Start, InstNumberT End) { |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 471 | assert(!getIgnoreLiveness()); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 472 | Live.addSegment(Start, End); |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 473 | } |
Jim Stichnoth | 037fa1d | 2014-10-07 11:09:33 -0700 | [diff] [blame] | 474 | void trimLiveRange(InstNumberT Start) { Live.trim(Start); } |
| 475 | void untrimLiveRange() { Live.untrim(); } |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 476 | bool rangeEndsBefore(const Variable *Other) const { |
| 477 | return Live.endsBefore(Other->Live); |
| 478 | } |
| 479 | bool rangeOverlaps(const Variable *Other) const { |
| 480 | const bool UseTrimmed = true; |
| 481 | return Live.overlaps(Other->Live, UseTrimmed); |
| 482 | } |
| 483 | bool rangeOverlapsStart(const Variable *Other) const { |
| 484 | const bool UseTrimmed = true; |
| 485 | return Live.overlapsInst(Other->Live.getStart(), UseTrimmed); |
| 486 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 487 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 488 | Variable *getLo() const { return LoVar; } |
| 489 | Variable *getHi() const { return HiVar; } |
| 490 | void setLoHi(Variable *Lo, Variable *Hi) { |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 491 | assert(LoVar == nullptr); |
| 492 | assert(HiVar == nullptr); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 493 | LoVar = Lo; |
| 494 | HiVar = Hi; |
| 495 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 496 | /// Creates a temporary copy of the variable with a different type. Used |
| 497 | /// primarily for syntactic correctness of textual assembly emission. Note |
| 498 | /// that only basic information is copied, in particular not IsArgument, |
| 499 | /// IsImplicitArgument, IgnoreLiveness, RegNumTmp, Live, LoVar, HiVar, |
| 500 | /// VarsReal. |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 501 | Variable *asType(Type Ty); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 502 | |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 503 | void emit(const Cfg *Func) const override; |
Jim Stichnoth | 2e8bfbb | 2014-09-16 10:16:00 -0700 | [diff] [blame] | 504 | using Operand::dump; |
Jim Stichnoth | b56c8f4 | 2014-09-26 09:28:46 -0700 | [diff] [blame] | 505 | void dump(const Cfg *Func, Ostream &Str) const override; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 506 | |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 507 | /// Return reg num of base register, if different from stack/frame register. |
| 508 | virtual int32_t getBaseRegNum() const { return NoRegister; } |
| 509 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 510 | static bool classof(const Operand *Operand) { |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 511 | OperandKind Kind = Operand->getKind(); |
Andrew Scull | 6ef7949 | 2015-09-09 15:50:42 -0700 | [diff] [blame] | 512 | return Kind >= kVariable && Kind <= kVariable_Max; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 515 | protected: |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 516 | Variable(OperandKind K, Type Ty, SizeT Index) |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 517 | : Operand(K, Ty), Number(Index) { |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 518 | Vars = VarsReal; |
| 519 | Vars[0] = this; |
| 520 | NumVars = 1; |
| 521 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 522 | /// Number is unique across all variables, and is used as a (bit)vector index |
| 523 | /// for liveness analysis. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 524 | const SizeT Number; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 525 | Cfg::IdentifierIndexType NameIndex = Cfg::IdentifierIndexInvalid; |
| 526 | bool IsArgument = false; |
| 527 | bool IsImplicitArgument = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 528 | /// IgnoreLiveness means that the variable should be ignored when constructing |
| 529 | /// and validating live ranges. This is usually reserved for the stack |
| 530 | /// pointer. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 531 | bool IgnoreLiveness = false; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 532 | /// StackOffset is the canonical location on stack (only if RegNum==NoRegister |
| 533 | /// || IsArgument). |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 534 | int32_t StackOffset = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 535 | /// RegNum is the allocated register, or NoRegister if it isn't |
| 536 | /// register-allocated. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 537 | int32_t RegNum = NoRegister; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 538 | /// RegNumTmp is the tentative assignment during register allocation. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 539 | int32_t RegNumTmp = NoRegister; |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 540 | RegRequirement RegRequirement = RR_MayHaveRegister; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 541 | LiveRange Live; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 542 | // LoVar and HiVar are needed for lowering from 64 to 32 bits. When lowering |
| 543 | // from I64 to I32 on a 32-bit architecture, we split the variable into two |
| 544 | // machine-size pieces. LoVar is the low-order machine-size portion, and |
| 545 | // HiVar is the remaining high-order portion. |
| 546 | // TODO: It's wasteful to penalize all variables on all targets this way; use |
| 547 | // a sparser representation. It's also wasteful for a 64-bit target. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 548 | Variable *LoVar = nullptr; |
| 549 | Variable *HiVar = nullptr; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 550 | /// VarsReal (and Operand::Vars) are set up such that Vars[0] == this. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 551 | Variable *VarsReal[1]; |
| 552 | }; |
| 553 | |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 554 | enum MetadataKind { |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 555 | VMK_Uses, /// Track only uses, not defs |
| 556 | VMK_SingleDefs, /// Track uses+defs, but only record single def |
| 557 | VMK_All /// Track uses+defs, including full def list |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 558 | }; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 559 | using InstDefList = CfgVector<const Inst *>; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 560 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 561 | /// VariableTracking tracks the metadata for a single variable. It is |
| 562 | /// only meant to be used internally by VariablesMetadata. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 563 | class VariableTracking { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 564 | VariableTracking &operator=(const VariableTracking &) = delete; |
| 565 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 566 | public: |
| 567 | enum MultiDefState { |
| 568 | // TODO(stichnot): Consider using just a simple counter. |
| 569 | MDS_Unknown, |
| 570 | MDS_SingleDef, |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 571 | MDS_MultiDefSingleBlock, |
| 572 | MDS_MultiDefMultiBlock |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 573 | }; |
Jim Stichnoth | dd842db | 2015-01-27 12:53:53 -0800 | [diff] [blame] | 574 | enum MultiBlockState { MBS_Unknown, MBS_SingleBlock, MBS_MultiBlock }; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 575 | VariableTracking() = default; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 576 | VariableTracking(const VariableTracking &) = default; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 577 | MultiDefState getMultiDef() const { return MultiDef; } |
| 578 | MultiBlockState getMultiBlock() const { return MultiBlock; } |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 579 | const Inst *getFirstDefinition() const; |
| 580 | const Inst *getSingleDefinition() const; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 581 | const InstDefList &getLatterDefinitions() const { return Definitions; } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 582 | CfgNode *getNode() const { return SingleUseNode; } |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 583 | RegWeight getUseWeight() const { return UseWeight; } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 584 | void markUse(MetadataKind TrackingKind, const Inst *Instr, CfgNode *Node, |
| 585 | bool IsImplicit); |
| 586 | void markDef(MetadataKind TrackingKind, const Inst *Instr, CfgNode *Node); |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 587 | |
| 588 | private: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 589 | MultiDefState MultiDef = MDS_Unknown; |
| 590 | MultiBlockState MultiBlock = MBS_Unknown; |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 591 | CfgNode *SingleUseNode = nullptr; |
| 592 | CfgNode *SingleDefNode = nullptr; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 593 | /// All definitions of the variable are collected here, in increasing |
| 594 | /// order of instruction number. |
| 595 | InstDefList Definitions; /// Only used if Kind==VMK_All |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 596 | const Inst *FirstOrSingleDefinition = |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 597 | nullptr; /// Is a copy of Definitions[0] if Kind==VMK_All |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 598 | RegWeight UseWeight; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 599 | }; |
| 600 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 601 | /// VariablesMetadata analyzes and summarizes the metadata for the complete set |
| 602 | /// of Variables. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 603 | class VariablesMetadata { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 604 | VariablesMetadata() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 605 | VariablesMetadata(const VariablesMetadata &) = delete; |
| 606 | VariablesMetadata &operator=(const VariablesMetadata &) = delete; |
| 607 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 608 | public: |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 609 | explicit VariablesMetadata(const Cfg *Func) : Func(Func) {} |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 610 | /// Initialize the state by traversing all instructions/variables in the CFG. |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 611 | void init(MetadataKind TrackingKind); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 612 | /// 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] | 613 | /// incrementally from elsewhere, e.g. after edge-splitting. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 614 | void addNode(CfgNode *Node); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 615 | /// Returns whether the given Variable is tracked in this object. It should |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 616 | /// only return false if changes were made to the CFG after running init(), in |
| 617 | /// which case the state is stale and the results shouldn't be trusted (but it |
| 618 | /// may be OK e.g. for dumping). |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 619 | bool isTracked(const Variable *Var) const { |
| 620 | return Var->getIndex() < Metadata.size(); |
| 621 | } |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 622 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 623 | /// Returns whether the given Variable has multiple definitions. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 624 | bool isMultiDef(const Variable *Var) const; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 625 | /// Returns the first definition instruction of the given Variable. This is |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 626 | /// only valid for variables whose definitions are all within the same block, |
| 627 | /// e.g. T after the lowered sequence "T=B; T+=C; A=T", for which |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 628 | /// getFirstDefinition(T) would return the "T=B" instruction. For variables |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 629 | /// with definitions span multiple blocks, nullptr is returned. |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 630 | const Inst *getFirstDefinition(const Variable *Var) const; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 631 | /// Returns the definition instruction of the given Variable, when the |
| 632 | /// variable has exactly one definition. Otherwise, nullptr is returned. |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 633 | const Inst *getSingleDefinition(const Variable *Var) const; |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 634 | /// Returns the list of all definition instructions of the given Variable. |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 635 | const InstDefList &getLatterDefinitions(const Variable *Var) const; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 636 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 637 | /// Returns whether the given Variable is live across multiple blocks. Mainly, |
| 638 | /// this is used to partition Variables into single-block versus multi-block |
| 639 | /// sets for leveraging sparsity in liveness analysis, and for implementing |
| 640 | /// simple stack slot coalescing. As a special case, function arguments are |
| 641 | /// always considered multi-block because they are live coming into the entry |
| 642 | /// block. |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 643 | bool isMultiBlock(const Variable *Var) const; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 644 | /// Returns the node that the given Variable is used in, assuming |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 645 | /// isMultiBlock() returns false. Otherwise, nullptr is returned. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 646 | CfgNode *getLocalUseNode(const Variable *Var) const; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 647 | |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 648 | /// Returns the total use weight computed as the sum of uses multiplied by a |
| 649 | /// loop nest depth factor for each use. |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 650 | RegWeight getUseWeight(const Variable *Var) const; |
Andrew Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 651 | |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 652 | private: |
| 653 | const Cfg *Func; |
Jim Stichnoth | 877b04e | 2014-10-15 15:13:06 -0700 | [diff] [blame] | 654 | MetadataKind Kind; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 655 | CfgVector<VariableTracking> Metadata; |
Jim Stichnoth | ad40353 | 2014-09-25 12:44:17 -0700 | [diff] [blame] | 656 | const static InstDefList NoDefinitions; |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 657 | }; |
| 658 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 659 | } // end of namespace Ice |
| 660 | |
| 661 | #endif // SUBZERO_SRC_ICEOPERAND_H |