aboutsummaryrefslogtreecommitdiff
path: root/backend/include/rcc/CodeGen/GlobalISel/GenericMachineInstrs.h
blob: ea3532cad453b7593a156e3985993546f365ff6d (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef RCC_CODEGEN_GLOBALISEL_GENERICMACHINEINSTRS_H
#define RCC_CODEGEN_GLOBALISEL_GENERICMACHINEINSTRS_H

#include "rcc/CodeGen/GenericOpcodes.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/Register.h"

#include <cassert>
#include <cstddef>

namespace rcc {

class GenericMachineInstr : public MachineInstr {
public:
  GenericMachineInstr() = delete;

  [[nodiscard]] Register getReg(std::size_t Index) const {
    const MachineOperand *Operand = operandAt(Index);
    assert(Operand != nullptr && Operand->isRegister());
    const Register *Reg = Operand->getIf<Register>();
    assert(Reg != nullptr);
    return *Reg;
  }

  [[nodiscard]] static bool classof(const MachineInstr *Instruction) noexcept {
    return Instruction != nullptr && isGenericOpcode(Instruction->opcode());
  }
};

class GUnmerge : public GenericMachineInstr {
public:
  [[nodiscard]] std::size_t getNumDefs() const noexcept {
    return operandCount() - 1;
  }

  [[nodiscard]] Register getSourceReg() const {
    return getReg(getNumDefs());
  }

  [[nodiscard]] static bool classof(const MachineInstr *Instruction) noexcept {
    return Instruction != nullptr &&
           Instruction->opcode() == rcc::opcode(GenericOpcode::G_UNMERGE_VALUES);
  }
};

class GMergeLikeInstr : public GenericMachineInstr {
public:
  [[nodiscard]] std::size_t getNumSources() const noexcept {
    return operandCount() - 1;
  }

  [[nodiscard]] Register getSourceReg(std::size_t Index) const {
    return getReg(Index + 1);
  }

  [[nodiscard]] static bool classof(const MachineInstr *Instruction) noexcept {
    if (Instruction == nullptr)
      return false;
    switch (static_cast<GenericOpcode>(Instruction->opcode().value())) {
    case GenericOpcode::G_MERGE_VALUES:
    case GenericOpcode::G_CONCAT_VECTORS:
    case GenericOpcode::G_BUILD_VECTOR:
      return true;
    default:
      return false;
    }
  }
};

class GMerge : public GMergeLikeInstr {
public:
  [[nodiscard]] static bool classof(const MachineInstr *Instruction) noexcept {
    return Instruction != nullptr &&
           Instruction->opcode() == rcc::opcode(GenericOpcode::G_MERGE_VALUES);
  }
};

class GConcatVectors : public GMergeLikeInstr {
public:
  [[nodiscard]] static bool classof(const MachineInstr *Instruction) noexcept {
    return Instruction != nullptr &&
           Instruction->opcode() == rcc::opcode(GenericOpcode::G_CONCAT_VECTORS);
  }
};

class GBuildVector : public GMergeLikeInstr {
public:
  [[nodiscard]] static bool classof(const MachineInstr *Instruction) noexcept {
    return Instruction != nullptr &&
           Instruction->opcode() == rcc::opcode(GenericOpcode::G_BUILD_VECTOR);
  }
};

template <typename InstructionT>
[[nodiscard]] InstructionT &castGenericMachineInstr(MachineInstr &Instruction) {
  assert(InstructionT::classof(&Instruction));
  return static_cast<InstructionT &>(Instruction);
}

template <typename InstructionT>
[[nodiscard]] const InstructionT &castGenericMachineInstr(const MachineInstr &Instruction) {
  assert(InstructionT::classof(&Instruction));
  return static_cast<const InstructionT &>(Instruction);
}

} // namespace rcc

#endif // RCC_CODEGEN_GLOBALISEL_GENERICMACHINEINSTRS_H