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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file declares the Liveness and LivenessNode classes, |
| 11 | // which are used for liveness analysis. The node-specific |
| 12 | // information tracked for each Variable includes whether it is |
| 13 | // live on entry, whether it is live on exit, the instruction number |
| 14 | // that starts its live range, and the instruction number that ends |
| 15 | // its live range. At the Cfg level, the actual live intervals are |
| 16 | // recorded. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #ifndef SUBZERO_SRC_ICELIVENESS_H |
| 21 | #define SUBZERO_SRC_ICELIVENESS_H |
| 22 | |
| 23 | #include "IceDefs.h" |
| 24 | #include "IceTypes.h" |
| 25 | |
| 26 | namespace Ice { |
| 27 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 28 | class Liveness { |
Jim Stichnoth | c6ead20 | 2015-02-24 09:30:30 -0800 | [diff] [blame] | 29 | Liveness() = delete; |
Jim Stichnoth | 5ce0abb | 2014-10-15 10:16:54 -0700 | [diff] [blame] | 30 | Liveness(const Liveness &) = delete; |
| 31 | Liveness &operator=(const Liveness &) = delete; |
| 32 | |
Jim Stichnoth | 7e57136 | 2015-01-09 11:43:26 -0800 | [diff] [blame] | 33 | class LivenessNode { |
| 34 | LivenessNode &operator=(const LivenessNode &) = delete; |
| 35 | |
| 36 | public: |
| 37 | LivenessNode() : NumLocals(0), NumNonDeadPhis(0) {} |
| 38 | LivenessNode(const LivenessNode &) = default; |
| 39 | // NumLocals is the number of Variables local to this block. |
| 40 | SizeT NumLocals; |
| 41 | // NumNonDeadPhis tracks the number of Phi instructions that |
| 42 | // Inst::liveness() identified as tentatively live. If |
| 43 | // NumNonDeadPhis changes from the last liveness pass, then liveness |
| 44 | // has not yet converged. |
| 45 | SizeT NumNonDeadPhis; |
| 46 | // LiveToVarMap maps a liveness bitvector index to a Variable. This |
| 47 | // is generally just for printing/dumping. The index should be less |
| 48 | // than NumLocals + Liveness::NumGlobals. |
| 49 | std::vector<Variable *> LiveToVarMap; |
| 50 | // LiveIn and LiveOut track the in- and out-liveness of the global |
| 51 | // variables. The size of each vector is |
| 52 | // LivenessNode::NumGlobals. |
| 53 | LivenessBV LiveIn, LiveOut; |
| 54 | // LiveBegin and LiveEnd track the instruction numbers of the start |
| 55 | // and end of each variable's live range within this block. The |
| 56 | // index/key of each element is less than NumLocals + |
| 57 | // Liveness::NumGlobals. |
| 58 | LiveBeginEndMap LiveBegin, LiveEnd; |
| 59 | }; |
| 60 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 61 | public: |
| 62 | Liveness(Cfg *Func, LivenessMode Mode) |
| 63 | : Func(Func), Mode(Mode), NumGlobals(0) {} |
| 64 | void init(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 65 | Cfg *getFunc() const { return Func; } |
| 66 | LivenessMode getMode() const { return Mode; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 67 | Variable *getVariable(SizeT LiveIndex, const CfgNode *Node) const; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 68 | SizeT getLiveIndex(SizeT VarIndex) const { return VarToLiveMap[VarIndex]; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 69 | SizeT getNumGlobalVars() const { return NumGlobals; } |
| 70 | SizeT getNumVarsInNode(const CfgNode *Node) const { |
| 71 | return NumGlobals + Nodes[Node->getIndex()].NumLocals; |
| 72 | } |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 73 | SizeT &getNumNonDeadPhis(const CfgNode *Node) { |
| 74 | return Nodes[Node->getIndex()].NumNonDeadPhis; |
| 75 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 76 | LivenessBV &getLiveIn(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 77 | SizeT Index = Node->getIndex(); |
| 78 | resize(Index); |
| 79 | return Nodes[Index].LiveIn; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 80 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 81 | LivenessBV &getLiveOut(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 82 | SizeT Index = Node->getIndex(); |
| 83 | resize(Index); |
| 84 | return Nodes[Index].LiveOut; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 85 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 86 | LiveBeginEndMap *getLiveBegin(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 87 | SizeT Index = Node->getIndex(); |
| 88 | resize(Index); |
| 89 | return &Nodes[Index].LiveBegin; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 90 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 91 | LiveBeginEndMap *getLiveEnd(const CfgNode *Node) { |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 92 | SizeT Index = Node->getIndex(); |
| 93 | resize(Index); |
| 94 | return &Nodes[Index].LiveEnd; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 95 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 96 | |
| 97 | private: |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 98 | // Resize Nodes so that Nodes[Index] is valid. |
| 99 | void resize(SizeT Index) { |
| 100 | if (Index >= Nodes.size()) |
| 101 | Nodes.resize(Index + 1); |
| 102 | } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 103 | Cfg *Func; |
| 104 | LivenessMode Mode; |
| 105 | SizeT NumGlobals; |
| 106 | // Size of Nodes is Cfg::Nodes.size(). |
| 107 | std::vector<LivenessNode> Nodes; |
| 108 | // VarToLiveMap maps a Variable's Variable::Number to its live index |
| 109 | // within its basic block. |
| 110 | std::vector<SizeT> VarToLiveMap; |
| 111 | // LiveToVarMap is analogous to LivenessNode::LiveToVarMap, but for |
| 112 | // non-local variables. |
| 113 | std::vector<Variable *> LiveToVarMap; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // end of namespace Ice |
| 117 | |
| 118 | #endif // SUBZERO_SRC_ICELIVENESS_H |