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