From b7cb5381e520a25e8f0f465903d8eea1972761c2 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Thu, 27 Aug 2026 15:53:10 +0800 Subject: [CodeGen] Run machine optimizations once --- backend/include/rcc/CodeGen/MachineCombiner.h | 3 ++- .../include/rcc/CodeGen/MachineCopyPropagation.h | 3 ++- backend/lib/CodeGen/MachineBranchOptimization.cpp | 19 +-------------- backend/lib/CodeGen/MachineCSE.cpp | 21 +--------------- backend/lib/CodeGen/MachineCombiner.cpp | 28 +++------------------- backend/lib/CodeGen/MachineCopyPropagation.cpp | 28 ++++------------------ backend/lib/CodeGen/MachineDCE.cpp | 19 +-------------- backend/lib/CodeGen/MachineLICM.cpp | 20 +--------------- backend/lib/CodeGen/MachineMemoryFolding.cpp | 18 +------------- backend/lib/CodeGen/MachinePeepholeOptimizer.cpp | 21 +--------------- backend/lib/CodeGen/MachineSink.cpp | 20 +--------------- backend/lib/CodeGen/MachineStackSlotColoring.cpp | 20 ++-------------- backend/lib/CodeGen/MachineTailDuplication.cpp | 18 +------------- 13 files changed, 21 insertions(+), 217 deletions(-) diff --git a/backend/include/rcc/CodeGen/MachineCombiner.h b/backend/include/rcc/CodeGen/MachineCombiner.h index 42ca301..d9df1d1 100644 --- a/backend/include/rcc/CodeGen/MachineCombiner.h +++ b/backend/include/rcc/CodeGen/MachineCombiner.h @@ -29,7 +29,8 @@ public: private: [[nodiscard]] Expected runImplementation(MachineFunction &Function, const TargetDescription &Target, - MachineActionReplay &Actions) const; + MachineFunctionAnalysisManager + &Analyses) const; const TargetMachineCombinerInfo *Info; MachineCombinerOptions Options; diff --git a/backend/include/rcc/CodeGen/MachineCopyPropagation.h b/backend/include/rcc/CodeGen/MachineCopyPropagation.h index 59a3a77..e689632 100644 --- a/backend/include/rcc/CodeGen/MachineCopyPropagation.h +++ b/backend/include/rcc/CodeGen/MachineCopyPropagation.h @@ -30,7 +30,8 @@ public: private: [[nodiscard]] Expected runImplementation(MachineFunction &Function, const TargetDescription &Target, - MachineActionReplay &Actions) const; + MachineFunctionAnalysisManager + &Analyses) const; const TargetInstrInfo *Info; MachineCopyPropagationOptions Options; 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 #include #include -#include #include #include #include @@ -581,22 +579,7 @@ const MachinePassContract &MachineBranchOptimizationPass::contract() const noexc Expected 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 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 #include -#include #include #include #include @@ -293,24 +291,7 @@ const MachinePassContract &MachineCSEPass::contract() const noexcept { Expected 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 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 #include -#include #include namespace rcc { @@ -204,32 +202,12 @@ const MachinePassContract &MachineCombinerPass::contract() const noexcept { Expected 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 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 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 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 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 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 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 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 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 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 - namespace rcc { namespace { @@ -41,23 +38,7 @@ const MachinePassContract &MachinePeepholeOptimizerPass::contract() const noexce Expected 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 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 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 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 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 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 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 -- cgit v1.2.3