1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Collect meta information for a module. This information should be in a 10 // neutral form that can be used by different debugging and exception handling 11 // schemes. 12 // 13 // The organization of information is primarily clustered around the source 14 // compile units. The main exception is source line correspondence where 15 // inlining may interleave code from various compile units. 16 // 17 // The following information can be retrieved from the MachineModuleInfo. 18 // 19 // -- Source directories - Directories are uniqued based on their canonical 20 // string and assigned a sequential numeric ID (base 1.) 21 // -- Source files - Files are also uniqued based on their name and directory 22 // ID. A file ID is sequential number (base 1.) 23 // -- Source line correspondence - A vector of file ID, line#, column# triples. 24 // A DEBUG_LOCATION instruction is generated by the DAG Legalizer 25 // corresponding to each entry in the source line list. This allows a debug 26 // emitter to generate labels referenced by debug information tables. 27 // 28 //===----------------------------------------------------------------------===// 29 30 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H 31 #define LLVM_CODEGEN_MACHINEMODULEINFO_H 32 33 #include "llvm/ADT/ArrayRef.h" 34 #include "llvm/ADT/DenseMap.h" 35 #include "llvm/ADT/PointerIntPair.h" 36 #include "llvm/IR/PassManager.h" 37 #include "llvm/MC/MCContext.h" 38 #include "llvm/MC/MCSymbol.h" 39 #include "llvm/Pass.h" 40 #include <memory> 41 #include <utility> 42 #include <vector> 43 44 namespace llvm { 45 46 class BasicBlock; 47 class Function; 48 class LLVMTargetMachine; 49 class MMIAddrLabelMap; 50 class MachineFunction; 51 class Module; 52 53 //===----------------------------------------------------------------------===// 54 /// This class can be derived from and used by targets to hold private 55 /// target-specific information for each Module. Objects of type are 56 /// accessed/created with MachineModuleInfo::getObjFileInfo and destroyed when 57 /// the MachineModuleInfo is destroyed. 58 /// 59 class MachineModuleInfoImpl { 60 public: 61 using StubValueTy = PointerIntPair<MCSymbol *, 1, bool>; 62 using SymbolListTy = std::vector<std::pair<MCSymbol *, StubValueTy>>; 63 64 virtual ~MachineModuleInfoImpl(); 65 66 protected: 67 /// Return the entries from a DenseMap in a deterministic sorted orer. 68 /// Clears the map. 69 static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&); 70 }; 71 72 //===----------------------------------------------------------------------===// 73 /// This class contains meta information specific to a module. Queries can be 74 /// made by different debugging and exception handling schemes and reformated 75 /// for specific use. 76 /// 77 class MachineModuleInfo { 78 friend class MachineModuleInfoWrapperPass; 79 friend class MachineModuleAnalysis; 80 81 const LLVMTargetMachine &TM; 82 83 /// This is the MCContext used for the entire code generator. 84 MCContext Context; 85 // This is an external context, that if assigned, will be used instead of the 86 // internal context. 87 MCContext *ExternalContext = nullptr; 88 89 /// This is the LLVM Module being worked on. 90 const Module *TheModule; 91 92 /// This is the object-file-format-specific implementation of 93 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they 94 /// want. 95 MachineModuleInfoImpl *ObjFileMMI; 96 97 /// \name Exception Handling 98 /// \{ 99 100 /// Vector of all personality functions ever seen. Used to emit common EH 101 /// frames. 102 std::vector<const Function *> Personalities; 103 104 /// The current call site index being processed, if any. 0 if none. 105 unsigned CurCallSite; 106 107 /// \} 108 109 /// This map keeps track of which symbol is being used for the specified 110 /// basic block's address of label. 111 MMIAddrLabelMap *AddrLabelSymbols; 112 113 // TODO: Ideally, what we'd like is to have a switch that allows emitting 114 // synchronous (precise at call-sites only) CFA into .eh_frame. However, 115 // even under this switch, we'd like .debug_frame to be precise when using 116 // -g. At this moment, there's no way to specify that some CFI directives 117 // go into .eh_frame only, while others go into .debug_frame only. 118 119 /// True if debugging information is available in this module. 120 bool DbgInfoAvailable; 121 122 /// True if this module is being built for windows/msvc, and uses floating 123 /// point. This is used to emit an undefined reference to _fltused. 124 bool UsesMSVCFloatingPoint; 125 126 /// True if the module calls the __morestack function indirectly, as is 127 /// required under the large code model on x86. This is used to emit 128 /// a definition of a symbol, __morestack_addr, containing the address. See 129 /// comments in lib/Target/X86/X86FrameLowering.cpp for more details. 130 bool UsesMorestackAddr; 131 132 /// True if the module contains split-stack functions. This is used to 133 /// emit .note.GNU-split-stack section as required by the linker for 134 /// special handling split-stack function calling no-split-stack function. 135 bool HasSplitStack; 136 137 /// True if the module contains no-split-stack functions. This is used to 138 /// emit .note.GNU-no-split-stack section when it also contains split-stack 139 /// functions. 140 bool HasNosplitStack; 141 142 /// Maps IR Functions to their corresponding MachineFunctions. 143 DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions; 144 /// Next unique number available for a MachineFunction. 145 unsigned NextFnNum = 0; 146 const Function *LastRequest = nullptr; ///< Used for shortcut/cache. 147 MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache. 148 149 MachineModuleInfo &operator=(MachineModuleInfo &&MMII) = delete; 150 151 public: 152 explicit MachineModuleInfo(const LLVMTargetMachine *TM = nullptr); 153 154 explicit MachineModuleInfo(const LLVMTargetMachine *TM, 155 MCContext *ExtContext); 156 157 MachineModuleInfo(MachineModuleInfo &&MMII); 158 159 ~MachineModuleInfo(); 160 161 void initialize(); 162 void finalize(); 163 164 const LLVMTargetMachine &getTarget() const { return TM; } 165 166 const MCContext &getContext() const { 167 return ExternalContext ? *ExternalContext : Context; 168 } 169 MCContext &getContext() { 170 return ExternalContext ? *ExternalContext : Context; 171 } 172 173 const Module *getModule() const { return TheModule; } 174 175 /// Returns the MachineFunction constructed for the IR function \p F. 176 /// Creates a new MachineFunction if none exists yet. 177 MachineFunction &getOrCreateMachineFunction(Function &F); 178 179 /// \brief Returns the MachineFunction associated to IR function \p F if there 180 /// is one, otherwise nullptr. 181 MachineFunction *getMachineFunction(const Function &F) const; 182 183 /// Delete the MachineFunction \p MF and reset the link in the IR Function to 184 /// Machine Function map. 185 void deleteMachineFunctionFor(Function &F); 186 187 /// Keep track of various per-module pieces of information for backends 188 /// that would like to do so. 189 template<typename Ty> 190 Ty &getObjFileInfo() { 191 if (ObjFileMMI == nullptr) 192 ObjFileMMI = new Ty(*this); 193 return *static_cast<Ty*>(ObjFileMMI); 194 } 195 196 template<typename Ty> 197 const Ty &getObjFileInfo() const { 198 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>(); 199 } 200 201 /// Returns true if valid debug info is present. 202 bool hasDebugInfo() const { return DbgInfoAvailable; } 203 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; } 204 205 bool usesMSVCFloatingPoint() const { return UsesMSVCFloatingPoint; } 206 207 void setUsesMSVCFloatingPoint(bool b) { UsesMSVCFloatingPoint = b; } 208 209 bool usesMorestackAddr() const { 210 return UsesMorestackAddr; 211 } 212 213 void setUsesMorestackAddr(bool b) { 214 UsesMorestackAddr = b; 215 } 216 217 bool hasSplitStack() const { 218 return HasSplitStack; 219 } 220 221 void setHasSplitStack(bool b) { 222 HasSplitStack = b; 223 } 224 225 bool hasNosplitStack() const { 226 return HasNosplitStack; 227 } 228 229 void setHasNosplitStack(bool b) { 230 HasNosplitStack = b; 231 } 232 233 /// Return the symbol to be used for the specified basic block when its 234 /// address is taken. This cannot be its normal LBB label because the block 235 /// may be accessed outside its containing function. 236 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) { 237 return getAddrLabelSymbolToEmit(BB).front(); 238 } 239 240 /// Return the symbol to be used for the specified basic block when its 241 /// address is taken. If other blocks were RAUW'd to this one, we may have 242 /// to emit them as well, return the whole set. 243 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB); 244 245 /// If the specified function has had any references to address-taken blocks 246 /// generated, but the block got deleted, return the symbol now so we can 247 /// emit it. This prevents emitting a reference to a symbol that has no 248 /// definition. 249 void takeDeletedSymbolsForFunction(const Function *F, 250 std::vector<MCSymbol*> &Result); 251 252 /// \name Exception Handling 253 /// \{ 254 255 /// Set the call site currently being processed. 256 void setCurrentCallSite(unsigned Site) { CurCallSite = Site; } 257 258 /// Get the call site currently being processed, if any. return zero if 259 /// none. 260 unsigned getCurrentCallSite() { return CurCallSite; } 261 262 /// Provide the personality function for the exception information. 263 void addPersonality(const Function *Personality); 264 265 /// Return array of personality functions ever seen. 266 const std::vector<const Function *>& getPersonalities() const { 267 return Personalities; 268 } 269 /// \} 270 271 // MMI owes MCContext. It should never be invalidated. 272 bool invalidate(Module &, const PreservedAnalyses &, 273 ModuleAnalysisManager::Invalidator &) { 274 return false; 275 } 276 }; // End class MachineModuleInfo 277 278 class MachineModuleInfoWrapperPass : public ImmutablePass { 279 MachineModuleInfo MMI; 280 281 public: 282 static char ID; // Pass identification, replacement for typeid 283 explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM = nullptr); 284 285 explicit MachineModuleInfoWrapperPass(const LLVMTargetMachine *TM, 286 MCContext *ExtContext); 287 288 // Initialization and Finalization 289 bool doInitialization(Module &) override; 290 bool doFinalization(Module &) override; 291 292 MachineModuleInfo &getMMI() { return MMI; } 293 const MachineModuleInfo &getMMI() const { return MMI; } 294 }; 295 296 /// An analysis that produces \c MachineInfo for a module. 297 class MachineModuleAnalysis : public AnalysisInfoMixin<MachineModuleAnalysis> { 298 friend AnalysisInfoMixin<MachineModuleAnalysis>; 299 static AnalysisKey Key; 300 301 const LLVMTargetMachine *TM; 302 303 public: 304 /// Provide the result type for this analysis pass. 305 using Result = MachineModuleInfo; 306 307 MachineModuleAnalysis(const LLVMTargetMachine *TM) : TM(TM) {} 308 309 /// Run the analysis pass and produce machine module information. 310 MachineModuleInfo run(Module &M, ModuleAnalysisManager &); 311 }; 312 313 } // end namespace llvm 314 315 #endif // LLVM_CODEGEN_MACHINEMODULEINFO_H 316