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/MachineFunctionPass.h"
21*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
22*0b57cec5SDimitry Andric using namespace llvm;
23*0b57cec5SDimitry Andric 
24*0b57cec5SDimitry Andric #define SYSTEMZ_POSTREWRITE_NAME "SystemZ Post Rewrite pass"
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric #define DEBUG_TYPE "systemz-postrewrite"
27*0b57cec5SDimitry Andric STATISTIC(MemFoldCopies, "Number of copies inserted before folded mem ops.");
28*0b57cec5SDimitry Andric STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");
29*0b57cec5SDimitry Andric 
30*0b57cec5SDimitry Andric namespace llvm {
31*0b57cec5SDimitry Andric   void initializeSystemZPostRewritePass(PassRegistry&);
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric 
34*0b57cec5SDimitry Andric namespace {
35*0b57cec5SDimitry Andric 
36*0b57cec5SDimitry Andric class SystemZPostRewrite : public MachineFunctionPass {
37*0b57cec5SDimitry Andric public:
38*0b57cec5SDimitry Andric   static char ID;
SystemZPostRewrite()39*0b57cec5SDimitry Andric   SystemZPostRewrite() : MachineFunctionPass(ID) {
40*0b57cec5SDimitry Andric     initializeSystemZPostRewritePass(*PassRegistry::getPassRegistry());
41*0b57cec5SDimitry Andric   }
42*0b57cec5SDimitry Andric 
43*0b57cec5SDimitry Andric   const SystemZInstrInfo *TII;
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &Fn) override;
46*0b57cec5SDimitry Andric 
getPassName() const47*0b57cec5SDimitry Andric   StringRef getPassName() const override { return SYSTEMZ_POSTREWRITE_NAME; }
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric private:
50*0b57cec5SDimitry Andric   void selectLOCRMux(MachineBasicBlock &MBB,
51*0b57cec5SDimitry Andric                      MachineBasicBlock::iterator MBBI,
52*0b57cec5SDimitry Andric                      MachineBasicBlock::iterator &NextMBBI,
53*0b57cec5SDimitry Andric                      unsigned LowOpcode,
54*0b57cec5SDimitry Andric                      unsigned HighOpcode);
55*0b57cec5SDimitry Andric   void selectSELRMux(MachineBasicBlock &MBB,
56*0b57cec5SDimitry Andric                      MachineBasicBlock::iterator MBBI,
57*0b57cec5SDimitry Andric                      MachineBasicBlock::iterator &NextMBBI,
58*0b57cec5SDimitry Andric                      unsigned LowOpcode,
59*0b57cec5SDimitry Andric                      unsigned HighOpcode);
60*0b57cec5SDimitry Andric   bool expandCondMove(MachineBasicBlock &MBB,
61*0b57cec5SDimitry Andric                       MachineBasicBlock::iterator MBBI,
62*0b57cec5SDimitry Andric                       MachineBasicBlock::iterator &NextMBBI);
63*0b57cec5SDimitry Andric   bool selectMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
64*0b57cec5SDimitry Andric                 MachineBasicBlock::iterator &NextMBBI);
65*0b57cec5SDimitry Andric   bool selectMBB(MachineBasicBlock &MBB);
66*0b57cec5SDimitry Andric };
67*0b57cec5SDimitry Andric 
68*0b57cec5SDimitry Andric char SystemZPostRewrite::ID = 0;
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric } // end anonymous namespace
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric INITIALIZE_PASS(SystemZPostRewrite, "systemz-post-rewrite",
73*0b57cec5SDimitry Andric                 SYSTEMZ_POSTREWRITE_NAME, false, false)
74*0b57cec5SDimitry Andric 
75*0b57cec5SDimitry Andric /// Returns an instance of the Post Rewrite pass.
createSystemZPostRewritePass(SystemZTargetMachine & TM)76*0b57cec5SDimitry Andric FunctionPass *llvm::createSystemZPostRewritePass(SystemZTargetMachine &TM) {
77*0b57cec5SDimitry Andric   return new SystemZPostRewrite();
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric // MI is a load-register-on-condition pseudo instruction.  Replace it with
81*0b57cec5SDimitry Andric // LowOpcode if source and destination are both low GR32s and HighOpcode if
82*0b57cec5SDimitry Andric // source and destination are both high GR32s. Otherwise, a branch sequence
83*0b57cec5SDimitry Andric // is created.
selectLOCRMux(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI,unsigned LowOpcode,unsigned HighOpcode)84*0b57cec5SDimitry Andric void SystemZPostRewrite::selectLOCRMux(MachineBasicBlock &MBB,
85*0b57cec5SDimitry Andric                                        MachineBasicBlock::iterator MBBI,
86*0b57cec5SDimitry Andric                                        MachineBasicBlock::iterator &NextMBBI,
87*0b57cec5SDimitry Andric                                        unsigned LowOpcode,
88*0b57cec5SDimitry Andric                                        unsigned HighOpcode) {
89*0b57cec5SDimitry Andric   Register DestReg = MBBI->getOperand(0).getReg();
90*0b57cec5SDimitry Andric   Register SrcReg = MBBI->getOperand(2).getReg();
91*0b57cec5SDimitry Andric   bool DestIsHigh = SystemZ::isHighReg(DestReg);
92*0b57cec5SDimitry Andric   bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric   if (!DestIsHigh && !SrcIsHigh)
95*0b57cec5SDimitry Andric     MBBI->setDesc(TII->get(LowOpcode));
96*0b57cec5SDimitry Andric   else if (DestIsHigh && SrcIsHigh)
97*0b57cec5SDimitry Andric     MBBI->setDesc(TII->get(HighOpcode));
98*0b57cec5SDimitry Andric   else
99*0b57cec5SDimitry Andric     expandCondMove(MBB, MBBI, NextMBBI);
100*0b57cec5SDimitry Andric }
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric // MI is a select pseudo instruction.  Replace it with LowOpcode if source
103*0b57cec5SDimitry Andric // and destination are all low GR32s and HighOpcode if source and destination
104*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)105*0b57cec5SDimitry Andric void SystemZPostRewrite::selectSELRMux(MachineBasicBlock &MBB,
106*0b57cec5SDimitry Andric                                        MachineBasicBlock::iterator MBBI,
107*0b57cec5SDimitry Andric                                        MachineBasicBlock::iterator &NextMBBI,
108*0b57cec5SDimitry Andric                                        unsigned LowOpcode,
109*0b57cec5SDimitry Andric                                        unsigned HighOpcode) {
110*0b57cec5SDimitry Andric   Register DestReg = MBBI->getOperand(0).getReg();
111*0b57cec5SDimitry Andric   Register Src1Reg = MBBI->getOperand(1).getReg();
112*0b57cec5SDimitry Andric   Register Src2Reg = MBBI->getOperand(2).getReg();
113*0b57cec5SDimitry Andric   bool DestIsHigh = SystemZ::isHighReg(DestReg);
114*0b57cec5SDimitry Andric   bool Src1IsHigh = SystemZ::isHighReg(Src1Reg);
115*0b57cec5SDimitry Andric   bool Src2IsHigh = SystemZ::isHighReg(Src2Reg);
116*0b57cec5SDimitry Andric 
117*0b57cec5SDimitry Andric   // If sources and destination aren't all high or all low, we may be able to
118*0b57cec5SDimitry Andric   // simplify the operation by moving one of the sources to the destination
119*0b57cec5SDimitry Andric   // first.  But only if this doesn't clobber the other source.
120*0b57cec5SDimitry Andric   if (DestReg != Src1Reg && DestReg != Src2Reg) {
121*0b57cec5SDimitry Andric     if (DestIsHigh != Src1IsHigh) {
122*0b57cec5SDimitry Andric       BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
123*0b57cec5SDimitry Andric               TII->get(SystemZ::COPY), DestReg)
124*0b57cec5SDimitry Andric         .addReg(MBBI->getOperand(1).getReg(), getRegState(MBBI->getOperand(1)));
125       MBBI->getOperand(1).setReg(DestReg);
126       Src1Reg = DestReg;
127       Src1IsHigh = DestIsHigh;
128     } else if (DestIsHigh != Src2IsHigh) {
129       BuildMI(*MBBI->getParent(), MBBI, MBBI->getDebugLoc(),
130               TII->get(SystemZ::COPY), DestReg)
131         .addReg(MBBI->getOperand(2).getReg(), getRegState(MBBI->getOperand(2)));
132       MBBI->getOperand(2).setReg(DestReg);
133       Src2Reg = DestReg;
134       Src2IsHigh = DestIsHigh;
135     }
136   }
137 
138   // If the destination (now) matches one source, prefer this to be first.
139   if (DestReg != Src1Reg && DestReg == Src2Reg) {
140     TII->commuteInstruction(*MBBI, false, 1, 2);
141     std::swap(Src1Reg, Src2Reg);
142     std::swap(Src1IsHigh, Src2IsHigh);
143   }
144 
145   if (!DestIsHigh && !Src1IsHigh && !Src2IsHigh)
146     MBBI->setDesc(TII->get(LowOpcode));
147   else if (DestIsHigh && Src1IsHigh && Src2IsHigh)
148     MBBI->setDesc(TII->get(HighOpcode));
149   else
150     // Given the simplification above, we must already have a two-operand case.
151     expandCondMove(MBB, MBBI, NextMBBI);
152 }
153 
154 // Replace MBBI by a branch sequence that performs a conditional move of
155 // operand 2 to the destination register. Operand 1 is expected to be the
156 // same register as the destination.
expandCondMove(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI)157 bool SystemZPostRewrite::expandCondMove(MachineBasicBlock &MBB,
158                                         MachineBasicBlock::iterator MBBI,
159                                         MachineBasicBlock::iterator &NextMBBI) {
160   MachineFunction &MF = *MBB.getParent();
161   const BasicBlock *BB = MBB.getBasicBlock();
162   MachineInstr &MI = *MBBI;
163   DebugLoc DL = MI.getDebugLoc();
164   Register DestReg = MI.getOperand(0).getReg();
165   Register SrcReg = MI.getOperand(2).getReg();
166   unsigned CCValid = MI.getOperand(3).getImm();
167   unsigned CCMask = MI.getOperand(4).getImm();
168   assert(DestReg == MI.getOperand(1).getReg() &&
169          "Expected destination and first source operand to be the same.");
170 
171   LivePhysRegs LiveRegs(TII->getRegisterInfo());
172   LiveRegs.addLiveOuts(MBB);
173   for (auto I = std::prev(MBB.end()); I != MBBI; --I)
174     LiveRegs.stepBackward(*I);
175 
176   // Splice MBB at MI, moving the rest of the block into RestMBB.
177   MachineBasicBlock *RestMBB = MF.CreateMachineBasicBlock(BB);
178   MF.insert(std::next(MachineFunction::iterator(MBB)), RestMBB);
179   RestMBB->splice(RestMBB->begin(), &MBB, MI, MBB.end());
180   RestMBB->transferSuccessors(&MBB);
181   for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
182     RestMBB->addLiveIn(*I);
183 
184   // Create a new block MoveMBB to hold the move instruction.
185   MachineBasicBlock *MoveMBB = MF.CreateMachineBasicBlock(BB);
186   MF.insert(std::next(MachineFunction::iterator(MBB)), MoveMBB);
187   MoveMBB->addLiveIn(SrcReg);
188   for (auto I = LiveRegs.begin(); I != LiveRegs.end(); ++I)
189     MoveMBB->addLiveIn(*I);
190 
191   // At the end of MBB, create a conditional branch to RestMBB if the
192   // condition is false, otherwise fall through to MoveMBB.
193   BuildMI(&MBB, DL, TII->get(SystemZ::BRC))
194     .addImm(CCValid).addImm(CCMask ^ CCValid).addMBB(RestMBB);
195   MBB.addSuccessor(RestMBB);
196   MBB.addSuccessor(MoveMBB);
197 
198   // In MoveMBB, emit an instruction to move SrcReg into DestReg,
199   // then fall through to RestMBB.
200   BuildMI(*MoveMBB, MoveMBB->end(), DL, TII->get(SystemZ::COPY), DestReg)
201       .addReg(MI.getOperand(2).getReg(), getRegState(MI.getOperand(2)));
202   MoveMBB->addSuccessor(RestMBB);
203 
204   NextMBBI = MBB.end();
205   MI.eraseFromParent();
206   LOCRMuxJumps++;
207   return true;
208 }
209 
210 /// If MBBI references a pseudo instruction that should be selected here,
211 /// do it and return true.  Otherwise return false.
selectMI(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,MachineBasicBlock::iterator & NextMBBI)212 bool SystemZPostRewrite::selectMI(MachineBasicBlock &MBB,
213                                   MachineBasicBlock::iterator MBBI,
214                                   MachineBasicBlock::iterator &NextMBBI) {
215   MachineInstr &MI = *MBBI;
216   unsigned Opcode = MI.getOpcode();
217 
218   // Note: If this could be done during regalloc in foldMemoryOperandImpl()
219   // while also updating the LiveIntervals, there would be no need for the
220   // MemFoldPseudo to begin with.
221   int TargetMemOpcode = SystemZ::getTargetMemOpcode(Opcode);
222   if (TargetMemOpcode != -1) {
223     MI.setDesc(TII->get(TargetMemOpcode));
224     MI.tieOperands(0, 1);
225     Register DstReg = MI.getOperand(0).getReg();
226     MachineOperand &SrcMO = MI.getOperand(1);
227     if (DstReg != SrcMO.getReg()) {
228       BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), DstReg)
229         .addReg(SrcMO.getReg());
230       SrcMO.setReg(DstReg);
231       MemFoldCopies++;
232     }
233     return true;
234   }
235 
236   switch (Opcode) {
237   case SystemZ::LOCRMux:
238     selectLOCRMux(MBB, MBBI, NextMBBI, SystemZ::LOCR, SystemZ::LOCFHR);
239     return true;
240   case SystemZ::SELRMux:
241     selectSELRMux(MBB, MBBI, NextMBBI, SystemZ::SELR, SystemZ::SELFHR);
242     return true;
243   }
244 
245   return false;
246 }
247 
248 /// Iterate over the instructions in basic block MBB and select any
249 /// pseudo instructions.  Return true if anything was modified.
selectMBB(MachineBasicBlock & MBB)250 bool SystemZPostRewrite::selectMBB(MachineBasicBlock &MBB) {
251   bool Modified = false;
252 
253   MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
254   while (MBBI != E) {
255     MachineBasicBlock::iterator NMBBI = std::next(MBBI);
256     Modified |= selectMI(MBB, MBBI, NMBBI);
257     MBBI = NMBBI;
258   }
259 
260   return Modified;
261 }
262 
runOnMachineFunction(MachineFunction & MF)263 bool SystemZPostRewrite::runOnMachineFunction(MachineFunction &MF) {
264   TII = static_cast<const SystemZInstrInfo *>(MF.getSubtarget().getInstrInfo());
265 
266   bool Modified = false;
267   for (auto &MBB : MF)
268     Modified |= selectMBB(MBB);
269 
270   return Modified;
271 }
272 
273