CodeGenRegisters.h revision 221345
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 "llvm/CodeGen/ValueTypes.h"
19#include "llvm/ADT/DenseMap.h"
20#include <string>
21#include <vector>
22#include <set>
23#include <cstdlib>
24
25namespace llvm {
26  class Record;
27
28  /// CodeGenRegister - Represents a register definition.
29  struct CodeGenRegister {
30    Record *TheDef;
31    const std::string &getName() const;
32    unsigned EnumValue;
33    unsigned CostPerUse;
34    CodeGenRegister(Record *R);
35  };
36
37
38  struct CodeGenRegisterClass {
39    Record *TheDef;
40    std::string Namespace;
41    std::vector<Record*> Elements;
42    std::vector<MVT::SimpleValueType> VTs;
43    unsigned SpillSize;
44    unsigned SpillAlignment;
45    int CopyCost;
46    // Map SubRegIndex -> RegisterClass
47    DenseMap<Record*,Record*> SubRegClasses;
48    std::string MethodProtos, MethodBodies;
49
50    const std::string &getName() const;
51    const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
52    unsigned getNumValueTypes() const { return VTs.size(); }
53
54    MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
55      if (VTNum < VTs.size())
56        return VTs[VTNum];
57      assert(0 && "VTNum greater than number of ValueTypes in RegClass!");
58      abort();
59    }
60
61    bool containsRegister(Record *R) const {
62      for (unsigned i = 0, e = Elements.size(); i != e; ++i)
63        if (Elements[i] == R) return true;
64      return false;
65    }
66
67    // Returns true if RC is a strict subclass.
68    // RC is a sub-class of this class if it is a valid replacement for any
69    // instruction operand where a register of this classis required. It must
70    // satisfy these conditions:
71    //
72    // 1. All RC registers are also in this.
73    // 2. The RC spill size must not be smaller than our spill size.
74    // 3. RC spill alignment must be compatible with ours.
75    //
76    bool hasSubClass(const CodeGenRegisterClass *RC) const {
77
78      if (RC->Elements.size() > Elements.size() ||
79          (SpillAlignment && RC->SpillAlignment % SpillAlignment) ||
80          SpillSize > RC->SpillSize)
81        return false;
82
83      std::set<Record*> RegSet;
84      for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
85        Record *Reg = Elements[i];
86        RegSet.insert(Reg);
87      }
88
89      for (unsigned i = 0, e = RC->Elements.size(); i != e; ++i) {
90        Record *Reg = RC->Elements[i];
91        if (!RegSet.count(Reg))
92          return false;
93      }
94
95      return true;
96    }
97
98    CodeGenRegisterClass(Record *R);
99  };
100}
101
102#endif
103