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

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

#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineOptimizationPipeline.h"
#include "rcc/CodeGen/MachinePipelinePasses.h"
#include "rcc/CodeGen/MachineSSAExit.h"
#include "rcc/CodeGen/RegAllocPBQP.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/Target/RISCV/RISCVCodeGen.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(MachinePassManager &Pipeline, ArgumentT &&...Arguments) {
  return Pipeline.addPass(std::make_unique<PassT>(std::forward<ArgumentT>(Arguments)...));
}

[[nodiscard]] Expected<void> addInitialPipeline(MachinePassManager &Pipeline,
                                                RISCVSubtargetFeatures Features) {
  auto Added = addPass<LegalizerPass>(Pipeline, createLegalizerInfo(Features));
  if (!Added)
    return std::unexpected(Added.error());
  Added = addPass<RegisterBankSelectPass>(Pipeline, registerBankInfo());
  if (!Added)
    return std::unexpected(Added.error());
  return addPass<InstructionSelectPass>(Pipeline, instructionSelector());
}

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

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

[[nodiscard]] Expected<void> addCompleteRegistry(MachinePassManager &Pipeline,
                                                 RISCVSubtargetFeatures Features) {
  auto Added = addInitialPipeline(Pipeline, Features);
  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());
}

} // namespace

Expected<void> addCanonicalPipeline(MachinePassManager &Pipeline, const MachineFunction &Function) {
  return addCanonicalPipeline(Pipeline, Function, RISCVSubtargetFeatures::rv64gc());
}

Expected<void> addCanonicalPipeline(MachinePassManager &Pipeline, const MachineFunction &Function,
                                    RISCVSubtargetFeatures Features) {
  if (!Function.properties().has(MachineFunctionProperty::Legalized)) {
    auto Added = addPass<LegalizerPass>(Pipeline, createLegalizerInfo(Features));
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::RegBankSelected)) {
    auto Added = addPass<RegisterBankSelectPass>(Pipeline, registerBankInfo());
    if (!Added)
      return std::unexpected(Added.error());
  }
  if (!Function.properties().has(MachineFunctionProperty::Selected)) {
    auto Added = addPass<InstructionSelectPass>(Pipeline, instructionSelector());
    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, ssaInfo());
      if (!Added)
        return std::unexpected(Added.error());
    }
  }
  if (!Function.properties().has(MachineFunctionProperty::TiedOpsRewritten)) {
    auto Added = addPass<TwoAddressRewritePass>(Pipeline, ssaInfo());
    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());
    if (!Added)
      return std::unexpected(Added.error());
  }
  return {};
}

Expected<std::unique_ptr<MachineFunctionPass>> createCanonicalPipelinePass(std::string_view Name) {
  return createCanonicalPipelinePass(Name, RISCVSubtargetFeatures::rv64gc());
}

Expected<std::unique_ptr<MachineFunctionPass>>
createCanonicalPipelinePass(std::string_view Name, RISCVSubtargetFeatures Features) {
  if (Name.empty())
    return makeUnexpected(ErrorCode::InvalidArgument, "pipeline pass name cannot be empty");
  MachinePassManager Registry;
  auto Added = addCompleteRegistry(Registry, Features);
  if (!Added)
    return std::unexpected(Added.error());
  MachinePassManager::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) {
  MachinePassManager Pipeline;
  auto Added = addCanonicalPipeline(Pipeline, Function);
  if (!Added)
    return std::unexpected(Added.error());
  MachinePassManager::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;
}

Expected<void> runCanonicalPipeline(MachineFunction &Function, const TargetDescription &Target,
                                    MachinePassManagerOptions Options,
                                    MachinePassInstrumentation *Instrumentation) {
  MachinePassManager Pipeline(Options);
  auto Added = addCanonicalPipeline(Pipeline, Function);
  if (!Added)
    return std::unexpected(Added.error());
  MachineAnalysisManager Analyses;
  auto Registered = registerMachineOptimizationAnalyses(Analyses);
  if (!Registered)
    return std::unexpected(Registered.error());
  return Pipeline.run(Function, Target, Analyses, Instrumentation);
}

} // namespace rcc::riscv