OLD | NEW |
---|---|
1 //===- subzero/src/IceOperand.h - High-level operands -----------*- C++ -*-===// | 1 //===- subzero/src/IceOperand.h - High-level operands -----------*- C++ -*-===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This file declares the Operand class and its target-independent | 10 // This file declares the Operand class and its target-independent |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 | 42 |
43 // Every Operand keeps an array of the Variables referenced in | 43 // Every Operand keeps an array of the Variables referenced in |
44 // the operand. This is so that the liveness operations can get | 44 // the operand. This is so that the liveness operations can get |
45 // quick access to the variables of interest, without having to dig | 45 // quick access to the variables of interest, without having to dig |
46 // so far into the operand. | 46 // so far into the operand. |
47 SizeT getNumVars() const { return NumVars; } | 47 SizeT getNumVars() const { return NumVars; } |
48 Variable *getVar(SizeT I) const { | 48 Variable *getVar(SizeT I) const { |
49 assert(I < getNumVars()); | 49 assert(I < getNumVars()); |
50 return Vars[I]; | 50 return Vars[I]; |
51 } | 51 } |
52 virtual void emit(const Cfg *Func) const = 0; | |
52 virtual void dump(const Cfg *Func) const = 0; | 53 virtual void dump(const Cfg *Func) const = 0; |
53 | 54 |
54 // Query whether this object was allocated in isolation, or added to | 55 // Query whether this object was allocated in isolation, or added to |
55 // some higher-level pool. This determines whether a containing | 56 // some higher-level pool. This determines whether a containing |
56 // object's destructor should delete this object. Generally, | 57 // object's destructor should delete this object. Generally, |
57 // constants are pooled globally, variables are pooled per-CFG, and | 58 // constants are pooled globally, variables are pooled per-CFG, and |
58 // target-specific operands are not pooled. | 59 // target-specific operands are not pooled. |
59 virtual bool isPooled() const { return false; } | 60 virtual bool isPooled() const { return false; } |
60 | 61 |
61 virtual ~Operand() {} | 62 virtual ~Operand() {} |
(...skipping 10 matching lines...) Expand all Loading... | |
72 | 73 |
73 private: | 74 private: |
74 Operand(const Operand &) LLVM_DELETED_FUNCTION; | 75 Operand(const Operand &) LLVM_DELETED_FUNCTION; |
75 Operand &operator=(const Operand &) LLVM_DELETED_FUNCTION; | 76 Operand &operator=(const Operand &) LLVM_DELETED_FUNCTION; |
76 }; | 77 }; |
77 | 78 |
78 // Constant is the abstract base class for constants. All | 79 // Constant is the abstract base class for constants. All |
79 // constants are allocated from a global arena and are pooled. | 80 // constants are allocated from a global arena and are pooled. |
80 class Constant : public Operand { | 81 class Constant : public Operand { |
81 public: | 82 public: |
83 virtual void emit(const Cfg *Func) const = 0; | |
82 virtual void dump(const Cfg *Func) const = 0; | 84 virtual void dump(const Cfg *Func) const = 0; |
83 | 85 |
84 static bool classof(const Operand *Operand) { | 86 static bool classof(const Operand *Operand) { |
85 OperandKind Kind = Operand->getKind(); | 87 OperandKind Kind = Operand->getKind(); |
86 return Kind >= kConst_Base && Kind <= kConst_Num; | 88 return Kind >= kConst_Base && Kind <= kConst_Num; |
87 } | 89 } |
88 | 90 |
89 protected: | 91 protected: |
90 Constant(OperandKind Kind, Type Ty) : Operand(Kind, Ty) { | 92 Constant(OperandKind Kind, Type Ty) : Operand(Kind, Ty) { |
91 Vars = NULL; | 93 Vars = NULL; |
92 NumVars = 0; | 94 NumVars = 0; |
93 } | 95 } |
94 virtual ~Constant() {} | 96 virtual ~Constant() {} |
95 | 97 |
96 private: | 98 private: |
97 Constant(const Constant &) LLVM_DELETED_FUNCTION; | 99 Constant(const Constant &) LLVM_DELETED_FUNCTION; |
98 Constant &operator=(const Constant &) LLVM_DELETED_FUNCTION; | 100 Constant &operator=(const Constant &) LLVM_DELETED_FUNCTION; |
99 }; | 101 }; |
100 | 102 |
101 // ConstantPrimitive<> wraps a primitive type. | 103 // ConstantPrimitive<> wraps a primitive type. |
102 template <typename T, Operand::OperandKind K> | 104 template <typename T, Operand::OperandKind K> |
103 class ConstantPrimitive : public Constant { | 105 class ConstantPrimitive : public Constant { |
104 public: | 106 public: |
105 static ConstantPrimitive *create(GlobalContext *Ctx, Type Ty, T Value) { | 107 static ConstantPrimitive *create(GlobalContext *Ctx, Type Ty, T Value) { |
106 return new (Ctx->allocate<ConstantPrimitive>()) | 108 return new (Ctx->allocate<ConstantPrimitive>()) |
107 ConstantPrimitive(Ty, Value); | 109 ConstantPrimitive(Ty, Value); |
108 } | 110 } |
109 T getValue() const { return Value; } | 111 T getValue() const { return Value; } |
112 virtual void emit(const Cfg *Func) const { | |
113 Ostream &Str = Func->getContext()->getStrEmit(); | |
114 Str << getValue(); | |
115 } | |
110 virtual void dump(const Cfg *Func) const { | 116 virtual void dump(const Cfg *Func) const { |
111 Ostream &Str = Func->getContext()->getStrDump(); | 117 Ostream &Str = Func->getContext()->getStrDump(); |
112 Str << getValue(); | 118 Str << getValue(); |
113 } | 119 } |
114 | 120 |
115 static bool classof(const Operand *Operand) { | 121 static bool classof(const Operand *Operand) { |
116 return Operand->getKind() == K; | 122 return Operand->getKind() == K; |
117 } | 123 } |
118 | 124 |
119 private: | 125 private: |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 public: | 162 public: |
157 static ConstantRelocatable *create(GlobalContext *Ctx, Type Ty, | 163 static ConstantRelocatable *create(GlobalContext *Ctx, Type Ty, |
158 const RelocatableTuple &Tuple) { | 164 const RelocatableTuple &Tuple) { |
159 return new (Ctx->allocate<ConstantRelocatable>()) ConstantRelocatable( | 165 return new (Ctx->allocate<ConstantRelocatable>()) ConstantRelocatable( |
160 Ty, Tuple.Offset, Tuple.Name, Tuple.SuppressMangling); | 166 Ty, Tuple.Offset, Tuple.Name, Tuple.SuppressMangling); |
161 } | 167 } |
162 int64_t getOffset() const { return Offset; } | 168 int64_t getOffset() const { return Offset; } |
163 IceString getName() const { return Name; } | 169 IceString getName() const { return Name; } |
164 void setSuppressMangling(bool Value) { SuppressMangling = Value; } | 170 void setSuppressMangling(bool Value) { SuppressMangling = Value; } |
165 bool getSuppressMangling() const { return SuppressMangling; } | 171 bool getSuppressMangling() const { return SuppressMangling; } |
172 virtual void emit(const Cfg *Func) const; | |
166 virtual void dump(const Cfg *Func) const; | 173 virtual void dump(const Cfg *Func) const; |
167 | 174 |
168 static bool classof(const Operand *Operand) { | 175 static bool classof(const Operand *Operand) { |
169 OperandKind Kind = Operand->getKind(); | 176 OperandKind Kind = Operand->getKind(); |
170 return Kind == kConstRelocatable; | 177 return Kind == kConstRelocatable; |
171 } | 178 } |
172 | 179 |
173 private: | 180 private: |
174 ConstantRelocatable(Type Ty, int64_t Offset, const IceString &Name, | 181 ConstantRelocatable(Type Ty, int64_t Offset, const IceString &Name, |
175 bool SuppressMangling) | 182 bool SuppressMangling) |
176 : Constant(kConstRelocatable, Ty), Offset(Offset), Name(Name), | 183 : Constant(kConstRelocatable, Ty), Offset(Offset), Name(Name), |
177 SuppressMangling(SuppressMangling) {} | 184 SuppressMangling(SuppressMangling) {} |
178 ConstantRelocatable(const ConstantRelocatable &) LLVM_DELETED_FUNCTION; | 185 ConstantRelocatable(const ConstantRelocatable &) LLVM_DELETED_FUNCTION; |
179 ConstantRelocatable & | 186 ConstantRelocatable & |
180 operator=(const ConstantRelocatable &) LLVM_DELETED_FUNCTION; | 187 operator=(const ConstantRelocatable &) LLVM_DELETED_FUNCTION; |
181 virtual ~ConstantRelocatable() {} | 188 virtual ~ConstantRelocatable() {} |
182 const int64_t Offset; // fixed offset to add | 189 const int64_t Offset; // fixed offset to add |
183 const IceString Name; // optional for debug/dump | 190 const IceString Name; // optional for debug/dump |
184 bool SuppressMangling; | 191 bool SuppressMangling; |
185 }; | 192 }; |
186 | 193 |
194 // RegWeight is a wrapper for a uint32_t weight value, with a | |
195 // special value that represents infinite weight, and an addWeight() | |
196 // method that ensures that W+infinity=infinity. | |
197 class RegWeight { | |
198 public: | |
199 RegWeight() : Weight(0) {} | |
200 RegWeight(uint32_t Weight) : Weight(Weight) {} | |
201 const static uint32_t Inf = ~0; // Force regalloc to give a register | |
202 const static uint32_t Zero = 0; // Force regalloc NOT to give a register | |
203 void addWeight(uint32_t Delta) { | |
204 if (Delta == Inf) | |
205 Weight = Inf; | |
206 else if (Weight != Inf) | |
207 Weight += Delta; | |
208 } | |
209 void addWeight(const RegWeight &Other) { addWeight(Other.Weight); } | |
210 void setWeight(uint32_t Val) { Weight = Val; } | |
211 uint32_t getWeight() const { return Weight; } | |
212 bool isInf() const { return Weight == Inf; } | |
213 | |
214 private: | |
215 uint32_t Weight; | |
216 }; | |
217 Ostream &operator<<(Ostream &Str, const RegWeight &W); | |
218 bool operator<(const RegWeight &A, const RegWeight &B); | |
219 bool operator<=(const RegWeight &A, const RegWeight &B); | |
220 bool operator==(const RegWeight &A, const RegWeight &B); | |
221 | |
187 // Variable represents an operand that is register-allocated or | 222 // Variable represents an operand that is register-allocated or |
188 // stack-allocated. If it is register-allocated, it will ultimately | 223 // stack-allocated. If it is register-allocated, it will ultimately |
189 // have a non-negative RegNum field. | 224 // have a non-negative RegNum field. |
190 class Variable : public Operand { | 225 class Variable : public Operand { |
191 public: | 226 public: |
192 static Variable *create(Cfg *Func, Type Ty, const CfgNode *Node, SizeT Index, | 227 static Variable *create(Cfg *Func, Type Ty, const CfgNode *Node, SizeT Index, |
193 const IceString &Name) { | 228 const IceString &Name) { |
194 return new (Func->allocate<Variable>()) Variable(Ty, Node, Index, Name); | 229 return new (Func->allocate<Variable>()) Variable(Ty, Node, Index, Name); |
195 } | 230 } |
196 | 231 |
197 SizeT getIndex() const { return Number; } | 232 SizeT getIndex() const { return Number; } |
198 IceString getName() const; | 233 IceString getName() const; |
199 | 234 |
200 Inst *getDefinition() const { return DefInst; } | 235 Inst *getDefinition() const { return DefInst; } |
201 void setDefinition(Inst *Inst, const CfgNode *Node); | 236 void setDefinition(Inst *Inst, const CfgNode *Node); |
202 void replaceDefinition(Inst *Inst, const CfgNode *Node); | 237 void replaceDefinition(Inst *Inst, const CfgNode *Node); |
203 | 238 |
204 const CfgNode *getLocalUseNode() const { return DefNode; } | 239 const CfgNode *getLocalUseNode() const { return DefNode; } |
205 bool isMultiblockLife() const { return (DefNode == NULL); } | 240 bool isMultiblockLife() const { return (DefNode == NULL); } |
206 void setUse(const Inst *Inst, const CfgNode *Node); | 241 void setUse(const Inst *Inst, const CfgNode *Node); |
207 | 242 |
208 bool getIsArg() const { return IsArgument; } | 243 bool getIsArg() const { return IsArgument; } |
209 void setIsArg(Cfg *Func); | 244 void setIsArg(Cfg *Func); |
210 | 245 |
246 int32_t getStackOffset() const { return StackOffset; } | |
247 void setStackOffset(int32_t Offset) { StackOffset = Offset; } | |
248 | |
249 static const int32_t NoRegister = -1; | |
250 bool hasReg() const { return getRegNum() != NoRegister; } | |
251 int32_t getRegNum() const { return RegNum; } | |
252 void setRegNum(int32_t NewRegNum) { | |
253 // Regnum shouldn't be set more than once. | |
254 assert(!hasReg() || RegNum == NewRegNum); | |
255 RegNum = NewRegNum; | |
256 } | |
257 | |
258 RegWeight getWeight() const { return Weight; } | |
259 void setWeight(uint32_t NewWeight) { Weight = NewWeight; } | |
260 void setWeightInfinite() { Weight = RegWeight::Inf; } | |
261 | |
262 Variable *getPreferredRegister() const { return RegisterPreference; } | |
263 bool getRegisterOverlap() const { return AllowRegisterOverlap; } | |
264 void setPreferredRegister(Variable *Prefer, bool Overlap) { | |
265 RegisterPreference = Prefer; | |
266 AllowRegisterOverlap = Overlap; | |
267 } | |
268 | |
269 Variable *getLo() const { return LoVar; } | |
270 Variable *getHi() const { return HiVar; } | |
271 void setLoHi(Variable *Lo, Variable *Hi) { | |
272 assert(LoVar == NULL); | |
273 assert(HiVar == NULL); | |
274 LoVar = Lo; | |
275 HiVar = Hi; | |
276 } | |
277 // Creates a temporary copy of the variable with a different type. | |
278 // Used primarily for syntactic correctness of textual assembly | |
279 // emission. | |
280 Variable asType(Type Ty); | |
281 | |
282 virtual void emit(const Cfg *Func) const; | |
211 virtual void dump(const Cfg *Func) const; | 283 virtual void dump(const Cfg *Func) const; |
212 | 284 |
213 static bool classof(const Operand *Operand) { | 285 static bool classof(const Operand *Operand) { |
214 return Operand->getKind() == kVariable; | 286 return Operand->getKind() == kVariable; |
215 } | 287 } |
216 | 288 |
289 // The destructor is public because of the asType() method. | |
290 virtual ~Variable() {} | |
291 | |
217 private: | 292 private: |
218 Variable(Type Ty, const CfgNode *Node, SizeT Index, const IceString &Name) | 293 Variable(Type Ty, const CfgNode *Node, SizeT Index, const IceString &Name) |
219 : Operand(kVariable, Ty), Number(Index), Name(Name), DefInst(NULL), | 294 : Operand(kVariable, Ty), Number(Index), Name(Name), DefInst(NULL), |
220 DefNode(Node), IsArgument(false) { | 295 DefNode(Node), IsArgument(false), StackOffset(0), RegNum(NoRegister), |
296 Weight(1), RegisterPreference(NULL), AllowRegisterOverlap(false), | |
297 LoVar(NULL), HiVar(NULL) { | |
221 Vars = VarsReal; | 298 Vars = VarsReal; |
222 Vars[0] = this; | 299 Vars[0] = this; |
223 NumVars = 1; | 300 NumVars = 1; |
224 } | 301 } |
225 Variable(const Variable &) LLVM_DELETED_FUNCTION; | 302 Variable(const Variable &) LLVM_DELETED_FUNCTION; |
226 Variable &operator=(const Variable &) LLVM_DELETED_FUNCTION; | 303 Variable &operator=(const Variable &) LLVM_DELETED_FUNCTION; |
227 virtual ~Variable() {} | |
228 // Number is unique across all variables, and is used as a | 304 // Number is unique across all variables, and is used as a |
229 // (bit)vector index for liveness analysis. | 305 // (bit)vector index for liveness analysis. |
230 const SizeT Number; | 306 const SizeT Number; |
231 // Name is optional. | 307 // Name is optional. |
232 const IceString Name; | 308 const IceString Name; |
233 // DefInst is the instruction that produces this variable as its | 309 // DefInst is the instruction that produces this variable as its |
234 // dest. | 310 // dest. |
235 Inst *DefInst; | 311 Inst *DefInst; |
236 // DefNode is the node where this variable was produced, and is | 312 // DefNode is the node where this variable was produced, and is |
237 // reset to NULL if it is used outside that node. This is used for | 313 // reset to NULL if it is used outside that node. This is used for |
238 // detecting isMultiblockLife(). TODO: Collapse this to a single | 314 // detecting isMultiblockLife(). TODO: Collapse this to a single |
239 // bit and use a separate pass to calculate the values across the | 315 // bit and use a separate pass to calculate the values across the |
240 // Cfg. This saves space in the Variable, and removes the fragility | 316 // Cfg. This saves space in the Variable, and removes the fragility |
241 // of incrementally computing and maintaining the information. | 317 // of incrementally computing and maintaining the information. |
242 const CfgNode *DefNode; | 318 const CfgNode *DefNode; |
243 bool IsArgument; | 319 bool IsArgument; |
320 // StackOffset is the canonical location on stack (only if | |
321 // RegNum<0 || IsArgument). | |
322 int32_t StackOffset; | |
323 // RegNum is the allocated register, or -1 if it isn't | |
JF
2014/05/04 23:54:58
s/-1/NoRegister/
Jim Stichnoth
2014/05/05 07:03:55
Done.
| |
324 // register-allocated. | |
325 int32_t RegNum; | |
326 RegWeight Weight; // Register allocation priority | |
327 // RegisterPreference says that if possible, the register allocator | |
328 // should prefer the register that was assigned to this linked | |
329 // variable. It also allows a spill slot to share its stack | |
330 // location with another variable, if that variable does not get | |
331 // register-allocated and therefore has a stack location. | |
332 Variable *RegisterPreference; | |
333 // AllowRegisterOverlap says that it is OK to honor | |
334 // RegisterPreference and "share" a register even if the two live | |
335 // ranges overlap. | |
336 bool AllowRegisterOverlap; | |
337 // LoVar and HiVar are needed for lowering from 64 to 32 bits. When | |
338 // lowering from I64 to I32 on a 32-bit architecture, we split the | |
339 // variable into two machine-size pieces. LoVar is the low-order | |
340 // machine-size portion, and HiVar is the remaining high-order | |
341 // portion. TODO: It's wasteful to penalize all variables on all | |
342 // targets this way; use a sparser representation. It's also | |
343 // wasteful for a 64-bit target. | |
344 Variable *LoVar; | |
345 Variable *HiVar; | |
244 // VarsReal (and Operand::Vars) are set up such that Vars[0] == | 346 // VarsReal (and Operand::Vars) are set up such that Vars[0] == |
245 // this. | 347 // this. |
246 Variable *VarsReal[1]; | 348 Variable *VarsReal[1]; |
247 }; | 349 }; |
248 | 350 |
249 } // end of namespace Ice | 351 } // end of namespace Ice |
250 | 352 |
251 #endif // SUBZERO_SRC_ICEOPERAND_H | 353 #endif // SUBZERO_SRC_ICEOPERAND_H |
OLD | NEW |