Added comments, destructors where necessary.
[oota-llvm.git] / lib / Target / SparcV9 / RegAlloc / LiveRange.h
1 /* Title:   LiveRange.h
2    Author:  Ruchira Sasanka
3    Date:    July 25, 01
4    Purpose: To keep info about a live range. 
5    Asuumptions:
6
7    Since the Value pointed by a use is the same as of its def, it is sufficient
8    to keep only defs in a LiveRange.
9 */
10
11 #ifndef LIVE_RANGE_H
12 #define LIVE_RANGE_H
13
14 #include "llvm/Analysis/LiveVar/ValueSet.h"
15 #include "llvm/Type.h"
16
17 class RegClass;
18 class IGNode;
19
20
21 //----------------------------------------------------------------------------
22 // Class LiveRange
23 //
24 // Implements a live range using a ValueSet. A LiveRange is a simple set
25 // of Values. 
26 //----------------------------------------------------------------------------
27
28 class LiveRange : public ValueSet
29 {
30  private:
31
32   RegClass *MyRegClass;       // register classs (e.g., int, FP) for this LR
33
34
35   bool doesSpanAcrossCalls;
36   //
37   // Does this live range span across calls? 
38   // This information is used by graph
39   // coloring algo to avoid allocating volatile colors to live ranges
40   // that span across calls (since they have to be saved/restored)
41   
42
43   IGNode *UserIGNode;         // IGNode which uses this LR
44
45   int Color;                  // color assigned to this live range
46
47   bool mustSpill;             // whether this LR must be spilt
48
49
50   bool mustSaveAcrossCalls;        
51   //
52   // whether this LR must be saved accross calls ***TODO REMOVE this
53   
54   int SuggestedColor;        // The suggested color for this LR
55   //
56   // if this LR has a suggested color, can it be really alloated?
57   // A suggested color cannot be allocated when the suggested color is
58   // volatile and when there are call interferences.
59
60   bool CanUseSuggestedCol;
61   // 
62   // It is possible that a suggested color for this live range is not
63   // available before graph coloring (e.g., it can be allocated to another
64   // live range which interferes with this)
65
66   int SpilledStackOffsetFromFP;
67   //
68   // if this LR is spilled, its stack offset from *FP*. The spilled offsets
69   // must always be relative to the FP.
70
71   bool HasSpillOffset;
72   //
73   // Whether this live range has a spill offset
74
75   unsigned SpillCost;
76   //
77   // The spill cost of this live range. Calculated using loop depth of
78   // each reference to each Value in the live range
79
80  public:
81
82   // constructor
83   //
84   LiveRange() : ValueSet() {
85     Color = SuggestedColor = -1;        // not yet colored 
86     mustSpill = mustSaveAcrossCalls = false;
87     MyRegClass = NULL;
88     UserIGNode = NULL;
89     doesSpanAcrossCalls = false;
90     CanUseSuggestedCol = true;
91     HasSpillOffset  = false;
92     SpillCost = 0;
93   }
94
95   // empty destructor since there are nothing to be deleted
96   //
97   ~LiveRange() {}          
98
99
100   void setRegClass(RegClass *const RC) 
101     { MyRegClass = RC; }
102
103   inline RegClass *const getRegClass() const 
104     { assert(MyRegClass); return MyRegClass; } 
105
106   inline bool hasColor() const 
107     { return Color != -1; }
108   
109   inline unsigned int getColor() const 
110     { assert( Color != -1); return (unsigned) Color ; }
111
112   inline void setColor(unsigned int Col) 
113     { Color = (int) Col ; }
114
115   
116   inline void setCallInterference() { 
117     doesSpanAcrossCalls = 1;
118   }
119
120
121   inline bool isCallInterference() const { 
122     return (doesSpanAcrossCalls == 1); 
123   } 
124
125   inline void markForSpill() { mustSpill = true; }
126
127   inline bool isMarkedForSpill() { return  mustSpill; }
128
129   inline void setSpillOffFromFP(int StackOffset) {
130     assert( mustSpill && "This LR is not spilled");
131     SpilledStackOffsetFromFP = StackOffset;
132     HasSpillOffset = true;
133   }
134
135   inline void modifySpillOffFromFP(int StackOffset) {
136     assert( mustSpill && "This LR is not spilled");
137     SpilledStackOffsetFromFP = StackOffset;
138     HasSpillOffset = true;
139   }
140
141
142
143   inline bool hasSpillOffset() const {
144     return  HasSpillOffset;
145   }
146
147
148   inline int getSpillOffFromFP() const {
149     assert( HasSpillOffset && "This LR is not spilled");
150     return SpilledStackOffsetFromFP;
151   }
152
153
154   inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
155
156   
157   inline void setUserIGNode( IGNode *const IGN) 
158     { assert( !UserIGNode); UserIGNode = IGN; }
159
160   inline IGNode * getUserIGNode() const 
161     { return UserIGNode; }    // NULL if the user is not allocated
162
163   inline const Type* getType() const {
164     const Value *val = *begin();
165     assert(val && "Can't find type - Live range is empty" );
166     return val->getType();
167   }
168   
169   inline Type::PrimitiveID getTypeID() const {
170     return this->getType()->getPrimitiveID();
171   }
172
173   inline void setSuggestedColor(int Col) {
174     //assert( (SuggestedColor == -1) && "Changing an already suggested color");
175
176     if(SuggestedColor == -1 )
177       SuggestedColor = Col;
178     else if (DEBUG_RA) 
179       cerr << "Already has a suggested color " << Col << endl;
180   }
181
182   inline unsigned getSuggestedColor() const {
183     assert( SuggestedColor != -1);      // only a valid color is obtained
184     return (unsigned) SuggestedColor;
185   }
186
187   inline bool hasSuggestedColor() const {
188     return ( SuggestedColor > -1);
189   }
190
191   inline bool isSuggestedColorUsable() const {
192     assert( hasSuggestedColor() && "No suggested color");
193     return CanUseSuggestedCol;
194   }
195
196   inline void setSuggestedColorUsable(const bool val) {
197     assert( hasSuggestedColor() && "No suggested color");
198     CanUseSuggestedCol = val;
199   }
200
201   inline void addSpillCost(unsigned cost) {
202     SpillCost += cost;
203   }
204
205   inline unsigned getSpillCost() const {
206     return SpillCost;
207   }
208
209 };
210
211
212
213
214
215 #endif
216