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/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/LegacyPassManager.h"
22 #include "llvm/IR/Mangler.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCCodeGenInfo.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 #include "llvm/Target/TargetLowering.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetSubtargetInfo.h"
33 using namespace llvm;
34 
35 //---------------------------------------------------------------------------
36 // TargetMachine Class
37 //
38 
39 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
40                              const Triple &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), MRI(nullptr),
44       MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
45       Options(Options) {}
46 
47 TargetMachine::~TargetMachine() {
48   delete CodeGenInfo;
49   delete AsmInfo;
50   delete MRI;
51   delete MII;
52   delete STI;
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) there's no default state here to keep,
59 // c) these target options should be passed only on the function
60 //    and not on the TargetMachine (via TargetOptions) at all.
61 void TargetMachine::resetTargetOptions(const Function &F) const {
62 #define RESET_OPTION(X, Y)                                                     \
63   do {                                                                         \
64     if (F.hasFnAttribute(Y))                                                   \
65       Options.X = (F.getFnAttribute(Y).getValueAsString() == "true");          \
66   } while (0)
67 
68   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
69   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
70   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
71   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
72 }
73 
74 /// Returns the code generation relocation model. The choices are static, PIC,
75 /// and dynamic-no-pic.
76 Reloc::Model TargetMachine::getRelocationModel() const {
77   if (!CodeGenInfo)
78     return Reloc::Static; // FIXME
79   return CodeGenInfo->getRelocationModel();
80 }
81 
82 /// getCodeModel - Returns the code model. The choices are small, kernel,
83 /// medium, large, and target default.
84 CodeModel::Model TargetMachine::getCodeModel() const {
85   if (!CodeGenInfo)
86     return CodeModel::Default;
87   return CodeGenInfo->getCodeModel();
88 }
89 
90 /// Get the IR-specified TLS model for Var.
91 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
92   switch (GV->getThreadLocalMode()) {
93   case GlobalVariable::NotThreadLocal:
94     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
95     break;
96   case GlobalVariable::GeneralDynamicTLSModel:
97     return TLSModel::GeneralDynamic;
98   case GlobalVariable::LocalDynamicTLSModel:
99     return TLSModel::LocalDynamic;
100   case GlobalVariable::InitialExecTLSModel:
101     return TLSModel::InitialExec;
102   case GlobalVariable::LocalExecTLSModel:
103     return TLSModel::LocalExec;
104   }
105   llvm_unreachable("invalid TLS model");
106 }
107 
108 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
109   bool isLocal = GV->hasLocalLinkage();
110   bool isDeclaration = GV->isDeclaration();
111   bool isPIC = getRelocationModel() == Reloc::PIC_;
112   bool isPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
113   // FIXME: what should we do for protected and internal visibility?
114   // For variables, is internal different from hidden?
115   bool isHidden = GV->hasHiddenVisibility();
116 
117   TLSModel::Model Model;
118   if (isPIC && !isPIE) {
119     if (isLocal || isHidden)
120       Model = TLSModel::LocalDynamic;
121     else
122       Model = TLSModel::GeneralDynamic;
123   } else {
124     if (!isDeclaration || isHidden)
125       Model = TLSModel::LocalExec;
126     else
127       Model = TLSModel::InitialExec;
128   }
129 
130   // If the user specified a more specific model, use that.
131   TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
132   if (SelectedModel > Model)
133     return SelectedModel;
134 
135   return Model;
136 }
137 
138 /// getOptLevel - Returns the optimization level: None, Less,
139 /// Default, or Aggressive.
140 CodeGenOpt::Level TargetMachine::getOptLevel() const {
141   if (!CodeGenInfo)
142     return CodeGenOpt::Default;
143   return CodeGenInfo->getOptLevel();
144 }
145 
146 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
147   if (CodeGenInfo)
148     CodeGenInfo->setOptLevel(Level);
149 }
150 
151 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
152   return TargetIRAnalysis([this](const Function &F) {
153     return TargetTransformInfo(F.getParent()->getDataLayout());
154   });
155 }
156 
157 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
158                                       const GlobalValue *GV, Mangler &Mang,
159                                       bool MayAlwaysUsePrivate) const {
160   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
161     // Simple case: If GV is not private, it is not important to find out if
162     // private labels are legal in this case or not.
163     Mang.getNameWithPrefix(Name, GV, false);
164     return;
165   }
166   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
167   TLOF->getNameWithPrefix(Name, GV, Mang, *this);
168 }
169 
170 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
171   SmallString<128> NameStr;
172   getNameWithPrefix(NameStr, GV, Mang);
173   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
174   return TLOF->getContext().getOrCreateSymbol(NameStr);
175 }
176