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