blob: e1e2992a69d8c4d17b787950696d319c6beade17 [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
Chris Lattner2182c782002-02-04 05:52:08 +000029class LiveRange : public ValueSet {
Ruchira Sasankac7136d22001-09-08 14:10:34 +000030 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
31
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000032 bool doesSpanAcrossCalls;
33 //
34 // Does this live range span across calls?
Ruchira Sasanka36f77072001-10-19 17:21:59 +000035 // This information is used by graph
36 // coloring algo to avoid allocating volatile colors to live ranges
37 // that span across calls (since they have to be saved/restored)
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000038
Ruchira Sasankac7136d22001-09-08 14:10:34 +000039
40 IGNode *UserIGNode; // IGNode which uses this LR
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000041
Ruchira Sasankac7136d22001-09-08 14:10:34 +000042 int Color; // color assigned to this live range
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000043
Ruchira Sasankac7136d22001-09-08 14:10:34 +000044 bool mustSpill; // whether this LR must be spilt
45
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000046
Ruchira Sasankac7136d22001-09-08 14:10:34 +000047 bool mustSaveAcrossCalls;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000048 //
49 // whether this LR must be saved accross calls ***TODO REMOVE this
50
Ruchira Sasankaab304c42001-09-30 23:19:57 +000051 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000052 //
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000053 // if this LR has a suggested color, can it be really alloated?
54 // A suggested color cannot be allocated when the suggested color is
55 // volatile and when there are call interferences.
56
57 bool CanUseSuggestedCol;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000058 //
59 // It is possible that a suggested color for this live range is not
60 // available before graph coloring (e.g., it can be allocated to another
61 // live range which interferes with this)
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000062
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000063 int SpilledStackOffsetFromFP;
64 //
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000065 // if this LR is spilled, its stack offset from *FP*. The spilled offsets
66 // must always be relative to the FP.
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000067
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000068 bool HasSpillOffset;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000069 //
70 // Whether this live range has a spill offset
71
72 unsigned SpillCost;
73 //
74 // The spill cost of this live range. Calculated using loop depth of
75 // each reference to each Value in the live range
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000076
Ruchira Sasankac7136d22001-09-08 14:10:34 +000077 public:
78
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000079 // constructor
80 //
81 LiveRange() : ValueSet() {
82 Color = SuggestedColor = -1; // not yet colored
83 mustSpill = mustSaveAcrossCalls = false;
84 MyRegClass = NULL;
85 UserIGNode = NULL;
86 doesSpanAcrossCalls = false;
87 CanUseSuggestedCol = true;
88 HasSpillOffset = false;
89 SpillCost = 0;
90 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000091
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000092 // empty destructor since there are nothing to be deleted
93 //
94 ~LiveRange() {}
95
Ruchira Sasankac7136d22001-09-08 14:10:34 +000096
97 void setRegClass(RegClass *const RC)
98 { MyRegClass = RC; }
99
100 inline RegClass *const getRegClass() const
101 { assert(MyRegClass); return MyRegClass; }
102
103 inline bool hasColor() const
104 { return Color != -1; }
Ruchira Sasanka36f77072001-10-19 17:21:59 +0000105
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000106 inline unsigned int getColor() const
107 { assert( Color != -1); return (unsigned) Color ; }
108
109 inline void setColor(unsigned int Col)
110 { Color = (int) Col ; }
111
112
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000113 inline void setCallInterference() {
114 doesSpanAcrossCalls = 1;
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000115 }
116
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000117
118 inline bool isCallInterference() const {
119 return (doesSpanAcrossCalls == 1);
120 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000121
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000122 inline void markForSpill() { mustSpill = true; }
123
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000124 inline bool isMarkedForSpill() { return mustSpill; }
125
126 inline void setSpillOffFromFP(int StackOffset) {
127 assert( mustSpill && "This LR is not spilled");
128 SpilledStackOffsetFromFP = StackOffset;
129 HasSpillOffset = true;
130 }
131
132 inline void modifySpillOffFromFP(int StackOffset) {
133 assert( mustSpill && "This LR is not spilled");
134 SpilledStackOffsetFromFP = StackOffset;
135 HasSpillOffset = true;
136 }
137
138
139
Ruchira Sasanka825dd552001-11-15 20:22:37 +0000140 inline bool hasSpillOffset() const {
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000141 return HasSpillOffset;
142 }
143
144
145 inline int getSpillOffFromFP() const {
146 assert( HasSpillOffset && "This LR is not spilled");
147 return SpilledStackOffsetFromFP;
148 }
149
150
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000151 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
152
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000153
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000154 inline void setUserIGNode( IGNode *const IGN)
155 { assert( !UserIGNode); UserIGNode = IGN; }
156
157 inline IGNode * getUserIGNode() const
158 { return UserIGNode; } // NULL if the user is not allocated
159
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000160 inline const Type* getType() const {
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000161 const Value *val = *begin();
162 assert(val && "Can't find type - Live range is empty" );
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000163 return val->getType();
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000164 }
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000165
166 inline Type::PrimitiveID getTypeID() const {
Chris Lattner2182c782002-02-04 05:52:08 +0000167 return getType()->getPrimitiveID();
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000168 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000169
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000170 inline void setSuggestedColor(int Col) {
171 //assert( (SuggestedColor == -1) && "Changing an already suggested color");
172
173 if(SuggestedColor == -1 )
174 SuggestedColor = Col;
Chris Lattner2182c782002-02-04 05:52:08 +0000175#if 0
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000176 else if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000177 std::cerr << "Already has a suggested color " << Col << "\n";
Chris Lattner2182c782002-02-04 05:52:08 +0000178#endif
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000179 }
180
181 inline unsigned getSuggestedColor() const {
182 assert( SuggestedColor != -1); // only a valid color is obtained
183 return (unsigned) SuggestedColor;
184 }
185
186 inline bool hasSuggestedColor() const {
187 return ( SuggestedColor > -1);
188 }
189
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000190 inline bool isSuggestedColorUsable() const {
191 assert( hasSuggestedColor() && "No suggested color");
192 return CanUseSuggestedCol;
193 }
194
195 inline void setSuggestedColorUsable(const bool val) {
196 assert( hasSuggestedColor() && "No suggested color");
197 CanUseSuggestedCol = val;
198 }
199
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000200 inline void addSpillCost(unsigned cost) {
201 SpillCost += cost;
202 }
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000203
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000204 inline unsigned getSpillCost() const {
205 return SpillCost;
206 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000207};
208
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000209#endif