1 //===-- llvm/CodeGen/MachineModuleInfo.h ------------------------*- C++ -*-===// 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 // Collect meta information for a module. This information should be in a 11 // neutral form that can be used by different debugging and exception handling 12 // schemes. 13 // 14 // The organization of information is primarily clustered around the source 15 // compile units. The main exception is source line correspondence where 16 // inlining may interleave code from various compile units. 17 // 18 // The following information can be retrieved from the MachineModuleInfo. 19 // 20 // -- Source directories - Directories are uniqued based on their canonical 21 // string and assigned a sequential numeric ID (base 1.) 22 // -- Source files - Files are also uniqued based on their name and directory 23 // ID. A file ID is sequential number (base 1.) 24 // -- Source line correspondence - A vector of file ID, line#, column# triples. 25 // A DEBUG_LOCATION instruction is generated by the DAG Legalizer 26 // corresponding to each entry in the source line list. This allows a debug 27 // emitter to generate labels referenced by debug information tables. 28 // 29 //===----------------------------------------------------------------------===// 30 31 #ifndef LLVM_CODEGEN_MACHINEMODULEINFO_H 32 #define LLVM_CODEGEN_MACHINEMODULEINFO_H 33 34 #include "llvm/ADT/DenseMap.h" 35 #include "llvm/ADT/PointerIntPair.h" 36 #include "llvm/ADT/SmallPtrSet.h" 37 #include "llvm/ADT/SmallVector.h" 38 #include "llvm/Analysis/EHPersonalities.h" 39 #include "llvm/IR/DebugLoc.h" 40 #include "llvm/IR/Metadata.h" 41 #include "llvm/IR/ValueHandle.h" 42 #include "llvm/MC/MCContext.h" 43 #include "llvm/MC/MCSymbol.h" 44 #include "llvm/MC/MachineLocation.h" 45 #include "llvm/Pass.h" 46 #include "llvm/Support/DataTypes.h" 47 #include "llvm/Support/Dwarf.h" 48 49 namespace llvm { 50 51 //===----------------------------------------------------------------------===// 52 // Forward declarations. 53 class Constant; 54 class GlobalVariable; 55 class BlockAddress; 56 class MDNode; 57 class MMIAddrLabelMap; 58 class MachineBasicBlock; 59 class MachineFunction; 60 class Module; 61 class PointerType; 62 class StructType; 63 64 struct SEHHandler { 65 // Filter or finally function. Null indicates a catch-all. 66 const Function *FilterOrFinally; 67 68 // Address of block to recover at. Null for a finally handler. 69 const BlockAddress *RecoverBA; 70 }; 71 72 //===----------------------------------------------------------------------===// 73 /// LandingPadInfo - This structure is used to retain landing pad info for 74 /// the current function. 75 /// 76 struct LandingPadInfo { 77 MachineBasicBlock *LandingPadBlock; // Landing pad block. 78 SmallVector<MCSymbol *, 1> BeginLabels; // Labels prior to invoke. 79 SmallVector<MCSymbol *, 1> EndLabels; // Labels after invoke. 80 SmallVector<SEHHandler, 1> SEHHandlers; // SEH handlers active at this lpad. 81 MCSymbol *LandingPadLabel; // Label at beginning of landing pad. 82 std::vector<int> TypeIds; // List of type ids (filters negative). 83 LandingPadInfoLandingPadInfo84 explicit LandingPadInfo(MachineBasicBlock *MBB) 85 : LandingPadBlock(MBB), LandingPadLabel(nullptr) {} 86 }; 87 88 //===----------------------------------------------------------------------===// 89 /// MachineModuleInfoImpl - This class can be derived from and used by targets 90 /// to hold private target-specific information for each Module. Objects of 91 /// type are accessed/created with MMI::getInfo and destroyed when the 92 /// MachineModuleInfo is destroyed. 93 /// 94 class MachineModuleInfoImpl { 95 public: 96 typedef PointerIntPair<MCSymbol*, 1, bool> StubValueTy; 97 virtual ~MachineModuleInfoImpl(); 98 typedef std::vector<std::pair<MCSymbol*, StubValueTy> > SymbolListTy; 99 protected: 100 101 /// Return the entries from a DenseMap in a deterministic sorted orer. 102 /// Clears the map. 103 static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&); 104 }; 105 106 //===----------------------------------------------------------------------===// 107 /// MachineModuleInfo - This class contains meta information specific to a 108 /// module. Queries can be made by different debugging and exception handling 109 /// schemes and reformated for specific use. 110 /// 111 class MachineModuleInfo : public ImmutablePass { 112 /// Context - This is the MCContext used for the entire code generator. 113 MCContext Context; 114 115 /// TheModule - This is the LLVM Module being worked on. 116 const Module *TheModule; 117 118 /// ObjFileMMI - This is the object-file-format-specific implementation of 119 /// MachineModuleInfoImpl, which lets targets accumulate whatever info they 120 /// want. 121 MachineModuleInfoImpl *ObjFileMMI; 122 123 /// List of moves done by a function's prolog. Used to construct frame maps 124 /// by debug and exception handling consumers. 125 std::vector<MCCFIInstruction> FrameInstructions; 126 127 /// LandingPads - List of LandingPadInfo describing the landing pad 128 /// information in the current function. 129 std::vector<LandingPadInfo> LandingPads; 130 131 /// LPadToCallSiteMap - Map a landing pad's EH symbol to the call site 132 /// indexes. 133 DenseMap<MCSymbol*, SmallVector<unsigned, 4> > LPadToCallSiteMap; 134 135 /// CallSiteMap - Map of invoke call site index values to associated begin 136 /// EH_LABEL for the current function. 137 DenseMap<MCSymbol*, unsigned> CallSiteMap; 138 139 /// CurCallSite - The current call site index being processed, if any. 0 if 140 /// none. 141 unsigned CurCallSite; 142 143 /// TypeInfos - List of C++ TypeInfo used in the current function. 144 std::vector<const GlobalValue *> TypeInfos; 145 146 /// FilterIds - List of typeids encoding filters used in the current function. 147 std::vector<unsigned> FilterIds; 148 149 /// FilterEnds - List of the indices in FilterIds corresponding to filter 150 /// terminators. 151 std::vector<unsigned> FilterEnds; 152 153 /// Personalities - Vector of all personality functions ever seen. Used to 154 /// emit common EH frames. 155 std::vector<const Function *> Personalities; 156 157 /// AddrLabelSymbols - This map keeps track of which symbol is being used for 158 /// the specified basic block's address of label. 159 MMIAddrLabelMap *AddrLabelSymbols; 160 161 bool CallsEHReturn; 162 bool CallsUnwindInit; 163 bool HasEHFunclets; 164 165 // TODO: Ideally, what we'd like is to have a switch that allows emitting 166 // synchronous (precise at call-sites only) CFA into .eh_frame. However, 167 // even under this switch, we'd like .debug_frame to be precise when using. 168 // -g. At this moment, there's no way to specify that some CFI directives 169 // go into .eh_frame only, while others go into .debug_frame only. 170 171 /// DbgInfoAvailable - True if debugging information is available 172 /// in this module. 173 bool DbgInfoAvailable; 174 175 /// UsesVAFloatArgument - True if this module calls VarArg function with 176 /// floating-point arguments. This is used to emit an undefined reference 177 /// to _fltused on Windows targets. 178 bool UsesVAFloatArgument; 179 180 /// UsesMorestackAddr - True if the module calls the __morestack function 181 /// indirectly, as is required under the large code model on x86. This is used 182 /// to emit a definition of a symbol, __morestack_addr, containing the 183 /// address. See comments in lib/Target/X86/X86FrameLowering.cpp for more 184 /// details. 185 bool UsesMorestackAddr; 186 187 EHPersonality PersonalityTypeCache; 188 189 public: 190 static char ID; // Pass identification, replacement for typeid 191 192 struct VariableDbgInfo { 193 const DILocalVariable *Var; 194 const DIExpression *Expr; 195 unsigned Slot; 196 const DILocation *Loc; 197 VariableDbgInfoVariableDbgInfo198 VariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr, 199 unsigned Slot, const DILocation *Loc) 200 : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {} 201 }; 202 typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy; 203 VariableDbgInfoMapTy VariableDbgInfos; 204 205 MachineModuleInfo(); // DUMMY CONSTRUCTOR, DO NOT CALL. 206 // Real constructor. 207 MachineModuleInfo(const MCAsmInfo &MAI, const MCRegisterInfo &MRI, 208 const MCObjectFileInfo *MOFI); 209 ~MachineModuleInfo() override; 210 211 // Initialization and Finalization 212 bool doInitialization(Module &) override; 213 bool doFinalization(Module &) override; 214 215 /// EndFunction - Discard function meta information. 216 /// 217 void EndFunction(); 218 getContext()219 const MCContext &getContext() const { return Context; } getContext()220 MCContext &getContext() { return Context; } 221 setModule(const Module * M)222 void setModule(const Module *M) { TheModule = M; } getModule()223 const Module *getModule() const { return TheModule; } 224 225 /// getInfo - Keep track of various per-function pieces of information for 226 /// backends that would like to do so. 227 /// 228 template<typename Ty> getObjFileInfo()229 Ty &getObjFileInfo() { 230 if (ObjFileMMI == nullptr) 231 ObjFileMMI = new Ty(*this); 232 return *static_cast<Ty*>(ObjFileMMI); 233 } 234 235 template<typename Ty> getObjFileInfo()236 const Ty &getObjFileInfo() const { 237 return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>(); 238 } 239 240 /// hasDebugInfo - Returns true if valid debug info is present. 241 /// hasDebugInfo()242 bool hasDebugInfo() const { return DbgInfoAvailable; } setDebugInfoAvailability(bool avail)243 void setDebugInfoAvailability(bool avail) { DbgInfoAvailable = avail; } 244 callsEHReturn()245 bool callsEHReturn() const { return CallsEHReturn; } setCallsEHReturn(bool b)246 void setCallsEHReturn(bool b) { CallsEHReturn = b; } 247 callsUnwindInit()248 bool callsUnwindInit() const { return CallsUnwindInit; } setCallsUnwindInit(bool b)249 void setCallsUnwindInit(bool b) { CallsUnwindInit = b; } 250 hasEHFunclets()251 bool hasEHFunclets() const { return HasEHFunclets; } setHasEHFunclets(bool V)252 void setHasEHFunclets(bool V) { HasEHFunclets = V; } 253 usesVAFloatArgument()254 bool usesVAFloatArgument() const { 255 return UsesVAFloatArgument; 256 } 257 setUsesVAFloatArgument(bool b)258 void setUsesVAFloatArgument(bool b) { 259 UsesVAFloatArgument = b; 260 } 261 usesMorestackAddr()262 bool usesMorestackAddr() const { 263 return UsesMorestackAddr; 264 } 265 setUsesMorestackAddr(bool b)266 void setUsesMorestackAddr(bool b) { 267 UsesMorestackAddr = b; 268 } 269 270 /// \brief Returns a reference to a list of cfi instructions in the current 271 /// function's prologue. Used to construct frame maps for debug and exception 272 /// handling comsumers. getFrameInstructions()273 const std::vector<MCCFIInstruction> &getFrameInstructions() const { 274 return FrameInstructions; 275 } 276 277 unsigned LLVM_ATTRIBUTE_UNUSED_RESULT addFrameInst(const MCCFIInstruction & Inst)278 addFrameInst(const MCCFIInstruction &Inst) { 279 FrameInstructions.push_back(Inst); 280 return FrameInstructions.size() - 1; 281 } 282 283 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic 284 /// block when its address is taken. This cannot be its normal LBB label 285 /// because the block may be accessed outside its containing function. getAddrLabelSymbol(const BasicBlock * BB)286 MCSymbol *getAddrLabelSymbol(const BasicBlock *BB) { 287 return getAddrLabelSymbolToEmit(BB).front(); 288 } 289 290 /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified 291 /// basic block when its address is taken. If other blocks were RAUW'd to 292 /// this one, we may have to emit them as well, return the whole set. 293 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB); 294 295 /// takeDeletedSymbolsForFunction - If the specified function has had any 296 /// references to address-taken blocks generated, but the block got deleted, 297 /// return the symbol now so we can emit it. This prevents emitting a 298 /// reference to a symbol that has no definition. 299 void takeDeletedSymbolsForFunction(const Function *F, 300 std::vector<MCSymbol*> &Result); 301 302 303 //===- EH ---------------------------------------------------------------===// 304 305 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 306 /// specified MachineBasicBlock. 307 LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad); 308 309 /// addInvoke - Provide the begin and end labels of an invoke style call and 310 /// associate it with a try landing pad block. 311 void addInvoke(MachineBasicBlock *LandingPad, 312 MCSymbol *BeginLabel, MCSymbol *EndLabel); 313 314 /// addLandingPad - Add a new panding pad. Returns the label ID for the 315 /// landing pad entry. 316 MCSymbol *addLandingPad(MachineBasicBlock *LandingPad); 317 318 /// addPersonality - Provide the personality function for the exception 319 /// information. 320 void addPersonality(const Function *Personality); 321 322 /// getPersonalities - Return array of personality functions ever seen. getPersonalities()323 const std::vector<const Function *>& getPersonalities() const { 324 return Personalities; 325 } 326 327 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 328 /// 329 void addCatchTypeInfo(MachineBasicBlock *LandingPad, 330 ArrayRef<const GlobalValue *> TyInfo); 331 332 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 333 /// 334 void addFilterTypeInfo(MachineBasicBlock *LandingPad, 335 ArrayRef<const GlobalValue *> TyInfo); 336 337 /// addCleanup - Add a cleanup action for a landing pad. 338 /// 339 void addCleanup(MachineBasicBlock *LandingPad); 340 341 void addSEHCatchHandler(MachineBasicBlock *LandingPad, const Function *Filter, 342 const BlockAddress *RecoverLabel); 343 344 void addSEHCleanupHandler(MachineBasicBlock *LandingPad, 345 const Function *Cleanup); 346 347 /// getTypeIDFor - Return the type id for the specified typeinfo. This is 348 /// function wide. 349 unsigned getTypeIDFor(const GlobalValue *TI); 350 351 /// getFilterIDFor - Return the id of the filter encoded by TyIds. This is 352 /// function wide. 353 int getFilterIDFor(std::vector<unsigned> &TyIds); 354 355 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing 356 /// pads. 357 void TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap = nullptr); 358 359 /// getLandingPads - Return a reference to the landing pad info for the 360 /// current function. getLandingPads()361 const std::vector<LandingPadInfo> &getLandingPads() const { 362 return LandingPads; 363 } 364 365 /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call 366 /// site indexes. 367 void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites); 368 369 /// getCallSiteLandingPad - Get the call site indexes for a landing pad EH 370 /// symbol. getCallSiteLandingPad(MCSymbol * Sym)371 SmallVectorImpl<unsigned> &getCallSiteLandingPad(MCSymbol *Sym) { 372 assert(hasCallSiteLandingPad(Sym) && 373 "missing call site number for landing pad!"); 374 return LPadToCallSiteMap[Sym]; 375 } 376 377 /// hasCallSiteLandingPad - Return true if the landing pad Eh symbol has an 378 /// associated call site. hasCallSiteLandingPad(MCSymbol * Sym)379 bool hasCallSiteLandingPad(MCSymbol *Sym) { 380 return !LPadToCallSiteMap[Sym].empty(); 381 } 382 383 /// setCallSiteBeginLabel - Map the begin label for a call site. setCallSiteBeginLabel(MCSymbol * BeginLabel,unsigned Site)384 void setCallSiteBeginLabel(MCSymbol *BeginLabel, unsigned Site) { 385 CallSiteMap[BeginLabel] = Site; 386 } 387 388 /// getCallSiteBeginLabel - Get the call site number for a begin label. getCallSiteBeginLabel(MCSymbol * BeginLabel)389 unsigned getCallSiteBeginLabel(MCSymbol *BeginLabel) { 390 assert(hasCallSiteBeginLabel(BeginLabel) && 391 "Missing call site number for EH_LABEL!"); 392 return CallSiteMap[BeginLabel]; 393 } 394 395 /// hasCallSiteBeginLabel - Return true if the begin label has a call site 396 /// number associated with it. hasCallSiteBeginLabel(MCSymbol * BeginLabel)397 bool hasCallSiteBeginLabel(MCSymbol *BeginLabel) { 398 return CallSiteMap[BeginLabel] != 0; 399 } 400 401 /// setCurrentCallSite - Set the call site currently being processed. setCurrentCallSite(unsigned Site)402 void setCurrentCallSite(unsigned Site) { CurCallSite = Site; } 403 404 /// getCurrentCallSite - Get the call site currently being processed, if any. 405 /// return zero if none. getCurrentCallSite()406 unsigned getCurrentCallSite() { return CurCallSite; } 407 408 /// getTypeInfos - Return a reference to the C++ typeinfo for the current 409 /// function. getTypeInfos()410 const std::vector<const GlobalValue *> &getTypeInfos() const { 411 return TypeInfos; 412 } 413 414 /// getFilterIds - Return a reference to the typeids encoding filters used in 415 /// the current function. getFilterIds()416 const std::vector<unsigned> &getFilterIds() const { 417 return FilterIds; 418 } 419 420 /// setVariableDbgInfo - Collect information used to emit debugging 421 /// information of a variable. setVariableDbgInfo(const DILocalVariable * Var,const DIExpression * Expr,unsigned Slot,const DILocation * Loc)422 void setVariableDbgInfo(const DILocalVariable *Var, const DIExpression *Expr, 423 unsigned Slot, const DILocation *Loc) { 424 VariableDbgInfos.emplace_back(Var, Expr, Slot, Loc); 425 } 426 getVariableDbgInfo()427 VariableDbgInfoMapTy &getVariableDbgInfo() { return VariableDbgInfos; } 428 429 }; // End class MachineModuleInfo 430 431 } // End llvm namespace 432 433 #endif 434