blob: 325381be88fdb018dc3c9eac7abbfaf3fe4b73fd [file] [log] [blame]
Chris Lattner22a6a902001-09-14 05:34:53 +00001//===-- TargetMachine.cpp - General Target Information ---------------------==//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner22a6a902001-09-14 05:34:53 +00009//
10// This file describes the general parts of a Target machine.
11//
12//===----------------------------------------------------------------------===//
Vikram S. Adve3414e782001-07-21 12:42:08 +000013
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Target/TargetMachine.h"
Bill Wendling965bd582013-03-13 22:26:59 +000015#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/GlobalAlias.h"
18#include "llvm/IR/GlobalValue.h"
19#include "llvm/IR/GlobalVariable.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000020#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000021#include "llvm/MC/MCAsmInfo.h"
Craig Topperd4a964c2012-03-25 18:10:17 +000022#include "llvm/MC/MCCodeGenInfo.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000023#include "llvm/MC/MCContext.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000024#include "llvm/MC/MCTargetOptions.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000025#include "llvm/MC/SectionKind.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/CommandLine.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000027#include "llvm/Target/TargetLowering.h"
28#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner5d236002003-12-28 21:23:38 +000029using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000030
Vikram S. Adve36d3e032002-09-16 15:39:26 +000031//---------------------------------------------------------------------------
Brian Gaeke8351d8c2004-03-04 19:16:23 +000032// Command-line options that tend to be useful on more than one back-end.
33//
34
Misha Brukman96041e52004-06-21 21:44:12 +000035namespace llvm {
Evan Chenga7c7b542011-04-07 00:58:44 +000036 bool HasDivModLibcall;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000037 bool AsmVerbosityDefault(false);
Chris Lattneraa2372562006-05-24 17:04:05 +000038}
Misha Brukman069ca062004-06-21 21:08:45 +000039
Chris Lattner5b212a32010-04-13 00:36:43 +000040static cl::opt<bool>
41DataSections("fdata-sections",
42 cl::desc("Emit data into separate sections"),
43 cl::init(false));
44static cl::opt<bool>
45FunctionSections("ffunction-sections",
46 cl::desc("Emit functions into separate sections"),
47 cl::init(false));
Andrew Tricke77e84e2012-01-13 06:30:30 +000048
Brian Gaeke8351d8c2004-03-04 19:16:23 +000049//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000050// TargetMachine Class
51//
Misha Brukman3decf862004-08-10 23:10:25 +000052
Evan Cheng4d1ca962011-07-08 01:53:10 +000053TargetMachine::TargetMachine(const Target &T,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000054 StringRef TT, StringRef CPU, StringRef FS,
55 const TargetOptions &Options)
Benjamin Kramercc38ef62011-07-20 01:27:58 +000056 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
Craig Topper062a2ba2014-04-25 05:30:21 +000057 CodeGenInfo(nullptr), AsmInfo(nullptr),
Rafael Espindolab58867c2010-11-19 02:26:16 +000058 MCRelaxAll(false),
Rafael Espindolac5efca42011-01-23 18:50:12 +000059 MCNoExecStack(false),
Daniel Dunbar3e2b3352011-03-28 22:49:19 +000060 MCSaveTempLabels(false),
Nick Lewycky40f8f2f2011-10-17 23:05:28 +000061 MCUseCFI(true),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000062 MCUseDwarfDirectory(false),
Vincent Lejeune92b0a642013-12-07 01:49:19 +000063 RequireStructuredCFG(false),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000064 Options(Options) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +000065}
66
Chris Lattner5d236002003-12-28 21:23:38 +000067TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000068 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000069 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +000070}
71
Bill Wendling965bd582013-03-13 22:26:59 +000072/// \brief Reset the target options based on the function's attributes.
73void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
74 const Function *F = MF->getFunction();
75 TargetOptions &TO = MF->getTarget().Options;
NAKAMURA Takumi66c95432013-11-21 11:08:31 +000076
Bill Wendling965bd582013-03-13 22:26:59 +000077#define RESET_OPTION(X, Y) \
78 do { \
79 if (F->hasFnAttribute(Y)) \
80 TO.X = \
81 (F->getAttributes(). \
82 getAttribute(AttributeSet::FunctionIndex, \
83 Y).getValueAsString() == "true"); \
84 } while (0)
85
86 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
Bill Wendling965bd582013-03-13 22:26:59 +000087 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
88 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
89 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
90 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
91 RESET_OPTION(UseSoftFloat, "use-soft-float");
92 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000093
94 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
Bill Wendling965bd582013-03-13 22:26:59 +000095}
96
Evan Cheng73136df2006-02-22 20:19:42 +000097/// getRelocationModel - Returns the code generation relocation model. The
98/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +000099Reloc::Model TargetMachine::getRelocationModel() const {
100 if (!CodeGenInfo)
101 return Reloc::Default;
102 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +0000103}
Evan Chengac4f66f2006-05-23 18:18:46 +0000104
Evan Cheng04417462006-07-06 01:53:36 +0000105/// getCodeModel - Returns the code model. The choices are small, kernel,
106/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +0000107CodeModel::Model TargetMachine::getCodeModel() const {
108 if (!CodeGenInfo)
109 return CodeModel::Default;
110 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +0000111}
112
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000113/// Get the IR-specified TLS model for Var.
114static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
115 switch (Var->getThreadLocalMode()) {
116 case GlobalVariable::NotThreadLocal:
117 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
118 break;
119 case GlobalVariable::GeneralDynamicTLSModel:
120 return TLSModel::GeneralDynamic;
121 case GlobalVariable::LocalDynamicTLSModel:
122 return TLSModel::LocalDynamic;
123 case GlobalVariable::InitialExecTLSModel:
124 return TLSModel::InitialExec;
125 case GlobalVariable::LocalExecTLSModel:
126 return TLSModel::LocalExec;
127 }
128 llvm_unreachable("invalid TLS model");
129}
130
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000131TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000132 // If GV is an alias then use the aliasee for determining
133 // thread-localness.
134 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Rafael Espindola24a669d2014-03-27 15:26:56 +0000135 GV = GA->getAliasedGlobal();
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000136 const GlobalVariable *Var = cast<GlobalVariable>(GV);
137
138 bool isLocal = Var->hasLocalLinkage();
139 bool isDeclaration = Var->isDeclaration();
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000140 bool isPIC = getRelocationModel() == Reloc::PIC_;
141 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000142 // FIXME: what should we do for protected and internal visibility?
143 // For variables, is internal different from hidden?
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000144 bool isHidden = Var->hasHiddenVisibility();
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000145
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000146 TLSModel::Model Model;
147 if (isPIC && !isPIE) {
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000148 if (isLocal || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000149 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000150 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000151 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000152 } else {
153 if (!isDeclaration || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000154 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000155 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000156 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000157 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000158
159 // If the user specified a more specific model, use that.
160 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
161 if (SelectedModel > Model)
162 return SelectedModel;
163
164 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000165}
166
Evan Chengecb29082011-11-16 08:38:26 +0000167/// getOptLevel - Returns the optimization level: None, Less,
168/// Default, or Aggressive.
169CodeGenOpt::Level TargetMachine::getOptLevel() const {
170 if (!CodeGenInfo)
171 return CodeGenOpt::Default;
172 return CodeGenInfo->getOptLevel();
173}
174
Paul Robinsond89125a2013-11-22 19:11:24 +0000175void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
176 if (CodeGenInfo)
177 CodeGenInfo->setOptLevel(Level);
178}
179
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000180bool TargetMachine::getAsmVerbosityDefault() {
181 return AsmVerbosityDefault;
182}
183
184void TargetMachine::setAsmVerbosityDefault(bool V) {
185 AsmVerbosityDefault = V;
186}
187
Chris Lattner5b212a32010-04-13 00:36:43 +0000188bool TargetMachine::getFunctionSections() {
189 return FunctionSections;
190}
191
192bool TargetMachine::getDataSections() {
193 return DataSections;
194}
195
196void TargetMachine::setFunctionSections(bool V) {
197 FunctionSections = V;
198}
199
200void TargetMachine::setDataSections(bool V) {
201 DataSections = V;
202}
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000203
204void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
205 const GlobalValue *GV, Mangler &Mang,
206 bool MayAlwaysUsePrivate) const {
207 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
208 // Simple case: If GV is not private, it is not important to find out if
209 // private labels are legal in this case or not.
210 Mang.getNameWithPrefix(Name, GV, false);
211 return;
212 }
213 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
214 const TargetLoweringObjectFile &TLOF =
215 getTargetLowering()->getObjFileLowering();
216 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
217 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
218 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
219}
220
221MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
222 SmallString<60> NameStr;
223 getNameWithPrefix(NameStr, GV, Mang);
224 const TargetLoweringObjectFile &TLOF =
225 getTargetLowering()->getObjFileLowering();
226 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
227}