1 //==---- SystemZPostRewrite.cpp - Select pseudos after RegAlloc ---*- C++ -*-=//
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 file contains a pass that is run immediately after VirtRegRewriter
10 // but before MachineCopyPropagation. The purpose is to lower pseudos to
11 // target instructions before any later pass might substitute a register for
14 //===----------------------------------------------------------------------===//
17 #include "SystemZInstrInfo.h"
18 #include "SystemZSubtarget.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #define SYSTEMZ_POSTREWRITE_NAME "SystemZ Post Rewrite pass"
26 #define DEBUG_TYPE "systemz-postrewrite"
27 STATISTIC(MemFoldCopies
, "Number of copies inserted before folded mem ops.");
30 void initializeSystemZPostRewritePass(PassRegistry
&);
35 class SystemZPostRewrite
: public MachineFunctionPass
{
38 SystemZPostRewrite() : MachineFunctionPass(ID
) {
39 initializeSystemZPostRewritePass(*PassRegistry::getPassRegistry());
42 const SystemZInstrInfo
*TII
;
44 bool runOnMachineFunction(MachineFunction
&Fn
) override
;
46 StringRef
getPassName() const override
{ return SYSTEMZ_POSTREWRITE_NAME
; }
48 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
50 MachineFunctionPass::getAnalysisUsage(AU
);
54 bool selectMI(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MBBI
,
55 MachineBasicBlock::iterator
&NextMBBI
);
56 bool selectMBB(MachineBasicBlock
&MBB
);
59 char SystemZPostRewrite::ID
= 0;
61 } // end anonymous namespace
63 INITIALIZE_PASS(SystemZPostRewrite
, "systemz-post-rewrite",
64 SYSTEMZ_POSTREWRITE_NAME
, false, false)
66 /// Returns an instance of the Post Rewrite pass.
67 FunctionPass
*llvm::createSystemZPostRewritePass(SystemZTargetMachine
&TM
) {
68 return new SystemZPostRewrite();
71 /// If MBBI references a pseudo instruction that should be selected here,
72 /// do it and return true. Otherwise return false.
73 bool SystemZPostRewrite::selectMI(MachineBasicBlock
&MBB
,
74 MachineBasicBlock::iterator MBBI
,
75 MachineBasicBlock::iterator
&NextMBBI
) {
76 MachineInstr
&MI
= *MBBI
;
77 unsigned Opcode
= MI
.getOpcode();
79 // Note: If this could be done during regalloc in foldMemoryOperandImpl()
80 // while also updating the LiveIntervals, there would be no need for the
81 // MemFoldPseudo to begin with.
82 int TargetMemOpcode
= SystemZ::getTargetMemOpcode(Opcode
);
83 if (TargetMemOpcode
!= -1) {
84 MI
.setDesc(TII
->get(TargetMemOpcode
));
86 unsigned DstReg
= MI
.getOperand(0).getReg();
87 MachineOperand
&SrcMO
= MI
.getOperand(1);
88 if (DstReg
!= SrcMO
.getReg()) {
89 BuildMI(MBB
, &MI
, MI
.getDebugLoc(), TII
->get(SystemZ::COPY
), DstReg
)
90 .addReg(SrcMO
.getReg());
100 /// Iterate over the instructions in basic block MBB and select any
101 /// pseudo instructions. Return true if anything was modified.
102 bool SystemZPostRewrite::selectMBB(MachineBasicBlock
&MBB
) {
103 bool Modified
= false;
105 MachineBasicBlock::iterator MBBI
= MBB
.begin(), E
= MBB
.end();
107 MachineBasicBlock::iterator NMBBI
= std::next(MBBI
);
108 Modified
|= selectMI(MBB
, MBBI
, NMBBI
);
115 bool SystemZPostRewrite::runOnMachineFunction(MachineFunction
&MF
) {
116 TII
= static_cast<const SystemZInstrInfo
*>(MF
.getSubtarget().getInstrInfo());
118 bool Modified
= false;
120 Modified
|= selectMBB(MBB
);