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 | |
| 28 | class LivenessNode { |
| 29 | public: |
| 30 | LivenessNode() : NumLocals(0) {} |
| 31 | // NumLocals is the number of Variables local to this block. |
| 32 | SizeT NumLocals; |
| 33 | // LiveToVarMap maps a liveness bitvector index to a Variable. This |
| 34 | // is generally just for printing/dumping. The index should be less |
| 35 | // than NumLocals + Liveness::NumGlobals. |
| 36 | std::vector<Variable *> LiveToVarMap; |
| 37 | // LiveIn and LiveOut track the in- and out-liveness of the global |
| 38 | // variables. The size of each vector is |
| 39 | // LivenessNode::NumGlobals. |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 40 | LivenessBV LiveIn, LiveOut; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 41 | // LiveBegin and LiveEnd track the instruction numbers of the start |
| 42 | // and end of each variable's live range within this block. The |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 43 | // index/key of each element is less than NumLocals + |
| 44 | // Liveness::NumGlobals. |
| 45 | LiveBeginEndMap LiveBegin, LiveEnd; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | // TODO: Disable these constructors when Liveness::Nodes is no |
| 49 | // longer an STL container. |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 50 | // LivenessNode(const LivenessNode &) = delete; |
| 51 | // LivenessNode &operator=(const LivenessNode &) = delete; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | class Liveness { |
| 55 | public: |
| 56 | Liveness(Cfg *Func, LivenessMode Mode) |
| 57 | : Func(Func), Mode(Mode), NumGlobals(0) {} |
| 58 | void init(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 59 | Cfg *getFunc() const { return Func; } |
| 60 | LivenessMode getMode() const { return Mode; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 61 | Variable *getVariable(SizeT LiveIndex, const CfgNode *Node) const; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 62 | SizeT getLiveIndex(SizeT VarIndex) const { return VarToLiveMap[VarIndex]; } |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 63 | SizeT getNumGlobalVars() const { return NumGlobals; } |
| 64 | SizeT getNumVarsInNode(const CfgNode *Node) const { |
| 65 | return NumGlobals + Nodes[Node->getIndex()].NumLocals; |
| 66 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 67 | LivenessBV &getLiveIn(const CfgNode *Node) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 68 | return Nodes[Node->getIndex()].LiveIn; |
| 69 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 70 | LivenessBV &getLiveOut(const CfgNode *Node) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 71 | return Nodes[Node->getIndex()].LiveOut; |
| 72 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 73 | LiveBeginEndMap *getLiveBegin(const CfgNode *Node) { |
| 74 | return &Nodes[Node->getIndex()].LiveBegin; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 75 | } |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame^] | 76 | LiveBeginEndMap *getLiveEnd(const CfgNode *Node) { |
| 77 | return &Nodes[Node->getIndex()].LiveEnd; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 78 | } |
| 79 | LiveRange &getLiveRange(Variable *Var); |
| 80 | void addLiveRange(Variable *Var, InstNumberT Start, InstNumberT End, |
| 81 | uint32_t WeightDelta); |
| 82 | |
| 83 | private: |
| 84 | Cfg *Func; |
| 85 | LivenessMode Mode; |
| 86 | SizeT NumGlobals; |
| 87 | // Size of Nodes is Cfg::Nodes.size(). |
| 88 | std::vector<LivenessNode> Nodes; |
| 89 | // VarToLiveMap maps a Variable's Variable::Number to its live index |
| 90 | // within its basic block. |
| 91 | std::vector<SizeT> VarToLiveMap; |
| 92 | // LiveToVarMap is analogous to LivenessNode::LiveToVarMap, but for |
| 93 | // non-local variables. |
| 94 | std::vector<Variable *> LiveToVarMap; |
| 95 | // LiveRanges maps a Variable::Number to its live range. |
| 96 | std::vector<LiveRange> LiveRanges; |
Jim Stichnoth | 0795ba0 | 2014-10-01 14:23:01 -0700 | [diff] [blame] | 97 | Liveness(const Liveness &) = delete; |
| 98 | Liveness &operator=(const Liveness &) = delete; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // end of namespace Ice |
| 102 | |
| 103 | #endif // SUBZERO_SRC_ICELIVENESS_H |