blob: a1de893644c2814059079e3f856a04c6426b8389 [file] [log] [blame]
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001//===- subzero/src/IceLiveness.cpp - Liveness analysis implementation -----===//
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 Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
11/// This file provides some of the support for the Liveness class. In
12/// particular, it handles the sparsity representation of the mapping
13/// between Variables and CfgNodes. The idea is that since most
14/// variables are used only within a single basic block, we can
15/// partition the variables into "local" and "global" sets. Instead of
16/// sizing and indexing vectors according to Variable::Number, we
17/// create a mapping such that global variables are mapped to low
18/// indexes that are common across nodes, and local variables are
19/// mapped to a higher index space that is shared across nodes.
20///
Jim Stichnothd97c7df2014-06-04 11:57:08 -070021//===----------------------------------------------------------------------===//
22
John Porto67f8de92015-06-25 10:14:17 -070023#include "IceLiveness.h"
24
Jim Stichnothd97c7df2014-06-04 11:57:08 -070025#include "IceCfg.h"
26#include "IceCfgNode.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070027#include "IceDefs.h"
Jim Stichnothd97c7df2014-06-04 11:57:08 -070028#include "IceInst.h"
Jim Stichnothd97c7df2014-06-04 11:57:08 -070029#include "IceOperand.h"
30
31namespace Ice {
32
Jim Stichnotha3f57b92015-07-30 12:46:04 -070033// Initializes the basic liveness-related data structures for full liveness
34// analysis (IsFullInit=true), or for incremental update after phi lowering
35// (IsFullInit=false). In the latter case, FirstNode points to the first node
36// added since starting phi lowering, and FirstVar points to the first Variable
37// added since starting phi lowering.
38void Liveness::initInternal(NodeList::const_iterator FirstNode,
39 VarList::const_iterator FirstVar, bool IsFullInit) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070040 // Initialize most of the container sizes.
41 SizeT NumVars = Func->getVariables().size();
42 SizeT NumNodes = Func->getNumNodes();
Jim Stichnoth47752552014-10-13 17:15:08 -070043 VariablesMetadata *VMetadata = Func->getVMetadata();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070044 Nodes.resize(NumNodes);
45 VarToLiveMap.resize(NumVars);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070046
Jim Stichnotha3f57b92015-07-30 12:46:04 -070047 // Count the number of globals, and the number of locals for each block.
48 SizeT TmpNumGlobals = 0;
49 for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) {
50 Variable *Var = *I;
Jim Stichnoth47752552014-10-13 17:15:08 -070051 if (VMetadata->isMultiBlock(Var)) {
Jim Stichnotha3f57b92015-07-30 12:46:04 -070052 ++TmpNumGlobals;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070053 } else {
Jim Stichnoth47752552014-10-13 17:15:08 -070054 SizeT Index = VMetadata->getLocalUseNode(Var)->getIndex();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070055 ++Nodes[Index].NumLocals;
56 }
57 }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070058 if (IsFullInit)
59 NumGlobals = TmpNumGlobals;
60 else
61 assert(TmpNumGlobals == 0);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070062
Jim Stichnotha3f57b92015-07-30 12:46:04 -070063 // Resize each LivenessNode::LiveToVarMap, and the global LiveToVarMap. Reset
64 // the counts to 0.
65 for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) {
66 LivenessNode &N = Nodes[(*I)->getIndex()];
67 N.LiveToVarMap.assign(N.NumLocals, nullptr);
68 N.NumLocals = 0;
69 N.NumNonDeadPhis = 0;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070070 }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070071 if (IsFullInit)
72 LiveToVarMap.assign(NumGlobals, nullptr);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070073
74 // Sort each variable into the appropriate LiveToVarMap. Also set
75 // VarToLiveMap.
Jim Stichnotha3f57b92015-07-30 12:46:04 -070076 TmpNumGlobals = 0;
77 for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) {
78 Variable *Var = *I;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070079 SizeT VarIndex = Var->getIndex();
80 SizeT LiveIndex;
Jim Stichnoth47752552014-10-13 17:15:08 -070081 if (VMetadata->isMultiBlock(Var)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070082 LiveIndex = TmpNumGlobals++;
83 LiveToVarMap[LiveIndex] = Var;
84 } else {
Jim Stichnoth47752552014-10-13 17:15:08 -070085 SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070086 LiveIndex = Nodes[NodeIndex].NumLocals++;
87 Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var;
88 LiveIndex += NumGlobals;
89 }
90 VarToLiveMap[VarIndex] = LiveIndex;
91 }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070092 assert(TmpNumGlobals == (IsFullInit ? NumGlobals : 0));
Jim Stichnothd97c7df2014-06-04 11:57:08 -070093
94 // Process each node.
Jim Stichnotha3f57b92015-07-30 12:46:04 -070095 for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) {
96 LivenessNode &Node = Nodes[(*I)->getIndex()];
Jim Stichnothd97c7df2014-06-04 11:57:08 -070097 // NumLocals, LiveToVarMap already initialized
98 Node.LiveIn.resize(NumGlobals);
99 Node.LiveOut.resize(NumGlobals);
100 // LiveBegin and LiveEnd are reinitialized before each pass over
101 // the block.
102 }
Jim Stichnoth552490c2015-08-05 16:21:42 -0700103
104 // Initialize the bitmask of which variables to track.
105 RangeMask.resize(NumVars);
106 RangeMask.set(0, NumVars);
107 if (!IsFullInit) {
108 // Reset initial variables that are not pre-colored or infinite-weight.
109 for (auto I = Func->getVariables().begin(); I != FirstVar; ++I) {
110 Variable *Var = *I;
111 RangeMask[Var->getIndex()] = (Var->hasReg() || Var->getWeight().isInf());
112 }
113 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700114}
115
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700116void Liveness::init() {
117 constexpr bool IsFullInit = true;
118 NodeList::const_iterator FirstNode = Func->getNodes().begin();
119 VarList::const_iterator FirstVar = Func->getVariables().begin();
120 initInternal(FirstNode, FirstVar, IsFullInit);
121}
122
123void Liveness::initPhiEdgeSplits(NodeList::const_iterator FirstNode,
124 VarList::const_iterator FirstVar) {
125 constexpr bool IsFullInit = false;
126 initInternal(FirstNode, FirstVar, IsFullInit);
127}
128
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700129Variable *Liveness::getVariable(SizeT LiveIndex, const CfgNode *Node) const {
130 if (LiveIndex < NumGlobals)
131 return LiveToVarMap[LiveIndex];
132 SizeT NodeIndex = Node->getIndex();
133 return Nodes[NodeIndex].LiveToVarMap[LiveIndex - NumGlobals];
134}
135
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700136} // end of namespace Ice