LLVM 24.0.0git
MipsExpandPseudo.cpp
Go to the documentation of this file.
1//===-- MipsExpandPseudoInsts.cpp - Expand pseudo instructions ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains a pass that expands pseudo instructions into target
10// instructions to allow proper scheduling, if-conversion, and other late
11// optimizations. This pass should be run after register allocation but before
12// the post-regalloc scheduling pass.
13//
14// This is currently only used for expanding atomic pseudos after register
15// allocation. We do this to avoid the fast register allocator introducing
16// spills between ll and sc. These stores cause some MIPS implementations to
17// abort the atomic RMW sequence.
18//
19//===----------------------------------------------------------------------===//
20
21#include "Mips.h"
22#include "MipsInstrInfo.h"
23#include "MipsSubtarget.h"
29#include "llvm/IR/Analysis.h"
30
31using namespace llvm;
32
33#define DEBUG_TYPE "mips-expand-pseudo"
34
35namespace {
36class MipsExpandPseudoLegacy : public MachineFunctionPass {
37public:
38 static char ID;
39 MipsExpandPseudoLegacy() : MachineFunctionPass(ID) {}
40
41 bool runOnMachineFunction(MachineFunction &Fn) override;
42
43 MachineFunctionProperties getRequiredProperties() const override {
44 return MachineFunctionProperties().setNoVRegs();
45 }
46
47 StringRef getPassName() const override {
48 return "Mips pseudo instruction expansion pass";
49 }
50};
51char MipsExpandPseudoLegacy::ID = 0;
52} // namespace
53
57 const MipsSubtarget *STI,
58 const MipsInstrInfo *TII) {
59
60 MachineFunction *MF = BB.getParent();
61
62 const bool ArePtrs64bit = STI->getABI().ArePtrs64bit();
63 DebugLoc DL = I->getDebugLoc();
64 unsigned LL, SC;
65
66 unsigned ZERO = Mips::R0;
67 unsigned BNE = Mips::BNE;
68 unsigned BEQ = Mips::BEQ;
69 unsigned SEOp =
70 I->getOpcode() == Mips::ATOMIC_CMP_SWAP_I8_POSTRA ? Mips::SEB : Mips::SEH;
71
72 if (STI->inMicroMipsMode()) {
73 LL = STI->hasMips32r6() ? Mips::LL_MMR6 : Mips::LL_MM;
74 SC = STI->hasMips32r6() ? Mips::SC_MMR6 : Mips::SC_MM;
75 BNE = STI->hasMips32r6() ? Mips::BNEC_MMR6 : Mips::BNE_MM;
76 BEQ = STI->hasMips32r6() ? Mips::BEQC_MMR6 : Mips::BEQ_MM;
77 } else {
78 LL = STI->hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
79 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
80 SC = STI->hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
81 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
82 }
83
84 Register Dest = I->getOperand(0).getReg();
85 Register Ptr = I->getOperand(1).getReg();
86 Register Mask = I->getOperand(2).getReg();
87 Register ShiftCmpVal = I->getOperand(3).getReg();
88 Register Mask2 = I->getOperand(4).getReg();
89 Register ShiftNewVal = I->getOperand(5).getReg();
90 Register ShiftAmnt = I->getOperand(6).getReg();
91 Register Scratch = I->getOperand(7).getReg();
92 Register Scratch2 = I->getOperand(8).getReg();
93
94 // insert new blocks after the current block
95 const BasicBlock *LLVM_BB = BB.getBasicBlock();
96 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
97 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
98 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
99 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
101 MF->insert(It, loop1MBB);
102 MF->insert(It, loop2MBB);
103 MF->insert(It, sinkMBB);
104 MF->insert(It, exitMBB);
105
106 // Transfer the remainder of BB and its successor edges to exitMBB.
107 exitMBB->splice(exitMBB->begin(), &BB,
108 std::next(MachineBasicBlock::iterator(I)), BB.end());
110
111 // thisMBB:
112 // ...
113 // fallthrough --> loop1MBB
115 loop1MBB->addSuccessor(sinkMBB);
116 loop1MBB->addSuccessor(loop2MBB);
117 loop1MBB->normalizeSuccProbs();
118 loop2MBB->addSuccessor(loop1MBB);
119 loop2MBB->addSuccessor(sinkMBB);
120 loop2MBB->normalizeSuccProbs();
121 sinkMBB->addSuccessor(exitMBB, BranchProbability::getOne());
122
123 // loop1MBB:
124 // ll dest, 0(ptr)
125 // and Mask', dest, Mask
126 // bne Mask', ShiftCmpVal, exitMBB
127 BuildMI(loop1MBB, DL, TII->get(LL), Scratch).addReg(Ptr).addImm(0);
128 BuildMI(loop1MBB, DL, TII->get(Mips::AND), Scratch2)
129 .addReg(Scratch)
130 .addReg(Mask);
131 BuildMI(loop1MBB, DL, TII->get(BNE))
132 .addReg(Scratch2).addReg(ShiftCmpVal).addMBB(sinkMBB);
133
134 // loop2MBB:
135 // and dest, dest, mask2
136 // or dest, dest, ShiftNewVal
137 // sc dest, dest, 0(ptr)
138 // beq dest, $0, loop1MBB
139 BuildMI(loop2MBB, DL, TII->get(Mips::AND), Scratch)
140 .addReg(Scratch, RegState::Kill)
141 .addReg(Mask2);
142 BuildMI(loop2MBB, DL, TII->get(Mips::OR), Scratch)
143 .addReg(Scratch, RegState::Kill)
144 .addReg(ShiftNewVal);
145 BuildMI(loop2MBB, DL, TII->get(SC), Scratch)
146 .addReg(Scratch, RegState::Kill)
147 .addReg(Ptr)
148 .addImm(0);
149 BuildMI(loop2MBB, DL, TII->get(BEQ))
150 .addReg(Scratch, RegState::Kill)
151 .addReg(ZERO)
152 .addMBB(loop1MBB);
153
154 // sinkMBB:
155 // srl srlres, Mask', shiftamt
156 // sign_extend dest,srlres
157 BuildMI(sinkMBB, DL, TII->get(Mips::SRLV), Dest)
158 .addReg(Scratch2)
159 .addReg(ShiftAmnt);
160 if (STI->hasMips32r2()) {
161 BuildMI(sinkMBB, DL, TII->get(SEOp), Dest).addReg(Dest);
162 } else {
163 const unsigned ShiftImm =
164 I->getOpcode() == Mips::ATOMIC_CMP_SWAP_I16_POSTRA ? 16 : 24;
165 BuildMI(sinkMBB, DL, TII->get(Mips::SLL), Dest)
166 .addReg(Dest, RegState::Kill)
167 .addImm(ShiftImm);
168 BuildMI(sinkMBB, DL, TII->get(Mips::SRA), Dest)
169 .addReg(Dest, RegState::Kill)
170 .addImm(ShiftImm);
171 }
172
174 computeAndAddLiveIns(LiveRegs, *loop1MBB);
175 computeAndAddLiveIns(LiveRegs, *loop2MBB);
178
179 NMBBI = BB.end();
180 I->eraseFromParent();
181 return true;
182}
183
187 const MipsSubtarget *STI,
188 const MipsInstrInfo *TII) {
189
190 const unsigned Size =
191 I->getOpcode() == Mips::ATOMIC_CMP_SWAP_I32_POSTRA ? 4 : 8;
192 MachineFunction *MF = BB.getParent();
193
194 const bool ArePtrs64bit = STI->getABI().ArePtrs64bit();
195 DebugLoc DL = I->getDebugLoc();
196
197 unsigned LL, SC, ZERO, BNE, BEQ, MOVE;
198
199 if (Size == 4) {
200 if (STI->inMicroMipsMode()) {
201 LL = STI->hasMips32r6() ? Mips::LL_MMR6 : Mips::LL_MM;
202 SC = STI->hasMips32r6() ? Mips::SC_MMR6 : Mips::SC_MM;
203 BNE = STI->hasMips32r6() ? Mips::BNEC_MMR6 : Mips::BNE_MM;
204 BEQ = STI->hasMips32r6() ? Mips::BEQC_MMR6 : Mips::BEQ_MM;
205 } else {
206 LL = STI->hasMips32r6()
207 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
208 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
209 SC = STI->hasMips32r6()
210 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
211 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
212 BNE = Mips::BNE;
213 BEQ = Mips::BEQ;
214 }
215
216 ZERO = Mips::R0;
217 MOVE = Mips::OR;
218 } else {
219 LL = STI->hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
220 SC = STI->hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
221 ZERO = Mips::R0_64;
222 BNE = Mips::BNE64;
223 BEQ = Mips::BEQ64;
224 MOVE = Mips::OR64;
225 }
226
227 Register Dest = I->getOperand(0).getReg();
228 Register Ptr = I->getOperand(1).getReg();
229 Register OldVal = I->getOperand(2).getReg();
230 Register NewVal = I->getOperand(3).getReg();
231 Register Scratch = I->getOperand(4).getReg();
232
233 // insert new blocks after the current block
234 const BasicBlock *LLVM_BB = BB.getBasicBlock();
235 MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
236 MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
237 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
239 MF->insert(It, loop1MBB);
240 MF->insert(It, loop2MBB);
241 MF->insert(It, exitMBB);
242
243 // Transfer the remainder of BB and its successor edges to exitMBB.
244 exitMBB->splice(exitMBB->begin(), &BB,
245 std::next(MachineBasicBlock::iterator(I)), BB.end());
247
248 // thisMBB:
249 // ...
250 // fallthrough --> loop1MBB
252 loop1MBB->addSuccessor(exitMBB);
253 loop1MBB->addSuccessor(loop2MBB);
254 loop1MBB->normalizeSuccProbs();
255 loop2MBB->addSuccessor(loop1MBB);
256 loop2MBB->addSuccessor(exitMBB);
257 loop2MBB->normalizeSuccProbs();
258
259 // loop1MBB:
260 // ll dest, 0(ptr)
261 // bne dest, oldval, exitMBB
262 BuildMI(loop1MBB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
263 BuildMI(loop1MBB, DL, TII->get(BNE))
264 .addReg(Dest, RegState::Kill).addReg(OldVal).addMBB(exitMBB);
265
266 // loop2MBB:
267 // move scratch, NewVal
268 // sc Scratch, Scratch, 0(ptr)
269 // beq Scratch, $0, loop1MBB
270 BuildMI(loop2MBB, DL, TII->get(MOVE), Scratch).addReg(NewVal).addReg(ZERO);
271 BuildMI(loop2MBB, DL, TII->get(SC), Scratch)
272 .addReg(Scratch).addReg(Ptr).addImm(0);
273 BuildMI(loop2MBB, DL, TII->get(BEQ))
274 .addReg(Scratch, RegState::Kill).addReg(ZERO).addMBB(loop1MBB);
275
277 computeAndAddLiveIns(LiveRegs, *loop1MBB);
278 computeAndAddLiveIns(LiveRegs, *loop2MBB);
280
281 NMBBI = BB.end();
282 I->eraseFromParent();
283 return true;
284}
285
289 const MipsSubtarget *STI,
290 const MipsInstrInfo *TII) {
291
292 MachineFunction *MF = BB.getParent();
293
294 const bool ArePtrs64bit = STI->getABI().ArePtrs64bit();
295 DebugLoc DL = I->getDebugLoc();
296
297 unsigned LL, SC, SLT, SLTu, OR, MOVN, MOVZ, SELNEZ, SELEQZ;
298 unsigned BEQ = Mips::BEQ;
299 unsigned SEOp = Mips::SEH;
300
301 if (STI->inMicroMipsMode()) {
302 LL = STI->hasMips32r6() ? Mips::LL_MMR6 : Mips::LL_MM;
303 SC = STI->hasMips32r6() ? Mips::SC_MMR6 : Mips::SC_MM;
304 BEQ = STI->hasMips32r6() ? Mips::BEQC_MMR6 : Mips::BEQ_MM;
305 SLT = Mips::SLT_MM;
306 SLTu = Mips::SLTu_MM;
307 OR = STI->hasMips32r6() ? Mips::OR_MMR6 : Mips::OR_MM;
308 MOVN = Mips::MOVN_I_MM;
309 MOVZ = Mips::MOVZ_I_MM;
310 SELNEZ = STI->hasMips32r6() ? Mips::SELNEZ_MMR6 : Mips::SELNEZ;
311 SELEQZ = STI->hasMips32r6() ? Mips::SELEQZ_MMR6 : Mips::SELEQZ;
312 } else {
313 LL = STI->hasMips32r6() ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
314 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
315 SC = STI->hasMips32r6() ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
316 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
317 SLT = Mips::SLT;
318 SLTu = Mips::SLTu;
319 OR = Mips::OR;
320 MOVN = Mips::MOVN_I_I;
321 MOVZ = Mips::MOVZ_I_I;
322 SELNEZ = Mips::SELNEZ;
323 SELEQZ = Mips::SELEQZ;
324 }
325
326 bool IsSwap = false;
327 bool IsNand = false;
328 bool IsMin = false;
329 bool IsMax = false;
330 bool IsUnsigned = false;
331 bool DestOK = false;
332
333 unsigned Opcode = 0;
334 switch (I->getOpcode()) {
335 case Mips::ATOMIC_LOAD_NAND_I8_POSTRA:
336 SEOp = Mips::SEB;
337 [[fallthrough]];
338 case Mips::ATOMIC_LOAD_NAND_I16_POSTRA:
339 IsNand = true;
340 break;
341 case Mips::ATOMIC_SWAP_I8_POSTRA:
342 SEOp = Mips::SEB;
343 [[fallthrough]];
344 case Mips::ATOMIC_SWAP_I16_POSTRA:
345 IsSwap = true;
346 break;
347 case Mips::ATOMIC_LOAD_ADD_I8_POSTRA:
348 SEOp = Mips::SEB;
349 [[fallthrough]];
350 case Mips::ATOMIC_LOAD_ADD_I16_POSTRA:
351 Opcode = Mips::ADDu;
352 break;
353 case Mips::ATOMIC_LOAD_SUB_I8_POSTRA:
354 SEOp = Mips::SEB;
355 [[fallthrough]];
356 case Mips::ATOMIC_LOAD_SUB_I16_POSTRA:
357 Opcode = Mips::SUBu;
358 break;
359 case Mips::ATOMIC_LOAD_AND_I8_POSTRA:
360 SEOp = Mips::SEB;
361 [[fallthrough]];
362 case Mips::ATOMIC_LOAD_AND_I16_POSTRA:
363 Opcode = Mips::AND;
364 break;
365 case Mips::ATOMIC_LOAD_OR_I8_POSTRA:
366 SEOp = Mips::SEB;
367 [[fallthrough]];
368 case Mips::ATOMIC_LOAD_OR_I16_POSTRA:
369 Opcode = Mips::OR;
370 break;
371 case Mips::ATOMIC_LOAD_XOR_I8_POSTRA:
372 SEOp = Mips::SEB;
373 [[fallthrough]];
374 case Mips::ATOMIC_LOAD_XOR_I16_POSTRA:
375 Opcode = Mips::XOR;
376 break;
377 case Mips::ATOMIC_LOAD_UMIN_I8_POSTRA:
378 SEOp = Mips::SEB;
379 IsUnsigned = true;
380 IsMin = true;
381 break;
382 case Mips::ATOMIC_LOAD_UMIN_I16_POSTRA:
383 IsUnsigned = true;
384 IsMin = true;
385 break;
386 case Mips::ATOMIC_LOAD_MIN_I8_POSTRA:
387 SEOp = Mips::SEB;
388 IsMin = true;
389 break;
390 case Mips::ATOMIC_LOAD_MIN_I16_POSTRA:
391 IsMin = true;
392 break;
393 case Mips::ATOMIC_LOAD_UMAX_I8_POSTRA:
394 SEOp = Mips::SEB;
395 IsUnsigned = true;
396 IsMax = true;
397 break;
398 case Mips::ATOMIC_LOAD_UMAX_I16_POSTRA:
399 IsUnsigned = true;
400 IsMax = true;
401 break;
402 case Mips::ATOMIC_LOAD_MAX_I8_POSTRA:
403 SEOp = Mips::SEB;
404 IsMax = true;
405 break;
406 case Mips::ATOMIC_LOAD_MAX_I16_POSTRA:
407 IsMax = true;
408 break;
409 default:
410 llvm_unreachable("Unknown subword atomic pseudo for expansion!");
411 }
412
413 Register Dest = I->getOperand(0).getReg();
414 Register Ptr = I->getOperand(1).getReg();
415 Register Incr = I->getOperand(2).getReg();
416 Register Mask = I->getOperand(3).getReg();
417 Register Mask2 = I->getOperand(4).getReg();
418 Register ShiftAmnt = I->getOperand(5).getReg();
419 Register OldVal = I->getOperand(6).getReg();
420 Register BinOpRes = I->getOperand(7).getReg();
421 Register StoreVal = I->getOperand(8).getReg();
422 bool NoMovnInstr = (IsMin || IsMax) && !STI->hasMips4() && !STI->hasMips32();
423
424 const BasicBlock *LLVM_BB = BB.getBasicBlock();
425 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
426 MachineBasicBlock *loop1MBB = nullptr;
427 MachineBasicBlock *loop2MBB = nullptr;
428 if (NoMovnInstr) {
429 loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
430 loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
431 }
432 MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
433 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
435 MF->insert(It, loopMBB);
436 if (NoMovnInstr) {
437 MF->insert(It, loop1MBB);
438 MF->insert(It, loop2MBB);
439 }
440 MF->insert(It, sinkMBB);
441 MF->insert(It, exitMBB);
442
443 exitMBB->splice(exitMBB->begin(), &BB, std::next(I), BB.end());
445
447 if (NoMovnInstr) {
448 loopMBB->addSuccessor(loop1MBB);
449 loopMBB->addSuccessor(loop2MBB);
450 } else {
451 loopMBB->addSuccessor(sinkMBB);
452 loopMBB->addSuccessor(loopMBB);
453 loopMBB->normalizeSuccProbs();
454 }
455 if (NoMovnInstr) {
456 loop1MBB->addSuccessor(loop2MBB);
457 loop2MBB->addSuccessor(loopMBB);
458 loop2MBB->addSuccessor(sinkMBB);
459 }
460
461 BuildMI(loopMBB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
462 if (IsNand) {
463 // and andres, oldval, incr2
464 // nor binopres, $0, andres
465 // and newval, binopres, mask
466 BuildMI(loopMBB, DL, TII->get(Mips::AND), BinOpRes)
467 .addReg(OldVal)
468 .addReg(Incr);
469 BuildMI(loopMBB, DL, TII->get(Mips::NOR), BinOpRes)
470 .addReg(Mips::R0)
471 .addReg(BinOpRes);
472 BuildMI(loopMBB, DL, TII->get(Mips::AND), BinOpRes)
473 .addReg(BinOpRes)
474 .addReg(Mask);
475 } else if (IsMin || IsMax) {
476
477 assert(I->getNumOperands() == 10 &&
478 "Atomics min|max|umin|umax use an additional register");
479 Register Scratch4 = I->getOperand(9).getReg();
480
481 unsigned SLTScratch4 = IsUnsigned ? SLTu : SLT;
482 unsigned SELIncr = IsMax ? SELNEZ : SELEQZ;
483 unsigned SELOldVal = IsMax ? SELEQZ : SELNEZ;
484 unsigned MOVIncr = IsMax ? MOVN : MOVZ;
485
486 BuildMI(loopMBB, DL, TII->get(Mips::SRAV), StoreVal)
487 .addReg(OldVal)
488 .addReg(ShiftAmnt);
489 if (IsUnsigned) {
490 const unsigned OpMask = SEOp == Mips::SEH ? 0xffff : 0xff;
491 BuildMI(loopMBB, DL, TII->get(Mips::ANDi), StoreVal)
492 .addReg(StoreVal)
493 .addImm(OpMask);
494 } else if (STI->hasMips32r2()) {
495 BuildMI(loopMBB, DL, TII->get(SEOp), StoreVal).addReg(StoreVal);
496 } else {
497 const unsigned ShiftImm = SEOp == Mips::SEH ? 16 : 24;
498 const unsigned SROp = IsUnsigned ? Mips::SRL : Mips::SRA;
499 BuildMI(loopMBB, DL, TII->get(Mips::SLL), StoreVal)
500 .addReg(StoreVal, RegState::Kill)
501 .addImm(ShiftImm);
502 BuildMI(loopMBB, DL, TII->get(SROp), StoreVal)
503 .addReg(StoreVal, RegState::Kill)
504 .addImm(ShiftImm);
505 }
506 BuildMI(loopMBB, DL, TII->get(Mips::OR), Dest)
507 .addReg(Mips::R0)
508 .addReg(StoreVal);
509 DestOK = true;
510 BuildMI(loopMBB, DL, TII->get(Mips::SLLV), StoreVal)
511 .addReg(StoreVal)
512 .addReg(ShiftAmnt);
513
514 // unsigned: sltu Scratch4, StoreVal, Incr
515 // signed: slt Scratch4, StoreVal, Incr
516 BuildMI(loopMBB, DL, TII->get(SLTScratch4), Scratch4)
517 .addReg(StoreVal)
518 .addReg(Incr);
519
520 if (STI->hasMips64r6() || STI->hasMips32r6()) {
521 // max: seleqz BinOpRes, OldVal, Scratch4
522 // selnez Scratch4, Incr, Scratch4
523 // or BinOpRes, BinOpRes, Scratch4
524 // min: selnqz BinOpRes, OldVal, Scratch4
525 // seleqz Scratch4, Incr, Scratch4
526 // or BinOpRes, BinOpRes, Scratch4
527 BuildMI(loopMBB, DL, TII->get(SELOldVal), BinOpRes)
528 .addReg(StoreVal)
529 .addReg(Scratch4);
530 BuildMI(loopMBB, DL, TII->get(SELIncr), Scratch4)
531 .addReg(Incr)
532 .addReg(Scratch4);
533 BuildMI(loopMBB, DL, TII->get(OR), BinOpRes)
534 .addReg(BinOpRes)
535 .addReg(Scratch4);
536 } else if (STI->hasMips4() || STI->hasMips32()) {
537 // max: move BinOpRes, StoreVal
538 // movn BinOpRes, Incr, Scratch4, BinOpRes
539 // min: move BinOpRes, StoreVal
540 // movz BinOpRes, Incr, Scratch4, BinOpRes
541 BuildMI(loopMBB, DL, TII->get(OR), BinOpRes)
542 .addReg(StoreVal)
543 .addReg(Mips::R0);
544 BuildMI(loopMBB, DL, TII->get(MOVIncr), BinOpRes)
545 .addReg(Incr)
546 .addReg(Scratch4)
547 .addReg(BinOpRes);
548 } else {
549 // if min:
550 // loopMBB: move BinOpRes, StoreVal
551 // bne Scratch4, 0, loop2MBB
552 // loop1MBB: move BinOpRes, Incr
553 // loop2MBB: and BinOpRes, BinOpRes, Mask
554 // and StoreVal, OlddVal, Mask2
555 // or StoreVal, StoreVal, BinOpRes
556 // StoreVal<tied1> = sc StoreVal, 0(Ptr)
557 // beq StoreVal, zero, loopMBB
558 //
559 // if max:
560 // loopMBB: move BinOpRes, Incr
561 // bne Scratch4, 0, loop2MBB
562 // loop1MBB: move BinOpRes, StoreVal
563 // loop2MBB: and BinOpRes, BinOpRes, Mask
564 // and StoreVal, OlddVal, Mask2
565 // or StoreVal, StoreVal, BinOpRes
566 // StoreVal<tied1> = sc StoreVal, 0(Ptr)
567 // beq StoreVal, zero, loopMBB
568 if (IsMin) {
569 BuildMI(loopMBB, DL, TII->get(OR), BinOpRes)
570 .addReg(StoreVal)
571 .addReg(Mips::R0);
572 BuildMI(loop1MBB, DL, TII->get(OR), BinOpRes)
573 .addReg(Incr)
574 .addReg(Mips::R0);
575 } else {
576 BuildMI(loopMBB, DL, TII->get(OR), BinOpRes)
577 .addReg(Incr)
578 .addReg(Mips::R0);
579 BuildMI(loop1MBB, DL, TII->get(OR), BinOpRes)
580 .addReg(StoreVal)
581 .addReg(Mips::R0);
582 }
583 // loop1MBB is laid out directly after loopMBB, so fall through to it
584 // rather than using an unconditional jump.
585 BuildMI(loopMBB, DL, TII->get(Mips::BNE))
586 .addReg(Scratch4)
587 .addReg(Mips::R0)
588 .addMBB(loop2MBB);
589 }
590
591 // and BinOpRes, BinOpRes, Mask
592 if (NoMovnInstr)
593 BuildMI(loop2MBB, DL, TII->get(Mips::AND), BinOpRes)
594 .addReg(BinOpRes)
595 .addReg(Mask);
596 else
597 BuildMI(loopMBB, DL, TII->get(Mips::AND), BinOpRes)
598 .addReg(BinOpRes)
599 .addReg(Mask);
600
601 } else if (!IsSwap) {
602 // <binop> binopres, oldval, incr2
603 // and newval, binopres, mask
604 BuildMI(loopMBB, DL, TII->get(Opcode), BinOpRes)
605 .addReg(OldVal)
606 .addReg(Incr);
607 BuildMI(loopMBB, DL, TII->get(Mips::AND), BinOpRes)
608 .addReg(BinOpRes)
609 .addReg(Mask);
610 } else { // atomic.swap
611 // and newval, incr2, mask
612 BuildMI(loopMBB, DL, TII->get(Mips::AND), BinOpRes)
613 .addReg(Incr)
614 .addReg(Mask);
615 }
616
617 // and StoreVal, OlddVal, Mask2
618 // or StoreVal, StoreVal, BinOpRes
619 // StoreVal<tied1> = sc StoreVal, 0(Ptr)
620 // beq StoreVal, zero, loopMBB
621 if (NoMovnInstr) {
622 BuildMI(loop2MBB, DL, TII->get(Mips::AND), StoreVal)
623 .addReg(OldVal)
624 .addReg(Mask2);
625 BuildMI(loop2MBB, DL, TII->get(Mips::OR), StoreVal)
626 .addReg(StoreVal)
627 .addReg(BinOpRes);
628 BuildMI(loop2MBB, DL, TII->get(SC), StoreVal)
629 .addReg(StoreVal)
630 .addReg(Ptr)
631 .addImm(0);
632 BuildMI(loop2MBB, DL, TII->get(BEQ))
633 .addReg(StoreVal)
634 .addReg(Mips::R0)
635 .addMBB(loopMBB);
636 } else {
637 BuildMI(loopMBB, DL, TII->get(Mips::AND), StoreVal)
638 .addReg(OldVal)
639 .addReg(Mask2);
640 BuildMI(loopMBB, DL, TII->get(Mips::OR), StoreVal)
641 .addReg(StoreVal)
642 .addReg(BinOpRes);
643 BuildMI(loopMBB, DL, TII->get(SC), StoreVal)
644 .addReg(StoreVal)
645 .addReg(Ptr)
646 .addImm(0);
647 BuildMI(loopMBB, DL, TII->get(BEQ))
648 .addReg(StoreVal)
649 .addReg(Mips::R0)
650 .addMBB(loopMBB);
651 }
652
653 // sinkMBB:
654 // and maskedoldval1,oldval,mask
655 // srl srlres,maskedoldval1,shiftamt
656 // sign_extend dest,srlres
657
658 if (!DestOK) {
659 sinkMBB->addSuccessor(exitMBB, BranchProbability::getOne());
660 BuildMI(sinkMBB, DL, TII->get(Mips::AND), Dest).addReg(OldVal).addReg(Mask);
661 BuildMI(sinkMBB, DL, TII->get(Mips::SRLV), Dest)
662 .addReg(Dest)
663 .addReg(ShiftAmnt);
664
665 if (STI->hasMips32r2()) {
666 BuildMI(sinkMBB, DL, TII->get(SEOp), Dest).addReg(Dest);
667 } else {
668 const unsigned ShiftImm = SEOp == Mips::SEH ? 16 : 24;
669 BuildMI(sinkMBB, DL, TII->get(Mips::SLL), Dest)
670 .addReg(Dest, RegState::Kill)
671 .addImm(ShiftImm);
672 BuildMI(sinkMBB, DL, TII->get(Mips::SRA), Dest)
673 .addReg(Dest, RegState::Kill)
674 .addImm(ShiftImm);
675 }
676 }
677
680 if (loop1MBB) {
681 assert(loop2MBB && "should have 2 loop blocks");
682 computeAndAddLiveIns(LiveRegs, *loop1MBB);
683 computeAndAddLiveIns(LiveRegs, *loop2MBB);
684 }
687
688 NMBBI = BB.end();
689 I->eraseFromParent();
690
691 return true;
692}
693
696 MachineBasicBlock::iterator &NMBBI, unsigned Size,
697 const MipsSubtarget *STI,
698 const MipsInstrInfo *TII) {
699 MachineFunction *MF = BB.getParent();
700
701 const bool ArePtrs64bit = STI->getABI().ArePtrs64bit();
702 DebugLoc DL = I->getDebugLoc();
703
704 unsigned LL, SC, ZERO, BEQ, SLT, SLTu, OR, MOVN, MOVZ, SELNEZ, SELEQZ;
705
706 if (Size == 4) {
707 if (STI->inMicroMipsMode()) {
708 LL = STI->hasMips32r6() ? Mips::LL_MMR6 : Mips::LL_MM;
709 SC = STI->hasMips32r6() ? Mips::SC_MMR6 : Mips::SC_MM;
710 BEQ = STI->hasMips32r6() ? Mips::BEQC_MMR6 : Mips::BEQ_MM;
711 SLT = Mips::SLT_MM;
712 SLTu = Mips::SLTu_MM;
713 OR = STI->hasMips32r6() ? Mips::OR_MMR6 : Mips::OR_MM;
714 MOVN = Mips::MOVN_I_MM;
715 MOVZ = Mips::MOVZ_I_MM;
716 SELNEZ = STI->hasMips32r6() ? Mips::SELNEZ_MMR6 : Mips::SELNEZ;
717 SELEQZ = STI->hasMips32r6() ? Mips::SELEQZ_MMR6 : Mips::SELEQZ;
718 } else {
719 LL = STI->hasMips32r6()
720 ? (ArePtrs64bit ? Mips::LL64_R6 : Mips::LL_R6)
721 : (ArePtrs64bit ? Mips::LL64 : Mips::LL);
722 SC = STI->hasMips32r6()
723 ? (ArePtrs64bit ? Mips::SC64_R6 : Mips::SC_R6)
724 : (ArePtrs64bit ? Mips::SC64 : Mips::SC);
725 BEQ = Mips::BEQ;
726 SLT = Mips::SLT;
727 SLTu = Mips::SLTu;
728 OR = Mips::OR;
729 MOVN = Mips::MOVN_I_I;
730 MOVZ = Mips::MOVZ_I_I;
731 SELNEZ = Mips::SELNEZ;
732 SELEQZ = Mips::SELEQZ;
733 }
734
735 ZERO = Mips::R0;
736 } else {
737 LL = STI->hasMips64r6() ? Mips::LLD_R6 : Mips::LLD;
738 SC = STI->hasMips64r6() ? Mips::SCD_R6 : Mips::SCD;
739 ZERO = Mips::R0_64;
740 BEQ = Mips::BEQ64;
741 SLT = Mips::SLT64;
742 SLTu = Mips::SLTu64;
743 OR = Mips::OR64;
744 MOVN = Mips::MOVN_I64_I64;
745 MOVZ = Mips::MOVZ_I64_I64;
746 SELNEZ = Mips::SELNEZ64;
747 SELEQZ = Mips::SELEQZ64;
748 }
749
750 Register OldVal = I->getOperand(0).getReg();
751 Register Ptr = I->getOperand(1).getReg();
752 Register Incr = I->getOperand(2).getReg();
753 Register Scratch = I->getOperand(3).getReg();
754
755 unsigned Opcode = 0;
756 unsigned AND = 0;
757 unsigned NOR = 0;
758
759 bool IsOr = false;
760 bool IsNand = false;
761 bool IsMin = false;
762 bool IsMax = false;
763 bool IsUnsigned = false;
764
765 switch (I->getOpcode()) {
766 case Mips::ATOMIC_LOAD_ADD_I32_POSTRA:
767 Opcode = Mips::ADDu;
768 break;
769 case Mips::ATOMIC_LOAD_SUB_I32_POSTRA:
770 Opcode = Mips::SUBu;
771 break;
772 case Mips::ATOMIC_LOAD_AND_I32_POSTRA:
773 Opcode = Mips::AND;
774 break;
775 case Mips::ATOMIC_LOAD_OR_I32_POSTRA:
776 Opcode = Mips::OR;
777 break;
778 case Mips::ATOMIC_LOAD_XOR_I32_POSTRA:
779 Opcode = Mips::XOR;
780 break;
781 case Mips::ATOMIC_LOAD_NAND_I32_POSTRA:
782 IsNand = true;
783 AND = Mips::AND;
784 NOR = Mips::NOR;
785 break;
786 case Mips::ATOMIC_SWAP_I32_POSTRA:
787 IsOr = true;
788 break;
789 case Mips::ATOMIC_LOAD_ADD_I64_POSTRA:
790 Opcode = Mips::DADDu;
791 break;
792 case Mips::ATOMIC_LOAD_SUB_I64_POSTRA:
793 Opcode = Mips::DSUBu;
794 break;
795 case Mips::ATOMIC_LOAD_AND_I64_POSTRA:
796 Opcode = Mips::AND64;
797 break;
798 case Mips::ATOMIC_LOAD_OR_I64_POSTRA:
799 Opcode = Mips::OR64;
800 break;
801 case Mips::ATOMIC_LOAD_XOR_I64_POSTRA:
802 Opcode = Mips::XOR64;
803 break;
804 case Mips::ATOMIC_LOAD_NAND_I64_POSTRA:
805 IsNand = true;
806 AND = Mips::AND64;
807 NOR = Mips::NOR64;
808 break;
809 case Mips::ATOMIC_SWAP_I64_POSTRA:
810 IsOr = true;
811 break;
812 case Mips::ATOMIC_LOAD_UMIN_I32_POSTRA:
813 case Mips::ATOMIC_LOAD_UMIN_I64_POSTRA:
814 IsUnsigned = true;
815 [[fallthrough]];
816 case Mips::ATOMIC_LOAD_MIN_I32_POSTRA:
817 case Mips::ATOMIC_LOAD_MIN_I64_POSTRA:
818 IsMin = true;
819 break;
820 case Mips::ATOMIC_LOAD_UMAX_I32_POSTRA:
821 case Mips::ATOMIC_LOAD_UMAX_I64_POSTRA:
822 IsUnsigned = true;
823 [[fallthrough]];
824 case Mips::ATOMIC_LOAD_MAX_I32_POSTRA:
825 case Mips::ATOMIC_LOAD_MAX_I64_POSTRA:
826 IsMax = true;
827 break;
828 default:
829 llvm_unreachable("Unknown pseudo atomic!");
830 }
831
832 bool NoMovnInstr = (IsMin || IsMax) && !STI->hasMips4() && !STI->hasMips32();
833 const BasicBlock *LLVM_BB = BB.getBasicBlock();
834 MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
835 MachineBasicBlock *loop1MBB = nullptr;
836 MachineBasicBlock *loop2MBB = nullptr;
837 if (NoMovnInstr) {
838 loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
839 loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
840 }
841 MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
843 MF->insert(It, loopMBB);
844 if (NoMovnInstr) {
845 MF->insert(It, loop1MBB);
846 MF->insert(It, loop2MBB);
847 }
848 MF->insert(It, exitMBB);
849
850 exitMBB->splice(exitMBB->begin(), &BB, std::next(I), BB.end());
852
854 if (NoMovnInstr) {
855 loopMBB->addSuccessor(loop1MBB);
856 loopMBB->addSuccessor(loop2MBB);
857 } else {
858 loopMBB->addSuccessor(exitMBB);
859 loopMBB->addSuccessor(loopMBB);
860 }
861 loopMBB->normalizeSuccProbs();
862 if (NoMovnInstr) {
863 loop1MBB->addSuccessor(loop2MBB);
864 loop2MBB->addSuccessor(loopMBB);
865 loop2MBB->addSuccessor(exitMBB);
866 }
867
868 BuildMI(loopMBB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
869 assert((OldVal != Ptr) && "Clobbered the wrong ptr reg!");
870 assert((OldVal != Incr) && "Clobbered the wrong reg!");
871 if (IsMin || IsMax) {
872 // Remove trailing kill/dead register operands added by
873 // MachineInstr::addRegisterKilled. These are super-register markers that
874 // must correspond to one of the physical registers in operands 1-4.
875 // The kill/dead markers may also appear on preceding subregs.
876 const TargetRegisterInfo *TRI = STI->getRegisterInfo();
877 while (I->getNumOperands() > 5) {
878 auto &Op = I->getOperand(I->getNumOperands() - 1);
879 // Check if this register overlaps with any physical register in
880 // operands 1-4 that has kill/dead marker (i.e., it's a super-register
881 // marker for subregs).
882 bool HasOverlapWithKill = false;
883 for (unsigned i = 1; i <= 4; ++i) {
884 auto &RefOp = I->getOperand(i);
885 if (RefOp.isReg() && RefOp.getReg().isPhysical() &&
886 TRI->regsOverlap(Op.getReg(), RefOp.getReg()) &&
887 (RefOp.isKill() || RefOp.isDead())) {
888 HasOverlapWithKill = true;
889 break;
890 }
891 }
892 // Remove if HasOverlapWithKill is true or Op has kill/dead marker.
893 bool HasKillOrDead = Op.isReg() && Op.getReg().isPhysical() &&
894 (Op.isKill() || Op.isDead());
895 if (!HasOverlapWithKill && !HasKillOrDead)
896 break;
897 I->removeOperand(I->getNumOperands() - 1);
898 }
899
900 assert(I->getNumOperands() == 5 &&
901 "Atomics min|max|umin|umax use an additional register");
902 MCRegister Scratch2 = I->getOperand(4).getReg().asMCReg();
903
904 // On Mips64 result of slt is GPR32.
905 MCRegister Scratch2_32 =
906 (Size == 8) ? STI->getRegisterInfo()->getSubReg(Scratch2, Mips::sub_32)
907 : Scratch2;
908
909 unsigned SLTScratch2 = IsUnsigned ? SLTu : SLT;
910 unsigned SELIncr = IsMax ? SELNEZ : SELEQZ;
911 unsigned SELOldVal = IsMax ? SELEQZ : SELNEZ;
912 unsigned MOVIncr = IsMax ? MOVN : MOVZ;
913
914 // unsigned: sltu Scratch2, oldVal, Incr
915 // signed: slt Scratch2, oldVal, Incr
916 BuildMI(loopMBB, DL, TII->get(SLTScratch2), Scratch2_32)
917 .addReg(OldVal)
918 .addReg(Incr);
919
920 if (STI->hasMips64r6() || STI->hasMips32r6()) {
921 // max: seleqz Scratch, OldVal, Scratch2
922 // selnez Scratch2, Incr, Scratch2
923 // or Scratch, Scratch, Scratch2
924 // min: selnez Scratch, OldVal, Scratch2
925 // seleqz Scratch2, Incr, Scratch2
926 // or Scratch, Scratch, Scratch2
927 BuildMI(loopMBB, DL, TII->get(SELOldVal), Scratch)
928 .addReg(OldVal)
929 .addReg(Scratch2);
930 BuildMI(loopMBB, DL, TII->get(SELIncr), Scratch2)
931 .addReg(Incr)
932 .addReg(Scratch2);
933 BuildMI(loopMBB, DL, TII->get(OR), Scratch)
934 .addReg(Scratch)
935 .addReg(Scratch2);
936 } else if (STI->hasMips4() || STI->hasMips32()) {
937 // max: move Scratch, OldVal
938 // movn Scratch, Incr, Scratch2, Scratch
939 // min: move Scratch, OldVal
940 // movz Scratch, Incr, Scratch2, Scratch
941 BuildMI(loopMBB, DL, TII->get(OR), Scratch)
942 .addReg(OldVal)
943 .addReg(ZERO);
944 BuildMI(loopMBB, DL, TII->get(MOVIncr), Scratch)
945 .addReg(Incr)
946 .addReg(Scratch2)
947 .addReg(Scratch);
948 } else {
949 // if min:
950 // loopMBB: move Scratch, OldVal
951 // bne Scratch2_32, 0, loop2MBB
952 // loop1MBB: move Scratch, Incr
953 // loop2MBB: sc $2, 0($4)
954 // beqz $2, $BB0_1
955 // nop
956 //
957 // if max:
958 // loopMBB: move Scratch, Incr
959 // bne Scratch2_32, 0, loop2MBB
960 // loop1MBB: move Scratch, OldVal
961 // loop2MBB: sc $2, 0($4)
962 // beqz $2, $BB0_1
963 // nop
964 if (IsMin) {
965 BuildMI(loopMBB, DL, TII->get(OR), Scratch).addReg(OldVal).addReg(ZERO);
966 BuildMI(loop1MBB, DL, TII->get(OR), Scratch).addReg(Incr).addReg(ZERO);
967 } else {
968 BuildMI(loopMBB, DL, TII->get(OR), Scratch).addReg(Incr).addReg(ZERO);
969 BuildMI(loop1MBB, DL, TII->get(OR), Scratch)
970 .addReg(OldVal)
971 .addReg(ZERO);
972 }
973 // Scratch2_32 is always a GPR32, so use the 32-bit BNE even for the
974 // 64-bit atomics. loop1MBB is laid out directly after loopMBB, so fall
975 // through to it rather than using an unconditional jump.
976 BuildMI(loopMBB, DL, TII->get(Mips::BNE))
977 .addReg(Scratch2_32)
978 .addReg(Mips::R0)
979 .addMBB(loop2MBB);
980 }
981
982 } else if (Opcode) {
983 BuildMI(loopMBB, DL, TII->get(Opcode), Scratch).addReg(OldVal).addReg(Incr);
984 } else if (IsNand) {
985 assert(AND && NOR &&
986 "Unknown nand instruction for atomic pseudo expansion");
987 BuildMI(loopMBB, DL, TII->get(AND), Scratch).addReg(OldVal).addReg(Incr);
988 BuildMI(loopMBB, DL, TII->get(NOR), Scratch).addReg(ZERO).addReg(Scratch);
989 } else {
990 assert(IsOr && OR && "Unknown instruction for atomic pseudo expansion!");
991 (void)IsOr;
992 BuildMI(loopMBB, DL, TII->get(OR), Scratch).addReg(Incr).addReg(ZERO);
993 }
994
995 if (NoMovnInstr) {
996 BuildMI(loop2MBB, DL, TII->get(SC), Scratch)
997 .addReg(Scratch)
998 .addReg(Ptr)
999 .addImm(0);
1000 BuildMI(loop2MBB, DL, TII->get(BEQ))
1001 .addReg(Scratch)
1002 .addReg(ZERO)
1003 .addMBB(loopMBB);
1004 } else {
1005 BuildMI(loopMBB, DL, TII->get(SC), Scratch)
1006 .addReg(Scratch)
1007 .addReg(Ptr)
1008 .addImm(0);
1009 BuildMI(loopMBB, DL, TII->get(BEQ))
1010 .addReg(Scratch)
1011 .addReg(ZERO)
1012 .addMBB(loopMBB);
1013 }
1014
1015 NMBBI = BB.end();
1016 I->eraseFromParent();
1017
1019 computeAndAddLiveIns(LiveRegs, *loopMBB);
1020 if (loop1MBB) {
1021 assert(loop2MBB && "should have 2 loop blocks");
1022 computeAndAddLiveIns(LiveRegs, *loop1MBB);
1023 computeAndAddLiveIns(LiveRegs, *loop2MBB);
1024 }
1025 computeAndAddLiveIns(LiveRegs, *exitMBB);
1026
1027 return true;
1028}
1029
1032 const MipsSubtarget *STI, const MipsInstrInfo *TII) {
1033
1034 bool Modified = false;
1035
1036 switch (MBBI->getOpcode()) {
1037 case Mips::ATOMIC_CMP_SWAP_I32_POSTRA:
1038 case Mips::ATOMIC_CMP_SWAP_I64_POSTRA:
1039 return expandAtomicCmpSwap(MBB, MBBI, NMBB, STI, TII);
1040 case Mips::ATOMIC_CMP_SWAP_I8_POSTRA:
1041 case Mips::ATOMIC_CMP_SWAP_I16_POSTRA:
1042 return expandAtomicCmpSwapSubword(MBB, MBBI, NMBB, STI, TII);
1043 case Mips::ATOMIC_SWAP_I8_POSTRA:
1044 case Mips::ATOMIC_SWAP_I16_POSTRA:
1045 case Mips::ATOMIC_LOAD_NAND_I8_POSTRA:
1046 case Mips::ATOMIC_LOAD_NAND_I16_POSTRA:
1047 case Mips::ATOMIC_LOAD_ADD_I8_POSTRA:
1048 case Mips::ATOMIC_LOAD_ADD_I16_POSTRA:
1049 case Mips::ATOMIC_LOAD_SUB_I8_POSTRA:
1050 case Mips::ATOMIC_LOAD_SUB_I16_POSTRA:
1051 case Mips::ATOMIC_LOAD_AND_I8_POSTRA:
1052 case Mips::ATOMIC_LOAD_AND_I16_POSTRA:
1053 case Mips::ATOMIC_LOAD_OR_I8_POSTRA:
1054 case Mips::ATOMIC_LOAD_OR_I16_POSTRA:
1055 case Mips::ATOMIC_LOAD_XOR_I8_POSTRA:
1056 case Mips::ATOMIC_LOAD_XOR_I16_POSTRA:
1057 case Mips::ATOMIC_LOAD_MIN_I8_POSTRA:
1058 case Mips::ATOMIC_LOAD_MIN_I16_POSTRA:
1059 case Mips::ATOMIC_LOAD_MAX_I8_POSTRA:
1060 case Mips::ATOMIC_LOAD_MAX_I16_POSTRA:
1061 case Mips::ATOMIC_LOAD_UMIN_I8_POSTRA:
1062 case Mips::ATOMIC_LOAD_UMIN_I16_POSTRA:
1063 case Mips::ATOMIC_LOAD_UMAX_I8_POSTRA:
1064 case Mips::ATOMIC_LOAD_UMAX_I16_POSTRA:
1065 return expandAtomicBinOpSubword(MBB, MBBI, NMBB, STI, TII);
1066 case Mips::ATOMIC_LOAD_ADD_I32_POSTRA:
1067 case Mips::ATOMIC_LOAD_SUB_I32_POSTRA:
1068 case Mips::ATOMIC_LOAD_AND_I32_POSTRA:
1069 case Mips::ATOMIC_LOAD_OR_I32_POSTRA:
1070 case Mips::ATOMIC_LOAD_XOR_I32_POSTRA:
1071 case Mips::ATOMIC_LOAD_NAND_I32_POSTRA:
1072 case Mips::ATOMIC_SWAP_I32_POSTRA:
1073 case Mips::ATOMIC_LOAD_MIN_I32_POSTRA:
1074 case Mips::ATOMIC_LOAD_MAX_I32_POSTRA:
1075 case Mips::ATOMIC_LOAD_UMIN_I32_POSTRA:
1076 case Mips::ATOMIC_LOAD_UMAX_I32_POSTRA:
1077 return expandAtomicBinOp(MBB, MBBI, NMBB, 4, STI, TII);
1078 case Mips::ATOMIC_LOAD_ADD_I64_POSTRA:
1079 case Mips::ATOMIC_LOAD_SUB_I64_POSTRA:
1080 case Mips::ATOMIC_LOAD_AND_I64_POSTRA:
1081 case Mips::ATOMIC_LOAD_OR_I64_POSTRA:
1082 case Mips::ATOMIC_LOAD_XOR_I64_POSTRA:
1083 case Mips::ATOMIC_LOAD_NAND_I64_POSTRA:
1084 case Mips::ATOMIC_SWAP_I64_POSTRA:
1085 case Mips::ATOMIC_LOAD_MIN_I64_POSTRA:
1086 case Mips::ATOMIC_LOAD_MAX_I64_POSTRA:
1087 case Mips::ATOMIC_LOAD_UMIN_I64_POSTRA:
1088 case Mips::ATOMIC_LOAD_UMAX_I64_POSTRA:
1089 return expandAtomicBinOp(MBB, MBBI, NMBB, 8, STI, TII);
1090 default:
1091 return Modified;
1092 }
1093}
1094
1096 const MipsInstrInfo *TII) {
1097 bool Modified = false;
1098
1099 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1100 while (MBBI != E) {
1101 MachineBasicBlock::iterator NMBBI = std::next(MBBI);
1102 Modified |= expandMI(MBB, MBBI, NMBBI, STI, TII);
1103 MBBI = NMBBI;
1104 }
1105
1106 return Modified;
1107}
1108
1110 const MipsSubtarget *STI = &MF.getSubtarget<MipsSubtarget>();
1111 const MipsInstrInfo *TII = STI->getInstrInfo();
1112
1113 bool Modified = false;
1114 for (MachineBasicBlock &MBB : MF)
1115 Modified |= expandMBB(MBB, STI, TII);
1116
1117 if (Modified)
1118 MF.RenumberBlocks();
1119
1120 return Modified;
1121}
1122
1123/// createMipsExpandPseudoPass - returns an instance of the pseudo instruction
1124/// expansion pass.
1126 return new MipsExpandPseudoLegacy();
1127}
1128
1129bool MipsExpandPseudoLegacy::runOnMachineFunction(MachineFunction &MF) {
1130 return expandPseudosInMF(MF);
1131}
1132
1133PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
@ ZERO
Special weight used for cases with exact zero probability.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
static bool expandPseudosInMF(MachineFunction &MF)
static bool expandAtomicBinOp(MachineBasicBlock &BB, MachineBasicBlock::iterator I, MachineBasicBlock::iterator &NMBBI, unsigned Size, const MipsSubtarget *STI, const MipsInstrInfo *TII)
static bool expandAtomicCmpSwap(MachineBasicBlock &BB, MachineBasicBlock::iterator I, MachineBasicBlock::iterator &NMBBI, const MipsSubtarget *STI, const MipsInstrInfo *TII)
static bool expandMBB(MachineBasicBlock &MBB, const MipsSubtarget *STI, const MipsInstrInfo *TII)
static bool expandAtomicCmpSwapSubword(MachineBasicBlock &BB, MachineBasicBlock::iterator I, MachineBasicBlock::iterator &NMBBI, const MipsSubtarget *STI, const MipsInstrInfo *TII)
static bool expandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, MachineBasicBlock::iterator &NMBB, const MipsSubtarget *STI, const MipsInstrInfo *TII)
static bool expandAtomicBinOpSubword(MachineBasicBlock &BB, MachineBasicBlock::iterator I, MachineBasicBlock::iterator &NMBBI, const MipsSubtarget *STI, const MipsInstrInfo *TII)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
static constexpr BranchProbability getOne()
A debug info location.
Definition DebugLoc.h:127
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
BasicBlockListType::iterator iterator
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
bool ArePtrs64bit() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
bool hasMips32r6() const
bool hasMips4() const
bool inMicroMipsMode() const
const MipsInstrInfo * getInstrInfo() const override
bool hasMips64r6() const
bool hasMips32() const
const MipsRegisterInfo * getRegisterInfo() const override
bool hasMips32r2() const
const MipsABIInfo & getABI() const
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
self_iterator getIterator()
Definition ilist_node.h:123
Pass manager infrastructure for declaring and invalidating analyses.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
@ Kill
The last use of a register.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
FunctionPass * createMipsExpandPseudoLegacyPass()
createMipsExpandPseudoPass - returns an instance of the pseudo instruction expansion pass.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
DWARFExpression::Operation Op
LLVM_ABI void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().