blob: 2d00c826dfa3ebb83d458f5fa5ab390b7337daec [file] [log] [blame]
Chris Lattner52de05c2003-06-22 03:08:05 +00001//===-- LiveRange.h - Store info about a live range -------------*- C++ -*-===//
Chris Lattner30e8fb62002-02-05 01:43:49 +00002//
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
Brian Gaeke3a0a5fc2003-09-21 02:31:37 +000011#ifndef LIVERANGE_H
12#define LIVERANGE_H
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000013
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
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000017class RegClass;
18class IGNode;
19
Chris Lattnerb0da8b22002-02-04 05:52:08 +000020class LiveRange : public ValueSet {
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000021 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
22
Chris Lattner30e8fb62002-02-05 01:43:49 +000023 // doesSpanAcrossCalls - Does this live range span across calls?
Ruchira Sasanka6275a042001-10-19 17:21:59 +000024 // This information is used by graph
25 // coloring algo to avoid allocating volatile colors to live ranges
26 // that span across calls (since they have to be saved/restored)
Chris Lattner30e8fb62002-02-05 01:43:49 +000027 //
28 bool doesSpanAcrossCalls;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000029
30 IGNode *UserIGNode; // IGNode which uses this LR
31 int Color; // color assigned to this live range
32 bool mustSpill; // whether this LR must be spilt
33
Chris Lattner30e8fb62002-02-05 01:43:49 +000034 // mustSaveAcrossCalls - whether this LR must be saved accross calls
35 // ***TODO REMOVE this
36 //
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000037 bool mustSaveAcrossCalls;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000038
Chris Lattner30e8fb62002-02-05 01:43:49 +000039 // SuggestedColor - if this LR has a suggested color, can it be
40 // really alloated? A suggested color cannot be allocated when the
41 // suggested color is volatile and when there are call
42 // interferences.
43 //
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +000044 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000045
Chris Lattner30e8fb62002-02-05 01:43:49 +000046 // CanUseSuggestedCol - It is possible that a suggested color for
47 // this live range is not available before graph coloring (e.g., it
48 // can be allocated to another live range which interferes with
49 // this)
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000050 //
Chris Lattner30e8fb62002-02-05 01:43:49 +000051 bool CanUseSuggestedCol;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000052
Chris Lattner30e8fb62002-02-05 01:43:49 +000053 // SpilledStackOffsetFromFP - If this LR is spilled, its stack
54 // offset from *FP*. The spilled offsets must always be relative to
55 // the FP.
56 //
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000057 int SpilledStackOffsetFromFP;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000058
Chris Lattner30e8fb62002-02-05 01:43:49 +000059 // HasSpillOffset 0 Whether this live range has a spill offset
60 //
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000061 bool HasSpillOffset;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000062
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000063 // The spill cost of this live range. Calculated using loop depth of
64 // each reference to each Value in the live range
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000065 //
Chris Lattner30e8fb62002-02-05 01:43:49 +000066 unsigned SpillCost;
67
68public:
69 LiveRange() {
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000070 Color = SuggestedColor = -1; // not yet colored
71 mustSpill = mustSaveAcrossCalls = false;
Chris Lattner30e8fb62002-02-05 01:43:49 +000072 MyRegClass = 0;
73 UserIGNode = 0;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000074 doesSpanAcrossCalls = false;
75 CanUseSuggestedCol = true;
Chris Lattner30e8fb62002-02-05 01:43:49 +000076 HasSpillOffset = false;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000077 SpillCost = 0;
78 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000079
Chris Lattner30e8fb62002-02-05 01:43:49 +000080 void setRegClass(RegClass *RC) { MyRegClass = RC; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000081
Chris Lattner30e8fb62002-02-05 01:43:49 +000082 RegClass *getRegClass() const { assert(MyRegClass); return MyRegClass; }
Chris Lattnerc75dc482003-01-15 20:28:36 +000083 unsigned getRegClassID() const;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000084
Chris Lattner30e8fb62002-02-05 01:43:49 +000085 bool hasColor() const { return Color != -1; }
Ruchira Sasanka6275a042001-10-19 17:21:59 +000086
Chris Lattner30e8fb62002-02-05 01:43:49 +000087 unsigned getColor() const { assert(Color != -1); return (unsigned)Color; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000088
Chris Lattner30e8fb62002-02-05 01:43:49 +000089 void setColor(unsigned Col) { Color = (int)Col; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000090
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000091 inline void setCallInterference() {
92 doesSpanAcrossCalls = 1;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000093 }
Vikram S. Advec533f042002-03-31 18:58:14 +000094 inline void clearCallInterference() {
95 doesSpanAcrossCalls = 0;
96 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000097
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000098 inline bool isCallInterference() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +000099 return doesSpanAcrossCalls == 1;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000100 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000101
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000102 inline void markForSpill() { mustSpill = true; }
103
Vikram S. Adveabf331d2003-07-10 19:45:28 +0000104 inline bool isMarkedForSpill() const { return mustSpill; }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000105
106 inline void setSpillOffFromFP(int StackOffset) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000107 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000108 SpilledStackOffsetFromFP = StackOffset;
109 HasSpillOffset = true;
110 }
111
112 inline void modifySpillOffFromFP(int StackOffset) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000113 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000114 SpilledStackOffsetFromFP = StackOffset;
115 HasSpillOffset = true;
116 }
117
Ruchira Sasankadec9bfd2001-11-15 20:22:37 +0000118 inline bool hasSpillOffset() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000119 return HasSpillOffset;
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000120 }
121
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000122 inline int getSpillOffFromFP() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000123 assert(HasSpillOffset && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000124 return SpilledStackOffsetFromFP;
125 }
126
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000127 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000128
Chris Lattner30e8fb62002-02-05 01:43:49 +0000129 inline void setUserIGNode(IGNode *IGN) {
130 assert(!UserIGNode); UserIGNode = IGN;
131 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000132
Chris Lattner30e8fb62002-02-05 01:43:49 +0000133 // getUserIGNode - NULL if the user is not allocated
134 inline IGNode *getUserIGNode() const { return UserIGNode; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000135
Chris Lattner30e8fb62002-02-05 01:43:49 +0000136 inline const Type *getType() const {
137 return (*begin())->getType(); // set's don't have a front
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000138 }
Vikram S. Advebdbb8022001-11-08 04:49:52 +0000139
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000140 inline void setSuggestedColor(int Col) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000141 if (SuggestedColor == -1)
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000142 SuggestedColor = Col;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000143 }
144
145 inline unsigned getSuggestedColor() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000146 assert(SuggestedColor != -1); // only a valid color is obtained
147 return (unsigned)SuggestedColor;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000148 }
149
150 inline bool hasSuggestedColor() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000151 return SuggestedColor != -1;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000152 }
153
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000154 inline bool isSuggestedColorUsable() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000155 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000156 return CanUseSuggestedCol;
157 }
158
Chris Lattner30e8fb62002-02-05 01:43:49 +0000159 inline void setSuggestedColorUsable(bool val) {
160 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000161 CanUseSuggestedCol = val;
162 }
163
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000164 inline void addSpillCost(unsigned cost) {
165 SpillCost += cost;
166 }
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000167
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000168 inline unsigned getSpillCost() const {
169 return SpillCost;
170 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000171};
172
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000173#endif