blob: 07e478bc543f45c86e1cfdcd048a5ca85b4d3171 [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 Lattner71270652003-09-01 20:05:47 +000014#include "IGNode.h"
Misha Brukman88df8762003-10-23 18:10:02 +000015#include "RegAllocCommon.h"
16#include "RegClass.h"
Brian Gaeke71509a92004-04-23 18:15:47 +000017#include "../SparcV9RegInfo.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000018#include <iostream>
Ruchira Sasanka808568e2001-09-14 21:18:34 +000019
Brian Gaeke960707c2003-11-11 22:41:34 +000020namespace llvm {
21
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000022//----------------------------------------------------------------------------
23// This constructor inits IG. The actual matrix is created by a call to
24// createInterferenceGraph() above.
25//----------------------------------------------------------------------------
Chris Lattner95f65b62002-04-08 22:01:15 +000026RegClass::RegClass(const Function *M,
Brian Gaekedca24dd2004-06-03 02:45:09 +000027 const SparcV9RegInfo *_MRI_,
Vikram S. Adve45766ab2003-07-25 21:06:09 +000028 const TargetRegClassInfo *_MRC_)
29 : Meth(M), MRI(_MRI_), MRC(_MRC_),
30 RegClassID( _MRC_->getRegClassID() ),
31 IG(this), IGNodeStack() {
Misha Brukman88df8762003-10-23 18:10:02 +000032 if (DEBUG_RA >= RA_DEBUG_Interference)
Chris Lattner71270652003-09-01 20:05:47 +000033 std::cerr << "Created Reg Class: " << RegClassID << "\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000034
Vikram S. Adve45766ab2003-07-25 21:06:09 +000035 IsColorUsedArr.resize(MRC->getNumOfAllRegs());
Ruchira Sasanka808568e2001-09-14 21:18:34 +000036}
37
38
39
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000040//----------------------------------------------------------------------------
41// Main entry point for coloring a register class.
42//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +000043void RegClass::colorAllRegs()
44{
Misha Brukman88df8762003-10-23 18:10:02 +000045 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner71270652003-09-01 20:05:47 +000046 std::cerr << "Coloring IG of reg class " << RegClassID << " ...\n";
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000047 // pre-color IGNodes
Ruchira Sasanka808568e2001-09-14 21:18:34 +000048 pushAllIGNodes(); // push all IG Nodes
49
50 unsigned int StackSize = IGNodeStack.size();
51 IGNode *CurIGNode;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000052 // for all LRs on stack
Misha Brukman88df8762003-10-23 18:10:02 +000053 for (unsigned int IGN=0; IGN < StackSize; IGN++) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +000054 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
55 IGNodeStack.pop();
56 colorIGNode (CurIGNode); // color it
57 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +000058}
59
60
61
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000062//----------------------------------------------------------------------------
63// The method for pushing all IGNodes on to the stack.
64//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +000065void RegClass::pushAllIGNodes()
66{
67 bool NeedMoreSpills;
Ruchira Sasanka074d52d2001-11-06 15:25:38 +000068
Ruchira Sasanka808568e2001-09-14 21:18:34 +000069
70 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
71
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000072 // push non-constrained IGNodes
Ruchira Sasanka808568e2001-09-14 21:18:34 +000073 bool PushedAll = pushUnconstrainedIGNodes();
74
Misha Brukman88df8762003-10-23 18:10:02 +000075 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +000076 std::cerr << " Puhsed all-unconstrained IGNodes. ";
77 if( PushedAll ) std::cerr << " No constrained nodes left.";
78 std::cerr << "\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000079 }
80
Misha Brukman88df8762003-10-23 18:10:02 +000081 if (PushedAll) // if NO constrained nodes left
Ruchira Sasanka808568e2001-09-14 21:18:34 +000082 return;
83
84
85 // now, we have constrained nodes. So, push one of them (the one with min
86 // spill cost) and try to push the others as unConstrained nodes.
87 // Repeat this.
88
Chris Lattner75323bc2002-04-15 20:36:15 +000089 do {
Vikram S. Adve928833e2001-11-06 05:11:05 +000090 //get node with min spill cost
Ruchira Sasanka074d52d2001-11-06 15:25:38 +000091 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
Vikram S. Adve928833e2001-11-06 05:11:05 +000092 // push that node on to stack
Chris Lattner75323bc2002-04-15 20:36:15 +000093 IGNodeStack.push(IGNodeSpill);
Vikram S. Adve928833e2001-11-06 05:11:05 +000094 // set its OnStack flag and decrement degree of neighs
95 IGNodeSpill->pushOnStack();
Misha Brukmanacda7df2003-09-11 22:34:13 +000096 // now push NON-constrained ones, if any
Chris Lattner75323bc2002-04-15 20:36:15 +000097 NeedMoreSpills = !pushUnconstrainedIGNodes();
Vikram S. Adve0e56b362002-09-14 23:05:33 +000098 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner71270652003-09-01 20:05:47 +000099 std::cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Chris Lattner75323bc2002-04-15 20:36:15 +0000100 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000101
102}
103
104
105
106
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000107//--------------------------------------------------------------------------
108// This method goes thru all IG nodes in the IGNodeList of an IG of a
109// register class and push any unconstrained IG node left (that is not
110// already pushed)
111//--------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000112
113bool RegClass::pushUnconstrainedIGNodes()
114{
115 // # of LRs for this reg class
116 unsigned int IGNodeListSize = IG.getIGNodeList().size();
117 bool pushedall = true;
118
119 // a pass over IGNodeList
Misha Brukman88df8762003-10-23 18:10:02 +0000120 for (unsigned i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000121
122 // get IGNode i from IGNodeList
123 IGNode *IGNode = IG.getIGNodeList()[i];
124
Misha Brukman88df8762003-10-23 18:10:02 +0000125 if (!IGNode ) // can be null due to merging
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000126 continue;
127
128 // if already pushed on stack, continue. This can happen since this
129 // method can be called repeatedly until all constrained nodes are
130 // pushed
Misha Brukman88df8762003-10-23 18:10:02 +0000131 if (IGNode->isOnStack() )
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000132 continue;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000133 // if the degree of IGNode is lower
Misha Brukman88df8762003-10-23 18:10:02 +0000134 if ((unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs()) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000135 IGNodeStack.push( IGNode ); // push IGNode on to the stack
136 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
137
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000138 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000139 std::cerr << " pushed un-constrained IGNode " << IGNode->getIndex()
140 << " on to stack\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000141 }
142 }
143 else pushedall = false; // we didn't push all live ranges
144
145 } // for
146
147 // returns true if we pushed all live ranges - else false
148 return pushedall;
149}
150
151
152
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000153//----------------------------------------------------------------------------
Misha Brukmanacda7df2003-09-11 22:34:13 +0000154// Get the IGNode with the minimum spill cost
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000155//----------------------------------------------------------------------------
Misha Brukman88df8762003-10-23 18:10:02 +0000156IGNode * RegClass::getIGNodeWithMinSpillCost() {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000157 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Chris Lattner5ae3bd62002-10-22 23:34:11 +0000158 double MinSpillCost = 0;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000159 IGNode *MinCostIGNode = NULL;
Ruchira Sasankabc284552002-01-08 16:29:23 +0000160 bool isFirstNode = true;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000161
162 // pass over IGNodeList to find the IGNode with minimum spill cost
163 // among all IGNodes that are not yet pushed on to the stack
Misha Brukman88df8762003-10-23 18:10:02 +0000164 for (unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000165 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000166
Misha Brukman88df8762003-10-23 18:10:02 +0000167 if (!IGNode) // can be null due to merging
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000168 continue;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000169
Misha Brukman88df8762003-10-23 18:10:02 +0000170 if (!IGNode->isOnStack()) {
Ruchira Sasankabc284552002-01-08 16:29:23 +0000171 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
172 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000173
Misha Brukman88df8762003-10-23 18:10:02 +0000174 if (isFirstNode) { // for the first IG node
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000175 MinSpillCost = SpillCost;
176 MinCostIGNode = IGNode;
Ruchira Sasankabc284552002-01-08 16:29:23 +0000177 isFirstNode = false;
Misha Brukman88df8762003-10-23 18:10:02 +0000178 } else if (MinSpillCost > SpillCost) {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000179 MinSpillCost = SpillCost;
180 MinCostIGNode = IGNode;
181 }
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000182 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000183 }
184
Misha Brukman88df8762003-10-23 18:10:02 +0000185 assert (MinCostIGNode && "No IGNode to spill");
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000186 return MinCostIGNode;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000187}
188
189
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000190//----------------------------------------------------------------------------
191// Color the IGNode using the machine specific code.
192//----------------------------------------------------------------------------
Misha Brukman88df8762003-10-23 18:10:02 +0000193void RegClass::colorIGNode(IGNode *const Node) {
194 if (! Node->hasColor()) { // not colored as an arg etc.
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000195
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000196 // init all elements of to IsColorUsedAr false;
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000197 clearColorsUsed();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000198
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000199 // initialize all colors used by neighbors of this node to true
200 LiveRange *LR = Node->getParentLR();
201 unsigned NumNeighbors = Node->getNumOfNeighbors();
202 for (unsigned n=0; n < NumNeighbors; n++) {
203 IGNode *NeighIGNode = Node->getAdjIGNode(n);
204 LiveRange *NeighLR = NeighIGNode->getParentLR();
205
Misha Brukmanacda7df2003-09-11 22:34:13 +0000206 // Don't use a color if it is in use by the neighbor,
207 // or is suggested for use by the neighbor,
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000208 // markColorsUsed() should be given the color and the reg type for
209 // LR, not for NeighLR, because it should mark registers used based on
210 // the type we are looking for, not on the regType for the neighbour.
211 if (NeighLR->hasColor())
212 this->markColorsUsed(NeighLR->getColor(),
213 MRI->getRegTypeForLR(NeighLR),
214 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
215 else if (NeighLR->hasSuggestedColor() &&
216 NeighLR->isSuggestedColorUsable())
217 this->markColorsUsed(NeighLR->getSuggestedColor(),
218 MRI->getRegTypeForLR(NeighLR),
219 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000220 }
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000221
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000222 // call the target specific code for coloring
223 //
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000224 MRC->colorIGNode(Node, IsColorUsedArr);
Misha Brukman88df8762003-10-23 18:10:02 +0000225 } else {
226 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000227 std::cerr << " Node " << Node->getIndex();
228 std::cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000229 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000230 }
231
232
Misha Brukman88df8762003-10-23 18:10:02 +0000233 if (!Node->hasColor() ) {
234 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000235 std::cerr << " Node " << Node->getIndex();
236 std::cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000237 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000238 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000239}
240
Chris Lattner5abe44b2002-10-29 16:51:05 +0000241void RegClass::printIGNodeList() const {
242 std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
243 IG.printIGNodeList();
244}
245
246void RegClass::printIG() {
247 std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
248 IG.printIG();
249}
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000250
Brian Gaeke960707c2003-11-11 22:41:34 +0000251} // End llvm namespace