Chris Lattner | 22a6a90 | 2001-09-14 05:34:53 +0000 | [diff] [blame] | 1 | //===-- TargetMachine.cpp - General Target Information ---------------------==// |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 10468d8 | 2005-04-21 22:55:34 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 22a6a90 | 2001-09-14 05:34:53 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file describes the general parts of a Target machine. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 3414e78 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 13 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/Target/TargetMachine.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/TargetTransformInfo.h" |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/MachineFunction.h" |
| 17 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/GlobalAlias.h" |
| 19 | #include "llvm/IR/GlobalValue.h" |
| 20 | #include "llvm/IR/GlobalVariable.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Mangler.h" |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCAsmInfo.h" |
Craig Topper | d4a964c | 2012-03-25 18:10:17 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCCodeGenInfo.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCContext.h" |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSectionMachO.h" |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCTargetOptions.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 27 | #include "llvm/MC/SectionKind.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 28 | #include "llvm/PassManager.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetLowering.h" |
| 31 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Vikram S. Adve | 36d3e03 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 35 | //--------------------------------------------------------------------------- |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 36 | // TargetMachine Class |
| 37 | // |
Misha Brukman | 3decf86 | 2004-08-10 23:10:25 +0000 | [diff] [blame] | 38 | |
Evan Cheng | 4d1ca96 | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 39 | TargetMachine::TargetMachine(const Target &T, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 40 | StringRef TT, StringRef CPU, StringRef FS, |
| 41 | const TargetOptions &Options) |
Benjamin Kramer | cc38ef6 | 2011-07-20 01:27:58 +0000 | [diff] [blame] | 42 | : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS), |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 43 | CodeGenInfo(nullptr), AsmInfo(nullptr), |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 44 | RequireStructuredCFG(false), |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 45 | Options(Options) { |
Anton Korobeynikov | 77d1943 | 2009-06-08 22:53:56 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 48 | TargetMachine::~TargetMachine() { |
Benjamin Kramer | cc38ef6 | 2011-07-20 01:27:58 +0000 | [diff] [blame] | 49 | delete CodeGenInfo; |
Jim Laskey | ae92ce8 | 2006-09-07 23:39:26 +0000 | [diff] [blame] | 50 | delete AsmInfo; |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 53 | /// \brief Reset the target options based on the function's attributes. |
Eric Christopher | 3976f78 | 2014-09-26 01:28:10 +0000 | [diff] [blame] | 54 | void TargetMachine::resetTargetOptions(const Function &F) const { |
| 55 | #define RESET_OPTION(X, Y) \ |
| 56 | do { \ |
| 57 | if (F.hasFnAttribute(Y)) \ |
| 58 | Options.X = (F.getAttributes() \ |
| 59 | .getAttribute(AttributeSet::FunctionIndex, Y) \ |
| 60 | .getValueAsString() == "true"); \ |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 61 | } while (0) |
| 62 | |
| 63 | RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim"); |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 64 | RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); |
| 65 | RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); |
| 66 | RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); |
| 67 | RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); |
| 68 | RESET_OPTION(UseSoftFloat, "use-soft-float"); |
| 69 | RESET_OPTION(DisableTailCalls, "disable-tail-calls"); |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 70 | |
Eric Christopher | 3976f78 | 2014-09-26 01:28:10 +0000 | [diff] [blame] | 71 | Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress); |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 74 | /// getRelocationModel - Returns the code generation relocation model. The |
| 75 | /// choices are static, PIC, and dynamic-no-pic, and target default. |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 76 | Reloc::Model TargetMachine::getRelocationModel() const { |
| 77 | if (!CodeGenInfo) |
| 78 | return Reloc::Default; |
| 79 | return CodeGenInfo->getRelocationModel(); |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 80 | } |
Evan Cheng | ac4f66f | 2006-05-23 18:18:46 +0000 | [diff] [blame] | 81 | |
Evan Cheng | 0441746 | 2006-07-06 01:53:36 +0000 | [diff] [blame] | 82 | /// getCodeModel - Returns the code model. The choices are small, kernel, |
| 83 | /// medium, large, and target default. |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 84 | CodeModel::Model TargetMachine::getCodeModel() const { |
| 85 | if (!CodeGenInfo) |
| 86 | return CodeModel::Default; |
| 87 | return CodeGenInfo->getCodeModel(); |
Evan Cheng | 0441746 | 2006-07-06 01:53:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 90 | /// Get the IR-specified TLS model for Var. |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 91 | static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { |
| 92 | switch (GV->getThreadLocalMode()) { |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 93 | case GlobalVariable::NotThreadLocal: |
| 94 | llvm_unreachable("getSelectedTLSModel for non-TLS variable"); |
| 95 | break; |
| 96 | case GlobalVariable::GeneralDynamicTLSModel: |
| 97 | return TLSModel::GeneralDynamic; |
| 98 | case GlobalVariable::LocalDynamicTLSModel: |
| 99 | return TLSModel::LocalDynamic; |
| 100 | case GlobalVariable::InitialExecTLSModel: |
| 101 | return TLSModel::InitialExec; |
| 102 | case GlobalVariable::LocalExecTLSModel: |
| 103 | return TLSModel::LocalExec; |
| 104 | } |
| 105 | llvm_unreachable("invalid TLS model"); |
| 106 | } |
| 107 | |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 108 | TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { |
Rafael Espindola | a5bb2f6 | 2014-05-23 19:16:56 +0000 | [diff] [blame] | 109 | bool isLocal = GV->hasLocalLinkage(); |
| 110 | bool isDeclaration = GV->isDeclaration(); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 111 | bool isPIC = getRelocationModel() == Reloc::PIC_; |
| 112 | bool isPIE = Options.PositionIndependentExecutable; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 113 | // FIXME: what should we do for protected and internal visibility? |
| 114 | // For variables, is internal different from hidden? |
Rafael Espindola | a5bb2f6 | 2014-05-23 19:16:56 +0000 | [diff] [blame] | 115 | bool isHidden = GV->hasHiddenVisibility(); |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 116 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 117 | TLSModel::Model Model; |
| 118 | if (isPIC && !isPIE) { |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 119 | if (isLocal || isHidden) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 120 | Model = TLSModel::LocalDynamic; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 121 | else |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 122 | Model = TLSModel::GeneralDynamic; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 123 | } else { |
| 124 | if (!isDeclaration || isHidden) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 125 | Model = TLSModel::LocalExec; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 126 | else |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 127 | Model = TLSModel::InitialExec; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 128 | } |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 129 | |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 130 | // If the user specified a more specific model, use that. |
| 131 | TLSModel::Model SelectedModel = getSelectedTLSModel(GV); |
| 132 | if (SelectedModel > Model) |
| 133 | return SelectedModel; |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 134 | |
| 135 | return Model; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 138 | /// getOptLevel - Returns the optimization level: None, Less, |
| 139 | /// Default, or Aggressive. |
| 140 | CodeGenOpt::Level TargetMachine::getOptLevel() const { |
| 141 | if (!CodeGenInfo) |
| 142 | return CodeGenOpt::Default; |
| 143 | return CodeGenInfo->getOptLevel(); |
| 144 | } |
| 145 | |
Paul Robinson | d89125a | 2013-11-22 19:11:24 +0000 | [diff] [blame] | 146 | void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { |
| 147 | if (CodeGenInfo) |
| 148 | CodeGenInfo->setOptLevel(Level); |
| 149 | } |
| 150 | |
Eric Christopher | eb71972 | 2014-05-20 23:59:50 +0000 | [diff] [blame] | 151 | bool TargetMachine::getAsmVerbosityDefault() const { |
| 152 | return Options.MCOptions.AsmVerbose; |
Evan Cheng | 5e5a63c | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void TargetMachine::setAsmVerbosityDefault(bool V) { |
Eric Christopher | eb71972 | 2014-05-20 23:59:50 +0000 | [diff] [blame] | 156 | Options.MCOptions.AsmVerbose = V; |
Evan Cheng | 5e5a63c | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Eric Christopher | 2feed5f | 2014-05-20 21:25:34 +0000 | [diff] [blame] | 159 | bool TargetMachine::getFunctionSections() const { |
| 160 | return Options.FunctionSections; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Eric Christopher | 2feed5f | 2014-05-20 21:25:34 +0000 | [diff] [blame] | 163 | bool TargetMachine::getDataSections() const { |
| 164 | return Options.DataSections; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void TargetMachine::setFunctionSections(bool V) { |
Eric Christopher | 2feed5f | 2014-05-20 21:25:34 +0000 | [diff] [blame] | 168 | Options.FunctionSections = V; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void TargetMachine::setDataSections(bool V) { |
Eric Christopher | 2feed5f | 2014-05-20 21:25:34 +0000 | [diff] [blame] | 172 | Options.DataSections = V; |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 173 | } |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 174 | |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 175 | TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { |
Chandler Carruth | 8b04c0d | 2015-02-01 13:20:00 +0000 | [diff] [blame^] | 176 | return TargetIRAnalysis( |
| 177 | [this](Function &) { return TargetTransformInfo(getDataLayout()); }); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 180 | static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, |
| 181 | const MCSection &Section) { |
| 182 | if (!AsmInfo.isSectionAtomizableBySymbols(Section)) |
| 183 | return true; |
| 184 | |
| 185 | // If it is not dead stripped, it is safe to use private labels. |
| 186 | const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); |
| 187 | if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) |
| 188 | return true; |
| 189 | |
| 190 | return false; |
| 191 | } |
| 192 | |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 193 | void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, |
| 194 | const GlobalValue *GV, Mangler &Mang, |
| 195 | bool MayAlwaysUsePrivate) const { |
| 196 | if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { |
| 197 | // Simple case: If GV is not private, it is not important to find out if |
| 198 | // private labels are legal in this case or not. |
| 199 | Mang.getNameWithPrefix(Name, GV, false); |
| 200 | return; |
| 201 | } |
| 202 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this); |
| 203 | const TargetLoweringObjectFile &TLOF = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 204 | getSubtargetImpl()->getTargetLowering()->getObjFileLowering(); |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 205 | const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this); |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 206 | bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection); |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 207 | Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel); |
| 208 | } |
| 209 | |
| 210 | MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { |
| 211 | SmallString<60> NameStr; |
| 212 | getNameWithPrefix(NameStr, GV, Mang); |
| 213 | const TargetLoweringObjectFile &TLOF = |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 214 | getSubtargetImpl()->getTargetLowering()->getObjFileLowering(); |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 215 | return TLOF.getContext().GetOrCreateSymbol(NameStr.str()); |
| 216 | } |