2012-04-11 18:25:24 +08:00
|
|
|
//===-- TargetMachine.cpp -------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-04-11 18:25:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LLVM-C part of TargetMachine.h
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/Core.h"
|
|
|
|
#include "llvm-c/Target.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm-c/TargetMachine.h"
|
2015-01-31 19:17:59 +08:00
|
|
|
#include "llvm/Analysis/TargetTransformInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2018-04-12 06:40:42 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
2014-04-30 07:26:49 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-24 07:58:21 +08:00
|
|
|
#include "llvm/Target/CodeGenCWrappers.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2012-04-11 18:25:24 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-07-28 02:27:23 +08:00
|
|
|
static TargetMachine *unwrap(LLVMTargetMachineRef P) {
|
2015-08-27 05:16:29 +08:00
|
|
|
return reinterpret_cast<TargetMachine *>(P);
|
2013-04-23 06:47:22 +08:00
|
|
|
}
|
2015-07-28 02:27:23 +08:00
|
|
|
static Target *unwrap(LLVMTargetRef P) {
|
2013-04-23 06:47:22 +08:00
|
|
|
return reinterpret_cast<Target*>(P);
|
|
|
|
}
|
2015-07-28 02:27:23 +08:00
|
|
|
static LLVMTargetMachineRef wrap(const TargetMachine *P) {
|
2015-08-27 05:16:29 +08:00
|
|
|
return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P));
|
2013-04-23 06:47:22 +08:00
|
|
|
}
|
2015-07-28 02:27:23 +08:00
|
|
|
static LLVMTargetRef wrap(const Target * P) {
|
2013-04-23 06:47:22 +08:00
|
|
|
return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P));
|
|
|
|
}
|
2012-04-11 18:25:24 +08:00
|
|
|
|
|
|
|
LLVMTargetRef LLVMGetFirstTarget() {
|
2015-05-12 06:20:48 +08:00
|
|
|
if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) {
|
2014-04-25 13:30:21 +08:00
|
|
|
return nullptr;
|
2013-10-17 18:25:24 +08:00
|
|
|
}
|
|
|
|
|
2015-05-12 06:20:48 +08:00
|
|
|
const Target *target = &*TargetRegistry::targets().begin();
|
2013-10-17 18:25:24 +08:00
|
|
|
return wrap(target);
|
2012-04-11 18:25:24 +08:00
|
|
|
}
|
|
|
|
LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) {
|
|
|
|
return wrap(unwrap(T)->getNext());
|
|
|
|
}
|
|
|
|
|
2013-11-15 10:51:01 +08:00
|
|
|
LLVMTargetRef LLVMGetTargetFromName(const char *Name) {
|
2013-11-15 10:51:18 +08:00
|
|
|
StringRef NameRef = Name;
|
2016-08-12 11:55:06 +08:00
|
|
|
auto I = find_if(TargetRegistry::targets(),
|
|
|
|
[&](const Target &T) { return T.getName() == NameRef; });
|
2015-05-12 06:20:48 +08:00
|
|
|
return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr;
|
2013-11-06 18:25:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T,
|
|
|
|
char **ErrorMessage) {
|
|
|
|
std::string Error;
|
2015-08-27 05:16:29 +08:00
|
|
|
|
2013-11-06 18:25:18 +08:00
|
|
|
*T = wrap(TargetRegistry::lookupTarget(TripleStr, Error));
|
2015-08-27 05:16:29 +08:00
|
|
|
|
2013-11-06 18:25:18 +08:00
|
|
|
if (!*T) {
|
|
|
|
if (ErrorMessage)
|
|
|
|
*ErrorMessage = strdup(Error.c_str());
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2015-08-27 05:16:29 +08:00
|
|
|
|
2013-11-06 18:25:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-11 18:25:24 +08:00
|
|
|
const char * LLVMGetTargetName(LLVMTargetRef T) {
|
2016-10-01 15:08:23 +08:00
|
|
|
return unwrap(T)->getName();
|
2012-04-11 18:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * LLVMGetTargetDescription(LLVMTargetRef T) {
|
2016-10-01 15:08:23 +08:00
|
|
|
return unwrap(T)->getShortDescription();
|
2012-04-11 18:25:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->hasJIT();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->hasTargetMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) {
|
|
|
|
return unwrap(T)->hasMCAsmBackend();
|
|
|
|
}
|
|
|
|
|
2013-11-15 10:51:12 +08:00
|
|
|
LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T,
|
2017-01-09 21:54:51 +08:00
|
|
|
const char *Triple, const char *CPU, const char *Features,
|
2013-11-15 10:51:12 +08:00
|
|
|
LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc,
|
|
|
|
LLVMCodeModel CodeModel) {
|
2016-05-19 06:04:49 +08:00
|
|
|
Optional<Reloc::Model> RM;
|
2012-04-11 18:25:24 +08:00
|
|
|
switch (Reloc){
|
|
|
|
case LLVMRelocStatic:
|
|
|
|
RM = Reloc::Static;
|
|
|
|
break;
|
|
|
|
case LLVMRelocPIC:
|
|
|
|
RM = Reloc::PIC_;
|
|
|
|
break;
|
|
|
|
case LLVMRelocDynamicNoPic:
|
|
|
|
RM = Reloc::DynamicNoPIC;
|
|
|
|
break;
|
2019-01-03 08:33:44 +08:00
|
|
|
case LLVMRelocROPI:
|
|
|
|
RM = Reloc::ROPI;
|
|
|
|
break;
|
|
|
|
case LLVMRelocRWPI:
|
|
|
|
RM = Reloc::RWPI;
|
|
|
|
break;
|
|
|
|
case LLVMRelocROPI_RWPI:
|
|
|
|
RM = Reloc::ROPI_RWPI;
|
|
|
|
break;
|
2012-04-11 18:25:24 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-08-03 10:16:21 +08:00
|
|
|
bool JIT;
|
|
|
|
Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT);
|
2012-04-11 18:25:24 +08:00
|
|
|
|
2013-05-02 06:58:00 +08:00
|
|
|
CodeGenOpt::Level OL;
|
2012-04-11 18:25:24 +08:00
|
|
|
switch (Level) {
|
|
|
|
case LLVMCodeGenLevelNone:
|
|
|
|
OL = CodeGenOpt::None;
|
|
|
|
break;
|
|
|
|
case LLVMCodeGenLevelLess:
|
|
|
|
OL = CodeGenOpt::Less;
|
|
|
|
break;
|
|
|
|
case LLVMCodeGenLevelAggressive:
|
|
|
|
OL = CodeGenOpt::Aggressive;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
OL = CodeGenOpt::Default;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetOptions opt;
|
2017-08-03 10:16:21 +08:00
|
|
|
return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM,
|
|
|
|
OL, JIT));
|
2012-04-11 18:25:24 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 05:16:29 +08:00
|
|
|
void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); }
|
2012-04-11 18:25:24 +08:00
|
|
|
|
|
|
|
LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) {
|
|
|
|
const Target* target = &(unwrap(T)->getTarget());
|
|
|
|
return wrap(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) {
|
2015-06-16 21:15:50 +08:00
|
|
|
std::string StringRep = unwrap(T)->getTargetTriple().str();
|
2012-04-11 18:25:24 +08:00
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LLVMGetTargetMachineCPU(LLVMTargetMachineRef T) {
|
|
|
|
std::string StringRep = unwrap(T)->getTargetCPU();
|
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
char* LLVMGetTargetMachineFeatureString(LLVMTargetMachineRef T) {
|
|
|
|
std::string StringRep = unwrap(T)->getTargetFeatureString();
|
|
|
|
return strdup(StringRep.c_str());
|
|
|
|
}
|
|
|
|
|
2013-11-06 18:25:18 +08:00
|
|
|
void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T,
|
|
|
|
LLVMBool VerboseAsm) {
|
2015-02-13 05:16:34 +08:00
|
|
|
unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm;
|
2013-11-06 18:25:18 +08:00
|
|
|
}
|
|
|
|
|
2016-02-16 13:11:24 +08:00
|
|
|
LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) {
|
|
|
|
return wrap(new DataLayout(unwrap(T)->createDataLayout()));
|
|
|
|
}
|
|
|
|
|
2013-04-17 07:12:56 +08:00
|
|
|
static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
|
2015-04-15 06:14:34 +08:00
|
|
|
raw_pwrite_stream &OS,
|
2015-04-10 01:16:25 +08:00
|
|
|
LLVMCodeGenFileType codegen,
|
|
|
|
char **ErrorMessage) {
|
2012-04-11 18:25:24 +08:00
|
|
|
TargetMachine* TM = unwrap(T);
|
|
|
|
Module* Mod = unwrap(M);
|
|
|
|
|
2015-02-13 18:01:29 +08:00
|
|
|
legacy::PassManager pass;
|
2012-04-11 18:25:24 +08:00
|
|
|
|
|
|
|
std::string error;
|
|
|
|
|
2015-07-25 00:04:22 +08:00
|
|
|
Mod->setDataLayout(TM->createDataLayout());
|
2012-04-11 18:25:24 +08:00
|
|
|
|
|
|
|
TargetMachine::CodeGenFileType ft;
|
|
|
|
switch (codegen) {
|
|
|
|
case LLVMAssemblyFile:
|
|
|
|
ft = TargetMachine::CGFT_AssemblyFile;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ft = TargetMachine::CGFT_ObjectFile;
|
|
|
|
break;
|
|
|
|
}
|
2018-05-22 04:16:41 +08:00
|
|
|
if (TM->addPassesToEmitFile(pass, OS, nullptr, ft)) {
|
2013-03-11 06:01:44 +08:00
|
|
|
error = "TargetMachine can't emit a file of this type";
|
2012-04-11 18:25:24 +08:00
|
|
|
*ErrorMessage = strdup(error.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pass.run(*Mod);
|
|
|
|
|
2013-04-17 07:12:56 +08:00
|
|
|
OS.flush();
|
2012-04-11 18:25:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-17 07:12:56 +08:00
|
|
|
|
|
|
|
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
|
|
|
|
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
|
2014-08-26 02:16:47 +08:00
|
|
|
std::error_code EC;
|
|
|
|
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
|
|
|
|
if (EC) {
|
|
|
|
*ErrorMessage = strdup(EC.message().c_str());
|
2013-04-17 07:12:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-04-10 05:06:08 +08:00
|
|
|
bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage);
|
2013-04-17 07:12:56 +08:00
|
|
|
dest.flush();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T,
|
|
|
|
LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage,
|
|
|
|
LLVMMemoryBufferRef *OutMemBuf) {
|
2015-04-10 01:16:25 +08:00
|
|
|
SmallString<0> CodeString;
|
|
|
|
raw_svector_ostream OStream(CodeString);
|
2015-04-10 05:06:08 +08:00
|
|
|
bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage);
|
2013-04-17 07:12:56 +08:00
|
|
|
|
2015-04-10 01:16:25 +08:00
|
|
|
StringRef Data = OStream.str();
|
|
|
|
*OutMemBuf =
|
|
|
|
LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), "");
|
2013-04-17 07:12:56 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2013-11-06 18:25:18 +08:00
|
|
|
|
|
|
|
char *LLVMGetDefaultTargetTriple(void) {
|
|
|
|
return strdup(sys::getDefaultTargetTriple().c_str());
|
|
|
|
}
|
2014-01-24 03:23:28 +08:00
|
|
|
|
2018-07-17 18:57:39 +08:00
|
|
|
char *LLVMNormalizeTargetTriple(const char* triple) {
|
|
|
|
return strdup(Triple::normalize(StringRef(triple)).c_str());
|
|
|
|
}
|
|
|
|
|
2018-04-12 06:40:42 +08:00
|
|
|
char *LLVMGetHostCPUName(void) {
|
|
|
|
return strdup(sys::getHostCPUName().data());
|
|
|
|
}
|
|
|
|
|
|
|
|
char *LLVMGetHostCPUFeatures(void) {
|
|
|
|
SubtargetFeatures Features;
|
|
|
|
StringMap<bool> HostFeatures;
|
|
|
|
|
|
|
|
if (sys::getHostCPUFeatures(HostFeatures))
|
|
|
|
for (auto &F : HostFeatures)
|
|
|
|
Features.AddFeature(F.first(), F.second);
|
|
|
|
|
|
|
|
return strdup(Features.getString().c_str());
|
|
|
|
}
|
|
|
|
|
2014-01-24 03:23:28 +08:00
|
|
|
void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) {
|
2015-02-01 20:26:09 +08:00
|
|
|
unwrap(PM)->add(
|
|
|
|
createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis()));
|
2014-01-24 03:23:28 +08:00
|
|
|
}
|