1*da58b97aSjoerg //===---------- SystemZPhysRegCopy.cpp - Handle phys reg copies -----------===//
2*da58b97aSjoerg //
3*da58b97aSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*da58b97aSjoerg // See https://llvm.org/LICENSE.txt for license information.
5*da58b97aSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*da58b97aSjoerg //
7*da58b97aSjoerg //===----------------------------------------------------------------------===//
8*da58b97aSjoerg //
9*da58b97aSjoerg // This pass makes sure that a COPY of a physical register will be
10*da58b97aSjoerg // implementable after register allocation in copyPhysReg() (this could be
11*da58b97aSjoerg // done in EmitInstrWithCustomInserter() instead if COPY instructions would
12*da58b97aSjoerg // be passed to it).
13*da58b97aSjoerg //
14*da58b97aSjoerg //===----------------------------------------------------------------------===//
15*da58b97aSjoerg 
16*da58b97aSjoerg #include "SystemZMachineFunctionInfo.h"
17*da58b97aSjoerg #include "SystemZTargetMachine.h"
18*da58b97aSjoerg #include "llvm/CodeGen/MachineDominators.h"
19*da58b97aSjoerg #include "llvm/CodeGen/MachineFunctionPass.h"
20*da58b97aSjoerg #include "llvm/CodeGen/MachineInstrBuilder.h"
21*da58b97aSjoerg #include "llvm/CodeGen/MachineRegisterInfo.h"
22*da58b97aSjoerg #include "llvm/CodeGen/TargetInstrInfo.h"
23*da58b97aSjoerg #include "llvm/CodeGen/TargetRegisterInfo.h"
24*da58b97aSjoerg #include "llvm/Target/TargetMachine.h"
25*da58b97aSjoerg 
26*da58b97aSjoerg using namespace llvm;
27*da58b97aSjoerg 
28*da58b97aSjoerg #define SYSTEMZ_COPYPHYSREGS_NAME "SystemZ Copy Physregs"
29*da58b97aSjoerg 
30*da58b97aSjoerg namespace llvm {
31*da58b97aSjoerg   void initializeSystemZCopyPhysRegsPass(PassRegistry&);
32*da58b97aSjoerg }
33*da58b97aSjoerg 
34*da58b97aSjoerg namespace {
35*da58b97aSjoerg 
36*da58b97aSjoerg class SystemZCopyPhysRegs : public MachineFunctionPass {
37*da58b97aSjoerg public:
38*da58b97aSjoerg   static char ID;
SystemZCopyPhysRegs()39*da58b97aSjoerg   SystemZCopyPhysRegs()
40*da58b97aSjoerg     : MachineFunctionPass(ID), TII(nullptr), MRI(nullptr) {
41*da58b97aSjoerg     initializeSystemZCopyPhysRegsPass(*PassRegistry::getPassRegistry());
42*da58b97aSjoerg   }
43*da58b97aSjoerg 
getPassName() const44*da58b97aSjoerg   StringRef getPassName() const override { return SYSTEMZ_COPYPHYSREGS_NAME; }
45*da58b97aSjoerg 
46*da58b97aSjoerg   bool runOnMachineFunction(MachineFunction &MF) override;
47*da58b97aSjoerg   void getAnalysisUsage(AnalysisUsage &AU) const override;
48*da58b97aSjoerg 
49*da58b97aSjoerg private:
50*da58b97aSjoerg 
51*da58b97aSjoerg   bool visitMBB(MachineBasicBlock &MBB);
52*da58b97aSjoerg 
53*da58b97aSjoerg   const SystemZInstrInfo *TII;
54*da58b97aSjoerg   MachineRegisterInfo *MRI;
55*da58b97aSjoerg };
56*da58b97aSjoerg 
57*da58b97aSjoerg char SystemZCopyPhysRegs::ID = 0;
58*da58b97aSjoerg 
59*da58b97aSjoerg } // end anonymous namespace
60*da58b97aSjoerg 
61*da58b97aSjoerg INITIALIZE_PASS(SystemZCopyPhysRegs, "systemz-copy-physregs",
62*da58b97aSjoerg                 SYSTEMZ_COPYPHYSREGS_NAME, false, false)
63*da58b97aSjoerg 
createSystemZCopyPhysRegsPass(SystemZTargetMachine & TM)64*da58b97aSjoerg FunctionPass *llvm::createSystemZCopyPhysRegsPass(SystemZTargetMachine &TM) {
65*da58b97aSjoerg   return new SystemZCopyPhysRegs();
66*da58b97aSjoerg }
67*da58b97aSjoerg 
getAnalysisUsage(AnalysisUsage & AU) const68*da58b97aSjoerg void SystemZCopyPhysRegs::getAnalysisUsage(AnalysisUsage &AU) const {
69*da58b97aSjoerg   AU.setPreservesCFG();
70*da58b97aSjoerg   MachineFunctionPass::getAnalysisUsage(AU);
71*da58b97aSjoerg }
72*da58b97aSjoerg 
visitMBB(MachineBasicBlock & MBB)73*da58b97aSjoerg bool SystemZCopyPhysRegs::visitMBB(MachineBasicBlock &MBB) {
74*da58b97aSjoerg   bool Modified = false;
75*da58b97aSjoerg 
76*da58b97aSjoerg   // Certain special registers can only be copied from a subset of the
77*da58b97aSjoerg   // default register class of the type. It is therefore necessary to create
78*da58b97aSjoerg   // the target copy instructions before regalloc instead of in copyPhysReg().
79*da58b97aSjoerg   for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
80*da58b97aSjoerg        MBBI != E; ) {
81*da58b97aSjoerg     MachineInstr *MI = &*MBBI++;
82*da58b97aSjoerg     if (!MI->isCopy())
83*da58b97aSjoerg       continue;
84*da58b97aSjoerg 
85*da58b97aSjoerg     DebugLoc DL = MI->getDebugLoc();
86*da58b97aSjoerg     Register SrcReg = MI->getOperand(1).getReg();
87*da58b97aSjoerg     Register DstReg = MI->getOperand(0).getReg();
88*da58b97aSjoerg     if (DstReg.isVirtual() &&
89*da58b97aSjoerg         (SrcReg == SystemZ::CC || SystemZ::AR32BitRegClass.contains(SrcReg))) {
90*da58b97aSjoerg       Register Tmp = MRI->createVirtualRegister(&SystemZ::GR32BitRegClass);
91*da58b97aSjoerg       if (SrcReg == SystemZ::CC)
92*da58b97aSjoerg         BuildMI(MBB, MI, DL, TII->get(SystemZ::IPM), Tmp);
93*da58b97aSjoerg       else
94*da58b97aSjoerg         BuildMI(MBB, MI, DL, TII->get(SystemZ::EAR), Tmp).addReg(SrcReg);
95*da58b97aSjoerg       MI->getOperand(1).setReg(Tmp);
96*da58b97aSjoerg       Modified = true;
97*da58b97aSjoerg     }
98*da58b97aSjoerg     else if (SrcReg.isVirtual() &&
99*da58b97aSjoerg              SystemZ::AR32BitRegClass.contains(DstReg)) {
100*da58b97aSjoerg       Register Tmp = MRI->createVirtualRegister(&SystemZ::GR32BitRegClass);
101*da58b97aSjoerg       MI->getOperand(0).setReg(Tmp);
102*da58b97aSjoerg       BuildMI(MBB, MBBI, DL, TII->get(SystemZ::SAR), DstReg).addReg(Tmp);
103*da58b97aSjoerg       Modified = true;
104*da58b97aSjoerg     }
105*da58b97aSjoerg   }
106*da58b97aSjoerg 
107*da58b97aSjoerg   return Modified;
108*da58b97aSjoerg }
109*da58b97aSjoerg 
runOnMachineFunction(MachineFunction & F)110*da58b97aSjoerg bool SystemZCopyPhysRegs::runOnMachineFunction(MachineFunction &F) {
111*da58b97aSjoerg   TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
112*da58b97aSjoerg   MRI = &F.getRegInfo();
113*da58b97aSjoerg 
114*da58b97aSjoerg   bool Modified = false;
115*da58b97aSjoerg   for (auto &MBB : F)
116*da58b97aSjoerg     Modified |= visitMBB(MBB);
117*da58b97aSjoerg 
118*da58b97aSjoerg   return Modified;
119*da58b97aSjoerg }
120*da58b97aSjoerg 
121