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