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 |
| 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 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 |
| 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. |
| 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 | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 53 | } else { |
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 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 63 | // 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 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 | |
| 74 | // Sort each variable into the appropriate LiveToVarMap. Also set |
| 75 | // VarToLiveMap. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 76 | TmpNumGlobals = 0; |
| 77 | for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) { |
| 78 | Variable *Var = *I; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 79 | SizeT VarIndex = Var->getIndex(); |
| 80 | SizeT LiveIndex; |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 81 | if (VMetadata->isMultiBlock(Var)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 82 | LiveIndex = TmpNumGlobals++; |
| 83 | LiveToVarMap[LiveIndex] = Var; |
| 84 | } else { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 85 | SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 86 | LiveIndex = Nodes[NodeIndex].NumLocals++; |
| 87 | Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var; |
| 88 | LiveIndex += NumGlobals; |
| 89 | } |
| 90 | VarToLiveMap[VarIndex] = LiveIndex; |
| 91 | } |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 92 | assert(TmpNumGlobals == (IsFullInit ? NumGlobals : 0)); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 93 | |
| 94 | // Process each node. |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 95 | for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) { |
| 96 | LivenessNode &Node = Nodes[(*I)->getIndex()]; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 97 | // 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 Stichnoth | 552490c | 2015-08-05 16:21:42 -0700 | [diff] [blame^] | 103 | |
| 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 Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Jim Stichnoth | a3f57b9 | 2015-07-30 12:46:04 -0700 | [diff] [blame] | 116 | void 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 | |
| 123 | void Liveness::initPhiEdgeSplits(NodeList::const_iterator FirstNode, |
| 124 | VarList::const_iterator FirstVar) { |
| 125 | constexpr bool IsFullInit = false; |
| 126 | initInternal(FirstNode, FirstVar, IsFullInit); |
| 127 | } |
| 128 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 129 | Variable *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 Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 136 | } // end of namespace Ice |