Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1 | //===- subzero/src/IceLiveness.h - Liveness analysis ------------*- 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 Liveness and LivenessNode classes, which are used for |
| 12 | /// liveness analysis. |
| 13 | /// |
| 14 | /// The node-specific information tracked for each Variable includes whether it |
| 15 | /// is live on entry, whether it is live on exit, the instruction number that |
| 16 | /// starts its live range, and the instruction number that ends its live range. |
| 17 | /// At the Cfg level, the actual live intervals are recorded. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 18 | /// |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #ifndef SUBZERO_SRC_ICELIVENESS_H |
| 22 | #define SUBZERO_SRC_ICELIVENESS_H |
| 23 | |
| 24 | #include "IceDefs.h" |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 25 | #include "IceBitVector.h" |
| 26 | #include "IceCfgNode.h" |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 27 | #include "IceTLS.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 28 | #include "IceTypes.h" |
| 29 | |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 30 | #include <memory> |
| 31 | #include <utility> |
| 32 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 33 | namespace Ice { |
| 34 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 35 | class Liveness { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 36 | Liveness() = delete; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 37 | Liveness(const Liveness &) = delete; |
| 38 | Liveness &operator=(const Liveness &) = delete; |
| 39 | |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 40 | class LivenessNode { |
| 41 | LivenessNode &operator=(const LivenessNode &) = delete; |
| 42 | |
| 43 | public: |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 44 | LivenessNode() = default; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 45 | LivenessNode(const LivenessNode &) = default; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 46 | /// NumLocals is the number of Variables local to this block. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 47 | SizeT NumLocals = 0; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 48 | /// NumNonDeadPhis tracks the number of Phi instructions that |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 49 | /// Inst::liveness() identified as tentatively live. If NumNonDeadPhis |
| 50 | /// changes from the last liveness pass, then liveness has not yet |
| 51 | /// converged. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 52 | SizeT NumNonDeadPhis = 0; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 53 | // LiveToVarMap maps a liveness bitvector index to a Variable. This is |
| 54 | // generally just for printing/dumping. The index should be less than |
| 55 | // NumLocals + Liveness::NumGlobals. |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 56 | LivenessVector<Variable *> LiveToVarMap; |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 57 | // LiveIn and LiveOut track the in- and out-liveness of the global |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 58 | // variables. The size of each vector is LivenessNode::NumGlobals. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 59 | LivenessBV LiveIn, LiveOut; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 60 | // LiveBegin and LiveEnd track the instruction numbers of the start and end |
| 61 | // of each variable's live range within this block. The index/key of each |
| 62 | // element is less than NumLocals + Liveness::NumGlobals. |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 63 | LiveBeginEndMap LiveBegin, LiveEnd; |
| 64 | }; |
| 65 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 66 | public: |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 67 | void init(); |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 68 | void initPhiEdgeSplits(NodeList::const_iterator FirstNode, |
| 69 | VarList::const_iterator FirstVar); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 70 | Cfg *getFunc() const { return Func; } |
| 71 | LivenessMode getMode() const { return Mode; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 72 | Variable *getVariable(SizeT LiveIndex, const CfgNode *Node) const; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 73 | SizeT getLiveIndex(SizeT VarIndex) const { |
| 74 | const SizeT LiveIndex = VarToLiveMap[VarIndex]; |
| 75 | assert(LiveIndex != InvalidLiveIndex); |
| 76 | return LiveIndex; |
| 77 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 78 | SizeT getNumGlobalVars() const { return NumGlobals; } |
| 79 | SizeT getNumVarsInNode(const CfgNode *Node) const { |
| 80 | return NumGlobals + Nodes[Node->getIndex()].NumLocals; |
| 81 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 82 | SizeT &getNumNonDeadPhis(const CfgNode *Node) { |
| 83 | return Nodes[Node->getIndex()].NumNonDeadPhis; |
| 84 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 85 | LivenessBV &getLiveIn(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 86 | SizeT Index = Node->getIndex(); |
| 87 | resize(Index); |
| 88 | return Nodes[Index].LiveIn; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 89 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 90 | LivenessBV &getLiveOut(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 91 | SizeT Index = Node->getIndex(); |
| 92 | resize(Index); |
| 93 | return Nodes[Index].LiveOut; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 94 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 95 | LivenessBV &getScratchBV() { return ScratchBV; } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 96 | LiveBeginEndMap *getLiveBegin(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 97 | SizeT Index = Node->getIndex(); |
| 98 | resize(Index); |
| 99 | return &Nodes[Index].LiveBegin; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 100 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 101 | LiveBeginEndMap *getLiveEnd(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 102 | SizeT Index = Node->getIndex(); |
| 103 | resize(Index); |
| 104 | return &Nodes[Index].LiveEnd; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 105 | } |
Jim Stichnoth | 552490c | 2015-08-05 16:21:42 -0700 | [diff] [blame] | 106 | bool getRangeMask(SizeT Index) const { return RangeMask[Index]; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 107 | |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 108 | ArenaAllocator *getAllocator() const { return Alloc.get(); } |
| 109 | |
| 110 | static std::unique_ptr<Liveness> create(Cfg *Func, LivenessMode Mode) { |
| 111 | return std::unique_ptr<Liveness>(new Liveness(Func, Mode)); |
| 112 | } |
| 113 | |
| 114 | static void TlsInit() { LivenessAllocatorTraits::init(); } |
| 115 | |
| 116 | std::string dumpStr() const { |
| 117 | return "MaxLocals(" + std::to_string(MaxLocals) + "), " |
| 118 | "NumGlobals(" + |
| 119 | std::to_string(NumGlobals) + ")"; |
| 120 | } |
| 121 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 122 | private: |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 123 | Liveness(Cfg *Func, LivenessMode Mode) |
| 124 | : Alloc(new ArenaAllocator()), AllocScope(this), Func(Func), Mode(Mode) {} |
| 125 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 126 | void initInternal(NodeList::const_iterator FirstNode, |
| 127 | VarList::const_iterator FirstVar, bool IsFullInit); |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 128 | /// Resize Nodes so that Nodes[Index] is valid. |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 129 | void resize(SizeT Index) { |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 130 | if (Index >= Nodes.size()) { |
| 131 | assert(false && "The Nodes array is not expected to be resized."); |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 132 | Nodes.resize(Index + 1); |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 133 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 134 | } |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 135 | std::unique_ptr<ArenaAllocator> Alloc; |
| 136 | LivenessAllocatorScope AllocScope; // Must be declared after Alloc. |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 137 | static constexpr SizeT InvalidLiveIndex = -1; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 138 | Cfg *Func; |
| 139 | LivenessMode Mode; |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 140 | /// Size of Nodes is Cfg::Nodes.size(). |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 141 | LivenessVector<LivenessNode> Nodes; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 142 | /// VarToLiveMap maps a Variable's Variable::Number to its live index within |
| 143 | /// its basic block. |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 144 | LivenessVector<SizeT> VarToLiveMap; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 145 | /// LiveToVarMap is analogous to LivenessNode::LiveToVarMap, but for non-local |
| 146 | /// variables. |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 147 | LivenessVector<Variable *> LiveToVarMap; |
Jim Stichnoth | 552490c | 2015-08-05 16:21:42 -0700 | [diff] [blame] | 148 | /// RangeMask[Variable::Number] indicates whether we want to track that |
| 149 | /// Variable's live range. |
John Porto | 36d6aa6 | 2016-02-26 07:19:59 -0800 | [diff] [blame] | 150 | LivenessBV RangeMask; |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 151 | /// ScratchBV is a bitvector that can be reused across CfgNode passes, to |
| 152 | /// avoid having to allocate/deallocate memory so frequently. |
| 153 | LivenessBV ScratchBV; |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 154 | /// MaxLocals indicates what is the maximum number of local variables in a |
| 155 | /// single basic block, across all blocks in a function. |
| 156 | SizeT MaxLocals = 0; |
| 157 | /// NumGlobals indicates how many global variables (i.e., Multi Block) exist |
| 158 | /// for a function. |
| 159 | SizeT NumGlobals = 0; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | } // end of namespace Ice |
| 163 | |
| 164 | #endif // SUBZERO_SRC_ICELIVENESS_H |