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
|
//===-- PPCCTRLoops.cpp - Generate CTR loops ------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This pass generates machine instructions for the CTR loops related pseudos:
// 1: MTCTRloop/DecreaseCTRloop
// 2: MTCTR8loop/DecreaseCTR8loop
//
// If a CTR loop can be generated:
// 1: MTCTRloop/MTCTR8loop will be converted to "mtctr"
// 2: DecreaseCTRloop/DecreaseCTR8loop will be converted to "bdnz/bdz" and
// its user branch instruction can be deleted.
//
// If a CTR loop can not be generated due to clobber of CTR:
// 1: MTCTRloop/MTCTR8loop can be deleted.
// 2: DecreaseCTRloop/DecreaseCTR8loop will be converted to "addi -1" and
// a "cmplwi/cmpldi".
//
// This pass runs just before register allocation, because we don't want
// register allocator to allocate register for DecreaseCTRloop if a CTR can be
// generated or if a CTR loop can not be generated, we don't have any condition
// register for the new added "cmplwi/cmpldi".
//
//===----------------------------------------------------------------------===//
#include "PPC.h"
#include "PPCInstrInfo.h"
#include "PPCSubtarget.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/Register.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include <cassert>
using namespace llvm;
#define DEBUG_TYPE "ppc-ctrloops"
STATISTIC(NumCTRLoops, "Number of CTR loops generated");
STATISTIC(NumNormalLoops, "Number of normal compare + branch loops generated");
namespace {
class PPCCTRLoops : public MachineFunctionPass {
public:
static char ID;
PPCCTRLoops() : MachineFunctionPass(ID) {
initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<MachineLoopInfo>();
MachineFunctionPass::getAnalysisUsage(AU);
}
bool runOnMachineFunction(MachineFunction &MF) override;
private:
const PPCInstrInfo *TII = nullptr;
MachineRegisterInfo *MRI = nullptr;
bool processLoop(MachineLoop *ML);
bool isCTRClobber(MachineInstr *MI, bool CheckReads) const;
void expandNormalLoops(MachineLoop *ML, MachineInstr *Start,
MachineInstr *Dec);
void expandCTRLoops(MachineLoop *ML, MachineInstr *Start, MachineInstr *Dec);
};
} // namespace
char PPCCTRLoops::ID = 0;
INITIALIZE_PASS_BEGIN(PPCCTRLoops, DEBUG_TYPE, "PowerPC CTR loops generation",
false, false)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
INITIALIZE_PASS_END(PPCCTRLoops, DEBUG_TYPE, "PowerPC CTR loops generation",
false, false)
FunctionPass *llvm::createPPCCTRLoopsPass() { return new PPCCTRLoops(); }
bool PPCCTRLoops::runOnMachineFunction(MachineFunction &MF) {
bool Changed = false;
auto &MLI = getAnalysis<MachineLoopInfo>();
TII = static_cast<const PPCInstrInfo *>(MF.getSubtarget().getInstrInfo());
MRI = &MF.getRegInfo();
for (auto *ML : MLI) {
if (ML->isOutermost())
Changed |= processLoop(ML);
}
#ifndef NDEBUG
for (const MachineBasicBlock &BB : MF) {
for (const MachineInstr &I : BB)
assert((I.getOpcode() != PPC::DecreaseCTRloop &&
I.getOpcode() != PPC::DecreaseCTR8loop) &&
"CTR loop pseudo is not expanded!");
}
#endif
return Changed;
}
bool PPCCTRLoops::isCTRClobber(MachineInstr *MI, bool CheckReads) const {
if (!CheckReads) {
// If we are only checking for defs, that is we are going to find
// definitions before MTCTRloop, for this case:
// CTR defination inside the callee of a call instruction will not impact
// the defination of MTCTRloop, so we can use definesRegister() for the
// check, no need to check the regmask.
return MI->definesRegister(PPC::CTR) || MI->definesRegister(PPC::CTR8);
}
if (MI->modifiesRegister(PPC::CTR) || MI->modifiesRegister(PPC::CTR8))
return true;
if (MI->getDesc().isCall())
return true;
// We define the CTR in the loop preheader, so if there is any CTR reader in
// the loop, we also can not use CTR loop form.
if (MI->readsRegister(PPC::CTR) || MI->readsRegister(PPC::CTR8))
return true;
return false;
}
bool PPCCTRLoops::processLoop(MachineLoop *ML) {
bool Changed = false;
// Align with HardwareLoop pass, process inner loops first.
for (MachineLoop *I : *ML)
Changed |= processLoop(I);
// If any inner loop is changed, outter loop must be without hardware loop
// intrinsics.
if (Changed)
return true;
auto IsLoopStart = [](MachineInstr &MI) {
return MI.getOpcode() == PPC::MTCTRloop ||
MI.getOpcode() == PPC::MTCTR8loop;
};
auto SearchForStart =
[&IsLoopStart](MachineBasicBlock *MBB) -> MachineInstr * {
for (auto &MI : *MBB) {
if (IsLoopStart(MI))
return &MI;
}
return nullptr;
};
MachineInstr *Start = nullptr;
MachineInstr *Dec = nullptr;
bool InvalidCTRLoop = false;
MachineBasicBlock *Preheader = ML->getLoopPreheader();
// If there is no preheader for this loop, there must be no MTCTRloop
// either.
if (!Preheader)
return false;
Start = SearchForStart(Preheader);
// This is not a CTR loop candidate.
if (!Start)
return false;
// If CTR is live to the preheader, we can not redefine the CTR register.
if (Preheader->isLiveIn(PPC::CTR) || Preheader->isLiveIn(PPC::CTR8))
InvalidCTRLoop = true;
// Make sure there is also no CTR clobber in the block preheader between the
// begin and MTCTR.
for (MachineBasicBlock::reverse_instr_iterator I =
std::next(Start->getReverseIterator());
I != Preheader->instr_rend(); ++I)
// Only check the definitions of CTR. If there is non-dead definition for
// the CTR, we conservatively don't generate a CTR loop.
if (isCTRClobber(&*I, /* CheckReads */ false)) {
InvalidCTRLoop = true;
break;
}
// Make sure there is also no CTR clobber/user in the block preheader between
// MTCTR and the end.
for (MachineBasicBlock::instr_iterator I = std::next(Start->getIterator());
I != Preheader->instr_end(); ++I)
if (isCTRClobber(&*I, /* CheckReads */ true)) {
InvalidCTRLoop = true;
break;
}
// Find the CTR loop components and decide whether or not to fall back to a
// normal loop.
for (auto *MBB : reverse(ML->getBlocks())) {
for (auto &MI : *MBB) {
if (MI.getOpcode() == PPC::DecreaseCTRloop ||
MI.getOpcode() == PPC::DecreaseCTR8loop)
Dec = &MI;
else if (!InvalidCTRLoop)
// If any instruction clobber CTR, then we can not generate a CTR loop.
InvalidCTRLoop |= isCTRClobber(&MI, /* CheckReads */ true);
}
if (Dec && InvalidCTRLoop)
break;
}
assert(Dec && "CTR loop is not complete!");
if (InvalidCTRLoop) {
expandNormalLoops(ML, Start, Dec);
++NumNormalLoops;
}
else {
expandCTRLoops(ML, Start, Dec);
++NumCTRLoops;
}
return true;
}
void PPCCTRLoops::expandNormalLoops(MachineLoop *ML, MachineInstr *Start,
MachineInstr *Dec) {
bool Is64Bit =
Start->getParent()->getParent()->getSubtarget<PPCSubtarget>().isPPC64();
MachineBasicBlock *Preheader = Start->getParent();
MachineBasicBlock *Exiting = Dec->getParent();
assert((Preheader && Exiting) &&
"Preheader and exiting should exist for CTR loop!");
assert(Dec->getOperand(1).getImm() == 1 &&
"Loop decrement stride must be 1");
unsigned ADDIOpcode = Is64Bit ? PPC::ADDI8 : PPC::ADDI;
unsigned CMPOpcode = Is64Bit ? PPC::CMPLDI : PPC::CMPLWI;
Register PHIDef =
MRI->createVirtualRegister(Is64Bit ? &PPC::G8RC_and_G8RC_NOX0RegClass
: &PPC::GPRC_and_GPRC_NOR0RegClass);
Start->getParent()->getParent()->getProperties().reset(
MachineFunctionProperties::Property::NoPHIs);
// Generate "PHI" in the header block.
auto PHIMIB = BuildMI(*ML->getHeader(), ML->getHeader()->getFirstNonPHI(),
DebugLoc(), TII->get(TargetOpcode::PHI), PHIDef);
PHIMIB.addReg(Start->getOperand(0).getReg()).addMBB(Preheader);
Register ADDIDef =
MRI->createVirtualRegister(Is64Bit ? &PPC::G8RC_and_G8RC_NOX0RegClass
: &PPC::GPRC_and_GPRC_NOR0RegClass);
// Generate "addi -1" in the exiting block.
BuildMI(*Exiting, Dec, Dec->getDebugLoc(), TII->get(ADDIOpcode), ADDIDef)
.addReg(PHIDef)
.addImm(-1);
// Add other inputs for the PHI node.
if (ML->isLoopLatch(Exiting)) {
// There must be only two predecessors for the loop header, one is the
// Preheader and the other one is loop latch Exiting. In hardware loop
// insertion pass, the block containing DecreaseCTRloop must dominate all
// loop latches. So there must be only one latch.
assert(ML->getHeader()->pred_size() == 2 &&
"Loop header predecessor is not right!");
PHIMIB.addReg(ADDIDef).addMBB(Exiting);
} else {
// If the block containing DecreaseCTRloop is not a loop latch, we can use
// ADDIDef as the value for all other blocks for the PHI. In hardware loop
// insertion pass, the block containing DecreaseCTRloop must dominate all
// loop latches.
for (MachineBasicBlock *P : ML->getHeader()->predecessors()) {
if (ML->contains(P)) {
assert(ML->isLoopLatch(P) &&
"Loop's header in-loop predecessor is not loop latch!");
PHIMIB.addReg(ADDIDef).addMBB(P);
} else
assert(P == Preheader &&
"CTR loop should not be generated for irreducible loop!");
}
}
// Generate the compare in the exiting block.
Register CMPDef = MRI->createVirtualRegister(&PPC::CRRCRegClass);
auto CMPMIB =
BuildMI(*Exiting, Dec, Dec->getDebugLoc(), TII->get(CMPOpcode), CMPDef)
.addReg(ADDIDef)
.addImm(0);
BuildMI(*Exiting, Dec, Dec->getDebugLoc(), TII->get(TargetOpcode::COPY),
Dec->getOperand(0).getReg())
.addReg(CMPMIB->getOperand(0).getReg(), 0, PPC::sub_gt);
// Remove the pseudo instructions.
Start->eraseFromParent();
Dec->eraseFromParent();
}
void PPCCTRLoops::expandCTRLoops(MachineLoop *ML, MachineInstr *Start,
MachineInstr *Dec) {
bool Is64Bit =
Start->getParent()->getParent()->getSubtarget<PPCSubtarget>().isPPC64();
MachineBasicBlock *Preheader = Start->getParent();
MachineBasicBlock *Exiting = Dec->getParent();
(void)Preheader;
assert((Preheader && Exiting) &&
"Preheader and exiting should exist for CTR loop!");
assert(Dec->getOperand(1).getImm() == 1 && "Loop decrement must be 1!");
unsigned BDNZOpcode = Is64Bit ? PPC::BDNZ8 : PPC::BDNZ;
unsigned BDZOpcode = Is64Bit ? PPC::BDZ8 : PPC::BDZ;
auto BrInstr = MRI->use_instr_begin(Dec->getOperand(0).getReg());
assert(MRI->hasOneUse(Dec->getOperand(0).getReg()) &&
"There should be only one user for loop decrement pseudo!");
unsigned Opcode = 0;
switch (BrInstr->getOpcode()) {
case PPC::BC:
Opcode = BDNZOpcode;
(void) ML;
assert(ML->contains(BrInstr->getOperand(1).getMBB()) &&
"Invalid ctr loop!");
break;
case PPC::BCn:
Opcode = BDZOpcode;
assert(!ML->contains(BrInstr->getOperand(1).getMBB()) &&
"Invalid ctr loop!");
break;
default:
llvm_unreachable("Unhandled branch user for DecreaseCTRloop.");
}
// Generate "bdnz/bdz" in the exiting block just before the terminator.
BuildMI(*Exiting, &*BrInstr, BrInstr->getDebugLoc(), TII->get(Opcode))
.addMBB(BrInstr->getOperand(1).getMBB());
// Remove the pseudo instructions.
BrInstr->eraseFromParent();
Dec->eraseFromParent();
}
|