[llvm-commits] CVS: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp

Brian Gaeke gaeke at cs.uiuc.edu
Fri Apr 9 15:11:06 PDT 2004


Changes in directory reopt/lib/LightWtProfiling:

UnpackTraceFunction.cpp updated: 1.55 -> 1.56

---
Log message:

Add lots more guts to rewriteEpilog.
Factor the regSet loop out into its own function; call it from rewriteEpilog.
Don't call getStaticStackSize twice in debug mode.
Update comments and debug messages.


---
Diffs of the changes:  (+101 -50)

Index: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp
diff -u reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.55 reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.56
--- reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.55	Fri Apr  9 13:15:52 2004
+++ reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp	Fri Apr  9 15:10:25 2004
@@ -141,8 +141,8 @@
     // Emit store instruction from register TempReg to stack loc. Target
     TRI.cpReg2MemMI (mvec, TempReg, FramePtrReg, Target.Placement, RegType);
   }
-  // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we were
-  // provided.
+  // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we are
+  // working on.
   for (std::vector<MachineInstr *>::iterator i = mvec.begin (),
          e = mvec.end (); i != e; ++i)
     B.push_back (*i);
@@ -184,6 +184,22 @@
 static AllocInfo getValueAllocStateFromModule (Function *F, Value *V);
 static AllocInfo getValueAllocStateFromGlobal (Function *F, Value *V);
 
+/// Return the set of registers used in this function. Registers are
+/// represented by their 'unified register numbers' as used in the SPARCv9
+/// back-end.
+///
+static std::set<unsigned> getRegsUsedInFunction (MachineFunction &MF) {
+  std::set<unsigned> regSet;
+  for (MachineFunction::iterator fi = MF.begin (), fe = MF.end ();
+    fi != fe; ++fi)
+    for (MachineBasicBlock::iterator bi = fi->begin (), be = fi->end ();
+      bi != be; ++bi)
+      for (unsigned oi = 0, oe = bi->getNumOperands (); oi != oe; ++oi)
+        if (bi->getOperand (oi).isDef ())
+          regSet.insert (bi->getOperand (oi).getReg ());
+  return regSet;
+}
+
 void UnpackTraceFunction::rewriteProlog (MachineFunction &MF,
                                          MachineBasicBlock &E) {
   const TargetRegInfo &TRI = TM->getRegInfo ();
@@ -203,25 +219,18 @@
     MachineOperand::UseAndDef));
 
   // 1. Initialize regSet with the set of registers used in this function.
-  std::set<unsigned> regSet;
-  for (MachineFunction::iterator fi = MF.begin (), fe = MF.end ();
-    fi != fe; ++fi)
-    for (MachineBasicBlock::iterator bi = fi->begin (), be = fi->end ();
-      bi != be; ++bi)
-      for (unsigned oi = 0, oe = bi->getNumOperands (); oi != oe; ++oi)
-        if (bi->getOperand (oi).isDef ())
-          regSet.insert (bi->getOperand (oi).getReg ());
+  std::set<unsigned> regSet = getRegsUsedInFunction (MF);
 
-  DEBUG(std::cerr << "In rewriteProlog()...\n"
-                  << " static stack size is " << getStaticStackSize (MF)
-                  << "\n"
-                  << " reg set (size " << regSet.size () << ") contains: (");
+  unsigned stackSize = getStaticStackSize (MF);
+  DEBUG(std::cerr << "rewriteProlog: Static stack size is "
+                  << stackSize << "\n"
+                  << "rewriteProlog: Reg set (size " << regSet.size ()
+                  << ") contains: (");
   DEBUG(for (std::set<unsigned>::iterator i = regSet.begin (),
              e = regSet.end (); i != e; ++i) { std::cerr << *i << " "; });
-  DEBUG(std::cerr << " )\n\n");
+  DEBUG(std::cerr << " )\n");
 
   // 2. Get some stack space: (Stack Frame Size + Space for Regs).
-  unsigned stackSize = getStaticStackSize (MF);
   int Size = (stackSize + 32 * 8);
   E.push_back (BuildMI (V9::ADDi, 3).addMReg (sp).addSImm (-Size).addMReg (sp,
     MachineOperand::Def));
@@ -234,12 +243,12 @@
     mvec.clear ();
     unsigned R = *i;
     unsigned RegType = TRI.getRegType (R);
-    DEBUG (std::cerr << "Saving reg#" << R << ", type = " << RegType << ", "
-                     << "Class = " << TRI.getRegClassIDOfRegType(RegType)
-                     << "\n");
+    DEBUG (std::cerr << "rewriteProlog: Saving reg#" << R << ", type = "
+                     << RegType << ", " << "Class = "
+                     << TRI.getRegClassIDOfRegType(RegType) << "\n");
     TRI.cpReg2MemMI (mvec, R, sp, stackSize + R * 8, RegType);
-    // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we were
-    // provided.
+    // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we are
+    // working on.
     for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
            ve = mvec.end (); vi != ve; ++vi)
       E.push_back (*vi);
@@ -250,29 +259,26 @@
   Function *TraceF = TF->TraceFn, *MatrixF = TF->MatrixFn;
   LiveVariableSet &Si = TF->LiveInSet, &So = TF->LiveOutSet;
   std::vector<CopyInfo> EntryCopies;
-  DEBUG(std::cerr << "UnpackTraceFunction: Modifying entry BB\n");
   for (LiveVariableSet::iterator SI = Si.begin (), SE = Si.end (); SI != SE;
        ++SI) {
     Value *V = *SI;
     DEBUG(std::cerr
-          << "UnpackTraceFunction: Looking for alloc state for value: ";
+          << "rewriteProlog: Getting alloc state for live-in value ";
           WriteAsOperand (std::cerr, V, true, true, MatrixF->getParent ());
           std::cerr << "\n");
-    AllocInfo Source = getValueAllocStateFromModule (MatrixF, V);
-    DEBUG(std::cerr << "UnpackTraceFunction: Source = " << Source << "\n");
-    AllocInfo Target =
-      getValueAllocStateFromGlobal (TraceF, TF->getCorrespondingValue (V));
-    DEBUG(std::cerr << "UnpackTraceFunction: Target = " << Target << "\n");
+    AllocInfo Source = getValueAllocStateFromModule (MatrixF, V),
+              Target = getValueAllocStateFromGlobal (TraceF,
+                         TF->getCorrespondingValue (V));
+    DEBUG(std::cerr << "rewriteProlog: Source = " << Source << "\n"
+                    << "rewriteProlog: Target = " << Target << "\n");
     if (Source != Target)
       EntryCopies.push_back (CopyInfo (Source, Target, &E, V->getType ()));
   }
-  DEBUG(std::cerr << "UnpackTraceFunction: Inserting copy MachineInstrs:\n");
   for (std::vector<CopyInfo>::iterator i = EntryCopies.begin (),
          e = EntryCopies.end (); i != e; ++i) {
-    DEBUG(std::cerr << "UnpackTraceFunction: " << *i << "\n");
+    DEBUG(std::cerr << "rewriteProlog: Inserting copy " << *i << "\n");
     insertCopyMachineInstrs (i->Src, i->Targ, *i->Blk, i->Ty);
   }
-  DEBUG(std::cerr << "-------------\n\n");
 }
 
 /// Data structures describing the register allocator state
@@ -424,18 +430,23 @@
 
 void UnpackTraceFunction::rewriteEpilog (MachineFunction &MF,
                                          MachineBasicBlock &MBB) {
+  const TargetRegInfo &TRI = TM->getRegInfo ();
+  static const unsigned 
+    fp = SparcV9IntRegClass::i6,
+    sp = SparcV9IntRegClass::o6,
+    g0 = SparcV9IntRegClass::g0,
+    g1 = SparcV9IntRegClass::g1;
   Function *TraceF = TF->TraceFn, *MatrixF = TF->MatrixFn;
-  LiveVariableSet &Si = TF->LiveInSet, &So = TF->LiveOutSet;
-  // Let ReturnAddress be the address in memory of the compiled
-  // code for the MachineBasicBlock in MatrixF that RI would have
-  // returned to. Find it by using MappingInfo.
-  const BasicBlock *RBB = MBB.getBasicBlock ();
-  assert ((TF->ReturnBlockForTraceExit.find (RBB) !=
-		   TF->ReturnBlockForTraceExit.end ())
-		  && "Can't find matrix fn BB address to return to from trace");
-  uint64_t ReturnAddress = 
-	getBasicBlockInfo(TF->ReturnBlockForTraceExit[RBB]).first;
-  MBB.clear (); // Empty it out first.
+  LiveVariableSet &So = TF->LiveOutSet;
+
+  // UTF epilog: start out by clearing everything out of the exit basic block
+  // (FIXME: may not work once we start doing optimizations!!! We will probably
+  // have to use a separate MBB)
+  MBB.clear ();
+
+  // Restore old FP.
+  // FIXME
+
   // Insert copies from each live-out variable's reg. in the trace
   // to its reg. in the matrix function.
   for (LiveVariableSet::iterator SI = So.begin (), SE = So.end ();
@@ -447,6 +458,48 @@
 	if (Source != Target)
 	  insertCopyMachineInstrs (Source, Target, MBB, V->getType ());
   }
+
+  // Get the set of registers used in this function which we saved in
+  // rewriteProlog, earlier.
+  std::set<unsigned> regSet = getRegsUsedInFunction (MF);
+
+  unsigned stackSize = getStaticStackSize (MF);
+  DEBUG(std::cerr << "rewriteEpilog: Static stack size is "
+                  << stackSize << "\n"
+                  << "rewriteEpilog: Reg set (size " << regSet.size ()
+                  << ") contains: (");
+  DEBUG(for (std::set<unsigned>::iterator i = regSet.begin (),
+             e = regSet.end (); i != e; ++i) { std::cerr << *i << " "; });
+  DEBUG(std::cerr << " )\n");
+
+  // Restore all used registers from stack except SP.
+  std::vector<MachineInstr *> mvec;
+  int RegType;
+  for (std::set<unsigned>::iterator i = regSet.begin (), e = regSet.end ();
+    i != e; ++i) {
+    mvec.clear ();
+    unsigned R = *i;
+    unsigned RegType = TRI.getRegType (R);
+    DEBUG (std::cerr << "rewriteEpilog: Reloading reg#" << R << ", type = "
+                     << RegType << ", " << "Class = "
+                     << TRI.getRegClassIDOfRegType(RegType) << "\n");
+    TRI.cpMem2RegMI (mvec, sp, stackSize + R * 8, R, RegType);
+    // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we are
+    // working on.
+    for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
+           ve = mvec.end (); vi != ve; ++vi)
+      MBB.push_back (*vi);
+  }
+
+  // Let ReturnAddress be the address in memory of the compiled
+  // code for the MachineBasicBlock in MatrixF that RI would have
+  // returned to. Find it by using mapping info.
+  const BasicBlock *RBB = MBB.getBasicBlock ();
+  assert ((TF->ReturnBlockForTraceExit.find (RBB) !=
+		   TF->ReturnBlockForTraceExit.end ())
+		  && "Can't find matrix fn BB address to return to from trace");
+  uint64_t ReturnAddress = 
+	getBasicBlockInfo(TF->ReturnBlockForTraceExit[RBB]).first;
   // Insert a branch back to the return BasicBlock of the matrix fn.
   insertBranchMachineInstrs (ReturnAddress, MBB);
 }
@@ -468,23 +521,21 @@
   if (MF.getFunction () != TF->TraceFn)
     return false;
 
-  DEBUG(std::cerr << "In UnpackTraceFunction for "
+  DEBUG(std::cerr << "UnpackTraceFunction: unpacking "
           << MF.getFunction()->getName() << "()\n");
 
-  Function *TraceF = TF->TraceFn, *MatrixF = TF->MatrixFn;
-  LiveVariableSet &Si = TF->LiveInSet, &So = TF->LiveOutSet;
+  // Rewrite function prolog, found in the entry MachineBasicBlock of MF
+  rewriteProlog (MF, MF.front ());
 
-  // Modify ENTRY MachineBasicBlock of MF
-  MachineBasicBlock &E = MF.front (); // E = Entry MBB of MF
-  rewriteProlog (MF, E);
-
-  // Modify EXIT MachineBasicBlocks of MF
+  // Rewrite function epilogs, found in every exit MachineBasicBlock of MF
   for (MachineFunction::iterator I = MF.begin (), E = MF.end (); I != E; ++I) {
     MachineBasicBlock &MBB = *I;
     if (const MachineInstr *RI = containsReturnInstr (MBB))
       rewriteEpilog (MF, MBB);
   }
 
+  DEBUG(std::cerr << "UnpackTraceFunction: done unpacking "
+          << MF.getFunction()->getName() << "()\n");
   return true; // MachineFunction was modified
 }
 





More information about the llvm-commits mailing list