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