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
|
//===-- PPCExpandAtomicPseudoInsts.cpp - Expand atomic pseudo instrs. -----===//
//
// 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 file contains a pass that expands atomic pseudo instructions into
// target instructions post RA. With such method, LL/SC loop is considered as
// a whole blob and make spilling unlikely happens in the LL/SC loop.
//
//===----------------------------------------------------------------------===//
#include "MCTargetDesc/PPCPredicates.h"
#include "PPC.h"
#include "PPCInstrInfo.h"
#include "PPCTargetMachine.h"
#include "llvm/CodeGen/LivePhysRegs.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
using namespace llvm;
#define DEBUG_TYPE "ppc-atomic-expand"
namespace {
class PPCExpandAtomicPseudo : public MachineFunctionPass {
public:
const PPCInstrInfo *TII;
const PPCRegisterInfo *TRI;
static char ID;
PPCExpandAtomicPseudo() : MachineFunctionPass(ID) {
initializePPCExpandAtomicPseudoPass(*PassRegistry::getPassRegistry());
}
bool runOnMachineFunction(MachineFunction &MF) override;
private:
bool expandMI(MachineBasicBlock &MBB, MachineInstr &MI,
MachineBasicBlock::iterator &NMBBI);
bool expandAtomicRMW128(MachineBasicBlock &MBB, MachineInstr &MI,
MachineBasicBlock::iterator &NMBBI);
bool expandAtomicCmpSwap128(MachineBasicBlock &MBB, MachineInstr &MI,
MachineBasicBlock::iterator &NMBBI);
};
static void PairedCopy(const PPCInstrInfo *TII, MachineBasicBlock &MBB,
MachineBasicBlock::iterator MBBI, const DebugLoc &DL,
Register Dest0, Register Dest1, Register Src0,
Register Src1) {
const MCInstrDesc &OR = TII->get(PPC::OR8);
const MCInstrDesc &XOR = TII->get(PPC::XOR8);
if (Dest0 == Src1 && Dest1 == Src0) {
// The most tricky case, swapping values.
BuildMI(MBB, MBBI, DL, XOR, Dest0).addReg(Dest0).addReg(Dest1);
BuildMI(MBB, MBBI, DL, XOR, Dest1).addReg(Dest0).addReg(Dest1);
BuildMI(MBB, MBBI, DL, XOR, Dest0).addReg(Dest0).addReg(Dest1);
} else if (Dest0 != Src0 || Dest1 != Src1) {
if (Dest0 == Src1 || Dest1 != Src0) {
BuildMI(MBB, MBBI, DL, OR, Dest1).addReg(Src1).addReg(Src1);
BuildMI(MBB, MBBI, DL, OR, Dest0).addReg(Src0).addReg(Src0);
} else {
BuildMI(MBB, MBBI, DL, OR, Dest0).addReg(Src0).addReg(Src0);
BuildMI(MBB, MBBI, DL, OR, Dest1).addReg(Src1).addReg(Src1);
}
}
}
bool PPCExpandAtomicPseudo::runOnMachineFunction(MachineFunction &MF) {
bool Changed = false;
TII = static_cast<const PPCInstrInfo *>(MF.getSubtarget().getInstrInfo());
TRI = &TII->getRegisterInfo();
for (MachineBasicBlock &MBB : MF) {
for (MachineBasicBlock::iterator MBBI = MBB.begin(), MBBE = MBB.end();
MBBI != MBBE;) {
MachineInstr &MI = *MBBI;
MachineBasicBlock::iterator NMBBI = std::next(MBBI);
Changed |= expandMI(MBB, MI, NMBBI);
MBBI = NMBBI;
}
}
if (Changed)
MF.RenumberBlocks();
return Changed;
}
bool PPCExpandAtomicPseudo::expandMI(MachineBasicBlock &MBB, MachineInstr &MI,
MachineBasicBlock::iterator &NMBBI) {
switch (MI.getOpcode()) {
case PPC::ATOMIC_SWAP_I128:
case PPC::ATOMIC_LOAD_ADD_I128:
case PPC::ATOMIC_LOAD_SUB_I128:
case PPC::ATOMIC_LOAD_XOR_I128:
case PPC::ATOMIC_LOAD_NAND_I128:
case PPC::ATOMIC_LOAD_AND_I128:
case PPC::ATOMIC_LOAD_OR_I128:
return expandAtomicRMW128(MBB, MI, NMBBI);
case PPC::ATOMIC_CMP_SWAP_I128:
return expandAtomicCmpSwap128(MBB, MI, NMBBI);
case PPC::BUILD_QUADWORD: {
Register Dst = MI.getOperand(0).getReg();
Register DstHi = TRI->getSubReg(Dst, PPC::sub_gp8_x0);
Register DstLo = TRI->getSubReg(Dst, PPC::sub_gp8_x1);
Register Lo = MI.getOperand(1).getReg();
Register Hi = MI.getOperand(2).getReg();
PairedCopy(TII, MBB, MI, MI.getDebugLoc(), DstHi, DstLo, Hi, Lo);
MI.eraseFromParent();
return true;
}
default:
return false;
}
}
bool PPCExpandAtomicPseudo::expandAtomicRMW128(
MachineBasicBlock &MBB, MachineInstr &MI,
MachineBasicBlock::iterator &NMBBI) {
const MCInstrDesc &LL = TII->get(PPC::LQARX);
const MCInstrDesc &SC = TII->get(PPC::STQCX);
DebugLoc DL = MI.getDebugLoc();
MachineFunction *MF = MBB.getParent();
const BasicBlock *BB = MBB.getBasicBlock();
// Create layout of control flow.
MachineFunction::iterator MFI = ++MBB.getIterator();
MachineBasicBlock *LoopMBB = MF->CreateMachineBasicBlock(BB);
MachineBasicBlock *ExitMBB = MF->CreateMachineBasicBlock(BB);
MF->insert(MFI, LoopMBB);
MF->insert(MFI, ExitMBB);
ExitMBB->splice(ExitMBB->begin(), &MBB, std::next(MI.getIterator()),
MBB.end());
ExitMBB->transferSuccessorsAndUpdatePHIs(&MBB);
MBB.addSuccessor(LoopMBB);
// For non-min/max operations, control flow is kinda like:
// MBB:
// ...
// LoopMBB:
// lqarx in, ptr
// addc out.sub_x1, in.sub_x1, op.sub_x1
// adde out.sub_x0, in.sub_x0, op.sub_x0
// stqcx out, ptr
// bne- LoopMBB
// ExitMBB:
// ...
Register Old = MI.getOperand(0).getReg();
Register OldHi = TRI->getSubReg(Old, PPC::sub_gp8_x0);
Register OldLo = TRI->getSubReg(Old, PPC::sub_gp8_x1);
Register Scratch = MI.getOperand(1).getReg();
Register ScratchHi = TRI->getSubReg(Scratch, PPC::sub_gp8_x0);
Register ScratchLo = TRI->getSubReg(Scratch, PPC::sub_gp8_x1);
Register RA = MI.getOperand(2).getReg();
Register RB = MI.getOperand(3).getReg();
Register IncrLo = MI.getOperand(4).getReg();
Register IncrHi = MI.getOperand(5).getReg();
unsigned RMWOpcode = MI.getOpcode();
MachineBasicBlock *CurrentMBB = LoopMBB;
BuildMI(CurrentMBB, DL, LL, Old).addReg(RA).addReg(RB);
switch (RMWOpcode) {
case PPC::ATOMIC_SWAP_I128:
PairedCopy(TII, *CurrentMBB, CurrentMBB->end(), DL, ScratchHi, ScratchLo,
IncrHi, IncrLo);
break;
case PPC::ATOMIC_LOAD_ADD_I128:
BuildMI(CurrentMBB, DL, TII->get(PPC::ADDC8), ScratchLo)
.addReg(IncrLo)
.addReg(OldLo);
BuildMI(CurrentMBB, DL, TII->get(PPC::ADDE8), ScratchHi)
.addReg(IncrHi)
.addReg(OldHi);
break;
case PPC::ATOMIC_LOAD_SUB_I128:
BuildMI(CurrentMBB, DL, TII->get(PPC::SUBFC8), ScratchLo)
.addReg(IncrLo)
.addReg(OldLo);
BuildMI(CurrentMBB, DL, TII->get(PPC::SUBFE8), ScratchHi)
.addReg(IncrHi)
.addReg(OldHi);
break;
#define TRIVIAL_ATOMICRMW(Opcode, Instr) \
case Opcode: \
BuildMI(CurrentMBB, DL, TII->get((Instr)), ScratchLo) \
.addReg(IncrLo) \
.addReg(OldLo); \
BuildMI(CurrentMBB, DL, TII->get((Instr)), ScratchHi) \
.addReg(IncrHi) \
.addReg(OldHi); \
break
TRIVIAL_ATOMICRMW(PPC::ATOMIC_LOAD_OR_I128, PPC::OR8);
TRIVIAL_ATOMICRMW(PPC::ATOMIC_LOAD_XOR_I128, PPC::XOR8);
TRIVIAL_ATOMICRMW(PPC::ATOMIC_LOAD_AND_I128, PPC::AND8);
TRIVIAL_ATOMICRMW(PPC::ATOMIC_LOAD_NAND_I128, PPC::NAND8);
#undef TRIVIAL_ATOMICRMW
default:
llvm_unreachable("Unhandled atomic RMW operation");
}
BuildMI(CurrentMBB, DL, SC).addReg(Scratch).addReg(RA).addReg(RB);
BuildMI(CurrentMBB, DL, TII->get(PPC::BCC))
.addImm(PPC::PRED_NE)
.addReg(PPC::CR0)
.addMBB(LoopMBB);
CurrentMBB->addSuccessor(LoopMBB);
CurrentMBB->addSuccessor(ExitMBB);
recomputeLiveIns(*LoopMBB);
recomputeLiveIns(*ExitMBB);
NMBBI = MBB.end();
MI.eraseFromParent();
return true;
}
bool PPCExpandAtomicPseudo::expandAtomicCmpSwap128(
MachineBasicBlock &MBB, MachineInstr &MI,
MachineBasicBlock::iterator &NMBBI) {
const MCInstrDesc &LL = TII->get(PPC::LQARX);
const MCInstrDesc &SC = TII->get(PPC::STQCX);
DebugLoc DL = MI.getDebugLoc();
MachineFunction *MF = MBB.getParent();
const BasicBlock *BB = MBB.getBasicBlock();
Register Old = MI.getOperand(0).getReg();
Register OldHi = TRI->getSubReg(Old, PPC::sub_gp8_x0);
Register OldLo = TRI->getSubReg(Old, PPC::sub_gp8_x1);
Register Scratch = MI.getOperand(1).getReg();
Register ScratchHi = TRI->getSubReg(Scratch, PPC::sub_gp8_x0);
Register ScratchLo = TRI->getSubReg(Scratch, PPC::sub_gp8_x1);
Register RA = MI.getOperand(2).getReg();
Register RB = MI.getOperand(3).getReg();
Register CmpLo = MI.getOperand(4).getReg();
Register CmpHi = MI.getOperand(5).getReg();
Register NewLo = MI.getOperand(6).getReg();
Register NewHi = MI.getOperand(7).getReg();
// Create layout of control flow.
// loop:
// old = lqarx ptr
// <compare old, cmp>
// bne 0, fail
// succ:
// stqcx new ptr
// bne 0, loop
// b exit
// fail:
// stqcx old ptr
// exit:
// ....
MachineFunction::iterator MFI = ++MBB.getIterator();
MachineBasicBlock *LoopCmpMBB = MF->CreateMachineBasicBlock(BB);
MachineBasicBlock *CmpSuccMBB = MF->CreateMachineBasicBlock(BB);
MachineBasicBlock *CmpFailMBB = MF->CreateMachineBasicBlock(BB);
MachineBasicBlock *ExitMBB = MF->CreateMachineBasicBlock(BB);
MF->insert(MFI, LoopCmpMBB);
MF->insert(MFI, CmpSuccMBB);
MF->insert(MFI, CmpFailMBB);
MF->insert(MFI, ExitMBB);
ExitMBB->splice(ExitMBB->begin(), &MBB, std::next(MI.getIterator()),
MBB.end());
ExitMBB->transferSuccessorsAndUpdatePHIs(&MBB);
MBB.addSuccessor(LoopCmpMBB);
// Build loop.
MachineBasicBlock *CurrentMBB = LoopCmpMBB;
BuildMI(CurrentMBB, DL, LL, Old).addReg(RA).addReg(RB);
BuildMI(CurrentMBB, DL, TII->get(PPC::XOR8), ScratchLo)
.addReg(OldLo)
.addReg(CmpLo);
BuildMI(CurrentMBB, DL, TII->get(PPC::XOR8), ScratchHi)
.addReg(OldHi)
.addReg(CmpHi);
BuildMI(CurrentMBB, DL, TII->get(PPC::OR8_rec), ScratchLo)
.addReg(ScratchLo)
.addReg(ScratchHi);
BuildMI(CurrentMBB, DL, TII->get(PPC::BCC))
.addImm(PPC::PRED_NE)
.addReg(PPC::CR0)
.addMBB(CmpFailMBB);
CurrentMBB->addSuccessor(CmpSuccMBB);
CurrentMBB->addSuccessor(CmpFailMBB);
// Build succ.
CurrentMBB = CmpSuccMBB;
PairedCopy(TII, *CurrentMBB, CurrentMBB->end(), DL, ScratchHi, ScratchLo,
NewHi, NewLo);
BuildMI(CurrentMBB, DL, SC).addReg(Scratch).addReg(RA).addReg(RB);
BuildMI(CurrentMBB, DL, TII->get(PPC::BCC))
.addImm(PPC::PRED_NE)
.addReg(PPC::CR0)
.addMBB(LoopCmpMBB);
BuildMI(CurrentMBB, DL, TII->get(PPC::B)).addMBB(ExitMBB);
CurrentMBB->addSuccessor(LoopCmpMBB);
CurrentMBB->addSuccessor(ExitMBB);
CurrentMBB = CmpFailMBB;
BuildMI(CurrentMBB, DL, SC).addReg(Old).addReg(RA).addReg(RB);
CurrentMBB->addSuccessor(ExitMBB);
recomputeLiveIns(*LoopCmpMBB);
recomputeLiveIns(*CmpSuccMBB);
recomputeLiveIns(*CmpFailMBB);
recomputeLiveIns(*ExitMBB);
NMBBI = MBB.end();
MI.eraseFromParent();
return true;
}
} // namespace
INITIALIZE_PASS(PPCExpandAtomicPseudo, DEBUG_TYPE, "PowerPC Expand Atomic",
false, false)
char PPCExpandAtomicPseudo::ID = 0;
FunctionPass *llvm::createPPCExpandAtomicPseudoPass() {
return new PPCExpandAtomicPseudo();
}
|