summaryrefslogtreecommitdiffstats
path: root/llvm/lib/ProfileData/InstrProfReader.cpp
diff options
context:
space:
mode:
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-23 03:38:12 +0000
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>2014-03-23 03:38:12 +0000
commit4680361d7c881f80b6ddc96bc68d2118eda5cd25 (patch)
treef3dc00c5aaad728849ba6a7dda68115ed50fd445 /llvm/lib/ProfileData/InstrProfReader.cpp
parent96cf6c3892f08452273a9f3a2be495771bfa4edc (diff)
downloadbcm5719-llvm-4680361d7c881f80b6ddc96bc68d2118eda5cd25.tar.gz
bcm5719-llvm-4680361d7c881f80b6ddc96bc68d2118eda5cd25.zip
InstrProf: Check pointer size in raw profile
Since the profile can come from 32-bit machines, we need to check the pointer size. Change the magic number to facilitate this. Adds tests for reading 32-bit and 64-bit binaries (both big- and little-endian). The tests write a binary using printf in RUN lines (like raw-magic-but-no-header.test). Assuming the bots don't complain, this seems like a better way forward for testing RawInstrProfReader than committing binary files. <rdar://problem/16400648> llvm-svn: 204557
Diffstat (limited to 'llvm/lib/ProfileData/InstrProfReader.cpp')
-rw-r--r--llvm/lib/ProfileData/InstrProfReader.cpp51
1 files changed, 40 insertions, 11 deletions
diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp
index 48e740eaf42..47853ad8b72 100644
--- a/llvm/lib/ProfileData/InstrProfReader.cpp
+++ b/llvm/lib/ProfileData/InstrProfReader.cpp
@@ -30,8 +30,10 @@ error_code InstrProfReader::create(std::string Path,
return instrprof_error::too_large;
// Create the reader.
- if (RawInstrProfReader::hasFormat(*Buffer))
- Result.reset(new RawInstrProfReader(std::move(Buffer)));
+ 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(std::move(Buffer)));
@@ -85,7 +87,11 @@ error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
return success();
}
-static uint64_t getRawMagic() {
+template <class IntPtrT>
+static uint64_t getRawMagic();
+
+template <>
+uint64_t getRawMagic<uint64_t>() {
return
uint64_t(255) << 56 |
uint64_t('l') << 48 |
@@ -97,21 +103,36 @@ static uint64_t getRawMagic() {
uint64_t(129);
}
-bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
- if (DataBuffer.getBufferSize() < sizeof(getRawMagic()))
+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(getRawMagic<IntPtrT>()))
return false;
const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
- return getRawMagic() == Header->Magic ||
- sys::SwapByteOrder(getRawMagic()) == Header->Magic;
+ return getRawMagic<IntPtrT>() == Header->Magic ||
+ sys::SwapByteOrder(getRawMagic<IntPtrT>()) == Header->Magic;
}
-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 = (const RawHeader *)DataBuffer->getBufferStart();
- ShouldSwapBytes = Header->Magic != getRawMagic();
+ ShouldSwapBytes = Header->Magic != getRawMagic<IntPtrT>();
return readHeader(*Header);
}
@@ -119,7 +140,8 @@ static uint64_t getRawVersion() {
return 1;
}
-error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
+template <class IntPtrT>
+error_code RawInstrProfReader<IntPtrT>::readHeader(const RawHeader &Header) {
if (swap(Header.Version) != getRawVersion())
return error(instrprof_error::unsupported_version);
@@ -145,7 +167,9 @@ error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
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);
@@ -177,3 +201,8 @@ error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
++Data;
return success();
}
+
+namespace llvm {
+template class RawInstrProfReader<uint32_t>;
+template class RawInstrProfReader<uint64_t>;
+}
OpenPOWER on IntegriCloud