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/Mangler.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCCodeGenInfo.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCSectionMachO.h"
27 #include "llvm/MC/MCTargetOptions.h"
28 #include "llvm/MC/SectionKind.h"
29 #include "llvm/IR/LegacyPassManager.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Target/TargetLowering.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetSubtargetInfo.h"
34 using namespace llvm;
35 
36 //---------------------------------------------------------------------------
37 // TargetMachine Class
38 //
39 
TargetMachine(const Target & T,StringRef DataLayoutString,StringRef TT,StringRef CPU,StringRef FS,const TargetOptions & Options)40 TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
41                              StringRef TT, StringRef CPU, StringRef FS,
42                              const TargetOptions &Options)
43     : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
44       TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr),
45       MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
46       Options(Options) {}
47 
~TargetMachine()48 TargetMachine::~TargetMachine() {
49   delete CodeGenInfo;
50   delete AsmInfo;
51   delete MRI;
52   delete MII;
53   delete STI;
54 }
55 
56 /// \brief Reset the target options based on the function's attributes.
resetTargetOptions(const Function & F) const57 void TargetMachine::resetTargetOptions(const Function &F) const {
58 #define RESET_OPTION(X, Y)                                                     \
59   do {                                                                         \
60     if (F.hasFnAttribute(Y))                                                   \
61       Options.X = (F.getFnAttribute(Y).getValueAsString() == "true");          \
62   } while (0)
63 
64   RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
65   RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
66   RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
67   RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
68   RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
69   RESET_OPTION(UseSoftFloat, "use-soft-float");
70   RESET_OPTION(DisableTailCalls, "disable-tail-calls");
71 
72   Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
73 }
74 
75 /// getRelocationModel - Returns the code generation relocation model. The
76 /// choices are static, PIC, and dynamic-no-pic, and target default.
getRelocationModel() const77 Reloc::Model TargetMachine::getRelocationModel() const {
78   if (!CodeGenInfo)
79     return Reloc::Default;
80   return CodeGenInfo->getRelocationModel();
81 }
82 
83 /// getCodeModel - Returns the code model. The choices are small, kernel,
84 /// medium, large, and target default.
getCodeModel() const85 CodeModel::Model TargetMachine::getCodeModel() const {
86   if (!CodeGenInfo)
87     return CodeModel::Default;
88   return CodeGenInfo->getCodeModel();
89 }
90 
91 /// Get the IR-specified TLS model for Var.
getSelectedTLSModel(const GlobalValue * GV)92 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
93   switch (GV->getThreadLocalMode()) {
94   case GlobalVariable::NotThreadLocal:
95     llvm_unreachable("getSelectedTLSModel for non-TLS variable");
96     break;
97   case GlobalVariable::GeneralDynamicTLSModel:
98     return TLSModel::GeneralDynamic;
99   case GlobalVariable::LocalDynamicTLSModel:
100     return TLSModel::LocalDynamic;
101   case GlobalVariable::InitialExecTLSModel:
102     return TLSModel::InitialExec;
103   case GlobalVariable::LocalExecTLSModel:
104     return TLSModel::LocalExec;
105   }
106   llvm_unreachable("invalid TLS model");
107 }
108 
getTLSModel(const GlobalValue * GV) const109 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
110   bool isLocal = GV->hasLocalLinkage();
111   bool isDeclaration = GV->isDeclaration();
112   bool isPIC = getRelocationModel() == Reloc::PIC_;
113   bool isPIE = Options.PositionIndependentExecutable;
114   // FIXME: what should we do for protected and internal visibility?
115   // For variables, is internal different from hidden?
116   bool isHidden = GV->hasHiddenVisibility();
117 
118   TLSModel::Model Model;
119   if (isPIC && !isPIE) {
120     if (isLocal || isHidden)
121       Model = TLSModel::LocalDynamic;
122     else
123       Model = TLSModel::GeneralDynamic;
124   } else {
125     if (!isDeclaration || isHidden)
126       Model = TLSModel::LocalExec;
127     else
128       Model = TLSModel::InitialExec;
129   }
130 
131   // If the user specified a more specific model, use that.
132   TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
133   if (SelectedModel > Model)
134     return SelectedModel;
135 
136   return Model;
137 }
138 
139 /// getOptLevel - Returns the optimization level: None, Less,
140 /// Default, or Aggressive.
getOptLevel() const141 CodeGenOpt::Level TargetMachine::getOptLevel() const {
142   if (!CodeGenInfo)
143     return CodeGenOpt::Default;
144   return CodeGenInfo->getOptLevel();
145 }
146 
setOptLevel(CodeGenOpt::Level Level) const147 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
148   if (CodeGenInfo)
149     CodeGenInfo->setOptLevel(Level);
150 }
151 
getTargetIRAnalysis()152 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
153   return TargetIRAnalysis(
154       [this](Function &) { return TargetTransformInfo(getDataLayout()); });
155 }
156 
canUsePrivateLabel(const MCAsmInfo & AsmInfo,const MCSection & Section)157 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
158                                const MCSection &Section) {
159   if (!AsmInfo.isSectionAtomizableBySymbols(Section))
160     return true;
161 
162   // If it is not dead stripped, it is safe to use private labels.
163   const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
164   if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
165     return true;
166 
167   return false;
168 }
169 
getNameWithPrefix(SmallVectorImpl<char> & Name,const GlobalValue * GV,Mangler & Mang,bool MayAlwaysUsePrivate) const170 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
171                                       const GlobalValue *GV, Mangler &Mang,
172                                       bool MayAlwaysUsePrivate) const {
173   if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
174     // Simple case: If GV is not private, it is not important to find out if
175     // private labels are legal in this case or not.
176     Mang.getNameWithPrefix(Name, GV, false);
177     return;
178   }
179   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
180   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
181   const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
182   bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
183   TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this);
184 }
185 
getSymbol(const GlobalValue * GV,Mangler & Mang) const186 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
187   SmallString<60> NameStr;
188   getNameWithPrefix(NameStr, GV, Mang);
189   const TargetLoweringObjectFile *TLOF = getObjFileLowering();
190   return TLOF->getContext().GetOrCreateSymbol(NameStr);
191 }
192