Home | History | Annotate | Download | only in Target
      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/CodeGen/MachineFunction.h"
     16 #include "llvm/IR/Function.h"
     17 #include "llvm/IR/GlobalAlias.h"
     18 #include "llvm/IR/GlobalValue.h"
     19 #include "llvm/IR/GlobalVariable.h"
     20 #include "llvm/MC/MCAsmInfo.h"
     21 #include "llvm/MC/MCCodeGenInfo.h"
     22 #include "llvm/Support/CommandLine.h"
     23 using namespace llvm;
     24 
     25 //---------------------------------------------------------------------------
     26 // Command-line options that tend to be useful on more than one back-end.
     27 //
     28 
     29 namespace llvm {
     30   bool HasDivModLibcall;
     31   bool AsmVerbosityDefault(false);
     32 }
     33 
     34 static cl::opt<bool>
     35 DataSections("fdata-sections",
     36   cl::desc("Emit data into separate sections"),
     37   cl::init(false));
     38 static cl::opt<bool>
     39 FunctionSections("ffunction-sections",
     40   cl::desc("Emit functions into separate sections"),
     41   cl::init(false));
     42 
     43 //---------------------------------------------------------------------------
     44 // TargetMachine Class
     45 //
     46 
     47 TargetMachine::TargetMachine(const Target &T,
     48                              StringRef TT, StringRef CPU, StringRef FS,
     49                              const TargetOptions &Options)
     50   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
     51     CodeGenInfo(0), AsmInfo(0),
     52     MCRelaxAll(false),
     53     MCNoExecStack(false),
     54     MCSaveTempLabels(false),
     55     MCUseLoc(true),
     56     MCUseCFI(true),
     57     MCUseDwarfDirectory(false),
     58     Options(Options) {
     59 }
     60 
     61 TargetMachine::~TargetMachine() {
     62   delete CodeGenInfo;
     63   delete AsmInfo;
     64 }
     65 
     66 /// \brief Reset the target options based on the function's attributes.
     67 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
     68   const Function *F = MF->getFunction();
     69   TargetOptions &TO = MF->getTarget().Options;
     70 
     71 #define RESET_OPTION(X, Y)                                              \
     72   do {                                                                  \
     73     if (F->hasFnAttribute(Y))                                           \
     74       TO.X =                                                            \
     75         (F->getAttributes().                                            \
     76            getAttribute(AttributeSet::FunctionIndex,                    \
     77                         Y).getValueAsString() == "true");               \
     78   } while (0)
     79 
     80   RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
     81   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
     82   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
     83   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
     84   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
     85   RESET_OPTION(UseSoftFloat, "use-soft-float");
     86   RESET_OPTION(DisableTailCalls, "disable-tail-calls");
     87 }
     88 
     89 /// getRelocationModel - Returns the code generation relocation model. The
     90 /// choices are static, PIC, and dynamic-no-pic, and target default.
     91 Reloc::Model TargetMachine::getRelocationModel() const {
     92   if (!CodeGenInfo)
     93     return Reloc::Default;
     94   return CodeGenInfo->getRelocationModel();
     95 }
     96 
     97 /// getCodeModel - Returns the code model. The choices are small, kernel,
     98 /// medium, large, and target default.
     99 CodeModel::Model TargetMachine::getCodeModel() const {
    100   if (!CodeGenInfo)
    101     return CodeModel::Default;
    102   return CodeGenInfo->getCodeModel();
    103 }
    104 
    105 /// Get the IR-specified TLS model for Var.
    106 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
    107   switch (Var->getThreadLocalMode()) {
    108   case GlobalVariable::NotThreadLocal:
    109     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
    110     break;
    111   case GlobalVariable::GeneralDynamicTLSModel:
    112     return TLSModel::GeneralDynamic;
    113   case GlobalVariable::LocalDynamicTLSModel:
    114     return TLSModel::LocalDynamic;
    115   case GlobalVariable::InitialExecTLSModel:
    116     return TLSModel::InitialExec;
    117   case GlobalVariable::LocalExecTLSModel:
    118     return TLSModel::LocalExec;
    119   }
    120   llvm_unreachable("invalid TLS model");
    121 }
    122 
    123 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
    124   // If GV is an alias then use the aliasee for determining
    125   // thread-localness.
    126   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
    127     GV = GA->resolveAliasedGlobal(false);
    128   const GlobalVariable *Var = cast<GlobalVariable>(GV);
    129 
    130   bool isLocal = Var->hasLocalLinkage();
    131   bool isDeclaration = Var->isDeclaration();
    132   bool isPIC = getRelocationModel() == Reloc::PIC_;
    133   bool isPIE = Options.PositionIndependentExecutable;
    134   // FIXME: what should we do for protected and internal visibility?
    135   // For variables, is internal different from hidden?
    136   bool isHidden = Var->hasHiddenVisibility();
    137 
    138   TLSModel::Model Model;
    139   if (isPIC && !isPIE) {
    140     if (isLocal || isHidden)
    141       Model = TLSModel::LocalDynamic;
    142     else
    143       Model = TLSModel::GeneralDynamic;
    144   } else {
    145     if (!isDeclaration || isHidden)
    146       Model = TLSModel::LocalExec;
    147     else
    148       Model = TLSModel::InitialExec;
    149   }
    150 
    151   // If the user specified a more specific model, use that.
    152   TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
    153   if (SelectedModel > Model)
    154     return SelectedModel;
    155 
    156   return Model;
    157 }
    158 
    159 /// getOptLevel - Returns the optimization level: None, Less,
    160 /// Default, or Aggressive.
    161 CodeGenOpt::Level TargetMachine::getOptLevel() const {
    162   if (!CodeGenInfo)
    163     return CodeGenOpt::Default;
    164   return CodeGenInfo->getOptLevel();
    165 }
    166 
    167 bool TargetMachine::getAsmVerbosityDefault() {
    168   return AsmVerbosityDefault;
    169 }
    170 
    171 void TargetMachine::setAsmVerbosityDefault(bool V) {
    172   AsmVerbosityDefault = V;
    173 }
    174 
    175 bool TargetMachine::getFunctionSections() {
    176   return FunctionSections;
    177 }
    178 
    179 bool TargetMachine::getDataSections() {
    180   return DataSections;
    181 }
    182 
    183 void TargetMachine::setFunctionSections(bool V) {
    184   FunctionSections = V;
    185 }
    186 
    187 void TargetMachine::setDataSections(bool V) {
    188   DataSections = V;
    189 }
    190