1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- 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 #include "llvm/CodeGen/MachineModuleInfo.h" 10 #include "llvm/ADT/ArrayRef.h" 11 #include "llvm/ADT/DenseMap.h" 12 #include "llvm/ADT/PostOrderIterator.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/ADT/TinyPtrVector.h" 15 #include "llvm/CodeGen/MachineFunction.h" 16 #include "llvm/CodeGen/Passes.h" 17 #include "llvm/IR/BasicBlock.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/Instructions.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/IR/Value.h" 22 #include "llvm/IR/ValueHandle.h" 23 #include "llvm/InitializePasses.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCSymbol.h" 26 #include "llvm/Pass.h" 27 #include "llvm/Support/Casting.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include "llvm/Target/TargetLoweringObjectFile.h" 30 #include "llvm/Target/TargetMachine.h" 31 #include <algorithm> 32 #include <cassert> 33 #include <memory> 34 #include <utility> 35 #include <vector> 36 37 using namespace llvm; 38 using namespace llvm::dwarf; 39 40 // Out of line virtual method. 41 MachineModuleInfoImpl::~MachineModuleInfoImpl() = default; 42 43 namespace llvm { 44 45 class MMIAddrLabelMapCallbackPtr final : CallbackVH { 46 MMIAddrLabelMap *Map = nullptr; 47 48 public: 49 MMIAddrLabelMapCallbackPtr() = default; 50 MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V) {} 51 52 void setPtr(BasicBlock *BB) { 53 ValueHandleBase::operator=(BB); 54 } 55 56 void setMap(MMIAddrLabelMap *map) { Map = map; } 57 58 void deleted() override; 59 void allUsesReplacedWith(Value *V2) override; 60 }; 61 62 class MMIAddrLabelMap { 63 MCContext &Context; 64 struct AddrLabelSymEntry { 65 /// The symbols for the label. 66 TinyPtrVector<MCSymbol *> Symbols; 67 68 Function *Fn; // The containing function of the BasicBlock. 69 unsigned Index; // The index in BBCallbacks for the BasicBlock. 70 }; 71 72 DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols; 73 74 /// Callbacks for the BasicBlock's that we have entries for. We use this so 75 /// we get notified if a block is deleted or RAUWd. 76 std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks; 77 78 /// This is a per-function list of symbols whose corresponding BasicBlock got 79 /// deleted. These symbols need to be emitted at some point in the file, so 80 /// AsmPrinter emits them after the function body. 81 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>> 82 DeletedAddrLabelsNeedingEmission; 83 84 public: 85 MMIAddrLabelMap(MCContext &context) : Context(context) {} 86 87 ~MMIAddrLabelMap() { 88 assert(DeletedAddrLabelsNeedingEmission.empty() && 89 "Some labels for deleted blocks never got emitted"); 90 } 91 92 ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB); 93 94 void takeDeletedSymbolsForFunction(Function *F, 95 std::vector<MCSymbol*> &Result); 96 97 void UpdateForDeletedBlock(BasicBlock *BB); 98 void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New); 99 }; 100 101 } // end namespace llvm 102 103 ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) { 104 assert(BB->hasAddressTaken() && 105 "Shouldn't get label for block without address taken"); 106 AddrLabelSymEntry &Entry = AddrLabelSymbols[BB]; 107 108 // If we already had an entry for this block, just return it. 109 if (!Entry.Symbols.empty()) { 110 assert(BB->getParent() == Entry.Fn && "Parent changed"); 111 return Entry.Symbols; 112 } 113 114 // Otherwise, this is a new entry, create a new symbol for it and add an 115 // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd. 116 BBCallbacks.emplace_back(BB); 117 BBCallbacks.back().setMap(this); 118 Entry.Index = BBCallbacks.size() - 1; 119 Entry.Fn = BB->getParent(); 120 Entry.Symbols.push_back(Context.createTempSymbol(!BB->hasAddressTaken())); 121 return Entry.Symbols; 122 } 123 124 /// If we have any deleted symbols for F, return them. 125 void MMIAddrLabelMap:: 126 takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) { 127 DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>::iterator I = 128 DeletedAddrLabelsNeedingEmission.find(F); 129 130 // If there are no entries for the function, just return. 131 if (I == DeletedAddrLabelsNeedingEmission.end()) return; 132 133 // Otherwise, take the list. 134 std::swap(Result, I->second); 135 DeletedAddrLabelsNeedingEmission.erase(I); 136 } 137 138 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) { 139 // If the block got deleted, there is no need for the symbol. If the symbol 140 // was already emitted, we can just forget about it, otherwise we need to 141 // queue it up for later emission when the function is output. 142 AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]); 143 AddrLabelSymbols.erase(BB); 144 assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?"); 145 BBCallbacks[Entry.Index] = nullptr; // Clear the callback. 146 147 assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) && 148 "Block/parent mismatch"); 149 150 for (MCSymbol *Sym : Entry.Symbols) { 151 if (Sym->isDefined()) 152 return; 153 154 // If the block is not yet defined, we need to emit it at the end of the 155 // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list 156 // for the containing Function. Since the block is being deleted, its 157 // parent may already be removed, we have to get the function from 'Entry'. 158 DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym); 159 } 160 } 161 162 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) { 163 // Get the entry for the RAUW'd block and remove it from our map. 164 AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]); 165 AddrLabelSymbols.erase(Old); 166 assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?"); 167 168 AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New]; 169 170 // If New is not address taken, just move our symbol over to it. 171 if (NewEntry.Symbols.empty()) { 172 BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback. 173 NewEntry = std::move(OldEntry); // Set New's entry. 174 return; 175 } 176 177 BBCallbacks[OldEntry.Index] = nullptr; // Update the callback. 178 179 // Otherwise, we need to add the old symbols to the new block's set. 180 NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(), 181 OldEntry.Symbols.end()); 182 } 183 184 void MMIAddrLabelMapCallbackPtr::deleted() { 185 Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr())); 186 } 187 188 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) { 189 Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2)); 190 } 191 192 void MachineModuleInfo::initialize() { 193 ObjFileMMI = nullptr; 194 CurCallSite = 0; 195 UsesMSVCFloatingPoint = UsesMorestackAddr = false; 196 HasSplitStack = HasNosplitStack = false; 197 AddrLabelSymbols = nullptr; 198 } 199 200 void MachineModuleInfo::finalize() { 201 Personalities.clear(); 202 203 delete AddrLabelSymbols; 204 AddrLabelSymbols = nullptr; 205 206 Context.reset(); 207 208 delete ObjFileMMI; 209 ObjFileMMI = nullptr; 210 } 211 212 MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI) 213 : TM(std::move(MMI.TM)), 214 Context(MMI.TM.getMCAsmInfo(), MMI.TM.getMCRegisterInfo(), 215 MMI.TM.getObjFileLowering(), nullptr, nullptr, false) { 216 ObjFileMMI = MMI.ObjFileMMI; 217 CurCallSite = MMI.CurCallSite; 218 UsesMSVCFloatingPoint = MMI.UsesMSVCFloatingPoint; 219 UsesMorestackAddr = MMI.UsesMorestackAddr; 220 HasSplitStack = MMI.HasSplitStack; 221 HasNosplitStack = MMI.HasNosplitStack; 222 AddrLabelSymbols = MMI.AddrLabelSymbols; 223 TheModule = MMI.TheModule; 224 } 225 226 MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM) 227 : TM(*TM), Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(), 228 TM->getObjFileLowering(), nullptr, nullptr, false) { 229 initialize(); 230 } 231 232 MachineModuleInfo::~MachineModuleInfo() { finalize(); } 233 234 //===- Address of Block Management ----------------------------------------===// 235 236 ArrayRef<MCSymbol *> 237 MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) { 238 // Lazily create AddrLabelSymbols. 239 if (!AddrLabelSymbols) 240 AddrLabelSymbols = new MMIAddrLabelMap(Context); 241 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB)); 242 } 243 244 void MachineModuleInfo:: 245 takeDeletedSymbolsForFunction(const Function *F, 246 std::vector<MCSymbol*> &Result) { 247 // If no blocks have had their addresses taken, we're done. 248 if (!AddrLabelSymbols) return; 249 return AddrLabelSymbols-> 250 takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result); 251 } 252 253 /// \name Exception Handling 254 /// \{ 255 256 void MachineModuleInfo::addPersonality(const Function *Personality) { 257 for (unsigned i = 0; i < Personalities.size(); ++i) 258 if (Personalities[i] == Personality) 259 return; 260 Personalities.push_back(Personality); 261 } 262 263 /// \} 264 265 MachineFunction * 266 MachineModuleInfo::getMachineFunction(const Function &F) const { 267 auto I = MachineFunctions.find(&F); 268 return I != MachineFunctions.end() ? I->second.get() : nullptr; 269 } 270 271 MachineFunction & 272 MachineModuleInfo::getOrCreateMachineFunction(const Function &F) { 273 // Shortcut for the common case where a sequence of MachineFunctionPasses 274 // all query for the same Function. 275 if (LastRequest == &F) 276 return *LastResult; 277 278 auto I = MachineFunctions.insert( 279 std::make_pair(&F, std::unique_ptr<MachineFunction>())); 280 MachineFunction *MF; 281 if (I.second) { 282 // No pre-existing machine function, create a new one. 283 const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F); 284 MF = new MachineFunction(F, TM, STI, NextFnNum++, *this); 285 // Update the set entry. 286 I.first->second.reset(MF); 287 } else { 288 MF = I.first->second.get(); 289 } 290 291 LastRequest = &F; 292 LastResult = MF; 293 return *MF; 294 } 295 296 void MachineModuleInfo::deleteMachineFunctionFor(Function &F) { 297 MachineFunctions.erase(&F); 298 LastRequest = nullptr; 299 LastResult = nullptr; 300 } 301 302 namespace { 303 304 /// This pass frees the MachineFunction object associated with a Function. 305 class FreeMachineFunction : public FunctionPass { 306 public: 307 static char ID; 308 309 FreeMachineFunction() : FunctionPass(ID) {} 310 311 void getAnalysisUsage(AnalysisUsage &AU) const override { 312 AU.addRequired<MachineModuleInfoWrapperPass>(); 313 AU.addPreserved<MachineModuleInfoWrapperPass>(); 314 } 315 316 bool runOnFunction(Function &F) override { 317 MachineModuleInfo &MMI = 318 getAnalysis<MachineModuleInfoWrapperPass>().getMMI(); 319 MMI.deleteMachineFunctionFor(F); 320 return true; 321 } 322 323 StringRef getPassName() const override { 324 return "Free MachineFunction"; 325 } 326 }; 327 328 } // end anonymous namespace 329 330 char FreeMachineFunction::ID; 331 332 FunctionPass *llvm::createFreeMachineFunctionPass() { 333 return new FreeMachineFunction(); 334 } 335 336 MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass( 337 const LLVMTargetMachine *TM) 338 : ImmutablePass(ID), MMI(TM) { 339 initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry()); 340 } 341 342 // Handle the Pass registration stuff necessary to use DataLayout's. 343 INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo", 344 "Machine Module Information", false, false) 345 char MachineModuleInfoWrapperPass::ID = 0; 346 347 bool MachineModuleInfoWrapperPass::doInitialization(Module &M) { 348 MMI.initialize(); 349 MMI.TheModule = &M; 350 MMI.DbgInfoAvailable = !M.debug_compile_units().empty(); 351 return false; 352 } 353 354 bool MachineModuleInfoWrapperPass::doFinalization(Module &M) { 355 MMI.finalize(); 356 return false; 357 } 358 359 AnalysisKey MachineModuleAnalysis::Key; 360 361 MachineModuleInfo MachineModuleAnalysis::run(Module &M, 362 ModuleAnalysisManager &) { 363 MachineModuleInfo MMI(TM); 364 MMI.TheModule = &M; 365 MMI.DbgInfoAvailable = !M.debug_compile_units().empty(); 366 return MMI; 367 } 368