OLD | NEW |
(Empty) | |
| 1 //===- subzero/src/IceOperand.h - High-level operands -----------*- C++ -*-===// |
| 2 // |
| 3 // The Subzero Code Generator |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This file declares the Operand class and its target-independent |
| 11 // subclasses. The main classes are Variable, which represents an |
| 12 // LLVM variable that is either register- or stack-allocated, and the |
| 13 // Constant hierarchy, which represents integer, floating-point, |
| 14 // and/or symbolic constants. |
| 15 // |
| 16 //===----------------------------------------------------------------------===// |
| 17 |
| 18 #ifndef SUBZERO_SRC_ICEOPERAND_H |
| 19 #define SUBZERO_SRC_ICEOPERAND_H |
| 20 |
| 21 #include "IceDefs.h" |
| 22 #include "IceTypes.h" |
| 23 |
| 24 namespace Ice { |
| 25 |
| 26 class Operand { |
| 27 public: |
| 28 enum OperandKind { |
| 29 kConst_Base, |
| 30 kConstInteger, |
| 31 kConstFloat, |
| 32 kConstDouble, |
| 33 kConstRelocatable, |
| 34 kConst_Num, |
| 35 kVariable, |
| 36 // Target-specific operand classes use kTarget as the starting |
| 37 // point for their Kind enum space. |
| 38 kTarget |
| 39 }; |
| 40 OperandKind getKind() const { return Kind; } |
| 41 IceType getType() const { return Type; } |
| 42 |
| 43 // Every Operand keeps an array of the Variables referenced in |
| 44 // the operand. This is so that the liveness operations can get |
| 45 // quick access to the variables of interest, without having to dig |
| 46 // so far into the operand. |
| 47 IceSize_t getNumVars() const { return NumVars; } |
| 48 Variable *getVar(IceSize_t I) const { |
| 49 assert(I < getNumVars()); |
| 50 return Vars[I]; |
| 51 } |
| 52 virtual void dump(const IceCfg *Cfg) const = 0; |
| 53 |
| 54 // Query whether this object was allocated in isolation, or added to |
| 55 // some higher-level pool. This determines whether a containing |
| 56 // object's destructor should delete this object. Generally, |
| 57 // constants are pooled globally, variables are pooled per-CFG, and |
| 58 // target-specific operands are not pooled. |
| 59 virtual bool isPooled() const { return false; } |
| 60 |
| 61 virtual ~Operand() {} |
| 62 |
| 63 protected: |
| 64 Operand(OperandKind Kind, IceType Type) |
| 65 : Type(Type), Kind(Kind), NumVars(0), Vars(NULL) {} |
| 66 |
| 67 const IceType Type; |
| 68 const OperandKind Kind; |
| 69 // Vars and NumVars are initialized by the derived class. |
| 70 IceSize_t NumVars; |
| 71 Variable **Vars; |
| 72 |
| 73 private: |
| 74 Operand(const Operand &) LLVM_DELETED_FUNCTION; |
| 75 Operand &operator=(const Operand &) LLVM_DELETED_FUNCTION; |
| 76 }; |
| 77 |
| 78 // Constant is the abstract base class for constants. All |
| 79 // constants are allocated from a global arena and are pooled. |
| 80 class Constant : public Operand { |
| 81 public: |
| 82 virtual void dump(const IceCfg *Cfg) const = 0; |
| 83 |
| 84 static bool classof(const Operand *Operand) { |
| 85 OperandKind Kind = Operand->getKind(); |
| 86 return Kind >= kConst_Base && Kind <= kConst_Num; |
| 87 } |
| 88 |
| 89 protected: |
| 90 Constant(OperandKind Kind, IceType Type) : Operand(Kind, Type) { |
| 91 Vars = NULL; |
| 92 NumVars = 0; |
| 93 } |
| 94 virtual ~Constant() {} |
| 95 |
| 96 private: |
| 97 Constant(const Constant &) LLVM_DELETED_FUNCTION; |
| 98 Constant &operator=(const Constant &) LLVM_DELETED_FUNCTION; |
| 99 }; |
| 100 |
| 101 // ConstantPrimitive<> wraps a primitive type. |
| 102 template <typename T, Operand::OperandKind K> |
| 103 class ConstantPrimitive : public Constant { |
| 104 public: |
| 105 static ConstantPrimitive *create(GlobalContext *Ctx, IceType Type, T Value) { |
| 106 return new (Ctx->allocate<ConstantPrimitive>()) |
| 107 ConstantPrimitive(Type, Value); |
| 108 } |
| 109 T getValue() const { return Value; } |
| 110 virtual void dump(const IceCfg *Cfg) const { |
| 111 IceOstream &Str = Cfg->getContext()->getStrDump(); |
| 112 Str << getValue(); |
| 113 } |
| 114 |
| 115 static bool classof(const Operand *Operand) { |
| 116 return Operand->getKind() == K; |
| 117 } |
| 118 |
| 119 private: |
| 120 ConstantPrimitive(IceType Type, T Value) : Constant(K, Type), Value(Value) {} |
| 121 ConstantPrimitive(const ConstantPrimitive &) LLVM_DELETED_FUNCTION; |
| 122 ConstantPrimitive &operator=(const ConstantPrimitive &) LLVM_DELETED_FUNCTION; |
| 123 virtual ~ConstantPrimitive() {} |
| 124 const T Value; |
| 125 }; |
| 126 |
| 127 typedef ConstantPrimitive<uint64_t, Operand::kConstInteger> ConstantInteger; |
| 128 typedef ConstantPrimitive<float, Operand::kConstFloat> ConstantFloat; |
| 129 typedef ConstantPrimitive<double, Operand::kConstDouble> ConstantDouble; |
| 130 |
| 131 // RelocatableTuple bundles the parameters that are used to |
| 132 // construct an ConstantRelocatable. It is done this way so that |
| 133 // ConstantRelocatable can fit into the global constant pool |
| 134 // template mechanism. |
| 135 class RelocatableTuple { |
| 136 RelocatableTuple &operator=(const RelocatableTuple &) LLVM_DELETED_FUNCTION; |
| 137 |
| 138 public: |
| 139 RelocatableTuple(const int64_t Offset, const IceString &Name, |
| 140 bool SuppressMangling) |
| 141 : Offset(Offset), Name(Name), SuppressMangling(SuppressMangling) {} |
| 142 RelocatableTuple(const RelocatableTuple &Other) |
| 143 : Offset(Other.Offset), Name(Other.Name), |
| 144 SuppressMangling(Other.SuppressMangling) {} |
| 145 |
| 146 const int64_t Offset; |
| 147 const IceString Name; |
| 148 bool SuppressMangling; |
| 149 }; |
| 150 |
| 151 // ConstantRelocatable represents a symbolic constant combined with |
| 152 // a fixed offset. |
| 153 class ConstantRelocatable : public Constant { |
| 154 public: |
| 155 static ConstantRelocatable *create(GlobalContext *Ctx, IceType Type, |
| 156 const RelocatableTuple &Tuple) { |
| 157 return new (Ctx->allocate<ConstantRelocatable>()) ConstantRelocatable( |
| 158 Type, Tuple.Offset, Tuple.Name, Tuple.SuppressMangling); |
| 159 } |
| 160 int64_t getOffset() const { return Offset; } |
| 161 IceString getName() const { return Name; } |
| 162 void setSuppressMangling(bool Value) { SuppressMangling = Value; } |
| 163 bool getSuppressMangling() const { return SuppressMangling; } |
| 164 virtual void dump(const IceCfg *Cfg) const; |
| 165 |
| 166 static bool classof(const Operand *Operand) { |
| 167 OperandKind Kind = Operand->getKind(); |
| 168 return Kind == kConstRelocatable; |
| 169 } |
| 170 |
| 171 private: |
| 172 ConstantRelocatable(IceType Type, int64_t Offset, const IceString &Name, |
| 173 bool SuppressMangling) |
| 174 : Constant(kConstRelocatable, Type), Offset(Offset), Name(Name), |
| 175 SuppressMangling(SuppressMangling) {} |
| 176 ConstantRelocatable(const ConstantRelocatable &) LLVM_DELETED_FUNCTION; |
| 177 ConstantRelocatable & |
| 178 operator=(const ConstantRelocatable &) LLVM_DELETED_FUNCTION; |
| 179 virtual ~ConstantRelocatable() {} |
| 180 const int64_t Offset; // fixed offset to add |
| 181 const IceString Name; // optional for debug/dump |
| 182 bool SuppressMangling; |
| 183 }; |
| 184 |
| 185 // Variable represents an operand that is register-allocated or |
| 186 // stack-allocated. If it is register-allocated, it will ultimately |
| 187 // have a non-negative RegNum field. |
| 188 class Variable : public Operand { |
| 189 public: |
| 190 static Variable *create(IceCfg *Cfg, IceType Type, const CfgNode *Node, |
| 191 IceSize_t Index, const IceString &Name) { |
| 192 return new (Cfg->allocate<Variable>()) Variable(Type, Node, Index, Name); |
| 193 } |
| 194 |
| 195 IceSize_t getIndex() const { return Number; } |
| 196 IceString getName() const; |
| 197 |
| 198 Inst *getDefinition() const { return DefInst; } |
| 199 void setDefinition(Inst *Inst, const CfgNode *Node); |
| 200 void replaceDefinition(Inst *Inst, const CfgNode *Node); |
| 201 |
| 202 const CfgNode *getLocalUseNode() const { return DefNode; } |
| 203 bool isMultiblockLife() const { return (DefNode == NULL); } |
| 204 void setUse(const Inst *Inst, const CfgNode *Node); |
| 205 |
| 206 bool getIsArg() const { return IsArgument; } |
| 207 void setIsArg(IceCfg *Cfg); |
| 208 |
| 209 virtual void dump(const IceCfg *Cfg) const; |
| 210 |
| 211 static bool classof(const Operand *Operand) { |
| 212 return Operand->getKind() == kVariable; |
| 213 } |
| 214 |
| 215 private: |
| 216 Variable(IceType Type, const CfgNode *Node, IceSize_t Index, |
| 217 const IceString &Name) |
| 218 : Operand(kVariable, Type), Number(Index), Name(Name), DefInst(NULL), |
| 219 DefNode(Node), IsArgument(false) { |
| 220 Vars = VarsReal; |
| 221 Vars[0] = this; |
| 222 NumVars = 1; |
| 223 } |
| 224 Variable(const Variable &) LLVM_DELETED_FUNCTION; |
| 225 Variable &operator=(const Variable &) LLVM_DELETED_FUNCTION; |
| 226 virtual ~Variable() {} |
| 227 // Number is unique across all variables, and is used as a |
| 228 // (bit)vector index for liveness analysis. |
| 229 const IceSize_t Number; |
| 230 // Name is optional. |
| 231 const IceString Name; |
| 232 // DefInst is the instruction that produces this variable as its |
| 233 // dest. |
| 234 Inst *DefInst; |
| 235 // DefNode is the node where this variable was produced, and is |
| 236 // reset to NULL if it is used outside that node. This is used for |
| 237 // detecting isMultiblockLife(). |
| 238 const CfgNode *DefNode; |
| 239 bool IsArgument; |
| 240 // VarsReal (and Operand::Vars) are set up such that Vars[0] == |
| 241 // this. |
| 242 Variable *VarsReal[1]; |
| 243 }; |
| 244 |
| 245 } // end of namespace Ice |
| 246 |
| 247 #endif // SUBZERO_SRC_ICEOPERAND_H |
OLD | NEW |