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 | |
| 33 | void Liveness::init() { |
| 34 | // Initialize most of the container sizes. |
| 35 | SizeT NumVars = Func->getVariables().size(); |
| 36 | SizeT NumNodes = Func->getNumNodes(); |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 37 | VariablesMetadata *VMetadata = Func->getVMetadata(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 38 | Nodes.resize(NumNodes); |
| 39 | VarToLiveMap.resize(NumVars); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 40 | |
| 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 Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 45 | if (VMetadata->isMultiBlock(Var)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 46 | ++NumGlobals; |
| 47 | } else { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 48 | SizeT Index = VMetadata->getLocalUseNode(Var)->getIndex(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 49 | ++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 Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 56 | Nodes[i].LiveToVarMap.assign(Nodes[i].NumLocals, nullptr); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 57 | Nodes[i].NumLocals = 0; |
Jim Stichnoth | 336f6c4 | 2014-10-30 15:01:31 -0700 | [diff] [blame] | 58 | Nodes[i].NumNonDeadPhis = 0; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 59 | } |
Jim Stichnoth | ae95320 | 2014-12-20 06:17:49 -0800 | [diff] [blame] | 60 | LiveToVarMap.assign(NumGlobals, nullptr); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 61 | |
| 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 Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 69 | if (VMetadata->isMultiBlock(Var)) { |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 70 | LiveIndex = TmpNumGlobals++; |
| 71 | LiveToVarMap[LiveIndex] = Var; |
| 72 | } else { |
Jim Stichnoth | 4775255 | 2014-10-13 17:15:08 -0700 | [diff] [blame] | 73 | SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex(); |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 74 | 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 Stichnoth | 088b2be | 2014-10-23 12:02:08 -0700 | [diff] [blame] | 83 | for (const CfgNode *LNode : Func->getNodes()) { |
| 84 | LivenessNode &Node = Nodes[LNode->getIndex()]; |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 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 | |
Jim Stichnoth | d97c7df | 2014-06-04 11:57:08 -0700 | [diff] [blame] | 100 | } // end of namespace Ice |