1c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===- RegisterClassInfo.h - Dynamic Register Class Info --------*- C++ -*-===//
2c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
3c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//                     The LLVM Compiler Infrastructure
4c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
5c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file is distributed under the University of Illinois Open Source
6c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// License. See LICENSE.TXT for details.
7c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
8c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
9c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
10c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// This file implements the RegisterClassInfo class which provides dynamic
11c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// information about target register classes. Callee saved and reserved
12c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// registers depends on calling conventions and other dynamic information, so
13c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot// some things cannot be determined statically.
14c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//
15c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot//===----------------------------------------------------------------------===//
16c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
17c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#ifndef LLVM_CODEGEN_REGISTERCLASSINFO_H
18c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#define LLVM_CODEGEN_REGISTERCLASSINFO_H
19c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
20c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/ArrayRef.h"
21c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/BitVector.h"
22c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/ADT/SmallVector.h"
23c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/MC/MCRegisterInfo.h"
24c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include "llvm/Target/TargetRegisterInfo.h"
25c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <cassert>
26c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <cstdint>
27c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#include <memory>
28c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
29c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotnamespace llvm {
30c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
31c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotclass RegisterClassInfo {
32c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  struct RCInfo {
33c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    unsigned Tag = 0;
34c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    unsigned NumRegs = 0;
35c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    bool ProperSubClass = false;
36c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    uint8_t MinCost = 0;
37c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    uint16_t LastCostChange = 0;
38c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    std::unique_ptr<MCPhysReg[]> Order;
39c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
40c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    RCInfo() = default;
41c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
42c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    operator ArrayRef<MCPhysReg>() const {
43c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      return makeArrayRef(Order.get(), NumRegs);
44c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    }
45c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  };
46c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
47c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Brief cached information for each register class.
48c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  std::unique_ptr<RCInfo[]> RegClass;
49c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
50c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Tag changes whenever cached information needs to be recomputed. An RCInfo
51c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // entry is valid when its tag matches.
52c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned Tag = 0;
53c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
54c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const MachineFunction *MF = nullptr;
55c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const TargetRegisterInfo *TRI = nullptr;
56c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
57c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Callee saved registers of last MF. Assumed to be valid until the next
58c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // runOnFunction() call.
59c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Used only to determine if an update was made to CalleeSavedAliases.
60c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const MCPhysReg *CalleeSavedRegs = nullptr;
61c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
62c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Map register alias to the callee saved Register.
63c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  SmallVector<MCPhysReg, 4> CalleeSavedAliases;
64c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
65c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Reserved registers in the current MF.
66c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  BitVector Reserved;
67c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
68c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  std::unique_ptr<unsigned[]> PSetLimits;
69c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
70c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Compute all information about RC.
71c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void compute(const TargetRegisterClass *RC) const;
72c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
73c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  // Return an up-to-date RCInfo for RC.
74c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  const RCInfo &get(const TargetRegisterClass *RC) const {
75c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    const RCInfo &RCI = RegClass[RC->getID()];
76c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    if (Tag != RCI.Tag)
77c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      compute(RC);
78c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return RCI;
79c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
80c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
81c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotpublic:
82c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  RegisterClassInfo();
83c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
84c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// runOnFunction - Prepare to answer questions about MF. This must be called
85c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// before any other methods are used.
86c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  void runOnMachineFunction(const MachineFunction &MF);
87c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
88c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// getNumAllocatableRegs - Returns the number of actually allocatable
89c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// registers in RC in the current function.
90c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const {
91c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return get(RC).NumRegs;
92c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
93c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
94c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// getOrder - Returns the preferred allocation order for RC. The order
95c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// contains no reserved registers, and registers that alias callee saved
96c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// registers come last.
97c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ArrayRef<MCPhysReg> getOrder(const TargetRegisterClass *RC) const {
98c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return get(RC);
99c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
100c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
101c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// isProperSubClass - Returns true if RC has a legal super-class with more
102c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// allocatable registers.
103c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
104c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Register classes like GR32_NOSP are not proper sub-classes because %esp
105c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// is not allocatable.  Similarly, tGPR is not a proper sub-class in Thumb
106c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// mode because the GPR super-class is not legal.
107c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  bool isProperSubClass(const TargetRegisterClass *RC) const {
108c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return get(RC).ProperSubClass;
109c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
110c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
111c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// getLastCalleeSavedAlias - Returns the last callee saved register that
112c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// overlaps PhysReg, or 0 if Reg doesn't overlap a CalleeSavedAliases.
113c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned getLastCalleeSavedAlias(unsigned PhysReg) const {
114c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
115c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    if (PhysReg < CalleeSavedAliases.size())
116c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      return CalleeSavedAliases[PhysReg];
117c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return 0;
118c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
119c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
120c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Get the minimum register cost in RC's allocation order.
121c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// This is the smallest value returned by TRI->getCostPerUse(Reg) for all
122c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// the registers in getOrder(RC).
123c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned getMinCost(const TargetRegisterClass *RC) {
124c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return get(RC).MinCost;
125c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
126c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
127c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Get the position of the last cost change in getOrder(RC).
128c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
129c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// All registers in getOrder(RC).slice(getLastCostChange(RC)) will have the
130c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// same cost according to TRI->getCostPerUse().
131c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned getLastCostChange(const TargetRegisterClass *RC) {
132c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return get(RC).LastCostChange;
133c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
134c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
135c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// Get the register unit limit for the given pressure set index.
136c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  ///
137c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  /// RegisterClassInfo adjusts this limit for reserved registers.
138c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned getRegPressureSetLimit(unsigned Idx) const {
139c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    if (!PSetLimits[Idx])
140c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot      PSetLimits[Idx] = computePSetLimit(Idx);
141c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot    return PSetLimits[Idx];
142c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  }
143c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
144c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robotprotected:
145c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot  unsigned computePSetLimit(unsigned Idx) const;
146c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot};
147c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
148c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot} // end namespace llvm
149c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot
150c9cc9e7d29b8970d8ddb734c88fb62d01e0b727android-build-team Robot#endif // LLVM_CODEGEN_REGISTERCLASSINFO_H
151