blob: 8034751da999deed2a5e9fcfca4e67f8ce897e09 [file] [log] [blame]
Chris Lattner697954c2002-01-20 22:54:45 +00001/* Title: LiveRange.h -*- C++ -*-
Ruchira Sasankac7136d22001-09-08 14:10:34 +00002 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"
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
Ruchira Sasankac7136d22001-09-08 14:10:34 +000017
Ruchira Sasankac7136d22001-09-08 14:10:34 +000018class RegClass;
19class IGNode;
20
21
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000022//----------------------------------------------------------------------------
23// Class LiveRange
24//
25// Implements a live range using a ValueSet. A LiveRange is a simple set
26// of Values.
27//----------------------------------------------------------------------------
28
Ruchira Sasankac7136d22001-09-08 14:10:34 +000029class LiveRange : public ValueSet
30{
31 private:
32
33 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
34
Ruchira Sasanka36f77072001-10-19 17:21:59 +000035
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000036 bool doesSpanAcrossCalls;
37 //
38 // Does this live range span across calls?
Ruchira Sasanka36f77072001-10-19 17:21:59 +000039 // 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)
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000042
Ruchira Sasankac7136d22001-09-08 14:10:34 +000043
44 IGNode *UserIGNode; // IGNode which uses this LR
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000045
Ruchira Sasankac7136d22001-09-08 14:10:34 +000046 int Color; // color assigned to this live range
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000047
Ruchira Sasankac7136d22001-09-08 14:10:34 +000048 bool mustSpill; // whether this LR must be spilt
49
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000050
Ruchira Sasankac7136d22001-09-08 14:10:34 +000051 bool mustSaveAcrossCalls;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000052 //
53 // whether this LR must be saved accross calls ***TODO REMOVE this
54
Ruchira Sasankaab304c42001-09-30 23:19:57 +000055 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000056 //
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000057 // 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;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000062 //
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)
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000066
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000067 int SpilledStackOffsetFromFP;
68 //
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000069 // if this LR is spilled, its stack offset from *FP*. The spilled offsets
70 // must always be relative to the FP.
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000071
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000072 bool HasSpillOffset;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000073 //
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
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000080
Ruchira Sasankac7136d22001-09-08 14:10:34 +000081 public:
82
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000083 // 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 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000095
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000096 // empty destructor since there are nothing to be deleted
97 //
98 ~LiveRange() {}
99
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000100
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; }
Ruchira Sasanka36f77072001-10-19 17:21:59 +0000109
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000110 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
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000117 inline void setCallInterference() {
118 doesSpanAcrossCalls = 1;
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000119 }
120
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000121
122 inline bool isCallInterference() const {
123 return (doesSpanAcrossCalls == 1);
124 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000125
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000126 inline void markForSpill() { mustSpill = true; }
127
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000128 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
Ruchira Sasanka825dd552001-11-15 20:22:37 +0000144 inline bool hasSpillOffset() const {
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000145 return HasSpillOffset;
146 }
147
148
149 inline int getSpillOffFromFP() const {
150 assert( HasSpillOffset && "This LR is not spilled");
151 return SpilledStackOffsetFromFP;
152 }
153
154
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000155 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
156
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000157
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000158 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
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000164 inline const Type* getType() const {
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000165 const Value *val = *begin();
166 assert(val && "Can't find type - Live range is empty" );
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000167 return val->getType();
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000168 }
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000169
170 inline Type::PrimitiveID getTypeID() const {
171 return this->getType()->getPrimitiveID();
172 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000173
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000174 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)
Chris Lattner697954c2002-01-20 22:54:45 +0000180 std::cerr << "Already has a suggested color " << Col << "\n";
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000181 }
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
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000192 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
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000202 inline void addSpillCost(unsigned cost) {
203 SpillCost += cost;
204 }
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000205
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000206 inline unsigned getSpillCost() const {
207 return SpillCost;
208 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000209
210};
211
212
213
214
215
216#endif
217