LLVM API Documentation

ProfileInfoLoader.cpp

Go to the documentation of this file.
00001 //===- ProfileInfoLoad.cpp - Load profile information from disk -----------===//
00002 //
00003 //                      The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // The ProfileInfoLoader class is used to load and represent profiling
00011 // information read in from the dump file.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Analysis/ProfileInfoLoader.h"
00016 #include "llvm/Analysis/ProfileInfoTypes.h"
00017 #include "llvm/Module.h"
00018 #include "llvm/InstrTypes.h"
00019 #include "llvm/Support/Streams.h"
00020 #include <cstdio>
00021 #include <cstdlib>
00022 #include <map>
00023 using namespace llvm;
00024 
00025 // ByteSwap - Byteswap 'Var' if 'Really' is true.
00026 //
00027 static inline unsigned ByteSwap(unsigned Var, bool Really) {
00028   if (!Really) return Var;
00029   return ((Var & (255<< 0)) << 24) |
00030          ((Var & (255<< 8)) <<  8) |
00031          ((Var & (255<<16)) >>  8) |
00032          ((Var & (255<<24)) >> 24);
00033 }
00034 
00035 static void ReadProfilingBlock(const char *ToolName, FILE *F,
00036                                bool ShouldByteSwap,
00037                                std::vector<unsigned> &Data) {
00038   // Read the number of entries...
00039   unsigned NumEntries;
00040   if (fread(&NumEntries, sizeof(unsigned), 1, F) != 1) {
00041     cerr << ToolName << ": data packet truncated!\n";
00042     perror(0);
00043     exit(1);
00044   }
00045   NumEntries = ByteSwap(NumEntries, ShouldByteSwap);
00046 
00047   // Read the counts...
00048   std::vector<unsigned> TempSpace(NumEntries);
00049 
00050   // Read in the block of data...
00051   if (fread(&TempSpace[0], sizeof(unsigned)*NumEntries, 1, F) != 1) {
00052     cerr << ToolName << ": data packet truncated!\n";
00053     perror(0);
00054     exit(1);
00055   }
00056 
00057   // Make sure we have enough space...
00058   if (Data.size() < NumEntries)
00059     Data.resize(NumEntries);
00060 
00061   // Accumulate the data we just read into the data.
00062   if (!ShouldByteSwap) {
00063     for (unsigned i = 0; i != NumEntries; ++i)
00064       Data[i] += TempSpace[i];
00065   } else {
00066     for (unsigned i = 0; i != NumEntries; ++i)
00067       Data[i] += ByteSwap(TempSpace[i], true);
00068   }
00069 }
00070 
00071 // ProfileInfoLoader ctor - Read the specified profiling data file, exiting the
00072 // program if the file is invalid or broken.
00073 //
00074 ProfileInfoLoader::ProfileInfoLoader(const char *ToolName,
00075                                      const std::string &Filename,
00076                                      Module &TheModule) :
00077                                      Filename(Filename), 
00078                                      M(TheModule), Warned(false) {
00079   FILE *F = fopen(Filename.c_str(), "r");
00080   if (F == 0) {
00081     cerr << ToolName << ": Error opening '" << Filename << "': ";
00082     perror(0);
00083     exit(1);
00084   }
00085 
00086   // Keep reading packets until we run out of them.
00087   unsigned PacketType;
00088   while (fread(&PacketType, sizeof(unsigned), 1, F) == 1) {
00089     // If the low eight bits of the packet are zero, we must be dealing with an
00090     // endianness mismatch.  Byteswap all words read from the profiling
00091     // information.
00092     bool ShouldByteSwap = (char)PacketType == 0;
00093     PacketType = ByteSwap(PacketType, ShouldByteSwap);
00094 
00095     switch (PacketType) {
00096     case ArgumentInfo: {
00097       unsigned ArgLength;
00098       if (fread(&ArgLength, sizeof(unsigned), 1, F) != 1) {
00099         cerr << ToolName << ": arguments packet truncated!\n";
00100         perror(0);
00101         exit(1);
00102       }
00103       ArgLength = ByteSwap(ArgLength, ShouldByteSwap);
00104 
00105       // Read in the arguments...
00106       std::vector<char> Chars(ArgLength+4);
00107 
00108       if (ArgLength)
00109         if (fread(&Chars[0], (ArgLength+3) & ~3, 1, F) != 1) {
00110           cerr << ToolName << ": arguments packet truncated!\n";
00111           perror(0);
00112           exit(1);
00113         }
00114       CommandLines.push_back(std::string(&Chars[0], &Chars[ArgLength]));
00115       break;
00116     }
00117 
00118     case FunctionInfo:
00119       ReadProfilingBlock(ToolName, F, ShouldByteSwap, FunctionCounts);
00120       break;
00121 
00122     case BlockInfo:
00123       ReadProfilingBlock(ToolName, F, ShouldByteSwap, BlockCounts);
00124       break;
00125 
00126     case EdgeInfo:
00127       ReadProfilingBlock(ToolName, F, ShouldByteSwap, EdgeCounts);
00128       break;
00129 
00130     case BBTraceInfo:
00131       ReadProfilingBlock(ToolName, F, ShouldByteSwap, BBTrace);
00132       break;
00133 
00134     default:
00135       cerr << ToolName << ": Unknown packet type #" << PacketType << "!\n";
00136       exit(1);
00137     }
00138   }
00139 
00140   fclose(F);
00141 }
00142