LLVM 19.0.0git
SystemZPostRewrite.cpp
Go to the documentation of this file.
1//==---- SystemZPostRewrite.cpp - Select pseudos after RegAlloc ---*- C++ -*-=//
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 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
12// another.
13//
14//===----------------------------------------------------------------------===//
15
16#include "SystemZ.h"
17#include "SystemZInstrInfo.h"
18#include "SystemZSubtarget.h"
19#include "llvm/ADT/Statistic.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "systemz-postrewrite"
26STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");
27STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");
28
29namespace {
30
31class SystemZPostRewrite : public MachineFunctionPass {
32public:
33 static char ID;
34 SystemZPostRewrite() : MachineFunctionPass(ID) {
36 }
37
38 const SystemZInstrInfo *TII;
39
40 bool runOnMachineFunction(MachineFunction &Fn) override;
41
42private:
43 void selectLOCRMux(MachineBasicBlock &MBB,
46 unsigned LowOpcode,
47 unsigned HighOpcode);
48 void selectSELRMux(MachineBasicBlock &MBB,
51 unsigned LowOpcode,
52 unsigned HighOpcode);
53 bool expandCondMove(MachineBasicBlock &MBB,
58 bool selectMBB(MachineBasicBlock &MBB);
59};
60
61char SystemZPostRewrite::ID = 0;
62
63} // end anonymous namespace
64
65INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite",
66 "SystemZ Post Rewrite pass", false, false)
67
68/// Returns an instance of the Post Rewrite pass.
70 return new SystemZPostRewrite();
71}
72
73// MI is a load-register-on-condition pseudo instruction. Replace it with
74// LowOpcode if source and destination are both low GR32s and HighOpcode if
75// source and destination are both high GR32s. Otherwise, a branch sequence
76// is created.
77void SystemZPostRewrite::selectLOCRMux(MachineBasicBlock &MBB,
80 unsigned LowOpcode,
81 unsigned HighOpcode) {
82 Register DestReg = MBBI->getOperand(0).getReg();
83 Register SrcReg = MBBI->getOperand(2).getReg();
84 bool DestIsHigh = SystemZ::isHighReg(DestReg);
85 bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
86
87 if (!DestIsHigh && !SrcIsHigh)
88 MBBI->setDesc(TII->get(LowOpcode));
89 else if (DestIsHigh && SrcIsHigh)
90 MBBI->setDesc(TII->get(HighOpcode));
91 else
92 expandCondMove(MBB, MBBI, NextMBBI);
93}
94
95// MI is a select pseudo instruction. Replace it with LowOpcode if source
96// and destination are all low GR32s and HighOpcode if source and destination
97// are all high GR32s. Otherwise, a branch sequence is created.
98void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,
101 unsigned LowOpcode,
102 unsigned HighOpcode) {
103 Register DestReg = MBBI->getOperand(0).getReg();
104 Register Src1Reg = MBBI->getOperand(1).getReg();
105 Register Src2Reg = MBBI->getOperand(2).getReg();
106 bool DestIsHigh = SystemZ::isHighReg(DestReg);
107 bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);
108 bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);
109
110 // If sources and destination aren't all high or all low, we may be able to
111 // simplify the operation by moving one of the sources to the destination
112 // first. But only if this doesn't clobber the other source.
113 if (DestReg != Src1Reg && DestReg != Src2Reg) {
114 if (DestIsHigh != Src1IsHigh) {
115 BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
116 TII->get(SystemZ::COPY), DestReg)
117 .addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
118 MBBI->getOperand(1).setReg(DestReg);
119 Src1Reg = DestReg;
120 Src1IsHigh = DestIsHigh;
121 } else if (DestIsHigh != Src2IsHigh) {
122 BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
123 TII->get(SystemZ::COPY), DestReg)
124 .addReg(MBBI->getOperand(2).getReg(), getRegState(MBBI->getOperand(2)));
125 MBBI->getOperand(2).setReg(DestReg);
126 Src2Reg = DestReg;
127 Src2IsHigh = DestIsHigh;
128 }
129 }
130
131 // If the destination (now) matches one source, prefer this to be first.
132 if (DestReg != Src1Reg && DestReg == Src2Reg) {
133 TII->commuteInstruction(*MBBI, false, 1, 2);
134 std::swap(Src1Reg, Src2Reg);
135 std::swap(Src1IsHigh, Src2IsHigh);
136 }
137
138 if (!DestIsHigh && !Src1IsHigh && !Src2IsHigh)
139 MBBI->setDesc(TII->get(LowOpcode));
140 else if (DestIsHigh && Src1IsHigh && Src2IsHigh)
141 MBBI->setDesc(TII->get(HighOpcode));
142 else
143 // Given the simplification above, we must already have a two-operand case.
144 expandCondMove(MBB, MBBI, NextMBBI);
145}
146
147// Replace MBBI by a branch sequence that performs a conditional move of
148// operand 2 to the destination register. Operand 1 is expected to be the
149// same register as the destination.
150bool SystemZPostRewrite::expandCondMove(MachineBasicBlock &MBB,
152 MachineBasicBlock::iterator &NextMBBI) {
154 const BasicBlock *BB = MBB.getBasicBlock();
155 MachineInstr &MI = *MBBI;
156 DebugLoc DL = MI.getDebugLoc();
157 Register DestReg = MI.getOperand(0).getReg();
158 Register SrcReg = MI.getOperand(2).getReg();
159 unsigned CCValid = MI.getOperand(3).getImm();
160 unsigned CCMask = MI.getOperand(4).getImm();
161 assert(DestReg == MI.getOperand(1).getReg() &&
162 "Expected destination and first source operand to be the same.");
163
164 LivePhysRegs LiveRegs(TII->getRegisterInfo());
165 LiveRegs.addLiveOuts(MBB);
166 for (auto I = std::prev(MBB.end()); I != MBBI; --I)
167 LiveRegs.stepBackward(*I);
168
169 // Splice MBB at MI, moving the rest of the block into RestMBB.
171 MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
172 RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
173 RestMBB->transferSuccessors(&MBB);
174 for (MCPhysReg R : LiveRegs)
175 RestMBB->addLiveIn(R);
176
177 // Create a new block MoveMBB to hold the move instruction.
179 MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
180 MoveMBB->addLiveIn(SrcReg);
181 for (MCPhysReg R : LiveRegs)
182 MoveMBB->addLiveIn(R);
183
184 // At the end of MBB, create a conditional branch to RestMBB if the
185 // condition is false, otherwise fall through to MoveMBB.
186 BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
187 .addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);
188 MBB.addSuccessor(RestMBB);
189 MBB.addSuccessor(MoveMBB);
190
191 // In MoveMBB, emit an instruction to move SrcReg into DestReg,
192 // then fall through to RestMBB.
193 BuildMI(*MoveMBB, MoveMBB->end(), DL, TII->get(SystemZ::COPY), DestReg)
194 .addReg(MI.getOperand(2).getReg(), getRegState(MI.getOperand(2)));
195 MoveMBB->addSuccessor(RestMBB);
196
197 NextMBBI = MBB.end();
198 MI.eraseFromParent();
199 LOCRMuxJumps++;
200 return true;
201}
202
203/// If MBBI references a pseudo instruction that should be selected here,
204/// do it and return true. Otherwise return false.
205bool SystemZPostRewrite::selectMI(MachineBasicBlock &MBB,
207 MachineBasicBlock::iterator &NextMBBI) {
208 MachineInstr &MI = *MBBI;
209 unsigned Opcode = MI.getOpcode();
210
211 // Note: If this could be done during regalloc in foldMemoryOperandImpl()
212 // while also updating the LiveIntervals, there would be no need for the
213 // MemFoldPseudo to begin with.
214 int TargetMemOpcode = SystemZ::getTargetMemOpcode(Opcode);
215 if (TargetMemOpcode != -1) {
216 MI.setDesc(TII->get(TargetMemOpcode));
217 MI.tieOperands(0, 1);
218 Register DstReg = MI.getOperand(0).getReg();
219 MachineOperand &SrcMO = MI.getOperand(1);
220 if (DstReg != SrcMO.getReg()) {
221 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), DstReg)
222 .addReg(SrcMO.getReg());
223 SrcMO.setReg(DstReg);
224 MemFoldCopies++;
225 }
226 return true;
227 }
228
229 switch (Opcode) {
230 case SystemZ::LOCRMux:
231 selectLOCRMux(MBB, MBBI, NextMBBI, SystemZ::LOCR, SystemZ::LOCFHR);
232 return true;
233 case SystemZ::SELRMux:
234 selectSELRMux(MBB, MBBI, NextMBBI, SystemZ::SELR, SystemZ::SELFHR);
235 return true;
236 }
237
238 return false;
239}
240
241/// Iterate over the instructions in basic block MBB and select any
242/// pseudo instructions. Return true if anything was modified.
243bool SystemZPostRewrite::selectMBB(MachineBasicBlock &MBB) {
244 bool Modified = false;
245
247 while (MBBI != E) {
248 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
249 Modified |= selectMI(MBB, MBBI, NMBBI);
250 MBBI = NMBBI;
251 }
252
253 return Modified;
254}
255
256bool SystemZPostRewrite::runOnMachineFunction(MachineFunction &MF) {
257 TII = MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
258
259 bool Modified = false;
260 for (auto &MBB : MF)
261 Modified |= selectMBB(MBB);
262
263 return Modified;
264}
265
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition: MD5.cpp:58
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:52
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
int getTargetMemOpcode(uint16_t Opcode)
bool isHighReg(unsigned int Reg)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void initializeSystemZPostRewritePass(PassRegistry &)
unsigned getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
FunctionPass * createSystemZPostRewritePass(SystemZTargetMachine &TM)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860