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