1 //===-- RegClass.h - Machine Independent register coloring ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 /* Title: RegClass.h -*- C++ -*-
11 Author: Ruchira Sasanka
13 Purpose: Contains machine independent methods for register coloring.
20 #include "llvm/Target/TargetRegInfo.h"
21 #include "InterferenceGraph.h"
23 class TargetRegClassInfo;
26 //-----------------------------------------------------------------------------
29 // Implements a machine independent register class.
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
34 // dependent description of machine registers (TargetRegInfo class) to
35 // get hardware specific info and to color an individual IG node.
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.
42 //-----------------------------------------------------------------------------
44 const Function *const Meth; // Function we are working on
45 const TargetRegInfo *MRI; // Machine register information
46 const TargetRegClassInfo *const MRC; // Machine reg. class for this RegClass
47 const unsigned RegClassID; // my int ID
49 InterferenceGraph IG; // Interference graph - constructed by
50 // buildInterferenceGraph
51 std::stack<IGNode *> IGNodeStack; // the stack used for coloring
53 // IsColorUsedArr - An array used for coloring each node. This array must be
54 // of size MRC->getNumOfAllRegs(). Allocated once in the constructor for
57 std::vector<bool> IsColorUsedArr;
61 //--------------------------- private methods ------------------------------
63 void pushAllIGNodes();
65 bool pushUnconstrainedIGNodes();
67 IGNode * getIGNodeWithMinSpillCost();
69 void colorIGNode(IGNode *const Node);
71 // 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;
79 inline bool isColorUsed(unsigned regNum) const {
80 assert(regNum < IsColorUsedArr.size() && "Invalid register used?");
81 return IsColorUsedArr[regNum];
86 RegClass(const Function *M,
87 const TargetRegInfo *_MRI_,
88 const TargetRegClassInfo *_MRC_);
90 inline void createInterferenceGraph() { IG.createGraph(); }
92 inline InterferenceGraph &getIG() { return IG; }
94 inline const unsigned getID() const { return RegClassID; }
96 inline const TargetRegClassInfo* getTargetRegClass() const { return MRC; }
98 // main method called for coloring regs
102 inline unsigned getNumOfAvailRegs() const
103 { return MRC->getNumOfAvailRegs(); }
106 // --- following methods are provided to access the IG contained within this
107 // ---- RegClass easilly.
109 inline void addLRToIG(LiveRange *const LR)
110 { IG.addLRToIG(LR); }
112 inline void setInterference(const LiveRange *const LR1,
113 const LiveRange *const LR2)
114 { IG.setInterference(LR1, LR2); }
116 inline unsigned getInterference(const LiveRange *const LR1,
117 const LiveRange *const LR2) const
118 { return IG.getInterference(LR1, LR2); }
120 inline void mergeIGNodesOfLRs(const LiveRange *const LR1,
121 LiveRange *const LR2)
122 { IG.mergeIGNodesOfLRs(LR1, LR2); }
125 inline void clearColorsUsed() {
126 IsColorUsedArr.clear();
127 IsColorUsedArr.resize(MRC->getNumOfAllRegs());
129 inline void markColorsUsed(unsigned ClassRegNum,
132 MRC->markColorsUsed(ClassRegNum, UserRegType, RegTypeWanted,IsColorUsedArr);
134 inline int getUnusedColor(int machineRegType) const {
135 return MRC->findUnusedColor(machineRegType, IsColorUsedArr);
138 void printIGNodeList() const;