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