aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen
diff options
context:
space:
mode:
authorRichard-Qin-X <richard.qin.001@gmail.com>2026-08-27 15:53:10 +0800
committerRichard-Qin-X <richard.qin.001@gmail.com>2026-08-27 15:53:10 +0800
commitb7cb5381e520a25e8f0f465903d8eea1972761c2 (patch)
treedb2c30c677ed8eec20f0f4e37d6387fa05cc6a6b /backend/lib/CodeGen
parenta932de71457e16030d7d05e4230c56e231c7a8e9 (diff)
downloadrcc-project-b7cb5381e520a25e8f0f465903d8eea1972761c2.tar.gz
rcc-project-b7cb5381e520a25e8f0f465903d8eea1972761c2.tar.xz
[CodeGen] Run machine optimizations once
Diffstat (limited to 'backend/lib/CodeGen')
-rw-r--r--backend/lib/CodeGen/MachineBranchOptimization.cpp19
-rw-r--r--backend/lib/CodeGen/MachineCSE.cpp21
-rw-r--r--backend/lib/CodeGen/MachineCombiner.cpp28
-rw-r--r--backend/lib/CodeGen/MachineCopyPropagation.cpp28
-rw-r--r--backend/lib/CodeGen/MachineDCE.cpp19
-rw-r--r--backend/lib/CodeGen/MachineLICM.cpp20
-rw-r--r--backend/lib/CodeGen/MachineMemoryFolding.cpp18
-rw-r--r--backend/lib/CodeGen/MachinePeepholeOptimizer.cpp21
-rw-r--r--backend/lib/CodeGen/MachineSink.cpp20
-rw-r--r--backend/lib/CodeGen/MachineStackSlotColoring.cpp20
-rw-r--r--backend/lib/CodeGen/MachineTailDuplication.cpp18
11 files changed, 17 insertions, 215 deletions
diff --git a/backend/lib/CodeGen/MachineBranchOptimization.cpp b/backend/lib/CodeGen/MachineBranchOptimization.cpp
index e03b0d9..6e3bc17 100644
--- a/backend/lib/CodeGen/MachineBranchOptimization.cpp
+++ b/backend/lib/CodeGen/MachineBranchOptimization.cpp
@@ -8,7 +8,6 @@
#include "rcc/CodeGen/MachineBasicBlockAttributes.h"
#include "rcc/CodeGen/MachineCFGUpdater.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineFunctionRecords.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineJumpTableInfo.h"
@@ -21,7 +20,6 @@
#include <algorithm>
#include <cstddef>
#include <cstdint>
-#include <optional>
#include <ranges>
#include <string>
#include <unordered_set>
@@ -581,22 +579,7 @@ const MachinePassContract &MachineBranchOptimizationPass::contract() const noexc
Expected<MachinePassResult> MachineBranchOptimizationPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "branch optimization transaction setup failed: "));
- }
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
- auto Result = runImplementation(Function, Target);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "branch optimization diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachineCSE.cpp b/backend/lib/CodeGen/MachineCSE.cpp
index 50dca0e..7bd5a22 100644
--- a/backend/lib/CodeGen/MachineCSE.cpp
+++ b/backend/lib/CodeGen/MachineCSE.cpp
@@ -7,7 +7,6 @@
#include "rcc/CodeGen/MachineAnalyses.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
@@ -16,7 +15,6 @@
#include <cstddef>
#include <cstdint>
-#include <optional>
#include <string>
#include <unordered_map>
#include <unordered_set>
@@ -293,24 +291,7 @@ const MachinePassContract &MachineCSEPass::contract() const noexcept {
Expected<MachinePassResult> MachineCSEPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "machine CSE transaction setup failed: "));
- }
- MachineFunctionAnalysisManager TrialAnalyses;
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
-
- auto Result = runImplementation(Function, Target, Analyses);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "machine CSE diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target, Analyses);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachineCombiner.cpp b/backend/lib/CodeGen/MachineCombiner.cpp
index 55bdc00..4c2ff88 100644
--- a/backend/lib/CodeGen/MachineCombiner.cpp
+++ b/backend/lib/CodeGen/MachineCombiner.cpp
@@ -4,7 +4,6 @@
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineIRBuilder.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
@@ -13,7 +12,6 @@
#include <cstddef>
#include <cstdint>
-#include <optional>
#include <string>
namespace rcc {
@@ -204,32 +202,12 @@ const MachinePassContract &MachineCombinerPass::contract() const noexcept {
Expected<MachinePassResult> MachineCombinerPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "machine combiner transaction setup failed: "));
- }
- MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
- Actions.beginReplay();
- auto Result = runImplementation(Function, Target, Actions);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "machine combiner diverged after a successful transaction preflight: "));
- }
- auto Replayed = Actions.finishReplay();
- if (!Replayed)
- return std::unexpected(Replayed.error());
- return Result;
+ return runImplementation(Function, Target, Analyses);
}
Expected<MachinePassResult>
MachineCombinerPass::runImplementation(MachineFunction &Function, const TargetDescription &Target,
- MachineActionReplay &Actions) const {
+ MachineFunctionAnalysisManager &Analyses) const {
if (Options.MaximumIterations == 0)
return makeUnexpected(ErrorCode::InvalidArgument,
"machine combiner iteration budget must be nonzero");
@@ -251,7 +229,7 @@ MachineCombinerPass::runImplementation(MachineFunction &Function, const TargetDe
continue;
std::string Rule = "combine.";
Rule.append(Pattern.Name);
- auto Accepted = Actions.request(Rule, Function, Instruction->stableID());
+ auto Accepted = Analyses.requestAction(Rule, Function, Instruction->stableID());
if (!Accepted)
return std::unexpected(Accepted.error());
if (!*Accepted)
diff --git a/backend/lib/CodeGen/MachineCopyPropagation.cpp b/backend/lib/CodeGen/MachineCopyPropagation.cpp
index f5736b8..9cb64cb 100644
--- a/backend/lib/CodeGen/MachineCopyPropagation.cpp
+++ b/backend/lib/CodeGen/MachineCopyPropagation.cpp
@@ -6,7 +6,6 @@
#include "rcc/CodeGen/InstructionDescriptor.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/RegisterMask.h"
@@ -303,33 +302,13 @@ const MachinePassContract &MachineCopyPropagationPass::contract() const noexcept
Expected<MachinePassResult>
MachineCopyPropagationPass::run(MachineFunction &Function, const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "COPY propagation transaction setup failed: "));
- }
- MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
- Actions.beginReplay();
- auto Result = runImplementation(Function, Target, Actions);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "COPY propagation diverged after a successful transaction preflight: "));
- }
- auto Replayed = Actions.finishReplay();
- if (!Replayed)
- return std::unexpected(Replayed.error());
- return Result;
+ return runImplementation(Function, Target, Analyses);
}
Expected<MachinePassResult>
MachineCopyPropagationPass::runImplementation(MachineFunction &Function,
const TargetDescription &Target,
- MachineActionReplay &Actions) const {
+ MachineFunctionAnalysisManager &Analyses) const {
MachinePassResult Result;
std::size_t PropagatedCopies = 0;
for (MachineBasicBlock *Block = Function.firstBlock(); Block != nullptr; Block = Block->next()) {
@@ -347,7 +326,8 @@ MachineCopyPropagationPass::runImplementation(MachineFunction &Function,
Instruction = Next;
continue;
}
- auto Accepted = Actions.request("propagate-copy", Function, Instruction->stableID());
+ auto Accepted =
+ Analyses.requestAction("propagate-copy", Function, Instruction->stableID());
if (!Accepted)
return std::unexpected(Accepted.error());
if (!*Accepted) {
diff --git a/backend/lib/CodeGen/MachineDCE.cpp b/backend/lib/CodeGen/MachineDCE.cpp
index 6681bc1..269999b 100644
--- a/backend/lib/CodeGen/MachineDCE.cpp
+++ b/backend/lib/CodeGen/MachineDCE.cpp
@@ -6,7 +6,6 @@
#include "rcc/CodeGen/LiveVariables.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
@@ -111,23 +110,7 @@ const MachinePassContract &MachineDCEPass::contract() const noexcept {
Expected<MachinePassResult> MachineDCEPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "machine DCE transaction setup failed: "));
- }
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
-
- auto Result = runImplementation(Function, Target);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "machine DCE diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target);
}
Expected<MachinePassResult> MachineDCEPass::runImplementation(MachineFunction &Function,
diff --git a/backend/lib/CodeGen/MachineLICM.cpp b/backend/lib/CodeGen/MachineLICM.cpp
index 6d76d5d..6ab15dc 100644
--- a/backend/lib/CodeGen/MachineLICM.cpp
+++ b/backend/lib/CodeGen/MachineLICM.cpp
@@ -7,7 +7,6 @@
#include "rcc/CodeGen/MachineAnalyses.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
@@ -321,24 +320,7 @@ const MachinePassContract &MachineLICMPass::contract() const noexcept {
Expected<MachinePassResult> MachineLICMPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "machine LICM transaction setup failed: "));
- }
- MachineFunctionAnalysisManager TrialAnalyses;
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
-
- auto Result = runImplementation(Function, Target, Analyses);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "machine LICM diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target, Analyses);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachineMemoryFolding.cpp b/backend/lib/CodeGen/MachineMemoryFolding.cpp
index 388f72c..fb4b86d 100644
--- a/backend/lib/CodeGen/MachineMemoryFolding.cpp
+++ b/backend/lib/CodeGen/MachineMemoryFolding.cpp
@@ -5,7 +5,6 @@
#include "rcc/CodeGen/InstructionDescriptor.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
@@ -181,22 +180,7 @@ const MachinePassContract &MachineMemoryFoldingPass::contract() const noexcept {
Expected<MachinePassResult> MachineMemoryFoldingPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "memory folding transaction setup failed: "));
- }
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
- auto Result = runImplementation(Function, Target);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "memory folding diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachinePeepholeOptimizer.cpp b/backend/lib/CodeGen/MachinePeepholeOptimizer.cpp
index 16c935d..ae39334 100644
--- a/backend/lib/CodeGen/MachinePeepholeOptimizer.cpp
+++ b/backend/lib/CodeGen/MachinePeepholeOptimizer.cpp
@@ -4,13 +4,10 @@
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/TargetDescription.h"
#include "rcc/CodeGen/TargetInstrInfo.h"
-#include <optional>
-
namespace rcc {
namespace {
@@ -41,23 +38,7 @@ const MachinePassContract &MachinePeepholeOptimizerPass::contract() const noexce
Expected<MachinePassResult> MachinePeepholeOptimizerPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "machine peephole transaction setup failed: "));
- }
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
-
- auto Result = runImplementation(Function, Target);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "machine peephole diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachineSink.cpp b/backend/lib/CodeGen/MachineSink.cpp
index 1c2d2b7..dda2048 100644
--- a/backend/lib/CodeGen/MachineSink.cpp
+++ b/backend/lib/CodeGen/MachineSink.cpp
@@ -7,7 +7,6 @@
#include "rcc/CodeGen/MachineAnalyses.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
@@ -343,24 +342,7 @@ const MachinePassContract &MachineSinkPass::contract() const noexcept {
Expected<MachinePassResult> MachineSinkPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &Analyses) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "machine sink transaction setup failed: "));
- }
- MachineFunctionAnalysisManager TrialAnalyses;
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, TrialAnalyses);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
-
- auto Result = runImplementation(Function, Target, Analyses);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "machine sink diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target, Analyses);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachineStackSlotColoring.cpp b/backend/lib/CodeGen/MachineStackSlotColoring.cpp
index eef913f..1717a20 100644
--- a/backend/lib/CodeGen/MachineStackSlotColoring.cpp
+++ b/backend/lib/CodeGen/MachineStackSlotColoring.cpp
@@ -5,7 +5,6 @@
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineFrameInfo.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineMemOperand.h"
#include "rcc/CodeGen/MachineOperand.h"
@@ -221,28 +220,13 @@ const MachinePassContract &MachineStackSlotColoringPass::contract() const noexce
}
Expected<MachinePassResult> MachineStackSlotColoringPass::run(MachineFunction &Function,
- const TargetDescription &Target,
+ const TargetDescription &,
MachineFunctionAnalysisManager &) {
if (Function.frameInfo().isFrozen()) {
return makeUnexpected(ErrorCode::FrameLayoutFailed,
"stack-slot coloring must run before frame layout is frozen");
}
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "stack-slot coloring transaction setup failed: "));
- }
- auto TrialResult = runImplementation(*Trial->ClonedFunction);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
- auto Result = runImplementation(Function);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "stack-slot coloring diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function);
}
Expected<MachinePassResult>
diff --git a/backend/lib/CodeGen/MachineTailDuplication.cpp b/backend/lib/CodeGen/MachineTailDuplication.cpp
index 3478400..53c67c6 100644
--- a/backend/lib/CodeGen/MachineTailDuplication.cpp
+++ b/backend/lib/CodeGen/MachineTailDuplication.cpp
@@ -8,7 +8,6 @@
#include "rcc/CodeGen/MachineBasicBlockAttributes.h"
#include "rcc/CodeGen/MachineCFGUpdater.h"
#include "rcc/CodeGen/MachineFunction.h"
-#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineJumpTableInfo.h"
#include "rcc/CodeGen/MachineMemOperand.h"
@@ -240,22 +239,7 @@ const MachinePassContract &MachineTailDuplicationPass::contract() const noexcept
Expected<MachinePassResult> MachineTailDuplicationPass::run(MachineFunction &Function,
const TargetDescription &Target,
MachineFunctionAnalysisManager &) {
- auto Trial = cloneMachineFunction(
- Function, Target,
- MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
- if (!Trial) {
- return std::unexpected(
- withErrorContext(Trial.error(), "tail duplication transaction setup failed: "));
- }
- auto TrialResult = runImplementation(*Trial->ClonedFunction, Target);
- if (!TrialResult)
- return std::unexpected(TrialResult.error());
- auto Result = runImplementation(Function, Target);
- if (!Result) {
- return std::unexpected(withErrorContext(
- Result.error(), "tail duplication diverged after a successful transaction preflight: "));
- }
- return Result;
+ return runImplementation(Function, Target);
}
Expected<MachinePassResult>