aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/CodeGen/LiveRegUnits.h
blob: 02b9c55cc61f2667a0deefb4bb8f765e59dd9bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===-- llvm/CodeGen/LiveRegUnits.h - Live register unit set ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements a Set of live register units. This can be used for ad
// hoc liveness tracking after register allocation. You can start with the
// live-ins/live-outs at the beginning/end of a block and update the information
// while walking the instructions inside the block.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CODEGEN_LIVEREGUNITS_H
#define LLVM_CODEGEN_LIVEREGUNITS_H

#include "llvm/ADT/SparseSet.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include <cassert>

namespace llvm {

class MachineInstr;

/// A set of live register units with functions to track liveness when walking
/// backward/forward through a basic block.
class LiveRegUnits {
  SparseSet<unsigned> LiveUnits;

  LiveRegUnits(const LiveRegUnits&) LLVM_DELETED_FUNCTION;
  LiveRegUnits &operator=(const LiveRegUnits&) LLVM_DELETED_FUNCTION;
public:
  /// \brief Constructs a new empty LiveRegUnits set.
  LiveRegUnits() {}

  void init(const TargetRegisterInfo *TRI) {
    LiveUnits.clear();
    LiveUnits.setUniverse(TRI->getNumRegs());
  }

  void clear() { LiveUnits.clear(); }

  bool empty() const { return LiveUnits.empty(); }

  /// \brief Adds a register to the set.
  void addReg(unsigned Reg, const MCRegisterInfo &MCRI) {
    for (MCRegUnitIterator RUnits(Reg, &MCRI); RUnits.isValid(); ++RUnits)
      LiveUnits.insert(*RUnits);
  }

  /// \brief Removes a register from the set.
  void removeReg(unsigned Reg, const MCRegisterInfo &MCRI) {
    for (MCRegUnitIterator RUnits(Reg, &MCRI); RUnits.isValid(); ++RUnits)
      LiveUnits.erase(*RUnits);
  }

  /// \brief Removes registers clobbered by the regmask operand @p Op.
  void removeRegsInMask(const MachineOperand &Op, const MCRegisterInfo &MCRI);

  /// \brief Returns true if register @p Reg (or one of its super register) is
  /// contained in the set.
  bool contains(unsigned Reg, const MCRegisterInfo &MCRI) const {
    for (MCRegUnitIterator RUnits(Reg, &MCRI); RUnits.isValid(); ++RUnits) {
      if (LiveUnits.count(*RUnits))
        return true;
    }
    return false;
  }

  /// \brief Simulates liveness when stepping backwards over an
  /// instruction(bundle): Remove Defs, add uses.
  void stepBackward(const MachineInstr &MI, const MCRegisterInfo &MCRI);

  /// \brief Simulates liveness when stepping forward over an
  /// instruction(bundle): Remove killed-uses, add defs.
  void stepForward(const MachineInstr &MI, const MCRegisterInfo &MCRI);

  /// \brief Adds all registers in the live-in list of block @p BB.
  void addLiveIns(const MachineBasicBlock *MBB, const MCRegisterInfo &MCRI);
};

} // namespace llvm

#endif