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