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