blob: 5f89e5c95a7385b321137866fd7746f78dc2ae3c [file] [log] [blame]
Teresa Johnsondf6edc52016-05-23 22:54:06 +00001//===-LTO.cpp - LLVM Link Time Optimizer ----------------------------------===//
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// This file implements functions and classes used to support LTO.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/LTO/LTO.h"
15#include "llvm/Bitcode/ReaderWriter.h"
16#include "llvm/Support/MemoryBuffer.h"
17#include "llvm/Support/SourceMgr.h"
18#include "llvm/Support/raw_ostream.h"
19
20namespace llvm {
21
22// Simple helper to load a module from bitcode
23std::unique_ptr<Module> loadModuleFromBuffer(const MemoryBufferRef &Buffer,
24 LLVMContext &Context, bool Lazy) {
25 SMDiagnostic Err;
26 ErrorOr<std::unique_ptr<Module>> ModuleOrErr(nullptr);
27 if (Lazy) {
28 ModuleOrErr =
29 getLazyBitcodeModule(MemoryBuffer::getMemBuffer(Buffer, false), Context,
30 /* ShouldLazyLoadMetadata */ Lazy);
31 } else {
32 ModuleOrErr = parseBitcodeFile(Buffer, Context);
33 }
34 if (std::error_code EC = ModuleOrErr.getError()) {
35 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
36 EC.message());
37 Err.print("ThinLTO", errs());
38 report_fatal_error("Can't load module, abort.");
39 }
40 return std::move(ModuleOrErr.get());
41}
42}