obj2yaml: Use the correct relocation type for different machine types
[oota-llvm.git] / lib / ProfileData / InstrProfReader.cpp
index d2e5fbd9b9a132c33d4e76f3b2a40965cefb608e..b07f4027771a68ccf6e5bd8b26e346e0811733c4 100644 (file)
 
 using namespace llvm;
 
-static uint64_t getRawMagic() {
-  return
-    uint64_t('l') << 56 |
-    uint64_t('p') << 48 |
-    uint64_t('r') << 40 |
-    uint64_t('o') << 32 |
-    uint64_t('f') << 24 |
-    uint64_t('r') << 16 |
-    uint64_t('a') <<  8 |
-    uint64_t('w');
-}
-
 error_code InstrProfReader::create(std::string Path,
                                    std::unique_ptr<InstrProfReader> &Result) {
   std::unique_ptr<MemoryBuffer> Buffer;
@@ -41,17 +29,15 @@ error_code InstrProfReader::create(std::string Path,
   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
     return instrprof_error::too_large;
 
-  if (Buffer->getBufferSize() < sizeof(uint64_t)) {
-    Result.reset(new TextInstrProfReader(Buffer));
-    return Result->readHeader();
-  }
-
-  uint64_t Magic = *(uint64_t *)Buffer->getBufferStart();
-  uint64_t SwappedMagic = sys::SwapByteOrder(Magic);
-  if (Magic == getRawMagic() || SwappedMagic == getRawMagic())
-    Result.reset(new RawInstrProfReader(Buffer));
+  // Create the reader.
+  if (RawInstrProfReader64::hasFormat(*Buffer))
+    Result.reset(new RawInstrProfReader64(std::move(Buffer)));
+  else if (RawInstrProfReader32::hasFormat(*Buffer))
+    Result.reset(new RawInstrProfReader32(std::move(Buffer)));
   else
-    Result.reset(new TextInstrProfReader(Buffer));
+    Result.reset(new TextInstrProfReader(std::move(Buffer)));
+
+  // Read the header and return the result.
   return Result->readHeader();
 }
 
@@ -101,30 +87,63 @@ error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
   return success();
 }
 
-static uint64_t getRawVersion() {
-  return 1;
+template <class IntPtrT>
+static uint64_t getRawMagic();
+
+template <>
+uint64_t getRawMagic<uint64_t>() {
+  return
+    uint64_t(255) << 56 |
+    uint64_t('l') << 48 |
+    uint64_t('p') << 40 |
+    uint64_t('r') << 32 |
+    uint64_t('o') << 24 |
+    uint64_t('f') << 16 |
+    uint64_t('r') <<  8 |
+    uint64_t(129);
 }
-namespace {
+
+template <>
+uint64_t getRawMagic<uint32_t>() {
+  return
+    uint64_t(255) << 56 |
+    uint64_t('l') << 48 |
+    uint64_t('p') << 40 |
+    uint64_t('r') << 32 |
+    uint64_t('o') << 24 |
+    uint64_t('f') << 16 |
+    uint64_t('R') <<  8 |
+    uint64_t(129);
+}
+
+template <class IntPtrT>
+bool RawInstrProfReader<IntPtrT>::hasFormat(const MemoryBuffer &DataBuffer) {
+  if (DataBuffer.getBufferSize() < sizeof(uint64_t))
+    return false;
+  uint64_t Magic =
+    *reinterpret_cast<const uint64_t *>(DataBuffer.getBufferStart());
+  return getRawMagic<IntPtrT>() == Magic ||
+    sys::SwapByteOrder(getRawMagic<IntPtrT>()) == Magic;
 }
-RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
-    : DataBuffer(DataBuffer.release()) { }
 
-error_code RawInstrProfReader::readHeader() {
+template <class IntPtrT>
+error_code RawInstrProfReader<IntPtrT>::readHeader() {
+  if (!hasFormat(*DataBuffer))
+    return error(instrprof_error::bad_magic);
   if (DataBuffer->getBufferSize() < sizeof(RawHeader))
     return error(instrprof_error::bad_header);
-  const RawHeader *Header = (RawHeader *)DataBuffer->getBufferStart();
-  if (Header->Magic == getRawMagic())
-    ShouldSwapBytes = false;
-  else {
-    if (sys::SwapByteOrder(Header->Magic) != getRawMagic())
-      return error(instrprof_error::bad_magic);
-
-    ShouldSwapBytes = true;
-  }
+  auto *Header =
+    reinterpret_cast<const RawHeader *>(DataBuffer->getBufferStart());
+  ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
   return readHeader(*Header);
 }
 
-error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
+static uint64_t getRawVersion() {
+  return 1;
+}
+
+template <class IntPtrT>
+error_code RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
   if (swap(Header.Version) != getRawVersion())
     return error(instrprof_error::unsupported_version);
 
@@ -142,15 +161,18 @@ error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
   if (FileSize != DataBuffer->getBufferSize())
     return error(instrprof_error::bad_header);
 
-  Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
+  const char *Start = DataBuffer->getBufferStart();
+  Data = reinterpret_cast<const ProfileData *>(Start + DataOffset);
   DataEnd = Data + DataSize;
-  CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
-  NamesStart = DataBuffer->getBufferStart() + NamesOffset;
+  CountersStart = reinterpret_cast<const uint64_t *>(Start + CountersOffset);
+  NamesStart = Start + NamesOffset;
 
   return success();
 }
 
-error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
+template <class IntPtrT>
+error_code
+RawInstrProfReader<IntPtrT>::readNextRecord(InstrProfRecord &Record) {
   if (Data == DataEnd)
     return error(instrprof_error::eof);
 
@@ -160,10 +182,11 @@ error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
                                 swap(Data->NumCounters));
 
   // Check bounds.
+  auto *NamesStartAsCounter = reinterpret_cast<const uint64_t *>(NamesStart);
   if (RawName.data() < NamesStart ||
       RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
       RawCounts.data() < CountersStart ||
-      RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
+      RawCounts.data() + RawCounts.size() > NamesStartAsCounter)
     return error(instrprof_error::malformed);
 
   // Store the data in Record, byte-swapping as necessary.
@@ -182,3 +205,8 @@ error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
   ++Data;
   return success();
 }
+
+namespace llvm {
+template class RawInstrProfReader<uint32_t>;
+template class RawInstrProfReader<uint64_t>;
+}