#include "llvm/LTO/LTOModule.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/CodeGen/Analysis.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCTargetAsmParser.h"
#include "llvm/MC/SubtargetFeature.h"
+#include "llvm/Object/IRObjectFile.h"
+#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetRegisterInfo.h"
+#include "llvm/Target/TargetSubtargetInfo.h"
#include "llvm/Transforms/Utils/GlobalStatus.h"
#include <system_error>
using namespace llvm;
+using namespace llvm::object;
LTOModule::LTOModule(std::unique_ptr<object::IRObjectFile> Obj,
llvm::TargetMachine *TM)
/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
/// bitcode.
-bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
- return sys::fs::identify_magic(StringRef((const char *)mem, length)) ==
- sys::fs::file_magic::bitcode;
+bool LTOModule::isBitcodeFile(const void *Mem, size_t Length) {
+ ErrorOr<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
+ MemoryBufferRef(StringRef((const char *)Mem, Length), "<mem>"));
+ return bool(BCData);
}
-bool LTOModule::isBitcodeFile(const char *path) {
- sys::fs::file_magic type;
- if (sys::fs::identify_magic(path, type))
+bool LTOModule::isBitcodeFile(const char *Path) {
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
+ MemoryBuffer::getFile(Path);
+ if (!BufferOrErr)
return false;
- return type == sys::fs::file_magic::bitcode;
+
+ ErrorOr<MemoryBufferRef> BCData = IRObjectFile::findBitcodeInMemBuffer(
+ BufferOrErr.get()->getMemBufferRef());
+ return bool(BCData);
}
-bool LTOModule::isBitcodeForTarget(MemoryBuffer *buffer,
- StringRef triplePrefix) {
- StringRef Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
- return Triple.startswith(triplePrefix);
+bool LTOModule::isBitcodeForTarget(MemoryBuffer *Buffer,
+ StringRef TriplePrefix) {
+ ErrorOr<MemoryBufferRef> BCOrErr =
+ IRObjectFile::findBitcodeInMemBuffer(Buffer->getMemBufferRef());
+ if (!BCOrErr)
+ return false;
+ std::string Triple = getBitcodeTargetTriple(*BCOrErr, getGlobalContext());
+ return StringRef(Triple).startswith(TriplePrefix);
}
LTOModule *LTOModule::createFromFile(const char *path, TargetOptions options,
std::string &errMsg) {
- std::unique_ptr<MemoryBuffer> buffer;
- if (std::error_code ec = MemoryBuffer::getFile(path, buffer)) {
- errMsg = ec.message();
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
+ MemoryBuffer::getFile(path);
+ if (std::error_code EC = BufferOrErr.getError()) {
+ errMsg = EC.message();
return nullptr;
}
- return makeLTOModule(std::move(buffer), options, errMsg);
+ std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
+ return makeLTOModule(Buffer->getMemBufferRef(), options, errMsg);
}
LTOModule *LTOModule::createFromOpenFile(int fd, const char *path, size_t size,
size_t map_size, off_t offset,
TargetOptions options,
std::string &errMsg) {
- std::unique_ptr<MemoryBuffer> buffer;
- if (std::error_code ec =
- MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) {
- errMsg = ec.message();
+ ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
+ MemoryBuffer::getOpenFileSlice(fd, path, map_size, offset);
+ if (std::error_code EC = BufferOrErr.getError()) {
+ errMsg = EC.message();
return nullptr;
}
- return makeLTOModule(std::move(buffer), options, errMsg);
+ std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get());
+ return makeLTOModule(Buffer->getMemBufferRef(), options, errMsg);
}
LTOModule *LTOModule::createFromBuffer(const void *mem, size_t length,
TargetOptions options,
std::string &errMsg, StringRef path) {
- std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length, path));
- if (!buffer)
- return nullptr;
- return makeLTOModule(std::move(buffer), options, errMsg);
+ StringRef Data((const char *)mem, length);
+ MemoryBufferRef Buffer(Data, path);
+ return makeLTOModule(Buffer, options, errMsg);
}
-LTOModule *LTOModule::makeLTOModule(std::unique_ptr<MemoryBuffer> Buffer,
+LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer,
TargetOptions options,
std::string &errMsg) {
- ErrorOr<Module *> MOrErr =
- getLazyBitcodeModule(Buffer.get(), getGlobalContext());
+ ErrorOr<MemoryBufferRef> MBOrErr =
+ IRObjectFile::findBitcodeInMemBuffer(Buffer);
+ if (std::error_code EC = MBOrErr.getError()) {
+ errMsg = EC.message();
+ return nullptr;
+ }
+ ErrorOr<Module *> MOrErr = parseBitcodeFile(*MBOrErr, getGlobalContext());
if (std::error_code EC = MOrErr.getError()) {
errMsg = EC.message();
return nullptr;
CPU = "core2";
else if (Triple.getArch() == llvm::Triple::x86)
CPU = "yonah";
- else if (Triple.getArch() == llvm::Triple::arm64 ||
- Triple.getArch() == llvm::Triple::aarch64)
+ else if (Triple.getArch() == llvm::Triple::aarch64)
CPU = "cyclone";
}
TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr,
options);
- M->materializeAllPermanently(true);
- M->setDataLayout(target->getDataLayout());
+ M->setDataLayout(target->getSubtargetImpl()->getDataLayout());
std::unique_ptr<object::IRObjectFile> IRObj(
- new object::IRObjectFile(std::move(Buffer), std::move(M)));
+ new object::IRObjectFile(Buffer, std::move(M)));
LTOModule *Ret = new LTOModule(std::move(IRObj), target);
}
/// Create a MemoryBuffer from a memory range with an optional name.
-MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length,
- StringRef name) {
+std::unique_ptr<MemoryBuffer>
+LTOModule::makeBuffer(const void *mem, size_t length, StringRef name) {
const char *startPtr = (const char*)mem;
return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false);
}
addDefinedSymbol(Name, F, true);
}
-static bool canBeHidden(const GlobalValue *GV) {
- // FIXME: this is duplicated with another static function in AsmPrinter.cpp
- GlobalValue::LinkageTypes L = GV->getLinkage();
-
- if (L != GlobalValue::LinkOnceODRLinkage)
- return false;
-
- if (GV->hasUnnamedAddr())
- return true;
-
- // If it is a non constant variable, it needs to be uniqued across shared
- // objects.
- if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) {
- if (!Var->isConstant())
- return false;
- }
-
- GlobalStatus GS;
- if (GlobalStatus::analyzeGlobal(GV, GS))
- return false;
-
- return !GS.IsCompared;
-}
-
void LTOModule::addDefinedSymbol(const char *Name, const GlobalValue *def,
bool isFunction) {
// set alignment part log2() can have rounding errors
attr |= LTO_SYMBOL_SCOPE_HIDDEN;
else if (def->hasProtectedVisibility())
attr |= LTO_SYMBOL_SCOPE_PROTECTED;
- else if (canBeHidden(def))
+ else if (canBeOmittedFromSymbolTable(def))
attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
else
attr |= LTO_SYMBOL_SCOPE_DEFAULT;
MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii));
StringRef Op = _linkeropt_strings.
GetOrCreateValue(MDOption->getString()).getKey();
- StringRef DepLibName = _target->getTargetLowering()->
- getObjFileLowering().getDepLibFromLinkerOpt(Op);
+ StringRef DepLibName = _target->getSubtargetImpl()
+ ->getTargetLowering()
+ ->getObjFileLowering()
+ .getDepLibFromLinkerOpt(Op);
if (!DepLibName.empty())
_deplibs.push_back(DepLibName.data());
else if (!Op.empty())