diff options
Diffstat (limited to 'llvm/lib/CodeGen')
-rw-r--r-- | llvm/lib/CodeGen/CMakeLists.txt | 3 | ||||
-rw-r--r-- | llvm/lib/CodeGen/LLVMTargetMachine.cpp (renamed from llvm/lib/CodeGen/TargetMachine.cpp) | 225 | ||||
-rw-r--r-- | llvm/lib/CodeGen/MachineVerifier.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/CodeGen/TargetMachineC.cpp | 244 | ||||
-rw-r--r-- | llvm/lib/CodeGen/TargetPassConfig.cpp | 4 |
5 files changed, 39 insertions, 443 deletions
diff --git a/llvm/lib/CodeGen/CMakeLists.txt b/llvm/lib/CodeGen/CMakeLists.txt index f4949a7abe7..7ec7fda4e44 100644 --- a/llvm/lib/CodeGen/CMakeLists.txt +++ b/llvm/lib/CodeGen/CMakeLists.txt @@ -53,6 +53,7 @@ add_llvm_library(LLVMCodeGen LiveRegUnits.cpp LiveStackAnalysis.cpp LiveVariables.cpp + LLVMTargetMachine.cpp LocalStackSlotAllocation.cpp LowLevelType.cpp LowerEmuTLS.cpp @@ -141,8 +142,6 @@ add_llvm_library(LLVMCodeGen TargetInstrInfo.cpp TargetLoweringBase.cpp TargetLoweringObjectFileImpl.cpp - TargetMachine.cpp - TargetMachineC.cpp TargetOptionsImpl.cpp TargetPassConfig.cpp TargetRegisterInfo.cpp diff --git a/llvm/lib/CodeGen/TargetMachine.cpp b/llvm/lib/CodeGen/LLVMTargetMachine.cpp index e789273753f..e829409f097 100644 --- a/llvm/lib/CodeGen/TargetMachine.cpp +++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp @@ -1,4 +1,4 @@ -//===-- TargetMachine.cpp - Implement the TargetMachine class -------------===// +//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// // // The LLVM Compiler Infrastructure // @@ -7,12 +7,10 @@ // //===----------------------------------------------------------------------===// // -/// \file Implements the TargetMachine class. +// This file implements the LLVMTargetMachine class. // //===----------------------------------------------------------------------===// -#include "llvm/Target/TargetMachine.h" - #include "llvm/Analysis/Passes.h" #include "llvm/CodeGen/AsmPrinter.h" #include "llvm/CodeGen/BasicTTIImpl.h" @@ -21,7 +19,6 @@ #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/LegacyPassManager.h" -#include "llvm/IR/Mangler.h" #include "llvm/IR/Verifier.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmInfo.h" @@ -35,187 +32,12 @@ #include "llvm/Support/FormattedStream.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Target/TargetLoweringObjectFile.h" +#include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Transforms/Scalar.h" using namespace llvm; -TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString, - const Triple &TT, StringRef CPU, StringRef FS, - const TargetOptions &Options, Reloc::Model RM, - CodeModel::Model CM, CodeGenOpt::Level OL) - : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU), - TargetFS(FS), RM(RM), CMModel(CM), OptLevel(OL), AsmInfo(nullptr), - MRI(nullptr), MII(nullptr), STI(nullptr), RequireStructuredCFG(false), - DefaultOptions(Options), Options(Options) { -} - -TargetMachine::~TargetMachine() { - delete AsmInfo; - delete MRI; - delete MII; - delete STI; -} - -bool TargetMachine::isPositionIndependent() const { - return getRelocationModel() == Reloc::PIC_; -} - -// FIXME: This function needs to go away for a number of reasons: -// a) global state on the TargetMachine is terrible in general, -// b) these target options should be passed only on the function -// and not on the TargetMachine (via TargetOptions) at all. -void TargetMachine::resetTargetOptions(const Function &F) const { -#define RESET_OPTION(X, Y) \ - do { \ - if (F.hasFnAttribute(Y)) \ - Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \ - else \ - Options.X = DefaultOptions.X; \ - } while (0) - - RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); - RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); - RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); - RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math"); - RESET_OPTION(NoTrappingFPMath, "no-trapping-math"); - - StringRef Denormal = - F.getFnAttribute("denormal-fp-math").getValueAsString(); - if (Denormal == "ieee") - Options.FPDenormalMode = FPDenormal::IEEE; - else if (Denormal == "preserve-sign") - Options.FPDenormalMode = FPDenormal::PreserveSign; - else if (Denormal == "positive-zero") - Options.FPDenormalMode = FPDenormal::PositiveZero; - else - Options.FPDenormalMode = DefaultOptions.FPDenormalMode; -} - -Reloc::Model TargetMachine::getRelocationModel() const { return RM; } - -CodeModel::Model TargetMachine::getCodeModel() const { return CMModel; } - -/// Get the IR-specified TLS model for Var. -static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { - switch (GV->getThreadLocalMode()) { - case GlobalVariable::NotThreadLocal: - llvm_unreachable("getSelectedTLSModel for non-TLS variable"); - break; - case GlobalVariable::GeneralDynamicTLSModel: - return TLSModel::GeneralDynamic; - case GlobalVariable::LocalDynamicTLSModel: - return TLSModel::LocalDynamic; - case GlobalVariable::InitialExecTLSModel: - return TLSModel::InitialExec; - case GlobalVariable::LocalExecTLSModel: - return TLSModel::LocalExec; - } - llvm_unreachable("invalid TLS model"); -} - -bool TargetMachine::shouldAssumeDSOLocal(const Module &M, - const GlobalValue *GV) const { - Reloc::Model RM = getRelocationModel(); - const Triple &TT = getTargetTriple(); - - // DLLImport explicitly marks the GV as external. - if (GV && GV->hasDLLImportStorageClass()) - return false; - - // Every other GV is local on COFF. - // Make an exception for windows OS in the triple: Some firmwares builds use - // *-win32-macho triples. This (accidentally?) produced windows relocations - // without GOT tables in older clang versions; Keep this behaviour. - if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO())) - return true; - - if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility())) - return true; - - if (TT.isOSBinFormatMachO()) { - if (RM == Reloc::Static) - return true; - return GV && GV->isStrongDefinitionForLinker(); - } - - assert(TT.isOSBinFormatELF()); - assert(RM != Reloc::DynamicNoPIC); - - bool IsExecutable = - RM == Reloc::Static || M.getPIELevel() != PIELevel::Default; - if (IsExecutable) { - // If the symbol is defined, it cannot be preempted. - if (GV && !GV->isDeclarationForLinker()) - return true; - - bool IsTLS = GV && GV->isThreadLocal(); - bool IsAccessViaCopyRelocs = Options.MCOptions.MCPIECopyRelocations && GV && - isa<GlobalVariable>(GV) && - !GV->hasExternalWeakLinkage(); - Triple::ArchType Arch = TT.getArch(); - bool IsPPC = - Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le; - // Check if we can use copy relocations. PowerPC has no copy relocations. - if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs)) - return true; - } - - // ELF supports preemption of other symbols. - return false; -} - -TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { - bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default; - Reloc::Model RM = getRelocationModel(); - bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE; - bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV); - - TLSModel::Model Model; - if (IsSharedLibrary) { - if (IsLocal) - Model = TLSModel::LocalDynamic; - else - Model = TLSModel::GeneralDynamic; - } else { - if (IsLocal) - Model = TLSModel::LocalExec; - else - Model = TLSModel::InitialExec; - } - - // If the user specified a more specific model, use that. - TLSModel::Model SelectedModel = getSelectedTLSModel(GV); - if (SelectedModel > Model) - return SelectedModel; - - return Model; -} - -CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; } - -void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; } - -void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, - const GlobalValue *GV, Mangler &Mang, - bool MayAlwaysUsePrivate) const { - if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { - // Simple case: If GV is not private, it is not important to find out if - // private labels are legal in this case or not. - Mang.getNameWithPrefix(Name, GV, false); - return; - } - const TargetLoweringObjectFile *TLOF = getObjFileLowering(); - TLOF->getNameWithPrefix(Name, GV, *this); -} - -MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const { - const TargetLoweringObjectFile *TLOF = getObjFileLowering(); - SmallString<128> NameStr; - getNameWithPrefix(NameStr, GV, TLOF->getMangler()); - return TLOF->getContext().getOrCreateSymbol(NameStr); -} - -void TargetMachine::initAsmInfo() { +void LLVMTargetMachine::initAsmInfo() { MRI = TheTarget.createMCRegInfo(getTargetTriple().str()); MII = TheTarget.createMCInstrInfo(); // FIXME: Having an MCSubtargetInfo on the target machine is a hack due @@ -250,7 +72,19 @@ void TargetMachine::initAsmInfo() { AsmInfo = TmpAsmInfo; } -TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { +LLVMTargetMachine::LLVMTargetMachine(const Target &T, + StringRef DataLayoutString, + const Triple &TT, StringRef CPU, + StringRef FS, const TargetOptions &Options, + Reloc::Model RM, CodeModel::Model CM, + CodeGenOpt::Level OL) + : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { + this->RM = RM; + this->CMModel = CM; + this->OptLevel = OL; +} + +TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() { return TargetIRAnalysis([this](const Function &F) { return TargetTransformInfo(BasicTTIImpl(this, F)); }); @@ -258,7 +92,7 @@ TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { /// addPassesToX helper drives creation and initialization of TargetPassConfig. static MCContext * -addPassesToGenerateCode(TargetMachine *TM, PassManagerBase &PM, +addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, bool DisableVerify, bool &WillCompleteCodeGenPipeline, raw_pwrite_stream &Out, MachineModuleInfo *MMI) { // Targets may override createPassConfig to provide a target-specific @@ -282,7 +116,7 @@ addPassesToGenerateCode(TargetMachine *TM, PassManagerBase &PM, return &MMI->getContext(); } -bool TargetMachine::addAsmPrinter(PassManagerBase &PM, +bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType, MCContext &Context) { if (Options.MCOptions.MCSaveTempLabels) @@ -354,11 +188,11 @@ bool TargetMachine::addAsmPrinter(PassManagerBase &PM, return false; } -bool TargetMachine::addPassesToEmitFile(PassManagerBase &PM, - raw_pwrite_stream &Out, - CodeGenFileType FileType, - bool DisableVerify, - MachineModuleInfo *MMI) { +bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, + raw_pwrite_stream &Out, + CodeGenFileType FileType, + bool DisableVerify, + MachineModuleInfo *MMI) { // Add common CodeGen passes. bool WillCompleteCodeGenPipeline = true; MCContext *Context = addPassesToGenerateCode( @@ -373,9 +207,14 @@ bool TargetMachine::addPassesToEmitFile(PassManagerBase &PM, return false; } -bool TargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, - raw_pwrite_stream &Out, - bool DisableVerify) { +/// addPassesToEmitMC - Add passes to the specified pass manager to get +/// machine code emitted with the MCJIT. This method returns true if machine +/// code is not supported. It fills the MCContext Ctx pointer which can be +/// used to build custom MCStreamer. +/// +bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, + raw_pwrite_stream &Out, + bool DisableVerify) { // Add common CodeGen passes. bool WillCompleteCodeGenPipeline = true; Ctx = addPassesToGenerateCode(this, PM, DisableVerify, diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp index 3cfc800f0b1..0bd5c56871c 100644 --- a/llvm/lib/CodeGen/MachineVerifier.cpp +++ b/llvm/lib/CodeGen/MachineVerifier.cpp @@ -17,8 +17,10 @@ // Register live intervals: Registers must be defined only once, and must be // defined before use. // -// The machine code verifier is enabled with the command-line option -// -verify-machineinstrs. +// The machine code verifier is enabled from LLVMTargetMachine.cpp with the +// command-line option -verify-machineinstrs, or by defining the environment +// variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive +// the verifier errors. //===----------------------------------------------------------------------===// #include "llvm/ADT/BitVector.h" diff --git a/llvm/lib/CodeGen/TargetMachineC.cpp b/llvm/lib/CodeGen/TargetMachineC.cpp deleted file mode 100644 index 210375ff828..00000000000 --- a/llvm/lib/CodeGen/TargetMachineC.cpp +++ /dev/null @@ -1,244 +0,0 @@ -//===-- TargetMachine.cpp -------------------------------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the LLVM-C part of TargetMachine.h -// -//===----------------------------------------------------------------------===// - -#include "llvm-c/Core.h" -#include "llvm-c/Target.h" -#include "llvm-c/TargetMachine.h" -#include "llvm/Analysis/TargetTransformInfo.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/LegacyPassManager.h" -#include "llvm/IR/Module.h" -#include "llvm/Support/CodeGenCWrappers.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/FormattedStream.h" -#include "llvm/Support/Host.h" -#include "llvm/Support/TargetRegistry.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Target/TargetSubtargetInfo.h" -#include <cassert> -#include <cstdlib> -#include <cstring> - -using namespace llvm; - -static TargetMachine *unwrap(LLVMTargetMachineRef P) { - return reinterpret_cast<TargetMachine *>(P); -} -static Target *unwrap(LLVMTargetRef P) { - return reinterpret_cast<Target*>(P); -} -static LLVMTargetMachineRef wrap(const TargetMachine *P) { - return reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine *>(P)); -} -static LLVMTargetRef wrap(const Target * P) { - return reinterpret_cast<LLVMTargetRef>(const_cast<Target*>(P)); -} - -LLVMTargetRef LLVMGetFirstTarget() { - if (TargetRegistry::targets().begin() == TargetRegistry::targets().end()) { - return nullptr; - } - - const Target *target = &*TargetRegistry::targets().begin(); - return wrap(target); -} -LLVMTargetRef LLVMGetNextTarget(LLVMTargetRef T) { - return wrap(unwrap(T)->getNext()); -} - -LLVMTargetRef LLVMGetTargetFromName(const char *Name) { - StringRef NameRef = Name; - auto I = find_if(TargetRegistry::targets(), - [&](const Target &T) { return T.getName() == NameRef; }); - return I != TargetRegistry::targets().end() ? wrap(&*I) : nullptr; -} - -LLVMBool LLVMGetTargetFromTriple(const char* TripleStr, LLVMTargetRef *T, - char **ErrorMessage) { - std::string Error; - - *T = wrap(TargetRegistry::lookupTarget(TripleStr, Error)); - - if (!*T) { - if (ErrorMessage) - *ErrorMessage = strdup(Error.c_str()); - - return 1; - } - - return 0; -} - -const char * LLVMGetTargetName(LLVMTargetRef T) { - return unwrap(T)->getName(); -} - -const char * LLVMGetTargetDescription(LLVMTargetRef T) { - return unwrap(T)->getShortDescription(); -} - -LLVMBool LLVMTargetHasJIT(LLVMTargetRef T) { - return unwrap(T)->hasJIT(); -} - -LLVMBool LLVMTargetHasTargetMachine(LLVMTargetRef T) { - return unwrap(T)->hasTargetMachine(); -} - -LLVMBool LLVMTargetHasAsmBackend(LLVMTargetRef T) { - return unwrap(T)->hasMCAsmBackend(); -} - -LLVMTargetMachineRef LLVMCreateTargetMachine(LLVMTargetRef T, - const char *Triple, const char *CPU, const char *Features, - LLVMCodeGenOptLevel Level, LLVMRelocMode Reloc, - LLVMCodeModel CodeModel) { - Optional<Reloc::Model> RM; - switch (Reloc){ - case LLVMRelocStatic: - RM = Reloc::Static; - break; - case LLVMRelocPIC: - RM = Reloc::PIC_; - break; - case LLVMRelocDynamicNoPic: - RM = Reloc::DynamicNoPIC; - break; - default: - break; - } - - bool JIT; - Optional<CodeModel::Model> CM = unwrap(CodeModel, JIT); - - CodeGenOpt::Level OL; - 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; - return wrap(unwrap(T)->createTargetMachine(Triple, CPU, Features, opt, RM, CM, - OL, JIT)); -} - -void LLVMDisposeTargetMachine(LLVMTargetMachineRef T) { delete unwrap(T); } - -LLVMTargetRef LLVMGetTargetMachineTarget(LLVMTargetMachineRef T) { - const Target* target = &(unwrap(T)->getTarget()); - return wrap(target); -} - -char* LLVMGetTargetMachineTriple(LLVMTargetMachineRef T) { - std::string StringRep = unwrap(T)->getTargetTriple().str(); - 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()); -} - -void LLVMSetTargetMachineAsmVerbosity(LLVMTargetMachineRef T, - LLVMBool VerboseAsm) { - unwrap(T)->Options.MCOptions.AsmVerbose = VerboseAsm; -} - -LLVMTargetDataRef LLVMCreateTargetDataLayout(LLVMTargetMachineRef T) { - return wrap(new DataLayout(unwrap(T)->createDataLayout())); -} - -static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M, - raw_pwrite_stream &OS, - LLVMCodeGenFileType codegen, - char **ErrorMessage) { - TargetMachine* TM = unwrap(T); - Module* Mod = unwrap(M); - - legacy::PassManager pass; - - std::string error; - - Mod->setDataLayout(TM->createDataLayout()); - - TargetMachine::CodeGenFileType ft; - switch (codegen) { - case LLVMAssemblyFile: - ft = TargetMachine::CGFT_AssemblyFile; - break; - default: - ft = TargetMachine::CGFT_ObjectFile; - break; - } - if (TM->addPassesToEmitFile(pass, OS, ft)) { - error = "TargetMachine can't emit a file of this type"; - *ErrorMessage = strdup(error.c_str()); - return true; - } - - pass.run(*Mod); - - OS.flush(); - return false; -} - -LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M, - char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) { - std::error_code EC; - raw_fd_ostream dest(Filename, EC, sys::fs::F_None); - if (EC) { - *ErrorMessage = strdup(EC.message().c_str()); - return true; - } - bool Result = LLVMTargetMachineEmit(T, M, dest, codegen, ErrorMessage); - dest.flush(); - return Result; -} - -LLVMBool LLVMTargetMachineEmitToMemoryBuffer(LLVMTargetMachineRef T, - LLVMModuleRef M, LLVMCodeGenFileType codegen, char** ErrorMessage, - LLVMMemoryBufferRef *OutMemBuf) { - SmallString<0> CodeString; - raw_svector_ostream OStream(CodeString); - bool Result = LLVMTargetMachineEmit(T, M, OStream, codegen, ErrorMessage); - - StringRef Data = OStream.str(); - *OutMemBuf = - LLVMCreateMemoryBufferWithMemoryRangeCopy(Data.data(), Data.size(), ""); - return Result; -} - -char *LLVMGetDefaultTargetTriple(void) { - return strdup(sys::getDefaultTargetTriple().c_str()); -} - -void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM) { - unwrap(PM)->add( - createTargetTransformInfoWrapperPass(unwrap(T)->getTargetIRAnalysis())); -} diff --git a/llvm/lib/CodeGen/TargetPassConfig.cpp b/llvm/lib/CodeGen/TargetPassConfig.cpp index 0f6d152cee9..c5101b1ecfc 100644 --- a/llvm/lib/CodeGen/TargetPassConfig.cpp +++ b/llvm/lib/CodeGen/TargetPassConfig.cpp @@ -355,7 +355,7 @@ void TargetPassConfig::setStartStopPasses() { // Out of line constructor provides default values for pass options and // registers all common codegen passes. -TargetPassConfig::TargetPassConfig(TargetMachine &TM, PassManagerBase &pm) +TargetPassConfig::TargetPassConfig(LLVMTargetMachine &TM, PassManagerBase &pm) : ImmutablePass(ID), PM(&pm), TM(&TM) { Impl = new PassConfigImpl(); @@ -408,7 +408,7 @@ void TargetPassConfig::insertPass(AnalysisID TargetPassID, /// addPassToEmitX methods for generating a pipeline of CodeGen passes. /// /// Targets may override this to extend TargetPassConfig. -TargetPassConfig *TargetMachine::createPassConfig(PassManagerBase &PM) { +TargetPassConfig *LLVMTargetMachine::createPassConfig(PassManagerBase &PM) { return new TargetPassConfig(*this, PM); } |