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