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 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 Cfg(const Cfg &) = delete; | 28 Cfg(const Cfg &) = delete; |
| 29 Cfg &operator=(const Cfg &) = delete; | 29 Cfg &operator=(const Cfg &) = delete; |
| 30 | 30 |
| 31 public: | 31 public: |
| 32 ~Cfg(); | 32 ~Cfg(); |
| 33 | 33 |
| 34 static std::unique_ptr<Cfg> create(GlobalContext *Ctx, | 34 static std::unique_ptr<Cfg> create(GlobalContext *Ctx, |
| 35 uint32_t SequenceNumber) { | 35 uint32_t SequenceNumber) { |
| 36 return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber)); | 36 return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber)); |
| 37 } | 37 } |
| 38 // Gets a pointer to the current thread's Cfg. | 38 /// Gets a pointer to the current thread's Cfg. |
| 39 static const Cfg *getCurrentCfg() { return ICE_TLS_GET_FIELD(CurrentCfg); } | 39 static const Cfg *getCurrentCfg() { return ICE_TLS_GET_FIELD(CurrentCfg); } |
| 40 static void setCurrentCfg(const Cfg *Func) { | 40 static void setCurrentCfg(const Cfg *Func) { |
| 41 ICE_TLS_SET_FIELD(CurrentCfg, Func); | 41 ICE_TLS_SET_FIELD(CurrentCfg, Func); |
| 42 } | 42 } |
| 43 // Gets a pointer to the current thread's Cfg's allocator. | 43 /// Gets a pointer to the current thread's Cfg's allocator. |
| 44 static ArenaAllocator<> *getCurrentCfgAllocator() { | 44 static ArenaAllocator<> *getCurrentCfgAllocator() { |
| 45 assert(ICE_TLS_GET_FIELD(CurrentCfg)); | 45 assert(ICE_TLS_GET_FIELD(CurrentCfg)); |
| 46 return ICE_TLS_GET_FIELD(CurrentCfg)->Allocator.get(); | 46 return ICE_TLS_GET_FIELD(CurrentCfg)->Allocator.get(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 GlobalContext *getContext() const { return Ctx; } | 49 GlobalContext *getContext() const { return Ctx; } |
| 50 uint32_t getSequenceNumber() const { return SequenceNumber; } | 50 uint32_t getSequenceNumber() const { return SequenceNumber; } |
| 51 | 51 |
| 52 // Returns true if any of the specified options in the verbose mask | 52 /// Returns true if any of the specified options in the verbose mask |
| 53 // are set. If the argument is omitted, it checks if any verbose | 53 /// are set. If the argument is omitted, it checks if any verbose |
| 54 // options at all are set. | 54 /// options at all are set. |
|
Karl
2015/07/06 18:08:47
Should the next two methods be bundled, since the
ascull
2015/07/06 19:29:08
This only applies to isVerbose. It mentions nothin
| |
| 55 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; } | 55 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; } |
| 56 void setVerbose(VerboseMask Mask) { VMask = Mask; } | 56 void setVerbose(VerboseMask Mask) { VMask = Mask; } |
| 57 | 57 |
| 58 // Manage the name and return type of the function being translated. | 58 /// Manage the name and return type of the function being translated. |
|
Karl
2015/07/06 18:08:47
Same here.
ascull
2015/07/06 19:29:08
Done.
| |
| 59 void setFunctionName(const IceString &Name) { FunctionName = Name; } | 59 void setFunctionName(const IceString &Name) { FunctionName = Name; } |
| 60 IceString getFunctionName() const { return FunctionName; } | 60 IceString getFunctionName() const { return FunctionName; } |
| 61 void setReturnType(Type Ty) { ReturnType = Ty; } | 61 void setReturnType(Type Ty) { ReturnType = Ty; } |
| 62 | 62 |
| 63 // Manage the "internal" attribute of the function. | 63 /// Manage the "internal" attribute of the function. |
|
Karl
2015/07/06 18:08:48
Same here.
ascull
2015/07/06 19:29:08
Done.
| |
| 64 void setInternal(bool Internal) { IsInternalLinkage = Internal; } | 64 void setInternal(bool Internal) { IsInternalLinkage = Internal; } |
| 65 bool getInternal() const { return IsInternalLinkage; } | 65 bool getInternal() const { return IsInternalLinkage; } |
| 66 | 66 |
| 67 // Translation error flagging. If support for some construct is | 67 /// Translation error flagging. If support for some construct is |
| 68 // known to be missing, instead of an assertion failure, setError() | 68 /// known to be missing, instead of an assertion failure, setError() |
| 69 // should be called and the error should be propagated back up. | 69 /// should be called and the error should be propagated back up. |
| 70 // This way, we can gracefully fail to translate and let a fallback | 70 /// This way, we can gracefully fail to translate and let a fallback |
| 71 // translator handle the function. | 71 /// translator handle the function. |
|
Karl
2015/07/06 18:08:47
Same here.
ascull
2015/07/06 19:29:08
Done.
| |
| 72 void setError(const IceString &Message); | 72 void setError(const IceString &Message); |
| 73 bool hasError() const { return HasError; } | 73 bool hasError() const { return HasError; } |
| 74 IceString getError() const { return ErrorMessage; } | 74 IceString getError() const { return ErrorMessage; } |
| 75 | 75 |
| 76 // Manage nodes (a.k.a. basic blocks, CfgNodes). | 76 /// Manage nodes (a.k.a. basic blocks, CfgNodes). |
|
Karl
2015/07/06 18:08:47
Same here.
ascull
2015/07/06 19:29:08
Done.
| |
| 77 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; } | 77 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; } |
| 78 CfgNode *getEntryNode() const { return Entry; } | 78 CfgNode *getEntryNode() const { return Entry; } |
| 79 // Create a node and append it to the end of the linearized list. | 79 /// Create a node and append it to the end of the linearized list. |
| 80 CfgNode *makeNode(); | 80 CfgNode *makeNode(); |
| 81 SizeT getNumNodes() const { return Nodes.size(); } | 81 SizeT getNumNodes() const { return Nodes.size(); } |
| 82 const NodeList &getNodes() const { return Nodes; } | 82 const NodeList &getNodes() const { return Nodes; } |
| 83 | 83 |
| 84 typedef int32_t IdentifierIndexType; | 84 typedef int32_t IdentifierIndexType; |
| 85 // Adds a name to the list and returns its index, suitable for the | 85 /// Adds a name to the list and returns its index, suitable for the |
| 86 // argument to getIdentifierName(). No checking for duplicates is | 86 /// argument to getIdentifierName(). No checking for duplicates is |
| 87 // done. This is generally used for node names and variable names | 87 /// done. This is generally used for node names and variable names |
| 88 // to avoid embedding a std::string inside an arena-allocated | 88 /// to avoid embedding a std::string inside an arena-allocated |
| 89 // object. | 89 /// object. |
|
Karl
2015/07/06 18:08:48
Same here?
ascull
2015/07/06 19:29:08
Only applies to add.
| |
| 90 IdentifierIndexType addIdentifierName(const IceString &Name) { | 90 IdentifierIndexType addIdentifierName(const IceString &Name) { |
| 91 IdentifierIndexType Index = IdentifierNames.size(); | 91 IdentifierIndexType Index = IdentifierNames.size(); |
| 92 IdentifierNames.push_back(Name); | 92 IdentifierNames.push_back(Name); |
| 93 return Index; | 93 return Index; |
| 94 } | 94 } |
| 95 const IceString &getIdentifierName(IdentifierIndexType Index) const { | 95 const IceString &getIdentifierName(IdentifierIndexType Index) const { |
| 96 return IdentifierNames[Index]; | 96 return IdentifierNames[Index]; |
| 97 } | 97 } |
| 98 enum { IdentifierIndexInvalid = -1 }; | 98 enum { IdentifierIndexInvalid = -1 }; |
| 99 | 99 |
| 100 // Manage instruction numbering. | 100 /// Manage instruction numbering. |
|
Karl
2015/07/06 18:08:48
Same here?
ascull
2015/07/06 19:29:08
Done.
| |
| 101 InstNumberT newInstNumber() { return NextInstNumber++; } | 101 InstNumberT newInstNumber() { return NextInstNumber++; } |
| 102 InstNumberT getNextInstNumber() const { return NextInstNumber; } | 102 InstNumberT getNextInstNumber() const { return NextInstNumber; } |
| 103 | 103 |
| 104 // Manage Variables. | 104 /// Manage Variables. |
| 105 // Create a new Variable with a particular type and an optional | 105 /// Create a new Variable with a particular type and an optional |
| 106 // name. The Node argument is the node where the variable is defined. | 106 /// name. The Node argument is the node where the variable is defined. |
|
Karl
2015/07/06 18:08:47
Same here.
ascull
2015/07/06 19:29:08
Done.
| |
| 107 // TODO(jpp): untemplate this with two separate methods: makeVariable and | 107 // TODO(jpp): untemplate this with two separate methods: makeVariable and |
| 108 // makeSpillVariable. | 108 // makeSpillVariable. |
| 109 template <typename T = Variable> T *makeVariable(Type Ty) { | 109 template <typename T = Variable> T *makeVariable(Type Ty) { |
| 110 SizeT Index = Variables.size(); | 110 SizeT Index = Variables.size(); |
| 111 T *Var = T::create(this, Ty, Index); | 111 T *Var = T::create(this, Ty, Index); |
| 112 Variables.push_back(Var); | 112 Variables.push_back(Var); |
| 113 return Var; | 113 return Var; |
| 114 } | 114 } |
| 115 SizeT getNumVariables() const { return Variables.size(); } | 115 SizeT getNumVariables() const { return Variables.size(); } |
| 116 const VarList &getVariables() const { return Variables; } | 116 const VarList &getVariables() const { return Variables; } |
| 117 | 117 |
| 118 // Manage arguments to the function. | 118 // Manage arguments to the function. |
|
Karl
2015/07/06 18:08:48
Why not here? It is part of the public API.
ascull
2015/07/06 19:29:08
Done.
| |
| 119 void addArg(Variable *Arg); | 119 void addArg(Variable *Arg); |
| 120 const VarList &getArgs() const { return Args; } | 120 const VarList &getArgs() const { return Args; } |
| 121 VarList &getArgs() { return Args; } | 121 VarList &getArgs() { return Args; } |
| 122 void addImplicitArg(Variable *Arg); | 122 void addImplicitArg(Variable *Arg); |
| 123 const VarList &getImplicitArgs() const { return ImplicitArgs; } | 123 const VarList &getImplicitArgs() const { return ImplicitArgs; } |
| 124 | 124 |
| 125 // Miscellaneous accessors. | 125 // Miscellaneous accessors. |
|
Karl
2015/07/06 18:08:47
Maybe here?
ascull
2015/07/06 19:29:08
Done.
| |
| 126 TargetLowering *getTarget() const { return Target.get(); } | 126 TargetLowering *getTarget() const { return Target.get(); } |
| 127 VariablesMetadata *getVMetadata() const { return VMetadata.get(); } | 127 VariablesMetadata *getVMetadata() const { return VMetadata.get(); } |
| 128 Liveness *getLiveness() const { return Live.get(); } | 128 Liveness *getLiveness() const { return Live.get(); } |
| 129 template <typename T = Assembler> T *getAssembler() const { | 129 template <typename T = Assembler> T *getAssembler() const { |
| 130 return llvm::dyn_cast<T>(TargetAssembler.get()); | 130 return llvm::dyn_cast<T>(TargetAssembler.get()); |
| 131 } | 131 } |
| 132 Assembler *releaseAssembler() { return TargetAssembler.release(); } | 132 Assembler *releaseAssembler() { return TargetAssembler.release(); } |
| 133 std::unique_ptr<VariableDeclarationList> getGlobalInits() { | 133 std::unique_ptr<VariableDeclarationList> getGlobalInits() { |
| 134 return std::move(GlobalInits); | 134 return std::move(GlobalInits); |
| 135 } | 135 } |
| 136 bool hasComputedFrame() const; | 136 bool hasComputedFrame() const; |
| 137 bool getFocusedTiming() const { return FocusedTiming; } | 137 bool getFocusedTiming() const { return FocusedTiming; } |
| 138 void setFocusedTiming() { FocusedTiming = true; } | 138 void setFocusedTiming() { FocusedTiming = true; } |
| 139 | 139 |
| 140 // Returns true if Var is a global variable that is used by the profiling | 140 /// Returns true if Var is a global variable that is used by the profiling |
| 141 // code. | 141 /// code. |
| 142 static bool isProfileGlobal(const VariableDeclaration &Var); | 142 static bool isProfileGlobal(const VariableDeclaration &Var); |
| 143 | 143 |
| 144 // Passes over the CFG. | 144 /// Passes over the CFG. |
| 145 void translate(); | 145 void translate(); |
| 146 // After the CFG is fully constructed, iterate over the nodes and | 146 /// After the CFG is fully constructed, iterate over the nodes and |
| 147 // compute the predecessor and successor edges, in the form of | 147 /// compute the predecessor and successor edges, in the form of |
| 148 // CfgNode::InEdges[] and CfgNode::OutEdges[]. | 148 /// CfgNode::InEdges[] and CfgNode::OutEdges[]. |
| 149 void computeInOutEdges(); | 149 void computeInOutEdges(); |
| 150 void renumberInstructions(); | 150 void renumberInstructions(); |
| 151 void placePhiLoads(); | 151 void placePhiLoads(); |
| 152 void placePhiStores(); | 152 void placePhiStores(); |
| 153 void deletePhis(); | 153 void deletePhis(); |
| 154 void advancedPhiLowering(); | 154 void advancedPhiLowering(); |
| 155 void reorderNodes(); | 155 void reorderNodes(); |
| 156 void doAddressOpt(); | 156 void doAddressOpt(); |
| 157 void doArgLowering(); | 157 void doArgLowering(); |
| 158 void doNopInsertion(); | 158 void doNopInsertion(); |
| 159 void genCode(); | 159 void genCode(); |
| 160 void genFrame(); | 160 void genFrame(); |
| 161 void livenessLightweight(); | 161 void livenessLightweight(); |
| 162 void liveness(LivenessMode Mode); | 162 void liveness(LivenessMode Mode); |
| 163 bool validateLiveness() const; | 163 bool validateLiveness() const; |
| 164 void contractEmptyNodes(); | 164 void contractEmptyNodes(); |
| 165 void doBranchOpt(); | 165 void doBranchOpt(); |
| 166 | 166 |
| 167 // Manage the CurrentNode field, which is used for validating the | 167 /// Manage the CurrentNode field, which is used for validating the |
| 168 // Variable::DefNode field during dumping/emitting. | 168 /// Variable::DefNode field during dumping/emitting. |
|
Karl
2015/07/06 18:08:47
Again, bundle methods for documentation?
ascull
2015/07/06 19:29:08
Done.
| |
| 169 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } | 169 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } |
| 170 void resetCurrentNode() { setCurrentNode(nullptr); } | 170 void resetCurrentNode() { setCurrentNode(nullptr); } |
| 171 const CfgNode *getCurrentNode() const { return CurrentNode; } | 171 const CfgNode *getCurrentNode() const { return CurrentNode; } |
| 172 | 172 |
| 173 void emit(); | 173 void emit(); |
| 174 void emitIAS(); | 174 void emitIAS(); |
| 175 static void emitTextHeader(const IceString &MangledName, GlobalContext *Ctx, | 175 static void emitTextHeader(const IceString &MangledName, GlobalContext *Ctx, |
| 176 const Assembler *Asm); | 176 const Assembler *Asm); |
| 177 void dump(const IceString &Message = ""); | 177 void dump(const IceString &Message = ""); |
| 178 | 178 |
| 179 // Allocate data of type T using the per-Cfg allocator. | 179 /// Allocate data of type T using the per-Cfg allocator. |
| 180 template <typename T> T *allocate() { return Allocator->Allocate<T>(); } | 180 template <typename T> T *allocate() { return Allocator->Allocate<T>(); } |
| 181 | 181 |
| 182 // Allocate an array of data of type T using the per-Cfg allocator. | 182 /// Allocate an array of data of type T using the per-Cfg allocator. |
| 183 template <typename T> T *allocateArrayOf(size_t NumElems) { | 183 template <typename T> T *allocateArrayOf(size_t NumElems) { |
| 184 return Allocator->Allocate<T>(NumElems); | 184 return Allocator->Allocate<T>(NumElems); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Deallocate data that was allocated via allocate<T>(). | 187 /// Deallocate data that was allocated via allocate<T>(). |
| 188 template <typename T> void deallocate(T *Object) { | 188 template <typename T> void deallocate(T *Object) { |
| 189 Allocator->Deallocate(Object); | 189 Allocator->Deallocate(Object); |
| 190 } | 190 } |
| 191 | 191 |
| 192 // Deallocate data that was allocated via allocateArrayOf<T>(). | 192 /// Deallocate data that was allocated via allocateArrayOf<T>(). |
| 193 template <typename T> void deallocateArrayOf(T *Array) { | 193 template <typename T> void deallocateArrayOf(T *Array) { |
| 194 Allocator->Deallocate(Array); | 194 Allocator->Deallocate(Array); |
| 195 } | 195 } |
| 196 | 196 |
| 197 private: | 197 private: |
| 198 Cfg(GlobalContext *Ctx, uint32_t SequenceNumber); | 198 Cfg(GlobalContext *Ctx, uint32_t SequenceNumber); |
| 199 | 199 |
| 200 // Adds a call to the ProfileSummary runtime function as the first instruction | 200 /// Adds a call to the ProfileSummary runtime function as the first instructio n |
|
jvoung (off chromium)
2015/06/30 22:05:48
reflow (past 80 cols)? (fill-paragraph or what the
ascull
2015/07/06 19:29:08
Done.
| |
| 201 // in this CFG's entry block. | 201 /// in this CFG's entry block. |
| 202 void addCallToProfileSummary(); | 202 void addCallToProfileSummary(); |
| 203 | 203 |
| 204 // Iterates over the basic blocks in this CFG, adding profiling code to each | 204 /// Iterates over the basic blocks in this CFG, adding profiling code to each |
| 205 // one of them. It returns a list with all the globals that the profiling code | 205 /// one of them. It returns a list with all the globals that the profiling cod e |
|
jvoung (off chromium)
2015/06/30 22:05:48
reflow
ascull
2015/07/06 19:29:08
Done.
| |
| 206 // needs to be defined. | 206 /// needs to be defined. |
| 207 void profileBlocks(); | 207 void profileBlocks(); |
| 208 | 208 |
| 209 GlobalContext *Ctx; | 209 GlobalContext *Ctx; |
| 210 uint32_t SequenceNumber; // output order for emission | 210 uint32_t SequenceNumber; /// output order for emission |
| 211 VerboseMask VMask; | 211 VerboseMask VMask; |
| 212 IceString FunctionName = ""; | 212 IceString FunctionName = ""; |
| 213 Type ReturnType = IceType_void; | 213 Type ReturnType = IceType_void; |
| 214 bool IsInternalLinkage = false; | 214 bool IsInternalLinkage = false; |
| 215 bool HasError = false; | 215 bool HasError = false; |
| 216 bool FocusedTiming = false; | 216 bool FocusedTiming = false; |
| 217 IceString ErrorMessage = ""; | 217 IceString ErrorMessage = ""; |
| 218 CfgNode *Entry = nullptr; // entry basic block | 218 CfgNode *Entry = nullptr; /// entry basic block |
| 219 NodeList Nodes; // linearized node list; Entry should be first | 219 NodeList Nodes; /// linearized node list; Entry should be first |
| 220 std::vector<IceString> IdentifierNames; | 220 std::vector<IceString> IdentifierNames; |
| 221 InstNumberT NextInstNumber; | 221 InstNumberT NextInstNumber; |
| 222 VarList Variables; | 222 VarList Variables; |
| 223 VarList Args; // subset of Variables, in argument order | 223 VarList Args; /// subset of Variables, in argument order |
| 224 VarList ImplicitArgs; // subset of Variables | 224 VarList ImplicitArgs; /// subset of Variables |
| 225 std::unique_ptr<ArenaAllocator<>> Allocator; | 225 std::unique_ptr<ArenaAllocator<>> Allocator; |
| 226 std::unique_ptr<Liveness> Live; | 226 std::unique_ptr<Liveness> Live; |
| 227 std::unique_ptr<TargetLowering> Target; | 227 std::unique_ptr<TargetLowering> Target; |
| 228 std::unique_ptr<VariablesMetadata> VMetadata; | 228 std::unique_ptr<VariablesMetadata> VMetadata; |
| 229 std::unique_ptr<Assembler> TargetAssembler; | 229 std::unique_ptr<Assembler> TargetAssembler; |
| 230 // Globals required by this CFG. Mostly used for the profiler's globals. | 230 /// Globals required by this CFG. Mostly used for the profiler's globals. |
| 231 std::unique_ptr<VariableDeclarationList> GlobalInits; | 231 std::unique_ptr<VariableDeclarationList> GlobalInits; |
| 232 | 232 |
| 233 // CurrentNode is maintained during dumping/emitting just for | 233 /// CurrentNode is maintained during dumping/emitting just for |
| 234 // validating Variable::DefNode. Normally, a traversal over | 234 /// validating Variable::DefNode. Normally, a traversal over |
| 235 // CfgNodes maintains this, but before global operations like | 235 /// CfgNodes maintains this, but before global operations like |
| 236 // register allocation, resetCurrentNode() should be called to avoid | 236 /// register allocation, resetCurrentNode() should be called to avoid |
| 237 // spurious validation failures. | 237 /// spurious validation failures. |
| 238 const CfgNode *CurrentNode = nullptr; | 238 const CfgNode *CurrentNode = nullptr; |
| 239 | 239 |
| 240 // Maintain a pointer in TLS to the current Cfg being translated. | 240 /// Maintain a pointer in TLS to the current Cfg being translated. |
| 241 // This is primarily for accessing its allocator statelessly, but | 241 /// This is primarily for accessing its allocator statelessly, but |
| 242 // other uses are possible. | 242 /// other uses are possible. |
| 243 ICE_TLS_DECLARE_FIELD(const Cfg *, CurrentCfg); | 243 ICE_TLS_DECLARE_FIELD(const Cfg *, CurrentCfg); |
| 244 | 244 |
| 245 public: | 245 public: |
| 246 static void TlsInit() { ICE_TLS_INIT_FIELD(CurrentCfg); } | 246 static void TlsInit() { ICE_TLS_INIT_FIELD(CurrentCfg); } |
| 247 }; | 247 }; |
| 248 | 248 |
| 249 } // end of namespace Ice | 249 } // end of namespace Ice |
| 250 | 250 |
| 251 #endif // SUBZERO_SRC_ICECFG_H | 251 #endif // SUBZERO_SRC_ICECFG_H |
| OLD | NEW |