Support for function summary index bitcode sections and files.
[oota-llvm.git] / lib / Object / FunctionIndexObjectFile.cpp
1 //===- FunctionIndexObjectFile.cpp - Function index file implementation ----===//
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 // Part of the FunctionIndexObjectFile class implementation.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Object/FunctionIndexObjectFile.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/IR/FunctionInfo.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23 using namespace object;
24
25 FunctionIndexObjectFile::FunctionIndexObjectFile(
26     MemoryBufferRef Object, std::unique_ptr<FunctionInfoIndex> I)
27     : SymbolicFile(Binary::ID_FunctionIndex, Object), Index(std::move(I)) {}
28
29 FunctionIndexObjectFile::~FunctionIndexObjectFile() {}
30
31 std::unique_ptr<FunctionInfoIndex> FunctionIndexObjectFile::takeIndex() {
32   return std::move(Index);
33 }
34
35 ErrorOr<MemoryBufferRef> FunctionIndexObjectFile::findBitcodeInObject(
36     const ObjectFile &Obj) {
37   for (const SectionRef &Sec : Obj.sections()) {
38     StringRef SecName;
39     if (std::error_code EC = Sec.getName(SecName)) return EC;
40     if (SecName == ".llvmbc") {
41       StringRef SecContents;
42       if (std::error_code EC = Sec.getContents(SecContents)) return EC;
43       return MemoryBufferRef(SecContents, Obj.getFileName());
44     }
45   }
46
47   return object_error::bitcode_section_not_found;
48 }
49
50 ErrorOr<MemoryBufferRef> FunctionIndexObjectFile::findBitcodeInMemBuffer(
51     MemoryBufferRef Object) {
52   sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
53   switch (Type) {
54     case sys::fs::file_magic::bitcode:
55       return Object;
56     case sys::fs::file_magic::elf_relocatable:
57     case sys::fs::file_magic::macho_object:
58     case sys::fs::file_magic::coff_object: {
59       ErrorOr<std::unique_ptr<ObjectFile>> ObjFile =
60           ObjectFile::createObjectFile(Object, Type);
61       if (!ObjFile) return ObjFile.getError();
62       return findBitcodeInObject(*ObjFile->get());
63     }
64     default:
65       return object_error::invalid_file_type;
66   }
67 }
68
69 // Looks for function index in the given memory buffer.
70 // returns true if found, else false.
71 bool FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer(
72     MemoryBufferRef Object, LLVMContext &Context) {
73   ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
74   if (!BCOrErr) return false;
75
76   return hasFunctionSummary(BCOrErr.get(), Context, nullptr);
77 }
78
79 // Parse function index in the given memory buffer.
80 // Return new FunctionIndexObjectFile instance containing parsed
81 // function summary/index.
82 ErrorOr<std::unique_ptr<FunctionIndexObjectFile>>
83 FunctionIndexObjectFile::create(MemoryBufferRef Object, LLVMContext &Context,
84                                 bool IsLazy) {
85   std::unique_ptr<FunctionInfoIndex> Index;
86
87   ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object);
88   if (!BCOrErr) return BCOrErr.getError();
89
90   ErrorOr<std::unique_ptr<FunctionInfoIndex>> IOrErr =
91       getFunctionInfoIndex(BCOrErr.get(), Context, nullptr, IsLazy);
92
93   if (std::error_code EC = IOrErr.getError()) return EC;
94
95   Index = std::move(IOrErr.get());
96
97   return llvm::make_unique<FunctionIndexObjectFile>(Object, std::move(Index));
98 }
99
100 // Parse the function summary information for function with the
101 // given name out of the given buffer. Parsed information is
102 // stored on the index object saved in this object.
103 std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer(
104     MemoryBufferRef Object, LLVMContext &Context, StringRef FunctionName) {
105   sys::fs::file_magic Type = sys::fs::identify_magic(Object.getBuffer());
106   switch (Type) {
107     case sys::fs::file_magic::bitcode: {
108       return readFunctionSummary(Object, Context, nullptr, FunctionName,
109                                  std::move(Index));
110     }
111     default:
112       return object_error::invalid_file_type;
113   }
114 }