blob: 6af0ef05257f8caea0f14483a28f723d0af97e89 [file] [log] [blame]
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001//===- 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
26namespace Ice {
27
Jim Stichnothd97c7df2014-06-04 11:57:08 -070028class Liveness {
Jim Stichnothc6ead202015-02-24 09:30:30 -080029 Liveness() = delete;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -070030 Liveness(const Liveness &) = delete;
31 Liveness &operator=(const Liveness &) = delete;
32
Jim Stichnoth7e571362015-01-09 11:43:26 -080033 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 Stichnothd97c7df2014-06-04 11:57:08 -070061public:
62 Liveness(Cfg *Func, LivenessMode Mode)
63 : Func(Func), Mode(Mode), NumGlobals(0) {}
64 void init();
Jim Stichnoth47752552014-10-13 17:15:08 -070065 Cfg *getFunc() const { return Func; }
66 LivenessMode getMode() const { return Mode; }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070067 Variable *getVariable(SizeT LiveIndex, const CfgNode *Node) const;
Jim Stichnoth47752552014-10-13 17:15:08 -070068 SizeT getLiveIndex(SizeT VarIndex) const { return VarToLiveMap[VarIndex]; }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070069 SizeT getNumGlobalVars() const { return NumGlobals; }
70 SizeT getNumVarsInNode(const CfgNode *Node) const {
71 return NumGlobals + Nodes[Node->getIndex()].NumLocals;
72 }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070073 SizeT &getNumNonDeadPhis(const CfgNode *Node) {
74 return Nodes[Node->getIndex()].NumNonDeadPhis;
75 }
Jim Stichnoth47752552014-10-13 17:15:08 -070076 LivenessBV &getLiveIn(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070077 SizeT Index = Node->getIndex();
78 resize(Index);
79 return Nodes[Index].LiveIn;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070080 }
Jim Stichnoth47752552014-10-13 17:15:08 -070081 LivenessBV &getLiveOut(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070082 SizeT Index = Node->getIndex();
83 resize(Index);
84 return Nodes[Index].LiveOut;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070085 }
Jim Stichnoth47752552014-10-13 17:15:08 -070086 LiveBeginEndMap *getLiveBegin(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070087 SizeT Index = Node->getIndex();
88 resize(Index);
89 return &Nodes[Index].LiveBegin;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070090 }
Jim Stichnoth47752552014-10-13 17:15:08 -070091 LiveBeginEndMap *getLiveEnd(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070092 SizeT Index = Node->getIndex();
93 resize(Index);
94 return &Nodes[Index].LiveEnd;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070095 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070096
97private:
Jim Stichnoth336f6c42014-10-30 15:01:31 -070098 // 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 Stichnothd97c7df2014-06-04 11:57:08 -0700103 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 Stichnothd97c7df2014-06-04 11:57:08 -0700114};
115
116} // end of namespace Ice
117
118#endif // SUBZERO_SRC_ICELIVENESS_H