LLVM  14.0.0git
SystemZCopyPhysRegs.cpp
Go to the documentation of this file.
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 
17 #include "SystemZTargetMachine.h"
25 
26 using namespace llvm;
27 
28 #define SYSTEMZ_COPYPHYSREGS_NAME "SystemZ Copy Physregs"
29 
30 namespace llvm {
32 }
33 
34 namespace {
35 
36 class SystemZCopyPhysRegs : public MachineFunctionPass {
37 public:
38  static char ID;
39  SystemZCopyPhysRegs()
40  : MachineFunctionPass(ID), TII(nullptr), MRI(nullptr) {
42  }
43 
44  StringRef getPassName() const override { return SYSTEMZ_COPYPHYSREGS_NAME; }
45 
46  bool runOnMachineFunction(MachineFunction &MF) override;
47  void getAnalysisUsage(AnalysisUsage &AU) const override;
48 
49 private:
50 
51  bool visitMBB(MachineBasicBlock &MBB);
52 
53  const SystemZInstrInfo *TII;
55 };
56 
58 
59 } // end anonymous namespace
60 
61 INITIALIZE_PASS(SystemZCopyPhysRegs, "systemz-copy-physregs",
62  SYSTEMZ_COPYPHYSREGS_NAME, false, false)
63 
65  return new SystemZCopyPhysRegs();
66 }
67 
68 void SystemZCopyPhysRegs::getAnalysisUsage(AnalysisUsage &AU) const {
69  AU.setPreservesCFG();
71 }
72 
73 bool SystemZCopyPhysRegs::visitMBB(MachineBasicBlock &MBB) {
74  bool Modified = false;
75 
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().
80  MBBI != E; ) {
81  MachineInstr *MI = &*MBBI++;
82  if (!MI->isCopy())
83  continue;
84 
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);
93  else
94  BuildMI(MBB, MI, DL, TII->get(SystemZ::EAR), Tmp).addReg(SrcReg);
95  MI->getOperand(1).setReg(Tmp);
96  Modified = true;
97  }
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);
103  Modified = true;
104  }
105  }
106 
107  return Modified;
108 }
109 
110 bool SystemZCopyPhysRegs::runOnMachineFunction(MachineFunction &F) {
111  TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
112  MRI = &F.getRegInfo();
113 
114  bool Modified = false;
115  for (auto &MBB : F)
116  Modified |= visitMBB(MBB);
117 
118  return Modified;
119 }
120 
llvm::initializeSystemZCopyPhysRegsPass
void initializeSystemZCopyPhysRegsPass(PassRegistry &)
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MachineRegisterInfo::createVirtualRegister
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Definition: MachineRegisterInfo.cpp:158
llvm::MachineRegisterInfo
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Definition: MachineRegisterInfo.h:52
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
TargetInstrInfo.h
llvm::SystemZInstrInfo
Definition: SystemZInstrInfo.h:174
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:102
F
#define F(x, y, z)
Definition: MD5.cpp:56
MachineRegisterInfo.h
TargetMachine.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
LoopDeletionResult::Modified
@ Modified
INITIALIZE_PASS
INITIALIZE_PASS(SystemZCopyPhysRegs, "systemz-copy-physregs", SYSTEMZ_COPYPHYSREGS_NAME, false, false) FunctionPass *llvm
Definition: SystemZCopyPhysRegs.cpp:61
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::Register::isVirtual
bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:91
SystemZTargetMachine.h
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
SystemZMachineFunctionInfo.h
llvm::SystemZTargetMachine
Definition: SystemZTargetMachine.h:27
MachineFunctionPass.h
SYSTEMZ_COPYPHYSREGS_NAME
#define SYSTEMZ_COPYPHYSREGS_NAME
Definition: SystemZCopyPhysRegs.cpp:28
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::MachineFunction
Definition: MachineFunction.h:230
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
MBBI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Definition: AArch64SLSHardening.cpp:75
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::createSystemZCopyPhysRegsPass
FunctionPass * createSystemZCopyPhysRegsPass(SystemZTargetMachine &TM)
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::MachineInstrBundleIterator< MachineInstr >
TargetRegisterInfo.h
llvm::SystemZISD::IPM
@ IPM
Definition: SystemZISelLowering.h:152
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:270
MachineDominators.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37