aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/Target/RISCV/RISCVPipeline.cpp
blob: 62b77f09b811bad1c8a8c23613f290cc72d7a207 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/Target/RISCV/RISCVPipeline.h"

#include "GISel/RISCVRegisterBankInfo.h"
#include "RISCV.h"
#include "RISCVInstrInfo.h"
#include "rcc/CodeGen/GlobalISel/InstructionSelect.h"
#include "rcc/CodeGen/GlobalISel/Legalizer.h"
#include "rcc/CodeGen/GlobalISel/RegBankSelect.h"
#include "rcc/CodeGen/MachineCombiner.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineOptimizationPipeline.h"
#include "rcc/CodeGen/PHIElimination.h"
#include "rcc/CodeGen/RegAllocPBQP.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/CodeGen/TwoAddressInstructionPass.h"

#include <memory>
#include <ranges>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

namespace rcc::riscv {
namespace {

template <typename PassT, typename... ArgumentT>
[[nodiscard]] Expected<void> addPass(MachineFunctionPassManager &Pipeline,
                                     ArgumentT &&...Arguments) {
  return Pipeline.addPass(std::make_unique<PassT>(std::forward<ArgumentT>(Arguments)...));
}

[[nodiscard]] Expected<void> addInitialPipeline(MachineFunctionPassManager &Pipeline,
                                                const RISCVSubtarget &Subtarget,
                                                RISCVPipelineOptions Options) {
  auto Added = addPass<MachineCombinerPass>(Pipeline, machineCombinerInfo(),
                                            MachineCombinerStage::PreLegalizer);
  if (!Added)
    return std::unexpected(Added.error());
  Added = addPass<Legalizer>(Pipeline, Subtarget.getLegalizerInfo(), Options.Optimization);
  if (!Added)
    return std::unexpected(Added.error());
  Added = addPass<RegBankSelect>(Pipeline, Subtarget.getRegBankInfo());
  if (!Added)
    return std::unexpected(Added.error());
  return addPass<InstructionSelect>(Pipeline, Subtarget.getInstructionSelector());
}

[[nodiscard]] Expected<void> addSSAExitPipeline(MachineFunctionPassManager &Pipeline) {
  auto Added = addSelectedSSAOptimizationPipeline(Pipeline, optimizationPipelineInfo());
  if (!Added)
    return std::unexpected(Added.error());
  Added = addPass<PHIEliminationPass>(Pipeline, instrInfo());
  if (!Added)
    return std::unexpected(Added.error());
  return addPass<TwoAddressInstructionPass>(Pipeline, instrInfo());
}

[[nodiscard]] Expected<void> addRegisterAllocationPipeline(MachineFunctionPassManager &Pipeline) {
  auto Added = addPreRegisterAllocationOptimizationPipeline(Pipeline, optimizationPipelineInfo());
  if (!Added)
    return std::unexpected(Added.error());
  return addPass<PBQPAllocatorPass>(Pipeline, registerAllocationInfo());
}

[[nodiscard]] Expected<void> addCompleteRegistry(MachineFunctionPassManager &Pipeline,
                                                 const RISCVSubtarget &Subtarget,
                                                 RISCVPipelineOptions Options) {
  auto Added = addInitialPipeline(Pipeline, Subtarget, Options);
  if (!Added)
    return std::unexpected(Added.error());
  Added = addSSAExitPipeline(Pipeline);
  if (!Added)
    return std::unexpected(Added.error());
  Added = addRegisterAllocationPipeline(Pipeline);
  if (!Added)
    return std::unexpected(Added.error());
  return addPostRegisterAllocationPipeline(
      Pipeline, optimizationPipelineInfo(),
      MachineOptimizationPipelineOptions{.UseTargetCopyInstructions =
                                             Options.UseTargetCopyInstructions});
}

} // namespace

Expected<void> addCanonicalPipeline(MachineFunctionPassManager &Pipeline,
                                    const MachineFunction &Function,
                                    const RISCVSubtarget &Subtarget, RISCVPipelineOptions Options) {
  if (!Function.properties().has(MachineFunctionProperty::Legalized)) {
    auto Added = addPass<MachineCombinerPass>(Pipeline, machineCombinerInfo(),
                                              MachineCombinerStage::PreLegalizer);
    if (!Added)
      return std::unexpected(Added.error());
    Added = addPass<Legalizer>(Pipeline, Subtarget.getLegalizerInfo(), Options.Optimization);
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::RegBankSelected)) {
    auto Added = addPass<RegBankSelect>(Pipeline, Subtarget.getRegBankInfo());
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::Selected)) {
    auto Added = addPass<InstructionSelect>(Pipeline, Subtarget.getInstructionSelector());
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::TiedOpsRewritten)) {
    auto Added = addSelectedSSAOptimizationPipeline(Pipeline, optimizationPipelineInfo());
    if (!Added)
      return std::unexpected(Added.error());
    if (!Function.properties().has(MachineFunctionProperty::NoPHIs)) {
      Added = addPass<PHIEliminationPass>(Pipeline, instrInfo());
      if (!Added)
        return std::unexpected(Added.error());
    }
  }
  if (!Function.properties().has(MachineFunctionProperty::TiedOpsRewritten)) {
    auto Added = addPass<TwoAddressInstructionPass>(Pipeline, instrInfo());
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::NoVRegs)) {
    auto Added = addRegisterAllocationPipeline(Pipeline);
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::NoExecutablePseudos)) {
    auto Added = addPostRegisterAllocationPipeline(
        Pipeline, optimizationPipelineInfo(),
        MachineOptimizationPipelineOptions{.UseTargetCopyInstructions =
                                               Options.UseTargetCopyInstructions});
    if (!Added)
      return std::unexpected(Added.error());
  }
  return {};
}

Expected<std::unique_ptr<MachineFunctionPass>>
createCanonicalPipelinePass(std::string_view Name, const RISCVSubtarget &Subtarget,
                            RISCVPipelineOptions Options) {
  if (Name.empty())
    return makeUnexpected(ErrorCode::InvalidArgument, "pipeline pass name cannot be empty");
  MachineFunctionPassManager Registry;
  auto Added = addCompleteRegistry(Registry, Subtarget, Options);
  if (!Added)
    return std::unexpected(Added.error());
  MachineFunctionPassManager::PassList Passes = std::move(Registry).takePasses();
  auto Position = std::ranges::find_if(Passes, [Name](const auto &Pass) {
    return Pass != nullptr && Pass->contract().Name == Name;
  });
  if (Position == Passes.end()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "unknown RV64GC machine pass '" + std::string(Name) + "'");
  }
  return std::move(*Position);
}

Expected<std::vector<std::string>> canonicalPipelinePassNames(const MachineFunction &Function,
                                                              const RISCVSubtarget &Subtarget) {
  MachineFunctionPassManager Pipeline;
  auto Added = addCanonicalPipeline(Pipeline, Function, Subtarget);
  if (!Added)
    return std::unexpected(Added.error());
  MachineFunctionPassManager::PassList Passes = std::move(Pipeline).takePasses();
  std::vector<std::string> Result;
  Result.reserve(Passes.size());
  for (const std::unique_ptr<MachineFunctionPass> &Pass : Passes) {
    if (Pass == nullptr)
      return makeUnexpected(ErrorCode::InternalError, "canonical pipeline contains a null pass");
    Result.push_back(Pass->contract().Name);
  }
  return Result;
}

} // namespace rcc::riscv