44// See https://llvm.org/LICENSE.txt for license information.
55// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66//
7- // (c) Copyright 2024 Advanced Micro Devices, Inc. or its affiliates
7+ // (c) Copyright 2024-2025 Advanced Micro Devices, Inc. or its affiliates
88//
99// ===----------------------------------------------------------------------===//
1010//
1515#include " AIE2PTargetMachine.h"
1616#include " AIE2PTargetTransformInfo.h"
1717#include " llvm/CodeGen/TargetLoweringObjectFileImpl.h"
18+ #include " llvm/CodeGen/TargetRegisterInfo.h"
1819
1920using namespace llvm ;
2021extern cl::opt<bool > EnableStagedRA;
@@ -25,6 +26,7 @@ extern cl::opt<bool> EnableAddressChaining;
2526extern cl::opt<bool > EnableGlobalPtrModOptimizer;
2627extern cl::opt<bool > EnableWAWRegRewrite;
2728extern cl::opt<bool > EnableAIEIfConversion;
29+ extern cl::opt<bool > EnableFineGrainedStagedRA;
2830
2931void AIE2PTargetMachine::anchor () {}
3032
@@ -70,17 +72,44 @@ void AIE2PPassConfig::addPreRegBankSelect() {
7072 }
7173}
7274
75+ static bool isRegUsedBy2DOr3DInstruction (const MachineRegisterInfo &MRI,
76+ const Register &R) {
77+
78+ return std::any_of (
79+ MRI.use_nodbg_instructions (R).begin (),
80+ MRI.use_nodbg_instructions (R).end (), [&](const MachineInstr &MI) {
81+ auto &TII = *static_cast <const AIEBaseInstrInfo *>(
82+ MI.getMF ()->getSubtarget ().getInstrInfo ());
83+
84+ // We should recognize both cases, with and without splitting. A 2D/3D
85+ // instruction will always be split os splittable.
86+ return TII.getOpcodeWithTupleOperands (MI.getOpcode ()).has_value () ||
87+ TII.getOpcodeWithAtomicOperands (MI.getOpcode ()).has_value ();
88+ });
89+ }
90+
7391static bool onlyAllocate3DRegisters (const TargetRegisterInfo &TRI,
7492 const MachineRegisterInfo &MRI,
7593 const Register &R) {
76- return AIE2P::eDSRegClass.hasSubClassEq (MRI.getRegClass (R));
94+
95+ const TargetRegisterClass *RegClass = MRI.getRegClass (R);
96+ if (!AIE2P::eDSRegClass.hasSubClassEq (RegClass))
97+ return false ;
98+ return EnableFineGrainedStagedRA ? isRegUsedBy2DOr3DInstruction (MRI, R)
99+ : true ;
77100}
101+
78102static bool onlyAllocate3D2DRegisters (const TargetRegisterInfo &TRI,
79103 const MachineRegisterInfo &MRI,
80104 const Register &R) {
81- return AIE2P::eDSRegClass.hasSubClassEq (MRI.getRegClass (R)) ||
82- AIE2P::eDRegClass.hasSubClassEq (MRI.getRegClass (R));
105+ const TargetRegisterClass *RegClass = MRI.getRegClass (R);
106+ if (!AIE2P::eDSRegClass.hasSubClassEq (RegClass) &&
107+ !AIE2P::eDRegClass.hasSubClassEq (RegClass))
108+ return false ;
109+ return EnableFineGrainedStagedRA ? isRegUsedBy2DOr3DInstruction (MRI, R)
110+ : true ;
83111}
112+
84113static bool onlyAllocateMRegisters (const TargetRegisterInfo &TRI,
85114 const MachineRegisterInfo &MRI,
86115 const Register &R) {
0 commit comments