1 //===---------- SystemZPhysRegCopy.cpp - Handle phys reg copies -----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass makes sure that a COPY of a physical register will be
10 // implementable after register allocation in copyPhysReg() (this could be
11 // done in EmitInstrWithCustomInserter() instead if COPY instructions would
14 //===----------------------------------------------------------------------===//
16 #include "SystemZMachineFunctionInfo.h"
17 #include "SystemZTargetMachine.h"
18 #include "llvm/CodeGen/MachineDominators.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/TargetInstrInfo.h"
23 #include "llvm/CodeGen/TargetRegisterInfo.h"
24 #include "llvm/Target/TargetMachine.h"
28 #define SYSTEMZ_COPYPHYSREGS_NAME "SystemZ Copy Physregs"
31 void initializeSystemZCopyPhysRegsPass(PassRegistry
&);
36 class SystemZCopyPhysRegs
: public MachineFunctionPass
{
40 : MachineFunctionPass(ID
), TII(nullptr), MRI(nullptr) {
41 initializeSystemZCopyPhysRegsPass(*PassRegistry::getPassRegistry());
44 StringRef
getPassName() const override
{ return SYSTEMZ_COPYPHYSREGS_NAME
; }
46 bool runOnMachineFunction(MachineFunction
&MF
) override
;
47 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
51 bool visitMBB(MachineBasicBlock
&MBB
);
53 const SystemZInstrInfo
*TII
;
54 MachineRegisterInfo
*MRI
;
57 char SystemZCopyPhysRegs::ID
= 0;
59 } // end anonymous namespace
61 INITIALIZE_PASS(SystemZCopyPhysRegs
, "systemz-copy-physregs",
62 SYSTEMZ_COPYPHYSREGS_NAME
, false, false)
64 FunctionPass
*llvm::createSystemZCopyPhysRegsPass(SystemZTargetMachine
&TM
) {
65 return new SystemZCopyPhysRegs();
68 void SystemZCopyPhysRegs::getAnalysisUsage(AnalysisUsage
&AU
) const {
70 MachineFunctionPass::getAnalysisUsage(AU
);
73 bool SystemZCopyPhysRegs::visitMBB(MachineBasicBlock
&MBB
) {
74 bool Modified
= false;
76 // Certain special registers can only be copied from a subset of the
77 // default register class of the type. It is therefore necessary to create
78 // the target copy instructions before regalloc instead of in copyPhysReg().
79 for (MachineBasicBlock::iterator MBBI
= MBB
.begin(), E
= MBB
.end();
81 MachineInstr
*MI
= &*MBBI
++;
85 DebugLoc DL
= MI
->getDebugLoc();
86 Register SrcReg
= MI
->getOperand(1).getReg();
87 Register DstReg
= MI
->getOperand(0).getReg();
88 if (DstReg
.isVirtual() &&
89 (SrcReg
== SystemZ::CC
|| SystemZ::AR32BitRegClass
.contains(SrcReg
))) {
90 Register Tmp
= MRI
->createVirtualRegister(&SystemZ::GR32BitRegClass
);
91 if (SrcReg
== SystemZ::CC
)
92 BuildMI(MBB
, MI
, DL
, TII
->get(SystemZ::IPM
), Tmp
);
94 BuildMI(MBB
, MI
, DL
, TII
->get(SystemZ::EAR
), Tmp
).addReg(SrcReg
);
95 MI
->getOperand(1).setReg(Tmp
);
98 else if (SrcReg
.isVirtual() &&
99 SystemZ::AR32BitRegClass
.contains(DstReg
)) {
100 Register Tmp
= MRI
->createVirtualRegister(&SystemZ::GR32BitRegClass
);
101 MI
->getOperand(0).setReg(Tmp
);
102 BuildMI(MBB
, MBBI
, DL
, TII
->get(SystemZ::SAR
), DstReg
).addReg(Tmp
);
110 bool SystemZCopyPhysRegs::runOnMachineFunction(MachineFunction
&F
) {
111 TII
= static_cast<const SystemZInstrInfo
*>(F
.getSubtarget().getInstrInfo());
112 MRI
= &F
.getRegInfo();
114 bool Modified
= false;
116 Modified
|= visitMBB(MBB
);