blob: 70088b1a4976b88482d145d3975986c0f4fecd7f [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"
Rafael Espindolaa3088f02012-06-23 00:30:03 +000015#include "llvm/GlobalAlias.h"
Chandler Carruth16f0ebc2012-04-08 17:20:55 +000016#include "llvm/GlobalValue.h"
Rafael Espindolaa3088f02012-06-23 00:30:03 +000017#include "llvm/GlobalVariable.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000018#include "llvm/MC/MCAsmInfo.h"
Craig Topperd4a964c2012-03-25 18:10:17 +000019#include "llvm/MC/MCCodeGenInfo.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
Chris Lattner5d236002003-12-28 21:23:38 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Vikram S. Adve36d3e032002-09-16 15:39:26 +000023//---------------------------------------------------------------------------
Brian Gaeke8351d8c2004-03-04 19:16:23 +000024// Command-line options that tend to be useful on more than one back-end.
25//
26
Misha Brukman96041e52004-06-21 21:44:12 +000027namespace llvm {
Evan Chenga7c7b542011-04-07 00:58:44 +000028 bool HasDivModLibcall;
Evan Cheng5e5a63c2009-03-25 01:47:28 +000029 bool AsmVerbosityDefault(false);
Chris Lattneraa2372562006-05-24 17:04:05 +000030}
Misha Brukman069ca062004-06-21 21:08:45 +000031
Chris Lattner5b212a32010-04-13 00:36:43 +000032static cl::opt<bool>
33DataSections("fdata-sections",
34 cl::desc("Emit data into separate sections"),
35 cl::init(false));
36static cl::opt<bool>
37FunctionSections("ffunction-sections",
38 cl::desc("Emit functions into separate sections"),
39 cl::init(false));
Andrew Tricke77e84e2012-01-13 06:30:30 +000040
Brian Gaeke8351d8c2004-03-04 19:16:23 +000041//---------------------------------------------------------------------------
Chris Lattner5d236002003-12-28 21:23:38 +000042// TargetMachine Class
43//
Misha Brukman3decf862004-08-10 23:10:25 +000044
Evan Cheng4d1ca962011-07-08 01:53:10 +000045TargetMachine::TargetMachine(const Target &T,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000046 StringRef TT, StringRef CPU, StringRef FS,
47 const TargetOptions &Options)
Benjamin Kramercc38ef62011-07-20 01:27:58 +000048 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
49 CodeGenInfo(0), AsmInfo(0),
Rafael Espindolab58867c2010-11-19 02:26:16 +000050 MCRelaxAll(false),
Rafael Espindolac5efca42011-01-23 18:50:12 +000051 MCNoExecStack(false),
Daniel Dunbar3e2b3352011-03-28 22:49:19 +000052 MCSaveTempLabels(false),
Rafael Espindolaa3181d12011-04-30 03:44:37 +000053 MCUseLoc(true),
Nick Lewycky40f8f2f2011-10-17 23:05:28 +000054 MCUseCFI(true),
Nick Lewycky50f02cb2011-12-02 22:16:29 +000055 MCUseDwarfDirectory(false),
56 Options(Options) {
Anton Korobeynikov77d19432009-06-08 22:53:56 +000057}
58
Chris Lattner5d236002003-12-28 21:23:38 +000059TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000060 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000061 delete AsmInfo;
Chris Lattner5d236002003-12-28 21:23:38 +000062}
63
Evan Cheng73136df2006-02-22 20:19:42 +000064/// getRelocationModel - Returns the code generation relocation model. The
65/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +000066Reloc::Model TargetMachine::getRelocationModel() const {
67 if (!CodeGenInfo)
68 return Reloc::Default;
69 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +000070}
Evan Chengac4f66f2006-05-23 18:18:46 +000071
Evan Cheng04417462006-07-06 01:53:36 +000072/// getCodeModel - Returns the code model. The choices are small, kernel,
73/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +000074CodeModel::Model TargetMachine::getCodeModel() const {
75 if (!CodeGenInfo)
76 return CodeModel::Default;
77 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +000078}
79
Hans Wennborgcbe34b42012-06-23 11:37:03 +000080/// Get the IR-specified TLS model for Var.
81static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
82 switch (Var->getThreadLocalMode()) {
83 case GlobalVariable::NotThreadLocal:
84 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
85 break;
86 case GlobalVariable::GeneralDynamicTLSModel:
87 return TLSModel::GeneralDynamic;
88 case GlobalVariable::LocalDynamicTLSModel:
89 return TLSModel::LocalDynamic;
90 case GlobalVariable::InitialExecTLSModel:
91 return TLSModel::InitialExec;
92 case GlobalVariable::LocalExecTLSModel:
93 return TLSModel::LocalExec;
94 }
95 llvm_unreachable("invalid TLS model");
96}
97
Chandler Carruth16f0ebc2012-04-08 17:20:55 +000098TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolaa3088f02012-06-23 00:30:03 +000099 // If GV is an alias then use the aliasee for determining
100 // thread-localness.
101 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
102 GV = GA->resolveAliasedGlobal(false);
103 const GlobalVariable *Var = cast<GlobalVariable>(GV);
104
105 bool isLocal = Var->hasLocalLinkage();
106 bool isDeclaration = Var->isDeclaration();
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000107 bool isPIC = getRelocationModel() == Reloc::PIC_;
108 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000109 // FIXME: what should we do for protected and internal visibility?
110 // For variables, is internal different from hidden?
Rafael Espindolaa3088f02012-06-23 00:30:03 +0000111 bool isHidden = Var->hasHiddenVisibility();
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000112
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000113 TLSModel::Model Model;
114 if (isPIC && !isPIE) {
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000115 if (isLocal || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000116 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000117 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000118 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000119 } else {
120 if (!isDeclaration || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000121 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000122 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000123 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000124 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000125
126 // If the user specified a more specific model, use that.
127 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
128 if (SelectedModel > Model)
129 return SelectedModel;
130
131 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000132}
133
Evan Chengecb29082011-11-16 08:38:26 +0000134/// getOptLevel - Returns the optimization level: None, Less,
135/// Default, or Aggressive.
136CodeGenOpt::Level TargetMachine::getOptLevel() const {
137 if (!CodeGenInfo)
138 return CodeGenOpt::Default;
139 return CodeGenInfo->getOptLevel();
140}
141
Evan Cheng5e5a63c2009-03-25 01:47:28 +0000142bool TargetMachine::getAsmVerbosityDefault() {
143 return AsmVerbosityDefault;
144}
145
146void TargetMachine::setAsmVerbosityDefault(bool V) {
147 AsmVerbosityDefault = V;
148}
149
Chris Lattner5b212a32010-04-13 00:36:43 +0000150bool TargetMachine::getFunctionSections() {
151 return FunctionSections;
152}
153
154bool TargetMachine::getDataSections() {
155 return DataSections;
156}
157
158void TargetMachine::setFunctionSections(bool V) {
159 FunctionSections = V;
160}
161
162void TargetMachine::setDataSections(bool V) {
163 DataSections = V;
164}