1//===-- JIT.h - Abstract Execution Engine Interface -------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Common functionality for JITEventListener implementations
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef EVENT_LISTENER_COMMON_H
15#define EVENT_LISTENER_COMMON_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/IR/DebugInfo.h"
19#include "llvm/IR/Metadata.h"
20#include "llvm/IR/ValueHandle.h"
21#include "llvm/Support/Path.h"
22
23namespace llvm {
24
25namespace jitprofiling {
26
27class FilenameCache {
28  // Holds the filename of each Scope, so that we can pass a null-terminated
29  // string into oprofile.  Use an AssertingVH rather than a ValueMap because we
30  // shouldn't be modifying any MDNodes while this map is alive.
31  DenseMap<AssertingVH<MDNode>, std::string> Filenames;
32  DenseMap<AssertingVH<MDNode>, std::string> Paths;
33
34 public:
35  const char *getFilename(MDNode *Scope) {
36    std::string &Filename = Filenames[Scope];
37    if (Filename.empty()) {
38      DIScope DIScope(Scope);
39      Filename = DIScope.getFilename();
40    }
41    return Filename.c_str();
42  }
43
44  const char *getFullPath(MDNode *Scope) {
45    std::string &P = Paths[Scope];
46    if (P.empty()) {
47      DIScope DIScope(Scope);
48      StringRef DirName = DIScope.getDirectory();
49      StringRef FileName = DIScope.getFilename();
50      SmallString<256> FullPath;
51      if (DirName != "." && DirName != "") {
52        FullPath = DirName;
53      }
54      if (FileName != "") {
55        sys::path::append(FullPath, FileName);
56      }
57      P = FullPath.str();
58    }
59    return P.c_str();
60  }
61};
62
63} // namespace jitprofiling
64
65} // namespace llvm
66
67#endif //EVENT_LISTENER_COMMON_H
68