blob: 6c2952af6b9dbad56a406a8af4b70e7c508c1319 [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"
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
Chris Lattner2182c782002-02-04 05:52:08 +000021class LiveRange : public ValueSet {
Ruchira Sasankac7136d22001-09-08 14:10:34 +000022 RegClass *MyRegClass; // register classs (e.g., int, FP) for this LR
23
Chris Lattner0665a5f2002-02-05 01:43:49 +000024 // doesSpanAcrossCalls - Does this live range span across calls?
Ruchira Sasanka36f77072001-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 Lattner0665a5f2002-02-05 01:43:49 +000028 //
29 bool doesSpanAcrossCalls;
Ruchira Sasankac7136d22001-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 Lattner0665a5f2002-02-05 01:43:49 +000035 // mustSaveAcrossCalls - whether this LR must be saved accross calls
36 // ***TODO REMOVE this
37 //
Ruchira Sasankac7136d22001-09-08 14:10:34 +000038 bool mustSaveAcrossCalls;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000039
Chris Lattner0665a5f2002-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 Sasankaab304c42001-09-30 23:19:57 +000045 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000046
Chris Lattner0665a5f2002-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 Sasanka42bd1772002-01-07 19:16:26 +000051 //
Chris Lattner0665a5f2002-02-05 01:43:49 +000052 bool CanUseSuggestedCol;
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000053
Chris Lattner0665a5f2002-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 Sasanka42bd1772002-01-07 19:16:26 +000058 int SpilledStackOffsetFromFP;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000059
Chris Lattner0665a5f2002-02-05 01:43:49 +000060 // HasSpillOffset 0 Whether this live range has a spill offset
61 //
Ruchira Sasanka20c82b12001-10-28 18:15:12 +000062 bool HasSpillOffset;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000063
Ruchira Sasanka42bd1772002-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 Sasanka42bd1772002-01-07 19:16:26 +000066 //
Chris Lattner0665a5f2002-02-05 01:43:49 +000067 unsigned SpillCost;
68
69public:
70 LiveRange() {
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000071 Color = SuggestedColor = -1; // not yet colored
72 mustSpill = mustSaveAcrossCalls = false;
Chris Lattner0665a5f2002-02-05 01:43:49 +000073 MyRegClass = 0;
74 UserIGNode = 0;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000075 doesSpanAcrossCalls = false;
76 CanUseSuggestedCol = true;
Chris Lattner0665a5f2002-02-05 01:43:49 +000077 HasSpillOffset = false;
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000078 SpillCost = 0;
79 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000080
Chris Lattner0665a5f2002-02-05 01:43:49 +000081 void setRegClass(RegClass *RC) { MyRegClass = RC; }
Ruchira Sasanka42bd1772002-01-07 19:16:26 +000082
Chris Lattner0665a5f2002-02-05 01:43:49 +000083 RegClass *getRegClass() const { assert(MyRegClass); return MyRegClass; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000084
Chris Lattner0665a5f2002-02-05 01:43:49 +000085 bool hasColor() const { return Color != -1; }
Ruchira Sasanka36f77072001-10-19 17:21:59 +000086
Chris Lattner0665a5f2002-02-05 01:43:49 +000087 unsigned getColor() const { assert(Color != -1); return (unsigned)Color; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000088
Chris Lattner0665a5f2002-02-05 01:43:49 +000089 void setColor(unsigned Col) { Color = (int)Col; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000090
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000091 inline void setCallInterference() {
92 doesSpanAcrossCalls = 1;
Ruchira Sasankac7136d22001-09-08 14:10:34 +000093 }
94
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000095 inline bool isCallInterference() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +000096 return doesSpanAcrossCalls == 1;
Ruchira Sasanka44d2b942001-10-19 21:42:06 +000097 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +000098
Ruchira Sasankac7136d22001-09-08 14:10:34 +000099 inline void markForSpill() { mustSpill = true; }
100
Chris Lattner0665a5f2002-02-05 01:43:49 +0000101 inline bool isMarkedForSpill() { return mustSpill; }
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000102
103 inline void setSpillOffFromFP(int StackOffset) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000104 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000105 SpilledStackOffsetFromFP = StackOffset;
106 HasSpillOffset = true;
107 }
108
109 inline void modifySpillOffFromFP(int StackOffset) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000110 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000111 SpilledStackOffsetFromFP = StackOffset;
112 HasSpillOffset = true;
113 }
114
Ruchira Sasanka825dd552001-11-15 20:22:37 +0000115 inline bool hasSpillOffset() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000116 return HasSpillOffset;
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000117 }
118
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000119 inline int getSpillOffFromFP() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000120 assert(HasSpillOffset && "This LR is not spilled");
Ruchira Sasanka20c82b12001-10-28 18:15:12 +0000121 return SpilledStackOffsetFromFP;
122 }
123
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000124 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000125
Chris Lattner0665a5f2002-02-05 01:43:49 +0000126 inline void setUserIGNode(IGNode *IGN) {
127 assert(!UserIGNode); UserIGNode = IGN;
128 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000129
Chris Lattner0665a5f2002-02-05 01:43:49 +0000130 // getUserIGNode - NULL if the user is not allocated
131 inline IGNode *getUserIGNode() const { return UserIGNode; }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000132
Chris Lattner0665a5f2002-02-05 01:43:49 +0000133 inline const Type *getType() const {
134 return (*begin())->getType(); // set's don't have a front
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000135 }
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000136
137 inline Type::PrimitiveID getTypeID() const {
Chris Lattner2182c782002-02-04 05:52:08 +0000138 return getType()->getPrimitiveID();
Vikram S. Adve38f5d462001-11-08 04:49:52 +0000139 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000140
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000141 inline void setSuggestedColor(int Col) {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000142 if (SuggestedColor == -1)
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000143 SuggestedColor = Col;
Chris Lattner2182c782002-02-04 05:52:08 +0000144#if 0
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000145 else if (DEBUG_RA)
Chris Lattner697954c2002-01-20 22:54:45 +0000146 std::cerr << "Already has a suggested color " << Col << "\n";
Chris Lattner2182c782002-02-04 05:52:08 +0000147#endif
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000148 }
149
150 inline unsigned getSuggestedColor() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000151 assert(SuggestedColor != -1); // only a valid color is obtained
152 return (unsigned)SuggestedColor;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000153 }
154
155 inline bool hasSuggestedColor() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000156 return SuggestedColor != -1;
Ruchira Sasankaab304c42001-09-30 23:19:57 +0000157 }
158
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000159 inline bool isSuggestedColorUsable() const {
Chris Lattner0665a5f2002-02-05 01:43:49 +0000160 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000161 return CanUseSuggestedCol;
162 }
163
Chris Lattner0665a5f2002-02-05 01:43:49 +0000164 inline void setSuggestedColorUsable(bool val) {
165 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000166 CanUseSuggestedCol = val;
167 }
168
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000169 inline void addSpillCost(unsigned cost) {
170 SpillCost += cost;
171 }
Ruchira Sasanka44d2b942001-10-19 21:42:06 +0000172
Ruchira Sasanka42bd1772002-01-07 19:16:26 +0000173 inline unsigned getSpillCost() const {
174 return SpillCost;
175 }
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000176};
177
Ruchira Sasankac7136d22001-09-08 14:10:34 +0000178#endif