Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceCfg.h - Control flow graph ----------------*- 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 Cfg class, which represents the control flow |
| 11 | // graph and the overall per-function compilation context. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef SUBZERO_SRC_ICECFG_H |
| 16 | #define SUBZERO_SRC_ICECFG_H |
| 17 | |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 18 | #include <memory> |
| 19 | |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 20 | #include "assembler.h" |
| 21 | #include "IceClFlags.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 22 | #include "IceDefs.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 23 | #include "IceGlobalContext.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 24 | #include "IceTypes.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 25 | |
| 26 | namespace Ice { |
| 27 | |
| 28 | class Cfg { |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 29 | Cfg(const Cfg &) = delete; |
| 30 | Cfg &operator=(const Cfg &) = delete; |
| 31 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 32 | public: |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 33 | ~Cfg(); |
| 34 | |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 35 | // TODO(stichnot): Change this to return unique_ptr<Cfg>, and plumb |
| 36 | // it through the callers, to make ownership and lifetime and |
| 37 | // destruction requirements more explicit. |
| 38 | static Cfg *create(GlobalContext *Ctx) { |
| 39 | Cfg *Func = new Cfg(Ctx); |
| 40 | CurrentCfg = Func; |
| 41 | return Func; |
| 42 | } |
| 43 | // Gets a pointer to the current thread's Cfg. |
| 44 | static const Cfg *getCurrentCfg() { return CurrentCfg; } |
| 45 | // Gets a pointer to the current thread's Cfg's allocator. |
Jan Voung | 1d62cf0 | 2015-01-09 14:57:32 -0800 | [diff] [blame] | 46 | static ArenaAllocator<> *getCurrentCfgAllocator() { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 47 | assert(CurrentCfg); |
| 48 | return CurrentCfg->Allocator.get(); |
| 49 | } |
| 50 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 51 | GlobalContext *getContext() const { return Ctx; } |
| 52 | |
| 53 | // Manage the name and return type of the function being translated. |
| 54 | void setFunctionName(const IceString &Name) { FunctionName = Name; } |
| 55 | IceString getFunctionName() const { return FunctionName; } |
| 56 | void setReturnType(Type Ty) { ReturnType = Ty; } |
| 57 | |
| 58 | // Manage the "internal" attribute of the function. |
| 59 | void setInternal(bool Internal) { IsInternalLinkage = Internal; } |
| 60 | bool getInternal() const { return IsInternalLinkage; } |
| 61 | |
| 62 | // Translation error flagging. If support for some construct is |
| 63 | // known to be missing, instead of an assertion failure, setError() |
| 64 | // should be called and the error should be propagated back up. |
| 65 | // This way, we can gracefully fail to translate and let a fallback |
| 66 | // translator handle the function. |
| 67 | void setError(const IceString &Message); |
| 68 | bool hasError() const { return HasError; } |
| 69 | IceString getError() const { return ErrorMessage; } |
| 70 | |
| 71 | // Manage nodes (a.k.a. basic blocks, CfgNodes). |
| 72 | void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; } |
| 73 | CfgNode *getEntryNode() const { return Entry; } |
| 74 | // Create a node and append it to the end of the linearized list. |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 75 | CfgNode *makeNode(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 76 | SizeT getNumNodes() const { return Nodes.size(); } |
| 77 | const NodeList &getNodes() const { return Nodes; } |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 78 | |
| 79 | typedef int32_t IdentifierIndexType; |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 80 | // Adds a name to the list and returns its index, suitable for the |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 81 | // argument to getIdentifierName(). No checking for duplicates is |
| 82 | // done. This is generally used for node names and variable names |
| 83 | // to avoid embedding a std::string inside an arena-allocated |
| 84 | // object. |
| 85 | IdentifierIndexType addIdentifierName(const IceString &Name) { |
| 86 | IdentifierIndexType Index = IdentifierNames.size(); |
| 87 | IdentifierNames.push_back(Name); |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 88 | return Index; |
| 89 | } |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 90 | const IceString &getIdentifierName(IdentifierIndexType Index) const { |
| 91 | return IdentifierNames[Index]; |
| 92 | } |
| 93 | enum { |
| 94 | IdentifierIndexInvalid = -1 |
| 95 | }; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 96 | |
| 97 | // Manage instruction numbering. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 98 | InstNumberT newInstNumber() { return NextInstNumber++; } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 99 | InstNumberT getNextInstNumber() const { return NextInstNumber; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 100 | |
| 101 | // Manage Variables. |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 102 | // Create a new Variable with a particular type and an optional |
| 103 | // name. The Node argument is the node where the variable is defined. |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 104 | template <typename T = Variable> T *makeVariable(Type Ty) { |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 105 | SizeT Index = Variables.size(); |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 106 | T *Var = T::create(this, Ty, Index); |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 107 | Variables.push_back(Var); |
| 108 | return Var; |
| 109 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 110 | SizeT getNumVariables() const { return Variables.size(); } |
| 111 | const VarList &getVariables() const { return Variables; } |
| 112 | |
| 113 | // Manage arguments to the function. |
| 114 | void addArg(Variable *Arg); |
| 115 | const VarList &getArgs() const { return Args; } |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 116 | VarList &getArgs() { return Args; } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 117 | void addImplicitArg(Variable *Arg); |
| 118 | const VarList &getImplicitArgs() const { return ImplicitArgs; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 119 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 120 | // Miscellaneous accessors. |
| 121 | TargetLowering *getTarget() const { return Target.get(); } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 122 | VariablesMetadata *getVMetadata() const { return VMetadata.get(); } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 123 | Liveness *getLiveness() const { return Live.get(); } |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 124 | template <typename T> T *getAssembler() const { |
| 125 | return static_cast<T *>(TargetAssembler.get()); |
| 126 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 127 | bool hasComputedFrame() const; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 128 | bool getFocusedTiming() const { return FocusedTiming; } |
| 129 | void setFocusedTiming() { FocusedTiming = true; } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 130 | |
| 131 | // Passes over the CFG. |
| 132 | void translate(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 133 | // After the CFG is fully constructed, iterate over the nodes and |
| 134 | // compute the predecessor edges, in the form of |
| 135 | // CfgNode::InEdges[]. |
| 136 | void computePredecessors(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 137 | void renumberInstructions(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 138 | void placePhiLoads(); |
| 139 | void placePhiStores(); |
| 140 | void deletePhis(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 141 | void advancedPhiLowering(); |
| 142 | void reorderNodes(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 143 | void doAddressOpt(); |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 144 | void doArgLowering(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 145 | void doNopInsertion(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 146 | void genCode(); |
| 147 | void genFrame(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 148 | void livenessLightweight(); |
| 149 | void liveness(LivenessMode Mode); |
| 150 | bool validateLiveness() const; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 151 | void contractEmptyNodes(); |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 152 | void doBranchOpt(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 153 | |
| 154 | // Manage the CurrentNode field, which is used for validating the |
| 155 | // Variable::DefNode field during dumping/emitting. |
| 156 | void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 157 | void resetCurrentNode() { setCurrentNode(nullptr); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 158 | const CfgNode *getCurrentNode() const { return CurrentNode; } |
| 159 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 160 | void emit(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 161 | void emitIAS(); |
| 162 | void emitTextHeader(const IceString &MangledName); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 163 | void dump(const IceString &Message = ""); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 164 | |
| 165 | // Allocate data of type T using the per-Cfg allocator. |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 166 | template <typename T> T *allocate() { return Allocator->Allocate<T>(); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 167 | |
| 168 | // Allocate an array of data of type T using the per-Cfg allocator. |
| 169 | template <typename T> T *allocateArrayOf(size_t NumElems) { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 170 | return Allocator->Allocate<T>(NumElems); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // Deallocate data that was allocated via allocate<T>(). |
| 174 | template <typename T> void deallocate(T *Object) { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 175 | Allocator->Deallocate(Object); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // Deallocate data that was allocated via allocateArrayOf<T>(). |
| 179 | template <typename T> void deallocateArrayOf(T *Array) { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 180 | Allocator->Deallocate(Array); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private: |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 184 | Cfg(GlobalContext *Ctx); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 185 | |
| 186 | GlobalContext *Ctx; |
| 187 | IceString FunctionName; |
| 188 | Type ReturnType; |
| 189 | bool IsInternalLinkage; |
| 190 | bool HasError; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 191 | bool FocusedTiming; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 192 | IceString ErrorMessage; |
| 193 | CfgNode *Entry; // entry basic block |
| 194 | NodeList Nodes; // linearized node list; Entry should be first |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 195 | std::vector<IceString> IdentifierNames; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 196 | InstNumberT NextInstNumber; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 197 | VarList Variables; |
| 198 | VarList Args; // subset of Variables, in argument order |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 199 | VarList ImplicitArgs; // subset of Variables |
Jan Voung | 1d62cf0 | 2015-01-09 14:57:32 -0800 | [diff] [blame] | 200 | std::unique_ptr<ArenaAllocator<>> Allocator; |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 201 | std::unique_ptr<Liveness> Live; |
| 202 | std::unique_ptr<TargetLowering> Target; |
| 203 | std::unique_ptr<VariablesMetadata> VMetadata; |
| 204 | std::unique_ptr<Assembler> TargetAssembler; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 205 | |
| 206 | // CurrentNode is maintained during dumping/emitting just for |
| 207 | // validating Variable::DefNode. Normally, a traversal over |
| 208 | // CfgNodes maintains this, but before global operations like |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 209 | // register allocation, resetCurrentNode() should be called to avoid |
| 210 | // spurious validation failures. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 211 | const CfgNode *CurrentNode; |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 212 | |
| 213 | // Maintain a pointer in TLS to the current Cfg being translated. |
| 214 | // This is primarily for accessing its allocator statelessly, but |
| 215 | // other uses are possible. |
| 216 | thread_local static const Cfg *CurrentCfg; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | } // end of namespace Ice |
| 220 | |
| 221 | #endif // SUBZERO_SRC_ICECFG_H |