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