25 #define RISCV_EXPAND_PSEUDO_NAME "RISCV pseudo instruction expansion pass" 60 unsigned FlagsHi,
unsigned SecondOpcode);
81 Modified |= expandMBB(MBB);
91 Modified |= expandMI(MBB, MBBI, NMBBI);
101 switch (MBBI->getOpcode()) {
102 case RISCV::PseudoAtomicLoadNand32:
105 case RISCV::PseudoAtomicLoadNand64:
108 case RISCV::PseudoMaskedAtomicSwap32:
111 case RISCV::PseudoMaskedAtomicLoadAdd32:
113 case RISCV::PseudoMaskedAtomicLoadSub32:
115 case RISCV::PseudoMaskedAtomicLoadNand32:
118 case RISCV::PseudoMaskedAtomicLoadMax32:
121 case RISCV::PseudoMaskedAtomicLoadMin32:
124 case RISCV::PseudoMaskedAtomicLoadUMax32:
127 case RISCV::PseudoMaskedAtomicLoadUMin32:
130 case RISCV::PseudoCmpXchg32:
131 return expandAtomicCmpXchg(MBB, MBBI,
false, 32, NextMBBI);
132 case RISCV::PseudoCmpXchg64:
133 return expandAtomicCmpXchg(MBB, MBBI,
false, 64, NextMBBI);
134 case RISCV::PseudoMaskedCmpXchg32:
135 return expandAtomicCmpXchg(MBB, MBBI,
true, 32, NextMBBI);
136 case RISCV::PseudoLLA:
137 return expandLoadLocalAddress(MBB, MBBI, NextMBBI);
138 case RISCV::PseudoLA:
139 return expandLoadAddress(MBB, MBBI, NextMBBI);
140 case RISCV::PseudoLA_TLS_IE:
141 return expandLoadTLSIEAddress(MBB, MBBI, NextMBBI);
142 case RISCV::PseudoLA_TLS_GD:
143 return expandLoadTLSGDAddress(MBB, MBBI, NextMBBI);
156 return RISCV::LR_W_AQ;
160 return RISCV::LR_W_AQ;
162 return RISCV::LR_W_AQ_RL;
175 return RISCV::SC_W_RL;
177 return RISCV::SC_W_RL;
179 return RISCV::SC_W_AQ_RL;
190 return RISCV::LR_D_AQ;
194 return RISCV::LR_D_AQ;
196 return RISCV::LR_D_AQ_RL;
209 return RISCV::SC_D_RL;
211 return RISCV::SC_D_RL;
213 return RISCV::SC_D_AQ_RL;
219 return getLRForRMW32(Ordering);
221 return getLRForRMW64(Ordering);
227 return getSCForRMW32(Ordering);
229 return getSCForRMW64(Ordering);
250 BuildMI(LoopMBB, DL, TII->get(getLRForRMW(Ordering, Width)), DestReg)
259 BuildMI(LoopMBB, DL, TII->get(RISCV::XORI), ScratchReg)
264 BuildMI(LoopMBB, DL, TII->get(getSCForRMW(Ordering, Width)), ScratchReg)
267 BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
277 assert(OldValReg != ScratchReg &&
"OldValReg and ScratchReg must be unique");
278 assert(OldValReg != MaskReg &&
"OldValReg and MaskReg must be unique");
279 assert(ScratchReg != MaskReg &&
"ScratchReg and MaskReg must be unique");
295 static void doMaskedAtomicBinOpExpansion(
299 assert(Width == 32 &&
"Should never need to expand masked 64-bit operations");
316 BuildMI(LoopMBB, DL, TII->get(getLRForRMW32(Ordering)), DestReg)
322 BuildMI(LoopMBB, DL, TII->get(RISCV::ADDI), ScratchReg)
340 BuildMI(LoopMBB, DL, TII->get(RISCV::XORI), ScratchReg)
346 insertMaskedMerge(TII, DL, LoopMBB, ScratchReg, DestReg, ScratchReg, MaskReg,
349 BuildMI(LoopMBB, DL, TII->get(getSCForRMW32(Ordering)), ScratchReg)
352 BuildMI(LoopMBB, DL, TII->get(RISCV::BNE))
358 bool RISCVExpandPseudo::expandAtomicBinOp(
381 doAtomicBinOpExpansion(TII, MI, DL, &MBB, LoopMBB, DoneMBB, BinOp, Width);
383 doMaskedAtomicBinOpExpansion(TII, MI, DL, &MBB, LoopMBB, DoneMBB, BinOp,
386 NextMBBI = MBB.
end();
387 MI.eraseFromParent();
399 BuildMI(MBB, DL, TII->get(RISCV::SLL), ValReg)
407 bool RISCVExpandPseudo::expandAtomicMinMaxOp(
411 assert(IsMasked ==
true &&
412 "Should only need to expand masked atomic max/min");
413 assert(Width == 32 &&
"Should never need to expand masked 64-bit operations");
425 MF->
insert(++LoopHeadMBB->getIterator(), LoopIfBodyMBB);
426 MF->
insert(++LoopIfBodyMBB->getIterator(), LoopTailMBB);
427 MF->
insert(++LoopTailMBB->getIterator(), DoneMBB);
430 LoopHeadMBB->addSuccessor(LoopIfBodyMBB);
431 LoopHeadMBB->addSuccessor(LoopTailMBB);
432 LoopIfBodyMBB->addSuccessor(LoopTailMBB);
433 LoopTailMBB->addSuccessor(LoopHeadMBB);
434 LoopTailMBB->addSuccessor(DoneMBB);
439 Register DestReg = MI.getOperand(0).getReg();
440 Register Scratch1Reg = MI.getOperand(1).getReg();
441 Register Scratch2Reg = MI.getOperand(2).getReg();
442 Register AddrReg = MI.getOperand(3).getReg();
443 Register IncrReg = MI.getOperand(4).getReg();
444 Register MaskReg = MI.getOperand(5).getReg();
447 static_cast<AtomicOrdering>(MI.getOperand(IsSigned ? 7 : 6).getImm());
456 BuildMI(LoopHeadMBB, DL, TII->get(getLRForRMW32(Ordering)), DestReg)
461 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::ADDI), Scratch1Reg)
469 insertSext(TII, DL, LoopHeadMBB, Scratch2Reg, MI.getOperand(6).getReg());
470 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::BGE))
477 insertSext(TII, DL, LoopHeadMBB, Scratch2Reg, MI.getOperand(6).getReg());
478 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::BGE))
485 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::BGEU))
491 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::BGEU))
502 insertMaskedMerge(TII, DL, LoopIfBodyMBB, Scratch1Reg, DestReg, IncrReg,
503 MaskReg, Scratch1Reg);
508 BuildMI(LoopTailMBB, DL, TII->get(getSCForRMW32(Ordering)), Scratch1Reg)
511 BuildMI(LoopTailMBB, DL, TII->get(RISCV::BNE))
516 NextMBBI = MBB.
end();
517 MI.eraseFromParent();
528 bool RISCVExpandPseudo::expandAtomicCmpXchg(
540 MF->
insert(++LoopHeadMBB->getIterator(), LoopTailMBB);
541 MF->
insert(++LoopTailMBB->getIterator(), DoneMBB);
544 LoopHeadMBB->addSuccessor(LoopTailMBB);
545 LoopHeadMBB->addSuccessor(DoneMBB);
546 LoopTailMBB->addSuccessor(DoneMBB);
547 LoopTailMBB->addSuccessor(LoopHeadMBB);
552 Register DestReg = MI.getOperand(0).getReg();
553 Register ScratchReg = MI.getOperand(1).getReg();
554 Register AddrReg = MI.getOperand(2).getReg();
555 Register CmpValReg = MI.getOperand(3).getReg();
556 Register NewValReg = MI.getOperand(4).getReg();
558 static_cast<AtomicOrdering>(MI.getOperand(IsMasked ? 6 : 5).getImm());
564 BuildMI(LoopHeadMBB, DL, TII->get(getLRForRMW(Ordering, Width)), DestReg)
566 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::BNE))
573 BuildMI(LoopTailMBB, DL, TII->get(getSCForRMW(Ordering, Width)), ScratchReg)
576 BuildMI(LoopTailMBB, DL, TII->get(RISCV::BNE))
585 Register MaskReg = MI.getOperand(5).getReg();
586 BuildMI(LoopHeadMBB, DL, TII->get(getLRForRMW(Ordering, Width)), DestReg)
591 BuildMI(LoopHeadMBB, DL, TII->get(RISCV::BNE))
602 insertMaskedMerge(TII, DL, LoopTailMBB, ScratchReg, DestReg, NewValReg,
603 MaskReg, ScratchReg);
604 BuildMI(LoopTailMBB, DL, TII->get(getSCForRMW(Ordering, Width)), ScratchReg)
607 BuildMI(LoopTailMBB, DL, TII->get(RISCV::BNE))
613 NextMBBI = MBB.
end();
614 MI.eraseFromParent();
624 bool RISCVExpandPseudo::expandAuipcInstPair(
627 unsigned SecondOpcode) {
643 BuildMI(NewMBB, DL, TII->get(RISCV::AUIPC), DestReg)
644 .addDisp(Symbol, 0, FlagsHi);
645 BuildMI(NewMBB, DL, TII->get(SecondOpcode), DestReg)
650 NewMBB->
splice(NewMBB->
end(), &MBB, std::next(MBBI), MBB.
end());
660 NextMBBI = MBB.
end();
665 bool RISCVExpandPseudo::expandLoadLocalAddress(
672 bool RISCVExpandPseudo::expandLoadAddress(
677 unsigned SecondOpcode;
681 SecondOpcode = STI.is64Bit() ?
RISCV::LD : RISCV::LW;
684 SecondOpcode = RISCV::ADDI;
687 return expandAuipcInstPair(MBB, MBBI, NextMBBI, FlagsHi, SecondOpcode);
690 bool RISCVExpandPseudo::expandLoadTLSIEAddress(
696 unsigned SecondOpcode = STI.is64Bit() ?
RISCV::LD : RISCV::LW;
701 bool RISCVExpandPseudo::expandLoadTLSGDAddress(
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
*p = old <signed v ? old : v
This class represents lattice values for constants.
static bool IsMasked(Instruction *I)
void initializeRISCVExpandPseudoPass(PassRegistry &)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
*p = old <unsigned v ? old : v
*p = old >unsigned v ? old : v
*p = old >signed v ? old : v
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
AtomicOrdering
Atomic ordering for LLVM's memory model.
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Simple integer binary arithmetic operators.
virtual const TargetInstrInfo * getInstrInfo() const
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
FunctionPass class - This class is used to implement most global optimizations.
self_iterator getIterator()
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
void setLabelMustBeEmitted()
Set this block to reflect that, regardless how we flow to it, we need its label be emitted...
MachineOperand class - Representation of each machine instruction operand.
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
INITIALIZE_PASS(RISCVExpandPseudo, "riscv-expand-pseudo", RISCV_EXPAND_PSEUDO_NAME, false, false) namespace llvm
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Bitwise operators - logical and, logical or, logical xor.
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 '...
A set of physical registers with utility functions to track liveness when walking backward/forward th...
bool isPositionIndependent() const
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
#define RISCV_EXPAND_PSEUDO_NAME
const LLVMTargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
FunctionPass * createRISCVExpandPseudoPass()
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Wrapper class representing virtual and physical registers.