blob: b703105eaf42019c2eb656db2e9ef2ee57e7064f [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"
Rafael Espindola8121bec2016-06-27 20:19:14 +000016#include "llvm/CodeGen/Analysis.h"
Bill Wendling965bd582013-03-13 22:26:59 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/GlobalAlias.h"
20#include "llvm/IR/GlobalValue.h"
21#include "llvm/IR/GlobalVariable.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000022#include "llvm/IR/LegacyPassManager.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000023#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000024#include "llvm/MC/MCAsmInfo.h"
Craig Topperd4a964c2012-03-25 18:10:17 +000025#include "llvm/MC/MCCodeGenInfo.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000026#include "llvm/MC/MCContext.h"
Eric Christopher72e23a22015-03-19 22:36:32 +000027#include "llvm/MC/MCInstrInfo.h"
Lang Hames1e923ec2015-01-09 18:55:42 +000028#include "llvm/MC/MCSectionMachO.h"
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000029#include "llvm/MC/MCTargetOptions.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000030#include "llvm/MC/SectionKind.h"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000031#include "llvm/Target/TargetLowering.h"
32#include "llvm/Target/TargetLoweringObjectFile.h"
Eric Christopherd9134482014-08-04 21:25:23 +000033#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner5d236002003-12-28 21:23:38 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Vikram S. Adve36d3e032002-09-16 15:39:26 +000036//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000037// TargetMachine Class
38//
Misha Brukman3decf862004-08-10 23:10:25 +000039
Mehdi Amini93e1ea12015-03-12 00:07:24 +000040TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
Daniel Sanders3e5de882015-06-11 19:41:26 +000041 const Triple &TT, StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000042 const TargetOptions &Options)
Mehdi Amini93e1ea12015-03-12 00:07:24 +000043 : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU),
Eric Christopher72e23a22015-03-19 22:36:32 +000044 TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr),
Eric Christopher12cf76f2015-03-19 22:36:37 +000045 MII(nullptr), STI(nullptr), RequireStructuredCFG(false),
46 Options(Options) {}
Anton Korobeynikov77d19432009-06-08 22:53:56 +000047
Chris Lattner5d236002003-12-28 21:23:38 +000048TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000049 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000050 delete AsmInfo;
Eric Christopher72e23a22015-03-19 22:36:32 +000051 delete MRI;
52 delete MII;
Eric Christopher12cf76f2015-03-19 22:36:37 +000053 delete STI;
Chris Lattner5d236002003-12-28 21:23:38 +000054}
55
Rafael Espindolaf9e348b2016-06-27 21:33:08 +000056bool TargetMachine::isPositionIndependent() const {
57 return getRelocationModel() == Reloc::PIC_;
58}
59
Bill Wendling965bd582013-03-13 22:26:59 +000060/// \brief Reset the target options based on the function's attributes.
Eric Christopher35a8a622015-04-28 18:09:05 +000061// FIXME: This function needs to go away for a number of reasons:
62// a) global state on the TargetMachine is terrible in general,
63// b) there's no default state here to keep,
64// c) these target options should be passed only on the function
65// and not on the TargetMachine (via TargetOptions) at all.
Eric Christopher3976f782014-09-26 01:28:10 +000066void TargetMachine::resetTargetOptions(const Function &F) const {
67#define RESET_OPTION(X, Y) \
68 do { \
Duncan P. N. Exon Smith025c0ad2015-02-14 15:36:52 +000069 if (F.hasFnAttribute(Y)) \
70 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
Bill Wendling965bd582013-03-13 22:26:59 +000071 } while (0)
72
Bill Wendling965bd582013-03-13 22:26:59 +000073 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
74 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
75 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
76 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
Bill Wendling965bd582013-03-13 22:26:59 +000077}
78
Rafael Espindola38af4d62016-05-18 16:00:24 +000079/// Returns the code generation relocation model. The choices are static, PIC,
Rafael Espindola8c34dd82016-05-18 22:04:49 +000080/// and dynamic-no-pic.
Evan Cheng2129f592011-07-19 06:37:02 +000081Reloc::Model TargetMachine::getRelocationModel() const {
82 if (!CodeGenInfo)
Rafael Espindola8c34dd82016-05-18 22:04:49 +000083 return Reloc::Static; // FIXME
Evan Cheng2129f592011-07-19 06:37:02 +000084 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +000085}
Evan Chengac4f66f2006-05-23 18:18:46 +000086
Evan Cheng04417462006-07-06 01:53:36 +000087/// getCodeModel - Returns the code model. The choices are small, kernel,
88/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +000089CodeModel::Model TargetMachine::getCodeModel() const {
90 if (!CodeGenInfo)
91 return CodeModel::Default;
92 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +000093}
94
Hans Wennborgcbe34b42012-06-23 11:37:03 +000095/// Get the IR-specified TLS model for Var.
Rafael Espindola59f7eba2014-05-28 18:15:43 +000096static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
97 switch (GV->getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +000098 case GlobalVariable::NotThreadLocal:
99 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
100 break;
101 case GlobalVariable::GeneralDynamicTLSModel:
102 return TLSModel::GeneralDynamic;
103 case GlobalVariable::LocalDynamicTLSModel:
104 return TLSModel::LocalDynamic;
105 case GlobalVariable::InitialExecTLSModel:
106 return TLSModel::InitialExec;
107 case GlobalVariable::LocalExecTLSModel:
108 return TLSModel::LocalExec;
109 }
110 llvm_unreachable("invalid TLS model");
111}
112
Rafael Espindola3beef8d2016-06-27 23:15:57 +0000113// FIXME: make this a proper option
114static bool CanUseCopyRelocWithPIE = false;
115
116bool TargetMachine::shouldAssumeDSOLocal(const Module &M,
117 const GlobalValue *GV) const {
118 Reloc::Model RM = getRelocationModel();
119 const Triple &TT = getTargetTriple();
120
121 // DLLImport explicitly marks the GV as external.
122 if (GV && GV->hasDLLImportStorageClass())
123 return false;
124
125 // Every other GV is local on COFF
126 if (TT.isOSBinFormatCOFF())
127 return true;
128
129 if (GV && (GV->hasLocalLinkage() || !GV->hasDefaultVisibility()))
130 return true;
131
132 if (TT.isOSBinFormatMachO()) {
133 if (RM == Reloc::Static)
134 return true;
135 return GV && GV->isStrongDefinitionForLinker();
136 }
137
138 assert(TT.isOSBinFormatELF());
139 assert(RM != Reloc::DynamicNoPIC);
140
141 bool IsExecutable =
142 RM == Reloc::Static || M.getPIELevel() != PIELevel::Default;
143 if (IsExecutable) {
144 // If the symbol is defined, it cannot be preempted.
145 if (GV && !GV->isDeclarationForLinker())
146 return true;
147
148 bool IsTLS = GV && GV->isThreadLocal();
149 // Check if we can use copy relocations.
150 if (!IsTLS && (RM == Reloc::Static || CanUseCopyRelocWithPIE))
151 return true;
152 }
153
154 // ELF supports preemption of other symbols.
155 return false;
156}
157
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000158TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindola8121bec2016-06-27 20:19:14 +0000159 bool IsPIE = GV->getParent()->getPIELevel() != PIELevel::Default;
160 Reloc::Model RM = getRelocationModel();
161 bool IsSharedLibrary = RM == Reloc::PIC_ && !IsPIE;
Rafael Espindola3beef8d2016-06-27 23:15:57 +0000162 bool IsLocal = shouldAssumeDSOLocal(*GV->getParent(), GV);
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000163
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000164 TLSModel::Model Model;
Rafael Espindola8121bec2016-06-27 20:19:14 +0000165 if (IsSharedLibrary) {
166 if (IsLocal)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000167 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000168 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000169 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000170 } else {
Rafael Espindola8121bec2016-06-27 20:19:14 +0000171 if (IsLocal)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000172 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000173 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000174 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000175 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000176
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000177 // If the user specified a more specific model, use that.
178 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
179 if (SelectedModel > Model)
180 return SelectedModel;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000181
182 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000183}
184
Evan Chengecb29082011-11-16 08:38:26 +0000185/// getOptLevel - Returns the optimization level: None, Less,
186/// Default, or Aggressive.
187CodeGenOpt::Level TargetMachine::getOptLevel() const {
188 if (!CodeGenInfo)
189 return CodeGenOpt::Default;
190 return CodeGenInfo->getOptLevel();
191}
192
Paul Robinsond89125a2013-11-22 19:11:24 +0000193void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
194 if (CodeGenInfo)
195 CodeGenInfo->setOptLevel(Level);
196}
197
Chandler Carruthe0385522015-02-01 10:11:22 +0000198TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000199 return TargetIRAnalysis([this](const Function &F) {
Mehdi Amini5010ebf2015-07-09 02:08:42 +0000200 return TargetTransformInfo(F.getParent()->getDataLayout());
201 });
Chandler Carruth705b1852015-01-31 03:43:40 +0000202}
203
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000204void 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 }
Eric Christopher36fe0282015-02-03 07:22:52 +0000213 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
Peter Collingbourne94d77862015-11-03 23:40:03 +0000214 TLOF->getNameWithPrefix(Name, GV, Mang, *this);
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000215}
216
217MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
Benjamin Kramer1577f1f2015-05-23 17:20:53 +0000218 SmallString<128> NameStr;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000219 getNameWithPrefix(NameStr, GV, Mang);
Eric Christopher36fe0282015-02-03 07:22:52 +0000220 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
Jim Grosbach6f482002015-05-18 18:43:14 +0000221 return TLOF->getContext().getOrCreateSymbol(NameStr);
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000222}