================
@@ -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)
-{
-  // 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 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]
+  // Call's delay filler can def some of call's uses.
+  if (slot->isCall())
+    insertCallDefsUses(slot, RegDefs, RegUses);
+  else
+    insertDefsUses(slot, RegDefs, RegUses, Subtarget);
 
-  const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
-  Register reg = AddMI->getOperand(0).getReg();
-  if (reg < SP::I0 || reg > SP::I7)
-    return false;
+  bool done = 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();
+  MachineBasicBlock::iterator I = slot;
 
-  if (IsCall && AddMI->getOpcode() == SP::ADDrr &&
-      AddMI->readsRegister(SP::O7, TRI))
-    return false;
+  while (!done) {
+    done = (I == MBB.begin());
 
-  if (IsCall && AddMI->getOpcode() == SP::ADDri &&
-      AddMI->readsRegister(SP::O7, TRI))
-    return false;
+    if (!done)
+      --I;
 
-  // Erase RESTORE.
-  RestoreMI->eraseFromParent();
+    // Skip meta instructions.
+    if (I->isMetaInstruction())
+      continue;
 
-  // Change ADD to RESTORE.
-  AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
-                          ? SP::RESTORErr
-                          : SP::RESTOREri));
+    if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
+        I->hasDelaySlot() || I->isBundledWithSucc())
+      break;
 
-  // Map the destination register.
-  AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
+    if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses, Subtarget)) {
+      insertDefsUses(I, RegDefs, RegUses, Subtarget);
+      continue;
+    }
 
-  return true;
+    return I;
+  }
+  return MBB.end();
 }
 
-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]
+/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
+/// We assume there is only one delay slot per delayed instruction.
+///
+static bool runOnMachineBasicBlock(MachineBasicBlock &MBB) {
+  bool Changed = false;
+  const SparcSubtarget *Subtarget =
+      &MBB.getParent()->getSubtarget<SparcSubtarget>();
+  bool FillSlots =
+      !Subtarget->getCLOpts().disable_sparc_delay_filler &&
+      MBB.getParent()->getTarget().getOptLevel() != CodeGenOptLevel::None;
+  const SparcInstrInfo *TII = Subtarget->getInstrInfo();
 
-  const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
-  Register reg = OrMI->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;
 
-  // 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;
+    // If MI is restore, try combining it with previous inst.
+    if (FillSlots && (MI->getOpcode() == SP::RESTORErr ||
+                      MI->getOpcode() == SP::RESTOREri)) {
+      Changed |= tryCombineRestoreWithPrevInst(MBB, MI, Subtarget);
+      continue;
+    }
 
-  if (OrMI->getOpcode() == SP::ORri
-      && OrMI->getOperand(1).getReg() != SP::G0
-      && (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
-    return false;
+    // 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;
+    }
 
-  // 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 MI has no delay slot, skip.
+    if (!MI->hasDelaySlot())
+      continue;
 
-  if (IsCall && OrMI->getOpcode() == SP::ORrr &&
-      OrMI->readsRegister(SP::O7, TRI))
-    return false;
+    MachineBasicBlock::iterator D = findRequiredDelayInstr(MBB, MI);
 
-  // Erase RESTORE.
-  RestoreMI->eraseFromParent();
+    if (D == MBB.end() && FillSlots)
+      D = findDelayInstr(MBB, MI, Subtarget);
 
-  // Change OR to RESTORE.
-  OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
-                         ? SP::RESTORErr
-                         : SP::RESTOREri));
+    ++FilledSlots;
+    Changed = true;
 
-  // Map the destination register.
-  OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
+    if (D == MBB.end())
+      BuildMI(MBB, I, MI->getDebugLoc(), TII->get(SP::NOP));
+    else
+      MBB.splice(I, &MBB, D);
 
-  return true;
+    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 Changed;
 }
 
-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]
-
-  Register reg = SetHiMI->getOperand(0).getReg();
-  if (reg < SP::I0 || reg > SP::I7)
-    return false;
-
-  if (!SetHiMI->getOperand(1).isImm())
-    return false;
-
-  int64_t imm = SetHiMI->getOperand(1).getImm();
-
-  // Is it a 3 bit immediate?
-  if (!isInt<3>(imm))
-    return false;
-
-  // Make it a 13 bit immediate.
-  imm = (imm << 10) & 0x1FFF;
-
-  assert(RestoreMI->getOpcode() == SP::RESTORErr);
-
-  RestoreMI->setDesc(TII->get(SP::RESTOREri));
-
-  RestoreMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
-  RestoreMI->getOperand(1).setReg(SP::G0);
-  RestoreMI->getOperand(2).ChangeToImmediate(imm);
-
+static bool runDelaySlotFillerOnMachineFunction(MachineFunction &MF) {
+  bool Changed = false;
 
-  // Erase the original SETHI.
-  SetHiMI->eraseFromParent();
+  // This pass invalidates liveness information when it reorders
+  // instructions to fill delay slot.
+  MF.getRegInfo().invalidateLiveness();
----------------
arsenm wrote:

....what? This isn't how any of the liveness APIs work 

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

Reply via email to