LLVM 24.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"
27#include "llvm/IR/Analysis.h"
28using namespace llvm;
29
30#define DEBUG_TYPE "systemz-post-rewrite"
31STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");
32STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");
33
34namespace {
35
36class SystemZPostRewriteLegacy : public MachineFunctionPass {
37public:
38 static char ID;
39 SystemZPostRewriteLegacy() : MachineFunctionPass(ID) {}
40
41 bool runOnMachineFunction(MachineFunction &Fn) override;
42};
43
44char SystemZPostRewriteLegacy::ID = 0;
45
46} // end anonymous namespace
47
48INITIALIZE_PASS(SystemZPostRewriteLegacy, "systemz-post-rewrite",
49 "SystemZ Post Rewrite pass", false, false)
50
51/// Returns an instance of the Post Rewrite pass.
54 return new SystemZPostRewriteLegacy();
55}
56
57// Replace MBBI by a branch sequence that performs a conditional move of
58// operand 2 to the destination register. Operand 1 is expected to be the
59// same register as the destination.
63 const TargetInstrInfo *TII) {
64 MachineFunction &MF = *MBB.getParent();
65 const BasicBlock *BB = MBB.getBasicBlock();
67 DebugLoc DL = MI.getDebugLoc();
68 Register DestReg = MI.getOperand(0).getReg();
69 Register SrcReg = MI.getOperand(2).getReg();
70 unsigned CCValid = MI.getOperand(3).getImm();
71 unsigned CCMask = MI.getOperand(4).getImm();
72 assert(DestReg == MI.getOperand(1).getReg() &&
73 "Expected destination and first source operand to be the same.");
74
75 LivePhysRegs LiveRegs(TII->getRegisterInfo());
76 LiveRegs.addLiveOuts(MBB);
77 for (auto I = std::prev(MBB.end()); I != MBBI; --I)
78 LiveRegs.stepBackward(*I);
79
80 // Splice MBB at MI, moving the rest of the block into RestMBB.
82 MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
83 RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
84 RestMBB->transferSuccessors(&MBB);
85 for (MCPhysReg R : LiveRegs)
86 RestMBB->addLiveIn(R);
87
88 // Create a new block MoveMBB to hold the move instruction.
90 MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
91 MoveMBB->addLiveIn(SrcReg);
92 for (MCPhysReg R : LiveRegs)
93 MoveMBB->addLiveIn(R);
94
95 // At the end of MBB, create a conditional branch to RestMBB if the
96 // condition is false, otherwise fall through to MoveMBB.
97 BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
98 .addImm(CCValid)
99 .addImm(CCMask ^ CCValid)
100 .addMBB(RestMBB);
101 MBB.addSuccessor(RestMBB);
102 MBB.addSuccessor(MoveMBB);
103
104 // In MoveMBB, emit an instruction to move SrcReg into DestReg,
105 // then fall through to RestMBB.
106 MachineInstr *CopyInst =
107 BuildMI(*MoveMBB, MoveMBB->end(), DL, TII->get(SystemZ::COPY), DestReg)
108 .addReg(MI.getOperand(2).getReg(), getRegState(MI.getOperand(2)));
109 // record the debug value substitution for CopyInst
110 MBB.getParent()->substituteDebugValuesForInst(*MBBI, *CopyInst, 1);
111 MoveMBB->addSuccessor(RestMBB);
112
113 NextMBBI = MBB.end();
114 MI.eraseFromParent();
115 LOCRMuxJumps++;
116 return true;
117}
118
119// MI is a load-register-on-condition pseudo instruction. Replace it with
120// LowOpcode if source and destination are both low GR32s and HighOpcode if
121// source and destination are both high GR32s. Otherwise, a branch sequence
122// is created.
126 unsigned LowOpcode, unsigned HighOpcode,
127 const TargetInstrInfo *TII) {
128 Register DestReg = MBBI->getOperand(0).getReg();
129 Register SrcReg = MBBI->getOperand(2).getReg();
130 bool DestIsHigh = SystemZ::isHighReg(DestReg);
131 bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
132
133 if (!DestIsHigh && !SrcIsHigh)
134 MBBI->setDesc(TII->get(LowOpcode));
135 else if (DestIsHigh && SrcIsHigh)
136 MBBI->setDesc(TII->get(HighOpcode));
137 else
138 expandCondMove(MBB, MBBI, NextMBBI, TII);
139}
140
141// MI is a select pseudo instruction. Replace it with LowOpcode if source
142// and destination are all low GR32s and HighOpcode if source and destination
143// are all high GR32s. Otherwise, a branch sequence is created.
147 unsigned LowOpcode, unsigned HighOpcode,
148 const TargetInstrInfo *TII) {
149 Register DestReg = MBBI->getOperand(0).getReg();
150 MachineOperand &Src1MO = MBBI->getOperand(1);
151 MachineOperand &Src2MO = MBBI->getOperand(2);
152 Register Src1Reg = Src1MO.getReg();
153 Register Src2Reg = Src2MO.getReg();
154 bool DestIsHigh = SystemZ::isHighReg(DestReg);
155 bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);
156 bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);
157 // A copy instruction that we might create, held here for the purpose of
158 // debug instr value tracking.
159 MachineInstr *CopyInst = nullptr;
160
161 // In rare cases both sources are the same register (after
162 // machine-cse). This must be handled as it may lead to wrong-code (after
163 // machine-cp) if the kill flag on Src1 isn't cleared (with
164 // expandCondMove()).
165 if (Src1Reg == Src2Reg) {
166 CopyInst = BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
167 TII->get(SystemZ::COPY), DestReg)
168 .addReg(Src1Reg, getRegState(Src1MO) & getRegState(Src2MO));
169 MBB.getParent()->substituteDebugValuesForInst(*MBBI, *CopyInst, 1);
170 MBBI->eraseFromParent();
171 return;
172 }
173
174 // If sources and destination aren't all high or all low, we may be able to
175 // simplify the operation by moving one of the sources to the destination
176 // first. But only if this doesn't clobber the other source.
177 if (DestReg != Src1Reg && DestReg != Src2Reg) {
178 if (DestIsHigh != Src1IsHigh) {
179 CopyInst = BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
180 TII->get(SystemZ::COPY), DestReg)
181 .addReg(Src1Reg, getRegState(Src1MO));
182 Src1MO.setReg(DestReg);
183 Src1Reg = DestReg;
184 Src1IsHigh = DestIsHigh;
185 } else if (DestIsHigh != Src2IsHigh) {
186 CopyInst = BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
187 TII->get(SystemZ::COPY), DestReg)
188 .addReg(Src2Reg, getRegState(Src2MO));
189 Src2MO.setReg(DestReg);
190 Src2Reg = DestReg;
191 Src2IsHigh = DestIsHigh;
192 }
193 }
194 // if a copy instruction was inserted, record the debug value substitution
195 if (CopyInst)
196 MBB.getParent()->substituteDebugValuesForInst(*MBBI, *CopyInst, 1);
197
198 // If the destination (now) matches one source, prefer this to be first.
199 if (DestReg != Src1Reg && DestReg == Src2Reg) {
200 TII->commuteInstruction(*MBBI, false, 1, 2);
201 std::swap(Src1Reg, Src2Reg);
202 std::swap(Src1IsHigh, Src2IsHigh);
203 }
204
205 if (!DestIsHigh && !Src1IsHigh && !Src2IsHigh)
206 MBBI->setDesc(TII->get(LowOpcode));
207 else if (DestIsHigh && Src1IsHigh && Src2IsHigh)
208 MBBI->setDesc(TII->get(HighOpcode));
209 else
210 // Given the simplification above, we must already have a two-operand case.
211 expandCondMove(MBB, MBBI, NextMBBI, TII);
212}
213
214/// If MBBI references a pseudo instruction that should be selected here,
215/// do it and return true. Otherwise return false.
218 const TargetInstrInfo *TII) {
219 MachineInstr &MI = *MBBI;
220 unsigned Opcode = MI.getOpcode();
221
222 // Note: If this could be done during regalloc in foldMemoryOperandImpl()
223 // while also updating the LiveIntervals, there would be no need for the
224 // MemFoldPseudo to begin with.
225 int TargetMemOpcode = SystemZ::getTargetMemOpcode(Opcode);
226 if (TargetMemOpcode != -1) {
227 MI.setDesc(TII->get(TargetMemOpcode));
228 MI.tieOperands(0, 1);
229 Register DstReg = MI.getOperand(0).getReg();
230 MachineOperand &SrcMO = MI.getOperand(1);
231 if (DstReg != SrcMO.getReg()) {
232 BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), DstReg)
233 .addReg(SrcMO.getReg());
234 SrcMO.setReg(DstReg);
235 MemFoldCopies++;
236 }
237 return true;
238 }
239
240 switch (Opcode) {
241 case SystemZ::LOCRMux:
242 selectLOCRMux(MBB, MBBI, NextMBBI, SystemZ::LOCR, SystemZ::LOCFHR, TII);
243 return true;
244 case SystemZ::SELRMux:
245 selectSELRMux(MBB, MBBI, NextMBBI, SystemZ::SELR, SystemZ::SELFHR, TII);
246 return true;
247 }
248
249 return false;
250}
251
252/// Iterate over the instructions in basic block MBB and select any
253/// pseudo instructions. Return true if anything was modified.
255 bool Modified = false;
256
257 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
258 while (MBBI != E) {
259 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
260 Modified |= selectMI(MBB, MBBI, NMBBI, TII);
261 MBBI = NMBBI;
262 }
263
264 return Modified;
265}
266
268 const TargetInstrInfo *TII =
269 MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
270
271 bool Modified = false;
272 for (auto &MBB : MF)
274
275 return Modified;
276}
277
278bool SystemZPostRewriteLegacy::runOnMachineFunction(MachineFunction &MF) {
279 return runPostRewriteOnMF(MF);
280}
281
282PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:57
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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:171
static bool expandCondMove(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const TargetInstrInfo *TII)
static bool selectMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, const TargetInstrInfo *TII)
If MBBI references a pseudo instruction that should be selected here, do it and return true.
static bool selectMBB(MachineBasicBlock &MBB, const TargetInstrInfo *TII)
Iterate over the instructions in basic block MBB and select any pseudo instructions.
static void selectSELRMux(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, unsigned LowOpcode, unsigned HighOpcode, const TargetInstrInfo *TII)
static void selectLOCRMux(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NextMBBI, unsigned LowOpcode, unsigned HighOpcode, const TargetInstrInfo *TII)
static bool runPostRewriteOnMF(MachineFunction &MF)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
A set of physical registers with utility functions to track liveness when walking backward/forward th...
LLVM_ABI void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
LLVM_ABI 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.
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 '...
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
BasicBlockListType::iterator iterator
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
TargetInstrInfo - Interface to description of machine instruction set.
Pass manager infrastructure for declaring and invalidating analyses.
int32_t getTargetMemOpcode(uint32_t Opcode)
bool isHighReg(unsigned int Reg)
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
RegState getRegState(const MachineOperand &RegOp)
Get all register state flags from machine operand RegOp.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
FunctionPass * createSystemZPostRewriteLegacyPass(SystemZTargetMachine &TM)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881