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

Brian Gaeke gaeke at cs.uiuc.edu
Mon May 24 03:57:02 PDT 2004


Changes in directory reopt/lib/LightWtProfiling:

UnpackTraceFunction.cpp updated: 1.69 -> 1.70

---
Log message:

Remove some dead variables and update some debugging messages.

Bug fixes in rewriteEpilog:
* Ignore live-in values when looking up saved reg alloc state;
  before, if we had a value which was both live-in and live-out, we would
  pass in the live-in value, modify it, and return the live-in value!
* Don't reload fp twice in the epilog. 
* Instead of doing reg->reg copies for live-out values, save them into
  the stack slots from which they will be reloaded into the correct
  matrixFn registers later. This prevents read-after-write bugs when
  reading the live-out values (but for now we don't support live values
  in stack slots, sigh.)


---
Diffs of the changes:  (+53 -37)

Index: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp
diff -u reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.69 reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.70
--- reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.69	Sun May 23 05:05:02 2004
+++ reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp	Mon May 24 03:54:46 2004
@@ -200,10 +200,14 @@
         std::cerr << " )\n");
 }
 
+/// getValueAllocState - Returns a pair <MatrixState,TraceState> containing the
+/// saved register allocator state for a value.
+///
 static std::pair<AllocInfo, AllocInfo>
-getValueAllocState (TraceFunction *TF, Value *V) {
+getValueAllocState (TraceFunction *TF, Value *V, bool preferLiveIn = true) {
   return std::make_pair (getValueAllocStateFromModule (TF->MatrixFn, V),
-    getValueAllocStateFromGlobal (TF->TraceFn, TF->getCorrespondingValue (V)));
+    getValueAllocStateFromGlobal (TF->TraceFn,
+                                  TF->getCorrespondingValue (V, preferLiveIn)));
 }
 
 unsigned UnpackTraceFunction::stackOffsetForReg (unsigned R) {
@@ -213,8 +217,7 @@
 void UnpackTraceFunction::rewriteProlog (MachineFunction &MF,
                                          MachineBasicBlock &E) {
   const TargetRegInfo &TRI = TM->getRegInfo ();
-  static const unsigned fp = SparcV9::i6, sp = SparcV9::o6, g0 = SparcV9::g0,
-    g1 = SparcV9::g1, g2 = SparcV9::g2;
+  static const unsigned sp = SparcV9::o6, g1 = SparcV9::g1, g2 = SparcV9::g2;
 
   // UTF prolog: start out by clearing everything out of the entry basic block
   // (FIXME: may not work once we start doing optimizations!!! We will probably
@@ -244,8 +247,8 @@
     AllocInfo &Source = as.second.first, &Target = as.second.second;
     if (Source != Target)
       if (Source.AllocState == AllocInfo::Allocated) {
-        DEBUG (std::cerr << "Need to save incoming live value in reg "
-               << Source.Placement << " onto the stack\n");
+        DEBUG (std::cerr << "rewriteProlog: Need to save incoming live value "
+               << "in reg " << Source.Placement << " onto the stack\n");
         RegsToSave.insert (Source.Placement);
       }
   }
@@ -334,13 +337,9 @@
 static unsigned getSavedStateIndexOfInstruction (const Function *F,
                                                  const Instruction *I) {
   unsigned Key = 0;
-  DEBUG(std::cerr << "getSavedStateIndexOfInstruction(F = " << F->getName()
-        << "(), I = " << *I << ")\n");
   for (const_inst_iterator II=inst_begin (F), IE=inst_end (F); II!=IE; ++II) {
-    if (&*II == I) {
-      DEBUG(std::cerr << "--> returns " << Key << "\n");
+    if (&*II == I)
       return Key;
-    }
     ++Key;
   }
   // By this time we had better have found it, otherwise we are about to do bad
@@ -390,10 +389,15 @@
   // (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) {
+          AllocInfo AI (T.Instruction, T.Operand,
+                        (AllocInfo::AllocStateTy) T.AllocState, T.Placement);
+          DEBUG (std::cerr << "Alloc state saved in module for "
+                 << F->getName () << ":" << V->getName () << " (key = "
+                 << InstructionKey << "," << OperandKey << ") is " << AI
+                 << "\n");
+          return AI;
+        }
   }
   // By this time we had better have found it, otherwise we are about to do bad
   // things.
@@ -429,14 +433,14 @@
   }
   // Reconstruct the AllocInfo for V by searching
   // FState for a tuple that starts with (InstructionKey, OperandKey, ...):
-  DEBUG(std::cerr << "Looking for " << F->getName () << "()'s value "
-                  << *V << "(Instruction " << InstructionKey << " Operand "
-                  << OperandKey << ")... " << FState.size ()
-                  << " tuples to search.\n");
   for (unsigned i = 0, s = FState.size (); i < s; ++i) {
     AllocInfo &T = FState[i];
-    if (T.Instruction == InstructionKey && T.Operand == OperandKey)
+    if (T.Instruction == InstructionKey && T.Operand == OperandKey) {
+      DEBUG (std::cerr << "Alloc state saved in global for " << F->getName ()
+             << ":" << V->getName () << " (key = " << InstructionKey << ","
+             << OperandKey << ") is " << T << "\n");
       return T;
+    }
   }
   // By this time we had better have found it, otherwise we are about to do bad
   // things.
@@ -449,40 +453,52 @@
 void UnpackTraceFunction::rewriteEpilog (MachineFunction &MF,
                                          MachineBasicBlock &MBB) {
   const TargetRegInfo &TRI = TM->getRegInfo ();
-  static const unsigned fp = SparcV9::i6, sp = SparcV9::o6, g0 = SparcV9::g0,
-    g1 = SparcV9::g1, g2 = SparcV9::g2;
+  static const unsigned fp = SparcV9::i6, sp = SparcV9::o6, g2 = SparcV9::g2;
 
   // 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.
+  // Insert stores from each live-out variable's reg. in the trace
+  // to its stack slot in the trace function, from which it will be
+  // reloaded below into a register. 
   std::vector<MachineInstr *> mvec;
-  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);
-  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;
-    std::pair<AllocInfo, AllocInfo> ai = getValueAllocState (TF, V);
+    std::pair<AllocInfo, AllocInfo> ai = getValueAllocState (TF, V, false);
+    // Source is traceFn's register, Target is matrixFn's register
     AllocInfo &Target = ai.first, &Source = ai.second;
-    if (Source != Target)
-      insertCopyMachineInstrs (Source, Target, MBB, V->getType ());
+    assert (Target.AllocState == AllocInfo::Allocated
+            && Source.AllocState == AllocInfo::Allocated
+            && "FIXME: does not handle live values in stack slots yet");
+    mvec.clear ();
+    unsigned R = Source.Placement;
+    unsigned RegType = TRI.getRegType (R);
+    DEBUG (std::cerr << "rewriteEpilog: saving live-out value: " << V->getName()
+           << " is allocated to reg#" << Target.AllocState
+           << " in MatrixFn and reg#" << Source.AllocState << " in TraceFn\n");
+    TRI.cpReg2MemMI (mvec, R, sp, stackOffsetForReg (Target.Placement),
+                     RegType, g2);
+    for (std::vector<MachineInstr *>::iterator vi = mvec.begin (),
+           ve = mvec.end (); vi != ve; ++vi)
+      MBB.push_back (*vi);
   }
 
+  // Restore old FP.
+  mvec.clear ();
+  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);
+  RegsToSave.erase (fp);
+
   // Get the set of registers used in this function which we saved in
   // rewriteProlog, earlier, and restore all used registers from stack
   // except SP.
-  int RegType;
   for (std::set<unsigned>::iterator i = RegsToSave.begin (),
          e = RegsToSave.end (); i != e; ++i) {
     mvec.clear ();





More information about the llvm-commits mailing list