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