-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0019-Xtensa-Implement-branch-analysis.patch
378 lines (370 loc) · 12 KB
/
0019-Xtensa-Implement-branch-analysis.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
From 81215dcb26afbb07787725d55f935e42f9cc7ff0 Mon Sep 17 00:00:00 2001
From: Andrei Safronov <[email protected]>
Date: Wed, 5 Apr 2023 00:58:44 +0300
Subject: [PATCH 019/158] [Xtensa] Implement branch analysis
---
llvm/lib/Target/Xtensa/XtensaInstrInfo.cpp | 305 ++++++++++++++++++++-
llvm/lib/Target/Xtensa/XtensaInstrInfo.h | 27 ++
2 files changed, 330 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/Xtensa/XtensaInstrInfo.cpp b/llvm/lib/Target/Xtensa/XtensaInstrInfo.cpp
index 1bfd18b7ef3e..ded4df096d94 100644
--- a/llvm/lib/Target/Xtensa/XtensaInstrInfo.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaInstrInfo.cpp
@@ -45,7 +45,8 @@ addFrameReference(const MachineInstrBuilder &MIB, int FI) {
}
XtensaInstrInfo::XtensaInstrInfo(XtensaSubtarget &sti)
- : XtensaGenInstrInfo(Xtensa::ADJCALLSTACKDOWN, Xtensa::ADJCALLSTACKUP), RI(sti), STI(sti) {}
+ : XtensaGenInstrInfo(Xtensa::ADJCALLSTACKDOWN, Xtensa::ADJCALLSTACKUP),
+ RI(sti), STI(sti) {}
/// Adjust SP by Amount bytes.
void XtensaInstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
@@ -72,7 +73,9 @@ void XtensaInstrInfo::adjustStackPtr(unsigned SP, int64_t Amount,
.addReg(Reg1, RegState::Kill);
}
- BuildMI(MBB, I, DL, get(Xtensa::OR), SP).addReg(Reg, RegState::Kill).addReg(Reg, RegState::Kill);
+ BuildMI(MBB, I, DL, get(Xtensa::OR), SP)
+ .addReg(Reg, RegState::Kill)
+ .addReg(Reg, RegState::Kill);
}
void XtensaInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
@@ -158,3 +161,301 @@ void XtensaInstrInfo::loadImmediate(MachineBasicBlock &MBB,
llvm_unreachable("Unsupported load immediate value");
}
}
+
+bool XtensaInstrInfo::reverseBranchCondition(
+ SmallVectorImpl<MachineOperand> &Cond) const {
+ assert(Cond.size() <= 4 && "Invalid branch condition!");
+
+ switch (Cond[0].getImm()) {
+ case Xtensa::BEQ:
+ Cond[0].setImm(Xtensa::BNE);
+ return false;
+ case Xtensa::BNE:
+ Cond[0].setImm(Xtensa::BEQ);
+ return false;
+ case Xtensa::BLT:
+ Cond[0].setImm(Xtensa::BGE);
+ return false;
+ case Xtensa::BGE:
+ Cond[0].setImm(Xtensa::BLT);
+ return false;
+ case Xtensa::BLTU:
+ Cond[0].setImm(Xtensa::BGEU);
+ return false;
+ case Xtensa::BGEU:
+ Cond[0].setImm(Xtensa::BLTU);
+ return false;
+
+ case Xtensa::BEQI:
+ Cond[0].setImm(Xtensa::BNEI);
+ return false;
+ case Xtensa::BNEI:
+ Cond[0].setImm(Xtensa::BEQI);
+ return false;
+ case Xtensa::BGEI:
+ Cond[0].setImm(Xtensa::BLTI);
+ return false;
+ case Xtensa::BLTI:
+ Cond[0].setImm(Xtensa::BGEI);
+ return false;
+ case Xtensa::BGEUI:
+ Cond[0].setImm(Xtensa::BLTUI);
+ return false;
+ case Xtensa::BLTUI:
+ Cond[0].setImm(Xtensa::BGEUI);
+ return false;
+
+ case Xtensa::BEQZ:
+ Cond[0].setImm(Xtensa::BNEZ);
+ return false;
+ case Xtensa::BNEZ:
+ Cond[0].setImm(Xtensa::BEQZ);
+ return false;
+ case Xtensa::BLTZ:
+ Cond[0].setImm(Xtensa::BGEZ);
+ return false;
+ case Xtensa::BGEZ:
+ Cond[0].setImm(Xtensa::BLTZ);
+ return false;
+
+ default:
+ llvm_unreachable("Invalid branch condition!");
+ }
+}
+
+bool XtensaInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
+ MachineBasicBlock *&TBB,
+ MachineBasicBlock *&FBB,
+ SmallVectorImpl<MachineOperand> &Cond,
+ bool AllowModify = false) const {
+ // Most of the code and comments here are boilerplate.
+
+ // Start from the bottom of the block and work up, examining the
+ // terminator instructions.
+ MachineBasicBlock::iterator I = MBB.end();
+ while (I != MBB.begin()) {
+ --I;
+ if (I->isDebugValue())
+ continue;
+
+ // Working from the bottom, when we see a non-terminator instruction, we're
+ // done.
+ if (!isUnpredicatedTerminator(*I))
+ break;
+
+ // A terminator that isn't a branch can't easily be handled by this
+ // analysis.
+ SmallVector<MachineOperand, 4> ThisCond;
+ ThisCond.push_back(MachineOperand::CreateImm(0));
+ const MachineOperand *ThisTarget;
+ if (!isBranch(I, ThisCond, ThisTarget))
+ return true;
+
+ // Can't handle indirect branches.
+ if (!ThisTarget->isMBB())
+ return true;
+
+ if (ThisCond[0].getImm() == Xtensa::J) {
+ // Handle unconditional branches.
+ if (!AllowModify) {
+ TBB = ThisTarget->getMBB();
+ continue;
+ }
+
+ // If the block has any instructions after a JMP, delete them.
+ while (std::next(I) != MBB.end())
+ std::next(I)->eraseFromParent();
+
+ Cond.clear();
+ FBB = 0;
+
+ // TBB is used to indicate the unconditinal destination.
+ TBB = ThisTarget->getMBB();
+ continue;
+ }
+
+ // Working from the bottom, handle the first conditional branch.
+ if (Cond.empty()) {
+ // FIXME: add X86-style branch swap
+ FBB = TBB;
+ TBB = ThisTarget->getMBB();
+ Cond.push_back(MachineOperand::CreateImm(ThisCond[0].getImm()));
+
+ // push remaining operands
+ for (unsigned int i = 0; i < (I->getNumExplicitOperands() - 1); i++)
+ Cond.push_back(I->getOperand(i));
+
+ continue;
+ }
+
+ // Handle subsequent conditional branches.
+ assert(Cond.size() <= 4);
+ assert(TBB);
+
+ // Only handle the case where all conditional branches branch to the same
+ // destination.
+ if (TBB != ThisTarget->getMBB())
+ return true;
+
+ // If the conditions are the same, we can leave them alone.
+ unsigned OldCond = Cond[0].getImm();
+ if (OldCond == ThisCond[0].getImm())
+ continue;
+ }
+
+ return false;
+}
+
+unsigned XtensaInstrInfo::removeBranch(MachineBasicBlock &MBB,
+ int *BytesRemoved) const {
+ // Most of the code and comments here are boilerplate.
+ MachineBasicBlock::iterator I = MBB.end();
+ unsigned Count = 0;
+ if (BytesRemoved)
+ *BytesRemoved = 0;
+
+ while (I != MBB.begin()) {
+ --I;
+ SmallVector<MachineOperand, 4> Cond;
+ Cond.push_back(MachineOperand::CreateImm(0));
+ const MachineOperand *Target;
+ if (!isBranch(I, Cond, Target))
+ break;
+ if (!Target->isMBB())
+ break;
+ // Remove the branch.
+ if (BytesRemoved)
+ *BytesRemoved += getInstSizeInBytes(*I);
+ I->eraseFromParent();
+ I = MBB.end();
+ ++Count;
+ }
+ return Count;
+}
+
+unsigned XtensaInstrInfo::insertBranch(
+ MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
+ ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
+ unsigned Count = 0;
+ if (BytesAdded)
+ *BytesAdded = 0;
+ if (FBB) {
+ // Need to build two branches then
+ // one to branch to TBB on Cond
+ // and a second one immediately after to unconditionally jump to FBB
+ Count = InsertBranchAtInst(MBB, MBB.end(), TBB, Cond, DL, BytesAdded);
+ auto &MI = *BuildMI(&MBB, DL, get(Xtensa::J)).addMBB(FBB);
+ Count++;
+ if (BytesAdded)
+ *BytesAdded += getInstSizeInBytes(MI);
+ return Count;
+ }
+ // This function inserts the branch at the end of the MBB
+ Count += InsertBranchAtInst(MBB, MBB.end(), TBB, Cond, DL, BytesAdded);
+ return Count;
+}
+
+unsigned XtensaInstrInfo::InsertBranchAtInst(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator I,
+ MachineBasicBlock *TBB,
+ ArrayRef<MachineOperand> Cond,
+ const DebugLoc &DL,
+ int *BytesAdded) const {
+ // Shouldn't be a fall through.
+ assert(TBB && "InsertBranch must not be told to insert a fallthrough");
+ assert(Cond.size() <= 4 &&
+ "Xtensa branch conditions have less than four components!");
+
+ if (Cond.empty() || (Cond[0].getImm() == Xtensa::J)) {
+ // Unconditional branch
+ MachineInstr *MI = BuildMI(MBB, I, DL, get(Xtensa::J)).addMBB(TBB);
+ if (BytesAdded && MI)
+ *BytesAdded += getInstSizeInBytes(*MI);
+ return 1;
+ }
+
+ unsigned Count = 0;
+ unsigned BR_C = Cond[0].getImm();
+ MachineInstr *MI = nullptr;
+ switch (BR_C) {
+ case Xtensa::BEQ:
+ case Xtensa::BNE:
+ case Xtensa::BLT:
+ case Xtensa::BLTU:
+ case Xtensa::BGE:
+ case Xtensa::BGEU:
+ MI = BuildMI(MBB, I, DL, get(BR_C))
+ .addReg(Cond[1].getReg())
+ .addReg(Cond[2].getReg())
+ .addMBB(TBB);
+ break;
+ case Xtensa::BEQI:
+ case Xtensa::BNEI:
+ case Xtensa::BLTI:
+ case Xtensa::BLTUI:
+ case Xtensa::BGEI:
+ case Xtensa::BGEUI:
+ MI = BuildMI(MBB, I, DL, get(BR_C))
+ .addReg(Cond[1].getReg())
+ .addImm(Cond[2].getImm())
+ .addMBB(TBB);
+ break;
+ case Xtensa::BEQZ:
+ case Xtensa::BNEZ:
+ case Xtensa::BLTZ:
+ case Xtensa::BGEZ:
+ MI = BuildMI(MBB, I, DL, get(BR_C)).addReg(Cond[1].getReg()).addMBB(TBB);
+ break;
+ default:
+ llvm_unreachable("Invalid branch type!");
+ }
+ if (BytesAdded && MI)
+ *BytesAdded += getInstSizeInBytes(*MI);
+ ++Count;
+ return Count;
+}
+
+bool XtensaInstrInfo::isBranch(const MachineBasicBlock::iterator &MI,
+ SmallVectorImpl<MachineOperand> &Cond,
+ const MachineOperand *&Target) const {
+ unsigned OpCode = MI->getOpcode();
+ switch (OpCode) {
+ case Xtensa::J:
+ case Xtensa::JX:
+ case Xtensa::BR_JT:
+ Cond[0].setImm(OpCode);
+ Target = &MI->getOperand(0);
+ return true;
+ case Xtensa::BEQ:
+ case Xtensa::BNE:
+ case Xtensa::BLT:
+ case Xtensa::BLTU:
+ case Xtensa::BGE:
+ case Xtensa::BGEU:
+ Cond[0].setImm(OpCode);
+ Target = &MI->getOperand(2);
+ return true;
+
+ case Xtensa::BEQI:
+ case Xtensa::BNEI:
+ case Xtensa::BLTI:
+ case Xtensa::BLTUI:
+ case Xtensa::BGEI:
+ case Xtensa::BGEUI:
+ Cond[0].setImm(OpCode);
+ Target = &MI->getOperand(2);
+ return true;
+
+ case Xtensa::BEQZ:
+ case Xtensa::BNEZ:
+ case Xtensa::BLTZ:
+ case Xtensa::BGEZ:
+ Cond[0].setImm(OpCode);
+ Target = &MI->getOperand(1);
+ return true;
+
+ default:
+ assert(!MI->getDesc().isBranch() && "Unknown branch opcode");
+ return false;
+ }
+}
diff --git a/llvm/lib/Target/Xtensa/XtensaInstrInfo.h b/llvm/lib/Target/Xtensa/XtensaInstrInfo.h
index ddd5392d7ce6..09c40487dd38 100644
--- a/llvm/lib/Target/Xtensa/XtensaInstrInfo.h
+++ b/llvm/lib/Target/Xtensa/XtensaInstrInfo.h
@@ -64,6 +64,33 @@ public:
// physical register Reg.
void loadImmediate(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
unsigned *Reg, int64_t Value) const;
+ bool
+ reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
+ bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB,
+ MachineBasicBlock *&FBB,
+ SmallVectorImpl<MachineOperand> &Cond,
+ bool AllowModify) const override;
+ unsigned removeBranch(MachineBasicBlock &MBB,
+ int *BytesRemoved = nullptr) const override;
+ unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
+ MachineBasicBlock *FBB, ArrayRef<MachineOperand> Cond,
+ const DebugLoc &DL,
+ int *BytesAdded = nullptr) const override;
+
+ unsigned InsertBranchAtInst(MachineBasicBlock &MBB,
+ MachineBasicBlock::iterator I,
+ MachineBasicBlock *TBB,
+ ArrayRef<MachineOperand> Cond, const DebugLoc &DL,
+ int *BytesAdded) const;
+
+ // Return true if MI is a conditional or unconditional branch.
+ // When returning true, set Cond to the mask of condition-code
+ // values on which the instruction will branch, and set Target
+ // to the operand that contains the branch target. This target
+ // can be a register or a basic block.
+ bool isBranch(const MachineBasicBlock::iterator &MI,
+ SmallVectorImpl<MachineOperand> &Cond,
+ const MachineOperand *&Target) const;
};
} // end namespace llvm
--
2.40.1