Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceCfg.h - Control flow graph ----------------*- C++ -*-===// | 1 //===- subzero/src/IceCfg.h - Control flow graph ----------------*- 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 Cfg class, which represents the control flow | 10 // This file declares the Cfg class, which represents the control flow |
| 11 // graph and the overall per-function compilation context. | 11 // graph and the overall per-function compilation context. |
| 12 // | 12 // |
| 13 //===----------------------------------------------------------------------===// | 13 //===----------------------------------------------------------------------===// |
| 14 | 14 |
| 15 #ifndef SUBZERO_SRC_ICECFG_H | 15 #ifndef SUBZERO_SRC_ICECFG_H |
| 16 #define SUBZERO_SRC_ICECFG_H | 16 #define SUBZERO_SRC_ICECFG_H |
| 17 | 17 |
| 18 #include <memory> | 18 #include <memory> |
| 19 | 19 |
| 20 #include "llvm/Support/Allocator.h" | |
| 21 | |
| 22 #include "assembler.h" | 20 #include "assembler.h" |
| 23 #include "IceClFlags.h" | 21 #include "IceClFlags.h" |
| 24 #include "IceDefs.h" | 22 #include "IceDefs.h" |
| 25 #include "IceGlobalContext.h" | 23 #include "IceGlobalContext.h" |
| 26 #include "IceTypes.h" | 24 #include "IceTypes.h" |
| 27 | 25 |
| 28 namespace Ice { | 26 namespace Ice { |
| 29 | 27 |
| 30 class Cfg { | 28 class Cfg { |
| 31 Cfg(const Cfg &) = delete; | 29 Cfg(const Cfg &) = delete; |
| 32 Cfg &operator=(const Cfg &) = delete; | 30 Cfg &operator=(const Cfg &) = delete; |
| 33 | 31 |
| 34 public: | 32 public: |
| 35 Cfg(GlobalContext *Ctx); | |
| 36 ~Cfg(); | 33 ~Cfg(); |
| 37 | 34 |
| 35 static Cfg *create(GlobalContext *Ctx) { | |
| 36 Cfg *Func = new Cfg(Ctx); | |
| 37 CurrentCfg = Func; | |
| 38 return Func; | |
| 39 } | |
|
JF
2014/12/18 06:36:53
You should have a symmetric "destroy" static membe
Jim Stichnoth
2014/12/18 23:39:55
After discussion offline, I added a TODO to change
| |
| 40 // Gets a pointer to the current thread's Cfg. | |
| 41 static const Cfg *getCurrentCfg() { return CurrentCfg; } | |
| 42 // Gets a pointer to the current thread's Cfg's allocator. | |
| 43 static ArenaAllocator *getCurrentCfgAllocator() { | |
| 44 assert(CurrentCfg); | |
| 45 return CurrentCfg->Allocator.get(); | |
| 46 } | |
| 47 | |
| 38 GlobalContext *getContext() const { return Ctx; } | 48 GlobalContext *getContext() const { return Ctx; } |
| 39 | 49 |
| 40 // Manage the name and return type of the function being translated. | 50 // Manage the name and return type of the function being translated. |
| 41 void setFunctionName(const IceString &Name) { FunctionName = Name; } | 51 void setFunctionName(const IceString &Name) { FunctionName = Name; } |
| 42 IceString getFunctionName() const { return FunctionName; } | 52 IceString getFunctionName() const { return FunctionName; } |
| 43 void setReturnType(Type Ty) { ReturnType = Ty; } | 53 void setReturnType(Type Ty) { ReturnType = Ty; } |
| 44 | 54 |
| 45 // Manage the "internal" attribute of the function. | 55 // Manage the "internal" attribute of the function. |
| 46 void setInternal(bool Internal) { IsInternalLinkage = Internal; } | 56 void setInternal(bool Internal) { IsInternalLinkage = Internal; } |
| 47 bool getInternal() const { return IsInternalLinkage; } | 57 bool getInternal() const { return IsInternalLinkage; } |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 143 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } | 153 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } |
| 144 void resetCurrentNode() { setCurrentNode(NULL); } | 154 void resetCurrentNode() { setCurrentNode(NULL); } |
| 145 const CfgNode *getCurrentNode() const { return CurrentNode; } | 155 const CfgNode *getCurrentNode() const { return CurrentNode; } |
| 146 | 156 |
| 147 void emit(); | 157 void emit(); |
| 148 void emitIAS(); | 158 void emitIAS(); |
| 149 void emitTextHeader(const IceString &MangledName); | 159 void emitTextHeader(const IceString &MangledName); |
| 150 void dump(const IceString &Message = ""); | 160 void dump(const IceString &Message = ""); |
| 151 | 161 |
| 152 // Allocate data of type T using the per-Cfg allocator. | 162 // Allocate data of type T using the per-Cfg allocator. |
| 153 template <typename T> T *allocate() { return Allocator.Allocate<T>(); } | 163 template <typename T> T *allocate() { return Allocator->Allocate<T>(); } |
| 154 | |
| 155 // Allocate an instruction of type T using the per-Cfg instruction allocator. | |
| 156 template <typename T> T *allocateInst() { return Allocator.Allocate<T>(); } | |
| 157 | 164 |
| 158 // Allocate an array of data of type T using the per-Cfg allocator. | 165 // Allocate an array of data of type T using the per-Cfg allocator. |
| 159 template <typename T> T *allocateArrayOf(size_t NumElems) { | 166 template <typename T> T *allocateArrayOf(size_t NumElems) { |
| 160 return Allocator.Allocate<T>(NumElems); | 167 return Allocator->Allocate<T>(NumElems); |
| 161 } | 168 } |
| 162 | 169 |
| 163 // Deallocate data that was allocated via allocate<T>(). | 170 // Deallocate data that was allocated via allocate<T>(). |
| 164 template <typename T> void deallocate(T *Object) { | 171 template <typename T> void deallocate(T *Object) { |
| 165 Allocator.Deallocate(Object); | 172 Allocator->Deallocate(Object); |
| 166 } | |
| 167 | |
| 168 // Deallocate data that was allocated via allocateInst<T>(). | |
| 169 template <typename T> void deallocateInst(T *Instr) { | |
| 170 Allocator.Deallocate(Instr); | |
| 171 } | 173 } |
| 172 | 174 |
| 173 // Deallocate data that was allocated via allocateArrayOf<T>(). | 175 // Deallocate data that was allocated via allocateArrayOf<T>(). |
| 174 template <typename T> void deallocateArrayOf(T *Array) { | 176 template <typename T> void deallocateArrayOf(T *Array) { |
| 175 Allocator.Deallocate(Array); | 177 Allocator->Deallocate(Array); |
| 176 } | 178 } |
| 177 | 179 |
| 178 private: | 180 private: |
| 179 // TODO: for now, everything is allocated from the same allocator. In the | 181 Cfg(GlobalContext *Ctx); |
| 180 // future we may want to split this to several allocators, for example in | |
| 181 // order to use a "Recycler" to preserve memory. If we keep all allocation | |
| 182 // requests from the Cfg exposed via methods, we can always switch the | |
| 183 // implementation over at a later point. | |
| 184 llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator, 1024 * 1024> Allocator; | |
| 185 | 182 |
| 186 GlobalContext *Ctx; | 183 GlobalContext *Ctx; |
| 187 IceString FunctionName; | 184 IceString FunctionName; |
| 188 Type ReturnType; | 185 Type ReturnType; |
| 189 bool IsInternalLinkage; | 186 bool IsInternalLinkage; |
| 190 bool HasError; | 187 bool HasError; |
| 191 bool FocusedTiming; | 188 bool FocusedTiming; |
| 192 IceString ErrorMessage; | 189 IceString ErrorMessage; |
| 193 CfgNode *Entry; // entry basic block | 190 CfgNode *Entry; // entry basic block |
| 194 NodeList Nodes; // linearized node list; Entry should be first | 191 NodeList Nodes; // linearized node list; Entry should be first |
| 195 std::vector<IceString> IdentifierNames; | 192 std::vector<IceString> IdentifierNames; |
| 196 InstNumberT NextInstNumber; | 193 InstNumberT NextInstNumber; |
| 197 VarList Variables; | 194 VarList Variables; |
| 198 VarList Args; // subset of Variables, in argument order | 195 VarList Args; // subset of Variables, in argument order |
| 199 VarList ImplicitArgs; // subset of Variables | 196 VarList ImplicitArgs; // subset of Variables |
| 197 std::unique_ptr<ArenaAllocator> Allocator; | |
| 200 std::unique_ptr<Liveness> Live; | 198 std::unique_ptr<Liveness> Live; |
| 201 std::unique_ptr<TargetLowering> Target; | 199 std::unique_ptr<TargetLowering> Target; |
| 202 std::unique_ptr<VariablesMetadata> VMetadata; | 200 std::unique_ptr<VariablesMetadata> VMetadata; |
| 203 std::unique_ptr<Assembler> TargetAssembler; | 201 std::unique_ptr<Assembler> TargetAssembler; |
| 204 | 202 |
| 205 // CurrentNode is maintained during dumping/emitting just for | 203 // CurrentNode is maintained during dumping/emitting just for |
| 206 // validating Variable::DefNode. Normally, a traversal over | 204 // validating Variable::DefNode. Normally, a traversal over |
| 207 // CfgNodes maintains this, but before global operations like | 205 // CfgNodes maintains this, but before global operations like |
| 208 // register allocation, resetCurrentNode() should be called to avoid | 206 // register allocation, resetCurrentNode() should be called to avoid |
| 209 // spurious validation failures. | 207 // spurious validation failures. |
| 210 const CfgNode *CurrentNode; | 208 const CfgNode *CurrentNode; |
| 209 | |
| 210 // Maintain a pointer in TLS to the current Cfg being translated. | |
| 211 // This is primarily for accessing its allocator statelessly, but | |
| 212 // other uses are possible. | |
| 213 thread_local static const Cfg *CurrentCfg; | |
| 211 }; | 214 }; |
| 212 | 215 |
| 213 } // end of namespace Ice | 216 } // end of namespace Ice |
| 214 | 217 |
| 215 #endif // SUBZERO_SRC_ICECFG_H | 218 #endif // SUBZERO_SRC_ICECFG_H |
| OLD | NEW |