forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPFMIPeephole.cpp
191 lines (157 loc) · 5.15 KB
/
BPFMIPeephole.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//===-------------- BPFMIPeephole.cpp - MI Peephole Cleanups -------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass performs peephole optimizations to cleanup ugly code sequences at
// MachineInstruction layer.
//
// Currently, the only optimization in this pass is to eliminate type promotion
// sequences, those zero extend 32-bit subregisters to 64-bit registers, if the
// compiler could prove the subregisters is defined by 32-bit operations in
// which case the upper half of the underlying 64-bit registers were zeroed
// implicitly.
//
//===----------------------------------------------------------------------===//
#include "BPF.h"
#include "BPFInstrInfo.h"
#include "BPFTargetMachine.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
using namespace llvm;
#define DEBUG_TYPE "bpf-mi-promotion-elim"
STATISTIC(CmpPromotionElemNum, "Number of shifts for CMP promotion eliminated");
namespace {
struct BPFMIPeephole : public MachineFunctionPass {
static char ID;
const BPFInstrInfo *TII;
MachineFunction *MF;
MachineRegisterInfo *MRI;
BPFMIPeephole() : MachineFunctionPass(ID) {
initializeBPFMIPeepholePass(*PassRegistry::getPassRegistry());
}
private:
// Initialize class variables.
void initialize(MachineFunction &MFParm);
bool eliminateCmpPromotionSeq(void);
MachineInstr *getInsnDefZExtSubReg(unsigned Reg) const;
void updateInsnSeq(MachineBasicBlock &MBB, MachineInstr &MI,
unsigned Reg) const;
public:
// Main entry point for this pass.
bool runOnMachineFunction(MachineFunction &MF) override {
if (skipFunction(MF.getFunction()))
return false;
initialize(MF);
return eliminateCmpPromotionSeq();
}
};
// Initialize class variables.
void BPFMIPeephole::initialize(MachineFunction &MFParm) {
MF = &MFParm;
MRI = &MF->getRegInfo();
TII = MF->getSubtarget<BPFSubtarget>().getInstrInfo();
DEBUG(dbgs() << "*** BPF MI peephole pass ***\n\n");
}
MachineInstr *BPFMIPeephole::getInsnDefZExtSubReg(unsigned Reg) const {
MachineInstr *Insn = MRI->getVRegDef(Reg);
if (!Insn ||
Insn->isPHI() ||
Insn->getOpcode() != BPF::SRL_ri ||
Insn->getOperand(2).getImm() != 32)
return nullptr;
Insn = MRI->getVRegDef(Insn->getOperand(1).getReg());
if (!Insn ||
Insn->isPHI() ||
Insn->getOpcode() != BPF::SLL_ri ||
Insn->getOperand(2).getImm() != 32)
return nullptr;
Insn = MRI->getVRegDef(Insn->getOperand(1).getReg());
if (!Insn ||
Insn->isPHI() ||
Insn->getOpcode() != BPF::MOV_32_64)
return nullptr;
Insn = MRI->getVRegDef(Insn->getOperand(1).getReg());
if (!Insn || Insn->isPHI())
return nullptr;
if (Insn->getOpcode() == BPF::COPY) {
MachineOperand &opnd = Insn->getOperand(1);
if (!opnd.isReg())
return nullptr;
unsigned Reg = opnd.getReg();
if ((TargetRegisterInfo::isVirtualRegister(Reg) &&
MRI->getRegClass(Reg) == &BPF::GPRRegClass) ||
(TargetRegisterInfo::isPhysicalRegister(Reg) &&
BPF::GPRRegClass.contains(Reg)))
return nullptr;
}
return Insn;
}
void
BPFMIPeephole::updateInsnSeq(MachineBasicBlock &MBB, MachineInstr &MI,
unsigned Reg) const {
MachineInstr *Mov, *Lshift, *Rshift;
unsigned SubReg;
DebugLoc DL;
Rshift = MRI->getVRegDef(Reg);
Lshift = MRI->getVRegDef(Rshift->getOperand(1).getReg());
Mov = MRI->getVRegDef(Lshift->getOperand(1).getReg());
SubReg = Mov->getOperand(1).getReg();
DL = MI.getDebugLoc();
BuildMI(MBB, Rshift, DL, TII->get(BPF::SUBREG_TO_REG), Reg)
.addImm(0).addReg(SubReg).addImm(BPF::sub_32);
Rshift->eraseFromParent();
Lshift->eraseFromParent();
Mov->eraseFromParent();
CmpPromotionElemNum++;
}
bool BPFMIPeephole::eliminateCmpPromotionSeq(void) {
bool Eliminated = false;
MachineInstr *Mov;
unsigned Reg;
for (MachineBasicBlock &MBB : *MF) {
for (MachineInstr &MI : MBB) {
switch (MI.getOpcode()) {
default:
break;
case BPF::JUGT_rr:
case BPF::JUGE_rr:
case BPF::JULT_rr:
case BPF::JULE_rr:
case BPF::JEQ_rr:
case BPF::JNE_rr:
Reg = MI.getOperand(1).getReg();
Mov = getInsnDefZExtSubReg(Reg);
if (Mov) {
updateInsnSeq(MBB, MI, Reg);
Eliminated = true;
}
// Fallthrough
case BPF::JUGT_ri:
case BPF::JUGE_ri:
case BPF::JULT_ri:
case BPF::JULE_ri:
case BPF::JEQ_ri:
case BPF::JNE_ri:
Reg = MI.getOperand(0).getReg();
Mov = getInsnDefZExtSubReg(Reg);
if (!Mov)
break;
updateInsnSeq(MBB, MI, Reg);
Eliminated = true;
break;
}
}
}
return Eliminated;
}
} // end default namespace
INITIALIZE_PASS(BPFMIPeephole, DEBUG_TYPE, "BPF MI Peephole Optimization",
false, false)
char BPFMIPeephole::ID = 0;
FunctionPass* llvm::createBPFMIPeepholePass() { return new BPFMIPeephole(); }