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