blob: 778e070046e6b15c5426f39b3b1a62c85edbbefd [file] [log] [blame]
Ruchira Sasankac7136d22001-09-08 14:10:34 +00001/* 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
Ruchira Sasankac7136d22001-09-08 14:10:34 +000017class RegClass;
18class IGNode;
19
20
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000021//----------------------------------------------------------------------------
22// Class LiveRange
23//
24// Implements a live range using a ValueSet. A LiveRange is a simple set
25// of Values.
26//----------------------------------------------------------------------------
27
Ruchira Sasankac7136d22001-09-08 14:10:34 +000028class LiveRange : public ValueSet
29{
30 private:
31
32 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
33
Ruchira Sasanka36f77072001-10-19 17:21:59 +000034
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000035 bool doesSpanAcrossCalls;
36 //
37 // Does this live range span across calls?
Ruchira Sasanka36f77072001-10-19 17:21:59 +000038 // 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)
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000041
Ruchira Sasankac7136d22001-09-08 14:10:34 +000042
43 IGNode *UserIGNode; // IGNode which uses this LR
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000044
Ruchira Sasankac7136d22001-09-08 14:10:34 +000045 int Color; // color assigned to this live range
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000046
Ruchira Sasankac7136d22001-09-08 14:10:34 +000047 bool mustSpill; // whether this LR must be spilt
48
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000049
Ruchira Sasankac7136d22001-09-08 14:10:34 +000050 bool mustSaveAcrossCalls;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000051 //
52 // whether this LR must be saved accross calls ***TODO REMOVE this
53
Ruchira Sasankaab304c42001-09-30 23:19:57 +000054 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000055 //
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000056 // 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;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000061 //
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)
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000065
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000066 int SpilledStackOffsetFromFP;
67 //
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000068 // if this LR is spilled, its stack offset from *FP*. The spilled offsets
69 // must always be relative to the FP.
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000070
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000071 bool HasSpillOffset;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000072 //
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
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000079
Ruchira Sasankac7136d22001-09-08 14:10:34 +000080 public:
81
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000082 // 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 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000094
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000095 // empty destructor since there are nothing to be deleted
96 //
97 ~LiveRange() {}
98
Ruchira Sasankac7136d22001-09-08 14:10:34 +000099
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; }
Ruchira Sasanka36f77072001-10-19 17:21:59 +0000108
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000109 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
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000116 inline void setCallInterference() {
117 doesSpanAcrossCalls = 1;
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000118 }
119
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000120
121 inline bool isCallInterference() const {
122 return (doesSpanAcrossCalls == 1);
123 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000124
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000125 inline void markForSpill() { mustSpill = true; }
126
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000127 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
Ruchira Sasanka825dd552001-11-15 20:22:37 +0000143 inline bool hasSpillOffset() const {
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000144 return HasSpillOffset;
145 }
146
147
148 inline int getSpillOffFromFP() const {
149 assert( HasSpillOffset && "This LR is not spilled");
150 return SpilledStackOffsetFromFP;
151 }
152
153
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000154 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
155
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000156
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000157 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
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000163 inline const Type* getType() const {
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000164 const Value *val = *begin();
165 assert(val && "Can't find type - Live range is empty" );
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000166 return val->getType();
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000167 }
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000168
169 inline Type::PrimitiveID getTypeID() const {
170 return this->getType()->getPrimitiveID();
171 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000172
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000173 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)
Chris Lattner634b3522001-10-15 18:30:06 +0000179 cerr << "Already has a suggested color " << Col << endl;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000180 }
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
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000191 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
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000201 inline void addSpillCost(unsigned cost) {
202 SpillCost += cost;
203 }
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000204
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000205 inline unsigned getSpillCost() const {
206 return SpillCost;
207 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000208
209};
210
211
212
213
214
215#endif
216