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

Brian Gaeke gaeke at cs.uiuc.edu
Thu Jul 22 22:00:13 PDT 2004



Changes in directory reopt/lib/LightWtProfiling:

UnpackTraceFunction.cpp updated: 1.102 -> 1.103

---
Log message:

Add new runOnMachineBasicBlock method to encapsulate all the rewrites
that are common to all MBBs.
Use one common definition of sp, g3, MatrixFP, etc. for the whole
file.
Eliminate FMOVDs at the beginning of the function which are inserted by
the register allocator (why does it do this???)
Swap MatrixFP and TraceFP when we make a function call from on-trace.
This fixes Olden/em3d.


---
Diffs of the changes:  (+67 -36)

Index: reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp
diff -u reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.102 reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.103
--- reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp:1.102	Wed Jul 21 15:53:28 2004
+++ reopt/lib/LightWtProfiling/UnpackTraceFunction.cpp	Fri Jul 23 00:00:03 2004
@@ -65,6 +65,10 @@
 extern std::pair<AllocInfo, AllocInfo>
 GetValueAllocState (TraceFunction *TF, Value *V, bool preferLiveIn = true);
 
+// Commonly used registers:
+static const unsigned sp = SparcV9::o6, MatrixFP = SparcV9::i6,
+  TraceFP = SparcV9::g1, g2 = SparcV9::g2, g3 = SparcV9::g3;
+
 /// Fill in the set of registers used in this function, which is kept in
 /// 'RegsToSave' in the UnpackTraceFunction Pass object, and is used by
 /// rewriteProlog() and rewriteEpilog(). Registers are
@@ -97,6 +101,7 @@
         }
       }
     }
+  RegsToSave.insert (MatrixFP);
 
   // Deal with some sparc lunacy: If any of the floatcc regs are used, then we
   // only put %fsr in the set, not the floatcc regs which were actually used.
@@ -171,16 +176,12 @@
   std::cerr << " in TraceFn\n";
 }
 
-void UnpackTraceFunction::rewriteProlog (MachineFunction &MF,
-                                         MachineBasicBlock &EntryBB) {
-  const SparcV9RegInfo &TRI = *TM->getRegInfo ();
-  static const unsigned sp = SparcV9::o6, MatrixFP = SparcV9::i6,
-    TraceFP = SparcV9::g1, g2 = SparcV9::g2;
-
+void UnpackTraceFunction::rewriteProlog (MachineBasicBlock &EntryBB) {
   // UTF prolog: start out by clearing SAVE and stack-load instructions out
   // of the entry BB.
   while (EntryBB.front().getOpcode() == V9::SAVEi
-         || EntryBB.front().getOpcode() == V9::LDXi)
+         || EntryBB.front().getOpcode() == V9::LDXi
+         || EntryBB.front().getOpcode() == V9::FMOVD)
     EntryBB.pop_front();
 
   std::vector<MachineInstr *> E;
@@ -197,6 +198,7 @@
   // 2. Save used registers onto the stack.
   std::vector<MachineInstr *> mvec;
   int RegType;
+  const SparcV9RegInfo &TRI = *TM->getRegInfo ();
   for (std::set<unsigned>::iterator i = RegsToSave.begin (),
        e = RegsToSave.end (); i != e; ++i) {
     mvec.clear ();
@@ -340,8 +342,6 @@
        const AllocInfo &Target, Value *liveOutValue, Value *liveOutTraceValue) {
   const SparcV9RegInfo &TRI = *TM->getRegInfo ();
   std::vector<MachineInstr *> mvec;
-  static const unsigned sp = SparcV9::o6, TraceFP = SparcV9::g1,
-    g2 = SparcV9::g2, g3 = SparcV9::g3, MatrixFP = SparcV9::i6;
 
   assert ((Target.AllocState == AllocInfo::Allocated
           || Target.AllocState == AllocInfo::Spilled)
@@ -445,8 +445,6 @@
 
 bool UnpackTraceFunction::rewriteEpilogInstr (MachineBasicBlock &MBB,
                                               MachineBasicBlock::iterator iter){
-  static const unsigned sp = SparcV9::o6, g1 = SparcV9::g1,
-    g2 = SparcV9::g2, g3 = SparcV9::g3;
   const TargetInstrInfo &TII = *TM->getInstrInfo ();
   const SparcV9RegInfo &TRI = *TM->getRegInfo ();
   MachineInstr &inst = *iter;
@@ -492,7 +490,6 @@
             RegsToSave.insert (MatrixReg);
         } else if (MatrixAI.AllocState == AllocInfo::Spilled) {
           // MatrixAI.Placement is the stack slot allocated to V in MatrixFn.
-          static const unsigned MatrixFP = SparcV9::i6;
           // Store it into its stack slot on MatrixFn's stack.
           // using MatrixFn's frame pointer as the base register.
           newBaseReg = MatrixFP;
@@ -527,8 +524,7 @@
   return false; // Don't delete the instr.
 }
 
-void UnpackTraceFunction::rewriteEpilog (MachineFunction &MF,
-                                         MachineBasicBlock &MBB) {
+void UnpackTraceFunction::rewriteEpilog (MachineBasicBlock &MBB) {
   // Rewrite any stores into live-out pseudo-arguments to store into stack
   // slots instead. Delete old epilog leftovers in the process.
   for (MachineBasicBlock::iterator i = MBB.begin (), e = MBB.end (); i != e; ) {
@@ -556,12 +552,12 @@
   // eliminate them by inserting copies now.
   for (BasicBlock::const_iterator Inst = RBB->begin ();
        const PHINode *PN = dyn_cast<PHINode> (Inst); ++Inst)
-    eliminatePhiAtTraceExit (MF, MBB, PN);
+    eliminatePhiAtTraceExit (*MBB.getParent (), MBB, PN);
 
-  static const unsigned sp = SparcV9::o6, g2 = SparcV9::g2;
   std::vector<MachineInstr *> mvec;
   const SparcV9RegInfo &TRI = *TM->getRegInfo ();
 
+  RegsToSave.erase (MatrixFP);
   // Get the set of registers used in this function which we saved in
   // findRegsToSave, earlier, and restore all used registers from stack
   // except SP.
@@ -597,6 +593,57 @@
   BuildMI (&MBB, V9::NOP, 0);
 }
 
+bool UnpackTraceFunction::runOnMachineBasicBlock (MachineBasicBlock &MBB) {
+  const SparcV9RegInfo &TRI = *TM->getRegInfo ();
+  if (&MBB == &MBB.getParent()->front()) {
+	// Rewrite function prolog, found in the entry MachineBasicBlock.
+	rewriteProlog (MBB);
+	return true;
+  }
+  for (MachineBasicBlock::iterator BI = MBB.begin (); BI != MBB.end (); ++BI) {
+	// Rewrite references to %fp to use TraceFP (%g1) instead.
+	for (unsigned i = 0; i < BI->getNumOperands(); ++i)
+	  if (BI->getOperand (i).hasAllocatedReg ()
+		  && BI->getOperand (i).getReg () == MatrixFP)
+		BI->SetMachineOperandReg (i, TraceFP);
+	// If this is a CALL, add compensation code around it.
+	if (BI->getOpcode() == V9::CALL || BI->getOpcode() == V9::JMPLCALLi) {
+      std::vector<MachineInstr *> mvec;
+	  // Save TraceFP on the stack.
+      TRI.cpReg2MemMI (mvec, TraceFP, sp, stackOffsetForReg (TraceFP), 
+                       TRI.getRegType (TraceFP), g2);
+	  // Let fp = g1.
+      mvec.push_back (BuildMI (V9::ORr, 3).addMReg (TraceFP).addZImm (0)
+        .addMReg (MatrixFP, MachineOperand::Def));
+      // Insert all the instrs into the MBB after the call instruction.
+      for (std::vector<MachineInstr *>::iterator ei = mvec.begin (),
+           ee = mvec.end (); ei != ee; ++ei) {
+        BI = MBB.insert (BI, *ei);
+        ++BI;
+      }
+      ++BI; // Skip over the call instruction.
+      ++BI; // Skip over the delay slot.
+	  // Load saved TraceFP and MatrixFP off the TraceFn stack.
+      mvec.clear();
+      TRI.cpMem2RegMI (mvec, sp, stackOffsetForReg (MatrixFP), MatrixFP,
+                       TRI.getRegType (MatrixFP), g2);
+      TRI.cpMem2RegMI (mvec, sp, stackOffsetForReg (TraceFP), TraceFP,
+                       TRI.getRegType (TraceFP), g2);
+      // Insert all the instrs into the MBB after the call instruction.
+      for (std::vector<MachineInstr *>::iterator ei = mvec.begin (),
+           ee = mvec.end (); ei != ee; ++ei) {
+        BI = MBB.insert (BI, *ei);
+        ++BI;
+      }
+      --BI;
+	}
+  }
+  // Rewrite function epilogs, found in every exit MachineBasicBlock of MF.
+  if (containsReturnInstr (MBB))
+	rewriteEpilog (MBB);
+  return true; // MachineBasicBlock was modified
+}
+
 /// runOnMachineFunction - Prepare MF, which is the machine code for
 /// TF->TraceFn, to be executed using a low-overhead calling convention for
 /// traces. We use the live-variable and return-block information in TF and the
@@ -627,29 +674,13 @@
   DEBUG(std::cerr << "UnpackTraceFunction: Stack sizes: static = "
         << StaticStackSize << ", total = " << TotalStackSize << "\n");
 
-  for (MachineFunction::iterator I = MF.begin (), E = MF.end (); I != E; ++I) {
-    MachineBasicBlock &MBB = *I;
-    static const unsigned fp = SparcV9::i6, TraceFP = SparcV9::g1;
-    if (I == MF.begin()) {
-      // Rewrite function prolog, found in the entry MachineBasicBlock of MF.
-      rewriteProlog (MF, MBB);
-      continue;
-    }
-    // Rewrite references to %fp to use TraceFP (%g1) instead.
-    for (MachineBasicBlock::iterator BI = MBB.begin (), BE = MBB.end ();
-         BI != BE; ++BI)
-      for (unsigned i = 0; i < BI->getNumOperands(); ++i)
-        if (BI->getOperand (i).hasAllocatedReg ()
-            && BI->getOperand (i).getReg () == fp)
-          BI->SetMachineOperandReg (i, TraceFP);
-    // Rewrite function epilogs, found in every exit MachineBasicBlock of MF.
-    if (containsReturnInstr (MBB))
-      rewriteEpilog (MF, MBB);
-  }
+  bool Changed = false;
+  for (MachineFunction::iterator I = MF.begin (), E = MF.end (); I != E; ++I)
+    Changed |= runOnMachineBasicBlock (*I);
 
   DEBUG(std::cerr << "UnpackTraceFunction: done unpacking "
           << MF.getFunction()->getName() << "()\n");
-  return true; // MachineFunction was modified
+  return Changed;
 }
 
 } // end namespace llvm





More information about the llvm-commits mailing list