Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 1 | //===- 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 Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \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 Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 20 | /// |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
| 22 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 23 | #include "IceLiveness.h" |
| 24 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 25 | #include "IceCfg.h" |
| 26 | #include "IceCfgNode.h" |
Jim Stichnoth | a18cc9c | 2014-09-30 19:10:22 -0700 | [diff] [blame] | 27 | #include "IceDefs.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 28 | #include "IceInst.h" |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 29 | #include "IceOperand.h" |
| 30 | |
| 31 | namespace Ice { |
| 32 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 33 | // Initializes the basic liveness-related data structures for full liveness |
| 34 | // analysis (IsFullInit=true), or for incremental update after phi lowering |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 35 | // (IsFullInit=false). In the latter case, FirstNode points to the first node |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 36 | // added since starting phi lowering, and FirstVar points to the first Variable |
| 37 | // added since starting phi lowering. |
| 38 | void Liveness::initInternal(NodeList::const_iterator FirstNode, |
| 39 | VarList::const_iterator FirstVar, bool IsFullInit) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 40 | // Initialize most of the container sizes. |
| 41 | SizeT NumVars = Func->getVariables().size(); |
| 42 | SizeT NumNodes = Func->getNumNodes(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 43 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 44 | Nodes.resize(NumNodes); |
| 45 | VarToLiveMap.resize(NumVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 46 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 47 | // 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 Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 51 | if (VMetadata->isMultiBlock(Var)) { |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 52 | ++TmpNumGlobals; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 53 | } else if (VMetadata->isSingleBlock(Var)) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 54 | SizeT Index = VMetadata->getLocalUseNode(Var)->getIndex(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 55 | ++Nodes[Index].NumLocals; |
| 56 | } |
| 57 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 58 | if (IsFullInit) |
| 59 | NumGlobals = TmpNumGlobals; |
| 60 | else |
| 61 | assert(TmpNumGlobals == 0); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 62 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 63 | // Resize each LivenessNode::LiveToVarMap, and the global LiveToVarMap. Reset |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 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 Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 70 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 71 | if (IsFullInit) |
| 72 | LiveToVarMap.assign(NumGlobals, nullptr); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 73 | |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 74 | // Initialize the bitmask of which variables to track. |
| 75 | RangeMask.resize(NumVars); |
| 76 | RangeMask.set(0, NumVars); // Track all variables by default. |
| 77 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 78 | // Sort each variable into the appropriate LiveToVarMap. Set VarToLiveMap. |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 79 | // Set RangeMask correctly for each variable. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 80 | TmpNumGlobals = 0; |
| 81 | for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) { |
| 82 | Variable *Var = *I; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 83 | SizeT VarIndex = Var->getIndex(); |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 84 | SizeT LiveIndex = InvalidLiveIndex; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 85 | if (VMetadata->isMultiBlock(Var)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 86 | LiveIndex = TmpNumGlobals++; |
| 87 | LiveToVarMap[LiveIndex] = Var; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 88 | } else if (VMetadata->isSingleBlock(Var)) { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 89 | SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 90 | LiveIndex = Nodes[NodeIndex].NumLocals++; |
| 91 | Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var; |
| 92 | LiveIndex += NumGlobals; |
| 93 | } |
| 94 | VarToLiveMap[VarIndex] = LiveIndex; |
Jim Stichnoth | cc89c95 | 2016-03-31 11:55:23 -0700 | [diff] [blame] | 95 | if (LiveIndex == InvalidLiveIndex || Var->getIgnoreLiveness()) |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 96 | RangeMask[VarIndex] = false; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 97 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 98 | assert(TmpNumGlobals == (IsFullInit ? NumGlobals : 0)); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 99 | |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 100 | // 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 Scull | 11c9a32 | 2015-08-28 14:24:14 -0700 | [diff] [blame] | 105 | (!IsFullInit && !Var->hasReg() && !Var->mustHaveReg())) |
Jim Stichnoth | f9df452 | 2015-08-06 17:50:14 -0700 | [diff] [blame] | 106 | RangeMask[VarIndex] = false; |
| 107 | } |
| 108 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 109 | // Process each node. |
John Porto | 7bb9cab | 2016-04-01 05:43:09 -0700 | [diff] [blame] | 110 | MaxLocals = 0; |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 111 | for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) { |
| 112 | LivenessNode &Node = Nodes[(*I)->getIndex()]; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 113 | // NumLocals, LiveToVarMap already initialized |
| 114 | Node.LiveIn.resize(NumGlobals); |
| 115 | Node.LiveOut.resize(NumGlobals); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 116 | // LiveBegin and LiveEnd are reinitialized before each pass over the block. |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 117 | MaxLocals = std::max(MaxLocals, Node.NumLocals); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 118 | } |
Jim Stichnoth | 1bdb735 | 2016-02-29 16:58:15 -0800 | [diff] [blame] | 119 | ScratchBV.reserve(NumGlobals + MaxLocals); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 122 | void 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 | |
| 129 | void Liveness::initPhiEdgeSplits(NodeList::const_iterator FirstNode, |
| 130 | VarList::const_iterator FirstVar) { |
| 131 | constexpr bool IsFullInit = false; |
| 132 | initInternal(FirstNode, FirstVar, IsFullInit); |
| 133 | } |
| 134 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 135 | Variable *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 Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 142 | } // end of namespace Ice |