1dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//=-- InstrProfIndexed.h - Indexed profiling format support -------*- C++ -*-=//
2dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//
3dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//                     The LLVM Compiler Infrastructure
4dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//
5dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// This file is distributed under the University of Illinois Open Source
6dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// License. See LICENSE.TXT for details.
7dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//
8dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//===----------------------------------------------------------------------===//
9dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//
10dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines// Shared header for the instrumented profile data reader and writer.
11dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//
12dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines//===----------------------------------------------------------------------===//
13dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
14dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#ifndef LLVM_PROFILEDATA_INSTRPROF_INDEXED_H_
15dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define LLVM_PROFILEDATA_INSTRPROF_INDEXED_H_
16dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
17dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/Support/ErrorHandling.h"
18dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include "llvm/Support/MD5.h"
19dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
20dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesnamespace llvm {
21dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
22dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesnamespace IndexedInstrProf {
23dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesenum class HashT : uint32_t {
24dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MD5,
25dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
26dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Last = MD5
27dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines};
28dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
29dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic inline uint64_t MD5Hash(StringRef Str) {
30dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MD5 Hash;
31dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Hash.update(Str);
32dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  llvm::MD5::MD5Result Result;
33dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  Hash.final(Result);
34dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Return the least significant 8 bytes. Our MD5 implementation returns the
35dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // result in little endian, so we may need to swap bytes.
36dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  using namespace llvm::support;
37dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return endian::read<uint64_t, little, unaligned>(Result);
38dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
39dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
40dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesstatic inline uint64_t ComputeHash(HashT Type, StringRef K) {
41dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  switch (Type) {
42dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case HashT::MD5:
43dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return IndexedInstrProf::MD5Hash(K);
44dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
45dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  llvm_unreachable("Unhandled hash type");
46dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
47dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
48dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesconst uint64_t Magic = 0x8169666f72706cff; // "\xfflprofi\x81"
49dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesconst uint64_t Version = 1;
50dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesconst HashT HashType = HashT::MD5;
51dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
52dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
53dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines} // end namespace llvm
54dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
55dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#endif // LLVM_PROFILEDATA_INSTRPROF_INDEXED_H_
56