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" |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
| 16 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/GlobalAlias.h" |
| 18 | #include "llvm/IR/GlobalValue.h" |
| 19 | #include "llvm/IR/GlobalVariable.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Mangler.h" |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmInfo.h" |
Craig Topper | d4a964c | 2012-03-25 18:10:17 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCCodeGenInfo.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCContext.h" |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCTargetOptions.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 25 | #include "llvm/MC/SectionKind.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 26 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetLowering.h" |
| 28 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Vikram S. Adve | 36d3e03 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 31 | //--------------------------------------------------------------------------- |
Brian Gaeke | 8351d8c | 2004-03-04 19:16:23 +0000 | [diff] [blame] | 32 | // Command-line options that tend to be useful on more than one back-end. |
| 33 | // |
| 34 | |
Misha Brukman | 96041e5 | 2004-06-21 21:44:12 +0000 | [diff] [blame] | 35 | namespace llvm { |
Evan Cheng | 5e5a63c | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 36 | bool AsmVerbosityDefault(false); |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 37 | } |
Misha Brukman | 069ca06 | 2004-06-21 21:08:45 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 39 | static cl::opt<bool> |
| 40 | DataSections("fdata-sections", |
| 41 | cl::desc("Emit data into separate sections"), |
| 42 | cl::init(false)); |
| 43 | static cl::opt<bool> |
| 44 | FunctionSections("ffunction-sections", |
| 45 | cl::desc("Emit functions into separate sections"), |
| 46 | cl::init(false)); |
Andrew Trick | e77e84e | 2012-01-13 06:30:30 +0000 | [diff] [blame] | 47 | |
Brian Gaeke | 8351d8c | 2004-03-04 19:16:23 +0000 | [diff] [blame] | 48 | //--------------------------------------------------------------------------- |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 49 | // TargetMachine Class |
| 50 | // |
Misha Brukman | 3decf86 | 2004-08-10 23:10:25 +0000 | [diff] [blame] | 51 | |
Evan Cheng | 4d1ca96 | 2011-07-08 01:53:10 +0000 | [diff] [blame] | 52 | TargetMachine::TargetMachine(const Target &T, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 53 | StringRef TT, StringRef CPU, StringRef FS, |
| 54 | const TargetOptions &Options) |
Benjamin Kramer | cc38ef6 | 2011-07-20 01:27:58 +0000 | [diff] [blame] | 55 | : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS), |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 56 | CodeGenInfo(nullptr), AsmInfo(nullptr), |
Vincent Lejeune | 92b0a64 | 2013-12-07 01:49:19 +0000 | [diff] [blame] | 57 | RequireStructuredCFG(false), |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 58 | Options(Options) { |
Anton Korobeynikov | 77d1943 | 2009-06-08 22:53:56 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 61 | TargetMachine::~TargetMachine() { |
Benjamin Kramer | cc38ef6 | 2011-07-20 01:27:58 +0000 | [diff] [blame] | 62 | delete CodeGenInfo; |
Jim Laskey | ae92ce8 | 2006-09-07 23:39:26 +0000 | [diff] [blame] | 63 | delete AsmInfo; |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 66 | /// \brief Reset the target options based on the function's attributes. |
| 67 | void TargetMachine::resetTargetOptions(const MachineFunction *MF) const { |
| 68 | const Function *F = MF->getFunction(); |
| 69 | TargetOptions &TO = MF->getTarget().Options; |
NAKAMURA Takumi | 66c9543 | 2013-11-21 11:08:31 +0000 | [diff] [blame] | 70 | |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 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"); |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 81 | RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); |
| 82 | RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); |
| 83 | RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); |
| 84 | RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); |
| 85 | RESET_OPTION(UseSoftFloat, "use-soft-float"); |
| 86 | RESET_OPTION(DisableTailCalls, "disable-tail-calls"); |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 87 | |
| 88 | TO.MCOptions.SanitizeAddress = F->hasFnAttribute(Attribute::SanitizeAddress); |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 91 | /// getRelocationModel - Returns the code generation relocation model. The |
| 92 | /// choices are static, PIC, and dynamic-no-pic, and target default. |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 93 | Reloc::Model TargetMachine::getRelocationModel() const { |
| 94 | if (!CodeGenInfo) |
| 95 | return Reloc::Default; |
| 96 | return CodeGenInfo->getRelocationModel(); |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 97 | } |
Evan Cheng | ac4f66f | 2006-05-23 18:18:46 +0000 | [diff] [blame] | 98 | |
Evan Cheng | 0441746 | 2006-07-06 01:53:36 +0000 | [diff] [blame] | 99 | /// getCodeModel - Returns the code model. The choices are small, kernel, |
| 100 | /// medium, large, and target default. |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 101 | CodeModel::Model TargetMachine::getCodeModel() const { |
| 102 | if (!CodeGenInfo) |
| 103 | return CodeModel::Default; |
| 104 | return CodeGenInfo->getCodeModel(); |
Evan Cheng | 0441746 | 2006-07-06 01:53:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 107 | /// Get the IR-specified TLS model for Var. |
| 108 | static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) { |
| 109 | switch (Var->getThreadLocalMode()) { |
| 110 | case GlobalVariable::NotThreadLocal: |
| 111 | llvm_unreachable("getSelectedTLSModel for non-TLS variable"); |
| 112 | break; |
| 113 | case GlobalVariable::GeneralDynamicTLSModel: |
| 114 | return TLSModel::GeneralDynamic; |
| 115 | case GlobalVariable::LocalDynamicTLSModel: |
| 116 | return TLSModel::LocalDynamic; |
| 117 | case GlobalVariable::InitialExecTLSModel: |
| 118 | return TLSModel::InitialExec; |
| 119 | case GlobalVariable::LocalExecTLSModel: |
| 120 | return TLSModel::LocalExec; |
| 121 | } |
| 122 | llvm_unreachable("invalid TLS model"); |
| 123 | } |
| 124 | |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 125 | TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { |
Rafael Espindola | a3088f0 | 2012-06-23 00:30:03 +0000 | [diff] [blame] | 126 | // If GV is an alias then use the aliasee for determining |
| 127 | // thread-localness. |
| 128 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) |
Rafael Espindola | e009892 | 2014-05-16 22:37:03 +0000 | [diff] [blame^] | 129 | GV = GA->getAliasee(); |
Rafael Espindola | a3088f0 | 2012-06-23 00:30:03 +0000 | [diff] [blame] | 130 | const GlobalVariable *Var = cast<GlobalVariable>(GV); |
| 131 | |
| 132 | bool isLocal = Var->hasLocalLinkage(); |
| 133 | bool isDeclaration = Var->isDeclaration(); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 134 | bool isPIC = getRelocationModel() == Reloc::PIC_; |
| 135 | bool isPIE = Options.PositionIndependentExecutable; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 136 | // FIXME: what should we do for protected and internal visibility? |
| 137 | // For variables, is internal different from hidden? |
Rafael Espindola | a3088f0 | 2012-06-23 00:30:03 +0000 | [diff] [blame] | 138 | bool isHidden = Var->hasHiddenVisibility(); |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 139 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 140 | TLSModel::Model Model; |
| 141 | if (isPIC && !isPIE) { |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 142 | if (isLocal || isHidden) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 143 | Model = TLSModel::LocalDynamic; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 144 | else |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 145 | Model = TLSModel::GeneralDynamic; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 146 | } else { |
| 147 | if (!isDeclaration || isHidden) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 148 | Model = TLSModel::LocalExec; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 149 | else |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 150 | Model = TLSModel::InitialExec; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 151 | } |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 152 | |
| 153 | // If the user specified a more specific model, use that. |
| 154 | TLSModel::Model SelectedModel = getSelectedTLSModel(Var); |
| 155 | if (SelectedModel > Model) |
| 156 | return SelectedModel; |
| 157 | |
| 158 | return Model; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 161 | /// getOptLevel - Returns the optimization level: None, Less, |
| 162 | /// Default, or Aggressive. |
| 163 | CodeGenOpt::Level TargetMachine::getOptLevel() const { |
| 164 | if (!CodeGenInfo) |
| 165 | return CodeGenOpt::Default; |
| 166 | return CodeGenInfo->getOptLevel(); |
| 167 | } |
| 168 | |
Paul Robinson | d89125a | 2013-11-22 19:11:24 +0000 | [diff] [blame] | 169 | void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { |
| 170 | if (CodeGenInfo) |
| 171 | CodeGenInfo->setOptLevel(Level); |
| 172 | } |
| 173 | |
Evan Cheng | 5e5a63c | 2009-03-25 01:47:28 +0000 | [diff] [blame] | 174 | bool TargetMachine::getAsmVerbosityDefault() { |
| 175 | return AsmVerbosityDefault; |
| 176 | } |
| 177 | |
| 178 | void TargetMachine::setAsmVerbosityDefault(bool V) { |
| 179 | AsmVerbosityDefault = V; |
| 180 | } |
| 181 | |
Chris Lattner | 5b212a3 | 2010-04-13 00:36:43 +0000 | [diff] [blame] | 182 | bool TargetMachine::getFunctionSections() { |
| 183 | return FunctionSections; |
| 184 | } |
| 185 | |
| 186 | bool TargetMachine::getDataSections() { |
| 187 | return DataSections; |
| 188 | } |
| 189 | |
| 190 | void TargetMachine::setFunctionSections(bool V) { |
| 191 | FunctionSections = V; |
| 192 | } |
| 193 | |
| 194 | void TargetMachine::setDataSections(bool V) { |
| 195 | DataSections = V; |
| 196 | } |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 197 | |
| 198 | void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, |
| 199 | const GlobalValue *GV, Mangler &Mang, |
| 200 | bool MayAlwaysUsePrivate) const { |
| 201 | if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { |
| 202 | // Simple case: If GV is not private, it is not important to find out if |
| 203 | // private labels are legal in this case or not. |
| 204 | Mang.getNameWithPrefix(Name, GV, false); |
| 205 | return; |
| 206 | } |
| 207 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this); |
| 208 | const TargetLoweringObjectFile &TLOF = |
| 209 | getTargetLowering()->getObjFileLowering(); |
| 210 | const MCSection *TheSection = TLOF.SectionForGlobal(GV, GVKind, Mang, *this); |
| 211 | bool CannotUsePrivateLabel = TLOF.isSectionAtomizableBySymbols(*TheSection); |
| 212 | Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel); |
| 213 | } |
| 214 | |
| 215 | MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { |
| 216 | SmallString<60> NameStr; |
| 217 | getNameWithPrefix(NameStr, GV, Mang); |
| 218 | const TargetLoweringObjectFile &TLOF = |
| 219 | getTargetLowering()->getObjFileLowering(); |
| 220 | return TLOF.getContext().GetOrCreateSymbol(NameStr.str()); |
| 221 | } |