blob: c861fbae210e48284cc2c86eed6e2635317ea8a6 [file] [log] [blame]
John Criswell9583cfa2003-10-21 15:29:18 +00001//===-- RegClass.h - Machine Independent register coloring ------*- C++ -*-===//
2//
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//===----------------------------------------------------------------------===//
9
Chris Lattner87b3bf62001-09-14 06:08:03 +000010/* Title: RegClass.h -*- C++ -*-
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000011 Author: Ruchira Sasanka
12 Date: Aug 20, 01
13 Purpose: Contains machine independent methods for register coloring.
14
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000015*/
16
Brian Gaeke3a0a5fc2003-09-21 02:31:37 +000017#ifndef REGCLASS_H
18#define REGCLASS_H
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000019
Chris Lattnerf9781b52002-12-29 03:13:05 +000020#include "llvm/Target/TargetRegInfo.h"
Chris Lattnere46165f2003-01-15 21:02:16 +000021#include "InterferenceGraph.h"
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000022#include <stack>
Chris Lattnerf9781b52002-12-29 03:13:05 +000023class TargetRegClassInfo;
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000024
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000025
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000026//-----------------------------------------------------------------------------
27// Class RegClass
28//
Misha Brukmanbe372b92003-08-21 22:14:26 +000029// Implements a machine independent register class.
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000030//
31// This is the class that contains all data structures and common algos
32// for coloring a particular register class (e.g., int class, fp class).
33// This class is hardware independent. This class accepts a hardware
Chris Lattnerf9781b52002-12-29 03:13:05 +000034// dependent description of machine registers (TargetRegInfo class) to
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000035// get hardware specific info and to color an individual IG node.
36//
37// This class contains the InterferenceGraph (IG).
38// Also it contains an IGNode stack that can be used for coloring.
39// The class provides some easy access methods to the IG methods, since these
40// methods are called thru a register class.
41//
42//-----------------------------------------------------------------------------
Chris Lattnerb0da8b22002-02-04 05:52:08 +000043class RegClass {
Chris Lattnerf739fa82002-04-08 22:03:57 +000044 const Function *const Meth; // Function we are working on
Vikram S. Adve45766ab2003-07-25 21:06:09 +000045 const TargetRegInfo *MRI; // Machine register information
46 const TargetRegClassInfo *const MRC; // Machine reg. class for this RegClass
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000047 const unsigned RegClassID; // my int ID
48
49 InterferenceGraph IG; // Interference graph - constructed by
50 // buildInterferenceGraph
Chris Lattner7f74a562002-01-20 22:54:45 +000051 std::stack<IGNode *> IGNodeStack; // the stack used for coloring
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000052
Chris Lattnerabe98192002-05-23 15:50:03 +000053 // IsColorUsedArr - An array used for coloring each node. This array must be
54 // of size MRC->getNumOfAllRegs(). Allocated once in the constructor for
55 // efficiency.
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000056 //
Chris Lattnerabe98192002-05-23 15:50:03 +000057 std::vector<bool> IsColorUsedArr;
58
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000059
60
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000061 //--------------------------- private methods ------------------------------
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000062
63 void pushAllIGNodes();
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000064
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000065 bool pushUnconstrainedIGNodes();
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000066
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000067 IGNode * getIGNodeWithMinSpillCost();
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000068
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000069 void colorIGNode(IGNode *const Node);
70
Vikram S. Adve45766ab2003-07-25 21:06:09 +000071 // This directly marks the colors used by a particular register number
72 // within the register class. External users should use the public
73 // versions of this function below.
74 inline void markColorUsed(unsigned classRegNum) {
75 assert(classRegNum < IsColorUsedArr.size() && "Invalid register used?");
76 IsColorUsedArr[classRegNum] = true;
77 }
78
79 inline bool isColorUsed(unsigned regNum) const {
80 assert(regNum < IsColorUsedArr.size() && "Invalid register used?");
81 return IsColorUsedArr[regNum];
82 }
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000083
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000084 public:
85
Chris Lattnerf739fa82002-04-08 22:03:57 +000086 RegClass(const Function *M,
Vikram S. Adve45766ab2003-07-25 21:06:09 +000087 const TargetRegInfo *_MRI_,
88 const TargetRegClassInfo *_MRC_);
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000089
Chris Lattnerf739fa82002-04-08 22:03:57 +000090 inline void createInterferenceGraph() { IG.createGraph(); }
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +000091
92 inline InterferenceGraph &getIG() { return IG; }
93
94 inline const unsigned getID() const { return RegClassID; }
95
Vikram S. Adve45766ab2003-07-25 21:06:09 +000096 inline const TargetRegClassInfo* getTargetRegClass() const { return MRC; }
97
Ruchira Sasankaf20079d2002-01-07 19:16:26 +000098 // main method called for coloring regs
99 //
100 void colorAllRegs();
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000101
102 inline unsigned getNumOfAvailRegs() const
103 { return MRC->getNumOfAvailRegs(); }
104
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000105
106 // --- following methods are provided to access the IG contained within this
107 // ---- RegClass easilly.
108
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000109 inline void addLRToIG(LiveRange *const LR)
110 { IG.addLRToIG(LR); }
111
112 inline void setInterference(const LiveRange *const LR1,
113 const LiveRange *const LR2)
114 { IG.setInterference(LR1, LR2); }
115
116 inline unsigned getInterference(const LiveRange *const LR1,
117 const LiveRange *const LR2) const
118 { return IG.getInterference(LR1, LR2); }
119
120 inline void mergeIGNodesOfLRs(const LiveRange *const LR1,
121 LiveRange *const LR2)
122 { IG.mergeIGNodesOfLRs(LR1, LR2); }
123
124
Vikram S. Adve45766ab2003-07-25 21:06:09 +0000125 inline void clearColorsUsed() {
126 IsColorUsedArr.clear();
127 IsColorUsedArr.resize(MRC->getNumOfAllRegs());
128 }
129 inline void markColorsUsed(unsigned ClassRegNum,
130 int UserRegType,
131 int RegTypeWanted) {
132 MRC->markColorsUsed(ClassRegNum, UserRegType, RegTypeWanted,IsColorUsedArr);
133 }
134 inline int getUnusedColor(int machineRegType) const {
135 return MRC->findUnusedColor(machineRegType, IsColorUsedArr);
136 }
Ruchira Sasanka9c38dbc2001-10-28 18:15:12 +0000137
Chris Lattner189c0992002-10-29 16:50:33 +0000138 void printIGNodeList() const;
139 void printIG();
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000140};
141
Ruchira Sasankaf5788aa2001-09-08 14:22:50 +0000142#endif