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

Brian Gaeke gaeke at cs.uiuc.edu
Thu Jul 15 15:05:25 PDT 2004



Changes in directory reopt/lib/TraceToFunction:

TraceToFunction.cpp updated: 1.76 -> 1.77

---
Log message:

Make TraceFunctionBuilder into a FunctionPass, so that it can use real,
honest-to-goodness DominatorSet analysis results. Note that it's a
FunctionPass that runs on the *matrix* function!
Rename buildTraceFunction method --> runOnFunction.
Restructure data members and add some comments.
Rename getTraceLive{In,Out}Set methods --> buildTraceLive{In,Out}Set, to
clarify what they are doing.
Add hasUseDominatedByEdge() (disabled for now, because it's buggy!)
Nuke cloneMapDump(); no one uses it anymore.
Fill in the ArgToLiveOutMap along with LiveOutToArgMap, for the benefit of
the new epilog rewriter.


---
Diffs of the changes:  (+89 -51)

Index: reopt/lib/TraceToFunction/TraceToFunction.cpp
diff -u reopt/lib/TraceToFunction/TraceToFunction.cpp:1.76 reopt/lib/TraceToFunction/TraceToFunction.cpp:1.77
--- reopt/lib/TraceToFunction/TraceToFunction.cpp:1.76	Thu Jul 15 16:57:00 2004
+++ reopt/lib/TraceToFunction/TraceToFunction.cpp	Thu Jul 15 17:05:15 2004
@@ -52,25 +52,36 @@
 /// TraceFunctionBuilder - a Method Object which encapsulates the algorithm
 /// for building TraceFunctions given Traces.
 ///
-class TraceFunctionBuilder {
+class TraceFunctionBuilder : public FunctionPass {
+  // Inputs
+  Trace &T;
+  Module *Mod;
+  DominatorSet *DS;
+
+  // Assorted temporary data structures 
   BranchNumberMap BranchNumber;
-  TraceFunction *TF;
   std::vector<BasicBlock *> AlternateEntryPointsV;
 
+  // Output
+  TraceFunction *TF;
+
+  // Live-variable sets and their builder methods
   LiveVariableSet LiveInSet;
+  void buildTraceLiveInSet (LiveVariableSet &S, LiveVariableVector &LVV,
+                            Trace &T);
   LiveVariableSet LiveOutSet;
+  void buildTraceLiveOutSet (LiveVariableSet &S, LiveVariableVector &LVV,
+                             Trace &T);
 
-  void getTraceLiveInSet (LiveVariableSet &S, LiveVariableVector &LVV,
-                          Trace &T);
-  void getTraceLiveOutSet (LiveVariableSet &S, LiveVariableVector &LVV,
-                           Trace &T);
-
+  // Map showing which blocks contain first-level instrumentation (FLI),
+  // with builder and accessor methods
   FLIMapTy FLIMap;
   BasicBlock *getFLIEdgeSource (BasicBlock *BB) const;
   BasicBlock *getFLIEdgeTarget (BasicBlock *BB) const;
   void threadFLIEdges (Function *F);
   void buildFLIMap (Trace &T, FLIMapTy &FLIMap);
 
+  // Main methods of TraceFunctionBuilder transformation
   TypeVector createFunctionArgTypeVector (const LiveVariableVector &LiveIns,
                                           const LiveVariableVector &LiveOuts);
   void giveNamesToFunctionArgs (const LiveVariableVector &LiveIns,
@@ -78,20 +89,32 @@
                                 Function *F);
   void fillInFunctionBody (Trace &T, Function *F, LiveVariableVector &So);
   int findOffTracePhiSource (const PHINode *newPN);
+  bool hasUseDominatedByEdge (Instruction *defInst, BasicBlock *source,
+                              BasicBlock *target);
   void fixupFunctionBodyBB (Trace &T, Function *F, BasicBlock *srcB,
                             BasicBlock *dstB, ValueMap &O2CMap,
                             LiveVariableVector &So);
-
-  void cloneMapDump ();
 public:
-  TraceFunctionBuilder () { }
+  TraceFunctionBuilder (Trace &Tr) : T (Tr) { }
   virtual ~TraceFunctionBuilder () { }
+  TraceFunction *getTraceFunction () { return TF; }
 
-  /// buildTraceFunction - Given a Trace object, returns a pointer to a new
-  /// TraceFunction containing the same code as the Trace, along with some. 
+  bool doInitialization (Module &M) {
+    Mod = &M;
+    return false;
+  }
+
+  virtual void getAnalysisUsage (AnalysisUsage &AU) const {
+    AU.setPreservesAll ();
+    AU.addRequired<DominatorSet> ();
+  }
+
+  /// runOnFunction - Given a Trace object, and dominator information
+  /// about the Trace's parent function, returns a pointer to a new
+  /// TraceFunction containing the same code as the Trace, along with some
   /// auxiliary data structures.
   ///
-  virtual TraceFunction *buildTraceFunction (Trace &T);
+  virtual bool runOnFunction (Function &F);
 };
 
 static bool dominates (Trace &T, const BasicBlock *B1,
@@ -152,8 +175,7 @@
         if (BlockInst == UInst) {
           DEBUG (std::cerr << "TLV: " << V->getName () << " Found User" << UInst->getName () << " before Def!\n");
           return false;
-        }
-        else if (BlockInst == Inst) {
+        } else if (BlockInst == Inst) {
           DEBUG (std::cerr << "TLV: " << V->getName () << " Found Def before User: " << UInst->getName () << "\n");
           break;
         }
@@ -167,8 +189,7 @@
         } else {
           DEBUG (std::cerr << "TLV: " << V->getName () << "'s User " << UInst->getName () << " is off-trace, but we don't care\n");
         }
-      }
-      else {
+      } 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.
@@ -189,8 +210,8 @@
     Vector.push_back (V);
 }
 
-/// addTraceLiveInsToSet - helper fn. for getTraceLiveInSet and
-/// getTraceLiveOutSet. Adds the live-ins of T to S, assuming that
+/// addTraceLiveInsToSet - helper fn. for buildTraceLiveInSet and
+/// buildTraceLiveOutSet. Adds the live-ins of T to S, assuming that
 /// the trace starts at StartingFrom and ends at T.end().
 static void addTraceLiveInsToSet (LiveVariableSet &S, LiveVariableVector &LVV,
                                   Trace &T, Trace::iterator StartingFrom) {
@@ -230,23 +251,23 @@
     }
 }
 
-/// getTraceLiveInSet - Initialize LiveInSet with the live-in set of the
+/// buildTraceLiveInSet - Initialize 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.
 ///
-void TraceFunctionBuilder::getTraceLiveInSet (LiveVariableSet &S, LiveVariableVector &LVV, Trace &T) {
+void TraceFunctionBuilder::buildTraceLiveInSet (LiveVariableSet &S, LiveVariableVector &LVV, Trace &T) {
   addTraceLiveInsToSet (S, LVV, T, T.begin ());
 }
 
-/// getTraceLiveOutSet - Initializes S with the live-out set of trace T.
+/// buildTraceLiveOutSet - 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. This method must also take into account side entrances
 /// to the NON-traced version of the code caused by early exits that end up
 /// taking back-edge branches.
 ///
-void TraceFunctionBuilder::getTraceLiveOutSet (LiveVariableSet &S, LiveVariableVector &LVV, Trace &T) {
+void TraceFunctionBuilder::buildTraceLiveOutSet (LiveVariableSet &S, LiveVariableVector &LVV, Trace &T) {
   // Iterate over the instructions in the trace, adding live-out values to S.
   for (Trace::iterator TI = T.begin (), TE = T.end (); TI != TE; ++TI)
     for (BasicBlock::iterator Inst = (*TI)->begin (), BE = (*TI)->end ();
@@ -341,6 +362,7 @@
                            TF->MatrixFn->getParent ());
            std::cerr << " is argument # " << argPosition << " (%");
     TF->LiveOutToArgMap[V] = &*argIterator;
+    TF->ArgToLiveOutMap[&*argIterator] = V;
     std::string name (V->getName ());
     if (name == "")
       name = "arg" + utostr (argPosition);
@@ -630,6 +652,26 @@
   return -1;
 }
 
+bool
+TraceFunctionBuilder::hasUseDominatedByEdge (Instruction *defInst,
+                                             BasicBlock *source,
+                                             BasicBlock *target) {
+  for (Value::use_iterator UI = defInst->use_begin (), UE =
+       defInst->use_end (); UI != UE; ++UI) {
+    if (Instruction *useInst = dyn_cast<Instruction> (*UI)) {
+      if (DS->dominates (&target->front(), useInst))
+        return true;
+    } else {
+      // Gack, it's not an Instruction.
+      assert (0 && "Found non-Instruction User, don't know dominator info");
+    }
+  }
+  DEBUG (std::cerr << "hasUseDominatedByEdge: found no dominated use"
+                   << " starting from " << target->getName () << " for:\n"
+                   << *defInst);
+  return false;
+}
+
 /// fixupFunctionBodyBB - Given srcB in T and its clone dstB in F, and
 /// the map O2CMap detailing the correspondences between values in T
 /// and values in F, fix up dstB so that its contents are internally
@@ -723,7 +765,13 @@
         for (LiveVariableVector::iterator SI = So.begin (), SE = So.end ();
              SI != SE; ++SI)
           if (dominates (T, cast<Instruction> ((*SI))->getParent (),
+#if 0 // WARNING: hasUseDominatedByEdge isn't working right yet!
+                         BI->getParent ())
+              && hasUseDominatedByEdge (cast<Instruction> (*SI), srcB,
+                                         successor))
+#else
                          BI->getParent ()))
+#endif
             FB->getInstList ().push_back
               (new StoreInst (O2CMap[*SI], TF->LiveOutToArgMap[*SI]));
         // Make FB contain a return instruction that returns the
@@ -793,28 +841,14 @@
   }
 }
 
-/// 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());
-    std::cerr << "\" maps-to \"";
-    WriteAsOperand (std::cerr, i->second, true, true, TF->TraceFn->getParent());
-    std::cerr << "\")\n";
-  }
-  std::cerr << "\n";
-}
+bool TraceFunctionBuilder::runOnFunction (Function &F) {
+  if (T.getFunction () != &F) { return false; }
+
+  DS = &getAnalysis<DominatorSet>();
+  DEBUG (std::cerr << "Dominator set information:\n";
+         DS->print (std::cerr);
+         std::cerr << "End dominator set information.\n");
 
-TraceFunction *TraceFunctionBuilder::buildTraceFunction (Trace &T) {
   // Create a TraceFunction object to hold the trace function along with
   // its auxiliary data structures.
   TF = new TraceFunction (T);
@@ -828,8 +862,8 @@
 
   // Get some information about the trace's relationship to its parent
   // function.
-  getTraceLiveInSet (LiveInSet, TF->LiveInVector, T);
-  getTraceLiveOutSet (LiveOutSet, TF->LiveOutVector, T);
+  buildTraceLiveInSet (LiveInSet, TF->LiveInVector, T);
+  buildTraceLiveOutSet (LiveOutSet, TF->LiveOutVector, T);
   TypeVector P = createFunctionArgTypeVector (TF->LiveInVector,
                                               TF->LiveOutVector);
 
@@ -841,16 +875,20 @@
                               T.getModule ());
   giveNamesToFunctionArgs (TF->LiveInVector, TF->LiveOutVector, TF->TraceFn);
   fillInFunctionBody (T, TF->TraceFn, TF->LiveOutVector);
-  return TF;
+
+  return false;
 }
 
-/// TraceFunction::get - Given a Trace, returns a TraceFunction, which is
+/// BuildTraceFunction - Given a Trace, returns a TraceFunction, which is
 /// a Function containing the code from the Trace along with some supporting
 /// data structures.
 ///
-TraceFunction *TraceFunction::get (Trace &T) {
-  TraceFunctionBuilder TTF;
-  return TTF.buildTraceFunction (T);
+TraceFunction *BuildTraceFunction (Trace &T, ModuleProvider *MP) {
+  TraceFunctionBuilder *TTF = new TraceFunctionBuilder (T);
+  FunctionPassManager FPM (MP);
+  FPM.add (TTF);
+  FPM.run (*T.getFunction());
+  return TTF->getTraceFunction();
 }
 
 } // end namespace llvm





More information about the llvm-commits mailing list