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