blob: 09cc6b31ad832d47e7ab3a103ba4c33ae7e76eb7 [file] [log] [blame]
Yonghong Song60fed1f2018-02-23 23:49:32 +00001//===-------------- BPFMIPeephole.cpp - MI Peephole Cleanups -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs peephole optimizations to cleanup ugly code sequences at
11// MachineInstruction layer.
12//
Yonghong Songe91802f2018-03-13 06:47:06 +000013// Currently, there are two optimizations implemented:
14// - One pre-RA MachineSSA pass to eliminate type promotion sequences, those
15// zero extend 32-bit subregisters to 64-bit registers, if the compiler
16// could prove the subregisters is defined by 32-bit operations in which
17// case the upper half of the underlying 64-bit registers were zeroed
18// implicitly.
Yonghong Song60fed1f2018-02-23 23:49:32 +000019//
Yonghong Songe91802f2018-03-13 06:47:06 +000020// - One post-RA PreEmit pass to do final cleanup on some redundant
21// instructions generated due to bad RA on subregister.
Yonghong Song60fed1f2018-02-23 23:49:32 +000022//===----------------------------------------------------------------------===//
23
24#include "BPF.h"
25#include "BPFInstrInfo.h"
26#include "BPFTargetMachine.h"
27#include "llvm/ADT/Statistic.h"
28#include "llvm/CodeGen/MachineInstrBuilder.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
30
31using namespace llvm;
32
Yonghong Songc88bcde2018-03-13 06:47:03 +000033#define DEBUG_TYPE "bpf-mi-zext-elim"
Yonghong Song60fed1f2018-02-23 23:49:32 +000034
Yonghong Songc88bcde2018-03-13 06:47:03 +000035STATISTIC(ZExtElemNum, "Number of zero extension shifts eliminated");
Yonghong Song60fed1f2018-02-23 23:49:32 +000036
37namespace {
38
39struct BPFMIPeephole : public MachineFunctionPass {
40
41 static char ID;
42 const BPFInstrInfo *TII;
43 MachineFunction *MF;
44 MachineRegisterInfo *MRI;
45
46 BPFMIPeephole() : MachineFunctionPass(ID) {
47 initializeBPFMIPeepholePass(*PassRegistry::getPassRegistry());
48 }
49
50private:
51 // Initialize class variables.
52 void initialize(MachineFunction &MFParm);
53
Yonghong Songc88bcde2018-03-13 06:47:03 +000054 bool isMovFrom32Def(MachineInstr *MovMI);
55 bool eliminateZExtSeq(void);
Yonghong Song60fed1f2018-02-23 23:49:32 +000056
57public:
58
59 // Main entry point for this pass.
60 bool runOnMachineFunction(MachineFunction &MF) override {
61 if (skipFunction(MF.getFunction()))
62 return false;
63
64 initialize(MF);
65
Yonghong Songc88bcde2018-03-13 06:47:03 +000066 return eliminateZExtSeq();
Yonghong Song60fed1f2018-02-23 23:49:32 +000067 }
68};
69
70// Initialize class variables.
71void BPFMIPeephole::initialize(MachineFunction &MFParm) {
72 MF = &MFParm;
73 MRI = &MF->getRegInfo();
74 TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
Yonghong Songe91802f2018-03-13 06:47:06 +000075 DEBUG(dbgs() << "*** BPF MachineSSA peephole pass ***\n\n");
Yonghong Song60fed1f2018-02-23 23:49:32 +000076}
77
Yonghong Songc88bcde2018-03-13 06:47:03 +000078bool BPFMIPeephole::isMovFrom32Def(MachineInstr *MovMI)
79{
80 MachineInstr *DefInsn = MRI->getVRegDef(MovMI->getOperand(1).getReg());
Yonghong Song60fed1f2018-02-23 23:49:32 +000081
Yonghong Song82bf8bc2018-03-13 06:47:07 +000082 DEBUG(dbgs() << " Def of Mov Src:");
83 DEBUG(DefInsn->dump());
84
Yonghong Song1d28a752018-03-13 06:47:04 +000085 if (!DefInsn)
Yonghong Songc88bcde2018-03-13 06:47:03 +000086 return false;
Yonghong Song60fed1f2018-02-23 23:49:32 +000087
Yonghong Song1d28a752018-03-13 06:47:04 +000088 if (DefInsn->isPHI()) {
89 for (unsigned i = 1, e = DefInsn->getNumOperands(); i < e; i += 2) {
90 MachineOperand &opnd = DefInsn->getOperand(i);
91
92 if (!opnd.isReg())
93 return false;
94
95 MachineInstr *PhiDef = MRI->getVRegDef(opnd.getReg());
96 // quick check on PHI incoming definitions.
97 if (!PhiDef || PhiDef->isPHI() || PhiDef->getOpcode() == BPF::COPY)
98 return false;
99 }
100 }
101
Yonghong Songc88bcde2018-03-13 06:47:03 +0000102 if (DefInsn->getOpcode() == BPF::COPY) {
103 MachineOperand &opnd = DefInsn->getOperand(1);
Yonghong Song89e47ac2018-03-13 06:47:00 +0000104
105 if (!opnd.isReg())
Yonghong Songc88bcde2018-03-13 06:47:03 +0000106 return false;
Yonghong Song89e47ac2018-03-13 06:47:00 +0000107
108 unsigned Reg = opnd.getReg();
109 if ((TargetRegisterInfo::isVirtualRegister(Reg) &&
Yonghong Songc88bcde2018-03-13 06:47:03 +0000110 MRI->getRegClass(Reg) == &BPF::GPRRegClass))
111 return false;
Yonghong Song89e47ac2018-03-13 06:47:00 +0000112 }
113
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000114 DEBUG(dbgs() << " One ZExt elim sequence identified.\n");
115
Yonghong Songc88bcde2018-03-13 06:47:03 +0000116 return true;
Yonghong Song60fed1f2018-02-23 23:49:32 +0000117}
118
Yonghong Songc88bcde2018-03-13 06:47:03 +0000119bool BPFMIPeephole::eliminateZExtSeq(void) {
120 MachineInstr* ToErase = nullptr;
Yonghong Song60fed1f2018-02-23 23:49:32 +0000121 bool Eliminated = false;
Yonghong Song60fed1f2018-02-23 23:49:32 +0000122
123 for (MachineBasicBlock &MBB : *MF) {
124 for (MachineInstr &MI : MBB) {
Yonghong Songc88bcde2018-03-13 06:47:03 +0000125 // If the previous instruction was marked for elimination, remove it now.
126 if (ToErase) {
127 ToErase->eraseFromParent();
128 ToErase = nullptr;
129 }
Yonghong Song60fed1f2018-02-23 23:49:32 +0000130
Yonghong Songc88bcde2018-03-13 06:47:03 +0000131 // Eliminate the 32-bit to 64-bit zero extension sequence when possible.
132 //
133 // MOV_32_64 rB, wA
134 // SLL_ri rB, rB, 32
135 // SRL_ri rB, rB, 32
136 if (MI.getOpcode() == BPF::SRL_ri &&
137 MI.getOperand(2).getImm() == 32) {
138 unsigned DstReg = MI.getOperand(0).getReg();
139 unsigned ShfReg = MI.getOperand(1).getReg();
140 MachineInstr *SllMI = MRI->getVRegDef(ShfReg);
141
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000142 DEBUG(dbgs() << "Starting SRL found:");
143 DEBUG(MI.dump());
144
Yonghong Songc88bcde2018-03-13 06:47:03 +0000145 if (!SllMI ||
146 SllMI->isPHI() ||
147 SllMI->getOpcode() != BPF::SLL_ri ||
148 SllMI->getOperand(2).getImm() != 32)
149 continue;
150
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000151 DEBUG(dbgs() << " SLL found:");
152 DEBUG(SllMI->dump());
153
Yonghong Songc88bcde2018-03-13 06:47:03 +0000154 MachineInstr *MovMI = MRI->getVRegDef(SllMI->getOperand(1).getReg());
155 if (!MovMI ||
156 MovMI->isPHI() ||
157 MovMI->getOpcode() != BPF::MOV_32_64)
158 continue;
159
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000160 DEBUG(dbgs() << " Type cast Mov found:");
161 DEBUG(MovMI->dump());
162
Yonghong Song1d28a752018-03-13 06:47:04 +0000163 unsigned SubReg = MovMI->getOperand(1).getReg();
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000164 if (!isMovFrom32Def(MovMI)) {
165 DEBUG(dbgs() << " One ZExt elim sequence failed qualifying elim.\n");
Yonghong Songc88bcde2018-03-13 06:47:03 +0000166 continue;
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000167 }
Yonghong Songc88bcde2018-03-13 06:47:03 +0000168
Yonghong Songc88bcde2018-03-13 06:47:03 +0000169 BuildMI(MBB, MI, MI.getDebugLoc(), TII->get(BPF::SUBREG_TO_REG), DstReg)
170 .addImm(0).addReg(SubReg).addImm(BPF::sub_32);
171
172 SllMI->eraseFromParent();
173 MovMI->eraseFromParent();
174 // MI is the right shift, we can't erase it in it's own iteration.
175 // Mark it to ToErase, and erase in the next iteration.
176 ToErase = &MI;
177 ZExtElemNum++;
178 Eliminated = true;
Yonghong Song60fed1f2018-02-23 23:49:32 +0000179 }
180 }
181 }
182
183 return Eliminated;
184}
185
186} // end default namespace
187
Yonghong Songe91802f2018-03-13 06:47:06 +0000188INITIALIZE_PASS(BPFMIPeephole, DEBUG_TYPE,
189 "BPF MachineSSA Peephole Optimization", false, false)
Yonghong Song60fed1f2018-02-23 23:49:32 +0000190
191char BPFMIPeephole::ID = 0;
192FunctionPass* llvm::createBPFMIPeepholePass() { return new BPFMIPeephole(); }
Yonghong Songe91802f2018-03-13 06:47:06 +0000193
194STATISTIC(RedundantMovElemNum, "Number of redundant moves eliminated");
195
196namespace {
197
198struct BPFMIPreEmitPeephole : public MachineFunctionPass {
199
200 static char ID;
201 MachineFunction *MF;
202 const TargetRegisterInfo *TRI;
203
204 BPFMIPreEmitPeephole() : MachineFunctionPass(ID) {
205 initializeBPFMIPreEmitPeepholePass(*PassRegistry::getPassRegistry());
206 }
207
208private:
209 // Initialize class variables.
210 void initialize(MachineFunction &MFParm);
211
212 bool eliminateRedundantMov(void);
213
214public:
215
216 // Main entry point for this pass.
217 bool runOnMachineFunction(MachineFunction &MF) override {
218 if (skipFunction(MF.getFunction()))
219 return false;
220
221 initialize(MF);
222
223 return eliminateRedundantMov();
224 }
225};
226
227// Initialize class variables.
228void BPFMIPreEmitPeephole::initialize(MachineFunction &MFParm) {
229 MF = &MFParm;
230 TRI = MF->getSubtarget<BPFSubtarget>().getRegisterInfo();
231 DEBUG(dbgs() << "*** BPF PreEmit peephole pass ***\n\n");
232}
233
234bool BPFMIPreEmitPeephole::eliminateRedundantMov(void) {
235 MachineInstr* ToErase = nullptr;
236 bool Eliminated = false;
237
238 for (MachineBasicBlock &MBB : *MF) {
239 for (MachineInstr &MI : MBB) {
240 // If the previous instruction was marked for elimination, remove it now.
241 if (ToErase) {
Yonghong Song82bf8bc2018-03-13 06:47:07 +0000242 DEBUG(dbgs() << " Redundant Mov Eliminated:");
243 DEBUG(ToErase->dump());
Yonghong Songe91802f2018-03-13 06:47:06 +0000244 ToErase->eraseFromParent();
245 ToErase = nullptr;
246 }
247
248 // Eliminate identical move:
249 //
250 // MOV rA, rA
251 //
252 // This is particularly possible to happen when sub-register support
253 // enabled. The special type cast insn MOV_32_64 involves different
254 // register class on src (i32) and dst (i64), RA could generate useless
255 // instruction due to this.
256 if (MI.getOpcode() == BPF::MOV_32_64) {
257 unsigned dst = MI.getOperand(0).getReg();
258 unsigned dst_sub = TRI->getSubReg(dst, BPF::sub_32);
259 unsigned src = MI.getOperand(1).getReg();
260
261 if (dst_sub != src)
262 continue;
263
264 ToErase = &MI;
265 RedundantMovElemNum++;
266 Eliminated = true;
267 }
268 }
269 }
270
271 return Eliminated;
272}
273
274} // end default namespace
275
276INITIALIZE_PASS(BPFMIPreEmitPeephole, "bpf-mi-pemit-peephole",
277 "BPF PreEmit Peephole Optimization", false, false)
278
279char BPFMIPreEmitPeephole::ID = 0;
280FunctionPass* llvm::createBPFMIPreEmitPeepholePass()
281{
282 return new BPFMIPreEmitPeephole();
283}