Compute lists of sub-regs, super-regs, and overlapping regs.
[oota-llvm.git] / utils / TableGen / CodeGenRegisters.h
1 //===- CodeGenRegisters.h - Register and RegisterClass Info -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines structures to encapsulate information gleaned from the
11 // target register and register class definitions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef CODEGEN_REGISTERS_H
16 #define CODEGEN_REGISTERS_H
17
18 #include "Record.h"
19 #include "llvm/CodeGen/ValueTypes.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SetVector.h"
22 #include <cstdlib>
23 #include <map>
24 #include <string>
25 #include <set>
26 #include <vector>
27
28 namespace llvm {
29   class CodeGenRegBank;
30
31   /// CodeGenRegister - Represents a register definition.
32   struct CodeGenRegister {
33     Record *TheDef;
34     unsigned EnumValue;
35     unsigned CostPerUse;
36
37     // Map SubRegIndex -> Register.
38     typedef std::map<Record*, CodeGenRegister*, LessRecord> SubRegMap;
39
40     CodeGenRegister(Record *R, unsigned Enum);
41
42     const std::string &getName() const;
43
44     // Get a map of sub-registers computed lazily.
45     // This includes unique entries for all sub-sub-registers.
46     const SubRegMap &getSubRegs(CodeGenRegBank&);
47
48     const SubRegMap &getSubRegs() const {
49       assert(SubRegsComplete && "Must precompute sub-registers");
50       return SubRegs;
51     }
52
53     // Add sub-registers to OSet following a pre-order defined by the .td file.
54     void addSubRegsPreOrder(SetVector<CodeGenRegister*> &OSet) const;
55
56     // List of super-registers in topological order, small to large.
57     typedef std::vector<CodeGenRegister*> SuperRegList;
58
59     // Get the list of super-registers.
60     // This is only valid after computeDerivedInfo has visited all registers.
61     const SuperRegList &getSuperRegs() const {
62       assert(SubRegsComplete && "Must precompute sub-registers");
63       return SuperRegs;
64     }
65
66     // Order CodeGenRegister pointers by EnumValue.
67     struct Less {
68       bool operator()(const CodeGenRegister *A, const CodeGenRegister *B) {
69         return A->EnumValue < B->EnumValue;
70       }
71     };
72
73     // Canonically ordered set.
74     typedef std::set<CodeGenRegister*, Less> Set;
75
76   private:
77     bool SubRegsComplete;
78     SubRegMap SubRegs;
79     SuperRegList SuperRegs;
80   };
81
82
83   struct CodeGenRegisterClass {
84     Record *TheDef;
85     std::string Namespace;
86     std::vector<Record*> Elements;
87     std::vector<MVT::SimpleValueType> VTs;
88     unsigned SpillSize;
89     unsigned SpillAlignment;
90     int CopyCost;
91     bool Allocatable;
92     // Map SubRegIndex -> RegisterClass
93     DenseMap<Record*,Record*> SubRegClasses;
94     std::string MethodProtos, MethodBodies;
95
96     const std::string &getName() const;
97     const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
98     unsigned getNumValueTypes() const { return VTs.size(); }
99
100     MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
101       if (VTNum < VTs.size())
102         return VTs[VTNum];
103       assert(0 && "VTNum greater than number of ValueTypes in RegClass!");
104       abort();
105     }
106
107     bool containsRegister(Record *R) const {
108       for (unsigned i = 0, e = Elements.size(); i != e; ++i)
109         if (Elements[i] == R) return true;
110       return false;
111     }
112
113     // Returns true if RC is a strict subclass.
114     // RC is a sub-class of this class if it is a valid replacement for any
115     // instruction operand where a register of this classis required. It must
116     // satisfy these conditions:
117     //
118     // 1. All RC registers are also in this.
119     // 2. The RC spill size must not be smaller than our spill size.
120     // 3. RC spill alignment must be compatible with ours.
121     //
122     bool hasSubClass(const CodeGenRegisterClass *RC) const {
123
124       if (RC->Elements.size() > Elements.size() ||
125           (SpillAlignment && RC->SpillAlignment % SpillAlignment) ||
126           SpillSize > RC->SpillSize)
127         return false;
128
129       std::set<Record*> RegSet;
130       for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
131         Record *Reg = Elements[i];
132         RegSet.insert(Reg);
133       }
134
135       for (unsigned i = 0, e = RC->Elements.size(); i != e; ++i) {
136         Record *Reg = RC->Elements[i];
137         if (!RegSet.count(Reg))
138           return false;
139       }
140
141       return true;
142     }
143
144     CodeGenRegisterClass(Record *R);
145   };
146
147   // CodeGenRegBank - Represent a target's registers and the relations between
148   // them.
149   class CodeGenRegBank {
150     RecordKeeper &Records;
151     std::vector<Record*> SubRegIndices;
152     unsigned NumNamedIndices;
153     std::vector<CodeGenRegister> Registers;
154     DenseMap<Record*, CodeGenRegister*> Def2Reg;
155
156     // Composite SubRegIndex instances.
157     // Map (SubRegIndex, SubRegIndex) -> SubRegIndex.
158     typedef DenseMap<std::pair<Record*, Record*>, Record*> CompositeMap;
159     CompositeMap Composite;
160
161     // Populate the Composite map from sub-register relationships.
162     void computeComposites();
163
164   public:
165     CodeGenRegBank(RecordKeeper&);
166
167     // Sub-register indices. The first NumNamedIndices are defined by the user
168     // in the .td files. The rest are synthesized such that all sub-registers
169     // have a unique name.
170     const std::vector<Record*> &getSubRegIndices() { return SubRegIndices; }
171     unsigned getNumNamedIndices() { return NumNamedIndices; }
172
173     // Map a SubRegIndex Record to its enum value.
174     unsigned getSubRegIndexNo(Record *idx);
175
176     // Find or create a sub-register index representing the A+B composition.
177     Record *getCompositeSubRegIndex(Record *A, Record *B, bool create = false);
178
179     const std::vector<CodeGenRegister> &getRegisters() { return Registers; }
180
181     // Find a register from its Record def.
182     CodeGenRegister *getReg(Record*);
183
184     // Computed derived records such as missing sub-register indices.
185     void computeDerivedInfo();
186
187     // Compute full overlap sets for every register. These sets include the
188     // rarely used aliases that are neither sub nor super-registers.
189     //
190     // Map[R1].count(R2) is reflexive and symmetric, but not transitive.
191     //
192     // If R1 is a sub-register of R2, Map[R1] is a subset of Map[R2].
193     void computeOverlaps(std::map<const CodeGenRegister*,
194                                   CodeGenRegister::Set> &Map);
195   };
196 }
197
198 #endif