blob: e7282519d597f5d0b5e9b6ecf026c77852d91871 [file] [log] [blame]
Chris Lattnerb26bcc52001-09-14 05:34:53 +00001//===-- TargetMachine.cpp - General Target Information ---------------------==//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerb26bcc52001-09-14 05:34:53 +00009//
10// This file describes the general parts of a Target machine.
11//
12//===----------------------------------------------------------------------===//
Vikram S. Advedaae6992001-07-21 12:42:08 +000013
Chandler Carruthd04a8d42012-12-03 16:50:05 +000014#include "llvm/Target/TargetMachine.h"
Bill Wendling4cb1f5f2013-03-13 22:26:59 +000015#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/IR/Function.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/GlobalAlias.h"
18#include "llvm/IR/GlobalValue.h"
19#include "llvm/IR/GlobalVariable.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000020#include "llvm/MC/MCAsmInfo.h"
Craig Topper805853b2012-03-25 18:10:17 +000021#include "llvm/MC/MCCodeGenInfo.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerf70e0c22003-12-28 21:23:38 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Vikram S. Advee1f72802002-09-16 15:39:26 +000025//---------------------------------------------------------------------------
Brian Gaeke323819e2004-03-04 19:16:23 +000026// Command-line options that tend to be useful on more than one back-end.
27//
28
Misha Brukmanf90e4022004-06-21 21:44:12 +000029namespace llvm {
Evan Cheng2c69f8e2011-04-07 00:58:44 +000030 bool HasDivModLibcall;
Evan Cheng42bf74b2009-03-25 01:47:28 +000031 bool AsmVerbosityDefault(false);
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000032}
Misha Brukman0fb5a662004-06-21 21:08:45 +000033
Chris Lattner43ac7212010-04-13 00:36:43 +000034static cl::opt<bool>
35DataSections("fdata-sections",
36 cl::desc("Emit data into separate sections"),
37 cl::init(false));
38static cl::opt<bool>
39FunctionSections("ffunction-sections",
40 cl::desc("Emit functions into separate sections"),
41 cl::init(false));
Andrew Trick96f678f2012-01-13 06:30:30 +000042
Brian Gaeke323819e2004-03-04 19:16:23 +000043//---------------------------------------------------------------------------
Chris Lattnerf70e0c22003-12-28 21:23:38 +000044// TargetMachine Class
45//
Misha Brukman167deff2004-08-10 23:10:25 +000046
Evan Chengebdeeab2011-07-08 01:53:10 +000047TargetMachine::TargetMachine(const Target &T,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000048 StringRef TT, StringRef CPU, StringRef FS,
49 const TargetOptions &Options)
Benjamin Kramere236dc62011-07-20 01:27:58 +000050 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS),
51 CodeGenInfo(0), AsmInfo(0),
Rafael Espindola195a0ce2010-11-19 02:26:16 +000052 MCRelaxAll(false),
Rafael Espindoladd0dfdf2011-01-23 18:50:12 +000053 MCNoExecStack(false),
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +000054 MCSaveTempLabels(false),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +000055 MCUseLoc(true),
Nick Lewycky44d798d2011-10-17 23:05:28 +000056 MCUseCFI(true),
Nick Lewycky8a8d4792011-12-02 22:16:29 +000057 MCUseDwarfDirectory(false),
58 Options(Options) {
Anton Korobeynikov0eebf652009-06-08 22:53:56 +000059}
60
Chris Lattnerf70e0c22003-12-28 21:23:38 +000061TargetMachine::~TargetMachine() {
Benjamin Kramere236dc62011-07-20 01:27:58 +000062 delete CodeGenInfo;
Jim Laskeyfde1b3b2006-09-07 23:39:26 +000063 delete AsmInfo;
Chris Lattnerf70e0c22003-12-28 21:23:38 +000064}
65
Bill Wendling4cb1f5f2013-03-13 22:26:59 +000066/// \brief Reset the target options based on the function's attributes.
67void TargetMachine::resetTargetOptions(const MachineFunction *MF) const {
68 const Function *F = MF->getFunction();
69 TargetOptions &TO = MF->getTarget().Options;
70
71#define RESET_OPTION(X, Y) \
72 do { \
73 if (F->hasFnAttribute(Y)) \
74 TO.X = \
75 (F->getAttributes(). \
76 getAttribute(AttributeSet::FunctionIndex, \
77 Y).getValueAsString() == "true"); \
78 } while (0)
79
80 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
81 RESET_OPTION(NoFramePointerElimNonLeaf, "no-frame-pointer-elim-non-leaf");
82 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
83 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
84 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
85 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
86 RESET_OPTION(UseSoftFloat, "use-soft-float");
87 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
88}
89
Evan Cheng4c1aa862006-02-22 20:19:42 +000090/// getRelocationModel - Returns the code generation relocation model. The
91/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng43966132011-07-19 06:37:02 +000092Reloc::Model TargetMachine::getRelocationModel() const {
93 if (!CodeGenInfo)
94 return Reloc::Default;
95 return CodeGenInfo->getRelocationModel();
Evan Cheng4c1aa862006-02-22 20:19:42 +000096}
Evan Cheng80235d52006-05-23 18:18:46 +000097
Evan Cheng152ed052006-07-06 01:53:36 +000098/// getCodeModel - Returns the code model. The choices are small, kernel,
99/// medium, large, and target default.
Evan Cheng34ad6db2011-07-20 07:51:56 +0000100CodeModel::Model TargetMachine::getCodeModel() const {
101 if (!CodeGenInfo)
102 return CodeModel::Default;
103 return CodeGenInfo->getCodeModel();
Evan Cheng152ed052006-07-06 01:53:36 +0000104}
105
Hans Wennborgce718ff2012-06-23 11:37:03 +0000106/// Get the IR-specified TLS model for Var.
107static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
108 switch (Var->getThreadLocalMode()) {
109 case GlobalVariable::NotThreadLocal:
110 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
111 break;
112 case GlobalVariable::GeneralDynamicTLSModel:
113 return TLSModel::GeneralDynamic;
114 case GlobalVariable::LocalDynamicTLSModel:
115 return TLSModel::LocalDynamic;
116 case GlobalVariable::InitialExecTLSModel:
117 return TLSModel::InitialExec;
118 case GlobalVariable::LocalExecTLSModel:
119 return TLSModel::LocalExec;
120 }
121 llvm_unreachable("invalid TLS model");
122}
123
Chandler Carruth34797132012-04-08 17:20:55 +0000124TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolace0a5cd2012-06-23 00:30:03 +0000125 // If GV is an alias then use the aliasee for determining
126 // thread-localness.
127 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
128 GV = GA->resolveAliasedGlobal(false);
129 const GlobalVariable *Var = cast<GlobalVariable>(GV);
130
131 bool isLocal = Var->hasLocalLinkage();
132 bool isDeclaration = Var->isDeclaration();
Hans Wennborgce718ff2012-06-23 11:37:03 +0000133 bool isPIC = getRelocationModel() == Reloc::PIC_;
134 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth34797132012-04-08 17:20:55 +0000135 // FIXME: what should we do for protected and internal visibility?
136 // For variables, is internal different from hidden?
Rafael Espindolace0a5cd2012-06-23 00:30:03 +0000137 bool isHidden = Var->hasHiddenVisibility();
Chandler Carruth34797132012-04-08 17:20:55 +0000138
Hans Wennborgce718ff2012-06-23 11:37:03 +0000139 TLSModel::Model Model;
140 if (isPIC && !isPIE) {
Chandler Carruth34797132012-04-08 17:20:55 +0000141 if (isLocal || isHidden)
Hans Wennborgce718ff2012-06-23 11:37:03 +0000142 Model = TLSModel::LocalDynamic;
Chandler Carruth34797132012-04-08 17:20:55 +0000143 else
Hans Wennborgce718ff2012-06-23 11:37:03 +0000144 Model = TLSModel::GeneralDynamic;
Chandler Carruth34797132012-04-08 17:20:55 +0000145 } else {
146 if (!isDeclaration || isHidden)
Hans Wennborgce718ff2012-06-23 11:37:03 +0000147 Model = TLSModel::LocalExec;
Chandler Carruth34797132012-04-08 17:20:55 +0000148 else
Hans Wennborgce718ff2012-06-23 11:37:03 +0000149 Model = TLSModel::InitialExec;
Chandler Carruth34797132012-04-08 17:20:55 +0000150 }
Hans Wennborgce718ff2012-06-23 11:37:03 +0000151
152 // If the user specified a more specific model, use that.
153 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
154 if (SelectedModel > Model)
155 return SelectedModel;
156
157 return Model;
Chandler Carruth34797132012-04-08 17:20:55 +0000158}
159
Evan Chengb95fc312011-11-16 08:38:26 +0000160/// getOptLevel - Returns the optimization level: None, Less,
161/// Default, or Aggressive.
162CodeGenOpt::Level TargetMachine::getOptLevel() const {
163 if (!CodeGenInfo)
164 return CodeGenOpt::Default;
165 return CodeGenInfo->getOptLevel();
166}
167
Evan Cheng42bf74b2009-03-25 01:47:28 +0000168bool TargetMachine::getAsmVerbosityDefault() {
169 return AsmVerbosityDefault;
170}
171
172void TargetMachine::setAsmVerbosityDefault(bool V) {
173 AsmVerbosityDefault = V;
174}
175
Chris Lattner43ac7212010-04-13 00:36:43 +0000176bool TargetMachine::getFunctionSections() {
177 return FunctionSections;
178}
179
180bool TargetMachine::getDataSections() {
181 return DataSections;
182}
183
184void TargetMachine::setFunctionSections(bool V) {
185 FunctionSections = V;
186}
187
188void TargetMachine::setDataSections(bool V) {
189 DataSections = V;
190}