blob: deab75d07462b63143a25d557d84d46317ed52bb [file] [log] [blame]
Chris Lattner30e8fb62002-02-05 01:43:49 +00001//===-- 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//===----------------------------------------------------------------------===//
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000010
11#ifndef LIVE_RANGE_H
12#define LIVE_RANGE_H
13
Chris Lattnerde1d7292003-01-14 22:56:37 +000014#include "llvm/CodeGen/ValueSet.h"
Chris Lattnerf1223ac2002-02-05 03:51:37 +000015#include "llvm/Value.h"
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000016
John Criswellcfac7362003-06-11 14:01:36 +000017#include <assert.h>
18
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000019class RegClass;
20class IGNode;
Chris Lattnerf1223ac2002-02-05 03:51:37 +000021class Type;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000022
Chris Lattnerb0da8b22002-02-04 05:52:08 +000023class LiveRange : public ValueSet {
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000024 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
25
Chris Lattner30e8fb62002-02-05 01:43:49 +000026 // doesSpanAcrossCalls - Does this live range span across calls?
Ruchira Sasanka6275a042001-10-19 17:21:59 +000027 // This information is used by graph
28 // coloring algo to avoid allocating volatile colors to live ranges
29 // that span across calls (since they have to be saved/restored)
Chris Lattner30e8fb62002-02-05 01:43:49 +000030 //
31 bool doesSpanAcrossCalls;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000032
33 IGNode *UserIGNode; // IGNode which uses this LR
34 int Color; // color assigned to this live range
35 bool mustSpill; // whether this LR must be spilt
36
Chris Lattner30e8fb62002-02-05 01:43:49 +000037 // mustSaveAcrossCalls - whether this LR must be saved accross calls
38 // ***TODO REMOVE this
39 //
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000040 bool mustSaveAcrossCalls;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000041
Chris Lattner30e8fb62002-02-05 01:43:49 +000042 // SuggestedColor - if this LR has a suggested color, can it be
43 // really alloated? A suggested color cannot be allocated when the
44 // suggested color is volatile and when there are call
45 // interferences.
46 //
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +000047 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000048
Chris Lattner30e8fb62002-02-05 01:43:49 +000049 // CanUseSuggestedCol - It is possible that a suggested color for
50 // this live range is not available before graph coloring (e.g., it
51 // can be allocated to another live range which interferes with
52 // this)
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000053 //
Chris Lattner30e8fb62002-02-05 01:43:49 +000054 bool CanUseSuggestedCol;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000055
Chris Lattner30e8fb62002-02-05 01:43:49 +000056 // SpilledStackOffsetFromFP - If this LR is spilled, its stack
57 // offset from *FP*. The spilled offsets must always be relative to
58 // the FP.
59 //
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000060 int SpilledStackOffsetFromFP;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000061
Chris Lattner30e8fb62002-02-05 01:43:49 +000062 // HasSpillOffset 0 Whether this live range has a spill offset
63 //
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000064 bool HasSpillOffset;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000065
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000066 // The spill cost of this live range. Calculated using loop depth of
67 // each reference to each Value in the live range
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000068 //
Chris Lattner30e8fb62002-02-05 01:43:49 +000069 unsigned SpillCost;
70
71public:
72 LiveRange() {
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000073 Color = SuggestedColor = -1; // not yet colored
74 mustSpill = mustSaveAcrossCalls = false;
Chris Lattner30e8fb62002-02-05 01:43:49 +000075 MyRegClass = 0;
76 UserIGNode = 0;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000077 doesSpanAcrossCalls = false;
78 CanUseSuggestedCol = true;
Chris Lattner30e8fb62002-02-05 01:43:49 +000079 HasSpillOffset = false;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000080 SpillCost = 0;
81 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000082
Chris Lattner30e8fb62002-02-05 01:43:49 +000083 void setRegClass(RegClass *RC) { MyRegClass = RC; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000084
Chris Lattner30e8fb62002-02-05 01:43:49 +000085 RegClass *getRegClass() const { assert(MyRegClass); return MyRegClass; }
Chris Lattnerc75dc482003-01-15 20:28:36 +000086 unsigned getRegClassID() const;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000087
Chris Lattner30e8fb62002-02-05 01:43:49 +000088 bool hasColor() const { return Color != -1; }
Ruchira Sasanka6275a042001-10-19 17:21:59 +000089
Chris Lattner30e8fb62002-02-05 01:43:49 +000090 unsigned getColor() const { assert(Color != -1); return (unsigned)Color; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000091
Chris Lattner30e8fb62002-02-05 01:43:49 +000092 void setColor(unsigned Col) { Color = (int)Col; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000093
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000094 inline void setCallInterference() {
95 doesSpanAcrossCalls = 1;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000096 }
Vikram S. Advec533f042002-03-31 18:58:14 +000097 inline void clearCallInterference() {
98 doesSpanAcrossCalls = 0;
99 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000100
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000101 inline bool isCallInterference() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000102 return doesSpanAcrossCalls == 1;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000103 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000104
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000105 inline void markForSpill() { mustSpill = true; }
106
Chris Lattner30e8fb62002-02-05 01:43:49 +0000107 inline bool isMarkedForSpill() { return mustSpill; }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000108
109 inline void setSpillOffFromFP(int StackOffset) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000110 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000111 SpilledStackOffsetFromFP = StackOffset;
112 HasSpillOffset = true;
113 }
114
115 inline void modifySpillOffFromFP(int StackOffset) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000116 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000117 SpilledStackOffsetFromFP = StackOffset;
118 HasSpillOffset = true;
119 }
120
Ruchira Sasankadec9bfd2001-11-15 20:22:37 +0000121 inline bool hasSpillOffset() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000122 return HasSpillOffset;
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000123 }
124
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000125 inline int getSpillOffFromFP() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000126 assert(HasSpillOffset && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000127 return SpilledStackOffsetFromFP;
128 }
129
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000130 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000131
Chris Lattner30e8fb62002-02-05 01:43:49 +0000132 inline void setUserIGNode(IGNode *IGN) {
133 assert(!UserIGNode); UserIGNode = IGN;
134 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000135
Chris Lattner30e8fb62002-02-05 01:43:49 +0000136 // getUserIGNode - NULL if the user is not allocated
137 inline IGNode *getUserIGNode() const { return UserIGNode; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000138
Chris Lattner30e8fb62002-02-05 01:43:49 +0000139 inline const Type *getType() const {
140 return (*begin())->getType(); // set's don't have a front
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000141 }
Vikram S. Advebdbb8022001-11-08 04:49:52 +0000142
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000143 inline void setSuggestedColor(int Col) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000144 if (SuggestedColor == -1)
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000145 SuggestedColor = Col;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000146 }
147
148 inline unsigned getSuggestedColor() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000149 assert(SuggestedColor != -1); // only a valid color is obtained
150 return (unsigned)SuggestedColor;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000151 }
152
153 inline bool hasSuggestedColor() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000154 return SuggestedColor != -1;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000155 }
156
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000157 inline bool isSuggestedColorUsable() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000158 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000159 return CanUseSuggestedCol;
160 }
161
Chris Lattner30e8fb62002-02-05 01:43:49 +0000162 inline void setSuggestedColorUsable(bool val) {
163 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000164 CanUseSuggestedCol = val;
165 }
166
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000167 inline void addSpillCost(unsigned cost) {
168 SpillCost += cost;
169 }
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000170
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000171 inline unsigned getSpillCost() const {
172 return SpillCost;
173 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000174};
175
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000176#endif