move include searching logic from TGLexer to SourceMgr.
[oota-llvm.git] / lib / Support / Timer.cpp
index 077995d9fb8993a21d4a07120d20e1c366b0854a..3c8879bd06e35da6aad9377c189034c4d7ee79d3 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -39,12 +39,12 @@ static std::string &getLibSupportInfoOutputFilename() {
 }
 
 namespace {
-  cl::opt<bool>
+  static cl::opt<bool>
   TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
                                       "tracking (this may be slow)"),
              cl::Hidden);
 
-  cl::opt<std::string, true>
+  static cl::opt<std::string, true>
   InfoOutputFilename("info-output-file", cl::value_desc("filename"),
                      cl::desc("File to append -stats and -timer output to"),
                    cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
@@ -132,13 +132,13 @@ static ManagedStatic<std::vector<Timer*> > ActiveTimers;
 
 void Timer::startTimer() {
   Started = true;
+  ActiveTimers->push_back(this);
   TimeRecord TR = getTimeRecord(true);
   Elapsed    -= TR.Elapsed;
   UserTime   -= TR.UserTime;
   SystemTime -= TR.SystemTime;
   MemUsed    -= TR.MemUsed;
   PeakMemBase = TR.MemUsed;
-  ActiveTimers->push_back(this);
 }
 
 void Timer::stopTimer() {
@@ -182,19 +182,51 @@ void Timer::addPeakMemoryMeasurement() {
 //   NamedRegionTimer Implementation
 //===----------------------------------------------------------------------===//
 
-static ManagedStatic<std::map<std::string, Timer> > NamedTimers;
+namespace {
+
+typedef std::map<std::string, Timer> Name2Timer;
+typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
+
+}
+
+static ManagedStatic<Name2Timer> NamedTimers;
+
+static ManagedStatic<Name2Pair> NamedGroupedTimers;
 
 static Timer &getNamedRegionTimer(const std::string &Name) {
-  std::map<std::string, Timer>::iterator I = NamedTimers->lower_bound(Name);
-  if (I != NamedTimers->end() && I->first == Name)
+  Name2Timer::iterator I = NamedTimers->find(Name);
+  if (I != NamedTimers->end())
     return I->second;
 
   return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
 }
 
+static Timer &getNamedRegionTimer(const std::string &Name,
+                                  const std::string &GroupName) {
+
+  Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
+  if (I == NamedGroupedTimers->end()) {
+    TimerGroup TG(GroupName);
+    std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
+    I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
+  }
+
+  Name2Timer::iterator J = I->second.second.find(Name);
+  if (J == I->second.second.end())
+    J = I->second.second.insert(J,
+                                std::make_pair(Name,
+                                               Timer(Name,
+                                                     I->second.first)));
+
+  return J->second;
+}
+
 NamedRegionTimer::NamedRegionTimer(const std::string &Name)
   : TimeRegion(getNamedRegionTimer(Name)) {}
 
+NamedRegionTimer::NamedRegionTimer(const std::string &Name,
+                                   const std::string &GroupName)
+  : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
 
 //===----------------------------------------------------------------------===//
 //   TimerGroup Implementation