blob: 551a40067e5174c6af72542b9a3af5a332854fb3 [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
33void Liveness::init() {
34 // Initialize most of the container sizes.
35 SizeT NumVars = Func->getVariables().size();
36 SizeT NumNodes = Func->getNumNodes();
Jim Stichnoth47752552014-10-13 17:15:08 -070037 VariablesMetadata *VMetadata = Func->getVMetadata();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070038 Nodes.resize(NumNodes);
39 VarToLiveMap.resize(NumVars);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070040
41 // Count the number of globals, and the number of locals for each
42 // block.
43 for (SizeT i = 0; i < NumVars; ++i) {
44 Variable *Var = Func->getVariables()[i];
Jim Stichnoth47752552014-10-13 17:15:08 -070045 if (VMetadata->isMultiBlock(Var)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070046 ++NumGlobals;
47 } else {
Jim Stichnoth47752552014-10-13 17:15:08 -070048 SizeT Index = VMetadata->getLocalUseNode(Var)->getIndex();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070049 ++Nodes[Index].NumLocals;
50 }
51 }
52
53 // Resize each LivenessNode::LiveToVarMap, and the global
54 // LiveToVarMap. Reset the counts to 0.
55 for (SizeT i = 0; i < NumNodes; ++i) {
Jim Stichnothae953202014-12-20 06:17:49 -080056 Nodes[i].LiveToVarMap.assign(Nodes[i].NumLocals, nullptr);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070057 Nodes[i].NumLocals = 0;
Jim Stichnoth336f6c42014-10-30 15:01:31 -070058 Nodes[i].NumNonDeadPhis = 0;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070059 }
Jim Stichnothae953202014-12-20 06:17:49 -080060 LiveToVarMap.assign(NumGlobals, nullptr);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070061
62 // Sort each variable into the appropriate LiveToVarMap. Also set
63 // VarToLiveMap.
64 SizeT TmpNumGlobals = 0;
65 for (SizeT i = 0; i < NumVars; ++i) {
66 Variable *Var = Func->getVariables()[i];
67 SizeT VarIndex = Var->getIndex();
68 SizeT LiveIndex;
Jim Stichnoth47752552014-10-13 17:15:08 -070069 if (VMetadata->isMultiBlock(Var)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070070 LiveIndex = TmpNumGlobals++;
71 LiveToVarMap[LiveIndex] = Var;
72 } else {
Jim Stichnoth47752552014-10-13 17:15:08 -070073 SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070074 LiveIndex = Nodes[NodeIndex].NumLocals++;
75 Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var;
76 LiveIndex += NumGlobals;
77 }
78 VarToLiveMap[VarIndex] = LiveIndex;
79 }
80 assert(NumGlobals == TmpNumGlobals);
81
82 // Process each node.
Jim Stichnoth088b2be2014-10-23 12:02:08 -070083 for (const CfgNode *LNode : Func->getNodes()) {
84 LivenessNode &Node = Nodes[LNode->getIndex()];
Jim Stichnothd97c7df2014-06-04 11:57:08 -070085 // NumLocals, LiveToVarMap already initialized
86 Node.LiveIn.resize(NumGlobals);
87 Node.LiveOut.resize(NumGlobals);
88 // LiveBegin and LiveEnd are reinitialized before each pass over
89 // the block.
90 }
91}
92
93Variable *Liveness::getVariable(SizeT LiveIndex, const CfgNode *Node) const {
94 if (LiveIndex < NumGlobals)
95 return LiveToVarMap[LiveIndex];
96 SizeT NodeIndex = Node->getIndex();
97 return Nodes[NodeIndex].LiveToVarMap[LiveIndex - NumGlobals];
98}
99
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700100} // end of namespace Ice