blob: 8990a3eeaca2a67cc72c1b7a272ac10312784920 [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
Jim Stichnoth92a6e5b2015-12-02 16:52:44 -080011/// \brief Provides some of the support for the Liveness class.
12
13/// In particular, it handles the sparsity representation of the mapping
14/// between Variables and CfgNodes. The idea is that since most variables are
15/// used only within a single basic block, we can partition the variables into
16/// "local" and "global" sets. Instead of sizing and indexing vectors according
17/// to Variable::Number, we create a mapping such that global variables are
18/// mapped to low indexes that are common across nodes, and local variables are
19/// mapped to a higher index space that is shared across nodes.
Andrew Scull9612d322015-07-06 14:53:25 -070020///
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
Andrew Scull57e12682015-09-16 11:30:19 -070035// (IsFullInit=false). In the latter case, FirstNode points to the first node
Jim Stichnotha3f57b92015-07-30 12:46:04 -070036// 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 Stichnothcc89c952016-03-31 11:55:23 -070053 } else if (VMetadata->isSingleBlock(Var)) {
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
Andrew Scull57e12682015-09-16 11:30:19 -070063 // Resize each LivenessNode::LiveToVarMap, and the global LiveToVarMap. Reset
Jim Stichnotha3f57b92015-07-30 12:46:04 -070064 // 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
Jim Stichnothf9df4522015-08-06 17:50:14 -070074 // Initialize the bitmask of which variables to track.
75 RangeMask.resize(NumVars);
76 RangeMask.set(0, NumVars); // Track all variables by default.
77
Andrew Scull57e12682015-09-16 11:30:19 -070078 // Sort each variable into the appropriate LiveToVarMap. Set VarToLiveMap.
Jim Stichnothf9df4522015-08-06 17:50:14 -070079 // Set RangeMask correctly for each variable.
Jim Stichnotha3f57b92015-07-30 12:46:04 -070080 TmpNumGlobals = 0;
81 for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) {
82 Variable *Var = *I;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070083 SizeT VarIndex = Var->getIndex();
Jim Stichnothcc89c952016-03-31 11:55:23 -070084 SizeT LiveIndex = InvalidLiveIndex;
Jim Stichnoth47752552014-10-13 17:15:08 -070085 if (VMetadata->isMultiBlock(Var)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -070086 LiveIndex = TmpNumGlobals++;
87 LiveToVarMap[LiveIndex] = Var;
Jim Stichnothcc89c952016-03-31 11:55:23 -070088 } else if (VMetadata->isSingleBlock(Var)) {
Jim Stichnoth47752552014-10-13 17:15:08 -070089 SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070090 LiveIndex = Nodes[NodeIndex].NumLocals++;
91 Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var;
92 LiveIndex += NumGlobals;
93 }
94 VarToLiveMap[VarIndex] = LiveIndex;
Jim Stichnothcc89c952016-03-31 11:55:23 -070095 if (LiveIndex == InvalidLiveIndex || Var->getIgnoreLiveness())
Jim Stichnothf9df4522015-08-06 17:50:14 -070096 RangeMask[VarIndex] = false;
Jim Stichnothd97c7df2014-06-04 11:57:08 -070097 }
Jim Stichnotha3f57b92015-07-30 12:46:04 -070098 assert(TmpNumGlobals == (IsFullInit ? NumGlobals : 0));
Jim Stichnothd97c7df2014-06-04 11:57:08 -070099
Jim Stichnothf9df4522015-08-06 17:50:14 -0700100 // Fix up RangeMask for variables before FirstVar.
101 for (auto I = Func->getVariables().begin(); I != FirstVar; ++I) {
102 Variable *Var = *I;
103 SizeT VarIndex = Var->getIndex();
104 if (Var->getIgnoreLiveness() ||
Andrew Scull11c9a322015-08-28 14:24:14 -0700105 (!IsFullInit && !Var->hasReg() && !Var->mustHaveReg()))
Jim Stichnothf9df4522015-08-06 17:50:14 -0700106 RangeMask[VarIndex] = false;
107 }
108
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700109 // Process each node.
John Porto7bb9cab2016-04-01 05:43:09 -0700110 MaxLocals = 0;
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700111 for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) {
112 LivenessNode &Node = Nodes[(*I)->getIndex()];
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700113 // NumLocals, LiveToVarMap already initialized
114 Node.LiveIn.resize(NumGlobals);
115 Node.LiveOut.resize(NumGlobals);
Andrew Scull57e12682015-09-16 11:30:19 -0700116 // LiveBegin and LiveEnd are reinitialized before each pass over the block.
Jim Stichnoth1bdb7352016-02-29 16:58:15 -0800117 MaxLocals = std::max(MaxLocals, Node.NumLocals);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700118 }
Jim Stichnoth1bdb7352016-02-29 16:58:15 -0800119 ScratchBV.reserve(NumGlobals + MaxLocals);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700120}
121
Jim Stichnotha3f57b92015-07-30 12:46:04 -0700122void Liveness::init() {
123 constexpr bool IsFullInit = true;
124 NodeList::const_iterator FirstNode = Func->getNodes().begin();
125 VarList::const_iterator FirstVar = Func->getVariables().begin();
126 initInternal(FirstNode, FirstVar, IsFullInit);
127}
128
129void Liveness::initPhiEdgeSplits(NodeList::const_iterator FirstNode,
130 VarList::const_iterator FirstVar) {
131 constexpr bool IsFullInit = false;
132 initInternal(FirstNode, FirstVar, IsFullInit);
133}
134
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700135Variable *Liveness::getVariable(SizeT LiveIndex, const CfgNode *Node) const {
136 if (LiveIndex < NumGlobals)
137 return LiveToVarMap[LiveIndex];
138 SizeT NodeIndex = Node->getIndex();
139 return Nodes[NodeIndex].LiveToVarMap[LiveIndex - NumGlobals];
140}
141
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700142} // end of namespace Ice