switch the SUnit pred/succ sets from being std::sets to being smallvectors.
[oota-llvm.git] / lib / CodeGen / SelectionDAG / ScheduleDAG.cpp
index e05d9d71031e27ec4ab6ed310aa5671956c76c34..d33e1ecef7ea8a33ac4abb7d06e8c3827d43773f 100644 (file)
@@ -18,6 +18,7 @@
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/SSARegMap.h"
+#include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetLowering.h"
@@ -54,11 +55,16 @@ void ScheduleDAG::BuildSchedUnits() {
     
     // Scan up, adding flagged preds to FlaggedNodes.
     SDNode *N = NI;
-    while (N->getNumOperands() &&
-           N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
-      N = N->getOperand(N->getNumOperands()-1).Val;
-      NodeSUnit->FlaggedNodes.push_back(N);
-      SUnitMap[N] = NodeSUnit;
+    if (N->getNumOperands() &&
+        N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) {
+      do {
+        N = N->getOperand(N->getNumOperands()-1).Val;
+        NodeSUnit->FlaggedNodes.push_back(N);
+        SUnitMap[N] = NodeSUnit;
+      } while (N->getNumOperands() &&
+               N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag);
+      std::reverse(NodeSUnit->FlaggedNodes.begin(),
+                   NodeSUnit->FlaggedNodes.end());
     }
     
     // Scan down, adding this node and any flagged succs to FlaggedNodes if they
@@ -78,7 +84,7 @@ void ScheduleDAG::BuildSchedUnits() {
           N = *UI;
           break;
         }
-          if (!HasFlagUse) break;
+      if (!HasFlagUse) break;
     }
     
     // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node.
@@ -144,7 +150,7 @@ void ScheduleDAG::BuildSchedUnits() {
         assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!");
         bool isChain = OpVT == MVT::Other;
         
-        if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) {
+        if (SU->addPred(OpSU, isChain)) {
           if (!isChain) {
             SU->NumPreds++;
             SU->NumPredsLeft++;
@@ -152,7 +158,7 @@ void ScheduleDAG::BuildSchedUnits() {
             SU->NumChainPredsLeft++;
           }
         }
-        if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) {
+        if (OpSU->addSucc(SU, isChain)) {
           if (!isChain) {
             OpSU->NumSuccs++;
             OpSU->NumSuccsLeft++;
@@ -170,31 +176,35 @@ void ScheduleDAG::BuildSchedUnits() {
   return;
 }
 
-static void CalculateDepths(SUnit *SU, unsigned Depth) {
-  if (Depth > SU->Depth) SU->Depth = Depth;
-  for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(),
-         E = SU->Succs.end(); I != E; ++I)
-    CalculateDepths(I->first, Depth+1);
+static void CalculateDepths(SUnit &SU, unsigned Depth) {
+  if (SU.Depth == 0 || Depth > SU.Depth) {
+    SU.Depth = Depth;
+    for (SUnit::succ_iterator I = SU.Succs.begin(), E = SU.Succs.end();
+         I != E; ++I)
+      CalculateDepths(*I->first, Depth+1);
+  }
 }
 
 void ScheduleDAG::CalculateDepths() {
   SUnit *Entry = SUnitMap[DAG.getEntryNode().Val];
-  ::CalculateDepths(Entry, 0U);
+  ::CalculateDepths(*Entry, 0U);
   for (unsigned i = 0, e = SUnits.size(); i != e; ++i)
     if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) {
-      ::CalculateDepths(&SUnits[i], 0U);
+      ::CalculateDepths(SUnits[i], 0U);
     }
 }
 
-static void CalculateHeights(SUnit *SU, unsigned Height) {
-  if (Height > SU->Height) SU->Height = Height;
-  for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(),
-         E = SU->Preds.end(); I != E; ++I)
-    CalculateHeights(I->first, Height+1);
+static void CalculateHeights(SUnit &SU, unsigned Height) {
+  if (SU.Height == 0 || Height > SU.Height) {
+    SU.Height = Height;
+    for (SUnit::pred_iterator I = SU.Preds.begin(), E = SU.Preds.end();
+         I != E; ++I)
+      CalculateHeights(*I->first, Height+1);
+  }
 }
 void ScheduleDAG::CalculateHeights() {
   SUnit *Root = SUnitMap[DAG.getRoot().Val];
-  ::CalculateHeights(Root, 0U);
+  ::CalculateHeights(*Root, 0U);
 }
 
 /// CountResults - The results of target nodes have register or immediate
@@ -221,19 +231,35 @@ static unsigned CountOperands(SDNode *Node) {
   return N;
 }
 
-static unsigned CreateVirtualRegisters(MachineInstr *MI,
+static const TargetRegisterClass *getInstrOperandRegClass(
+        const MRegisterInfo *MRI, 
+        const TargetInstrInfo *TII,
+        const TargetInstrDescriptor *II,
+        unsigned Op) {
+  if (Op >= II->numOperands) {
+    assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction");
+    return NULL;
+  }
+  const TargetOperandInfo &toi = II->OpInfo[Op];
+  return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS)
+         ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass);
+}
+
+static unsigned CreateVirtualRegisters(const MRegisterInfo *MRI,
+                                       MachineInstr *MI,
                                        unsigned NumResults,
                                        SSARegMap *RegMap,
+                                       const TargetInstrInfo *TII,
                                        const TargetInstrDescriptor &II) {
   // Create the result registers for this node and add the result regs to
   // the machine instruction.
-  const TargetOperandInfo *OpInfo = II.OpInfo;
-  unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass);
+  unsigned ResultReg =
+    RegMap->createVirtualRegister(getInstrOperandRegClass(MRI, TII, &II, 0));
   MI->addRegOperand(ResultReg, MachineOperand::Def);
   for (unsigned i = 1; i != NumResults; ++i) {
-    assert(OpInfo[i].RegClass && "Isn't a register operand!");
-    MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass),
-                      MachineOperand::Def);
+    const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i);
+    assert(RC && "Isn't a register operand!");
+    MI->addRegOperand(RegMap->createVirtualRegister(RC), MachineOperand::Def);
   }
   return ResultReg;
 }
@@ -270,9 +296,10 @@ void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
     // Verify that it is right.
     assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
     if (II) {
-      assert(II->OpInfo[IIOpNum].RegClass &&
-             "Don't have operand info for this instruction!");
-      assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
+      const TargetRegisterClass *RC =
+                          getInstrOperandRegClass(MRI, TII, II, IIOpNum);
+      assert(RC && "Don't have operand info for this instruction!");
+      assert(RegMap->getRegClass(VReg) == RC &&
              "Register class of operand and regclass of use don't agree!");
     }
   } else if (ConstantSDNode *C =
@@ -327,9 +354,10 @@ void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op,
     // Verify that it is right.
     assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?");
     if (II) {
-      assert(II->OpInfo[IIOpNum].RegClass &&
-             "Don't have operand info for this instruction!");
-      assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass &&
+      const TargetRegisterClass *RC =
+                            getInstrOperandRegClass(MRI, TII, II, IIOpNum);
+      assert(RC && "Don't have operand info for this instruction!");
+      assert(RegMap->getRegClass(VReg) == RC &&
              "Register class of operand and regclass of use don't agree!");
     }
   }
@@ -352,7 +380,8 @@ void ScheduleDAG::EmitNode(SDNode *Node,
     unsigned NodeOperands = CountOperands(Node);
     unsigned NumMIOperands = NodeOperands + NumResults;
 #ifndef NDEBUG
-    assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&&
+    assert((unsigned(II.numOperands) == NumMIOperands ||
+            (II.Flags & M_VARIABLE_OPS)) &&
            "#operands for dag node doesn't match .td file!"); 
 #endif
 
@@ -382,7 +411,7 @@ void ScheduleDAG::EmitNode(SDNode *Node,
     
     // Otherwise, create new virtual registers.
     if (NumResults && VRBase == 0)
-      VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II);
+      VRBase = CreateVirtualRegisters(MRI, MI, NumResults, RegMap, TII, II);
     
     // Emit all of the actual operands of this instruction, adding them to the
     // instruction as appropriate.
@@ -396,7 +425,10 @@ void ScheduleDAG::EmitNode(SDNode *Node,
         DEBUG(std::cerr << "Sched: COMMUTING FAILED!\n");
       else {
         DEBUG(std::cerr << "Sched: COMMUTED TO: " << *NewMI);
-        MI = NewMI;
+        if (MI != NewMI) {
+          delete MI;
+          MI = NewMI;
+        }
       }
     }
 
@@ -411,7 +443,9 @@ void ScheduleDAG::EmitNode(SDNode *Node,
   } else {
     switch (Node->getOpcode()) {
     default:
-      Node->dump(); 
+#ifndef NDEBUG
+      Node->dump();
+#endif
       assert(0 && "This target-independent node should have been selected!");
     case ISD::EntryToken: // fall thru
     case ISD::TokenFactor:
@@ -534,6 +568,20 @@ void ScheduleDAG::EmitNoop() {
 
 /// EmitSchedule - Emit the machine code in scheduled order.
 void ScheduleDAG::EmitSchedule() {
+  // If this is the first basic block in the function, and if it has live ins
+  // that need to be copied into vregs, emit the copies into the top of the
+  // block before emitting the code for the block.
+  MachineFunction &MF = DAG.getMachineFunction();
+  if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) {
+    for (MachineFunction::livein_iterator LI = MF.livein_begin(),
+         E = MF.livein_end(); LI != E; ++LI)
+      if (LI->second)
+        MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second,
+                          LI->first, RegMap->getRegClass(LI->second));
+  }
+  
+  
+  // Finally, emit the code for all of the scheduled instructions.
   std::map<SDNode*, unsigned> VRBaseMap;
   for (unsigned i = 0, e = Sequence.size(); i != e; i++) {
     if (SUnit *SU = Sequence[i]) {
@@ -598,24 +646,24 @@ void SUnit::dumpAll(const SelectionDAG *G) const {
 
   if (Preds.size() != 0) {
     std::cerr << "  Predecessors:\n";
-    for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(),
-           E = Preds.end(); I != E; ++I) {
+    for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end();
+         I != E; ++I) {
       if (I->second)
-        std::cerr << "   ch  ";
+        std::cerr << "   ch  #";
       else
-        std::cerr << "   val ";
-      I->first->dump(G);
+        std::cerr << "   val #";
+      std::cerr << I->first << "\n";
     }
   }
   if (Succs.size() != 0) {
     std::cerr << "  Successors:\n";
-    for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(),
-           E = Succs.end(); I != E; ++I) {
+    for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end();
+         I != E; ++I) {
       if (I->second)
-        std::cerr << "   ch  ";
+        std::cerr << "   ch  #";
       else
-        std::cerr << "   val ";
-      I->first->dump(G);
+        std::cerr << "   val #";
+      std::cerr << I->first << "\n";
     }
   }
   std::cerr << "\n";