Mercurial > hg > CbC > CbC_llvm
comparison lib/Object/ModuleSummaryIndexObjectFile.cpp @ 120:1172e4bd9c6f
update 4.0.0
author | mir3636 |
---|---|
date | Fri, 25 Nov 2016 19:14:25 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
101:34baf5011add | 120:1172e4bd9c6f |
---|---|
1 //===- ModuleSummaryIndexObjectFile.cpp - Summary 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 ModuleSummaryIndexObjectFile class implementation. | |
11 // | |
12 //===----------------------------------------------------------------------===// | |
13 | |
14 #include "llvm/Object/ModuleSummaryIndexObjectFile.h" | |
15 #include "llvm/ADT/STLExtras.h" | |
16 #include "llvm/Bitcode/BitcodeReader.h" | |
17 #include "llvm/IR/ModuleSummaryIndex.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 ModuleSummaryIndexObjectFile::ModuleSummaryIndexObjectFile( | |
26 MemoryBufferRef Object, std::unique_ptr<ModuleSummaryIndex> I) | |
27 : SymbolicFile(Binary::ID_ModuleSummaryIndex, Object), Index(std::move(I)) { | |
28 } | |
29 | |
30 ModuleSummaryIndexObjectFile::~ModuleSummaryIndexObjectFile() {} | |
31 | |
32 std::unique_ptr<ModuleSummaryIndex> ModuleSummaryIndexObjectFile::takeIndex() { | |
33 return std::move(Index); | |
34 } | |
35 | |
36 ErrorOr<MemoryBufferRef> | |
37 ModuleSummaryIndexObjectFile::findBitcodeInObject(const ObjectFile &Obj) { | |
38 for (const SectionRef &Sec : Obj.sections()) { | |
39 if (Sec.isBitcode()) { | |
40 StringRef SecContents; | |
41 if (std::error_code EC = Sec.getContents(SecContents)) | |
42 return EC; | |
43 return MemoryBufferRef(SecContents, Obj.getFileName()); | |
44 } | |
45 } | |
46 | |
47 return object_error::bitcode_section_not_found; | |
48 } | |
49 | |
50 ErrorOr<MemoryBufferRef> | |
51 ModuleSummaryIndexObjectFile::findBitcodeInMemBuffer(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 Expected<std::unique_ptr<ObjectFile>> ObjFile = | |
60 ObjectFile::createObjectFile(Object, Type); | |
61 if (!ObjFile) | |
62 return errorToErrorCode(ObjFile.takeError()); | |
63 return findBitcodeInObject(*ObjFile->get()); | |
64 } | |
65 default: | |
66 return object_error::invalid_file_type; | |
67 } | |
68 } | |
69 | |
70 // Parse module summary index in the given memory buffer. | |
71 // Return new ModuleSummaryIndexObjectFile instance containing parsed | |
72 // module summary/index. | |
73 Expected<std::unique_ptr<ModuleSummaryIndexObjectFile>> | |
74 ModuleSummaryIndexObjectFile::create(MemoryBufferRef Object) { | |
75 ErrorOr<MemoryBufferRef> BCOrErr = findBitcodeInMemBuffer(Object); | |
76 if (!BCOrErr) | |
77 return errorCodeToError(BCOrErr.getError()); | |
78 | |
79 Expected<std::unique_ptr<ModuleSummaryIndex>> IOrErr = | |
80 getModuleSummaryIndex(BCOrErr.get()); | |
81 | |
82 if (!IOrErr) | |
83 return IOrErr.takeError(); | |
84 | |
85 std::unique_ptr<ModuleSummaryIndex> Index = std::move(IOrErr.get()); | |
86 return llvm::make_unique<ModuleSummaryIndexObjectFile>(Object, | |
87 std::move(Index)); | |
88 } | |
89 | |
90 // Parse the module summary index out of an IR file and return the summary | |
91 // index object if found, or nullptr if not. | |
92 Expected<std::unique_ptr<ModuleSummaryIndex>> | |
93 llvm::getModuleSummaryIndexForFile(StringRef Path) { | |
94 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = | |
95 MemoryBuffer::getFileOrSTDIN(Path); | |
96 std::error_code EC = FileOrErr.getError(); | |
97 if (EC) | |
98 return errorCodeToError(EC); | |
99 MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef(); | |
100 Expected<std::unique_ptr<object::ModuleSummaryIndexObjectFile>> ObjOrErr = | |
101 object::ModuleSummaryIndexObjectFile::create(BufferRef); | |
102 if (!ObjOrErr) | |
103 return ObjOrErr.takeError(); | |
104 | |
105 object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr; | |
106 return Obj.takeIndex(); | |
107 } |