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" |
Eric Christopher | 72e23a2 | 2015-03-19 22:36:32 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCInstrInfo.h" |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSectionMachO.h" |
Evgeniy Stepanov | 0a951b7 | 2014-04-23 11:16:03 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCTargetOptions.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 28 | #include "llvm/MC/SectionKind.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 29 | #include "llvm/IR/LegacyPassManager.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CommandLine.h" |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetLowering.h" |
| 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetSubtargetInfo.h" |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | |
Vikram S. Adve | 36d3e03 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 36 | //--------------------------------------------------------------------------- |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 37 | // TargetMachine Class |
| 38 | // |
Misha Brukman | 3decf86 | 2004-08-10 23:10:25 +0000 | [diff] [blame] | 39 | |
Mehdi Amini | 93e1ea1 | 2015-03-12 00:07:24 +0000 | [diff] [blame] | 40 | TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString, |
Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 41 | const Triple &TT, StringRef CPU, StringRef FS, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 42 | const TargetOptions &Options) |
Mehdi Amini | 93e1ea1 | 2015-03-12 00:07:24 +0000 | [diff] [blame] | 43 | : TheTarget(T), DL(DataLayoutString), TargetTriple(TT), TargetCPU(CPU), |
Eric Christopher | 72e23a2 | 2015-03-19 22:36:32 +0000 | [diff] [blame] | 44 | TargetFS(FS), CodeGenInfo(nullptr), AsmInfo(nullptr), MRI(nullptr), |
Eric Christopher | 12cf76f | 2015-03-19 22:36:37 +0000 | [diff] [blame] | 45 | MII(nullptr), STI(nullptr), RequireStructuredCFG(false), |
| 46 | Options(Options) {} |
Anton Korobeynikov | 77d1943 | 2009-06-08 22:53:56 +0000 | [diff] [blame] | 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; |
Eric Christopher | 72e23a2 | 2015-03-19 22:36:32 +0000 | [diff] [blame] | 51 | delete MRI; |
| 52 | delete MII; |
Eric Christopher | 12cf76f | 2015-03-19 22:36:37 +0000 | [diff] [blame] | 53 | delete STI; |
Chris Lattner | 5d23600 | 2003-12-28 21:23:38 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 56 | /// \brief Reset the target options based on the function's attributes. |
Eric Christopher | 35a8a62 | 2015-04-28 18:09:05 +0000 | [diff] [blame] | 57 | // FIXME: This function needs to go away for a number of reasons: |
| 58 | // a) global state on the TargetMachine is terrible in general, |
| 59 | // b) there's no default state here to keep, |
| 60 | // c) these target options should be passed only on the function |
| 61 | // and not on the TargetMachine (via TargetOptions) at all. |
Eric Christopher | 3976f78 | 2014-09-26 01:28:10 +0000 | [diff] [blame] | 62 | void TargetMachine::resetTargetOptions(const Function &F) const { |
| 63 | #define RESET_OPTION(X, Y) \ |
| 64 | do { \ |
Duncan P. N. Exon Smith | 025c0ad | 2015-02-14 15:36:52 +0000 | [diff] [blame] | 65 | if (F.hasFnAttribute(Y)) \ |
| 66 | Options.X = (F.getFnAttribute(Y).getValueAsString() == "true"); \ |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 67 | } while (0) |
| 68 | |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 69 | RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); |
| 70 | RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); |
| 71 | RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); |
| 72 | RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); |
Bill Wendling | 965bd58 | 2013-03-13 22:26:59 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 75 | /// getRelocationModel - Returns the code generation relocation model. The |
| 76 | /// choices are static, PIC, and dynamic-no-pic, and target default. |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 77 | Reloc::Model TargetMachine::getRelocationModel() const { |
| 78 | if (!CodeGenInfo) |
| 79 | return Reloc::Default; |
| 80 | return CodeGenInfo->getRelocationModel(); |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 81 | } |
Evan Cheng | ac4f66f | 2006-05-23 18:18:46 +0000 | [diff] [blame] | 82 | |
Evan Cheng | 0441746 | 2006-07-06 01:53:36 +0000 | [diff] [blame] | 83 | /// getCodeModel - Returns the code model. The choices are small, kernel, |
| 84 | /// medium, large, and target default. |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 85 | CodeModel::Model TargetMachine::getCodeModel() const { |
| 86 | if (!CodeGenInfo) |
| 87 | return CodeModel::Default; |
| 88 | return CodeGenInfo->getCodeModel(); |
Evan Cheng | 0441746 | 2006-07-06 01:53:36 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 91 | /// Get the IR-specified TLS model for Var. |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 92 | static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { |
| 93 | switch (GV->getThreadLocalMode()) { |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 94 | case GlobalVariable::NotThreadLocal: |
| 95 | llvm_unreachable("getSelectedTLSModel for non-TLS variable"); |
| 96 | break; |
| 97 | case GlobalVariable::GeneralDynamicTLSModel: |
| 98 | return TLSModel::GeneralDynamic; |
| 99 | case GlobalVariable::LocalDynamicTLSModel: |
| 100 | return TLSModel::LocalDynamic; |
| 101 | case GlobalVariable::InitialExecTLSModel: |
| 102 | return TLSModel::InitialExec; |
| 103 | case GlobalVariable::LocalExecTLSModel: |
| 104 | return TLSModel::LocalExec; |
| 105 | } |
| 106 | llvm_unreachable("invalid TLS model"); |
| 107 | } |
| 108 | |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 109 | TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { |
Rafael Espindola | a5bb2f6 | 2014-05-23 19:16:56 +0000 | [diff] [blame] | 110 | bool isLocal = GV->hasLocalLinkage(); |
| 111 | bool isDeclaration = GV->isDeclaration(); |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 112 | bool isPIC = getRelocationModel() == Reloc::PIC_; |
| 113 | bool isPIE = Options.PositionIndependentExecutable; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 114 | // FIXME: what should we do for protected and internal visibility? |
| 115 | // For variables, is internal different from hidden? |
Rafael Espindola | a5bb2f6 | 2014-05-23 19:16:56 +0000 | [diff] [blame] | 116 | bool isHidden = GV->hasHiddenVisibility(); |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 117 | |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 118 | TLSModel::Model Model; |
| 119 | if (isPIC && !isPIE) { |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 120 | if (isLocal || isHidden) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 121 | Model = TLSModel::LocalDynamic; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 122 | else |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 123 | Model = TLSModel::GeneralDynamic; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 124 | } else { |
| 125 | if (!isDeclaration || isHidden) |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 126 | Model = TLSModel::LocalExec; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 127 | else |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 128 | Model = TLSModel::InitialExec; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 129 | } |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 130 | |
Rafael Espindola | 59f7eba | 2014-05-28 18:15:43 +0000 | [diff] [blame] | 131 | // If the user specified a more specific model, use that. |
| 132 | TLSModel::Model SelectedModel = getSelectedTLSModel(GV); |
| 133 | if (SelectedModel > Model) |
| 134 | return SelectedModel; |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 135 | |
| 136 | return Model; |
Chandler Carruth | 16f0ebc | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 139 | /// getOptLevel - Returns the optimization level: None, Less, |
| 140 | /// Default, or Aggressive. |
| 141 | CodeGenOpt::Level TargetMachine::getOptLevel() const { |
| 142 | if (!CodeGenInfo) |
| 143 | return CodeGenOpt::Default; |
| 144 | return CodeGenInfo->getOptLevel(); |
| 145 | } |
| 146 | |
Paul Robinson | d89125a | 2013-11-22 19:11:24 +0000 | [diff] [blame] | 147 | void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { |
| 148 | if (CodeGenInfo) |
| 149 | CodeGenInfo->setOptLevel(Level); |
| 150 | } |
| 151 | |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 152 | TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { |
Eric Christopher | a4e5d3c | 2015-09-16 23:38:13 +0000 | [diff] [blame^] | 153 | return TargetIRAnalysis([this](const Function &F) { |
Mehdi Amini | 5010ebf | 2015-07-09 02:08:42 +0000 | [diff] [blame] | 154 | return TargetTransformInfo(F.getParent()->getDataLayout()); |
| 155 | }); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 158 | static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, |
| 159 | const MCSection &Section) { |
| 160 | if (!AsmInfo.isSectionAtomizableBySymbols(Section)) |
| 161 | return true; |
| 162 | |
| 163 | // If it is not dead stripped, it is safe to use private labels. |
| 164 | const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); |
| 165 | if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) |
| 166 | return true; |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 171 | void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, |
| 172 | const GlobalValue *GV, Mangler &Mang, |
| 173 | bool MayAlwaysUsePrivate) const { |
| 174 | if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { |
| 175 | // Simple case: If GV is not private, it is not important to find out if |
| 176 | // private labels are legal in this case or not. |
| 177 | Mang.getNameWithPrefix(Name, GV, false); |
| 178 | return; |
| 179 | } |
| 180 | SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this); |
Eric Christopher | 36fe028 | 2015-02-03 07:22:52 +0000 | [diff] [blame] | 181 | const TargetLoweringObjectFile *TLOF = getObjFileLowering(); |
| 182 | const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this); |
Lang Hames | 1e923ec | 2015-01-09 18:55:42 +0000 | [diff] [blame] | 183 | bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection); |
David Majnemer | 7db449a | 2015-03-17 23:54:51 +0000 | [diff] [blame] | 184 | TLOF->getNameWithPrefix(Name, GV, CannotUsePrivateLabel, Mang, *this); |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { |
Benjamin Kramer | 1577f1f | 2015-05-23 17:20:53 +0000 | [diff] [blame] | 188 | SmallString<128> NameStr; |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 189 | getNameWithPrefix(NameStr, GV, Mang); |
Eric Christopher | 36fe028 | 2015-02-03 07:22:52 +0000 | [diff] [blame] | 190 | const TargetLoweringObjectFile *TLOF = getObjFileLowering(); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 191 | return TLOF->getContext().getOrCreateSymbol(NameStr); |
Rafael Espindola | a3ad4e6 | 2014-02-19 20:30:41 +0000 | [diff] [blame] | 192 | } |