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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Declares the Cfg class, which represents the control flow graph and |
| 12 | /// the overall per-function compilation context. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 13 | /// |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef SUBZERO_SRC_ICECFG_H |
| 17 | #define SUBZERO_SRC_ICECFG_H |
| 18 | |
John Porto | aff4ccf | 2015-06-10 16:35:06 -0700 | [diff] [blame] | 19 | #include "IceAssembler.h" |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 20 | #include "IceClFlags.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 21 | #include "IceDefs.h" |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 22 | #include "IceGlobalContext.h" |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 23 | #include "IceStringPool.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 | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 29 | Cfg() = delete; |
Jim Stichnoth | 7b451a9 | 2014-10-15 14:39:23 -0700 | [diff] [blame] | 30 | Cfg(const Cfg &) = delete; |
| 31 | Cfg &operator=(const Cfg &) = delete; |
| 32 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 33 | public: |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 34 | ~Cfg(); |
| 35 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 36 | static std::unique_ptr<Cfg> create(GlobalContext *Ctx, |
| 37 | uint32_t SequenceNumber) { |
| 38 | return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber)); |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 39 | } |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 40 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 41 | GlobalContext *getContext() const { return Ctx; } |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 42 | uint32_t getSequenceNumber() const { return SequenceNumber; } |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 43 | OptLevel getOptLevel() const { return OptimizationLevel; } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 44 | |
Jim Stichnoth | a522965 | 2015-11-12 10:25:21 -0800 | [diff] [blame] | 45 | static constexpr VerboseMask defaultVerboseMask() { |
Jim Stichnoth | 9f9aa2c | 2016-03-07 08:25:24 -0800 | [diff] [blame] | 46 | return (IceV_NO_PER_PASS_DUMP_BEYOND << 1) - 1; |
Jim Stichnoth | a522965 | 2015-11-12 10:25:21 -0800 | [diff] [blame] | 47 | } |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 48 | /// Returns true if any of the specified options in the verbose mask are set. |
| 49 | /// If the argument is omitted, it checks if any verbose options at all are |
| 50 | /// set. |
Jim Stichnoth | a522965 | 2015-11-12 10:25:21 -0800 | [diff] [blame] | 51 | bool isVerbose(VerboseMask Mask = defaultVerboseMask()) const { |
| 52 | return VMask & Mask; |
| 53 | } |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 54 | void setVerbose(VerboseMask Mask) { VMask = Mask; } |
| 55 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 56 | /// \name Manage the name and return type of the function being translated. |
| 57 | /// @{ |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 58 | void setFunctionName(GlobalString Name) { FunctionName = Name; } |
| 59 | GlobalString getFunctionName() const { return FunctionName; } |
| 60 | std::string getFunctionNameAndSize() const; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 61 | void setReturnType(Type Ty) { ReturnType = Ty; } |
David Sehr | 0d9cf48 | 2015-11-16 17:00:38 -0800 | [diff] [blame] | 62 | Type getReturnType() const { return ReturnType; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 63 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 64 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 65 | /// \name Manage the "internal" attribute of the function. |
| 66 | /// @{ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 67 | void setInternal(bool Internal) { IsInternalLinkage = Internal; } |
| 68 | bool getInternal() const { return IsInternalLinkage; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 69 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 70 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 71 | /// \name Manage errors. |
| 72 | /// @{ |
| 73 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 74 | /// Translation error flagging. If support for some construct is known to be |
| 75 | /// missing, instead of an assertion failure, setError() should be called and |
| 76 | /// the error should be propagated back up. This way, we can gracefully fail |
| 77 | /// to translate and let a fallback translator handle the function. |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 78 | void setError(const std::string &Message); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 79 | bool hasError() const { return HasError; } |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 80 | std::string getError() const { return ErrorMessage; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 81 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 82 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 83 | /// \name Manage nodes (a.k.a. basic blocks, CfgNodes). |
| 84 | /// @{ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 85 | void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; } |
| 86 | CfgNode *getEntryNode() const { return Entry; } |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 87 | /// Create a node and append it to the end of the linearized list. The loop |
| 88 | /// nest depth of the new node may not be valid if it is created after |
| 89 | /// computeLoopNestDepth. |
Jim Stichnoth | 668a7a3 | 2014-12-10 15:32:25 -0800 | [diff] [blame] | 90 | CfgNode *makeNode(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 91 | SizeT getNumNodes() const { return Nodes.size(); } |
| 92 | const NodeList &getNodes() const { return Nodes; } |
Jim Stichnoth | e7dbc0b | 2015-09-15 10:09:24 -0700 | [diff] [blame] | 93 | /// Swap nodes of Cfg with given list of nodes. The number of nodes must |
| 94 | /// remain unchanged. |
Karl Schimpf | ac7d734 | 2015-08-06 12:55:23 -0700 | [diff] [blame] | 95 | void swapNodes(NodeList &NewNodes); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 96 | /// @} |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 97 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 98 | /// String pool for CfgNode::Name values. |
| 99 | StringPool *getNodeStrings() const { return NodeStrings.get(); } |
| 100 | /// String pool for Variable::Name values. |
| 101 | StringPool *getVarStrings() const { return VarStrings.get(); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 102 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 103 | /// \name Manage instruction numbering. |
| 104 | /// @{ |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 105 | InstNumberT newInstNumber() { return NextInstNumber++; } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 106 | InstNumberT getNextInstNumber() const { return NextInstNumber; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 107 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 108 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 109 | /// \name Manage Variables. |
| 110 | /// @{ |
| 111 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 112 | /// Create a new Variable with a particular type and an optional name. The |
| 113 | /// Node argument is the node where the variable is defined. |
Jan Voung | 28068ad | 2015-07-31 12:58:46 -0700 | [diff] [blame] | 114 | // TODO(jpp): untemplate this with separate methods: makeVariable, |
| 115 | // makeSpillVariable, and makeStackVariable. |
Jim Stichnoth | 9a04c07 | 2014-12-11 15:51:42 -0800 | [diff] [blame] | 116 | template <typename T = Variable> T *makeVariable(Type Ty) { |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 117 | SizeT Index = Variables.size(); |
Jim Stichnoth | 54f3d51 | 2015-12-11 09:53:00 -0800 | [diff] [blame] | 118 | auto *Var = T::create(this, Ty, Index); |
Jim Stichnoth | 800dab2 | 2014-09-20 12:25:02 -0700 | [diff] [blame] | 119 | Variables.push_back(Var); |
| 120 | return Var; |
| 121 | } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 122 | SizeT getNumVariables() const { return Variables.size(); } |
| 123 | const VarList &getVariables() const { return Variables; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 124 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 125 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 126 | /// \name Manage arguments to the function. |
| 127 | /// @{ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 128 | void addArg(Variable *Arg); |
| 129 | const VarList &getArgs() const { return Args; } |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 130 | VarList &getArgs() { return Args; } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 131 | void addImplicitArg(Variable *Arg); |
| 132 | const VarList &getImplicitArgs() const { return ImplicitArgs; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 133 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 134 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 135 | /// \name Manage the jump tables. |
| 136 | /// @{ |
| 137 | void addJumpTable(InstJumpTable *JumpTable) { |
| 138 | JumpTables.emplace_back(JumpTable); |
| 139 | } |
| 140 | /// @} |
| 141 | |
John Porto | dc61925 | 2016-02-10 15:57:16 -0800 | [diff] [blame] | 142 | /// \name Manage the Globals used by this function. |
| 143 | /// @{ |
| 144 | std::unique_ptr<VariableDeclarationList> getGlobalInits() { |
| 145 | return std::move(GlobalInits); |
| 146 | } |
| 147 | void addGlobal(VariableDeclaration *Global) { |
| 148 | if (GlobalInits == nullptr) { |
| 149 | GlobalInits.reset(new VariableDeclarationList); |
| 150 | } |
| 151 | GlobalInits->push_back(Global); |
| 152 | } |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 153 | VariableDeclarationList *getGlobalPool() { |
| 154 | if (GlobalInits == nullptr) { |
| 155 | GlobalInits.reset(new VariableDeclarationList); |
| 156 | } |
| 157 | return GlobalInits.get(); |
| 158 | } |
John Porto | dc61925 | 2016-02-10 15:57:16 -0800 | [diff] [blame] | 159 | /// @} |
| 160 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 161 | /// \name Miscellaneous accessors. |
| 162 | /// @{ |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 163 | TargetLowering *getTarget() const { return Target.get(); } |
Jim Stichnoth | 144cdce | 2014-09-22 16:02:59 -0700 | [diff] [blame] | 164 | VariablesMetadata *getVMetadata() const { return VMetadata.get(); } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 165 | Liveness *getLiveness() const { return Live.get(); } |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 166 | template <typename T = Assembler> T *getAssembler() const { |
John Porto | 2da710c | 2015-06-29 07:57:02 -0700 | [diff] [blame] | 167 | return llvm::dyn_cast<T>(TargetAssembler.get()); |
Jan Voung | 8acded0 | 2014-09-22 18:02:25 -0700 | [diff] [blame] | 168 | } |
John Porto | bd2e231 | 2016-03-15 11:06:25 -0700 | [diff] [blame] | 169 | std::unique_ptr<Assembler> releaseAssembler() { |
| 170 | return std::move(TargetAssembler); |
| 171 | } |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 172 | bool hasComputedFrame() const; |
Jim Stichnoth | 8363a06 | 2014-10-07 10:02:38 -0700 | [diff] [blame] | 173 | bool getFocusedTiming() const { return FocusedTiming; } |
| 174 | void setFocusedTiming() { FocusedTiming = true; } |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 175 | uint32_t getConstantBlindingCookie() const { return ConstantBlindingCookie; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 176 | /// @} |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 177 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 178 | /// Returns true if Var is a global variable that is used by the profiling |
| 179 | /// code. |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 180 | static bool isProfileGlobal(const VariableDeclaration &Var); |
| 181 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 182 | /// Passes over the CFG. |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 183 | void translate(); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 184 | /// After the CFG is fully constructed, iterate over the nodes and compute the |
| 185 | /// predecessor and successor edges, in the form of CfgNode::InEdges[] and |
| 186 | /// CfgNode::OutEdges[]. |
Jim Stichnoth | 69d3f9c | 2015-03-23 10:33:38 -0700 | [diff] [blame] | 187 | void computeInOutEdges(); |
Jim Stichnoth | 76719b4 | 2016-03-14 08:37:52 -0700 | [diff] [blame] | 188 | /// Renumber the non-deleted instructions in the Cfg. This needs to be done |
| 189 | /// in preparation for live range analysis. The instruction numbers in a |
| 190 | /// block must be monotonically increasing. The range of instruction numbers |
| 191 | /// in a block, from lowest to highest, must not overlap with the range of any |
| 192 | /// other block. |
| 193 | /// |
| 194 | /// Also, if the configuration specifies to do so, remove/unlink all deleted |
| 195 | /// instructions from the Cfg, to speed up later passes over the instructions. |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 196 | void renumberInstructions(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 197 | void placePhiLoads(); |
| 198 | void placePhiStores(); |
| 199 | void deletePhis(); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 200 | void advancedPhiLowering(); |
| 201 | void reorderNodes(); |
Qining Lu | 969f6a3 | 2015-07-31 09:58:34 -0700 | [diff] [blame] | 202 | void shuffleNodes(); |
Manasij Mukherjee | 032c315 | 2016-05-24 14:25:04 -0700 | [diff] [blame] | 203 | void localCSE(); |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 204 | |
David Sehr | 4318a41 | 2015-11-11 15:01:55 -0800 | [diff] [blame] | 205 | /// Scan allocas to determine whether we need to use a frame pointer. |
| 206 | /// If SortAndCombine == true, merge all the fixed-size allocas in the |
| 207 | /// entry block and emit stack or frame pointer-relative addressing. |
| 208 | void processAllocas(bool SortAndCombine); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 209 | void doAddressOpt(); |
John Porto | a47c11c | 2016-04-21 05:53:42 -0700 | [diff] [blame] | 210 | /// Find clusters of insertelement/extractelement instructions that can be |
| 211 | /// replaced by a shufflevector instruction. |
| 212 | void materializeVectorShuffles(); |
Matt Wala | 45a0623 | 2014-07-09 16:33:22 -0700 | [diff] [blame] | 213 | void doArgLowering(); |
Matt Wala | c330274 | 2014-08-15 16:21:56 -0700 | [diff] [blame] | 214 | void doNopInsertion(); |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 215 | void genCode(); |
| 216 | void genFrame(); |
Andrew Scull | aa6c109 | 2015-09-03 17:50:30 -0700 | [diff] [blame] | 217 | void computeLoopNestDepth(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 218 | void livenessLightweight(); |
| 219 | void liveness(LivenessMode Mode); |
| 220 | bool validateLiveness() const; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 221 | void contractEmptyNodes(); |
Jim Stichnoth | ff9c706 | 2014-09-18 04:50:49 -0700 | [diff] [blame] | 222 | void doBranchOpt(); |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 223 | void markNodesForSandboxing(); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 224 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 225 | /// \name Manage the CurrentNode field. |
| 226 | /// CurrentNode is used for validating the Variable::DefNode field during |
| 227 | /// dumping/emitting. |
| 228 | /// @{ |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 229 | void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 230 | void resetCurrentNode() { setCurrentNode(nullptr); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 231 | const CfgNode *getCurrentNode() const { return CurrentNode; } |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 232 | /// @} |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 233 | |
John Porto | a3984a1 | 2016-04-01 11:14:30 -0700 | [diff] [blame] | 234 | /// Get the total amount of memory held by the per-Cfg allocator. |
| 235 | size_t getTotalMemoryMB() const; |
| 236 | |
| 237 | /// Get the current memory usage due to liveness data structures. |
| 238 | size_t getLivenessMemoryMB() const; |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 239 | |
Jim Stichnoth | 5bc2b1d | 2014-05-22 13:38:48 -0700 | [diff] [blame] | 240 | void emit(); |
Jan Voung | 0faec4c | 2014-11-05 17:29:56 -0800 | [diff] [blame] | 241 | void emitIAS(); |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 242 | static void emitTextHeader(GlobalString Name, GlobalContext *Ctx, |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 243 | const Assembler *Asm); |
Jim Stichnoth | b88d8c8 | 2016-03-11 15:33:00 -0800 | [diff] [blame] | 244 | void dump(const char *Message = ""); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 245 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 246 | /// Allocate data of type T using the per-Cfg allocator. |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 247 | template <typename T> T *allocate() { return Allocator->Allocate<T>(); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 248 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 249 | /// Allocate an array of data of type T using the per-Cfg allocator. |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 250 | template <typename T> T *allocateArrayOf(size_t NumElems) { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 251 | return Allocator->Allocate<T>(NumElems); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 254 | /// Deallocate data that was allocated via allocate<T>(). |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 255 | template <typename T> void deallocate(T *Object) { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 256 | Allocator->Deallocate(Object); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 259 | /// Deallocate data that was allocated via allocateArrayOf<T>(). |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 260 | template <typename T> void deallocateArrayOf(T *Array) { |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 261 | Allocator->Deallocate(Array); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Eric Holk | 16f8061 | 2016-04-04 17:07:42 -0700 | [diff] [blame] | 264 | /// Update Phi labels with InEdges. |
| 265 | /// |
| 266 | /// The WASM translator cannot always determine the right incoming edge for a |
| 267 | /// value due to the CFG being built incrementally. The fixPhiNodes pass fills |
| 268 | /// in the correct information once everything is known. |
| 269 | void fixPhiNodes(); |
| 270 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 271 | private: |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 272 | friend class CfgAllocatorTraits; // for Allocator access. |
| 273 | |
Jim Stichnoth | bbca754 | 2015-02-11 16:08:31 -0800 | [diff] [blame] | 274 | Cfg(GlobalContext *Ctx, uint32_t SequenceNumber); |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 275 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 276 | /// Adds a call to the ProfileSummary runtime function as the first |
| 277 | /// instruction in this CFG's entry block. |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 278 | void addCallToProfileSummary(); |
| 279 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 280 | /// Iterates over the basic blocks in this CFG, adding profiling code to each |
| 281 | /// one of them. It returns a list with all the globals that the profiling |
| 282 | /// code needs to be defined. |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 283 | void profileBlocks(); |
| 284 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 285 | void createNodeNameDeclaration(const std::string &NodeAsmName); |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 286 | void |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 287 | createBlockProfilingInfoDeclaration(const std::string &NodeAsmName, |
John Porto | a78e4ba | 2016-03-15 09:28:04 -0700 | [diff] [blame] | 288 | VariableDeclaration *NodeNameDeclaration); |
| 289 | |
Andrew Scull | 86df4e9 | 2015-07-30 13:54:44 -0700 | [diff] [blame] | 290 | /// Iterate through the registered jump tables and emit them. |
| 291 | void emitJumpTables(); |
| 292 | |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 293 | enum AllocaBaseVariableType { |
| 294 | BVT_StackPointer, |
| 295 | BVT_FramePointer, |
| 296 | BVT_UserPointer |
| 297 | }; |
Thomas Lively | 1fd80c7 | 2016-06-27 14:47:21 -0700 | [diff] [blame^] | 298 | void sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas, |
Jim Stichnoth | 3607b6c | 2015-11-13 14:28:23 -0800 | [diff] [blame] | 299 | uint32_t CombinedAlignment, InstList &Insts, |
| 300 | AllocaBaseVariableType BaseVariableType); |
| 301 | void findRematerializable(); |
| 302 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 303 | GlobalContext *Ctx; |
Jim Stichnoth | dd6dcfa | 2016-04-18 12:52:09 -0700 | [diff] [blame] | 304 | uint32_t SequenceNumber; /// output order for emission |
| 305 | OptLevel OptimizationLevel = Opt_m1; |
Qining Lu | aee5fa8 | 2015-08-20 14:59:03 -0700 | [diff] [blame] | 306 | uint32_t ConstantBlindingCookie = 0; /// cookie for constant blinding |
Jim Stichnoth | fa4efea | 2015-01-27 05:06:03 -0800 | [diff] [blame] | 307 | VerboseMask VMask; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 308 | GlobalString FunctionName; |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 309 | Type ReturnType = IceType_void; |
| 310 | bool IsInternalLinkage = false; |
| 311 | bool HasError = false; |
| 312 | bool FocusedTiming = false; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 313 | std::string ErrorMessage = ""; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 314 | CfgNode *Entry = nullptr; /// entry basic block |
| 315 | NodeList Nodes; /// linearized node list; Entry should be first |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 316 | InstNumberT NextInstNumber; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 317 | VarList Variables; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 318 | VarList Args; /// subset of Variables, in argument order |
| 319 | VarList ImplicitArgs; /// subset of Variables |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 320 | std::unique_ptr<ArenaAllocator> Allocator; |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 321 | // Separate string pools for CfgNode and Variable names, due to a combination |
| 322 | // of the uniqueness requirement, and assumptions in lit tests. |
| 323 | std::unique_ptr<StringPool> NodeStrings; |
| 324 | std::unique_ptr<StringPool> VarStrings; |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 325 | std::unique_ptr<Liveness> Live; |
| 326 | std::unique_ptr<TargetLowering> Target; |
| 327 | std::unique_ptr<VariablesMetadata> VMetadata; |
| 328 | std::unique_ptr<Assembler> TargetAssembler; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 329 | /// Globals required by this CFG. Mostly used for the profiler's globals. |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 330 | std::unique_ptr<VariableDeclarationList> GlobalInits; |
Andrew Scull | 00741a0 | 2015-09-16 19:04:09 -0700 | [diff] [blame] | 331 | CfgVector<InstJumpTable *> JumpTables; |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 332 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 333 | /// CurrentNode is maintained during dumping/emitting just for validating |
| 334 | /// Variable::DefNode. Normally, a traversal over CfgNodes maintains this, but |
| 335 | /// before global operations like register allocation, resetCurrentNode() |
| 336 | /// should be called to avoid spurious validation failures. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 337 | const CfgNode *CurrentNode = nullptr; |
Jim Stichnoth | 31c9559 | 2014-12-19 12:51:35 -0800 | [diff] [blame] | 338 | |
Jim Stichnoth | a5fe17a | 2015-01-26 11:10:03 -0800 | [diff] [blame] | 339 | public: |
John Porto | e82b560 | 2016-02-24 15:58:55 -0800 | [diff] [blame] | 340 | static void TlsInit() { CfgAllocatorTraits::init(); } |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 341 | }; |
| 342 | |
Andrew Scull | 6d47bcd | 2015-09-17 17:10:05 -0700 | [diff] [blame] | 343 | template <> Variable *Cfg::makeVariable<Variable>(Type Ty); |
| 344 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 345 | struct NodeStringPoolTraits { |
| 346 | using OwnerType = Cfg; |
| 347 | static StringPool *getStrings(const OwnerType *PoolOwner) { |
| 348 | return PoolOwner->getNodeStrings(); |
| 349 | } |
| 350 | }; |
| 351 | using NodeString = StringID<NodeStringPoolTraits>; |
| 352 | |
| 353 | struct VariableStringPoolTraits { |
| 354 | using OwnerType = Cfg; |
| 355 | static StringPool *getStrings(const OwnerType *PoolOwner) { |
| 356 | return PoolOwner->getVarStrings(); |
| 357 | } |
| 358 | }; |
| 359 | using VariableString = StringID<VariableStringPoolTraits>; |
| 360 | |
Jim Stichnoth | f7c9a14 | 2014-04-29 10:52:43 -0700 | [diff] [blame] | 361 | } // end of namespace Ice |
| 362 | |
| 363 | #endif // SUBZERO_SRC_ICECFG_H |