blob: 4beecb4dbb689b065e623b380d4581de3c501a53 [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"
Chandler Carruth705b1852015-01-31 03:43:40 +000015#include "llvm/Analysis/TargetTransformInfo.h"
Bill Wendling965bd582013-03-13 22:26:59 +000016#include "llvm/CodeGen/MachineFunction.h"
17#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/GlobalAlias.h"
19#include "llvm/IR/GlobalValue.h"
20#include "llvm/IR/GlobalVariable.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000021#include "llvm/IR/LegacyPassManager.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000022#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000023#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000024#include "llvm/MC/MCContext.h"
Eric Christopher72e23a22015-03-19 22:36:32 +000025#include "llvm/MC/MCInstrInfo.h"
Lang Hames1e923ec2015-01-09 18:55:42 +000026#include "llvm/MC/MCSectionMachO.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000027#include "llvm/MC/MCTargetOptions.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000028#include "llvm/MC/SectionKind.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000029#include "llvm/Target/TargetLowering.h"
30#include "llvm/Target/TargetLoweringObjectFile.h"
Eric Christopherd9134482014-08-04 21:25:23 +000031#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner5d236002003-12-28 21:23:38 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Mehdi Aminicfed2562016-07-13 23:39:46 +000034cl::opt<bool> EnableIPRA("enable-ipra", cl::init(false), cl::Hidden,
35 cl::desc("Enable interprocedural register allocation "
36 "to reduce load/store at procedure calls."));
37
Dehao Chen1ce8d6c2017-01-19 00:44:11 +000038cl::opt<bool> DebugInfoForProfiling(
39 "debug-info-for-profiling", cl::init(false), cl::Hidden,
40 cl::desc("Emit extra debug info to make sample profile more accurate."));
41
Vikram S. Adve36d3e032002-09-16 15:39:26 +000042//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000043// TargetMachine Class
44//
Misha Brukman3decf862004-08-10 23:10:25 +000045
Mehdi Amini93e1ea12015-03-12 00:07:24 +000046TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
Daniel Sanders3e5de882015-06-11 19:41:26 +000047 const Triple &TT, StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000048 const TargetOptions &Options)
Mehdi Amini93e1ea12015-03-12 00:07:24 +000049 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
Rafael Espindolad86e8bb2016-06-30 18:25:11 +000050 TargetFS(FS), AsmInfo(nullptr), MRI(nullptr), MII(nullptr), STI(nullptr),
Justin Lebar7d818132017-01-10 23:43:04 +000051 RequireStructuredCFG(false), DefaultOptions(Options), Options(Options) {
Mehdi Aminicfed2562016-07-13 23:39:46 +000052 if (EnableIPRA.getNumOccurrences())
53 this->Options.EnableIPRA = EnableIPRA;
Dehao Chen1ce8d6c2017-01-19 00:44:11 +000054 if (DebugInfoForProfiling.getNumOccurrences())
55 this->Options.DebugInfoForProfiling = DebugInfoForProfiling;
Mehdi Aminicfed2562016-07-13 23:39:46 +000056}
Anton Korobeynikov77d19432009-06-08 22:53:56 +000057
Chris Lattner5d236002003-12-28 21:23:38 +000058TargetMachine::~TargetMachine() {
Jim Laskeyae92ce82006-09-07 23:39:26 +000059 delete AsmInfo;
Eric Christopher72e23a22015-03-19 22:36:32 +000060 delete MRI;
61 delete MII;
Eric Christopher12cf76f2015-03-19 22:36:37 +000062 delete STI;
Chris Lattner5d236002003-12-28 21:23:38 +000063}
64
Rafael Espindolaf9e348b2016-06-27 21:33:08 +000065bool TargetMachine::isPositionIndependent() const {
66 return getRelocationModel() == Reloc::PIC_;
67}
68
Bill Wendling965bd582013-03-13 22:26:59 +000069/// \brief Reset the target options based on the function's attributes.
Eric Christopher35a8a622015-04-28 18:09:05 +000070// FIXME: This function needs to go away for a number of reasons:
71// a) global state on the TargetMachine is terrible in general,
Justin Lebar7d818132017-01-10 23:43:04 +000072// b) these target options should be passed only on the function
Eric Christopher35a8a622015-04-28 18:09:05 +000073// and not on the TargetMachine (via TargetOptions) at all.
Eric Christopher3976f782014-09-26 01:28:10 +000074void TargetMachine::resetTargetOptions(const Function &F) const {
75#define RESET_OPTION(X, Y) \
76 do { \
Duncan P. N. Exon Smith025c0ad2015-02-14 15:36:52 +000077 if (F.hasFnAttribute(Y)) \
78 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
Justin Lebar7d818132017-01-10 23:43:04 +000079 else \
80 Options.X = DefaultOptions.X; \
Bill Wendling965bd582013-03-13 22:26:59 +000081 } while (0)
82
Bill Wendling965bd582013-03-13 22:26:59 +000083 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
84 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
85 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
86 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
Matt Arsenault732a5312017-01-25 06:08:42 +000087 RESET_OPTION(NoSignedZerosFPMath, "no-signed-zeros-fp-math");
Sjoerd Meijer46b5b882016-08-31 14:17:38 +000088 RESET_OPTION(NoTrappingFPMath, "no-trapping-math");
89
90 StringRef Denormal =
91 F.getFnAttribute("denormal-fp-math").getValueAsString();
92 if (Denormal == "ieee")
Sjoerd Meijer535529b2016-10-04 08:03:36 +000093 Options.FPDenormalMode = FPDenormal::IEEE;
Sjoerd Meijer46b5b882016-08-31 14:17:38 +000094 else if (Denormal == "preserve-sign")
Sjoerd Meijer535529b2016-10-04 08:03:36 +000095 Options.FPDenormalMode = FPDenormal::PreserveSign;
Sjoerd Meijer46b5b882016-08-31 14:17:38 +000096 else if (Denormal == "positive-zero")
Sjoerd Meijer535529b2016-10-04 08:03:36 +000097 Options.FPDenormalMode = FPDenormal::PositiveZero;
Justin Lebar7d818132017-01-10 23:43:04 +000098 else
99 Options.FPDenormalMode = DefaultOptions.FPDenormalMode;
Bill Wendling965bd582013-03-13 22:26:59 +0000100}
101
Rafael Espindola38af4d62016-05-18 16:00:24 +0000102/// Returns the code generation relocation model. The choices are static, PIC,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000103/// and dynamic-no-pic.
Rafael Espindolad86e8bb2016-06-30 18:25:11 +0000104Reloc::Model TargetMachine::getRelocationModel() const { return RM; }
Evan Chengac4f66f2006-05-23 18:18:46 +0000105
Rafael Espindola222a9d02016-06-30 12:44:52 +0000106/// Returns the code model. The choices are small, kernel, medium, large, and
107/// target default.
Rafael Espindolad86e8bb2016-06-30 18:25:11 +0000108CodeModel::Model TargetMachine::getCodeModel() const { return CMModel; }
Evan Cheng04417462006-07-06 01:53:36 +0000109
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000110/// Get the IR-specified TLS model for Var.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000111static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
112 switch (GV->getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000113 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
Rafael Espindola3beef8d2016-06-27 23:15:57 +0000128bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
129 const GlobalValue *GV) const {
130 Reloc::Model RM = getRelocationModel();
131 const Triple &TT = getTargetTriple();
132
133 // DLLImport explicitly marks the GV as external.
134 if (GV && GV->hasDLLImportStorageClass())
135 return false;
136
Matthias Brauneccdee92016-10-03 20:11:24 +0000137 // Every other GV is local on COFF.
138 // Make an exception for windows OS in the triple: Some firmwares builds use
139 // *-win32-macho triples. This (accidentally?) produced windows relocations
140 // without GOT tables in older clang versions; Keep this behaviour.
Matthias Braun9baa3e82016-10-03 22:12:37 +0000141 if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
Rafael Espindola3beef8d2016-06-27 23:15:57 +0000142 return true;
143
144 if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility()))
145 return true;
146
147 if (TT.isOSBinFormatMachO()) {
148 if (RM == Reloc::Static)
149 return true;
150 return GV && GV->isStrongDefinitionForLinker();
151 }
152
153 assert(TT.isOSBinFormatELF());
154 assert(RM != Reloc::DynamicNoPIC);
155
156 bool IsExecutable =
157 RM == Reloc::Static || M.getPIELevel() != PIELevel::Default;
158 if (IsExecutable) {
159 // If the symbol is defined, it cannot be preempted.
160 if (GV && !GV->isDeclarationForLinker())
161 return true;
162
163 bool IsTLS = GV && GV->isThreadLocal();
Sriraman Tallamf29fa582016-10-13 20:54:39 +0000164 bool IsAccessViaCopyRelocs =
165 Options.MCOptions.MCPIECopyRelocations && GV && isa<GlobalVariable>(GV);
Rafael Espindola82149a12017-01-26 15:02:31 +0000166 Triple::ArchType Arch = TT.getArch();
167 bool IsPPC =
168 Arch == Triple::ppc || Arch == Triple::ppc64 || Arch == Triple::ppc64le;
169 // Check if we can use copy relocations. PowerPC has no copy relocations.
170 if (!IsTLS && !IsPPC && (RM == Reloc::Static || IsAccessViaCopyRelocs))
Rafael Espindola3beef8d2016-06-27 23:15:57 +0000171 return true;
172 }
173
174 // ELF supports preemption of other symbols.
175 return false;
176}
177
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000178TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindola8121bec2016-06-27 20:19:14 +0000179 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
180 Reloc::Model RM = getRelocationModel();
181 bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
Rafael Espindola3beef8d2016-06-27 23:15:57 +0000182 bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV);
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000183
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000184 TLSModel::Model Model;
Rafael Espindola8121bec2016-06-27 20:19:14 +0000185 if (IsSharedLibrary) {
186 if (IsLocal)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000187 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000188 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000189 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000190 } else {
Rafael Espindola8121bec2016-06-27 20:19:14 +0000191 if (IsLocal)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000192 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000193 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000194 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000195 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000196
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000197 // If the user specified a more specific model, use that.
198 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
199 if (SelectedModel > Model)
200 return SelectedModel;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000201
202 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000203}
204
Rafael Espindola222a9d02016-06-30 12:44:52 +0000205/// Returns the optimization level: None, Less, Default, or Aggressive.
Rafael Espindolad86e8bb2016-06-30 18:25:11 +0000206CodeGenOpt::Level TargetMachine::getOptLevel() const { return OptLevel; }
Evan Chengecb29082011-11-16 08:38:26 +0000207
Rafael Espindolad86e8bb2016-06-30 18:25:11 +0000208void TargetMachine::setOptLevel(CodeGenOpt::Level Level) { OptLevel = Level; }
Paul Robinsond89125a2013-11-22 19:11:24 +0000209
Chandler Carruthe0385522015-02-01 10:11:22 +0000210TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
Malcolm Parsons17d266b2017-01-13 17:12:16 +0000211 return TargetIRAnalysis([](const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000212 return TargetTransformInfo(F.getParent()->getDataLayout());
213 });
Chandler Carruth705b1852015-01-31 03:43:40 +0000214}
215
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000216void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
Eric Christopherc4636b32016-09-20 22:03:28 +0000217 const GlobalValue *GV, Mangler &Mang,
218 bool MayAlwaysUsePrivate) const {
Eric Christopherc39f8b02016-10-14 17:28:23 +0000219 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
220 // Simple case: If GV is not private, it is not important to find out if
221 // private labels are legal in this case or not.
222 Mang.getNameWithPrefix(Name, GV, false);
223 return;
224 }
Eric Christopherc4636b32016-09-20 22:03:28 +0000225 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
226 TLOF->getNameWithPrefix(Name, GV, *this);
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000227}
228
Tim Northoverb64fb452016-11-22 16:17:20 +0000229MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV) const {
Eric Christopher36fe0282015-02-03 07:22:52 +0000230 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
Tim Northoverb64fb452016-11-22 16:17:20 +0000231 SmallString<128> NameStr;
232 getNameWithPrefix(NameStr, GV, TLOF->getMangler());
Jim Grosbach6f482002015-05-18 18:43:14 +0000233 return TLOF->getContext().getOrCreateSymbol(NameStr);
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000234}