blob: ddb70746fc5b689244cdd6386d67ac3d912dd99d [file] [log] [blame]
Chris Lattnerfeb62c32001-09-07 21:04:20 +00001/* Title: IGNode.h -*- C++ -*-
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +00002 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
Brian Gaeke73d10dc2003-09-21 02:31:15 +000012 the neighbors are decremented and this node is marked OnStack. Hence
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000013 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
Brian Gaeke73d10dc2003-09-21 02:31:15 +000016 The methods that modify/use the CurDegree must be called only
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000017 after all modifications to the IG are over (i.e., all neighbors are fixed).
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000018
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000019 The vector representation is the most efficient one for adj list.
Brian Gaeke73d10dc2003-09-21 02:31:15 +000020 Though nodes are removed when coalescing is done, we access it in sequence
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000021 for far many times when coloring (colorNode()).
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000022*/
23
Brian Gaeke73d10dc2003-09-21 02:31:15 +000024#ifndef IGNODE_H
25#define IGNODE_H
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000026
Chris Lattnereefb5652003-09-01 20:17:13 +000027#include "LiveRange.h"
Chris Lattner9177d112003-10-15 22:09:32 +000028#include <vector>
Chris Lattnerb0da8b22002-02-04 05:52:08 +000029class RegClass;
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000030
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000031//----------------------------------------------------------------------------
32// Class IGNode
33//
34// Represents a node in an interference graph.
35//----------------------------------------------------------------------------
36
Chris Lattnerb0da8b22002-02-04 05:52:08 +000037class IGNode {
Chris Lattner7e5ee422002-02-05 04:20:12 +000038 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
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000041
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000042 int CurDegree;
43 //
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000044 // 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.
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000048
Chris Lattner7e5ee422002-02-05 04:20:12 +000049 LiveRange *const ParentLR;
Chris Lattnerb0da8b22002-02-04 05:52:08 +000050public:
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000051
Chris Lattner7e5ee422002-02-05 04:20:12 +000052 IGNode(LiveRange *LR, unsigned index) : Index(index), ParentLR(LR) {
53 OnStack = false;
54 CurDegree = -1;
55 ParentLR->setUserIGNode(this);
56 }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000057
Chris Lattnerf1223ac2002-02-05 03:51:37 +000058 inline unsigned int getIndex() const { return Index; }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000059
60 // adjLists must be updated only once. However, the CurDegree can be changed
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000061 //
Chris Lattnerf1223ac2002-02-05 03:51:37 +000062 inline void addAdjIGNode(IGNode *AdjNode) { AdjList.push_back(AdjNode); }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000063
Chris Lattnerf1223ac2002-02-05 03:51:37 +000064 inline IGNode *getAdjIGNode(unsigned ind) const
65 { assert ( ind < AdjList.size()); return AdjList[ind]; }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000066
67 // delete a node in AdjList - node must be in the list
68 // should not be called often
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000069 //
Chris Lattnerf1223ac2002-02-05 03:51:37 +000070 void delAdjIGNode(const IGNode *Node);
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000071
Chris Lattnerf1223ac2002-02-05 03:51:37 +000072 inline unsigned getNumOfNeighbors() const { return AdjList.size(); }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000073
Vikram S. Adve5cc5d4b2002-09-20 00:55:04 +000074 // Get the number of unique neighbors if these two nodes are merged
75 unsigned getCombinedDegree(const IGNode* otherNode) const;
76
Chris Lattnerf1223ac2002-02-05 03:51:37 +000077 inline bool isOnStack() const { return OnStack; }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000078
79 // remove form IG and pushes on to stack (reduce the degree of neighbors)
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000080 //
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000081 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).
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000087 //
Chris Lattnerf1223ac2002-02-05 03:51:37 +000088 inline void setCurDegree() {
89 assert(CurDegree == -1);
90 CurDegree = AdjList.size();
91 }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000092
Chris Lattnerf1223ac2002-02-05 03:51:37 +000093 inline int getCurDegree() const { return CurDegree; }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000094
95 // called when a neigh is pushed on to stack
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000096 //
Chris Lattnerf1223ac2002-02-05 03:51:37 +000097 inline void decCurDegree() { assert(CurDegree > 0); --CurDegree; }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000098
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +000099 // 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
Chris Lattnerf1223ac2002-02-05 03:51:37 +0000103 inline bool hasColor() const { return ParentLR->hasColor(); }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +0000104
Chris Lattnerf1223ac2002-02-05 03:51:37 +0000105 inline unsigned int getColor() const { return ParentLR->getColor(); }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +0000106
Chris Lattnerf1223ac2002-02-05 03:51:37 +0000107 inline void setColor(unsigned Col) { ParentLR->setColor(Col); }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +0000108
Chris Lattnerf1223ac2002-02-05 03:51:37 +0000109 inline LiveRange *getParentLR() const { return ParentLR; }
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +0000110};
111
Ruchira Sasanka4d30f4b2001-08-31 20:59:58 +0000112#endif