]> granicus.if.org Git - clang/commitdiff
Keep track of the number of statements/expressions written to and read
authorDouglas Gregor <dgregor@apple.com>
Fri, 17 Apr 2009 22:13:46 +0000 (22:13 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 17 Apr 2009 22:13:46 +0000 (22:13 +0000)
from a PCH file. It turns out that "Hello, World!" is bringing in 19%
of all of the statements in Carbon.h, so we need to be lazy.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69393 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Frontend/PCHBitCodes.h
include/clang/Frontend/PCHReader.h
include/clang/Frontend/PCHWriter.h
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp

index 70b44b8448b2c0f3083fcf885ca1563f35baeeda..8bb81676bdcabc7c9d64b45e44d8fb746ae4f0d0 100644 (file)
@@ -140,7 +140,11 @@ namespace clang {
       /// reported to the AST consumer after the PCH file has been
       /// read, since their presence can affect the semantics of the
       /// program (e.g., for code generation).
-      EXTERNAL_DEFINITIONS = 7
+      EXTERNAL_DEFINITIONS = 7,
+
+      /// \brief Record code for the block of extra statistics we
+      /// gather while generating a PCH file.
+      STATISTICS = 8
     };
 
     /// \brief Record types used within a source manager block.
index 0507cfa5f5c9e6efafe773aabe467928ede5b788..1352908ef185bdef0dbcf7a81a315b632eae7b0d 100644 (file)
@@ -149,6 +149,14 @@ private:
   /// been de-serialized.
   std::multimap<unsigned, AddrLabelExpr *> UnresolvedAddrLabelExprs;
 
+  /// \brief The number of statements (and expressions) de-serialized
+  /// from the PCH file.
+  unsigned NumStatementsRead;
+
+  /// \brief The total number of statements (and expressions) stored
+  /// in the PCH file.
+  unsigned TotalNumStatements;
+
   PCHReadResult ReadPCHBlock();
   bool CheckPredefinesBuffer(const char *PCHPredef, 
                              unsigned PCHPredefLen,
@@ -168,7 +176,7 @@ public:
   typedef llvm::SmallVector<uint64_t, 64> RecordData;
 
   PCHReader(Preprocessor &PP, ASTContext &Context) 
-    : PP(PP), Context(Context), IdentifierTable(0) { }
+    : PP(PP), Context(Context), IdentifierTable(0), NumStatementsRead(0) { }
 
   ~PCHReader() {}
 
index 07fb3d3a2cc5061e358c0f57245453ce14185548..ee101b54f741be8f64f0c261ffad588d80b50fd3 100644 (file)
@@ -117,6 +117,9 @@ private:
   /// \brief Mapping from LabelStmt statements to IDs.
   std::map<LabelStmt *, unsigned> LabelIDs;
 
+  /// \brief The number of statements written to the PCH file.
+  unsigned NumStatements;
+
   void WriteTargetTriple(const TargetInfo &Target);
   void WriteLanguageOptions(const LangOptions &LangOpts);
   void WriteSourceManagerBlock(SourceManager &SourceMgr);
index da7b4229aa1d4871eaf10b2cff7467c59d09e5ba..5ea1f7c6c2254e3ed56257d4ed3413fbd2e6bf40 100644 (file)
@@ -1173,7 +1173,7 @@ PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
   }
 
   uint64_t PreprocessorBlockBit = 0;
-  
+
   // Read all of the records and blocks for the PCH file.
   RecordData Record;
   while (!Stream.AtEndOfStream()) {
@@ -1315,6 +1315,11 @@ PCHReader::PCHReadResult PCHReader::ReadPCHBlock() {
       }
       ExternalDefinitions.swap(Record);
       break;
+
+    case pch::STATISTICS:
+      TotalNumStatements = Record[0];
+      break;
+
     }
   }
 
@@ -1976,6 +1981,9 @@ void PCHReader::PrintStats() {
   std::fprintf(stderr, "  %u/%u identifiers read (%f%%)\n",
                NumIdentifiersLoaded, (unsigned)IdentifierData.size(),
                ((float)NumIdentifiersLoaded/IdentifierData.size() * 100));
+  std::fprintf(stderr, "  %u/%u statements read (%f%%)\n",
+               NumStatementsRead, TotalNumStatements,
+               ((float)NumStatementsRead/TotalNumStatements * 100));
   std::fprintf(stderr, "\n");
 }
 
@@ -2450,6 +2458,8 @@ Stmt *PCHReader::ReadStmt() {
     if (Finished)
       break;
 
+    ++NumStatementsRead;
+
     if (S) {
       unsigned NumSubStmts = Reader.Visit(S);
       while (NumSubStmts > 0) {
index 5775ac4c135559475825a177c4bc545b4940a22f..64bf3833b4f195a191812f9b251acce68a1398a2 100644 (file)
@@ -1723,7 +1723,7 @@ void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
 }
 
 PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream) 
-  : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS) { }
+  : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), NumStatements(0) { }
 
 void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
   // Emit the file header.
@@ -1749,6 +1749,11 @@ void PCHWriter::WritePCH(ASTContext &Context, const Preprocessor &PP) {
   Stream.EmitRecord(pch::DECL_OFFSET, DeclOffsets);
   if (!ExternalDefinitions.empty())
     Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
+  
+  // Some simple statistics
+  RecordData Record;
+  Record.push_back(NumStatements);
+  Stream.EmitRecord(pch::STATISTICS, Record);
   Stream.ExitBlock();
 }
 
@@ -1880,6 +1885,7 @@ void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
 void PCHWriter::WriteSubStmt(Stmt *S) {
   RecordData Record;
   PCHStmtWriter Writer(*this, Record);
+  ++NumStatements;
 
   if (!S) {
     Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
@@ -1900,6 +1906,7 @@ void PCHWriter::FlushStmts() {
   PCHStmtWriter Writer(*this, Record);
 
   for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
+    ++NumStatements;
     Stmt *S = StmtsToEmit[I];
 
     if (!S) {