1//===---------- SystemZPhysRegCopy.cpp - Handle phys reg copies -----------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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
12// be passed to it).
13//
14//===----------------------------------------------------------------------===//
15
16#include "SystemZ.h"
17#include "SystemZTargetMachine.h"
18#include "llvm/CodeGen/MachineDominators.h"
19#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachinePassManager.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/TargetInstrInfo.h"
25#include "llvm/CodeGen/TargetRegisterInfo.h"
26#include "llvm/IR/Analysis.h"
27
28using namespace llvm;
29
30namespace {
31
32class SystemZCopyPhysRegsLegacy : public MachineFunctionPass {
33public:
34 static char ID;
35 SystemZCopyPhysRegsLegacy() : MachineFunctionPass(ID) {}
36
37 bool runOnMachineFunction(MachineFunction &MF) override;
38 void getAnalysisUsage(AnalysisUsage &AU) const override;
39};
40
41char SystemZCopyPhysRegsLegacy::ID = 0;
42
43} // end anonymous namespace
44
45INITIALIZE_PASS(SystemZCopyPhysRegsLegacy, "systemz-copy-physregs",
46 "SystemZ Copy Physregs", false, false)
47
48FunctionPass *
49llvm::createSystemZCopyPhysRegsLegacyPass(SystemZTargetMachine &TM) {
50 return new SystemZCopyPhysRegsLegacy();
51}
52
53void SystemZCopyPhysRegsLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesCFG();
55 MachineFunctionPass::getAnalysisUsage(AU);
56}
57
58static bool visitMBB(MachineBasicBlock &MBB, MachineRegisterInfo *MRI,
59 const TargetInstrInfo *TII) {
60 bool Modified = false;
61
62 // Certain special registers can only be copied from a subset of the
63 // default register class of the type. It is therefore necessary to create
64 // the target copy instructions before regalloc instead of in copyPhysReg().
65 for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
66 MBBI != E; ) {
67 MachineInstr *MI = &*MBBI++;
68 if (!MI->isCopy())
69 continue;
70
71 DebugLoc DL = MI->getDebugLoc();
72 Register SrcReg = MI->getOperand(i: 1).getReg();
73 Register DstReg = MI->getOperand(i: 0).getReg();
74
75 if (DstReg.isVirtual() && SrcReg.isPhysical() &&
76 (SrcReg == SystemZ::CC || SystemZ::AR32BitRegClass.contains(Reg: SrcReg))) {
77 Register Tmp = MRI->createVirtualRegister(RegClass: &SystemZ::GR32BitRegClass);
78 if (SrcReg == SystemZ::CC)
79 BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII->get(Opcode: SystemZ::IPM), DestReg: Tmp);
80 else
81 BuildMI(BB&: MBB, I: MI, MIMD: DL, MCID: TII->get(Opcode: SystemZ::EAR), DestReg: Tmp).addReg(RegNo: SrcReg);
82 MI->getOperand(i: 1).setReg(Tmp);
83 Modified = true;
84 } else if (SrcReg.isVirtual() && DstReg.isPhysical() &&
85 SystemZ::AR32BitRegClass.contains(Reg: DstReg)) {
86 Register Tmp = MRI->createVirtualRegister(RegClass: &SystemZ::GR32BitRegClass);
87 MI->getOperand(i: 0).setReg(Tmp);
88 MachineInstr *NMI =
89 BuildMI(BB&: MBB, I: MBBI, MIMD: DL, MCID: TII->get(Opcode: SystemZ::SAR), DestReg: DstReg).addReg(RegNo: Tmp);
90 // SAR now writes the final value to DstReg, so update debug values.
91 MBB.getParent()->substituteDebugValuesForInst(Old: *MI, New&: *NMI);
92 Modified = true;
93 }
94 }
95
96 return Modified;
97}
98
99static bool runCopyPhysRegsOnMF(MachineFunction &F) {
100 const TargetInstrInfo *TII =
101 F.getSubtarget<SystemZSubtarget>().getInstrInfo();
102 MachineRegisterInfo *MRI = &F.getRegInfo();
103
104 bool Modified = false;
105 for (auto &MBB : F)
106 Modified |= visitMBB(MBB, MRI, TII);
107
108 return Modified;
109}
110
111bool SystemZCopyPhysRegsLegacy::runOnMachineFunction(MachineFunction &MF) {
112 return runCopyPhysRegsOnMF(F&: MF);
113}
114
115PreservedAnalyses
116SystemZCopyPhysRegsPass::run(MachineFunction &MF,
117 MachineFunctionAnalysisManager &MFAM) {
118 return runCopyPhysRegsOnMF(F&: MF) ? getMachineFunctionPassPreservedAnalyses()
119 .preserveSet<CFGAnalyses>()
120 : PreservedAnalyses::all();
121}
122