LLVM API Documentation

TargetMachine.cpp
Go to the documentation of this file.
00001 //===-- TargetMachine.cpp - General Target Information ---------------------==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file describes the general parts of a Target machine.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Target/TargetMachine.h"
00015 #include "llvm/CodeGen/MachineFunction.h"
00016 #include "llvm/IR/Function.h"
00017 #include "llvm/IR/GlobalAlias.h"
00018 #include "llvm/IR/GlobalValue.h"
00019 #include "llvm/IR/GlobalVariable.h"
00020 #include "llvm/MC/MCAsmInfo.h"
00021 #include "llvm/MC/MCCodeGenInfo.h"
00022 #include "llvm/Support/CommandLine.h"
00023 using namespace llvm;
00024 
00025 //---------------------------------------------------------------------------
00026 // Command-line options that tend to be useful on more than one back-end.
00027 //
00028 
00029 namespace llvm {
00030   bool HasDivModLibcall;
00031   bool AsmVerbosityDefault(false);
00032 }
00033 
00034 static cl::opt<bool>
00035 DataSections("fdata-sections",
00036   cl::desc("Emit data into separate sections"),
00037   cl::init(false));
00038 static cl::opt<bool>
00039 FunctionSections("ffunction-sections",
00040   cl::desc("Emit functions into separate sections"),
00041   cl::init(false));
00042 
00043 //---------------------------------------------------------------------------
00044 // TargetMachine Class
00045 //
00046 
00047 TargetMachine::TargetMachine(const Target &T,
00048                              StringRef TT, StringRef CPU, StringRef FS,
00049                              const TargetOptions &Options)
00050   : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
00051     CodeGenInfo(0), AsmInfo(0),
00052     MCRelaxAll(false),
00053     MCNoExecStack(false),
00054     MCSaveTempLabels(false),
00055     MCUseLoc(true),
00056     MCUseCFI(true),
00057     MCUseDwarfDirectory(false),
00058     Options(Options) {
00059 }
00060 
00061 TargetMachine::~TargetMachine() {
00062   delete CodeGenInfo;
00063   delete AsmInfo;
00064 }
00065 
00066 /// \brief Reset the target options based on the function's attributes.
00067 void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
00068   const Function *F = MF->getFunction();
00069   TargetOptions &TO = MF->getTarget().Options;
00070   
00071 #define RESET_OPTION(X, Y)                                              \
00072   do {                                                                  \
00073     if (F->hasFnAttribute(Y))                                           \
00074       TO.X =                                                            \
00075         (F->getAttributes().                                            \
00076            getAttribute(AttributeSet::FunctionIndex,                    \
00077                         Y).getValueAsString() == "true");               \
00078   } while (0)
00079 
00080   RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
00081   RESET_OPTION(NoFramePointerElimNonLeaf, "no-frame-pointer-elim-non-leaf");
00082   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
00083   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
00084   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
00085   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
00086   RESET_OPTION(UseSoftFloat, "use-soft-float");
00087   RESET_OPTION(DisableTailCalls, "disable-tail-calls");
00088 }
00089 
00090 /// getRelocationModel - Returns the code generation relocation model. The
00091 /// choices are static, PIC, and dynamic-no-pic, and target default.
00092 Reloc::Model TargetMachine::getRelocationModel() const {
00093   if (!CodeGenInfo)
00094     return Reloc::Default;
00095   return CodeGenInfo->getRelocationModel();
00096 }
00097 
00098 /// getCodeModel - Returns the code model. The choices are small, kernel,
00099 /// medium, large, and target default.
00100 CodeModel::Model TargetMachine::getCodeModel() const {
00101   if (!CodeGenInfo)
00102     return CodeModel::Default;
00103   return CodeGenInfo->getCodeModel();
00104 }
00105 
00106 /// Get the IR-specified TLS model for Var.
00107 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
00108   switch (Var->getThreadLocalMode()) {
00109   case GlobalVariable::NotThreadLocal:
00110     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
00111     break;
00112   case GlobalVariable::GeneralDynamicTLSModel:
00113     return TLSModel::GeneralDynamic;
00114   case GlobalVariable::LocalDynamicTLSModel:
00115     return TLSModel::LocalDynamic;
00116   case GlobalVariable::InitialExecTLSModel:
00117     return TLSModel::InitialExec;
00118   case GlobalVariable::LocalExecTLSModel:
00119     return TLSModel::LocalExec;
00120   }
00121   llvm_unreachable("invalid TLS model");
00122 }
00123 
00124 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
00125   // If GV is an alias then use the aliasee for determining
00126   // thread-localness.
00127   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
00128     GV = GA->resolveAliasedGlobal(false);
00129   const GlobalVariable *Var = cast<GlobalVariable>(GV);
00130 
00131   bool isLocal = Var->hasLocalLinkage();
00132   bool isDeclaration = Var->isDeclaration();
00133   bool isPIC = getRelocationModel() == Reloc::PIC_;
00134   bool isPIE = Options.PositionIndependentExecutable;
00135   // FIXME: what should we do for protected and internal visibility?
00136   // For variables, is internal different from hidden?
00137   bool isHidden = Var->hasHiddenVisibility();
00138 
00139   TLSModel::Model Model;
00140   if (isPIC && !isPIE) {
00141     if (isLocal || isHidden)
00142       Model = TLSModel::LocalDynamic;
00143     else
00144       Model = TLSModel::GeneralDynamic;
00145   } else {
00146     if (!isDeclaration || isHidden)
00147       Model = TLSModel::LocalExec;
00148     else
00149       Model = TLSModel::InitialExec;
00150   }
00151 
00152   // If the user specified a more specific model, use that.
00153   TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
00154   if (SelectedModel > Model)
00155     return SelectedModel;
00156 
00157   return Model;
00158 }
00159 
00160 /// getOptLevel - Returns the optimization level: None, Less,
00161 /// Default, or Aggressive.
00162 CodeGenOpt::Level TargetMachine::getOptLevel() const {
00163   if (!CodeGenInfo)
00164     return CodeGenOpt::Default;
00165   return CodeGenInfo->getOptLevel();
00166 }
00167 
00168 bool TargetMachine::getAsmVerbosityDefault() {
00169   return AsmVerbosityDefault;
00170 }
00171 
00172 void TargetMachine::setAsmVerbosityDefault(bool V) {
00173   AsmVerbosityDefault = V;
00174 }
00175 
00176 bool TargetMachine::getFunctionSections() {
00177   return FunctionSections;
00178 }
00179 
00180 bool TargetMachine::getDataSections() {
00181   return DataSections;
00182 }
00183 
00184 void TargetMachine::setFunctionSections(bool V) {
00185   FunctionSections = V;
00186 }
00187 
00188 void TargetMachine::setDataSections(bool V) {
00189   DataSections = V;
00190 }