blob: 31dbdc6f9a7f2a32cb2b1072e6f860622668ce67 [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"
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +000021#include "llvm/IR/Mangler.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000022#include "llvm/MC/MCAsmInfo.h"
Craig Topperd4a964c2012-03-25 18:10:17 +000023#include "llvm/MC/MCCodeGenInfo.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"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000030#include "llvm/Support/CommandLine.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,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000041 StringRef TT, StringRef CPU, StringRef FS,
42 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),
45 MII(nullptr), RequireStructuredCFG(false), Options(Options) {}
Anton Korobeynikov77d19432009-06-08 22:53:56 +000046
Chris Lattner5d236002003-12-28 21:23:38 +000047TargetMachine::~TargetMachine() {
Benjamin Kramercc38ef62011-07-20 01:27:58 +000048 delete CodeGenInfo;
Jim Laskeyae92ce82006-09-07 23:39:26 +000049 delete AsmInfo;
Eric Christopher72e23a22015-03-19 22:36:32 +000050 delete MRI;
51 delete MII;
Chris Lattner5d236002003-12-28 21:23:38 +000052}
53
Bill Wendling965bd582013-03-13 22:26:59 +000054/// \brief Reset the target options based on the function's attributes.
Eric Christopher3976f782014-09-26 01:28:10 +000055void TargetMachine::resetTargetOptions(const Function &F) const {
56#define RESET_OPTION(X, Y) \
57 do { \
Duncan P. N. Exon Smith025c0ad2015-02-14 15:36:52 +000058 if (F.hasFnAttribute(Y)) \
59 Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \
Bill Wendling965bd582013-03-13 22:26:59 +000060 } while (0)
61
62 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim");
Bill Wendling965bd582013-03-13 22:26:59 +000063 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad");
64 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math");
65 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math");
66 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math");
67 RESET_OPTION(UseSoftFloat, "use-soft-float");
68 RESET_OPTION(DisableTailCalls, "disable-tail-calls");
Evgeniy Stepanov0a951b72014-04-23 11:16:03 +000069
Eric Christopher3976f782014-09-26 01:28:10 +000070 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress);
Bill Wendling965bd582013-03-13 22:26:59 +000071}
72
Evan Cheng73136df2006-02-22 20:19:42 +000073/// getRelocationModel - Returns the code generation relocation model. The
74/// choices are static, PIC, and dynamic-no-pic, and target default.
Evan Cheng2129f592011-07-19 06:37:02 +000075Reloc::Model TargetMachine::getRelocationModel() const {
76 if (!CodeGenInfo)
77 return Reloc::Default;
78 return CodeGenInfo->getRelocationModel();
Evan Cheng73136df2006-02-22 20:19:42 +000079}
Evan Chengac4f66f2006-05-23 18:18:46 +000080
Evan Cheng04417462006-07-06 01:53:36 +000081/// getCodeModel - Returns the code model. The choices are small, kernel,
82/// medium, large, and target default.
Evan Chengefd9b422011-07-20 07:51:56 +000083CodeModel::Model TargetMachine::getCodeModel() const {
84 if (!CodeGenInfo)
85 return CodeModel::Default;
86 return CodeGenInfo->getCodeModel();
Evan Cheng04417462006-07-06 01:53:36 +000087}
88
Hans Wennborgcbe34b42012-06-23 11:37:03 +000089/// Get the IR-specified TLS model for Var.
Rafael Espindola59f7eba2014-05-28 18:15:43 +000090static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) {
91 switch (GV->getThreadLocalMode()) {
Hans Wennborgcbe34b42012-06-23 11:37:03 +000092 case GlobalVariable::NotThreadLocal:
93 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
94 break;
95 case GlobalVariable::GeneralDynamicTLSModel:
96 return TLSModel::GeneralDynamic;
97 case GlobalVariable::LocalDynamicTLSModel:
98 return TLSModel::LocalDynamic;
99 case GlobalVariable::InitialExecTLSModel:
100 return TLSModel::InitialExec;
101 case GlobalVariable::LocalExecTLSModel:
102 return TLSModel::LocalExec;
103 }
104 llvm_unreachable("invalid TLS model");
105}
106
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000107TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
Rafael Espindolaa5bb2f62014-05-23 19:16:56 +0000108 bool isLocal = GV->hasLocalLinkage();
109 bool isDeclaration = GV->isDeclaration();
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000110 bool isPIC = getRelocationModel() == Reloc::PIC_;
111 bool isPIE = Options.PositionIndependentExecutable;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000112 // FIXME: what should we do for protected and internal visibility?
113 // For variables, is internal different from hidden?
Rafael Espindolaa5bb2f62014-05-23 19:16:56 +0000114 bool isHidden = GV->hasHiddenVisibility();
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000115
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000116 TLSModel::Model Model;
117 if (isPIC && !isPIE) {
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000118 if (isLocal || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000119 Model = TLSModel::LocalDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000120 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000121 Model = TLSModel::GeneralDynamic;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000122 } else {
123 if (!isDeclaration || isHidden)
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000124 Model = TLSModel::LocalExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000125 else
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000126 Model = TLSModel::InitialExec;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000127 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000128
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000129 // If the user specified a more specific model, use that.
130 TLSModel::Model SelectedModel = getSelectedTLSModel(GV);
131 if (SelectedModel > Model)
132 return SelectedModel;
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000133
134 return Model;
Chandler Carruth16f0ebc2012-04-08 17:20:55 +0000135}
136
Evan Chengecb29082011-11-16 08:38:26 +0000137/// getOptLevel - Returns the optimization level: None, Less,
138/// Default, or Aggressive.
139CodeGenOpt::Level TargetMachine::getOptLevel() const {
140 if (!CodeGenInfo)
141 return CodeGenOpt::Default;
142 return CodeGenInfo->getOptLevel();
143}
144
Paul Robinsond89125a2013-11-22 19:11:24 +0000145void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const {
146 if (CodeGenInfo)
147 CodeGenInfo->setOptLevel(Level);
148}
149
Chandler Carruthe0385522015-02-01 10:11:22 +0000150TargetIRAnalysis TargetMachine::getTargetIRAnalysis() {
Chandler Carruth8b04c0d2015-02-01 13:20:00 +0000151 return TargetIRAnalysis(
152 [this](Function &) { return TargetTransformInfo(getDataLayout()); });
Chandler Carruth705b1852015-01-31 03:43:40 +0000153}
154
Lang Hames1e923ec2015-01-09 18:55:42 +0000155static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
156 const MCSection &Section) {
157 if (!AsmInfo.isSectionAtomizableBySymbols(Section))
158 return true;
159
160 // If it is not dead stripped, it is safe to use private labels.
161 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section);
162 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP))
163 return true;
164
165 return false;
166}
167
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000168void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name,
169 const GlobalValue *GV, Mangler &Mang,
170 bool MayAlwaysUsePrivate) const {
171 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) {
172 // Simple case: If GV is not private, it is not important to find out if
173 // private labels are legal in this case or not.
174 Mang.getNameWithPrefix(Name, GV, false);
175 return;
176 }
177 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this);
Eric Christopher36fe0282015-02-03 07:22:52 +0000178 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
179 const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this);
Lang Hames1e923ec2015-01-09 18:55:42 +0000180 bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection);
David Majnemer7db449a2015-03-17 23:54:51 +0000181 TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this);
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000182}
183
184MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const {
185 SmallString<60> NameStr;
186 getNameWithPrefix(NameStr, GV, Mang);
Eric Christopher36fe0282015-02-03 07:22:52 +0000187 const TargetLoweringObjectFile *TLOF = getObjFileLowering();
188 return TLOF->getContext().GetOrCreateSymbol(NameStr.str());
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000189}