blob: f8a58ec2922c26d627f26ea582ee23fbe3382a33 [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"
24#include "llvm/MC/SectionKind.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000025#include "llvm/Support/CommandLine.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000026#include "llvm/Target/TargetLowering.h"
27#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattner5d236002003-12-28 21:23:38 +000028using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000029
Vikram S. Adve36d3e032002-09-16 15:39:26 +000030//---------------------------------------------------------------------------
Brian Gaeke8351d8c2004-03-04 19:16:23 +000031// Command-line options that tend to be useful on more than one back-end.
32//
33
Misha Brukman96041e52004-06-21 21:44:12 +000034namespace llvm {
Evan Chenga7c7b542011-04-07 00:58:44 +000035 bool HasDivModLibcall;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000036 bool AsmVerbosityDefault(false);
Chris Lattneraa2372562006-05-24 17:04:05 +000037}
Misha Brukman069ca062004-06-21 21:08:45 +000038
Chris Lattner5b212a32010-04-13 00:36:43 +000039static cl::opt<bool>
40DataSections("fdata-sections",
41 cl::desc("Emit data into separate sections"),
42 cl::init(false));
43static cl::opt<bool>
44FunctionSections("ffunction-sections",
45 cl::desc("Emit functions into separate sections"),
46 cl::init(false));
Andrew Tricke77e84e2012-01-13 06:30:30 +000047
Brian Gaeke8351d8c2004-03-04 19:16:23 +000048//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000049// TargetMachine Class
50//
Misha Brukman3decf862004-08-10 23:10:25 +000051
Evan Cheng4d1ca962011-07-08 01:53:10 +000052TargetMachine::TargetMachine(const Target &T,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000053 StringRef TT, StringRef CPU, StringRef FS,
54 const TargetOptions &Options)
Benjamin Kramercc38ef62011-07-20 01:27:58 +000055 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
56 CodeGenInfo(0), AsmInfo(0),
Rafael Espindolab58867c2010-11-19 02:26:16 +000057 MCRelaxAll(false),
Rafael Espindolac5efca42011-01-23 18:50:12 +000058 MCNoExecStack(false),
Daniel Dunbar3e2b3352011-03-28 22:49:19 +000059 MCSaveTempLabels(false),
Nick Lewycky40f8f2f2011-10-17 23:05:28 +000060 MCUseCFI(true),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000061 MCUseDwarfDirectory(false),
Vincent Lejeune92b0a642013-12-07 01:49:19 +000062 RequireStructuredCFG(false),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000063 Options(Options) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +000064}
65
Chris Lattner5d236002003-12-28 21:23:38 +000066TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000067 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000068 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +000069}
70
Bill Wendling965bd582013-03-13 22:26:59 +000071/// \brief Reset the target options based on the function's attributes.
72void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
73 const Function *F = MF->getFunction();
74 TargetOptions &TO = MF->getTarget().Options;
NAKAMURA Takumi66c95432013-11-21 11:08:31 +000075
Bill Wendling965bd582013-03-13 22:26:59 +000076#define RESET_OPTION(X, Y) \
77 do { \
78 if (F->hasFnAttribute(Y)) \
79 TO.X = \
80 (F->getAttributes(). \
81 getAttribute(AttributeSet::FunctionIndex, \
82 Y).getValueAsString() == "true"); \
83 } while (0)
84
85 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
Bill Wendling965bd582013-03-13 22:26:59 +000086 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
87 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
88 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
89 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
90 RESET_OPTION(UseSoftFloat, "use-soft-float");
91 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
92}
93
Evan Cheng73136df2006-02-22 20:19:42 +000094/// getRelocationModel - Returns the code generation relocation model. The
95/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +000096Reloc::Model TargetMachine::getRelocationModel() const {
97 if (!CodeGenInfo)
98 return Reloc::Default;
99 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +0000100}
Evan Chengac4f66f2006-05-23 18:18:46 +0000101
Evan Cheng04417462006-07-06 01:53:36 +0000102/// getCodeModel - Returns the code model. The choices are small, kernel,
103/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +0000104CodeModel::Model TargetMachine::getCodeModel() const {
105 if (!CodeGenInfo)
106 return CodeModel::Default;
107 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +0000108}
109
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000110/// Get the IR-specified TLS model for Var.
111static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
112 switch (Var->getThreadLocalMode()) {
113 case GlobalVariable::NotThreadLocal:
114 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
115 break;
116 case GlobalVariable::GeneralDynamicTLSModel:
117 return TLSModel::GeneralDynamic;
118 case GlobalVariable::LocalDynamicTLSModel:
119 return TLSModel::LocalDynamic;
120 case GlobalVariable::InitialExecTLSModel:
121 return TLSModel::InitialExec;
122 case GlobalVariable::LocalExecTLSModel:
123 return TLSModel::LocalExec;
124 }
125 llvm_unreachable("invalid TLS model");
126}
127
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000128TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000129 // If GV is an alias then use the aliasee for determining
130 // thread-localness.
131 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
132 GV = GA->resolveAliasedGlobal(false);
133 const GlobalVariable *Var = cast<GlobalVariable>(GV);
134
135 bool isLocal = Var->hasLocalLinkage();
136 bool isDeclaration = Var->isDeclaration();
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000137 bool isPIC = getRelocationModel() == Reloc::PIC_;
138 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000139 // FIXME: what should we do for protected and internal visibility?
140 // For variables, is internal different from hidden?
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000141 bool isHidden = Var->hasHiddenVisibility();
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000142
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000143 TLSModel::Model Model;
144 if (isPIC && !isPIE) {
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000145 if (isLocal || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000146 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000147 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000148 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000149 } else {
150 if (!isDeclaration || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000151 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000152 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000153 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000154 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000155
156 // If the user specified a more specific model, use that.
157 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
158 if (SelectedModel > Model)
159 return SelectedModel;
160
161 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000162}
163
Evan Chengecb29082011-11-16 08:38:26 +0000164/// getOptLevel - Returns the optimization level: None, Less,
165/// Default, or Aggressive.
166CodeGenOpt::Level TargetMachine::getOptLevel() const {
167 if (!CodeGenInfo)
168 return CodeGenOpt::Default;
169 return CodeGenInfo->getOptLevel();
170}
171
Paul Robinsond89125a2013-11-22 19:11:24 +0000172void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
173 if (CodeGenInfo)
174 CodeGenInfo->setOptLevel(Level);
175}
176
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000177bool TargetMachine::getAsmVerbosityDefault() {
178 return AsmVerbosityDefault;
179}
180
181void TargetMachine::setAsmVerbosityDefault(bool V) {
182 AsmVerbosityDefault = V;
183}
184
Chris Lattner5b212a32010-04-13 00:36:43 +0000185bool TargetMachine::getFunctionSections() {
186 return FunctionSections;
187}
188
189bool TargetMachine::getDataSections() {
190 return DataSections;
191}
192
193void TargetMachine::setFunctionSections(bool V) {
194 FunctionSections = V;
195}
196
197void TargetMachine::setDataSections(bool V) {
198 DataSections = V;
199}
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000200
201void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
202 const GlobalValue *GV, Mangler &Mang,
203 bool MayAlwaysUsePrivate) const {
204 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
205 // Simple case: If GV is not private, it is not important to find out if
206 // private labels are legal in this case or not.
207 Mang.getNameWithPrefix(Name, GV, false);
208 return;
209 }
210 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
211 const TargetLoweringObjectFile &TLOF =
212 getTargetLowering()->getObjFileLowering();
213 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
214 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
215 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
216}
217
218MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
219 SmallString<60> NameStr;
220 getNameWithPrefix(NameStr, GV, Mang);
221 const TargetLoweringObjectFile &TLOF =
222 getTargetLowering()->getObjFileLowering();
223 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
224}