blob: 9c8603b82c3a366e2cf8f97620b4f590bdfd68c5 [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"
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() {
Misha Brukman88df8762003-10-23 18:10:02 +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{
Misha Brukman88df8762003-10-23 18:10:02 +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 Sasanka8c2d8252002-01-07 19:19:18 +000044 // pre-color IGNodes
Ruchira Sasanka808568e2001-09-14 21:18:34 +000045 pushAllIGNodes(); // push all IG Nodes
46
47 unsigned int StackSize = IGNodeStack.size();
48 IGNode *CurIGNode;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000049 // for all LRs on stack
Misha Brukman88df8762003-10-23 18:10:02 +000050 for (unsigned int IGN=0; IGN < StackSize; IGN++) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +000051 CurIGNode = IGNodeStack.top(); // pop the IGNode on top of stack
52 IGNodeStack.pop();
53 colorIGNode (CurIGNode); // color it
54 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +000055}
56
57
58
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000059//----------------------------------------------------------------------------
60// The method for pushing all IGNodes on to the stack.
61//----------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +000062void RegClass::pushAllIGNodes()
63{
64 bool NeedMoreSpills;
Ruchira Sasanka074d52d2001-11-06 15:25:38 +000065
Ruchira Sasanka808568e2001-09-14 21:18:34 +000066
67 IG.setCurDegreeOfIGNodes(); // calculate degree of IGNodes
68
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +000069 // push non-constrained IGNodes
Ruchira Sasanka808568e2001-09-14 21:18:34 +000070 bool PushedAll = pushUnconstrainedIGNodes();
71
Misha Brukman88df8762003-10-23 18:10:02 +000072 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +000073 std::cerr << " Puhsed all-unconstrained IGNodes. ";
74 if( PushedAll ) std::cerr << " No constrained nodes left.";
75 std::cerr << "\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +000076 }
77
Misha Brukman88df8762003-10-23 18:10:02 +000078 if (PushedAll) // if NO constrained nodes left
Ruchira Sasanka808568e2001-09-14 21:18:34 +000079 return;
80
81
82 // now, we have constrained nodes. So, push one of them (the one with min
83 // spill cost) and try to push the others as unConstrained nodes.
84 // Repeat this.
85
Chris Lattner75323bc2002-04-15 20:36:15 +000086 do {
Vikram S. Adve928833e2001-11-06 05:11:05 +000087 //get node with min spill cost
Ruchira Sasanka074d52d2001-11-06 15:25:38 +000088 IGNode *IGNodeSpill = getIGNodeWithMinSpillCost();
Vikram S. Adve928833e2001-11-06 05:11:05 +000089 // push that node on to stack
Chris Lattner75323bc2002-04-15 20:36:15 +000090 IGNodeStack.push(IGNodeSpill);
Vikram S. Adve928833e2001-11-06 05:11:05 +000091 // set its OnStack flag and decrement degree of neighs
92 IGNodeSpill->pushOnStack();
Misha Brukmanacda7df2003-09-11 22:34:13 +000093 // now push NON-constrained ones, if any
Chris Lattner75323bc2002-04-15 20:36:15 +000094 NeedMoreSpills = !pushUnconstrainedIGNodes();
Vikram S. Adve0e56b362002-09-14 23:05:33 +000095 if (DEBUG_RA >= RA_DEBUG_Coloring)
Chris Lattner71270652003-09-01 20:05:47 +000096 std::cerr << "\nConstrained IG Node found !@!" << IGNodeSpill->getIndex();
Chris Lattner75323bc2002-04-15 20:36:15 +000097 } while(NeedMoreSpills); // repeat until we have pushed all
Ruchira Sasanka808568e2001-09-14 21:18:34 +000098
99}
100
101
102
103
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000104//--------------------------------------------------------------------------
105// This method goes thru all IG nodes in the IGNodeList of an IG of a
106// register class and push any unconstrained IG node left (that is not
107// already pushed)
108//--------------------------------------------------------------------------
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000109
110bool RegClass::pushUnconstrainedIGNodes()
111{
112 // # of LRs for this reg class
113 unsigned int IGNodeListSize = IG.getIGNodeList().size();
114 bool pushedall = true;
115
116 // a pass over IGNodeList
Misha Brukman88df8762003-10-23 18:10:02 +0000117 for (unsigned i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000118
119 // get IGNode i from IGNodeList
120 IGNode *IGNode = IG.getIGNodeList()[i];
121
Misha Brukman88df8762003-10-23 18:10:02 +0000122 if (!IGNode ) // can be null due to merging
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000123 continue;
124
125 // if already pushed on stack, continue. This can happen since this
126 // method can be called repeatedly until all constrained nodes are
127 // pushed
Misha Brukman88df8762003-10-23 18:10:02 +0000128 if (IGNode->isOnStack() )
Ruchira Sasanka074d52d2001-11-06 15:25:38 +0000129 continue;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000130 // if the degree of IGNode is lower
Misha Brukman88df8762003-10-23 18:10:02 +0000131 if ((unsigned) IGNode->getCurDegree() < MRC->getNumOfAvailRegs()) {
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000132 IGNodeStack.push( IGNode ); // push IGNode on to the stack
133 IGNode->pushOnStack(); // set OnStack and dec deg of neighs
134
Vikram S. Adve0e56b362002-09-14 23:05:33 +0000135 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000136 std::cerr << " pushed un-constrained IGNode " << IGNode->getIndex()
137 << " on to stack\n";
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000138 }
139 }
140 else pushedall = false; // we didn't push all live ranges
141
142 } // for
143
144 // returns true if we pushed all live ranges - else false
145 return pushedall;
146}
147
148
149
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000150//----------------------------------------------------------------------------
Misha Brukmanacda7df2003-09-11 22:34:13 +0000151// Get the IGNode with the minimum spill cost
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000152//----------------------------------------------------------------------------
Misha Brukman88df8762003-10-23 18:10:02 +0000153IGNode * RegClass::getIGNodeWithMinSpillCost() {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000154 unsigned int IGNodeListSize = IG.getIGNodeList().size();
Chris Lattner5ae3bd62002-10-22 23:34:11 +0000155 double MinSpillCost = 0;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000156 IGNode *MinCostIGNode = NULL;
Ruchira Sasankabc284552002-01-08 16:29:23 +0000157 bool isFirstNode = true;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000158
159 // pass over IGNodeList to find the IGNode with minimum spill cost
160 // among all IGNodes that are not yet pushed on to the stack
Misha Brukman88df8762003-10-23 18:10:02 +0000161 for (unsigned int i =0; i < IGNodeListSize; i++) {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000162 IGNode *IGNode = IG.getIGNodeList()[i];
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000163
Misha Brukman88df8762003-10-23 18:10:02 +0000164 if (!IGNode) // can be null due to merging
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000165 continue;
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000166
Misha Brukman88df8762003-10-23 18:10:02 +0000167 if (!IGNode->isOnStack()) {
Ruchira Sasankabc284552002-01-08 16:29:23 +0000168 double SpillCost = (double) IGNode->getParentLR()->getSpillCost() /
169 (double) (IGNode->getCurDegree() + 1);
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000170
Misha Brukman88df8762003-10-23 18:10:02 +0000171 if (isFirstNode) { // for the first IG node
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000172 MinSpillCost = SpillCost;
173 MinCostIGNode = IGNode;
Ruchira Sasankabc284552002-01-08 16:29:23 +0000174 isFirstNode = false;
Misha Brukman88df8762003-10-23 18:10:02 +0000175 } else if (MinSpillCost > SpillCost) {
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000176 MinSpillCost = SpillCost;
177 MinCostIGNode = IGNode;
178 }
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000179 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000180 }
181
Misha Brukman88df8762003-10-23 18:10:02 +0000182 assert (MinCostIGNode && "No IGNode to spill");
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000183 return MinCostIGNode;
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000184}
185
186
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000187//----------------------------------------------------------------------------
188// Color the IGNode using the machine specific code.
189//----------------------------------------------------------------------------
Misha Brukman88df8762003-10-23 18:10:02 +0000190void RegClass::colorIGNode(IGNode *const Node) {
191 if (! Node->hasColor()) { // not colored as an arg etc.
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000192
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000193 // init all elements of to IsColorUsedAr false;
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000194 clearColorsUsed();
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000195
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000196 // initialize all colors used by neighbors of this node to true
197 LiveRange *LR = Node->getParentLR();
198 unsigned NumNeighbors = Node->getNumOfNeighbors();
199 for (unsigned n=0; n < NumNeighbors; n++) {
200 IGNode *NeighIGNode = Node->getAdjIGNode(n);
201 LiveRange *NeighLR = NeighIGNode->getParentLR();
202
Misha Brukmanacda7df2003-09-11 22:34:13 +0000203 // Don't use a color if it is in use by the neighbor,
204 // or is suggested for use by the neighbor,
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000205 // markColorsUsed() should be given the color and the reg type for
206 // LR, not for NeighLR, because it should mark registers used based on
207 // the type we are looking for, not on the regType for the neighbour.
208 if (NeighLR->hasColor())
209 this->markColorsUsed(NeighLR->getColor(),
210 MRI->getRegTypeForLR(NeighLR),
211 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
212 else if (NeighLR->hasSuggestedColor() &&
213 NeighLR->isSuggestedColorUsable())
214 this->markColorsUsed(NeighLR->getSuggestedColor(),
215 MRI->getRegTypeForLR(NeighLR),
216 MRI->getRegTypeForLR(LR)); // use LR, not NeighLR
Vikram S. Adve2780d2d2002-05-19 15:29:31 +0000217 }
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000218
Ruchira Sasanka8c2d8252002-01-07 19:19:18 +0000219 // call the target specific code for coloring
220 //
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000221 MRC->colorIGNode(Node, IsColorUsedArr);
Misha Brukman88df8762003-10-23 18:10:02 +0000222 } else {
223 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000224 std::cerr << " Node " << Node->getIndex();
225 std::cerr << " already colored with color " << Node->getColor() << "\n";
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000226 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000227 }
228
229
Misha Brukman88df8762003-10-23 18:10:02 +0000230 if (!Node->hasColor() ) {
231 if (DEBUG_RA >= RA_DEBUG_Coloring) {
Chris Lattner71270652003-09-01 20:05:47 +0000232 std::cerr << " Node " << Node->getIndex();
233 std::cerr << " - could not find a color (needs spilling)\n";
Ruchira Sasanka6fd95322001-09-15 19:06:58 +0000234 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000235 }
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000236}
237
Chris Lattner5abe44b2002-10-29 16:51:05 +0000238void RegClass::printIGNodeList() const {
239 std::cerr << "IG Nodes for Register Class " << RegClassID << ":" << "\n";
240 IG.printIGNodeList();
241}
242
243void RegClass::printIG() {
244 std::cerr << "IG for Register Class " << RegClassID << ":" << "\n";
245 IG.printIG();
246}
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000247
Ruchira Sasanka808568e2001-09-14 21:18:34 +0000248