-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0022-Xtensa-Add-code-size-reduction-pass.patch
314 lines (307 loc) · 9.81 KB
/
0022-Xtensa-Add-code-size-reduction-pass.patch
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
From 03eeeb57d406fd6c1b6b54f77f7c8c46b1e58375 Mon Sep 17 00:00:00 2001
From: Andrei Safronov <[email protected]>
Date: Wed, 5 Apr 2023 00:58:46 +0300
Subject: [PATCH 022/158] [Xtensa] Add code size reduction pass.
---
llvm/lib/Target/Xtensa/CMakeLists.txt | 1 +
llvm/lib/Target/Xtensa/Xtensa.h | 1 +
.../Target/Xtensa/XtensaSizeReductionPass.cpp | 253 ++++++++++++++++++
.../lib/Target/Xtensa/XtensaTargetMachine.cpp | 5 +-
4 files changed, 259 insertions(+), 1 deletion(-)
create mode 100644 llvm/lib/Target/Xtensa/XtensaSizeReductionPass.cpp
diff --git a/llvm/lib/Target/Xtensa/CMakeLists.txt b/llvm/lib/Target/Xtensa/CMakeLists.txt
index 2208428f4c10..1a3ff0c5311f 100644
--- a/llvm/lib/Target/Xtensa/CMakeLists.txt
+++ b/llvm/lib/Target/Xtensa/CMakeLists.txt
@@ -24,6 +24,7 @@ add_llvm_target(XtensaCodeGen
XtensaMachineFunctionInfo.cpp
XtensaMCInstLower.cpp
XtensaRegisterInfo.cpp
+ XtensaSizeReductionPass.cpp
XtensaSubtarget.cpp
XtensaTargetMachine.cpp
diff --git a/llvm/lib/Target/Xtensa/Xtensa.h b/llvm/lib/Target/Xtensa/Xtensa.h
index 43eadf88c779..ee054d131f35 100644
--- a/llvm/lib/Target/Xtensa/Xtensa.h
+++ b/llvm/lib/Target/Xtensa/Xtensa.h
@@ -26,5 +26,6 @@ class FunctionPass;
FunctionPass *createXtensaISelDag(XtensaTargetMachine &TM,
CodeGenOpt::Level OptLevel);
+FunctionPass *createXtensaSizeReductionPass();
} // namespace llvm
#endif /* LLVM_LIB_TARGET_XTENSA_XTENSA_H */
diff --git a/llvm/lib/Target/Xtensa/XtensaSizeReductionPass.cpp b/llvm/lib/Target/Xtensa/XtensaSizeReductionPass.cpp
new file mode 100644
index 000000000000..f69c1e601a78
--- /dev/null
+++ b/llvm/lib/Target/Xtensa/XtensaSizeReductionPass.cpp
@@ -0,0 +1,253 @@
+//===- XtensaSizeReductionPass.cpp - Xtensa Size Reduction ----------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Xtensa.h"
+#include "XtensaInstrInfo.h"
+#include "XtensaSubtarget.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/CodeGen//MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/MathExtras.h"
+#include "llvm/Target/TargetMachine.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "xtensa-size-reduce-pass"
+
+STATISTIC(NumReduced, "Number of 24-bit instructions reduced to 16-bit ones");
+
+class XtensaSizeReduce : public MachineFunctionPass {
+public:
+ static char ID;
+ XtensaSizeReduce() : MachineFunctionPass(ID) {}
+
+ const XtensaSubtarget *Subtarget;
+ static const XtensaInstrInfo *XtensaII;
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+
+ llvm::StringRef getPassName() const override {
+ return "Xtensa instruction size reduction pass";
+ }
+
+private:
+ /// Reduces width of instructions in the specified basic block.
+ bool ReduceMBB(MachineBasicBlock &MBB);
+
+ /// Attempts to reduce MI, returns true on success.
+ bool ReduceMI(const MachineBasicBlock::instr_iterator &MII);
+};
+
+char XtensaSizeReduce::ID = 0;
+const XtensaInstrInfo *XtensaSizeReduce::XtensaII;
+
+bool XtensaSizeReduce::ReduceMI(const MachineBasicBlock::instr_iterator &MII) {
+ MachineInstr *MI = &*MII;
+ MachineBasicBlock &MBB = *MI->getParent();
+ unsigned Opcode = MI->getOpcode();
+
+ switch (Opcode) {
+ case Xtensa::L32I: {
+ MachineOperand Op0 = MI->getOperand(0);
+ MachineOperand Op1 = MI->getOperand(1);
+ MachineOperand Op2 = MI->getOperand(2);
+
+ int64_t Imm = Op2.getImm();
+ if (Imm >= 0 && Imm <= 60) {
+ // Replace L32I to L32I.N
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::L32I_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ MIB.add(Op0);
+ MIB.add(Op1);
+ MIB.add(Op2);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+ }
+ } break;
+
+ case Xtensa::S32I: {
+ MachineOperand Op0 = MI->getOperand(0);
+ MachineOperand Op1 = MI->getOperand(1);
+ MachineOperand Op2 = MI->getOperand(2);
+
+ int64_t Imm = Op2.getImm();
+ if (Imm >= 0 && Imm <= 60) {
+ // Replace S32I to S32I.N
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::S32I_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ MIB.add(Op0);
+ MIB.add(Op1);
+ MIB.add(Op2);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+ }
+
+ } break;
+
+ case Xtensa::MOVI: {
+ MachineOperand Op0 = MI->getOperand(0);
+ MachineOperand Op1 = MI->getOperand(1);
+
+ int64_t Imm = Op1.getImm();
+ if (Imm >= -32 && Imm <= 95) {
+ // Replace MOVI to MOVI.N
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::MOVI_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ MIB.add(Op0);
+ MIB.add(Op1);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+ }
+
+ } break;
+
+ case Xtensa::ADD: {
+ MachineOperand Op0 = MI->getOperand(0);
+ MachineOperand Op1 = MI->getOperand(1);
+ MachineOperand Op2 = MI->getOperand(2);
+
+ // Replace ADD to ADD.N
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::ADD_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ MIB.add(Op0);
+ MIB.add(Op1);
+ MIB.add(Op2);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+
+ } break;
+
+ case Xtensa::ADDI: {
+ MachineOperand Op0 = MI->getOperand(0);
+ MachineOperand Op1 = MI->getOperand(1);
+ MachineOperand Op2 = MI->getOperand(2);
+
+ int64_t Imm = Op2.getImm();
+ if ((Imm >= 1 && Imm <= 15) || (Imm == -1)) {
+ // Replace ADDI to ADDI.N
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::ADDI_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ MIB.add(Op0);
+ MIB.add(Op1);
+ MIB.add(Op2);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+ }
+ } break;
+
+ case Xtensa::OR: {
+ MachineOperand Op0 = MI->getOperand(0);
+ MachineOperand Op1 = MI->getOperand(1);
+ MachineOperand Op2 = MI->getOperand(2);
+
+ if (Op1.getReg() != Op2.getReg())
+ break;
+
+ // Replace OR R1, R2, R2 to MOV.N R1, R2
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::MOV_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ MIB.add(Op0);
+ MIB.add(Op1);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+ } break;
+
+ case Xtensa::RET: {
+ // Replace RET to RET.N
+ DebugLoc dl = MI->getDebugLoc();
+ const MCInstrDesc &NewMCID = XtensaII->get(Xtensa::RET_N);
+ MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
+ // Transfer MI flags.
+ MIB.setMIFlags(MI->getFlags());
+ LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB);
+ NumReduced++;
+ MBB.erase_instr(MI);
+ return true;
+ } break;
+
+ default:
+ break;
+ }
+
+ return false;
+}
+
+bool XtensaSizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
+ bool Modified = false;
+ MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),
+ E = MBB.instr_end();
+ MachineBasicBlock::instr_iterator NextMII;
+
+ // Iterate through the instructions in the basic block
+ for (; MII != E; MII = NextMII) {
+ NextMII = std::next(MII);
+ MachineInstr *MI = &*MII;
+
+ // Don't reduce bundled instructions or pseudo operations
+ if (MI->isBundle() || MI->isTransient())
+ continue;
+
+ // Try to reduce 24-bit instruction into 16-bit instruction
+ Modified |= ReduceMI(MII);
+ }
+
+ return Modified;
+}
+
+bool XtensaSizeReduce::runOnMachineFunction(MachineFunction &MF) {
+
+ Subtarget = &static_cast<const XtensaSubtarget &>(MF.getSubtarget());
+ XtensaII = static_cast<const XtensaInstrInfo *>(Subtarget->getInstrInfo());
+ bool Modified = false;
+
+ if (!Subtarget->hasDensity())
+ return Modified;
+
+ MachineFunction::iterator I = MF.begin(), E = MF.end();
+
+ for (; I != E; ++I)
+ Modified |= ReduceMBB(*I);
+ return Modified;
+}
+
+FunctionPass *llvm::createXtensaSizeReductionPass() {
+ return new XtensaSizeReduce();
+}
diff --git a/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp b/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
index 59793d19c077..3d0de2199f45 100644
--- a/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
@@ -100,7 +100,10 @@ bool XtensaPassConfig::addInstSelector() {
return false;
}
-void XtensaPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); }
+void XtensaPassConfig::addPreEmitPass() {
+ addPass(createXtensaSizeReductionPass());
+ addPass(&BranchRelaxationPassID);
+}
TargetPassConfig *XtensaTargetMachine::createPassConfig(PassManagerBase &PM) {
return new XtensaPassConfig(*this, PM);
--
2.40.1