1 //===-- TargetMachine.cpp - General Target Information ---------------------==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file describes the general parts of a Target machine. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Target/TargetMachine.h" 15 #include "llvm/Analysis/TargetTransformInfo.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/IR/Function.h" 18 #include "llvm/IR/GlobalAlias.h" 19 #include "llvm/IR/GlobalValue.h" 20 #include "llvm/IR/GlobalVariable.h" 21 #include "llvm/IR/Mangler.h" 22 #include "llvm/MC/MCAsmInfo.h" 23 #include "llvm/MC/MCCodeGenInfo.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCSectionMachO.h" 26 #include "llvm/MC/MCTargetOptions.h" 27 #include "llvm/MC/SectionKind.h" 28 #include "llvm/IR/LegacyPassManager.h" 29 #include "llvm/Support/CommandLine.h" 30 #include "llvm/Target/TargetLowering.h" 31 #include "llvm/Target/TargetLoweringObjectFile.h" 32 #include "llvm/Target/TargetSubtargetInfo.h" 33 using namespace llvm; 34 35 //--------------------------------------------------------------------------- 36 // TargetMachine Class 37 // 38 39 TargetMachine::TargetMachine(const Target &T, 40 StringRef TT, StringRef CPU, StringRef FS, 41 const TargetOptions &Options) 42 : TheTarget(T), TargetTriple(TT), TargetCPU(CPU), TargetFS(FS), 43 CodeGenInfo(nullptr), AsmInfo(nullptr), 44 RequireStructuredCFG(false), 45 Options(Options) { 46 } 47 48 TargetMachine::~TargetMachine() { 49 delete CodeGenInfo; 50 delete AsmInfo; 51 } 52 53 /// \brief Reset the target options based on the function's attributes. 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.getFnAttribute(Y).getValueAsString() == "true"); \ 59 } while (0) 60 61 RESET_OPTION(NoFramePointerElim, "no-frame-pointer-elim"); 62 RESET_OPTION(LessPreciseFPMADOption, "less-precise-fpmad"); 63 RESET_OPTION(UnsafeFPMath, "unsafe-fp-math"); 64 RESET_OPTION(NoInfsFPMath, "no-infs-fp-math"); 65 RESET_OPTION(NoNaNsFPMath, "no-nans-fp-math"); 66 RESET_OPTION(UseSoftFloat, "use-soft-float"); 67 RESET_OPTION(DisableTailCalls, "disable-tail-calls"); 68 69 Options.MCOptions.SanitizeAddress = F.hasFnAttribute(Attribute::SanitizeAddress); 70 } 71 72 /// getRelocationModel - Returns the code generation relocation model. The 73 /// choices are static, PIC, and dynamic-no-pic, and target default. 74 Reloc::Model TargetMachine::getRelocationModel() const { 75 if (!CodeGenInfo) 76 return Reloc::Default; 77 return CodeGenInfo->getRelocationModel(); 78 } 79 80 /// getCodeModel - Returns the code model. The choices are small, kernel, 81 /// medium, large, and target default. 82 CodeModel::Model TargetMachine::getCodeModel() const { 83 if (!CodeGenInfo) 84 return CodeModel::Default; 85 return CodeGenInfo->getCodeModel(); 86 } 87 88 /// Get the IR-specified TLS model for Var. 89 static TLSModel::Model getSelectedTLSModel(const GlobalValue *GV) { 90 switch (GV->getThreadLocalMode()) { 91 case GlobalVariable::NotThreadLocal: 92 llvm_unreachable("getSelectedTLSModel for non-TLS variable"); 93 break; 94 case GlobalVariable::GeneralDynamicTLSModel: 95 return TLSModel::GeneralDynamic; 96 case GlobalVariable::LocalDynamicTLSModel: 97 return TLSModel::LocalDynamic; 98 case GlobalVariable::InitialExecTLSModel: 99 return TLSModel::InitialExec; 100 case GlobalVariable::LocalExecTLSModel: 101 return TLSModel::LocalExec; 102 } 103 llvm_unreachable("invalid TLS model"); 104 } 105 106 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const { 107 bool isLocal = GV->hasLocalLinkage(); 108 bool isDeclaration = GV->isDeclaration(); 109 bool isPIC = getRelocationModel() == Reloc::PIC_; 110 bool isPIE = Options.PositionIndependentExecutable; 111 // FIXME: what should we do for protected and internal visibility? 112 // For variables, is internal different from hidden? 113 bool isHidden = GV->hasHiddenVisibility(); 114 115 TLSModel::Model Model; 116 if (isPIC && !isPIE) { 117 if (isLocal || isHidden) 118 Model = TLSModel::LocalDynamic; 119 else 120 Model = TLSModel::GeneralDynamic; 121 } else { 122 if (!isDeclaration || isHidden) 123 Model = TLSModel::LocalExec; 124 else 125 Model = TLSModel::InitialExec; 126 } 127 128 // If the user specified a more specific model, use that. 129 TLSModel::Model SelectedModel = getSelectedTLSModel(GV); 130 if (SelectedModel > Model) 131 return SelectedModel; 132 133 return Model; 134 } 135 136 /// getOptLevel - Returns the optimization level: None, Less, 137 /// Default, or Aggressive. 138 CodeGenOpt::Level TargetMachine::getOptLevel() const { 139 if (!CodeGenInfo) 140 return CodeGenOpt::Default; 141 return CodeGenInfo->getOptLevel(); 142 } 143 144 void TargetMachine::setOptLevel(CodeGenOpt::Level Level) const { 145 if (CodeGenInfo) 146 CodeGenInfo->setOptLevel(Level); 147 } 148 149 TargetIRAnalysis TargetMachine::getTargetIRAnalysis() { 150 return TargetIRAnalysis( 151 [this](Function &) { return TargetTransformInfo(getDataLayout()); }); 152 } 153 154 static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo, 155 const MCSection &Section) { 156 if (!AsmInfo.isSectionAtomizableBySymbols(Section)) 157 return true; 158 159 // If it is not dead stripped, it is safe to use private labels. 160 const MCSectionMachO &SMO = cast<MCSectionMachO>(Section); 161 if (SMO.hasAttribute(MachO::S_ATTR_NO_DEAD_STRIP)) 162 return true; 163 164 return false; 165 } 166 167 void TargetMachine::getNameWithPrefix(SmallVectorImpl<char> &Name, 168 const GlobalValue *GV, Mangler &Mang, 169 bool MayAlwaysUsePrivate) const { 170 if (MayAlwaysUsePrivate || !GV->hasPrivateLinkage()) { 171 // Simple case: If GV is not private, it is not important to find out if 172 // private labels are legal in this case or not. 173 Mang.getNameWithPrefix(Name, GV, false); 174 return; 175 } 176 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, *this); 177 const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 178 const MCSection *TheSection = TLOF->SectionForGlobal(GV, GVKind, Mang, *this); 179 bool CannotUsePrivateLabel = !canUsePrivateLabel(*AsmInfo, *TheSection); 180 Mang.getNameWithPrefix(Name, GV, CannotUsePrivateLabel); 181 } 182 183 MCSymbol *TargetMachine::getSymbol(const GlobalValue *GV, Mangler &Mang) const { 184 SmallString<60> NameStr; 185 getNameWithPrefix(NameStr, GV, Mang); 186 const TargetLoweringObjectFile *TLOF = getObjFileLowering(); 187 return TLOF->getContext().GetOrCreateSymbol(NameStr.str()); 188 } 189