From f6fdfb8d484b4e52cdb24a94a31b0f00d41a5eb7 Mon Sep 17 00:00:00 2001 From: Richard-Qin-X Date: Sat, 15 Aug 2026 00:38:20 +0800 Subject: [GlobalISel] Match LLVM CSE builder result semantics --- .../GlobalISel/LegalizationArtifactCombiner.h | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h') diff --git a/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h b/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h index 7e2961d..c0c7be0 100644 --- a/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h +++ b/backend/include/rcc/CodeGen/GlobalISel/LegalizationArtifactCombiner.h @@ -101,8 +101,11 @@ public: break; case GenericOpcode::G_TRUNC: Changed = tryCombineTrunc(Instruction, DeadInstructions, UpdatedDefs, Observer); - if (Changed && !*Changed) - UpdatedDefs.push_back(*Instruction.operandAt(0)->registerValue()); + if (Changed && !*Changed) { + const Register *Destination = Instruction.operandAt(0)->getIf(); + assert(Destination != nullptr); + UpdatedDefs.push_back(*Destination); + } break; } if (!Changed) @@ -377,11 +380,11 @@ private: } } - [[nodiscard]] Expected isInstUnsupported(LegalizationQuery Query) const { + [[nodiscard]] Expected isInstUnsupported(const LegalizationQuery &Query) const { return LI.isUnsupported(Query); } - [[nodiscard]] Expected isInstLegal(LegalizationQuery Query) const { + [[nodiscard]] Expected isInstLegal(const LegalizationQuery &Query) const { return LI.isLegal(Query); } }; @@ -438,12 +441,15 @@ inline Expected LegalizationArtifactCombiner::buildConstant(Register Desti inline Expected LegalizationArtifactCombiner::buildConstant(LowLevelType Type, APInt Value) { - auto Destination = MRI.createVirtualRegister(Type); - if (!Destination) - return std::unexpected(Destination.error()); - auto Built = buildConstant(*Destination, std::move(Value)); + auto Built = Builder.buildConstant(Type, std::move(Value)); if (!Built) return std::unexpected(Built.error()); + const MachineOperand *Definition = (*Built)->operandAt(0); + const Register *Destination = Definition == nullptr ? nullptr : Definition->getIf(); + if (Destination == nullptr || !Definition->isDef()) { + return makeUnexpected(ErrorCode::InternalError, + "constant builder returned an instruction without a register definition"); + } return *Destination; } @@ -1309,7 +1315,11 @@ inline Expected LegalizationArtifactCombiner::tryCombineUnmergeValues( Destinations.reserve(NewDefCount); for (std::size_t Index = 0; Index < NewDefCount; ++Index) Destinations.push_back(Unmerge.getReg(SourceIndex * NewDefCount + Index)); - Register SplitSource = *registerOperand(*Merge, SourceIndex + 1); + const auto SplitSourceOperand = registerOperand(*Merge, SourceIndex + 1); + if (!SplitSourceOperand) + return makeUnexpected(ErrorCode::VerificationError, + "merge source is not a register operand"); + Register SplitSource = SplitSourceOperand.value(); if (ConvertOpcode) { auto ConvertedType = divideType(*OperandType, MergeSourceCount); if (!ConvertedType) -- cgit v1.2.3