blob: aa409c63fbcc2fd508b7d849694e4369a7456f45 [file] [log] [blame]
Chris Lattner52de05c2003-06-22 03:08:05 +00001//===-- LiveRange.h - Store info about a live range -------------*- C++ -*-===//
John Criswell29265fe2003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner30e8fb62002-02-05 01:43:49 +00009//
10// Implements a live range using a ValueSet. A LiveRange is a simple set
11// of Values.
12//
13// Since the Value pointed by a use is the same as of its def, it is sufficient
14// to keep only defs in a LiveRange.
15//
16//===----------------------------------------------------------------------===//
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000017
Brian Gaeke3a0a5fc2003-09-21 02:31:37 +000018#ifndef LIVERANGE_H
19#define LIVERANGE_H
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000020
Chris Lattnerf1223ac2002-02-05 03:51:37 +000021#include "llvm/Value.h"
Misha Brukman0b624fe2003-10-23 18:03:50 +000022#include "llvm/CodeGen/ValueSet.h"
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000023
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000024class RegClass;
25class IGNode;
26
Chris Lattnerb0da8b22002-02-04 05:52:08 +000027class LiveRange : public ValueSet {
Misha Brukman0b624fe2003-10-23 18:03:50 +000028 RegClass *MyRegClass; // register class (e.g., int, FP) for this LR
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000029
Misha Brukman0b624fe2003-10-23 18:03:50 +000030 /// doesSpanAcrossCalls - Does this live range span across calls?
31 /// This information is used by graph coloring algo to avoid allocating
32 /// volatile colors to live ranges that span across calls (since they have to
33 /// be saved/restored)
34 ///
Chris Lattner30e8fb62002-02-05 01:43:49 +000035 bool doesSpanAcrossCalls;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000036
37 IGNode *UserIGNode; // IGNode which uses this LR
38 int Color; // color assigned to this live range
39 bool mustSpill; // whether this LR must be spilt
40
Misha Brukman0b624fe2003-10-23 18:03:50 +000041 /// mustSaveAcrossCalls - whether this LR must be saved accross calls
42 /// ***TODO REMOVE this
43 ///
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000044 bool mustSaveAcrossCalls;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000045
Misha Brukman0b624fe2003-10-23 18:03:50 +000046 /// SuggestedColor - if this LR has a suggested color, can it be
47 /// really alloated? A suggested color cannot be allocated when the
48 /// suggested color is volatile and when there are call
49 /// interferences.
50 ///
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +000051 int SuggestedColor; // The suggested color for this LR
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000052
Misha Brukman0b624fe2003-10-23 18:03:50 +000053 /// CanUseSuggestedCol - It is possible that a suggested color for
54 /// this live range is not available before graph coloring (e.g., it
55 /// can be allocated to another live range which interferes with
56 /// this)
57 ///
Chris Lattner30e8fb62002-02-05 01:43:49 +000058 bool CanUseSuggestedCol;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000059
Misha Brukman0b624fe2003-10-23 18:03:50 +000060 /// SpilledStackOffsetFromFP - If this LR is spilled, its stack
61 /// offset from *FP*. The spilled offsets must always be relative to
62 /// the FP.
63 ///
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000064 int SpilledStackOffsetFromFP;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000065
Misha Brukman0b624fe2003-10-23 18:03:50 +000066 /// HasSpillOffset 0 Whether this live range has a spill offset
67 ///
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +000068 bool HasSpillOffset;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000069
Misha Brukman0b624fe2003-10-23 18:03:50 +000070 /// The spill cost of this live range. Calculated using loop depth of
71 /// each reference to each Value in the live range
72 ///
Chris Lattner30e8fb62002-02-05 01:43:49 +000073 unsigned SpillCost;
74
75public:
76 LiveRange() {
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000077 Color = SuggestedColor = -1; // not yet colored
78 mustSpill = mustSaveAcrossCalls = false;
Chris Lattner30e8fb62002-02-05 01:43:49 +000079 MyRegClass = 0;
80 UserIGNode = 0;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000081 doesSpanAcrossCalls = false;
82 CanUseSuggestedCol = true;
Chris Lattner30e8fb62002-02-05 01:43:49 +000083 HasSpillOffset = false;
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000084 SpillCost = 0;
85 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000086
Chris Lattner30e8fb62002-02-05 01:43:49 +000087 void setRegClass(RegClass *RC) { MyRegClass = RC; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000088
Chris Lattner30e8fb62002-02-05 01:43:49 +000089 RegClass *getRegClass() const { assert(MyRegClass); return MyRegClass; }
Chris Lattnerc75dc482003-01-15 20:28:36 +000090 unsigned getRegClassID() const;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000091
Chris Lattner30e8fb62002-02-05 01:43:49 +000092 bool hasColor() const { return Color != -1; }
Ruchira Sasanka6275a042001-10-19 17:21:59 +000093
Chris Lattner30e8fb62002-02-05 01:43:49 +000094 unsigned getColor() const { assert(Color != -1); return (unsigned)Color; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000095
Chris Lattner30e8fb62002-02-05 01:43:49 +000096 void setColor(unsigned Col) { Color = (int)Col; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +000097
Ruchira Sasanka53516cd2001-10-19 21:42:06 +000098 inline void setCallInterference() {
99 doesSpanAcrossCalls = 1;
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000100 }
Vikram S. Advec533f042002-03-31 18:58:14 +0000101 inline void clearCallInterference() {
102 doesSpanAcrossCalls = 0;
103 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000104
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000105 inline bool isCallInterference() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000106 return doesSpanAcrossCalls == 1;
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000107 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000108
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000109 inline void markForSpill() { mustSpill = true; }
110
Vikram S. Adveabf331d2003-07-10 19:45:28 +0000111 inline bool isMarkedForSpill() const { return mustSpill; }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000112
113 inline void setSpillOffFromFP(int StackOffset) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000114 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000115 SpilledStackOffsetFromFP = StackOffset;
116 HasSpillOffset = true;
117 }
118
119 inline void modifySpillOffFromFP(int StackOffset) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000120 assert(mustSpill && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000121 SpilledStackOffsetFromFP = StackOffset;
122 HasSpillOffset = true;
123 }
124
Ruchira Sasankadec9bfd2001-11-15 20:22:37 +0000125 inline bool hasSpillOffset() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000126 return HasSpillOffset;
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000127 }
128
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000129 inline int getSpillOffFromFP() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000130 assert(HasSpillOffset && "This LR is not spilled");
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000131 return SpilledStackOffsetFromFP;
132 }
133
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000134 inline void markForSaveAcrossCalls() { mustSaveAcrossCalls = true; }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000135
Chris Lattner30e8fb62002-02-05 01:43:49 +0000136 inline void setUserIGNode(IGNode *IGN) {
137 assert(!UserIGNode); UserIGNode = IGN;
138 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000139
Chris Lattner30e8fb62002-02-05 01:43:49 +0000140 // getUserIGNode - NULL if the user is not allocated
141 inline IGNode *getUserIGNode() const { return UserIGNode; }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000142
Chris Lattner30e8fb62002-02-05 01:43:49 +0000143 inline const Type *getType() const {
144 return (*begin())->getType(); // set's don't have a front
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000145 }
Vikram S. Advebdbb8022001-11-08 04:49:52 +0000146
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000147 inline void setSuggestedColor(int Col) {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000148 if (SuggestedColor == -1)
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000149 SuggestedColor = Col;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000150 }
151
152 inline unsigned getSuggestedColor() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000153 assert(SuggestedColor != -1); // only a valid color is obtained
154 return (unsigned)SuggestedColor;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000155 }
156
157 inline bool hasSuggestedColor() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000158 return SuggestedColor != -1;
Ruchira Sasanka560b0ad2001-09-30 23:19:57 +0000159 }
160
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000161 inline bool isSuggestedColorUsable() const {
Chris Lattner30e8fb62002-02-05 01:43:49 +0000162 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000163 return CanUseSuggestedCol;
164 }
165
Chris Lattner30e8fb62002-02-05 01:43:49 +0000166 inline void setSuggestedColorUsable(bool val) {
167 assert(hasSuggestedColor() && "No suggested color");
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000168 CanUseSuggestedCol = val;
169 }
170
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000171 inline void addSpillCost(unsigned cost) {
172 SpillCost += cost;
173 }
Ruchira Sasanka53516cd2001-10-19 21:42:06 +0000174
Ruchira Sasankaf20079d2002-01-07 19:16:26 +0000175 inline unsigned getSpillCost() const {
176 return SpillCost;
177 }
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000178};
179
Ruchira Sasankae5d0fb82001-09-08 14:10:34 +0000180#endif