blob: ca53a9f51864065e0600d46e7b71871a8bb8ee17 [file] [log] [blame]
Vikram S. Adve0e56b362002-09-14 23:05:33 +00001//===-- RegClass.cpp -----------------------------------------------------===//
2//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Vikram S. Adve0e56b362002-09-14 23:05:33 +000010// class RegClass for coloring-based register allocation for LLVM.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattnere46165f2003-01-15 21:02:16 +000014#include "RegClass.h"
Chris Lattnera23969b2003-01-15 19:57:07 +000015#include "RegAllocCommon.h"
Chris Lattner71270652003-09-01 20:05:47 +000016#include "IGNode.h"
Vikram S. Adve45766ab2003-07-25 21:06:09 +000017#include "llvm/Target/TargetRegInfo.h"
Ruchira Sasanka808568e2001-09-14 21:18:34 +000018
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000019//----------------------------------------------------------------------------
20// This constructor inits IG. The actual matrix is created by a call to
21// createInterferenceGraph() above.
22//----------------------------------------------------------------------------
Chris Lattner95f65b62002-04-08 22:01:15 +000023RegClass::RegClass(const Function *M,
Vikram S. Adve45766ab2003-07-25 21:06:09 +000024 const TargetRegInfo *_MRI_,
25 const TargetRegClassInfo *_MRC_)
26 : Meth(M), MRI(_MRI_), MRC(_MRC_),
27 RegClassID( _MRC_->getRegClassID() ),
28 IG(this), IGNodeStack() {
Vikram S. Adve0e56b362002-09-14 23:05:33 +000029 if( DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner71270652003-09-01 20:05:47 +000030 std::cerr << "Created Reg Class: " << RegClassID << "\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000031
Vikram S. Adve45766ab2003-07-25 21:06:09 +000032 IsColorUsedArr.resize(MRC->getNumOfAllRegs());
Ruchira Sasanka808568e2001-09-14 21:18:34 +000033}
34
35
36
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000037//----------------------------------------------------------------------------
38// Main entry point for coloring a register class.
39//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +000040void RegClass::colorAllRegs()
41{
Vikram S. Adve0e56b362002-09-14 23:05:33 +000042 if(DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner71270652003-09-01 20:05:47 +000043 std::cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000044
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000045 // pre-color IGNodes
Ruchira Sasanka808568e2001-09-14 21:18:34 +000046 pushAllIGNodes(); // push all IG Nodes
47
48 unsigned int StackSize = IGNodeStack.size();
49 IGNode *CurIGNode;
50
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000051 // for all LRs on stack
Ruchira Sasanka808568e2001-09-14 21:18:34 +000052 for( unsigned int IGN=0; IGN < StackSize; IGN++) {
53
54 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
55 IGNodeStack.pop();
56 colorIGNode (CurIGNode); // color it
57 }
58
Ruchira Sasanka808568e2001-09-14 21:18:34 +000059}
60
61
62
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000063//----------------------------------------------------------------------------
64// The method for pushing all IGNodes on to the stack.
65//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +000066void RegClass::pushAllIGNodes()
67{
68 bool NeedMoreSpills;
Ruchira Sasanka074d52d2001-11-06 15:25:38 +000069
Ruchira Sasanka808568e2001-09-14 21:18:34 +000070
71 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
72
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000073 // push non-constrained IGNodes
Ruchira Sasanka808568e2001-09-14 21:18:34 +000074 bool PushedAll = pushUnconstrainedIGNodes();
75
Vikram S. Adve0e56b362002-09-14 23:05:33 +000076 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +000077 std::cerr << " Puhsed all-unconstrained IGNodes. ";
78 if( PushedAll ) std::cerr << " No constrained nodes left.";
79 std::cerr << "\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000080 }
81
82 if( PushedAll ) // if NO constrained nodes left
83 return;
84
85
86 // now, we have constrained nodes. So, push one of them (the one with min
87 // spill cost) and try to push the others as unConstrained nodes.
88 // Repeat this.
89
Chris Lattner75323bc2002-04-15 20:36:15 +000090 do {
Vikram S. Adve928833e2001-11-06 05:11:05 +000091 //get node with min spill cost
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000092 //
Ruchira Sasanka074d52d2001-11-06 15:25:38 +000093 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
94
Vikram S. Adve928833e2001-11-06 05:11:05 +000095 // push that node on to stack
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000096 //
Chris Lattner75323bc2002-04-15 20:36:15 +000097 IGNodeStack.push(IGNodeSpill);
Ruchira Sasanka808568e2001-09-14 21:18:34 +000098
Vikram S. Adve928833e2001-11-06 05:11:05 +000099 // set its OnStack flag and decrement degree of neighs
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000100 //
Vikram S. Adve928833e2001-11-06 05:11:05 +0000101 IGNodeSpill->pushOnStack();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000102
Misha Brukmanacda7df2003-09-11 22:34:13 +0000103 // now push NON-constrained ones, if any
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000104 //
Chris Lattner75323bc2002-04-15 20:36:15 +0000105 NeedMoreSpills = !pushUnconstrainedIGNodes();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000106
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000107 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner71270652003-09-01 20:05:47 +0000108 std::cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Ruchira Sasankad1d5e972001-11-10 21:21:36 +0000109
Chris Lattner75323bc2002-04-15 20:36:15 +0000110 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000111
112}
113
114
115
116
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000117//--------------------------------------------------------------------------
118// This method goes thru all IG nodes in the IGNodeList of an IG of a
119// register class and push any unconstrained IG node left (that is not
120// already pushed)
121//--------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000122
123bool RegClass::pushUnconstrainedIGNodes()
124{
125 // # of LRs for this reg class
126 unsigned int IGNodeListSize = IG.getIGNodeList().size();
127 bool pushedall = true;
128
129 // a pass over IGNodeList
130 for( unsigned i =0; i < IGNodeListSize; i++) {
131
132 // get IGNode i from IGNodeList
133 IGNode *IGNode = IG.getIGNodeList()[i];
134
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000135 if( !IGNode ) // can be null due to merging
136 continue;
137
138 // if already pushed on stack, continue. This can happen since this
139 // method can be called repeatedly until all constrained nodes are
140 // pushed
141 if( IGNode->isOnStack() )
142 continue;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000143 // if the degree of IGNode is lower
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000144 if( (unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs()) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000145 IGNodeStack.push( IGNode ); // push IGNode on to the stack
146 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
147
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000148 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000149 std::cerr << " pushed un-constrained IGNode " << IGNode->getIndex()
150 << " on to stack\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000151 }
152 }
153 else pushedall = false; // we didn't push all live ranges
154
155 } // for
156
157 // returns true if we pushed all live ranges - else false
158 return pushedall;
159}
160
161
162
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000163//----------------------------------------------------------------------------
Misha Brukmanacda7df2003-09-11 22:34:13 +0000164// Get the IGNode with the minimum spill cost
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000165//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000166IGNode * RegClass::getIGNodeWithMinSpillCost()
167{
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000168
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000169 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Chris Lattner5ae3bd62002-10-22 23:34:11 +0000170 double MinSpillCost = 0;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000171 IGNode *MinCostIGNode = NULL;
Ruchira Sasankabc284552002-01-08 16:29:23 +0000172 bool isFirstNode = true;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000173
174 // pass over IGNodeList to find the IGNode with minimum spill cost
175 // among all IGNodes that are not yet pushed on to the stack
176 //
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000177 for( unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000178 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000179
180 if( ! IGNode ) // can be null due to merging
181 continue;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000182
183 if( ! IGNode->isOnStack() ) {
184
Ruchira Sasankabc284552002-01-08 16:29:23 +0000185 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
186 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000187
Ruchira Sasankabc284552002-01-08 16:29:23 +0000188 if( isFirstNode ) { // for the first IG node
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000189 MinSpillCost = SpillCost;
190 MinCostIGNode = IGNode;
Ruchira Sasankabc284552002-01-08 16:29:23 +0000191 isFirstNode = false;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000192 }
193
194 else if( MinSpillCost > SpillCost) {
195 MinSpillCost = SpillCost;
196 MinCostIGNode = IGNode;
197 }
198
199 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000200 }
201
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000202 assert( MinCostIGNode && "No IGNode to spill");
203 return MinCostIGNode;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000204}
205
206
207
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000208//----------------------------------------------------------------------------
209// Color the IGNode using the machine specific code.
210//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000211void RegClass::colorIGNode(IGNode *const Node)
212{
213
214 if( ! Node->hasColor() ) { // not colored as an arg etc.
215
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000216 // init all elements of to IsColorUsedAr false;
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000217 clearColorsUsed();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000218
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000219 // initialize all colors used by neighbors of this node to true
220 LiveRange *LR = Node->getParentLR();
221 unsigned NumNeighbors = Node->getNumOfNeighbors();
222 for (unsigned n=0; n < NumNeighbors; n++) {
223 IGNode *NeighIGNode = Node->getAdjIGNode(n);
224 LiveRange *NeighLR = NeighIGNode->getParentLR();
225
Misha Brukmanacda7df2003-09-11 22:34:13 +0000226 // Don't use a color if it is in use by the neighbor,
227 // or is suggested for use by the neighbor,
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000228 // markColorsUsed() should be given the color and the reg type for
229 // LR, not for NeighLR, because it should mark registers used based on
230 // the type we are looking for, not on the regType for the neighbour.
231 if (NeighLR->hasColor())
232 this->markColorsUsed(NeighLR->getColor(),
233 MRI->getRegTypeForLR(NeighLR),
234 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
235 else if (NeighLR->hasSuggestedColor() &&
236 NeighLR->isSuggestedColorUsable())
237 this->markColorsUsed(NeighLR->getSuggestedColor(),
238 MRI->getRegTypeForLR(NeighLR),
239 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000240 }
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000241
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000242 // call the target specific code for coloring
243 //
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000244 MRC->colorIGNode(Node, IsColorUsedArr);
245 }
246 else {
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000247 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000248 std::cerr << " Node " << Node->getIndex();
249 std::cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000250 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000251 }
252
253
254 if( !Node->hasColor() ) {
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000255 if( DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000256 std::cerr << " Node " << Node->getIndex();
257 std::cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000258 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000259 }
260
261}
262
Chris Lattner5abe44b2002-10-29 16:51:05 +0000263void RegClass::printIGNodeList() const {
264 std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
265 IG.printIGNodeList();
266}
267
268void RegClass::printIG() {
269 std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
270 IG.printIG();
271}
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000272
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000273