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

Brian Gaeke gaeke at cs.uiuc.edu
Sun May 23 02:47:01 PDT 2004


Changes in directory reopt/lib/LightWtProfiling:

UnpackTraceFunction.cpp updated: 1.66 -> 1.67

---
Log message:

Move the calculation of stack sizes and register sets out of rewriteProlog(),
and save the answers in the UnpackTraceFunction object for later use in
rewriteEpilog().

Split regsUsed out into RegsToRestore and RegsToSave.  findRegsUsedInFunction
has been renamed to findRegsToRestore(). I haven't decided the fates of these
sets yet.

Refactor the calculation of stack offsets for saved registers in the trace
function out into stackOffsetForReg().

Do not try to save and restore the stack pointer.

Perform live-in value copies by loading them off the stack where they were
saved.  Do not batch live-in value copies into the EntryCopies vector.
Ignore insertCopyMachineInstrs for now because it is going to screw up
copies from the MatrixFn's stack to the TraceFn's stack.

Other minor edits, primarily concerning debug messages.


---
Diffs of the changes:  (+105 -85)

Index: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp
diff -u reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.66 reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.67
--- reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.66	Sat May 22 21:02:13 2004
+++ reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp	Sun May 23 02:46:26 2004
@@ -40,12 +40,24 @@
   TargetMachine *TM;
   TraceFunction *TF;
 
+  /// Static stack size of the MachineFunction. Filled in by rewriteProlog ()
+  /// by calling getStaticStackSize (), and then used again by rewriteEpilog ().
+  ///
+  unsigned StaticStackSize;
+
+  /// Total stack size of the MachineFunction, including space for regs
+  /// modified in TraceFn to be saved.
+  ///
+  int TotalStackSize;
+
   /// Registers clobbered in the trace. Filled in by findRegsUsedInFunction ().
   ///
-  std::set<unsigned> regsUsed;
+  std::set<unsigned> RegsToRestore;
+  std::set<unsigned> RegsToSave;
 
   unsigned getStaticStackSize (MachineFunction &MF);
-  void findRegsUsedInFunction (MachineFunction &MF);
+  unsigned stackOffsetForReg (unsigned R);
+  void findRegsToRestore (MachineFunction &MF);
   void insertCopyMachineInstrs (AllocInfo &Source, AllocInfo &Target,
                                 MachineBasicBlock &B, const Type *Ty);
   void insertBranchMachineInstrs (uint64_t Target, MachineBasicBlock &B);
@@ -190,13 +202,13 @@
 static AllocInfo getValueAllocStateFromGlobal (Function *F, Value *V);
 
 /// Fill in the set of registers used in this function, which is kept in
-/// 'regsUsed' in the UnpackTraceFunction Pass object, and is used by
+/// 'RegsToRestore' in the UnpackTraceFunction Pass object, and is used by
 /// rewriteProlog() and rewriteEpilog(). Registers are
 /// represented by their 'unified register numbers' as used in the SPARCv9
 /// back-end.
 ///
 void
-UnpackTraceFunction::findRegsUsedInFunction (MachineFunction &MF) {
+UnpackTraceFunction::findRegsToRestore (MachineFunction &MF) {
   const TargetRegInfo &TRI = TM->getRegInfo ();
   bool intCCRegSeen = false, floatCCRegSeen = false;
   for (MachineFunction::iterator fi = MF.begin (), fe = MF.end ();
@@ -212,8 +224,9 @@
           } else if (TRI.getRegType(regNo) == SparcV9RegInfo::FloatCCRegType
                   || TRI.getRegType(regNo) == SparcV9RegInfo::SpecialRegType) {
             floatCCRegSeen = true;
-          } else if (regNo != SparcV9::g0) { // Ignore defs of the zero register
-            regsUsed.insert (regNo);
+          } else if (regNo != SparcV9::g0 && regNo != SparcV9::o6 /* sp */) {
+            // Defs of certain registers are ignored
+            RegsToRestore.insert (regNo);
           }
         }
       }
@@ -223,13 +236,19 @@
   // If the intcc regs are used, then we only put %ccr in the
   // set, not the individual intcc regs.
   if (floatCCRegSeen)
-    regsUsed.insert (SparcV9::fsr);
+    RegsToRestore.insert (SparcV9::fsr);
   if (intCCRegSeen)
-    regsUsed.insert (SparcV9::ccr);
+    RegsToRestore.insert (SparcV9::ccr);
 
   // Always put fp in the set because it is restored unconditionally.
   static const unsigned fp = SparcV9::i6;
-  regsUsed.insert (fp);
+  RegsToRestore.insert (fp);
+
+  DEBUG(std::cerr << "findRegsToRestore: RegsToRestore (size " 
+        << RegsToRestore.size () << ") contains: (";
+        for (std::set<unsigned>::iterator i = RegsToRestore.begin (),
+             e = RegsToRestore.end (); i != e; ++i) { std::cerr << *i << " "; }
+        std::cerr << " )\n");
 }
 
 static std::pair<AllocInfo, AllocInfo>
@@ -238,6 +257,10 @@
     getValueAllocStateFromGlobal (TF->TraceFn, TF->getCorrespondingValue (V)));
 }
 
+unsigned UnpackTraceFunction::stackOffsetForReg (unsigned R) {
+  return (2047 + StaticStackSize + 176 + (R) * 8);
+}
+
 void UnpackTraceFunction::rewriteProlog (MachineFunction &MF,
                                          MachineBasicBlock &E) {
   const TargetRegInfo &TRI = TM->getRegInfo ();
@@ -253,25 +276,14 @@
   E.push_back (BuildMI (V9::ORr, 3).addMReg (sp).addZImm (0).addMReg (g1,
     MachineOperand::UseAndDef));
 
-  // 1. Initialize regsUsed with the set of registers used in this function.
-  findRegsUsedInFunction (MF);
-
-  unsigned stackSize = getStaticStackSize (MF);
-  DEBUG(std::cerr << "rewriteProlog: Static stack size is "
-                  << stackSize << "\n"
-                  << "rewriteProlog: Reg set (size " << regsUsed.size ()
-                  << ") contains: (");
-  DEBUG(for (std::set<unsigned>::iterator i = regsUsed.begin (),
-             e = regsUsed.end (); i != e; ++i) { std::cerr << *i << " "; });
-  DEBUG(std::cerr << " )\n");
-
-  // 2. Get some stack space: (Stack Frame Size + Slop + Space for Regs).
-  int Size = (stackSize + 176 + 104 * 8);
-  E.push_back (BuildMI (V9::ADDi, 3).addMReg (sp).addSImm (-Size).addMReg (sp,
-    MachineOperand::Def));
-
-  std::set<unsigned> regsToSave (regsUsed);
-  // Save live-in values in registers onto the stack
+  // 1. Emit ADD instruction to allocate the right amount of stack space.
+  E.push_back (BuildMI (V9::ADDi, 3).addMReg (sp).addSImm (-TotalStackSize)
+    .addMReg (sp, MachineOperand::Def));
+
+  // 2. Get the saved register allocator state for all live-in variables.
+  // We are going to need to save live-in values which reside in registers
+  // on the stack, so we need to dig their register numbers out now.
+  RegsToSave = RegsToRestore;
   LiveVariableSet &Si = TF->LiveInSet;
   std::map<Value *, std::pair<AllocInfo, AllocInfo> > AllocStates;
   for (LiveVariableSet::iterator SI = Si.begin (), SE = Si.end (); SI != SE;
@@ -285,60 +297,57 @@
       if (Source.AllocState == AllocInfo::Allocated) {
         DEBUG (std::cerr << "Need to save incoming live value in reg "
                << Source.Placement << " onto the stack\n");
-        regsToSave.insert (Source.Placement);
+        RegsToSave.insert (Source.Placement);
       }
   }
 
   // 3. Save used registers onto the stack.
   std::vector<MachineInstr *> mvec;
   int RegType;
-  for (std::set<unsigned>::iterator i = regsToSave.begin (),
-       e = regsToSave.end (); i != e; ++i) {
+  for (std::set<unsigned>::iterator i = RegsToSave.begin (),
+       e = RegsToSave.end (); i != e; ++i) {
     mvec.clear ();
     unsigned R = *i;
     unsigned RegType = TRI.getRegType (R);
-    static const char *RegTypeStrings[] = { "IntRegType",
-      "FPSingleRegType", "FPDoubleRegType", "IntCCRegType",
-      "FloatCCRegType", "SpecialRegType" };
+
+    static const char *RegTypeStrings[] = { "IntRegType", "FPSingleRegType",
+      "FPDoubleRegType", "IntCCRegType", "FloatCCRegType", "SpecialRegType" };
     static const char *RegClassStrings[] = { "IntRegClassID",
       "FloatRegClassID", "IntCCRegClassID", "FloatCCRegClassID",
       "SpecialRegClassID" };
     DEBUG (std::cerr << "rewriteProlog: Saving reg#" << R << ", type = "
-                     << RegType << " (" << RegTypeStrings[RegType]
-                     << "), Class = " << TRI.getRegClassIDOfRegType(RegType)
-                     << " ("
-                     << RegClassStrings[TRI.getRegClassIDOfRegType(RegType)]
-                     << ")\n");
-    TRI.cpReg2MemMI (mvec, R, sp, 2047 + stackSize + 176 + R * 8, RegType, g2);
-    // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we are
-    // working on.
+           << RegType << " (" << RegTypeStrings[RegType]
+           << "), ClassID = " << TRI.getRegClassIDOfRegType(RegType) << " ("
+           << RegClassStrings[TRI.getRegClassIDOfRegType(RegType)] << ")\n");
+    TRI.cpReg2MemMI (mvec, R, sp, stackOffsetForReg (R), RegType, g2);
     for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
-           ve = mvec.end (); vi != ve; ++vi) {
-      DEBUG (std::cerr << "rewriteProlog: Insns: " << **vi);
+           ve = mvec.end (); vi != ve; ++vi)
       E.push_back (*vi);
-    }
   }
 
-
-  // 4. Insert copies from each live-in variable's reg. in the matrix fn.
-  // to its reg. in the trace.
-  Function *MatrixF = TF->MatrixFn;
-  std::vector<CopyInfo> EntryCopies;
+  // 4. Insert a copy for each live-in variable to copy it from the stack
+  // to the register that was allocated for it in the TraceFn.
   for (LiveVariableSet::iterator SI = Si.begin (), SE = Si.end (); SI != SE;
        ++SI) {
     Value *V = *SI;
     std::pair<AllocInfo, AllocInfo> &ai = AllocStates[V];
     AllocInfo &Source = ai.first, &Target = ai.second;
-    DEBUG(std::cerr << "rewriteProlog: Source in MatrixFn = " << Source << "\n"
-                    << "rewriteProlog: Target in TraceFn = " << Target << "\n");
-    if (Source != Target)
-      EntryCopies.push_back (CopyInfo (Source, Target, &E, V->getType ()));
-  }
-
-  for (std::vector<CopyInfo>::iterator i = EntryCopies.begin (),
-         e = EntryCopies.end (); i != e; ++i) {
-    DEBUG(std::cerr << "rewriteProlog: Inserting copy " << *i << "\n");
-    insertCopyMachineInstrs (i->Src, i->Targ, *i->Blk, i->Ty);
+    if (Source != Target) {
+      assert (Target.AllocState == AllocInfo::Allocated
+              && "FIXME: can't do mem-->mem copy of live-ins yet");
+      // FIXME: The following should really be using insertCopyMachineInstrs or
+      // something like it.
+      mvec.clear ();
+      assert (TRI.getRegType (Target.Placement)
+              == TRI.getRegType (Source.Placement)
+              && "Live-in value changed reg type?!");
+      unsigned RegType = TRI.getRegType (Target.Placement);
+      TRI.cpMem2RegMI (mvec, sp, stackOffsetForReg (Source.Placement),
+                       Target.Placement, RegType, g2);
+      for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
+             ve = mvec.end (); vi != ve; ++vi)
+        E.push_back (*vi);
+    }
   }
 }
 
@@ -432,10 +441,10 @@
   // (InstructionKey, OperandKey, ...):
   for (unsigned i = 0; i < FAllocState->numTuples; ++i) {
 	OperandAllocState &T = FAllocState->tuples[i];
-	if (T.Instruction == InstructionKey && T.Operand == OperandKey)
-	  return AllocInfo (T.Instruction, T.Operand,
-						(AllocInfo::AllocStateTy) T.AllocState,
-						T.Placement);
+        if (T.Instruction == InstructionKey && T.Operand == OperandKey)
+          return AllocInfo (T.Instruction, T.Operand,
+                            (AllocInfo::AllocStateTy) T.AllocState,
+                            T.Placement);
   }
   // By this time we had better have found it, otherwise we are about to do bad
   // things.
@@ -493,8 +502,6 @@
   const TargetRegInfo &TRI = TM->getRegInfo ();
   static const unsigned fp = SparcV9::i6, sp = SparcV9::o6, g0 = SparcV9::g0,
     g1 = SparcV9::g1, g2 = SparcV9::g2;
-  Function *TraceF = TF->TraceFn, *MatrixF = TF->MatrixFn;
-  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
@@ -503,16 +510,17 @@
 
   // Restore old FP.
   std::vector<MachineInstr *> mvec;
-  unsigned stackSize = getStaticStackSize (MF);
-  TRI.cpMem2RegMI (mvec, sp, 2047 + stackSize + 176 + fp * 8, fp,
-                   TRI.getRegType(fp), g2);
+  TRI.cpMem2RegMI (mvec, sp, stackOffsetForReg (fp), fp, TRI.getRegType(fp),
+                   g2);
   for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
        ve = mvec.end (); vi != ve; ++vi)
     MBB.push_back (*vi);
-  regsUsed.erase (fp);
+  RegsToRestore.erase (fp);
 
   // Insert copies from each live-out variable's reg. in the trace
   // to its reg. in the matrix function.
+  Function *TraceF = TF->TraceFn, *MatrixF = TF->MatrixFn;
+  LiveVariableSet &So = TF->LiveOutSet;
   for (LiveVariableSet::iterator SI = So.begin (), SE = So.end ();
 	   SI != SE; ++SI) {
 	Value *V = *SI;
@@ -526,41 +534,38 @@
   // rewriteProlog, earlier, and restore all used registers from stack
   // except SP.
   int RegType;
-  for (std::set<unsigned>::iterator i = regsUsed.begin (), e = regsUsed.end ();
-    i != e; ++i) {
+  for (std::set<unsigned>::iterator i = RegsToSave.begin (),
+         e = RegsToSave.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, 2047 + stackSize + 176 + R * 8, R, RegType, g2);
-    // Add whatever the TargetRegInfo gave us to the MachineBasicBlock we are
-    // working on.
+           << RegType << ", " << "ClassID = "
+           << TRI.getRegClassIDOfRegType(RegType) << "\n");
+    TRI.cpMem2RegMI (mvec, sp, stackOffsetForReg (R), R, RegType, g2);
     for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
            ve = mvec.end (); vi != ve; ++vi)
       MBB.push_back (*vi);
   }
 
   // Restore stack pointer.
-  int Size = (stackSize + 176 + 104 * 8);
-  MBB.push_back (BuildMI (V9::ADDi, 3).addMReg (sp).addSImm (Size).addMReg (sp,
-    MachineOperand::Def));
+  MBB.push_back (BuildMI (V9::ADDi, 3).addMReg (sp).addSImm (TotalStackSize)
+    .addMReg (sp, MachineOperand::Def));
 
   // 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");
+           TF->ReturnBlockForTraceExit.end ())
+          && "Can't find matrix fn BB address to return to from trace");
   std::pair<uint64_t, uint64_t> BlockAddrs =
     getBasicBlockInfo(TF->ReturnBlockForTraceExit[RBB]);
   uint64_t ReturnAddress = BlockAddrs.first;
-  DEBUG (std::cerr << "Return block for trace exit path is: "
+  DEBUG (std::cerr << "rewriteEpilog: Return block for trace exit path is:\n"
          << *TF->ReturnBlockForTraceExit[RBB] << "\n"
-         << "Mapping info says addresses are: return address = 0x"
-         << std::hex << BlockAddrs.first << ", end of block = 0x"
+         << "rewriteEpilog: Mapping info says addresses are: return address ="
+         << " 0x" << std::hex << BlockAddrs.first << ", end of block = 0x"
          << BlockAddrs.second << std::dec << "\n");
   // Insert a branch back to the return BasicBlock of the matrix fn.
   insertBranchMachineInstrs (ReturnAddress, MBB);
@@ -585,6 +590,21 @@
 
   DEBUG(std::cerr << "UnpackTraceFunction: unpacking "
           << MF.getFunction()->getName() << "()\n");
+
+  // Initialize RegsToRestore with the set of registers we'll need to restore
+  // in the epilog. This is a subset of the registers we'll need to save on the
+  // stack in the prolog!
+  findRegsToRestore (MF);
+
+  // Calculate the stack size.
+  // The actual amount we will allocate is (Static Stack Frame Size + Slop +
+  // Space for Regs).  The Slop = 176 number is from looking at the SparcV9
+  // ABI. The Space for Regs = 104 number is the number of 64-bit registers
+  // that are known to the SparcV9 backend.
+  StaticStackSize = getStaticStackSize (MF);
+  TotalStackSize = (StaticStackSize + 176 + 104 * 8);
+  DEBUG(std::cerr << "rewriteProlog: Stack sizes: static = " << StaticStackSize
+        << ", total = " << TotalStackSize << "\n");
 
   // Rewrite function prolog, found in the entry MachineBasicBlock of MF
   rewriteProlog (MF, MF.front ());





More information about the llvm-commits mailing list