blob: 3a88a27fa1fff752d075c1935ee34d5d9a6f7b77 [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
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000017class RegClass;
18class IGNode;
Chris Lattnerf1223ac2002-02-05 03:51:37 +000019class Type;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000020
Chris Lattnerb0da8b22002-02-04 05:52:08 +000021class LiveRange : public ValueSet {
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000022 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
23
Chris Lattner30e8fb62002-02-05 01:43:49 +000024 // doesSpanAcrossCalls - Does this live range span across calls?
Ruchira Sasanka6275a042001-10-19 17:21:59 +000025 // This information is used by graph
26 // coloring algo to avoid allocating volatile colors to live ranges
27 // that span across calls (since they have to be saved/restored)
Chris Lattner30e8fb62002-02-05 01:43:49 +000028 //
29 bool doesSpanAcrossCalls;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000030
31 IGNode *UserIGNode; // IGNode which uses this LR
32 int Color; // color assigned to this live range
33 bool mustSpill; // whether this LR must be spilt
34
Chris Lattner30e8fb62002-02-05 01:43:49 +000035 // mustSaveAcrossCalls - whether this LR must be saved accross calls
36 // ***TODO REMOVE this
37 //
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000038 bool mustSaveAcrossCalls;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000039
Chris Lattner30e8fb62002-02-05 01:43:49 +000040 // SuggestedColor - if this LR has a suggested color, can it be
41 // really alloated? A suggested color cannot be allocated when the
42 // suggested color is volatile and when there are call
43 // interferences.
44 //
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +000045 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000046
Chris Lattner30e8fb62002-02-05 01:43:49 +000047 // CanUseSuggestedCol - It is possible that a suggested color for
48 // this live range is not available before graph coloring (e.g., it
49 // can be allocated to another live range which interferes with
50 // this)
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000051 //
Chris Lattner30e8fb62002-02-05 01:43:49 +000052 bool CanUseSuggestedCol;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000053
Chris Lattner30e8fb62002-02-05 01:43:49 +000054 // SpilledStackOffsetFromFP - If this LR is spilled, its stack
55 // offset from *FP*. The spilled offsets must always be relative to
56 // the FP.
57 //
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000058 int SpilledStackOffsetFromFP;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000059
Chris Lattner30e8fb62002-02-05 01:43:49 +000060 // HasSpillOffset 0 Whether this live range has a spill offset
61 //
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000062 bool HasSpillOffset;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000063
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000064 // The spill cost of this live range. Calculated using loop depth of
65 // each reference to each Value in the live range
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000066 //
Chris Lattner30e8fb62002-02-05 01:43:49 +000067 unsigned SpillCost;
68
69public:
70 LiveRange() {
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000071 Color = SuggestedColor = -1; // not yet colored
72 mustSpill = mustSaveAcrossCalls = false;
Chris Lattner30e8fb62002-02-05 01:43:49 +000073 MyRegClass = 0;
74 UserIGNode = 0;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000075 doesSpanAcrossCalls = false;
76 CanUseSuggestedCol = true;
Chris Lattner30e8fb62002-02-05 01:43:49 +000077 HasSpillOffset = false;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000078 SpillCost = 0;
79 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000080
Chris Lattner30e8fb62002-02-05 01:43:49 +000081 void setRegClass(RegClass *RC) { MyRegClass = RC; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000082
Chris Lattner30e8fb62002-02-05 01:43:49 +000083 RegClass *getRegClass() const { assert(MyRegClass); return MyRegClass; }
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
Chris Lattner30e8fb62002-02-05 01:43:49 +0000104 inline bool isMarkedForSpill() { 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