1 //===-- TargetMachine.cpp - General Target Information ---------------------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the general parts of a Target machine.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/Analysis/TargetTransformInfo.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalAlias.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/Mangler.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCCodeGenInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCSectionMachO.h"
26 #include "llvm/MC/MCTargetOptions.h"
27 #include "llvm/MC/SectionKind.h"
28 #include "llvm/IR/LegacyPassManager.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Target/TargetLowering.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetSubtargetInfo.h"
35 //---------------------------------------------------------------------------
36 // TargetMachine Class
39 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
40 StringRef TT, StringRef CPU, StringRef FS,
41 const TargetOptions &Options)
42 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
43 TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr),
44 RequireStructuredCFG(false), Options(Options) {}
46 TargetMachine::~TargetMachine() {
51 /// \brief Reset the target options based on the function's attributes.
52 void TargetMachine::resetTargetOptions(const Function &F) const {
53 #define RESET_OPTION(X, Y) \
55 if (F.hasFnAttribute(Y)) \
56 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
59 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
60 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
61 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
62 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
63 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
64 RESET_OPTION(UseSoftFloat, "use-soft-float");
65 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
67 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
70 /// getRelocationModel - Returns the code generation relocation model. The
71 /// choices are static, PIC, and dynamic-no-pic, and target default.
72 Reloc::Model TargetMachine::getRelocationModel() const {
74 return Reloc::Default;
75 return CodeGenInfo->getRelocationModel();
78 /// getCodeModel - Returns the code model. The choices are small, kernel,
79 /// medium, large, and target default.
80 CodeModel::Model TargetMachine::getCodeModel() const {
82 return CodeModel::Default;
83 return CodeGenInfo->getCodeModel();
86 /// Get the IR-specified TLS model for Var.
87 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
88 switch (GV->getThreadLocalMode()) {
89 case GlobalVariable::NotThreadLocal:
90 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
92 case GlobalVariable::GeneralDynamicTLSModel:
93 return TLSModel::GeneralDynamic;
94 case GlobalVariable::LocalDynamicTLSModel:
95 return TLSModel::LocalDynamic;
96 case GlobalVariable::InitialExecTLSModel:
97 return TLSModel::InitialExec;
98 case GlobalVariable::LocalExecTLSModel:
99 return TLSModel::LocalExec;
101 llvm_unreachable("invalid TLS model");
104 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
105 bool isLocal = GV->hasLocalLinkage();
106 bool isDeclaration = GV->isDeclaration();
107 bool isPIC = getRelocationModel() == Reloc::PIC_;
108 bool isPIE = Options.PositionIndependentExecutable;
109 // FIXME: what should we do for protected and internal visibility?
110 // For variables, is internal different from hidden?
111 bool isHidden = GV->hasHiddenVisibility();
113 TLSModel::Model Model;
114 if (isPIC && !isPIE) {
115 if (isLocal || isHidden)
116 Model = TLSModel::LocalDynamic;
118 Model = TLSModel::GeneralDynamic;
120 if (!isDeclaration || isHidden)
121 Model = TLSModel::LocalExec;
123 Model = TLSModel::InitialExec;
126 // If the user specified a more specific model, use that.
127 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
128 if (SelectedModel > Model)
129 return SelectedModel;
134 /// getOptLevel - Returns the optimization level: None, Less,
135 /// Default, or Aggressive.
136 CodeGenOpt::Level TargetMachine::getOptLevel() const {
138 return CodeGenOpt::Default;
139 return CodeGenInfo->getOptLevel();
142 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
144 CodeGenInfo->setOptLevel(Level);
147 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
148 return TargetIRAnalysis(
149 [this](Function &) { return TargetTransformInfo(getDataLayout()); });
152 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
153 const MCSection &Section) {
154 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
157 // If it is not dead stripped, it is safe to use private labels.
158 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
159 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
165 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
166 const GlobalValue *GV, Mangler &Mang,
167 bool MayAlwaysUsePrivate) const {
168 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
169 // Simple case: If GV is not private, it is not important to find out if
170 // private labels are legal in this case or not.
171 Mang.getNameWithPrefix(Name, GV, false);
174 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
175 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
176 const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
177 bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
178 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
181 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
182 SmallString<60> NameStr;
183 getNameWithPrefix(NameStr, GV, Mang);
184 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
185 return TLOF->getContext().GetOrCreateSymbol(NameStr.str());