CodeGenRegisters.h revision 218893
115755Swosch//===- CodeGenRegisters.h - Register and RegisterClass Info -----*- C++ -*-===//
215755Swosch//
315755Swosch//                     The LLVM Compiler Infrastructure
415755Swosch//
515755Swosch// This file is distributed under the University of Illinois Open Source
615755Swosch// License. See LICENSE.TXT for details.
715755Swosch//
815755Swosch//===----------------------------------------------------------------------===//
915755Swosch//
1015755Swosch// This file defines structures to encapsulate information gleaned from the
1115755Swosch// target register and register class definitions.
1215755Swosch//
1315755Swosch//===----------------------------------------------------------------------===//
1415755Swosch
1515755Swosch#ifndef CODEGEN_REGISTERS_H
1615755Swosch#define CODEGEN_REGISTERS_H
1715755Swosch
1815755Swosch#include "llvm/CodeGen/ValueTypes.h"
1915755Swosch#include "llvm/ADT/DenseMap.h"
2015755Swosch#include <string>
2115755Swosch#include <vector>
2215755Swosch#include <set>
2315755Swosch#include <cstdlib>
2415755Swosch
2515755Swoschnamespace llvm {
2615755Swosch  class Record;
2715755Swosch
2815755Swosch  /// CodeGenRegister - Represents a register definition.
2915755Swosch  struct CodeGenRegister {
3015755Swosch    Record *TheDef;
3115755Swosch    const std::string &getName() const;
3215755Swosch    unsigned DeclaredSpillSize, DeclaredSpillAlignment;
3315755Swosch    CodeGenRegister(Record *R);
3415755Swosch  };
3515755Swosch
36
37  struct CodeGenRegisterClass {
38    Record *TheDef;
39    std::string Namespace;
40    std::vector<Record*> Elements;
41    std::vector<MVT::SimpleValueType> VTs;
42    unsigned SpillSize;
43    unsigned SpillAlignment;
44    int CopyCost;
45    // Map SubRegIndex -> RegisterClass
46    DenseMap<Record*,Record*> SubRegClasses;
47    std::string MethodProtos, MethodBodies;
48
49    const std::string &getName() const;
50    const std::vector<MVT::SimpleValueType> &getValueTypes() const {return VTs;}
51    unsigned getNumValueTypes() const { return VTs.size(); }
52
53    MVT::SimpleValueType getValueTypeNum(unsigned VTNum) const {
54      if (VTNum < VTs.size())
55        return VTs[VTNum];
56      assert(0 && "VTNum greater than number of ValueTypes in RegClass!");
57      abort();
58    }
59
60    bool containsRegister(Record *R) const {
61      for (unsigned i = 0, e = Elements.size(); i != e; ++i)
62        if (Elements[i] == R) return true;
63      return false;
64    }
65
66    // Returns true if RC is a strict subclass.
67    // RC is a sub-class of this class if it is a valid replacement for any
68    // instruction operand where a register of this classis required. It must
69    // satisfy these conditions:
70    //
71    // 1. All RC registers are also in this.
72    // 2. The RC spill size must not be smaller than our spill size.
73    // 3. RC spill alignment must be compatible with ours.
74    //
75    bool hasSubClass(const CodeGenRegisterClass *RC) const {
76
77      if (RC->Elements.size() > Elements.size() ||
78          (SpillAlignment && RC->SpillAlignment % SpillAlignment) ||
79          SpillSize > RC->SpillSize)
80        return false;
81
82      std::set<Record*> RegSet;
83      for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
84        Record *Reg = Elements[i];
85        RegSet.insert(Reg);
86      }
87
88      for (unsigned i = 0, e = RC->Elements.size(); i != e; ++i) {
89        Record *Reg = RC->Elements[i];
90        if (!RegSet.count(Reg))
91          return false;
92      }
93
94      return true;
95    }
96
97    CodeGenRegisterClass(Record *R);
98  };
99}
100
101#endif
102