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