aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Target/RISCV/GISel/RISCVGISelCombiner.cpp
blob: bf67c0ac3138663617fdb27be6be06b83fda6dc8 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "RISCV.h"
#include "rcc/CodeGen/GlobalISel/GIMatchTableExecutor.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"

#include <string>

namespace rcc::riscv {
namespace {

#define GET_GLOBALISEL_COMBINER_TABLE
#include "RISCVGenGlobalISelCombiner.inc"
#undef GET_GLOBALISEL_COMBINER_TABLE

class RISCVGISelCombinerExecutor final : public GIMatchTableExecutor {
protected:
  [[nodiscard]] Expected<bool>
  matchesTargetPredicate(std::string_view Name, const MachineInstr &Instruction,
                         const MachineFunction &Function,
                         const TargetDescription &Target) const override {
    if (Name != "sext_cheaper_than_zext")
      return GIMatchTableExecutor::matchesTargetPredicate(Name, Instruction, Function, Target);
    const MachineOperand *Destination = Instruction.operandAt(0);
    const MachineOperand *Source = Instruction.operandAt(1);
    const Register *DestinationReg =
        Destination == nullptr ? nullptr : Destination->getIf<Register>();
    const Register *SourceReg = Source == nullptr ? nullptr : Source->getIf<Register>();
    if (DestinationReg == nullptr || SourceReg == nullptr || !DestinationReg->isVirtual() ||
        !SourceReg->isVirtual())
      return false;
    auto DestinationType = Function.registerInfo().getType(*DestinationReg);
    auto SourceType = Function.registerInfo().getType(*SourceReg);
    if (!DestinationType || !SourceType)
      return false;
    return DestinationType->isCategoryNeutralScalar() && SourceType->isCategoryNeutralScalar() &&
           DestinationType->scalarBitWidth() == 64 && SourceType->scalarBitWidth() == 32;
  }
};

} // namespace

const GIMatchTable &giselCombinerTable() noexcept {
  static constexpr GIMatchTable Table = generatedGlobalISelCombinerMatchTable();
  return Table;
}

const GIMatchTableExecutor &giselCombinerExecutor() noexcept {
  static const RISCVGISelCombinerExecutor Executor;
  return Executor;
}

} // namespace rcc::riscv