LCOV - code coverage report
Current view: top level - lib/Target - TargetMachine.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 71 76 93.4 %
Date: 2018-02-18 03:11:45 Functions: 13 15 86.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- TargetMachine.cpp - General Target Information ---------------------==//
       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 describes the general parts of a Target machine.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Target/TargetMachine.h"
      15             : #include "llvm/Analysis/TargetTransformInfo.h"
      16             : #include "llvm/CodeGen/TargetLoweringObjectFile.h"
      17             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      18             : #include "llvm/IR/Function.h"
      19             : #include "llvm/IR/GlobalAlias.h"
      20             : #include "llvm/IR/GlobalValue.h"
      21             : #include "llvm/IR/GlobalVariable.h"
      22             : #include "llvm/IR/LegacyPassManager.h"
      23             : #include "llvm/IR/Mangler.h"
      24             : #include "llvm/MC/MCAsmInfo.h"
      25             : #include "llvm/MC/MCContext.h"
      26             : #include "llvm/MC/MCInstrInfo.h"
      27             : #include "llvm/MC/MCSectionMachO.h"
      28             : #include "llvm/MC/MCTargetOptions.h"
      29             : #include "llvm/MC/SectionKind.h"
      30             : using namespace llvm;
      31             : 
      32             : //---------------------------------------------------------------------------
      33             : // TargetMachine Class
      34             : //
      35             : 
      36       32653 : TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
      37             :                              const Triple &TT, StringRef CPU, StringRef FS,
      38       32653 :                              const TargetOptions &Options)
      39             :     : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
      40             :       TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr),
      41       65306 :       RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) {
      42       32653 : }
      43             : 
      44       29968 : TargetMachine::~TargetMachine() {
      45       29968 :   delete AsmInfo;
      46       59933 :   delete MRI;
      47       29968 :   delete MII;
      48       29968 :   delete STI;
      49       29968 : }
      50             : 
      51     2882571 : bool TargetMachine::isPositionIndependent() const {
      52     2882571 :   return getRelocationModel() == Reloc::PIC_;
      53             : }
      54             : 
      55             : /// \brief Reset the target options based on the function's attributes.
      56             : // FIXME: This function needs to go away for a number of reasons:
      57             : // a) global state on the TargetMachine is terrible in general,
      58             : // b) these target options should be passed only on the function
      59             : //    and not on the TargetMachine (via TargetOptions) at all.
      60      222847 : void TargetMachine::resetTargetOptions(const Function &F) const {
      61             : #define RESET_OPTION(X, Y)                                                     \
      62             :   do {                                                                         \
      63             :     if (F.hasFnAttribute(Y))                                                   \
      64             :       Options.X = (F.getFnAttribute(Y).getValueAsString() == "true");          \
      65             :     else                                                                       \
      66             :       Options.X = DefaultOptions.X;                                            \
      67             :   } while (0)
      68             : 
      69      314821 :   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
      70      312655 :   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
      71      313323 :   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
      72      309299 :   RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
      73      308933 :   RESET_OPTION(NoTrappingFPMath, "no-trapping-math");
      74             : 
      75             :   StringRef Denormal =
      76      222847 :     F.getFnAttribute("denormal-fp-math").getValueAsString();
      77             :   if (Denormal == "ieee")
      78          10 :     Options.FPDenormalMode = FPDenormal::IEEE;
      79             :   else if (Denormal == "preserve-sign")
      80          10 :     Options.FPDenormalMode = FPDenormal::PreserveSign;
      81             :   else if (Denormal == "positive-zero")
      82           3 :     Options.FPDenormalMode = FPDenormal::PositiveZero;
      83             :   else
      84      222824 :     Options.FPDenormalMode = DefaultOptions.FPDenormalMode;
      85      222847 : }
      86             : 
      87             : /// Returns the code generation relocation model. The choices are static, PIC,
      88             : /// and dynamic-no-pic.
      89     3406742 : Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
      90             : 
      91             : /// Returns the code model. The choices are small, kernel, medium, large, and
      92             : /// target default.
      93     7278453 : CodeModel::Model TargetMachine::getCodeModel() const { return CMModel; }
      94             : 
      95             : /// Get the IR-specified TLS model for Var.
      96             : static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
      97         446 :   switch (GV->getThreadLocalMode()) {
      98           0 :   case GlobalVariable::NotThreadLocal:
      99           0 :     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
     100             :     break;
     101             :   case GlobalVariable::GeneralDynamicTLSModel:
     102             :     return TLSModel::GeneralDynamic;
     103          77 :   case GlobalVariable::LocalDynamicTLSModel:
     104             :     return TLSModel::LocalDynamic;
     105          55 :   case GlobalVariable::InitialExecTLSModel:
     106             :     return TLSModel::InitialExec;
     107          30 :   case GlobalVariable::LocalExecTLSModel:
     108             :     return TLSModel::LocalExec;
     109             :   }
     110           0 :   llvm_unreachable("invalid TLS model");
     111             : }
     112             : 
     113     2484487 : bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
     114             :                                          const GlobalValue *GV) const {
     115             :   // If the IR producer requested that this GV be treated as dso local, obey.
     116     4962701 :   if (GV && GV->isDSOLocal())
     117             :     return true;
     118             : 
     119             :   // According to the llvm language reference, we should be able to just return
     120             :   // false in here if we have a GV, as we know it is dso_preemptable.
     121             :   // At this point in time, the various IR producers have not been transitioned
     122             :   // to always produce a dso_local when it is possible to do so. As a result we
     123             :   // still have some pre-dso_local logic in here to improve the quality of the
     124             :   // generated code:
     125             : 
     126      168343 :   Reloc::Model RM = getRelocationModel();
     127             :   const Triple &TT = getTargetTriple();
     128             : 
     129             :   // DLLImport explicitly marks the GV as external.
     130      330413 :   if (GV && GV->hasDLLImportStorageClass())
     131             :     return false;
     132             : 
     133             :   // Every other GV is local on COFF.
     134             :   // Make an exception for windows OS in the triple: Some firmwares builds use
     135             :   // *-win32-macho triples. This (accidentally?) produced windows relocations
     136             :   // without GOT tables in older clang versions; Keep this behaviour.
     137      168256 :   if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
     138             :     return true;
     139             : 
     140             :   // Most PIC code sequences that assume that a symbol is local cannot
     141             :   // produce a 0 if it turns out the symbol is undefined. While this
     142             :   // is ABI and relocation depended, it seems worth it to handle it
     143             :   // here.
     144      286714 :   if (GV && isPositionIndependent() && GV->hasExternalWeakLinkage())
     145             :     return false;
     146             : 
     147      325255 :   if (GV && !GV->hasDefaultVisibility())
     148             :     return true;
     149             : 
     150      165628 :   if (TT.isOSBinFormatMachO()) {
     151       15603 :     if (RM == Reloc::Static)
     152             :       return true;
     153       14672 :     return GV && GV->isStrongDefinitionForLinker();
     154             :   }
     155             : 
     156             :   assert(TT.isOSBinFormatELF());
     157             :   assert(RM != Reloc::DynamicNoPIC);
     158             : 
     159             :   bool IsExecutable =
     160      150025 :       RM == Reloc::Static || M.getPIELevel() != PIELevel::Default;
     161             :   if (IsExecutable) {
     162             :     // If the symbol is defined, it cannot be preempted.
     163       71239 :     if (GV && !GV->isDeclarationForLinker())
     164             :       return true;
     165             : 
     166             :     // A symbol marked nonlazybind should not be accessed with a plt. If the
     167             :     // symbol turns out to be external, the linker will convert a direct
     168             :     // access to an access via the plt, so don't assume it is local.
     169             :     const Function *F = dyn_cast_or_null<Function>(GV);
     170        8881 :     if (F && F->hasFnAttribute(Attribute::NonLazyBind))
     171             :       return false;
     172             : 
     173       47033 :     bool IsTLS = GV && GV->isThreadLocal();
     174             :     bool IsAccessViaCopyRelocs =
     175       25478 :         Options.MCOptions.MCPIECopyRelocations && GV && isa<GlobalVariable>(GV);
     176             :     Triple::ArchType Arch = TT.getArch();
     177             :     bool IsPPC =
     178       25478 :         Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le;
     179             :     // Check if we can use copy relocations. PowerPC has no copy relocations.
     180       25478 :     if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs))
     181             :       return true;
     182             :   }
     183             : 
     184             :   // ELF supports preemption of other symbols.
     185             :   return false;
     186             : }
     187             : 
     188         446 : TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
     189         446 :   bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
     190         446 :   Reloc::Model RM = getRelocationModel();
     191         446 :   bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
     192         446 :   bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV);
     193             : 
     194             :   TLSModel::Model Model;
     195         446 :   if (IsSharedLibrary) {
     196         238 :     if (IsLocal)
     197             :       Model = TLSModel::LocalDynamic;
     198             :     else
     199             :       Model = TLSModel::GeneralDynamic;
     200             :   } else {
     201         208 :     if (IsLocal)
     202             :       Model = TLSModel::LocalExec;
     203             :     else
     204             :       Model = TLSModel::InitialExec;
     205             :   }
     206             : 
     207             :   // If the user specified a more specific model, use that.
     208             :   TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
     209         446 :   if (SelectedModel > Model)
     210             :     return SelectedModel;
     211             : 
     212         356 :   return Model;
     213             : }
     214             : 
     215             : /// Returns the optimization level: None, Less, Default, or Aggressive.
     216     2287203 : CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; }
     217             : 
     218         222 : void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; }
     219             : 
     220           0 : TargetTransformInfo TargetMachine::getTargetTransformInfo(const Function &F) {
     221           0 :   return TargetTransformInfo(F.getParent()->getDataLayout());
     222             : }
     223             : 
     224     1351686 : void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
     225             :                                       const GlobalValue *GV, Mangler &Mang,
     226             :                                       bool MayAlwaysUsePrivate) const {
     227     2638529 :   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
     228             :     // Simple case: If GV is not private, it is not important to find out if
     229             :     // private labels are legal in this case or not.
     230     1214173 :     Mang.getNameWithPrefix(Name, GV, false);
     231     1214173 :     return;
     232             :   }
     233      137513 :   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
     234      137513 :   TLOF->getNameWithPrefix(Name, GV, *this);
     235             : }
     236             : 
     237      480340 : MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const {
     238      480340 :   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
     239             :   SmallString<128> NameStr;
     240      480340 :   getNameWithPrefix(NameStr, GV, TLOF->getMangler());
     241      960680 :   return TLOF->getContext().getOrCreateSymbol(NameStr);
     242             : }
     243             : 
     244       54884 : TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
     245             :   // Since Analysis can't depend on Target, use a std::function to invert the
     246             :   // dependency.
     247             :   return TargetIRAnalysis(
     248     2350436 :       [this](const Function &F) { return this->getTargetTransformInfo(F); });
     249             : }

Generated by: LCOV version 1.13