ddb70746fc5b689244cdd6386d67ac3d912dd99d
[oota-llvm.git] / lib / Target / SparcV9 / RegAlloc / IGNode.h
1 /* Title:   IGNode.h                      -*- C++ -*-
2    Author:  Ruchira Sasanka
3    Date:    July 25, 01
4    Purpose: Represents a node in an interference graph. 
5    Notes:
6
7    For efficiency, the AdjList is updated only once - ie. we can add but not
8    remove nodes from AdjList. 
9
10    The removal of nodes from IG is simulated by decrementing the CurDegree.
11    If this node is put on stack (that is removed from IG), the CurDegree of all
12    the neighbors are decremented and this node is marked OnStack. Hence
13    the effective neighbors in the AdjList are the ones that do not have the
14    OnStack flag set (therefore, they are in the IG).
15
16    The methods that modify/use the CurDegree must be called only
17    after all modifications to the IG are over (i.e., all neighbors are fixed).
18
19    The vector representation is the most efficient one for adj list.
20    Though nodes are removed when coalescing is done, we access it in sequence
21    for far many times when coloring (colorNode()).
22 */
23
24 #ifndef IGNODE_H
25 #define IGNODE_H
26
27 #include "LiveRange.h"
28 #include <vector>
29 class RegClass;
30
31 //----------------------------------------------------------------------------
32 // Class IGNode
33 //
34 // Represents a node in an interference graph.
35 //----------------------------------------------------------------------------
36
37 class IGNode {
38   const unsigned Index;         // index within IGNodeList 
39   bool OnStack;                 // this has been pushed on to stack for coloring
40   std::vector<IGNode *> AdjList;// adjacency list for this live range
41
42   int CurDegree;     
43   //
44   // set by InterferenceGraph::setCurDegreeOfIGNodes() after calculating
45   // all adjacency lists.
46   // Decremented when a neighbor is pushed on to the stack. 
47   // After that, never incremented/set again nor used.
48
49   LiveRange *const ParentLR;
50 public:
51
52   IGNode(LiveRange *LR, unsigned index) : Index(index), ParentLR(LR) {
53     OnStack = false;
54     CurDegree = -1;
55     ParentLR->setUserIGNode(this);
56   }
57
58   inline unsigned int getIndex() const { return Index; }
59
60   // adjLists must be updated only once.  However, the CurDegree can be changed
61   //
62   inline void addAdjIGNode(IGNode *AdjNode) { AdjList.push_back(AdjNode);  } 
63
64   inline IGNode *getAdjIGNode(unsigned ind) const 
65     { assert ( ind < AdjList.size()); return AdjList[ind]; }
66
67   // delete a node in AdjList - node must be in the list
68   // should not be called often
69   //
70   void delAdjIGNode(const IGNode *Node); 
71
72   inline unsigned getNumOfNeighbors() const { return AdjList.size(); }
73
74   // Get the number of unique neighbors if these two nodes are merged
75   unsigned getCombinedDegree(const IGNode* otherNode) const;
76
77   inline bool isOnStack() const { return OnStack; }
78
79   // remove form IG and pushes on to stack (reduce the degree of neighbors)
80   //
81   void pushOnStack(); 
82
83   // CurDegree is the effective number of neighbors when neighbors are
84   // pushed on to the stack during the coloring phase. Must be called
85   // after all modifications to the IG are over (i.e., all neighbors are
86   // fixed).
87   //
88   inline void setCurDegree() {
89     assert(CurDegree == -1);
90     CurDegree = AdjList.size();
91   }
92
93   inline int getCurDegree() const { return CurDegree; }
94
95   // called when a neigh is pushed on to stack
96   //
97   inline void decCurDegree() { assert(CurDegree > 0); --CurDegree; }
98
99   // The following methods call the methods in ParentLR
100   // They are added to this class for convenience
101   // If many of these are called within a single scope,
102   // consider calling the methods directly on LR
103   inline bool hasColor() const { return ParentLR->hasColor();  }
104
105   inline unsigned int getColor() const { return ParentLR->getColor();  }
106
107   inline void setColor(unsigned Col) { ParentLR->setColor(Col);  }
108
109   inline LiveRange *getParentLR() const { return ParentLR; }
110 };
111
112 #endif