blob: 5f3e41bd15285f5405306796320deec8b267cc86 [file] [log] [blame]
Chris Lattner0665a5f2002-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 Sasankac7136d22001-09-08 14:10:34 +000010
11#ifndef LIVE_RANGE_H
12#define LIVE_RANGE_H
13
14#include "llvm/Analysis/LiveVar/ValueSet.h"
Chris Lattner569ea232002-02-05 03:51:37 +000015#include "llvm/Value.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;
Chris Lattner569ea232002-02-05 03:51:37 +000020class Type;
Ruchira Sasankac7136d22001-09-08 14:10:34 +000021
Chris Lattner2182c782002-02-04 05:52:08 +000022class LiveRange : public ValueSet {
Ruchira Sasankac7136d22001-09-08 14:10:34 +000023 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
24
Chris Lattner0665a5f2002-02-05 01:43:49 +000025 // doesSpanAcrossCalls - Does this live range span across calls?
Ruchira Sasanka36f77072001-10-19 17:21:59 +000026 // This information is used by graph
27 // coloring algo to avoid allocating volatile colors to live ranges
28 // that span across calls (since they have to be saved/restored)
Chris Lattner0665a5f2002-02-05 01:43:49 +000029 //
30 bool doesSpanAcrossCalls;
Ruchira Sasankac7136d22001-09-08 14:10:34 +000031
32 IGNode *UserIGNode; // IGNode which uses this LR
33 int Color; // color assigned to this live range
34 bool mustSpill; // whether this LR must be spilt
35
Chris Lattner0665a5f2002-02-05 01:43:49 +000036 // mustSaveAcrossCalls - whether this LR must be saved accross calls
37 // ***TODO REMOVE this
38 //
Ruchira Sasankac7136d22001-09-08 14:10:34 +000039 bool mustSaveAcrossCalls;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000040
Chris Lattner0665a5f2002-02-05 01:43:49 +000041 // SuggestedColor - if this LR has a suggested color, can it be
42 // really alloated? A suggested color cannot be allocated when the
43 // suggested color is volatile and when there are call
44 // interferences.
45 //
Ruchira Sasankaab304c42001-09-30 23:19:57 +000046 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000047
Chris Lattner0665a5f2002-02-05 01:43:49 +000048 // CanUseSuggestedCol - It is possible that a suggested color for
49 // this live range is not available before graph coloring (e.g., it
50 // can be allocated to another live range which interferes with
51 // this)
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000052 //
Chris Lattner0665a5f2002-02-05 01:43:49 +000053 bool CanUseSuggestedCol;
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000054
Chris Lattner0665a5f2002-02-05 01:43:49 +000055 // SpilledStackOffsetFromFP - If this LR is spilled, its stack
56 // offset from *FP*. The spilled offsets must always be relative to
57 // the FP.
58 //
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000059 int SpilledStackOffsetFromFP;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000060
Chris Lattner0665a5f2002-02-05 01:43:49 +000061 // HasSpillOffset 0 Whether this live range has a spill offset
62 //
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000063 bool HasSpillOffset;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000064
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000065 // The spill cost of this live range. Calculated using loop depth of
66 // each reference to each Value in the live range
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000067 //
Chris Lattner0665a5f2002-02-05 01:43:49 +000068 unsigned SpillCost;
69
70public:
71 LiveRange() {
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000072 Color = SuggestedColor = -1; // not yet colored
73 mustSpill = mustSaveAcrossCalls = false;
Chris Lattner0665a5f2002-02-05 01:43:49 +000074 MyRegClass = 0;
75 UserIGNode = 0;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000076 doesSpanAcrossCalls = false;
77 CanUseSuggestedCol = true;
Chris Lattner0665a5f2002-02-05 01:43:49 +000078 HasSpillOffset = false;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000079 SpillCost = 0;
80 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000081
Chris Lattner0665a5f2002-02-05 01:43:49 +000082 void setRegClass(RegClass *RC) { MyRegClass = RC; }
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000083
Chris Lattner0665a5f2002-02-05 01:43:49 +000084 RegClass *getRegClass() const { assert(MyRegClass); return MyRegClass; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000085
Chris Lattner0665a5f2002-02-05 01:43:49 +000086 bool hasColor() const { return Color != -1; }
Ruchira Sasanka36f77072001-10-19 17:21:59 +000087
Chris Lattner0665a5f2002-02-05 01:43:49 +000088 unsigned getColor() const { assert(Color != -1); return (unsigned)Color; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000089
Chris Lattner0665a5f2002-02-05 01:43:49 +000090 void setColor(unsigned Col) { Color = (int)Col; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000091
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000092 inline void setCallInterference() {
93 doesSpanAcrossCalls = 1;
Ruchira Sasankac7136d22001-09-08 14:10:34 +000094 }
95
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000096 inline bool isCallInterference() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +000097 return doesSpanAcrossCalls == 1;
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000098 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000099
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000100 inline void markForSpill() { mustSpill = true; }
101
Chris Lattner0665a5f2002-02-05 01:43:49 +0000102 inline bool isMarkedForSpill() { return mustSpill; }
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000103
104 inline void setSpillOffFromFP(int StackOffset) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000105 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000106 SpilledStackOffsetFromFP = StackOffset;
107 HasSpillOffset = true;
108 }
109
110 inline void modifySpillOffFromFP(int StackOffset) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000111 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000112 SpilledStackOffsetFromFP = StackOffset;
113 HasSpillOffset = true;
114 }
115
Ruchira Sasanka825dd552001-11-15 20:22:37 +0000116 inline bool hasSpillOffset() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000117 return HasSpillOffset;
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000118 }
119
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000120 inline int getSpillOffFromFP() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000121 assert(HasSpillOffset && "This LR is not spilled");
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000122 return SpilledStackOffsetFromFP;
123 }
124
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000125 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000126
Chris Lattner0665a5f2002-02-05 01:43:49 +0000127 inline void setUserIGNode(IGNode *IGN) {
128 assert(!UserIGNode); UserIGNode = IGN;
129 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000130
Chris Lattner0665a5f2002-02-05 01:43:49 +0000131 // getUserIGNode - NULL if the user is not allocated
132 inline IGNode *getUserIGNode() const { return UserIGNode; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000133
Chris Lattner0665a5f2002-02-05 01:43:49 +0000134 inline const Type *getType() const {
135 return (*begin())->getType(); // set's don't have a front
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000136 }
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000137
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000138 inline void setSuggestedColor(int Col) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000139 if (SuggestedColor == -1)
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000140 SuggestedColor = Col;
Chris Lattner2182c782002-02-04 05:52:08 +0000141#if 0
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000142 else if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000143 std::cerr << "Already has a suggested color " << Col << "\n";
Chris Lattner2182c782002-02-04 05:52:08 +0000144#endif
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000145 }
146
147 inline unsigned getSuggestedColor() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000148 assert(SuggestedColor != -1); // only a valid color is obtained
149 return (unsigned)SuggestedColor;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000150 }
151
152 inline bool hasSuggestedColor() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000153 return SuggestedColor != -1;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000154 }
155
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000156 inline bool isSuggestedColorUsable() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000157 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000158 return CanUseSuggestedCol;
159 }
160
Chris Lattner0665a5f2002-02-05 01:43:49 +0000161 inline void setSuggestedColorUsable(bool val) {
162 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000163 CanUseSuggestedCol = val;
164 }
165
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000166 inline void addSpillCost(unsigned cost) {
167 SpillCost += cost;
168 }
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000169
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000170 inline unsigned getSpillCost() const {
171 return SpillCost;
172 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000173};
174
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000175#endif