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