Temporarily Revert "Nuke the old JIT." as it's not quite ready to
[oota-llvm.git] / lib / Target / Sparc / DelaySlotFiller.cpp
index 271c630e39336edc21f38a33c84fb6e95f47e896..28369fd5c342f064f3a44744af7fadcd0e4a9c36 100644 (file)
 // NOP is placed.
 //===----------------------------------------------------------------------===//
 
-#define DEBUG_TYPE "delay-slot-filler"
 #include "Sparc.h"
+#include "SparcSubtarget.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetMachine.h"
@@ -25,6 +26,8 @@
 
 using namespace llvm;
 
+#define DEBUG_TYPE "delay-slot-filler"
+
 STATISTIC(FilledSlots, "Number of delay slots filled");
 
 static cl::opt<bool> DisableDelaySlotFiller(
@@ -39,28 +42,32 @@ namespace {
     /// layout, etc.
     ///
     TargetMachine &TM;
-    const TargetInstrInfo *TII;
+    const SparcSubtarget *Subtarget;
 
     static char ID;
-    Filler(TargetMachine &tm) 
-      : MachineFunctionPass(ID), TM(tm), TII(tm.getInstrInfo()) { }
+    Filler(TargetMachine &tm)
+      : MachineFunctionPass(ID), TM(tm),
+        Subtarget(&TM.getSubtarget<SparcSubtarget>()) {
+    }
 
-    virtual const char *getPassName() const {
+    const char *getPassName() const override {
       return "SPARC Delay Slot Filler";
     }
 
     bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
-    bool runOnMachineFunction(MachineFunction &F) {
+    bool runOnMachineFunction(MachineFunction &F) override {
       bool Changed = false;
+
+      // This pass invalidates liveness information when it reorders
+      // instructions to fill delay slot.
+      F.getRegInfo().invalidateLiveness();
+
       for (MachineFunction::iterator FI = F.begin(), FE = F.end();
            FI != FE; ++FI)
         Changed |= runOnMachineBasicBlock(*FI);
       return Changed;
     }
 
-    bool isDelayFiller(MachineBasicBlock &MBB,
-                       MachineBasicBlock::iterator candidate);
-
     void insertCallDefsUses(MachineBasicBlock::iterator MI,
                             SmallSet<unsigned, 32>& RegDefs,
                             SmallSet<unsigned, 32>& RegUses);
@@ -103,11 +110,13 @@ FunctionPass *llvm::createSparcDelaySlotFillerPass(TargetMachine &tm) {
 bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
   bool Changed = false;
 
+  const TargetInstrInfo *TII = TM.getSubtargetImpl()->getInstrInfo();
+
   for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ) {
     MachineBasicBlock::iterator MI = I;
     ++I;
 
-    //If MI is restore, try combining it with previous inst.
+    // If MI is restore, try combining it with previous inst.
     if (!DisableDelaySlotFiller &&
         (MI->getOpcode() == SP::RESTORErr
          || MI->getOpcode() == SP::RESTOREri)) {
@@ -115,7 +124,15 @@ bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
       continue;
     }
 
-    //If MI has no delay slot, skip
+    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;
+    }
+
+    // If MI has no delay slot, skip.
     if (!MI->hasDelaySlot())
       continue;
 
@@ -135,10 +152,14 @@ bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
     unsigned structSize = 0;
     if (needsUnimp(MI, structSize)) {
       MachineBasicBlock::iterator J = MI;
-      ++J; //skip the delay filler.
+      ++J; // skip the delay filler.
       assert (J != MBB.end() && "MI needs a delay instruction.");
-      BuildMI(MBB, ++J, I->getDebugLoc(),
+      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;
@@ -156,7 +177,7 @@ Filler::findDelayInstr(MachineBasicBlock &MBB,
   if (slot == MBB.begin())
     return MBB.end();
 
-  if (slot->getOpcode() == SP::RET)
+  if (slot->getOpcode() == SP::RET || slot->getOpcode() == SP::TLS_CALL)
     return MBB.end();
 
   if (slot->getOpcode() == SP::RETL) {
@@ -165,13 +186,13 @@ Filler::findDelayInstr(MachineBasicBlock &MBB,
 
     if (J->getOpcode() == SP::RESTORErr
         || J->getOpcode() == SP::RESTOREri) {
-      //change retl to ret
-      slot->setDesc(TII->get(SP::RET));
+      // change retl to ret.
+      slot->setDesc(TM.getSubtargetImpl()->getInstrInfo()->get(SP::RET));
       return J;
     }
   }
 
-  //Call's delay filler can def some of call's uses.
+  // Call's delay filler can def some of call's uses.
   if (slot->isCall())
     insertCallDefsUses(slot, RegDefs, RegUses);
   else
@@ -191,12 +212,8 @@ Filler::findDelayInstr(MachineBasicBlock &MBB,
     if (I->isDebugValue())
       continue;
 
-
-    if (I->hasUnmodeledSideEffects()
-        || I->isInlineAsm()
-        || I->isLabel()
-        || I->hasDelaySlot()
-        || isDelayFiller(MBB, I))
+    if (I->hasUnmodeledSideEffects() || I->isInlineAsm() || I->isPosition() ||
+        I->hasDelaySlot() || I->isBundledWithSucc())
       break;
 
     if (delayHasHazard(I, sawLoad, sawStore, RegDefs, RegUses)) {
@@ -241,12 +258,12 @@ bool Filler::delayHasHazard(MachineBasicBlock::iterator candidate,
     unsigned Reg = MO.getReg();
 
     if (MO.isDef()) {
-      //check whether Reg is defined or used before delay slot.
+      // check whether Reg is defined or used before delay slot.
       if (IsRegInSet(RegDefs, Reg) || IsRegInSet(RegUses, Reg))
         return true;
     }
     if (MO.isUse()) {
-      //check whether Reg is defined before delay slot.
+      // check whether Reg is defined before delay slot.
       if (IsRegInSet(RegDefs, Reg))
         return true;
     }
@@ -259,31 +276,31 @@ void Filler::insertCallDefsUses(MachineBasicBlock::iterator MI,
                                 SmallSet<unsigned, 32>& RegDefs,
                                 SmallSet<unsigned, 32>& RegUses)
 {
-  //Call defines o7, which is visible to the instruction in delay slot.
+  // Call defines o7, which is visible to the instruction in delay slot.
   RegDefs.insert(SP::O7);
 
   switch(MI->getOpcode()) {
   default: llvm_unreachable("Unknown opcode.");
   case SP::CALL: break;
-  case SP::JMPLrr:
-  case SP::JMPLri:
+  case SP::CALLrr:
+  case SP::CALLri:
     assert(MI->getNumOperands() >= 2);
     const MachineOperand &Reg = MI->getOperand(0);
-    assert(Reg.isReg() && "JMPL first operand is not a register.");
-    assert(Reg.isUse() && "JMPL first operand is not a use.");
+    assert(Reg.isReg() && "CALL first operand is not a register.");
+    assert(Reg.isUse() && "CALL first operand is not a use.");
     RegUses.insert(Reg.getReg());
 
     const MachineOperand &RegOrImm = MI->getOperand(1);
     if (RegOrImm.isImm())
         break;
-    assert(RegOrImm.isReg() && "JMPLrr second operand is not a register.");
-    assert(RegOrImm.isUse() && "JMPLrr second operand is not a use.");
+    assert(RegOrImm.isReg() && "CALLrr second operand is not a register.");
+    assert(RegOrImm.isUse() && "CALLrr second operand is not a use.");
     RegUses.insert(RegOrImm.getReg());
     break;
   }
 }
 
-//Insert Defs and Uses of MI into the sets RegDefs and RegUses.
+// 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)
@@ -299,8 +316,8 @@ void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
     if (MO.isDef())
       RegDefs.insert(Reg);
     if (MO.isUse()) {
-      //Implicit register uses of retl are return values and
-      //retl does not use them.
+      // 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);
@@ -308,29 +325,18 @@ void Filler::insertDefsUses(MachineBasicBlock::iterator MI,
   }
 }
 
-//returns true if the Reg or its alias is in the RegSet.
+// 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, TM.getRegisterInfo(), true);
+  for (MCRegAliasIterator AI(Reg, TM.getSubtargetImpl()->getRegisterInfo(),
+                             true);
        AI.isValid(); ++AI)
     if (RegSet.count(*AI))
       return true;
   return false;
 }
 
-// return true if the candidate is a delay filler.
-bool Filler::isDelayFiller(MachineBasicBlock &MBB,
-                           MachineBasicBlock::iterator candidate)
-{
-  if (candidate == MBB.begin())
-    return false;
-  if (candidate->getOpcode() == SP::UNIMP)
-    return true;
-  --candidate;
-  return candidate->hasDelaySlot();
-}
-
 bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
 {
   if (!I->isCall())
@@ -340,8 +346,9 @@ bool Filler::needsUnimp(MachineBasicBlock::iterator I, unsigned &StructSize)
   switch (I->getOpcode()) {
   default: llvm_unreachable("Unknown call opcode.");
   case SP::CALL: structSizeOpNum = 1; break;
-  case SP::JMPLrr:
-  case SP::JMPLri: structSizeOpNum = 2; break;
+  case SP::CALLrr:
+  case SP::CALLri: structSizeOpNum = 2; break;
+  case SP::TLS_CALL: return false;
   }
 
   const MachineOperand &MO = I->getOperand(structSizeOpNum);
@@ -355,24 +362,24 @@ static bool combineRestoreADD(MachineBasicBlock::iterator RestoreMI,
                               MachineBasicBlock::iterator AddMI,
                               const TargetInstrInfo *TII)
 {
-  //Before:  add  <op0>, <op1>, %i[0-7]
-  //         restore %g0, %g0, %i[0-7]
+  // Before:  add  <op0>, <op1>, %i[0-7]
+  //          restore %g0, %g0, %i[0-7]
   //
-  //After :  restore <op0>, <op1>, %o[0-7]
+  // After :  restore <op0>, <op1>, %o[0-7]
 
   unsigned reg = AddMI->getOperand(0).getReg();
   if (reg < SP::I0 || reg > SP::I7)
     return false;
 
-  //Erase RESTORE
+  // Erase RESTORE.
   RestoreMI->eraseFromParent();
 
-  //Change ADD to RESTORE
+  // Change ADD to RESTORE.
   AddMI->setDesc(TII->get((AddMI->getOpcode() == SP::ADDrr)
                           ? SP::RESTORErr
                           : SP::RESTOREri));
 
-  //map the destination register
+  // Map the destination register.
   AddMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
 
   return true;
@@ -382,17 +389,17 @@ static bool combineRestoreOR(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,
+  // 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]
+  // After :  restore <op0>, <op1>, %o[0-7]
 
   unsigned reg = OrMI->getOperand(0).getReg();
   if (reg < SP::I0 || reg > SP::I7)
     return false;
 
-  //check whether it is a copy
+  // check whether it is a copy.
   if (OrMI->getOpcode() == SP::ORrr
       && OrMI->getOperand(1).getReg() != SP::G0
       && OrMI->getOperand(2).getReg() != SP::G0)
@@ -403,15 +410,15 @@ static bool combineRestoreOR(MachineBasicBlock::iterator RestoreMI,
       && (!OrMI->getOperand(2).isImm() || OrMI->getOperand(2).getImm() != 0))
     return false;
 
-  //Erase RESTORE
+  // Erase RESTORE.
   RestoreMI->eraseFromParent();
 
-  //Change OR to RESTORE
+  // Change OR to RESTORE.
   OrMI->setDesc(TII->get((OrMI->getOpcode() == SP::ORrr)
                          ? SP::RESTORErr
                          : SP::RESTOREri));
 
-  //map the destination register
+  // Map the destination register.
   OrMI->getOperand(0).setReg(reg - SP::I0 + SP::O0);
 
   return true;
@@ -421,10 +428,10 @@ static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
                                  MachineBasicBlock::iterator SetHiMI,
                                  const TargetInstrInfo *TII)
 {
-  //Before:  sethi imm3, %i[0-7]
-  //         restore %g0, %g0, %g0
+  // Before:  sethi imm3, %i[0-7]
+  //          restore %g0, %g0, %g0
   //
-  //After :  restore %g0, (imm3<<10), %o[0-7]
+  // After :  restore %g0, (imm3<<10), %o[0-7]
 
   unsigned reg = SetHiMI->getOperand(0).getReg();
   if (reg < SP::I0 || reg > SP::I7)
@@ -435,11 +442,11 @@ static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
 
   int64_t imm = SetHiMI->getOperand(1).getImm();
 
-  //is it a 3 bit immediate?
+  // Is it a 3 bit immediate?
   if (!isInt<3>(imm))
     return false;
 
-  //make it a 13 bit immediate
+  // Make it a 13 bit immediate.
   imm = (imm << 10) & 0x1FFF;
 
   assert(RestoreMI->getOpcode() == SP::RESTORErr);
@@ -451,7 +458,7 @@ static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
   RestoreMI->getOperand(2).ChangeToImmediate(imm);
 
 
-  //Erase the original SETHI
+  // Erase the original SETHI.
   SetHiMI->eraseFromParent();
 
   return true;
@@ -460,22 +467,24 @@ static bool combineRestoreSETHIi(MachineBasicBlock::iterator RestoreMI,
 bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
                                         MachineBasicBlock::iterator MBBI)
 {
-  //No previous instruction
+  // No previous instruction.
   if (MBBI == MBB.begin())
     return false;
 
-  //asssert that MBBI is "restore %g0, %g0, %g0"
+  // 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 = MBBI; --PrevInst;
+  MachineBasicBlock::iterator PrevInst = std::prev(MBBI);
 
-  //Cannot combine with a delay filler
-  if (isDelayFiller(MBB, PrevInst))
+  // It cannot be combined with a bundled instruction.
+  if (PrevInst->isBundledWithSucc())
     return false;
 
+  const TargetInstrInfo *TII = TM.getSubtargetImpl()->getInstrInfo();
+
   switch (PrevInst->getOpcode()) {
   default: break;
   case SP::ADDrr:
@@ -484,6 +493,6 @@ bool Filler::tryCombineRestoreWithPrevInst(MachineBasicBlock &MBB,
   case SP::ORri:  return combineRestoreOR(MBBI, PrevInst, TII); break;
   case SP::SETHIi: return combineRestoreSETHIi(MBBI, PrevInst, TII); break;
   }
-  //Cannot combine with the previous instruction
+  // It cannot combine with the previous instruction.
   return false;
 }