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

Brian Gaeke gaeke at cs.uiuc.edu
Mon Jun 14 02:30:01 PDT 2004


Changes in directory reopt/lib/TraceToFunction:

TraceToFunction.cpp updated: 1.62 -> 1.63

---
Log message:

Get rid of the NamedFunctionTimer and don't include Support/Timer.h; there
doesn't seem to be any other easy way to turn the timer off.

Make getTraceLiveInSet & getTraceLiveOutSet methods of TraceFunctionBuilder.
Make them take references to their respective sets and fill them in.  Update
the doxygen comments.

Make buildFLIMap build up the set of AlternateEntryPoints that a trace exit
could make us take, and make getTraceLiveOutSet fail an assertion for now, if
AlternateEntryPoints has anything in it.

Constify getFLIEdgeSource and getFLIEdgeTarget, and the LiveVariableSet
arguments to createFunctionArgTypeVector and createLiveOutType.

Update doxygen comments, wrap some long lines, and get rid of those pesky tabs.
Ugh!


---
Diffs of the changes:  (+77 -62)

Index: reopt/lib/TraceToFunction/TraceToFunction.cpp
diff -u reopt/lib/TraceToFunction/TraceToFunction.cpp:1.62 reopt/lib/TraceToFunction/TraceToFunction.cpp:1.63
--- reopt/lib/TraceToFunction/TraceToFunction.cpp:1.62	Mon Jun 14 00:05:46 2004
+++ reopt/lib/TraceToFunction/TraceToFunction.cpp	Mon Jun 14 02:29:06 2004
@@ -31,7 +31,6 @@
 #include "Support/StringExtras.h"  // for utostr()
 #define DEBUG_TYPE "tracetofunction"
 #include "Support/Debug.h"         // for DEBUG()
-#include "Support/Timer.h"
 #include <cassert>
 
 namespace llvm {
@@ -48,18 +47,22 @@
 class TraceFunctionBuilder {
   BranchNumberMap BranchNumber;
   TraceFunction *TF;
+  std::set<BasicBlock *> AlternateEntryPoints;
+  void getTraceLiveInSet (LiveVariableSet &S, Trace &T);
+  void getTraceLiveOutSet (LiveVariableSet &S, Trace &T);
 
   FLIMapTy FLIMap;
-  BasicBlock *getFLIEdgeSource (BasicBlock *BB);
-  BasicBlock *getFLIEdgeTarget (BasicBlock *BB);
+  BasicBlock *getFLIEdgeSource (BasicBlock *BB) const;
+  BasicBlock *getFLIEdgeTarget (BasicBlock *BB) const;
   void threadFLIEdges (Function *F);
   void buildFLIMap (Trace &T, FLIMapTy &FLIMap);
 
-  TypeVector createFunctionArgTypeVector (PointerType *ST, LiveVariableSet S);
+  TypeVector createFunctionArgTypeVector (PointerType *ST,
+                                          const LiveVariableSet &S);
   void fillInFunctionBody (Trace &T, Function *F, LiveVariableSet &So);
   void fixupFunctionBodyBB (Trace &T, Function *F, BasicBlock *srcB,
-				            BasicBlock *dstB, ValueMap &O2CMap,
-				            LiveVariableSet &So);
+                            BasicBlock *dstB, ValueMap &O2CMap,
+                            LiveVariableSet &So);
 
   void cloneMapDump ();
 public:
@@ -85,11 +88,11 @@
     // If any successors are on the trace, AND not the entry BB, check them too.
     // (Stop at entry BB because we don't want to loop if the CFG loops.)
     for (succ_const_iterator i = succ_begin (start), e = succ_end (start);
-	 i != e; ++i) {
+         i != e; ++i) {
       const BasicBlock *succ = *i;
       if (T.contains (succ) && (T.getEntryBasicBlock () != succ)
-	  && (!dominates (T, B1, B2, succ)))
-	return false;
+          && (!dominates (T, B1, B2, succ)))
+        return false;
     }
     return true; // Dominates on all successors ==> dominates here too
   }
@@ -115,35 +118,34 @@
       // Start at Block->begin() traversing next pointers; if UInst
       // appears BEFORE Inst in Block's InstList, return false
       for (BasicBlock::const_iterator bi = Block->begin (), be = Block->end ();
-	   bi != be; ++bi) {
-	const Instruction *BlockInst = bi;
-	if (BlockInst == UInst)
-	  return false;
-	else if (BlockInst == Inst)
-	  break;
+           bi != be; ++bi) {
+        const Instruction *BlockInst = bi;
+        if (BlockInst == UInst)
+          return false;
+        else if (BlockInst == Inst)
+          break;
       }
     } else {
       if (!T.contains (UBlock))
-	return false;
+        return false;
       else {
-	// If UBlock appears BEFORE Block on some path from the first
-	// basic block of the trace to an exit BB of the trace, return
-	// false.
-	if (!dominates (T, Block, UBlock))
-	  return false;
+        // If UBlock appears BEFORE Block on some path from the first
+        // basic block of the trace to an exit BB of the trace, return
+        // false.
+        if (!dominates (T, Block, UBlock))
+          return false;
       }
     }
   }
   return true;
 }
 
-/// getTraceLiveInSet - Return the live-in set of the trace.  Any
-/// variable which is used in the trace is potentially live-in, EXCEPT
-/// if it's a constant or a global, OR it is defined before being used
+/// getTraceLiveInSet - Initialize TF->LiveInSet with the live-in set of the
+/// trace.  Any variable which is used in the trace is potentially live-in,
+/// EXCEPT if it's a constant or a global, OR it is defined before being used
 /// in the trace.
 ///
-static LiveVariableSet getTraceLiveInSet (Trace &T) {
-  LiveVariableSet S;
+void TraceFunctionBuilder::getTraceLiveInSet (LiveVariableSet &S, Trace &T) {
   for (Trace::iterator TI = T.begin (), TE = T.end (); TI != TE; ++TI) {
     BasicBlock *B = *TI;
     // B is a basic block in the trace.
@@ -151,12 +153,12 @@
       Instruction &I = *BI;
       // I is an instruction in B, which is in the trace.
       for (unsigned i = 0; i < I.getNumOperands (); ++i) {
-	Value *V = I.getOperand (i);
-	// V is used in the trace by instruction I.
-	if (!(isa<Constant> (V) || isa<GlobalValue> (V)
+        Value *V = I.getOperand (i);
+        // V is used in the trace by instruction I.
+        if (!(isa<Constant> (V) || isa<GlobalValue> (V)
               || isa<BasicBlock>(V)))
-	  if (!DefinedInTraceBeforeUse (V, T))
-	    S.insert (V);
+          if (!DefinedInTraceBeforeUse (V, T))
+            S.insert (V);
       }
     }
   }
@@ -164,15 +166,14 @@
   for (BasicBlock::iterator Inst = T.getEntryBasicBlock ()->begin ();
        PHINode *PN = dyn_cast<PHINode> (Inst); ++Inst)
     S.insert (PN);
-  return S;
 }
 
-/// getTraceLiveOutSet - Any variable defined within the trace is
+/// getTraceLiveOutSet - Initializes S with the live-out set of trace T.
+/// Any variable defined within the trace is
 /// potentially live-out, EXCEPT if its only uses are in the trace AND
 /// come after the def.
 ///
-static LiveVariableSet getTraceLiveOutSet (Trace &T) {
-  LiveVariableSet S;
+void TraceFunctionBuilder::getTraceLiveOutSet (LiveVariableSet &S, Trace &T) {
   Function &F = *T.getFunction();
   for (Trace::iterator TI = T.begin (), TE = T.end (); TI != TE; ++TI) {
     BasicBlock *B = *TI;
@@ -187,7 +188,10 @@
           S.insert (&I);
     }
   }
-  return S;
+  if (!AlternateEntryPoints.empty ()) {
+    assert (AlternateEntryPoints.empty ()
+            && "Alternate entry points not handled yet");
+  }
 }
 
 /// containsTraceExitingBranch - If B contains a branch instruction BI
@@ -208,14 +212,16 @@
 /// createLiveOutType - Given the live-out set S of a trace, create a
 /// pointer-to-structure type that will encapsulate all the live-outs from S.
 ///
-static PointerType *createLiveOutType (LiveVariableSet S) {
+static PointerType *createLiveOutType (const LiveVariableSet &S) {
   TypeVector T;
   unsigned Counter = 0;
   // For each variable in S, make a new entry in T having its type.
   DEBUG (std::cerr << "Live-out values:\n");
-  for (LiveVariableSet::iterator I = S.begin (), E = S.end (); I != E; ++I) {
+  for (LiveVariableSet::const_iterator I = S.begin (), E = S.end (); I != E;
+       ++I) {
     DEBUG (WriteAsOperand (std::cerr, *I, true, true,
-                           cast<Instruction>(*I)->getParent ()->getParent ()->getParent ());
+                           cast<Instruction>(*I)->getParent ()->getParent ()
+                             ->getParent ());
            std::cerr << " is at position " << Counter++ << "\n");
     T.push_back ((*I)->getType ());
   }
@@ -230,12 +236,13 @@
 /// TF->LiveInToParameterMap.
 ///
 TypeVector TraceFunctionBuilder::createFunctionArgTypeVector (PointerType *PST,
-							 LiveVariableSet S) {
+                                                     const LiveVariableSet &S) {
   TypeVector P;
   P.push_back (PST);
 
   TF->LiveInToParameterMap.clear ();
-  for (LiveVariableSet::iterator I = S.begin (), E = S.end (); I != E; ++I) {
+  for (LiveVariableSet::const_iterator I = S.begin (), E = S.end (); I != E;
+       ++I) {
     Value *V = *I;
     P.push_back (V->getType ());
     TF->LiveInToParameterMap[V] = P.size () - 1;
@@ -243,7 +250,7 @@
   // Print out mapping of instructions producing live-ins to arg numbers.
   DEBUG(std::cerr << "\nLive-in values:\n";
         for (ValueToIntMap::iterator I = TF->LiveInToParameterMap.begin (),
-	       E = TF->LiveInToParameterMap.end (); I != E; ++I) {
+             E = TF->LiveInToParameterMap.end (); I != E; ++I) {
           WriteAsOperand (std::cerr, I->first, true, true,
             TF->MatrixFn->getParent ());
           std::cerr << " is parameter " << I->second << "\n";
@@ -396,7 +403,7 @@
 /// argument.
 ///
 void TraceFunctionBuilder::fillInFunctionBody (Trace &T, Function *F,
-					  LiveVariableSet &So) {
+                                               LiveVariableSet &So) {
   // First copy the basic blocks from the trace.
   cloneTraceBBsIntoFunction (TF);
 
@@ -426,7 +433,8 @@
 /// effect of this function, if it returns true, is to fill in the pair "edge"
 /// with the source and target of the back-edge.
 ///
-static bool identifyFLIEdge (BasicBlock *BB, std::pair<BasicBlock *, BasicBlock *> &edge) {
+static bool identifyFLIEdge (BasicBlock *BB,
+                             std::pair<BasicBlock *, BasicBlock *> &edge) {
   BasicBlock::iterator i = BB->begin ();
   // starts with llvm_first_trigger call
   if (!isFirstTriggerCall (*i))
@@ -490,6 +498,8 @@
 /// buildFLIMap - Removes any FLI blocks which ended up in the trace, and
 /// identifies the source and sink BasicBlocks of all FLI-instrumented
 /// back-edges. Information about FLI edges is stored in the FLIMap.
+/// Information about FLI sinks that could end up in the middle of the trace
+/// is stored in the AlternateEntryPoints set.
 ///
 void TraceFunctionBuilder::buildFLIMap (Trace &T, FLIMapTy &FLIMap) {
   Function *F = T.getFunction ();
@@ -499,9 +509,11 @@
       DEBUG (std::cerr << "buildFLIMap: identified FLI block " << i->getName()
         << " on edge <" << edge.first->getName () << ", "
         << edge.second->getName () << ">\n");
-      DEBUG (if (T.contains (edge.second) && edge.second != T.getEntryBasicBlock ())
-             std::cerr << "buildFLIMap: " << edge.second->getName()
-             << " is a back-edge target that's internal to the trace\n");
+      if (T.contains (edge.second) && edge.second != T.getEntryBasicBlock ()) {
+        DEBUG (std::cerr << "buildFLIMap: " << edge.second->getName()
+               << " is a back-edge target that's internal to the trace\n");
+        AlternateEntryPoints.insert (edge.second);
+      }
       // 1. remove the block from the trace if it is in there
       BasicBlock *bb = i;
       Trace::iterator TI = std::find (T.begin (), T.end (), bb);
@@ -515,14 +527,14 @@
   }
 }
 
-BasicBlock *TraceFunctionBuilder::getFLIEdgeSource (BasicBlock *BB) {
-  FLIMapTy::iterator i = FLIMap.find (BB);
+BasicBlock *TraceFunctionBuilder::getFLIEdgeSource (BasicBlock *BB) const {
+  FLIMapTy::const_iterator i = FLIMap.find (BB);
   if (i == FLIMap.end ()) return 0;
   return i->second.first;
 }
 
-BasicBlock *TraceFunctionBuilder::getFLIEdgeTarget (BasicBlock *BB) {
-  FLIMapTy::iterator i = FLIMap.find (BB);
+BasicBlock *TraceFunctionBuilder::getFLIEdgeTarget (BasicBlock *BB) const {
+  FLIMapTy::const_iterator i = FLIMap.find (BB);
   if (i == FLIMap.end ()) return 0;
   return i->second.second;
 }
@@ -534,9 +546,10 @@
 /// argument. (This is where all the magic gets done...) 
 ///
 void TraceFunctionBuilder::fixupFunctionBodyBB (Trace &T, Function *F,
-					   BasicBlock *srcB, BasicBlock *dstB,
-					   ValueMap &O2CMap,
-					   LiveVariableSet &So) {
+                                                BasicBlock *srcB,
+                                                BasicBlock *dstB,
+                                                ValueMap &O2CMap,
+                                                LiveVariableSet &So) {
   assert (T.contains (srcB) && "Source BB is not on the trace");
   assert (dstB->getParent () == F && "Clone is not in the function");
 
@@ -702,19 +715,22 @@
   }
 }
 
-/// Dump out the O2CMap for the given TraceFunction to stderr.
+/// cloneMapDump - Dump out the O2CMap for the given TraceFunction to stderr.
 /// You can call this from the debugger if you want to see the gory details,
 /// because it takes a really long time on big modules.
 ///
+/// FIXME: This method should probably be a method of TraceFunction,
+/// not TraceFunctionBuilder.
+///
 void TraceFunctionBuilder::cloneMapDump () {
   std::cerr << "\n; TraceFunctionBuilder Original-->Clone map follows:\n";
   for (ValueMap::const_iterator i = TF->O2CMap.begin(), e = TF->O2CMap.end();
        i != e; ++i) {
     const std::pair<const Value *, Value *> &elem = *i;
     std::cerr << "(original-value \"";
-    WriteAsOperand (std::cerr, i->first, true, true, TF->MatrixFn->getParent ());
+    WriteAsOperand (std::cerr, i->first, true, true, TF->MatrixFn->getParent());
     std::cerr << "\" maps-to \"";
-    WriteAsOperand (std::cerr, i->second, true, true, TF->TraceFn->getParent ());
+    WriteAsOperand (std::cerr, i->second, true, true, TF->TraceFn->getParent());
     std::cerr << "\")\n";
   }
   std::cerr << "\n";
@@ -734,8 +750,8 @@
 
   // Get some information about the trace's relationship to its parent
   // function.
-  TF->LiveInSet = getTraceLiveInSet (T);
-  TF->LiveOutSet = getTraceLiveOutSet (T);
+  getTraceLiveInSet (TF->LiveInSet, T);
+  getTraceLiveOutSet (TF->LiveOutSet, T);
   TypeVector P =
     createFunctionArgTypeVector (createLiveOutType (TF->LiveOutSet),
                                  TF->LiveInSet);
@@ -744,8 +760,8 @@
   // list P, in the same Module as the trace's parent function.
   std::string name (CurrentFnName + ".trace");
   TF->TraceFn = new Function (FunctionType::get (Type::UIntTy, P, false),
-			      GlobalValue::InternalLinkage, name,
-			      T.getModule ());
+                              GlobalValue::InternalLinkage, name,
+                              T.getModule ());
   DEBUG(giveNamesToFunctionArgs (TF->LiveInSet, TF->TraceFn));
   fillInFunctionBody (T, TF->TraceFn, TF->LiveOutSet);
   return TF;
@@ -756,7 +772,6 @@
 /// data structures.
 ///
 TraceFunction *TraceFunction::get (Trace &T) {
-  NamedRegionTimer X ("TraceToFunction");
   TraceFunctionBuilder TTF;
   return TTF.buildTraceFunction (T);
 }





More information about the llvm-commits mailing list