blob: c287dc22a995b92ed72a5b7e42a92e07f0177d83 [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 Stichnoth5ce0abb2014-10-15 10:16:54 -070029 Liveness(const Liveness &) = delete;
30 Liveness &operator=(const Liveness &) = delete;
31
Jim Stichnoth7e571362015-01-09 11:43:26 -080032 class LivenessNode {
33 LivenessNode &operator=(const LivenessNode &) = delete;
34
35 public:
36 LivenessNode() : NumLocals(0), NumNonDeadPhis(0) {}
37 LivenessNode(const LivenessNode &) = default;
38 // NumLocals is the number of Variables local to this block.
39 SizeT NumLocals;
40 // NumNonDeadPhis tracks the number of Phi instructions that
41 // Inst::liveness() identified as tentatively live. If
42 // NumNonDeadPhis changes from the last liveness pass, then liveness
43 // has not yet converged.
44 SizeT NumNonDeadPhis;
45 // LiveToVarMap maps a liveness bitvector index to a Variable. This
46 // is generally just for printing/dumping. The index should be less
47 // than NumLocals + Liveness::NumGlobals.
48 std::vector<Variable *> LiveToVarMap;
49 // LiveIn and LiveOut track the in- and out-liveness of the global
50 // variables. The size of each vector is
51 // LivenessNode::NumGlobals.
52 LivenessBV LiveIn, LiveOut;
53 // LiveBegin and LiveEnd track the instruction numbers of the start
54 // and end of each variable's live range within this block. The
55 // index/key of each element is less than NumLocals +
56 // Liveness::NumGlobals.
57 LiveBeginEndMap LiveBegin, LiveEnd;
58 };
59
Jim Stichnothd97c7df2014-06-04 11:57:08 -070060public:
61 Liveness(Cfg *Func, LivenessMode Mode)
62 : Func(Func), Mode(Mode), NumGlobals(0) {}
63 void init();
Jim Stichnoth47752552014-10-13 17:15:08 -070064 Cfg *getFunc() const { return Func; }
65 LivenessMode getMode() const { return Mode; }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070066 Variable *getVariable(SizeT LiveIndex, const CfgNode *Node) const;
Jim Stichnoth47752552014-10-13 17:15:08 -070067 SizeT getLiveIndex(SizeT VarIndex) const { return VarToLiveMap[VarIndex]; }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070068 SizeT getNumGlobalVars() const { return NumGlobals; }
69 SizeT getNumVarsInNode(const CfgNode *Node) const {
70 return NumGlobals + Nodes[Node->getIndex()].NumLocals;
71 }
Jim Stichnoth336f6c42014-10-30 15:01:31 -070072 SizeT &getNumNonDeadPhis(const CfgNode *Node) {
73 return Nodes[Node->getIndex()].NumNonDeadPhis;
74 }
Jim Stichnoth47752552014-10-13 17:15:08 -070075 LivenessBV &getLiveIn(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070076 SizeT Index = Node->getIndex();
77 resize(Index);
78 return Nodes[Index].LiveIn;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070079 }
Jim Stichnoth47752552014-10-13 17:15:08 -070080 LivenessBV &getLiveOut(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070081 SizeT Index = Node->getIndex();
82 resize(Index);
83 return Nodes[Index].LiveOut;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070084 }
Jim Stichnoth47752552014-10-13 17:15:08 -070085 LiveBeginEndMap *getLiveBegin(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070086 SizeT Index = Node->getIndex();
87 resize(Index);
88 return &Nodes[Index].LiveBegin;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070089 }
Jim Stichnoth47752552014-10-13 17:15:08 -070090 LiveBeginEndMap *getLiveEnd(const CfgNode *Node) {
Jim Stichnoth336f6c42014-10-30 15:01:31 -070091 SizeT Index = Node->getIndex();
92 resize(Index);
93 return &Nodes[Index].LiveEnd;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070094 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070095
96private:
Jim Stichnoth336f6c42014-10-30 15:01:31 -070097 // Resize Nodes so that Nodes[Index] is valid.
98 void resize(SizeT Index) {
99 if (Index >= Nodes.size())
100 Nodes.resize(Index + 1);
101 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700102 Cfg *Func;
103 LivenessMode Mode;
104 SizeT NumGlobals;
105 // Size of Nodes is Cfg::Nodes.size().
106 std::vector<LivenessNode> Nodes;
107 // VarToLiveMap maps a Variable's Variable::Number to its live index
108 // within its basic block.
109 std::vector<SizeT> VarToLiveMap;
110 // LiveToVarMap is analogous to LivenessNode::LiveToVarMap, but for
111 // non-local variables.
112 std::vector<Variable *> LiveToVarMap;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700113};
114
115} // end of namespace Ice
116
117#endif // SUBZERO_SRC_ICELIVENESS_H