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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides some of the support for the Liveness class. In |
| 11 | // particular, it handles the sparsity representation of the mapping |
| 12 | // between Variables and CfgNodes. The idea is that since most |
| 13 | // variables are used only within a single basic block, we can |
| 14 | // partition the variables into "local" and "global" sets. Instead of |
| 15 | // sizing and indexing vectors according to Variable::Number, we |
| 16 | // create a mapping such that global variables are mapped to low |
| 17 | // indexes that are common across nodes, and local variables are |
| 18 | // mapped to a higher index space that is shared across nodes. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "IceDefs.h" |
| 23 | #include "IceCfg.h" |
| 24 | #include "IceCfgNode.h" |
| 25 | #include "IceInst.h" |
| 26 | #include "IceLiveness.h" |
| 27 | #include "IceOperand.h" |
| 28 | |
| 29 | namespace Ice { |
| 30 | |
| 31 | void Liveness::init() { |
| 32 | // Initialize most of the container sizes. |
| 33 | SizeT NumVars = Func->getVariables().size(); |
| 34 | SizeT NumNodes = Func->getNumNodes(); |
| 35 | Nodes.resize(NumNodes); |
| 36 | VarToLiveMap.resize(NumVars); |
| 37 | if (Mode == Liveness_Intervals) |
| 38 | LiveRanges.resize(NumVars); |
| 39 | |
| 40 | // Count the number of globals, and the number of locals for each |
| 41 | // block. |
| 42 | for (SizeT i = 0; i < NumVars; ++i) { |
| 43 | Variable *Var = Func->getVariables()[i]; |
| 44 | if (Var->isMultiblockLife()) { |
| 45 | ++NumGlobals; |
| 46 | } else { |
| 47 | SizeT Index = Var->getLocalUseNode()->getIndex(); |
| 48 | ++Nodes[Index].NumLocals; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Resize each LivenessNode::LiveToVarMap, and the global |
| 53 | // LiveToVarMap. Reset the counts to 0. |
| 54 | for (SizeT i = 0; i < NumNodes; ++i) { |
| 55 | Nodes[i].LiveToVarMap.assign(Nodes[i].NumLocals, NULL); |
| 56 | Nodes[i].NumLocals = 0; |
| 57 | } |
| 58 | LiveToVarMap.assign(NumGlobals, NULL); |
| 59 | |
| 60 | // Sort each variable into the appropriate LiveToVarMap. Also set |
| 61 | // VarToLiveMap. |
| 62 | SizeT TmpNumGlobals = 0; |
| 63 | for (SizeT i = 0; i < NumVars; ++i) { |
| 64 | Variable *Var = Func->getVariables()[i]; |
| 65 | SizeT VarIndex = Var->getIndex(); |
| 66 | SizeT LiveIndex; |
| 67 | if (Var->isMultiblockLife()) { |
| 68 | LiveIndex = TmpNumGlobals++; |
| 69 | LiveToVarMap[LiveIndex] = Var; |
| 70 | } else { |
| 71 | SizeT NodeIndex = Var->getLocalUseNode()->getIndex(); |
| 72 | LiveIndex = Nodes[NodeIndex].NumLocals++; |
| 73 | Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var; |
| 74 | LiveIndex += NumGlobals; |
| 75 | } |
| 76 | VarToLiveMap[VarIndex] = LiveIndex; |
| 77 | } |
| 78 | assert(NumGlobals == TmpNumGlobals); |
| 79 | |
| 80 | // Process each node. |
| 81 | const NodeList &LNodes = Func->getNodes(); |
| 82 | SizeT NumLNodes = LNodes.size(); |
| 83 | for (SizeT i = 0; i < NumLNodes; ++i) { |
| 84 | LivenessNode &Node = Nodes[LNodes[i]->getIndex()]; |
| 85 | // 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 | |
| 93 | Variable *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 | |
| 100 | SizeT Liveness::getLiveIndex(const Variable *Var) const { |
| 101 | return VarToLiveMap[Var->getIndex()]; |
| 102 | } |
| 103 | |
| 104 | void Liveness::addLiveRange(Variable *Var, InstNumberT Start, InstNumberT End, |
| 105 | uint32_t WeightDelta) { |
| 106 | LiveRange &LiveRange = LiveRanges[Var->getIndex()]; |
| 107 | assert(WeightDelta != RegWeight::Inf); |
| 108 | LiveRange.addSegment(Start, End); |
| 109 | LiveRange.addWeight(WeightDelta); |
| 110 | } |
| 111 | |
| 112 | LiveRange &Liveness::getLiveRange(Variable *Var) { |
| 113 | return LiveRanges[Var->getIndex()]; |
| 114 | } |
| 115 | |
| 116 | } // end of namespace Ice |