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