llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-backend-sparc Author: Aiden Grossman (boomanaiden154) <details> <summary>Changes</summary> Standard NewPM pass porting. --- Patch is 32.80 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/237618.diff 6 Files Affected: - (modified) llvm/lib/Target/Sparc/DelaySlotFiller.cpp (+351-364) - (modified) llvm/lib/Target/Sparc/Sparc.h (+16-1) - (modified) llvm/lib/Target/Sparc/SparcCodeGenPassBuilder.cpp (+1-1) - (modified) llvm/lib/Target/Sparc/SparcPassRegistry.def (+1) - (modified) llvm/lib/Target/Sparc/SparcTargetMachine.cpp (+1-1) - (modified) llvm/test/CodeGen/SPARC/newpm-pipeline.ll (+1) ``````````diff diff --git a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp index adf1583f53784..3207adc3b1f7a 100644 --- a/llvm/lib/Target/Sparc/DelaySlotFiller.cpp +++ b/llvm/lib/Target/Sparc/DelaySlotFiller.cpp @@ -15,11 +15,15 @@ #include "SparcSubtarget.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/Statistic.h" +#include "llvm/CodeGen/MachineFunctionAnalysisManager.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstrBuilder.h" +#include "llvm/CodeGen/MachinePassManager.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/CodeGen/TargetRegisterInfo.h" +#include "llvm/IR/Analysis.h" +#include "llvm/Pass.h" #include "llvm/Target/TargetMachine.h" using namespace llvm; @@ -29,142 +33,202 @@ using namespace llvm; STATISTIC(FilledSlots, "Number of delay slots filled"); namespace { - struct Filler : public MachineFunctionPass { - const SparcSubtarget *Subtarget = nullptr; - bool FillSlots = false; +struct SparcDelaySlotFillerLegacy : public MachineFunctionPass { + static char ID; + SparcDelaySlotFillerLegacy() : MachineFunctionPass(ID) {} - static char ID; - Filler() : MachineFunctionPass(ID) {} + StringRef getPassName() const override { return "SPARC Delay Slot Filler"; } - StringRef getPassName() const override { return "SPARC Delay Slot Filler"; } + bool runOnMachineFunction(MachineFunction &MF) override; - bool runOnMachineBasicBlock(MachineBasicBlock &MBB); - bool runOnMachineFunction(MachineFunction &F) override { - bool Changed = false; - Subtarget = &F.getSubtarget<SparcSubtarget>(); - FillSlots = !Subtarget->getCLOpts().disable_sparc_delay_filler && - F.getTarget().getOptLevel() != CodeGenOptLevel::None; + MachineFunctionProperties getRequiredProperties() const override { + return MachineFunctionProperties().setNoVRegs(); + } - // This pass invalidates liveness information when it reorders - // instructions to fill delay slot. - F.getRegInfo().invalidateLiveness(); + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesCFG(); + MachineFunctionPass::getAnalysisUsage(AU); + } +}; +char SparcDelaySlotFillerLegacy::ID = 0; +} // end of anonymous namespace - for (MachineBasicBlock &MBB : F) - Changed |= runOnMachineBasicBlock(MBB); - return Changed; - } +/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay +/// slots in Sparc MachineFunctions +/// +FunctionPass *llvm::createSparcDelaySlotFillerLegacyPass() { + return new SparcDelaySlotFillerLegacy; +} - MachineFunctionProperties getRequiredProperties() const override { - return MachineFunctionProperties().setNoVRegs(); - } +static bool combineRestoreADD(MachineBasicBlock &MBB, + MachineBasicBlock::iterator RestoreMI, + MachineBasicBlock::iterator AddMI, + const TargetInstrInfo *TII) { + // Before: add <op0>, <op1>, %i[0-7] + // restore %g0, %g0, %i[0-7] + // + // After : restore <op0>, <op1>, %o[0-7] + + const TargetRegisterInfo *TRI = &TII->getRegisterInfo(); + Register reg = AddMI->getOperand(0).getReg(); + if (reg < SP::I0 || reg > SP::I7) + return false; + + // Check whether it uses %o7 as its source and the corresponding branch + // instruction is a call. + MachineBasicBlock::iterator LastInst = MBB.getFirstTerminator(); + bool IsCall = LastInst != MBB.end() && LastInst->isCall(); + + if (IsCall && AddMI->getOpcode() == SP::ADDrr && + AddMI->readsRegister(SP::O7, TRI)) + return false; + + if (IsCall && AddMI->getOpcode() == SP::ADDri && + AddMI->readsRegister(SP::O7, TRI)) + return false; - void insertCallDefsUses(MachineBasicBlock::iterator MI, - SmallSet<unsigned, 32>& RegDefs, - SmallSet<unsigned, 32>& RegUses); + // Erase RESTORE. + RestoreMI->eraseFromParent(); - void insertDefsUses(MachineBasicBlock::iterator MI, - SmallSet<unsigned, 32>& RegDefs, - SmallSet<unsigned, 32>& RegUses); + // Change ADD to RESTORE. + AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr) ? SP::RESTORErr + : SP::RESTOREri)); - bool IsRegInSet(SmallSet<unsigned, 32>& RegSet, - unsigned Reg); + // Map the destination register. + AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0); - bool delayHasHazard(MachineBasicBlock::iterator candidate, - bool &sawLoad, bool &sawStore, - SmallSet<unsigned, 32> &RegDefs, - SmallSet<unsigned, 32> &RegUses); + return true; +} - MachineBasicBlock::iterator - findRequiredDelayInstr(MachineBasicBlock &MBB, - MachineBasicBlock::iterator slot); +static bool combineRestoreOR(MachineBasicBlock &MBB, + MachineBasicBlock::iterator RestoreMI, + MachineBasicBlock::iterator OrMI, + const TargetInstrInfo *TII) { + // Before: or <op0>, <op1>, %i[0-7] + // restore %g0, %g0, %i[0-7] + // and <op0> or <op1> is zero, + // + // After : restore <op0>, <op1>, %o[0-7] - MachineBasicBlock::iterator - findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot); + const TargetRegisterInfo *TRI = &TII->getRegisterInfo(); + Register reg = OrMI->getOperand(0).getReg(); + if (reg < SP::I0 || reg > SP::I7) + return false; - bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB, - MachineBasicBlock::iterator MBBI); + // check whether it is a copy. + if (OrMI->getOpcode() == SP::ORrr && OrMI->getOperand(1).getReg() != SP::G0 && + OrMI->getOperand(2).getReg() != SP::G0) + return false; - }; - char Filler::ID = 0; -} // end of anonymous namespace + if (OrMI->getOpcode() == SP::ORri && OrMI->getOperand(1).getReg() != SP::G0 && + (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0)) + return false; -/// createSparcDelaySlotFillerPass - Returns a pass that fills in delay -/// slots in Sparc MachineFunctions -/// -FunctionPass *llvm::createSparcDelaySlotFillerPass() { - return new Filler; + // Check whether it uses %o7 as its source and the corresponding branch + // instruction is a call. + MachineBasicBlock::iterator LastInst = MBB.getFirstTerminator(); + bool IsCall = LastInst != MBB.end() && LastInst->isCall(); + + if (IsCall && OrMI->getOpcode() == SP::ORrr && + OrMI->readsRegister(SP::O7, TRI)) + return false; + + // Erase RESTORE. + RestoreMI->eraseFromParent(); + + // Change OR to RESTORE. + OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr) ? SP::RESTORErr + : SP::RESTOREri)); + + // Map the destination register. + OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0); + + return true; } +static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI, + MachineBasicBlock::iterator SetHiMI, + const TargetInstrInfo *TII) { + // Before: sethi imm3, %i[0-7] + // restore %g0, %g0, %g0 + // + // After : restore %g0, (imm3<<10), %o[0-7] -/// runOnMachineBasicBlock - Fill in delay slots for the given basic block. -/// We assume there is only one delay slot per delayed instruction. -/// -bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) { - bool Changed = false; - Subtarget = &MBB.getParent()->getSubtarget<SparcSubtarget>(); - const SparcInstrInfo *TII = Subtarget->getInstrInfo(); + Register reg = SetHiMI->getOperand(0).getReg(); + if (reg < SP::I0 || reg > SP::I7) + return false; - for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) { - MachineBasicBlock::iterator MI = I; - ++I; + if (!SetHiMI->getOperand(1).isImm()) + return false; - // If MI is restore, try combining it with previous inst. - if (FillSlots && (MI->getOpcode() == SP::RESTORErr || - MI->getOpcode() == SP::RESTOREri)) { - Changed |= tryCombineRestoreWithPrevInst(MBB, MI); - continue; - } + int64_t imm = SetHiMI->getOperand(1).getImm(); - // TODO: If we ever want to support v7, this needs to be extended - // to cover all floating point operations. - if (!Subtarget->isV9() && - (MI->getOpcode() == SP::FCMPS || MI->getOpcode() == SP::FCMPD - || MI->getOpcode() == SP::FCMPQ)) { - BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP)); - Changed = true; - continue; - } + // Is it a 3 bit immediate? + if (!isInt<3>(imm)) + return false; - // If MI has no delay slot, skip. - if (!MI->hasDelaySlot()) - continue; + // Make it a 13 bit immediate. + imm = (imm << 10) & 0x1FFF; - MachineBasicBlock::iterator D = findRequiredDelayInstr(MBB, MI); + assert(RestoreMI->getOpcode() == SP::RESTORErr); - if (D == MBB.end() && FillSlots) - D = findDelayInstr(MBB, MI); + RestoreMI->setDesc(TII->get(SP::RESTOREri)); - ++FilledSlots; - Changed = true; + RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0); + RestoreMI->getOperand(1).setReg(SP::G0); + RestoreMI->getOperand(2).ChangeToImmediate(imm); - if (D == MBB.end()) - BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP)); - else - MBB.splice(I, &MBB, D); + // Erase the original SETHI. + SetHiMI->eraseFromParent(); - unsigned structSize = 0; - if (TII->needsUnimp(*MI, structSize)) { - MachineBasicBlock::iterator J = MI; - ++J; // skip the delay filler. - assert (J != MBB.end() && "MI needs a delay instruction."); - BuildMI(MBB, ++J, MI->getDebugLoc(), - TII->get(SP::UNIMP)).addImm(structSize); - // Bundle the delay filler and unimp with the instruction. - MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), J); - } else { - MIBundleBuilder(MBB, MachineBasicBlock::iterator(MI), I); - } + return true; +} + +static bool tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB, + MachineBasicBlock::iterator MBBI, + const SparcSubtarget *Subtarget) { + // No previous instruction. + if (MBBI == MBB.begin()) + return false; + + // assert that MBBI is a "restore %g0, %g0, %g0". + assert(MBBI->getOpcode() == SP::RESTORErr && + MBBI->getOperand(0).getReg() == SP::G0 && + MBBI->getOperand(1).getReg() == SP::G0 && + MBBI->getOperand(2).getReg() == SP::G0); + + MachineBasicBlock::iterator PrevInst = std::prev(MBBI); + + // It cannot be combined with a bundled instruction. + if (PrevInst->isBundledWithSucc()) + return false; + + const TargetInstrInfo *TII = Subtarget->getInstrInfo(); + + switch (PrevInst->getOpcode()) { + default: + break; + case SP::ADDrr: + case SP::ADDri: + return combineRestoreADD(MBB, MBBI, PrevInst, TII); + case SP::ORrr: + case SP::ORri: + return combineRestoreOR(MBB, MBBI, PrevInst, TII); + case SP::SETHIi: + return combineRestoreSETHIi(MBBI, PrevInst, TII); + break; } - return Changed; + // It cannot combine with the previous instruction. + return false; } /// The restore before a tail call must run in its delay slot: `jmp %reg` /// reads a register of the current window, and `call` writes %o7. For the /// same reason a leaf `call` needs the copy of %o7 back from %g1 that /// SparcFrameLowering emits. -MachineBasicBlock::iterator -Filler::findRequiredDelayInstr(MachineBasicBlock &MBB, - MachineBasicBlock::iterator slot) { +static MachineBasicBlock::iterator +findRequiredDelayInstr(MachineBasicBlock &MBB, + MachineBasicBlock::iterator slot) { unsigned Opc = slot->getOpcode(); if ((Opc != SP::TAIL_CALL && Opc != SP::TAIL_CALLri) || slot == MBB.begin()) return MBB.end(); @@ -182,76 +246,79 @@ Filler::findRequiredDelayInstr(MachineBasicBlock &MBB, return MBB.end(); } -MachineBasicBlock::iterator -Filler::findDelayInstr(MachineBasicBlock &MBB, - MachineBasicBlock::iterator slot) -{ - SmallSet<unsigned, 32> RegDefs; - SmallSet<unsigned, 32> RegUses; - bool sawLoad = false; - bool sawStore = false; - - if (slot == MBB.begin()) - return MBB.end(); - - unsigned Opc = slot->getOpcode(); - - if (Opc == SP::RET || Opc == SP::TLS_CALL) - return MBB.end(); - - if (Opc == SP::RETL || Opc == SP::TAIL_CALL || Opc == SP::TAIL_CALLri) { - MachineBasicBlock::iterator J = slot; - --J; +static void insertCallDefsUses(MachineBasicBlock::iterator MI, + SmallSet<unsigned, 32> &RegDefs, + SmallSet<unsigned, 32> &RegUses) { + // Regular calls define o7, which is visible to the instruction in delay slot. + // On the other hand, tail calls preserve it. + switch (MI->getOpcode()) { + default: + llvm_unreachable("Unknown opcode."); + case SP::CALL: + RegDefs.insert(SP::O7); + break; + case SP::TAIL_CALL: + break; + case SP::CALLrr: + case SP::CALLri: + RegDefs.insert(SP::O7); + [[fallthrough]]; + case SP::TAIL_CALLri: + assert(MI->getNumOperands() >= 2); + const MachineOperand &Reg = MI->getOperand(0); + assert(Reg.isReg() && "CALL first operand is not a register."); + assert(Reg.isUse() && "CALL first operand is not a use."); + RegUses.insert(Reg.getReg()); - if (J->getOpcode() == SP::RESTORErr - || J->getOpcode() == SP::RESTOREri) { - // change retl to ret. - if (Opc == SP::RETL) - slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET)); - return J; - } + const MachineOperand &Operand1 = MI->getOperand(1); + if (Operand1.isImm() || Operand1.isGlobal()) + break; + assert(Operand1.isReg() && "CALLrr second operand is not a register."); + assert(Operand1.isUse() && "CALLrr second operand is not a use."); + RegUses.insert(Operand1.getReg()); + break; } +} - // Call's delay filler can def some of call's uses. - if (slot->isCall()) - insertCallDefsUses(slot, RegDefs, RegUses); - else - insertDefsUses(slot, RegDefs, RegUses); - - bool done = false; - - MachineBasicBlock::iterator I = slot; - - while (!done) { - done = (I == MBB.begin()); - - if (!done) - --I; - - // Skip meta instructions. - if (I->isMetaInstruction()) +// Insert Defs and Uses of MI into the sets RegDefs and RegUses. +static void insertDefsUses(MachineBasicBlock::iterator MI, + SmallSet<unsigned, 32> &RegDefs, + SmallSet<unsigned, 32> &RegUses, + const SparcSubtarget *Subtarget) { + for (const MachineOperand &MO : MI->operands()) { + if (!MO.isReg()) continue; - if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() || - I->hasDelaySlot() || I->isBundledWithSucc()) - break; - - if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) { - insertDefsUses(I, RegDefs, RegUses); + Register Reg = MO.getReg(); + if (Reg == 0 || Subtarget->getRegisterInfo()->isConstantPhysReg(Reg)) continue; + if (MO.isDef()) + RegDefs.insert(Reg); + if (MO.isUse()) { + // Implicit register uses of retl are return values and + // retl does not use them. + if (MO.isImplicit() && MI->getOpcode() == SP::RETL) + continue; + RegUses.insert(Reg); } - - return I; } - return MBB.end(); } -bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate, - bool &sawLoad, - bool &sawStore, - SmallSet<unsigned, 32> &RegDefs, - SmallSet<unsigned, 32> &RegUses) -{ +// returns true if the Reg or its alias is in the RegSet. +static bool IsRegInSet(SmallSet<unsigned, 32> &RegSet, unsigned Reg, + const SparcSubtarget *Subtarget) { + // Check Reg and all aliased Registers. + for (MCRegAliasIterator AI(Reg, Subtarget->getRegisterInfo(), true); + AI.isValid(); ++AI) + if (RegSet.count(*AI)) + return true; + return false; +} + +static bool delayHasHazard(MachineBasicBlock::iterator candidate, bool &sawLoad, + bool &sawStore, SmallSet<unsigned, 32> &RegDefs, + SmallSet<unsigned, 32> &RegUses, + const SparcSubtarget *Subtarget) { if (candidate->isImplicitDef() || candidate->isKill()) return true; @@ -278,12 +345,13 @@ bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate, if (MO.isDef()) { // check whether Reg is defined or used before delay slot. - if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg)) + if (IsRegInSet(RegDefs, Reg, Subtarget) || + IsRegInSet(RegUses, Reg, Subtarget)) return true; } if (MO.isUse()) { // check whether Reg is defined before delay slot. - if (IsRegInSet(RegDefs, Reg)) + if (IsRegInSet(RegDefs, Reg, Subtarget)) return true; } } @@ -320,237 +388,156 @@ bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate, return false; } +static MachineBasicBlock::iterator +findDelayInstr(MachineBasicBlock &MBB, MachineBasicBlock::iterator slot, + const SparcSubtarget *Subtarget) { + SmallSet<unsigned, 32> RegDefs; + SmallSet<unsigned, 32> RegUses; + bool sawLoad = false; + bool sawStore = false; -void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI, - SmallSet<unsigned, 32>& RegDefs, - SmallSet<unsigned, 32>& RegUses) -{ - // Regular calls define o7, which is visible to the instruction in delay slot. - // On the other hand, tail calls preserve it. - switch(MI->getOpcode()) { - default: llvm_unreachable("Unknown opcode."); - case SP::CALL: - RegDefs.insert(SP::O7); - break; - case SP::TAIL_CALL: - break; - case SP::CALLrr: - case SP::CALLri: - RegDefs.insert(SP::O7); - [[fallthrough]]; - case SP::TAIL_CALLri: - assert(MI->getNumOperands() >= 2); - const MachineOperand &Reg = MI->getOperand(0); - assert(Reg.isReg() && "CALL first operand is not a register."); - assert(Reg.isUse() && "CALL first operand is not a use."); - RegUses.insert(Reg.getReg()); + if (slot == MBB.begin()) + return MBB.end(); - const MachineOperand &Operand1 = MI->getOperand(1); - if (Operand1.isImm() || Operand1.isGlobal()) - break; - assert(Operand1.isReg() && "CALLrr second operand is not a register."); - assert(Operand1.isUse() && "CALLrr second operand is not a use."); - RegUses.insert(Operand1.getReg()); - break; - } -} + unsigned Opc = slot->getOpcode(); -// Insert Defs and Uses of MI into the sets RegDefs and RegUses. -void Filler::insertDefsUses(MachineBasicBlock::iterator MI, - SmallSet<unsigned, 32>& RegDefs, - SmallSet<unsigned, 32>& RegUses) -{ - for (const MachineOperand &MO : MI->operands()) { - if (!MO.isReg()) - continue; + if (Opc == SP::RET || Opc == SP::TLS_CALL) + return MBB.end(); - Register Reg = MO.getReg(); - if (Reg == 0 || Subtarget->getRegisterInfo()->isConstantPhysReg(Reg)) - continue; - if (MO.isDef()) - RegDefs.insert(Reg); - if (MO.isUse()) { - // Implicit register uses of retl are return values and - // retl does not use them. - if (MO.isImplicit() && MI->getOpcode() == SP::RETL) - continue; - RegUses.insert(Reg); + if (Opc == SP::RETL || Opc == SP::TAIL_CALL || Opc == SP::TAIL_CALLri) { + MachineBasicBlock::iterator J = slot; + --J; + + if (J->getOpcode() == SP::RESTORErr || J->getOpcode() == SP::RESTOREri) { + // change retl to ret. + if (Opc == SP::RETL) + slot->setDesc(Subtarget->getInstrInfo()->get(SP::RET)); + return J; } } -} - -// returns true if the Reg or its alias is in the RegSet. -bool Filler::IsRegInSet(SmallSet<unsigned, 32>& RegSet, unsigned Reg) -{... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/237618 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
