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