blob: 2949d568d68964f67793512cc6ba06a9b09f0499 [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
28class LivenessNode {
29public:
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 Stichnoth47752552014-10-13 17:15:08 -070040 LivenessBV LiveIn, LiveOut;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070041 // LiveBegin and LiveEnd track the instruction numbers of the start
42 // and end of each variable's live range within this block. The
Jim Stichnoth47752552014-10-13 17:15:08 -070043 // index/key of each element is less than NumLocals +
44 // Liveness::NumGlobals.
45 LiveBeginEndMap LiveBegin, LiveEnd;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070046
47private:
48 // TODO: Disable these constructors when Liveness::Nodes is no
49 // longer an STL container.
Jim Stichnoth0795ba02014-10-01 14:23:01 -070050 // LivenessNode(const LivenessNode &) = delete;
51 // LivenessNode &operator=(const LivenessNode &) = delete;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070052};
53
54class Liveness {
55public:
56 Liveness(Cfg *Func, LivenessMode Mode)
57 : Func(Func), Mode(Mode), NumGlobals(0) {}
58 void init();
Jim Stichnoth47752552014-10-13 17:15:08 -070059 Cfg *getFunc() const { return Func; }
60 LivenessMode getMode() const { return Mode; }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070061 Variable *getVariable(SizeT LiveIndex, const CfgNode *Node) const;
Jim Stichnoth47752552014-10-13 17:15:08 -070062 SizeT getLiveIndex(SizeT VarIndex) const { return VarToLiveMap[VarIndex]; }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070063 SizeT getNumGlobalVars() const { return NumGlobals; }
64 SizeT getNumVarsInNode(const CfgNode *Node) const {
65 return NumGlobals + Nodes[Node->getIndex()].NumLocals;
66 }
Jim Stichnoth47752552014-10-13 17:15:08 -070067 LivenessBV &getLiveIn(const CfgNode *Node) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070068 return Nodes[Node->getIndex()].LiveIn;
69 }
Jim Stichnoth47752552014-10-13 17:15:08 -070070 LivenessBV &getLiveOut(const CfgNode *Node) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070071 return Nodes[Node->getIndex()].LiveOut;
72 }
Jim Stichnoth47752552014-10-13 17:15:08 -070073 LiveBeginEndMap *getLiveBegin(const CfgNode *Node) {
74 return &Nodes[Node->getIndex()].LiveBegin;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070075 }
Jim Stichnoth47752552014-10-13 17:15:08 -070076 LiveBeginEndMap *getLiveEnd(const CfgNode *Node) {
77 return &Nodes[Node->getIndex()].LiveEnd;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070078 }
79 LiveRange &getLiveRange(Variable *Var);
80 void addLiveRange(Variable *Var, InstNumberT Start, InstNumberT End,
81 uint32_t WeightDelta);
82
83private:
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 Stichnoth0795ba02014-10-01 14:23:01 -070097 Liveness(const Liveness &) = delete;
98 Liveness &operator=(const Liveness &) = delete;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070099};
100
101} // end of namespace Ice
102
103#endif // SUBZERO_SRC_ICELIVENESS_H