blob: 4ccf5194947b543ca45e218212dbae7b6370897e [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 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
Brian Gaeke8351d8c2004-03-04 19:16:23 +000039//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000040// TargetMachine Class
41//
Misha Brukman3decf862004-08-10 23:10:25 +000042
Evan Cheng4d1ca962011-07-08 01:53:10 +000043TargetMachine::TargetMachine(const Target &T,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000044 StringRef TT, StringRef CPU, StringRef FS,
45 const TargetOptions &Options)
Benjamin Kramercc38ef62011-07-20 01:27:58 +000046 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
Craig Topper062a2ba2014-04-25 05:30:21 +000047 CodeGenInfo(nullptr), AsmInfo(nullptr),
Vincent Lejeune92b0a642013-12-07 01:49:19 +000048 RequireStructuredCFG(false),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000049 Options(Options) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +000050}
51
Chris Lattner5d236002003-12-28 21:23:38 +000052TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000053 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000054 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +000055}
56
Bill Wendling965bd582013-03-13 22:26:59 +000057/// \brief Reset the target options based on the function's attributes.
58void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
59 const Function *F = MF->getFunction();
60 TargetOptions &TO = MF->getTarget().Options;
NAKAMURA Takumi66c95432013-11-21 11:08:31 +000061
Bill Wendling965bd582013-03-13 22:26:59 +000062#define RESET_OPTION(X, Y) \
63 do { \
64 if (F->hasFnAttribute(Y)) \
65 TO.X = \
66 (F->getAttributes(). \
67 getAttribute(AttributeSet::FunctionIndex, \
68 Y).getValueAsString() == "true"); \
69 } while (0)
70
71 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
Bill Wendling965bd582013-03-13 22:26:59 +000072 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
73 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
74 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
75 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
76 RESET_OPTION(UseSoftFloat, "use-soft-float");
77 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000078
79 TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress);
Bill Wendling965bd582013-03-13 22:26:59 +000080}
81
Evan Cheng73136df2006-02-22 20:19:42 +000082/// getRelocationModel - Returns the code generation relocation model. The
83/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +000084Reloc::Model TargetMachine::getRelocationModel() const {
85 if (!CodeGenInfo)
86 return Reloc::Default;
87 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +000088}
Evan Chengac4f66f2006-05-23 18:18:46 +000089
Evan Cheng04417462006-07-06 01:53:36 +000090/// getCodeModel - Returns the code model. The choices are small, kernel,
91/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +000092CodeModel::Model TargetMachine::getCodeModel() const {
93 if (!CodeGenInfo)
94 return CodeModel::Default;
95 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +000096}
97
Hans Wennborgcbe34b42012-06-23 11:37:03 +000098/// Get the IR-specified TLS model for Var.
99static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
100 switch (Var->getThreadLocalMode()) {
101 case GlobalVariable::NotThreadLocal:
102 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
103 break;
104 case GlobalVariable::GeneralDynamicTLSModel:
105 return TLSModel::GeneralDynamic;
106 case GlobalVariable::LocalDynamicTLSModel:
107 return TLSModel::LocalDynamic;
108 case GlobalVariable::InitialExecTLSModel:
109 return TLSModel::InitialExec;
110 case GlobalVariable::LocalExecTLSModel:
111 return TLSModel::LocalExec;
112 }
113 llvm_unreachable("invalid TLS model");
114}
115
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000116TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000117 // If GV is an alias then use the aliasee for determining
118 // thread-localness.
119 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
Rafael Espindolae0098922014-05-16 22:37:03 +0000120 GV = GA->getAliasee();
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000121 const GlobalVariable *Var = cast<GlobalVariable>(GV);
122
123 bool isLocal = Var->hasLocalLinkage();
124 bool isDeclaration = Var->isDeclaration();
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000125 bool isPIC = getRelocationModel() == Reloc::PIC_;
126 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000127 // FIXME: what should we do for protected and internal visibility?
128 // For variables, is internal different from hidden?
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000129 bool isHidden = Var->hasHiddenVisibility();
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000130
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000131 TLSModel::Model Model;
132 if (isPIC && !isPIE) {
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000133 if (isLocal || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000134 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000135 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000136 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000137 } else {
138 if (!isDeclaration || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000139 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000140 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000141 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000142 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000143
144 // If the user specified a more specific model, use that.
145 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
146 if (SelectedModel > Model)
147 return SelectedModel;
148
149 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000150}
151
Evan Chengecb29082011-11-16 08:38:26 +0000152/// getOptLevel - Returns the optimization level: None, Less,
153/// Default, or Aggressive.
154CodeGenOpt::Level TargetMachine::getOptLevel() const {
155 if (!CodeGenInfo)
156 return CodeGenOpt::Default;
157 return CodeGenInfo->getOptLevel();
158}
159
Paul Robinsond89125a2013-11-22 19:11:24 +0000160void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
161 if (CodeGenInfo)
162 CodeGenInfo->setOptLevel(Level);
163}
164
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000165bool TargetMachine::getAsmVerbosityDefault() {
166 return AsmVerbosityDefault;
167}
168
169void TargetMachine::setAsmVerbosityDefault(bool V) {
170 AsmVerbosityDefault = V;
171}
172
Eric Christopher2feed5f2014-05-20 21:25:34 +0000173bool TargetMachine::getFunctionSections() const {
174 return Options.FunctionSections;
Chris Lattner5b212a32010-04-13 00:36:43 +0000175}
176
Eric Christopher2feed5f2014-05-20 21:25:34 +0000177bool TargetMachine::getDataSections() const {
178 return Options.DataSections;
Chris Lattner5b212a32010-04-13 00:36:43 +0000179}
180
181void TargetMachine::setFunctionSections(bool V) {
Eric Christopher2feed5f2014-05-20 21:25:34 +0000182 Options.FunctionSections = V;
Chris Lattner5b212a32010-04-13 00:36:43 +0000183}
184
185void TargetMachine::setDataSections(bool V) {
Eric Christopher2feed5f2014-05-20 21:25:34 +0000186 Options.DataSections = V;
Chris Lattner5b212a32010-04-13 00:36:43 +0000187}
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000188
189void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
190 const GlobalValue *GV, Mangler &Mang,
191 bool MayAlwaysUsePrivate) const {
192 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
193 // Simple case: If GV is not private, it is not important to find out if
194 // private labels are legal in this case or not.
195 Mang.getNameWithPrefix(Name, GV, false);
196 return;
197 }
198 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
199 const TargetLoweringObjectFile &TLOF =
200 getTargetLowering()->getObjFileLowering();
201 const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this);
202 bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection);
203 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel);
204}
205
206MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
207 SmallString<60> NameStr;
208 getNameWithPrefix(NameStr, GV, Mang);
209 const TargetLoweringObjectFile &TLOF =
210 getTargetLowering()->getObjFileLowering();
211 return TLOF.getContext().GetOrCreateSymbol(NameStr.str());
212}