1*0b57cec5SDimitry Andric //==---- SystemZPostRewrite.cpp - Select pseudos after RegAlloc ---*- C++ -*-=//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file contains a pass that is run immediately after VirtRegRewriter
10*0b57cec5SDimitry Andric // but before MachineCopyPropagation. The purpose is to lower pseudos to
11*0b57cec5SDimitry Andric // target instructions before any later pass might substitute a register for
12*0b57cec5SDimitry Andric // another.
13*0b57cec5SDimitry Andric //
14*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
15*0b57cec5SDimitry Andric
16*0b57cec5SDimitry Andric #include "SystemZ.h"
17*0b57cec5SDimitry Andric #include "SystemZInstrInfo.h"
18*0b57cec5SDimitry Andric #include "SystemZSubtarget.h"
19*0b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
20*0b57cec5SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h"
21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
22*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
23*0b57cec5SDimitry Andric using namespace llvm;
24*0b57cec5SDimitry Andric
25*0b57cec5SDimitry Andric #define DEBUG_TYPE "systemz-postrewrite"
26*0b57cec5SDimitry Andric STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");
27*0b57cec5SDimitry Andric STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");
28*0b57cec5SDimitry Andric
29*0b57cec5SDimitry Andric namespace {
30*0b57cec5SDimitry Andric
31*0b57cec5SDimitry Andric class SystemZPostRewrite : public MachineFunctionPass {
32*0b57cec5SDimitry Andric public:
33*0b57cec5SDimitry Andric static char ID;
SystemZPostRewrite()34*0b57cec5SDimitry Andric SystemZPostRewrite() : MachineFunctionPass(ID) {
35*0b57cec5SDimitry Andric initializeSystemZPostRewritePass(*PassRegistry::getPassRegistry());
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric
38*0b57cec5SDimitry Andric const SystemZInstrInfo *TII;
39*0b57cec5SDimitry Andric
40*0b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &Fn) override;
41*0b57cec5SDimitry Andric
42*0b57cec5SDimitry Andric private:
43*0b57cec5SDimitry Andric void selectLOCRMux(MachineBasicBlock &MBB,
44*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI,
45*0b57cec5SDimitry Andric MachineBasicBlock::iterator &NextMBBI,
46*0b57cec5SDimitry Andric unsigned LowOpcode,
47*0b57cec5SDimitry Andric unsigned HighOpcode);
48*0b57cec5SDimitry Andric void selectSELRMux(MachineBasicBlock &MBB,
49*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI,
50*0b57cec5SDimitry Andric MachineBasicBlock::iterator &NextMBBI,
51*0b57cec5SDimitry Andric unsigned LowOpcode,
52*0b57cec5SDimitry Andric unsigned HighOpcode);
53*0b57cec5SDimitry Andric bool expandCondMove(MachineBasicBlock &MBB,
54*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI,
55*0b57cec5SDimitry Andric MachineBasicBlock::iterator &NextMBBI);
56*0b57cec5SDimitry Andric bool selectMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
57*0b57cec5SDimitry Andric MachineBasicBlock::iterator &NextMBBI);
58*0b57cec5SDimitry Andric bool selectMBB(MachineBasicBlock &MBB);
59*0b57cec5SDimitry Andric };
60*0b57cec5SDimitry Andric
61*0b57cec5SDimitry Andric char SystemZPostRewrite::ID = 0;
62*0b57cec5SDimitry Andric
63*0b57cec5SDimitry Andric } // end anonymous namespace
64*0b57cec5SDimitry Andric
65*0b57cec5SDimitry Andric INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite",
66*0b57cec5SDimitry Andric "SystemZ Post Rewrite pass", false, false)
67*0b57cec5SDimitry Andric
68*0b57cec5SDimitry Andric /// Returns an instance of the Post Rewrite pass.
createSystemZPostRewritePass(SystemZTargetMachine & TM)69*0b57cec5SDimitry Andric FunctionPass *llvm::createSystemZPostRewritePass(SystemZTargetMachine &TM) {
70*0b57cec5SDimitry Andric return new SystemZPostRewrite();
71*0b57cec5SDimitry Andric }
72*0b57cec5SDimitry Andric
73*0b57cec5SDimitry Andric // MI is a load-register-on-condition pseudo instruction. Replace it with
74*0b57cec5SDimitry Andric // LowOpcode if source and destination are both low GR32s and HighOpcode if
75*0b57cec5SDimitry Andric // source and destination are both high GR32s. Otherwise, a branch sequence
76*0b57cec5SDimitry Andric // is created.
selectLOCRMux(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI,unsigned LowOpcode,unsigned HighOpcode)77*0b57cec5SDimitry Andric void SystemZPostRewrite::selectLOCRMux(MachineBasicBlock &MBB,
78*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI,
79*0b57cec5SDimitry Andric MachineBasicBlock::iterator &NextMBBI,
80*0b57cec5SDimitry Andric unsigned LowOpcode,
81*0b57cec5SDimitry Andric unsigned HighOpcode) {
82*0b57cec5SDimitry Andric Register DestReg = MBBI->getOperand(0).getReg();
83*0b57cec5SDimitry Andric Register SrcReg = MBBI->getOperand(2).getReg();
84*0b57cec5SDimitry Andric bool DestIsHigh = SystemZ::isHighReg(DestReg);
85*0b57cec5SDimitry Andric bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
86*0b57cec5SDimitry Andric
87*0b57cec5SDimitry Andric if (!DestIsHigh && !SrcIsHigh)
88*0b57cec5SDimitry Andric MBBI->setDesc(TII->get(LowOpcode));
89*0b57cec5SDimitry Andric else if (DestIsHigh && SrcIsHigh)
90*0b57cec5SDimitry Andric MBBI->setDesc(TII->get(HighOpcode));
91*0b57cec5SDimitry Andric else
92*0b57cec5SDimitry Andric expandCondMove(MBB, MBBI, NextMBBI);
93*0b57cec5SDimitry Andric }
94*0b57cec5SDimitry Andric
95*0b57cec5SDimitry Andric // MI is a select pseudo instruction. Replace it with LowOpcode if source
96*0b57cec5SDimitry Andric // and destination are all low GR32s and HighOpcode if source and destination
97*0b57cec5SDimitry Andric // are all high GR32s. Otherwise, a branch sequence is created.
selectSELRMux(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI,unsigned LowOpcode,unsigned HighOpcode)98*0b57cec5SDimitry Andric void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,
99*0b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI,
100*0b57cec5SDimitry Andric MachineBasicBlock::iterator &NextMBBI,
101*0b57cec5SDimitry Andric unsigned LowOpcode,
102*0b57cec5SDimitry Andric unsigned HighOpcode) {
103*0b57cec5SDimitry Andric Register DestReg = MBBI->getOperand(0).getReg();
104*0b57cec5SDimitry Andric Register Src1Reg = MBBI->getOperand(1).getReg();
105*0b57cec5SDimitry Andric Register Src2Reg = MBBI->getOperand(2).getReg();
106*0b57cec5SDimitry Andric bool DestIsHigh = SystemZ::isHighReg(DestReg);
107*0b57cec5SDimitry Andric bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);
108*0b57cec5SDimitry Andric bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);
109*0b57cec5SDimitry Andric
110*0b57cec5SDimitry Andric // If sources and destination aren't all high or all low, we may be able to
111*0b57cec5SDimitry Andric // simplify the operation by moving one of the sources to the destination
112*0b57cec5SDimitry Andric // first. But only if this doesn't clobber the other source.
113*0b57cec5SDimitry Andric if (DestReg != Src1Reg && DestReg != Src2Reg) {
114*0b57cec5SDimitry Andric if (DestIsHigh != Src1IsHigh) {
115*0b57cec5SDimitry Andric BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
116*0b57cec5SDimitry Andric TII->get(SystemZ::COPY), DestReg)
117*0b57cec5SDimitry Andric .addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
118*0b57cec5SDimitry Andric MBBI->getOperand(1).setReg(DestReg);
119*0b57cec5SDimitry Andric Src1Reg = DestReg;
120*0b57cec5SDimitry Andric Src1IsHigh = DestIsHigh;
121*0b57cec5SDimitry Andric } else if (DestIsHigh != Src2IsHigh) {
122*0b57cec5SDimitry Andric BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
123*0b57cec5SDimitry Andric TII->get(SystemZ::COPY), DestReg)
124*0b57cec5SDimitry Andric .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.
expandCondMove(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI)150 bool SystemZPostRewrite::expandCondMove(MachineBasicBlock &MBB,
151 MachineBasicBlock::iterator MBBI,
152 MachineBasicBlock::iterator &NextMBBI) {
153 MachineFunction &MF = *MBB.getParent();
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.
170 MachineBasicBlock *RestMBB = MF.CreateMachineBasicBlock(BB);
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.
178 MachineBasicBlock *MoveMBB = MF.CreateMachineBasicBlock(BB);
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.
selectMI(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI)205 bool SystemZPostRewrite::selectMI(MachineBasicBlock &MBB,
206 MachineBasicBlock::iterator MBBI,
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.
selectMBB(MachineBasicBlock & MBB)243 bool SystemZPostRewrite::selectMBB(MachineBasicBlock &MBB) {
244 bool Modified = false;
245
246 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
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
runOnMachineFunction(MachineFunction & MF)256 bool 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
266