Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: src/IceLiveness.cpp

Issue 1253833002: Subzero: Cleanly implement register allocation after phi lowering. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Cleanup Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- subzero/src/IceLiveness.cpp - Liveness analysis implementation -----===// 1 //===- subzero/src/IceLiveness.cpp - Liveness analysis implementation -----===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 /// 9 ///
10 /// \file 10 /// \file
(...skipping 12 matching lines...) Expand all
23 #include "IceLiveness.h" 23 #include "IceLiveness.h"
24 24
25 #include "IceCfg.h" 25 #include "IceCfg.h"
26 #include "IceCfgNode.h" 26 #include "IceCfgNode.h"
27 #include "IceDefs.h" 27 #include "IceDefs.h"
28 #include "IceInst.h" 28 #include "IceInst.h"
29 #include "IceOperand.h" 29 #include "IceOperand.h"
30 30
31 namespace Ice { 31 namespace Ice {
32 32
33 void Liveness::init() { 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) {
34 // Initialize most of the container sizes. 40 // Initialize most of the container sizes.
35 SizeT NumVars = Func->getVariables().size(); 41 SizeT NumVars = Func->getVariables().size();
36 SizeT NumNodes = Func->getNumNodes(); 42 SizeT NumNodes = Func->getNumNodes();
37 VariablesMetadata *VMetadata = Func->getVMetadata(); 43 VariablesMetadata *VMetadata = Func->getVMetadata();
38 Nodes.resize(NumNodes); 44 Nodes.resize(NumNodes);
39 VarToLiveMap.resize(NumVars); 45 VarToLiveMap.resize(NumVars);
40 46
41 // Count the number of globals, and the number of locals for each 47 // Count the number of globals, and the number of locals for each block.
42 // block. 48 SizeT TmpNumGlobals = 0;
43 for (SizeT i = 0; i < NumVars; ++i) { 49 for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) {
44 Variable *Var = Func->getVariables()[i]; 50 Variable *Var = *I;
45 if (VMetadata->isMultiBlock(Var)) { 51 if (VMetadata->isMultiBlock(Var)) {
46 ++NumGlobals; 52 ++TmpNumGlobals;
47 } else { 53 } else {
48 SizeT Index = VMetadata->getLocalUseNode(Var)->getIndex(); 54 SizeT Index = VMetadata->getLocalUseNode(Var)->getIndex();
49 ++Nodes[Index].NumLocals; 55 ++Nodes[Index].NumLocals;
50 } 56 }
51 } 57 }
58 if (IsFullInit)
59 NumGlobals = TmpNumGlobals;
60 else
61 assert(TmpNumGlobals == 0);
52 62
53 // Resize each LivenessNode::LiveToVarMap, and the global 63 // Resize each LivenessNode::LiveToVarMap, and the global LiveToVarMap. Reset
54 // LiveToVarMap. Reset the counts to 0. 64 // the counts to 0.
55 for (SizeT i = 0; i < NumNodes; ++i) { 65 for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) {
56 Nodes[i].LiveToVarMap.assign(Nodes[i].NumLocals, nullptr); 66 LivenessNode &N = Nodes[(*I)->getIndex()];
57 Nodes[i].NumLocals = 0; 67 N.LiveToVarMap.assign(N.NumLocals, nullptr);
58 Nodes[i].NumNonDeadPhis = 0; 68 N.NumLocals = 0;
69 N.NumNonDeadPhis = 0;
59 } 70 }
60 LiveToVarMap.assign(NumGlobals, nullptr); 71 if (IsFullInit)
72 LiveToVarMap.assign(NumGlobals, nullptr);
61 73
62 // Sort each variable into the appropriate LiveToVarMap. Also set 74 // Sort each variable into the appropriate LiveToVarMap. Also set
63 // VarToLiveMap. 75 // VarToLiveMap.
64 SizeT TmpNumGlobals = 0; 76 TmpNumGlobals = 0;
65 for (SizeT i = 0; i < NumVars; ++i) { 77 for (auto I = FirstVar, E = Func->getVariables().end(); I != E; ++I) {
66 Variable *Var = Func->getVariables()[i]; 78 Variable *Var = *I;
67 SizeT VarIndex = Var->getIndex(); 79 SizeT VarIndex = Var->getIndex();
68 SizeT LiveIndex; 80 SizeT LiveIndex;
69 if (VMetadata->isMultiBlock(Var)) { 81 if (VMetadata->isMultiBlock(Var)) {
70 LiveIndex = TmpNumGlobals++; 82 LiveIndex = TmpNumGlobals++;
71 LiveToVarMap[LiveIndex] = Var; 83 LiveToVarMap[LiveIndex] = Var;
72 } else { 84 } else {
73 SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex(); 85 SizeT NodeIndex = VMetadata->getLocalUseNode(Var)->getIndex();
74 LiveIndex = Nodes[NodeIndex].NumLocals++; 86 LiveIndex = Nodes[NodeIndex].NumLocals++;
75 Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var; 87 Nodes[NodeIndex].LiveToVarMap[LiveIndex] = Var;
76 LiveIndex += NumGlobals; 88 LiveIndex += NumGlobals;
77 } 89 }
78 VarToLiveMap[VarIndex] = LiveIndex; 90 VarToLiveMap[VarIndex] = LiveIndex;
79 } 91 }
80 assert(NumGlobals == TmpNumGlobals); 92 assert(TmpNumGlobals == (IsFullInit ? NumGlobals : 0));
81 93
82 // Process each node. 94 // Process each node.
83 for (const CfgNode *LNode : Func->getNodes()) { 95 for (auto I = FirstNode, E = Func->getNodes().end(); I != E; ++I) {
84 LivenessNode &Node = Nodes[LNode->getIndex()]; 96 LivenessNode &Node = Nodes[(*I)->getIndex()];
85 // NumLocals, LiveToVarMap already initialized 97 // NumLocals, LiveToVarMap already initialized
86 Node.LiveIn.resize(NumGlobals); 98 Node.LiveIn.resize(NumGlobals);
87 Node.LiveOut.resize(NumGlobals); 99 Node.LiveOut.resize(NumGlobals);
88 // LiveBegin and LiveEnd are reinitialized before each pass over 100 // LiveBegin and LiveEnd are reinitialized before each pass over
89 // the block. 101 // the block.
90 } 102 }
91 } 103 }
92 104
105 void Liveness::init() {
106 constexpr bool IsFullInit = true;
107 NodeList::const_iterator FirstNode = Func->getNodes().begin();
108 VarList::const_iterator FirstVar = Func->getVariables().begin();
109 initInternal(FirstNode, FirstVar, IsFullInit);
110 }
111
112 void Liveness::initPhiEdgeSplits(NodeList::const_iterator FirstNode,
113 VarList::const_iterator FirstVar) {
114 constexpr bool IsFullInit = false;
115 initInternal(FirstNode, FirstVar, IsFullInit);
116 }
117
93 Variable *Liveness::getVariable(SizeT LiveIndex, const CfgNode *Node) const { 118 Variable *Liveness::getVariable(SizeT LiveIndex, const CfgNode *Node) const {
94 if (LiveIndex < NumGlobals) 119 if (LiveIndex < NumGlobals)
95 return LiveToVarMap[LiveIndex]; 120 return LiveToVarMap[LiveIndex];
96 SizeT NodeIndex = Node->getIndex(); 121 SizeT NodeIndex = Node->getIndex();
97 return Nodes[NodeIndex].LiveToVarMap[LiveIndex - NumGlobals]; 122 return Nodes[NodeIndex].LiveToVarMap[LiveIndex - NumGlobals];
98 } 123 }
99 124
100 } // end of namespace Ice 125 } // end of namespace Ice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698