blob: b3ff0010c43462fe094d3a9c6a4ecae560a372f6 [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"
Lang Hames1e923ec2015-01-09 18:55:42 +000024#include "llvm/MC/MCSectionMachO.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000025#include "llvm/MC/MCTargetOptions.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000026#include "llvm/MC/SectionKind.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/CommandLine.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000028#include "llvm/Target/TargetLowering.h"
29#include "llvm/Target/TargetLoweringObjectFile.h"
Eric Christopherd9134482014-08-04 21:25:23 +000030#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner5d236002003-12-28 21:23:38 +000031using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000032
Vikram S. Adve36d3e032002-09-16 15:39:26 +000033//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000034// TargetMachine Class
35//
Misha Brukman3decf862004-08-10 23:10:25 +000036
Evan Cheng4d1ca962011-07-08 01:53:10 +000037TargetMachine::TargetMachine(const Target &T,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000038 StringRef TT, StringRef CPU, StringRef FS,
39 const TargetOptions &Options)
Benjamin Kramercc38ef62011-07-20 01:27:58 +000040 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
Craig Topper062a2ba2014-04-25 05:30:21 +000041 CodeGenInfo(nullptr), AsmInfo(nullptr),
Vincent Lejeune92b0a642013-12-07 01:49:19 +000042 RequireStructuredCFG(false),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000043 Options(Options) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +000044}
45
Chris Lattner5d236002003-12-28 21:23:38 +000046TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000047 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000048 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +000049}
50
Bill Wendling965bd582013-03-13 22:26:59 +000051/// \brief Reset the target options based on the function's attributes.
Eric Christopher3976f782014-09-26 01:28:10 +000052void TargetMachine::resetTargetOptions(const Function &F) const {
53#define RESET_OPTION(X, Y) \
54 do { \
55 if (F.hasFnAttribute(Y)) \
56 Options.X = (F.getAttributes() \
57 .getAttribute(AttributeSet::FunctionIndex, Y) \
58 .getValueAsString() == "true"); \
Bill Wendling965bd582013-03-13 22:26:59 +000059 } while (0)
60
61 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
Bill Wendling965bd582013-03-13 22:26:59 +000062 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
63 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
64 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
65 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
66 RESET_OPTION(UseSoftFloat, "use-soft-float");
67 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000068
Eric Christopher3976f782014-09-26 01:28:10 +000069 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
Bill Wendling965bd582013-03-13 22:26:59 +000070}
71
Evan Cheng73136df2006-02-22 20:19:42 +000072/// getRelocationModel - Returns the code generation relocation model. The
73/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +000074Reloc::Model TargetMachine::getRelocationModel() const {
75 if (!CodeGenInfo)
76 return Reloc::Default;
77 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +000078}
Evan Chengac4f66f2006-05-23 18:18:46 +000079
Evan Cheng04417462006-07-06 01:53:36 +000080/// getCodeModel - Returns the code model. The choices are small, kernel,
81/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +000082CodeModel::Model TargetMachine::getCodeModel() const {
83 if (!CodeGenInfo)
84 return CodeModel::Default;
85 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +000086}
87
Hans Wennborgcbe34b42012-06-23 11:37:03 +000088/// Get the IR-specified TLS model for Var.
Rafael Espindola59f7eba2014-05-28 18:15:43 +000089static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
90 switch (GV->getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +000091 case GlobalVariable::NotThreadLocal:
92 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
93 break;
94 case GlobalVariable::GeneralDynamicTLSModel:
95 return TLSModel::GeneralDynamic;
96 case GlobalVariable::LocalDynamicTLSModel:
97 return TLSModel::LocalDynamic;
98 case GlobalVariable::InitialExecTLSModel:
99 return TLSModel::InitialExec;
100 case GlobalVariable::LocalExecTLSModel:
101 return TLSModel::LocalExec;
102 }
103 llvm_unreachable("invalid TLS model");
104}
105
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000106TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolaa5bb2f62014-05-23 19:16:56 +0000107 bool isLocal = GV->hasLocalLinkage();
108 bool isDeclaration = GV->isDeclaration();
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000109 bool isPIC = getRelocationModel() == Reloc::PIC_;
110 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000111 // FIXME: what should we do for protected and internal visibility?
112 // For variables, is internal different from hidden?
Rafael Espindolaa5bb2f62014-05-23 19:16:56 +0000113 bool isHidden = GV->hasHiddenVisibility();
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000114
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000115 TLSModel::Model Model;
116 if (isPIC && !isPIE) {
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000117 if (isLocal || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000118 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000119 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000120 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000121 } else {
122 if (!isDeclaration || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000123 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000124 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000125 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000126 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000127
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000128 // If the user specified a more specific model, use that.
129 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
130 if (SelectedModel > Model)
131 return SelectedModel;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000132
133 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000134}
135
Evan Chengecb29082011-11-16 08:38:26 +0000136/// getOptLevel - Returns the optimization level: None, Less,
137/// Default, or Aggressive.
138CodeGenOpt::Level TargetMachine::getOptLevel() const {
139 if (!CodeGenInfo)
140 return CodeGenOpt::Default;
141 return CodeGenInfo->getOptLevel();
142}
143
Paul Robinsond89125a2013-11-22 19:11:24 +0000144void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
145 if (CodeGenInfo)
146 CodeGenInfo->setOptLevel(Level);
147}
148
Eric Christophereb719722014-05-20 23:59:50 +0000149bool TargetMachine::getAsmVerbosityDefault() const {
150 return Options.MCOptions.AsmVerbose;
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000151}
152
153void TargetMachine::setAsmVerbosityDefault(bool V) {
Eric Christophereb719722014-05-20 23:59:50 +0000154 Options.MCOptions.AsmVerbose = V;
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000155}
156
Eric Christopher2feed5f2014-05-20 21:25:34 +0000157bool TargetMachine::getFunctionSections() const {
158 return Options.FunctionSections;
Chris Lattner5b212a32010-04-13 00:36:43 +0000159}
160
Eric Christopher2feed5f2014-05-20 21:25:34 +0000161bool TargetMachine::getDataSections() const {
162 return Options.DataSections;
Chris Lattner5b212a32010-04-13 00:36:43 +0000163}
164
165void TargetMachine::setFunctionSections(bool V) {
Eric Christopher2feed5f2014-05-20 21:25:34 +0000166 Options.FunctionSections = V;
Chris Lattner5b212a32010-04-13 00:36:43 +0000167}
168
169void TargetMachine::setDataSections(bool V) {
Eric Christopher2feed5f2014-05-20 21:25:34 +0000170 Options.DataSections = V;
Chris Lattner5b212a32010-04-13 00:36:43 +0000171}
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000172
Lang Hames1e923ec2015-01-09 18:55:42 +0000173static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
174 const MCSection &Section) {
175 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
176 return true;
177
178 // If it is not dead stripped, it is safe to use private labels.
179 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
180 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
181 return true;
182
183 return false;
184}
185
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000186void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
187 const GlobalValue *GV, Mangler &Mang,
188 bool MayAlwaysUsePrivate) const {
189 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
190 // Simple case: If GV is not private, it is not important to find out if
191 // private labels are legal in this case or not.
192 Mang.getNameWithPrefix(Name, GV, false);
193 return;
194 }
195 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
196 const TargetLoweringObjectFile &TLOF =
Eric Christopherd9134482014-08-04 21:25:23 +0000197 getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000198 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
Lang Hames1e923ec2015-01-09 18:55:42 +0000199 bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000200 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
201}
202
203MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
204 SmallString<60> NameStr;
205 getNameWithPrefix(NameStr, GV, Mang);
206 const TargetLoweringObjectFile &TLOF =
Eric Christopherd9134482014-08-04 21:25:23 +0000207 getSubtargetImpl()->getTargetLowering()->getObjFileLowering();
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000208 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
209}