LLVM  mainline
MachineModuleInfo.cpp
Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/CodeGen/MachineModuleInfo.h"
00011 #include "llvm/ADT/PointerUnion.h"
00012 #include "llvm/Analysis/LibCallSemantics.h"
00013 #include "llvm/Analysis/ValueTracking.h"
00014 #include "llvm/CodeGen/MachineFunction.h"
00015 #include "llvm/CodeGen/MachineFunctionPass.h"
00016 #include "llvm/CodeGen/Passes.h"
00017 #include "llvm/CodeGen/WinEHFuncInfo.h"
00018 #include "llvm/IR/Constants.h"
00019 #include "llvm/IR/DerivedTypes.h"
00020 #include "llvm/IR/GlobalVariable.h"
00021 #include "llvm/IR/Module.h"
00022 #include "llvm/MC/MCObjectFileInfo.h"
00023 #include "llvm/MC/MCSymbol.h"
00024 #include "llvm/Support/Dwarf.h"
00025 #include "llvm/Support/ErrorHandling.h"
00026 using namespace llvm;
00027 using namespace llvm::dwarf;
00028 
00029 // Handle the Pass registration stuff necessary to use DataLayout's.
00030 INITIALIZE_PASS(MachineModuleInfo, "machinemoduleinfo",
00031                 "Machine Module Information", false, false)
00032 char MachineModuleInfo::ID = 0;
00033 
00034 // Out of line virtual method.
00035 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
00036 
00037 namespace llvm {
00038 class MMIAddrLabelMapCallbackPtr : CallbackVH {
00039   MMIAddrLabelMap *Map;
00040 public:
00041   MMIAddrLabelMapCallbackPtr() : Map(nullptr) {}
00042   MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(nullptr) {}
00043 
00044   void setPtr(BasicBlock *BB) {
00045     ValueHandleBase::operator=(BB);
00046   }
00047 
00048   void setMap(MMIAddrLabelMap *map) { Map = map; }
00049 
00050   void deleted() override;
00051   void allUsesReplacedWith(Value *V2) override;
00052 };
00053 
00054 class MMIAddrLabelMap {
00055   MCContext &Context;
00056   struct AddrLabelSymEntry {
00057     /// Symbols - The symbols for the label.  This is a pointer union that is
00058     /// either one symbol (the common case) or a list of symbols.
00059     PointerUnion<MCSymbol *, std::vector<MCSymbol*>*> Symbols;
00060 
00061     Function *Fn;   // The containing function of the BasicBlock.
00062     unsigned Index; // The index in BBCallbacks for the BasicBlock.
00063   };
00064 
00065   DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
00066 
00067   /// BBCallbacks - Callbacks for the BasicBlock's that we have entries for.  We
00068   /// use this so we get notified if a block is deleted or RAUWd.
00069   std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
00070 
00071   /// DeletedAddrLabelsNeedingEmission - This is a per-function list of symbols
00072   /// whose corresponding BasicBlock got deleted.  These symbols need to be
00073   /// emitted at some point in the file, so AsmPrinter emits them after the
00074   /// function body.
00075   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
00076     DeletedAddrLabelsNeedingEmission;
00077 public:
00078 
00079   MMIAddrLabelMap(MCContext &context) : Context(context) {}
00080   ~MMIAddrLabelMap() {
00081     assert(DeletedAddrLabelsNeedingEmission.empty() &&
00082            "Some labels for deleted blocks never got emitted");
00083 
00084     // Deallocate any of the 'list of symbols' case.
00085     for (DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry>::iterator
00086          I = AddrLabelSymbols.begin(), E = AddrLabelSymbols.end(); I != E; ++I)
00087       if (I->second.Symbols.is<std::vector<MCSymbol*>*>())
00088         delete I->second.Symbols.get<std::vector<MCSymbol*>*>();
00089   }
00090 
00091   MCSymbol *getAddrLabelSymbol(BasicBlock *BB);
00092   std::vector<MCSymbol*> getAddrLabelSymbolToEmit(BasicBlock *BB);
00093 
00094   void takeDeletedSymbolsForFunction(Function *F,
00095                                      std::vector<MCSymbol*> &Result);
00096 
00097   void UpdateForDeletedBlock(BasicBlock *BB);
00098   void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
00099 };
00100 }
00101 
00102 MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
00103   assert(BB->hasAddressTaken() &&
00104          "Shouldn't get label for block without address taken");
00105   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
00106 
00107   // If we already had an entry for this block, just return it.
00108   if (!Entry.Symbols.isNull()) {
00109     assert(BB->getParent() == Entry.Fn && "Parent changed");
00110     if (Entry.Symbols.is<MCSymbol*>())
00111       return Entry.Symbols.get<MCSymbol*>();
00112     return (*Entry.Symbols.get<std::vector<MCSymbol*>*>())[0];
00113   }
00114 
00115   // Otherwise, this is a new entry, create a new symbol for it and add an
00116   // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
00117   BBCallbacks.push_back(BB);
00118   BBCallbacks.back().setMap(this);
00119   Entry.Index = BBCallbacks.size()-1;
00120   Entry.Fn = BB->getParent();
00121   MCSymbol *Result = Context.createTempSymbol();
00122   Entry.Symbols = Result;
00123   return Result;
00124 }
00125 
00126 std::vector<MCSymbol*>
00127 MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
00128   assert(BB->hasAddressTaken() &&
00129          "Shouldn't get label for block without address taken");
00130   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
00131 
00132   std::vector<MCSymbol*> Result;
00133 
00134   // If we already had an entry for this block, just return it.
00135   if (Entry.Symbols.isNull())
00136     Result.push_back(getAddrLabelSymbol(BB));
00137   else if (MCSymbol *Sym = Entry.Symbols.dyn_cast<MCSymbol*>())
00138     Result.push_back(Sym);
00139   else
00140     Result = *Entry.Symbols.get<std::vector<MCSymbol*>*>();
00141   return Result;
00142 }
00143 
00144 
00145 /// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
00146 /// them.
00147 void MMIAddrLabelMap::
00148 takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
00149   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
00150     DeletedAddrLabelsNeedingEmission.find(F);
00151 
00152   // If there are no entries for the function, just return.
00153   if (I == DeletedAddrLabelsNeedingEmission.end()) return;
00154 
00155   // Otherwise, take the list.
00156   std::swap(Result, I->second);
00157   DeletedAddrLabelsNeedingEmission.erase(I);
00158 }
00159 
00160 
00161 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
00162   // If the block got deleted, there is no need for the symbol.  If the symbol
00163   // was already emitted, we can just forget about it, otherwise we need to
00164   // queue it up for later emission when the function is output.
00165   AddrLabelSymEntry Entry = AddrLabelSymbols[BB];
00166   AddrLabelSymbols.erase(BB);
00167   assert(!Entry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
00168   BBCallbacks[Entry.Index] = nullptr;  // Clear the callback.
00169 
00170   assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
00171          "Block/parent mismatch");
00172 
00173   // Handle both the single and the multiple symbols cases.
00174   if (MCSymbol *Sym = Entry.Symbols.dyn_cast<MCSymbol*>()) {
00175     if (Sym->isDefined())
00176       return;
00177 
00178     // If the block is not yet defined, we need to emit it at the end of the
00179     // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
00180     // for the containing Function.  Since the block is being deleted, its
00181     // parent may already be removed, we have to get the function from 'Entry'.
00182     DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
00183   } else {
00184     std::vector<MCSymbol*> *Syms = Entry.Symbols.get<std::vector<MCSymbol*>*>();
00185 
00186     for (unsigned i = 0, e = Syms->size(); i != e; ++i) {
00187       MCSymbol *Sym = (*Syms)[i];
00188       if (Sym->isDefined()) continue;  // Ignore already emitted labels.
00189 
00190       // If the block is not yet defined, we need to emit it at the end of the
00191       // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
00192       // for the containing Function.  Since the block is being deleted, its
00193       // parent may already be removed, we have to get the function from
00194       // 'Entry'.
00195       DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
00196     }
00197 
00198     // The entry is deleted, free the memory associated with the symbol list.
00199     delete Syms;
00200   }
00201 }
00202 
00203 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
00204   // Get the entry for the RAUW'd block and remove it from our map.
00205   AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old];
00206   AddrLabelSymbols.erase(Old);
00207   assert(!OldEntry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
00208 
00209   AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
00210 
00211   // If New is not address taken, just move our symbol over to it.
00212   if (NewEntry.Symbols.isNull()) {
00213     BBCallbacks[OldEntry.Index].setPtr(New);    // Update the callback.
00214     NewEntry = OldEntry;     // Set New's entry.
00215     return;
00216   }
00217 
00218   BBCallbacks[OldEntry.Index] = nullptr;    // Update the callback.
00219 
00220   // Otherwise, we need to add the old symbol to the new block's set.  If it is
00221   // just a single entry, upgrade it to a symbol list.
00222   if (MCSymbol *PrevSym = NewEntry.Symbols.dyn_cast<MCSymbol*>()) {
00223     std::vector<MCSymbol*> *SymList = new std::vector<MCSymbol*>();
00224     SymList->push_back(PrevSym);
00225     NewEntry.Symbols = SymList;
00226   }
00227 
00228   std::vector<MCSymbol*> *SymList =
00229     NewEntry.Symbols.get<std::vector<MCSymbol*>*>();
00230 
00231   // If the old entry was a single symbol, add it.
00232   if (MCSymbol *Sym = OldEntry.Symbols.dyn_cast<MCSymbol*>()) {
00233     SymList->push_back(Sym);
00234     return;
00235   }
00236 
00237   // Otherwise, concatenate the list.
00238   std::vector<MCSymbol*> *Syms =OldEntry.Symbols.get<std::vector<MCSymbol*>*>();
00239   SymList->insert(SymList->end(), Syms->begin(), Syms->end());
00240   delete Syms;
00241 }
00242 
00243 
00244 void MMIAddrLabelMapCallbackPtr::deleted() {
00245   Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
00246 }
00247 
00248 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
00249   Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
00250 }
00251 
00252 
00253 //===----------------------------------------------------------------------===//
00254 
00255 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI,
00256                                      const MCRegisterInfo &MRI,
00257                                      const MCObjectFileInfo *MOFI)
00258   : ImmutablePass(ID), Context(&MAI, &MRI, MOFI, nullptr, false) {
00259   initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
00260 }
00261 
00262 MachineModuleInfo::MachineModuleInfo()
00263   : ImmutablePass(ID), Context(nullptr, nullptr, nullptr) {
00264   llvm_unreachable("This MachineModuleInfo constructor should never be called, "
00265                    "MMI should always be explicitly constructed by "
00266                    "LLVMTargetMachine");
00267 }
00268 
00269 MachineModuleInfo::~MachineModuleInfo() {
00270 }
00271 
00272 bool MachineModuleInfo::doInitialization(Module &M) {
00273 
00274   ObjFileMMI = nullptr;
00275   CurCallSite = 0;
00276   CallsEHReturn = 0;
00277   CallsUnwindInit = 0;
00278   DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
00279   // Always emit some info, by default "no personality" info.
00280   Personalities.push_back(nullptr);
00281   PersonalityTypeCache = EHPersonality::Unknown;
00282   AddrLabelSymbols = nullptr;
00283   TheModule = nullptr;
00284 
00285   return false;
00286 }
00287 
00288 bool MachineModuleInfo::doFinalization(Module &M) {
00289 
00290   Personalities.clear();
00291 
00292   delete AddrLabelSymbols;
00293   AddrLabelSymbols = nullptr;
00294 
00295   Context.reset();
00296 
00297   delete ObjFileMMI;
00298   ObjFileMMI = nullptr;
00299 
00300   return false;
00301 }
00302 
00303 /// EndFunction - Discard function meta information.
00304 ///
00305 void MachineModuleInfo::EndFunction() {
00306   // Clean up frame info.
00307   FrameInstructions.clear();
00308 
00309   // Clean up exception info.
00310   LandingPads.clear();
00311   CallSiteMap.clear();
00312   TypeInfos.clear();
00313   FilterIds.clear();
00314   FilterEnds.clear();
00315   CallsEHReturn = 0;
00316   CallsUnwindInit = 0;
00317   VariableDbgInfos.clear();
00318 }
00319 
00320 /// AnalyzeModule - Scan the module for global debug information.
00321 ///
00322 void MachineModuleInfo::AnalyzeModule(const Module &M) {
00323   // Insert functions in the llvm.used array (but not llvm.compiler.used) into
00324   // UsedFunctions.
00325   const GlobalVariable *GV = M.getGlobalVariable("llvm.used");
00326   if (!GV || !GV->hasInitializer()) return;
00327 
00328   // Should be an array of 'i8*'.
00329   const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
00330 
00331   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
00332     if (const Function *F =
00333           dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
00334       UsedFunctions.insert(F);
00335 }
00336 
00337 //===- Address of Block Management ----------------------------------------===//
00338 
00339 
00340 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
00341 /// block when its address is taken.  This cannot be its normal LBB label
00342 /// because the block may be accessed outside its containing function.
00343 MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
00344   // Lazily create AddrLabelSymbols.
00345   if (!AddrLabelSymbols)
00346     AddrLabelSymbols = new MMIAddrLabelMap(Context);
00347   return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
00348 }
00349 
00350 /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
00351 /// basic block when its address is taken.  If other blocks were RAUW'd to
00352 /// this one, we may have to emit them as well, return the whole set.
00353 std::vector<MCSymbol*> MachineModuleInfo::
00354 getAddrLabelSymbolToEmit(const BasicBlock *BB) {
00355   // Lazily create AddrLabelSymbols.
00356   if (!AddrLabelSymbols)
00357     AddrLabelSymbols = new MMIAddrLabelMap(Context);
00358  return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
00359 }
00360 
00361 
00362 /// takeDeletedSymbolsForFunction - If the specified function has had any
00363 /// references to address-taken blocks generated, but the block got deleted,
00364 /// return the symbol now so we can emit it.  This prevents emitting a
00365 /// reference to a symbol that has no definition.
00366 void MachineModuleInfo::
00367 takeDeletedSymbolsForFunction(const Function *F,
00368                               std::vector<MCSymbol*> &Result) {
00369   // If no blocks have had their addresses taken, we're done.
00370   if (!AddrLabelSymbols) return;
00371   return AddrLabelSymbols->
00372      takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
00373 }
00374 
00375 //===- EH -----------------------------------------------------------------===//
00376 
00377 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
00378 /// specified MachineBasicBlock.
00379 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
00380     (MachineBasicBlock *LandingPad) {
00381   unsigned N = LandingPads.size();
00382   for (unsigned i = 0; i < N; ++i) {
00383     LandingPadInfo &LP = LandingPads[i];
00384     if (LP.LandingPadBlock == LandingPad)
00385       return LP;
00386   }
00387 
00388   LandingPads.push_back(LandingPadInfo(LandingPad));
00389   return LandingPads[N];
00390 }
00391 
00392 /// addInvoke - Provide the begin and end labels of an invoke style call and
00393 /// associate it with a try landing pad block.
00394 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
00395                                   MCSymbol *BeginLabel, MCSymbol *EndLabel) {
00396   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00397   LP.BeginLabels.push_back(BeginLabel);
00398   LP.EndLabels.push_back(EndLabel);
00399 }
00400 
00401 /// addLandingPad - Provide the label of a try LandingPad block.
00402 ///
00403 MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
00404   MCSymbol *LandingPadLabel = Context.createTempSymbol();
00405   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00406   LP.LandingPadLabel = LandingPadLabel;
00407   return LandingPadLabel;
00408 }
00409 
00410 /// addPersonality - Provide the personality function for the exception
00411 /// information.
00412 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
00413                                        const Function *Personality) {
00414   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00415   LP.Personality = Personality;
00416 
00417   for (unsigned i = 0; i < Personalities.size(); ++i)
00418     if (Personalities[i] == Personality)
00419       return;
00420 
00421   // If this is the first personality we're adding go
00422   // ahead and add it at the beginning.
00423   if (!Personalities[0])
00424     Personalities[0] = Personality;
00425   else
00426     Personalities.push_back(Personality);
00427 }
00428 
00429 void MachineModuleInfo::addWinEHState(MachineBasicBlock *LandingPad,
00430                                       int State) {
00431   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00432   LP.WinEHState = State;
00433 }
00434 
00435 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
00436 ///
00437 void MachineModuleInfo::
00438 addCatchTypeInfo(MachineBasicBlock *LandingPad,
00439                  ArrayRef<const GlobalValue *> TyInfo) {
00440   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00441   for (unsigned N = TyInfo.size(); N; --N)
00442     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
00443 }
00444 
00445 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
00446 ///
00447 void MachineModuleInfo::
00448 addFilterTypeInfo(MachineBasicBlock *LandingPad,
00449                   ArrayRef<const GlobalValue *> TyInfo) {
00450   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00451   std::vector<unsigned> IdsInFilter(TyInfo.size());
00452   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
00453     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
00454   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
00455 }
00456 
00457 /// addCleanup - Add a cleanup action for a landing pad.
00458 ///
00459 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
00460   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00461   LP.TypeIds.push_back(0);
00462 }
00463 
00464 void MachineModuleInfo::addSEHCatchHandler(MachineBasicBlock *LandingPad,
00465                                            const Function *Filter,
00466                                            const BlockAddress *RecoverBA) {
00467   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00468   SEHHandler Handler;
00469   Handler.FilterOrFinally = Filter;
00470   Handler.RecoverBA = RecoverBA;
00471   LP.SEHHandlers.push_back(Handler);
00472 }
00473 
00474 void MachineModuleInfo::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
00475                                              const Function *Cleanup) {
00476   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00477   SEHHandler Handler;
00478   Handler.FilterOrFinally = Cleanup;
00479   Handler.RecoverBA = nullptr;
00480   LP.SEHHandlers.push_back(Handler);
00481 }
00482 
00483 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
00484 /// pads.
00485 void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
00486   for (unsigned i = 0; i != LandingPads.size(); ) {
00487     LandingPadInfo &LandingPad = LandingPads[i];
00488     if (LandingPad.LandingPadLabel &&
00489         !LandingPad.LandingPadLabel->isDefined() &&
00490         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
00491       LandingPad.LandingPadLabel = nullptr;
00492 
00493     // Special case: we *should* emit LPs with null LP MBB. This indicates
00494     // "nounwind" case.
00495     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
00496       LandingPads.erase(LandingPads.begin() + i);
00497       continue;
00498     }
00499 
00500     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
00501       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
00502       MCSymbol *EndLabel = LandingPad.EndLabels[j];
00503       if ((BeginLabel->isDefined() ||
00504            (LPMap && (*LPMap)[BeginLabel] != 0)) &&
00505           (EndLabel->isDefined() ||
00506            (LPMap && (*LPMap)[EndLabel] != 0))) continue;
00507 
00508       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
00509       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
00510       --j, --e;
00511     }
00512 
00513     // Remove landing pads with no try-ranges.
00514     if (LandingPads[i].BeginLabels.empty()) {
00515       LandingPads.erase(LandingPads.begin() + i);
00516       continue;
00517     }
00518 
00519     // If there is no landing pad, ensure that the list of typeids is empty.
00520     // If the only typeid is a cleanup, this is the same as having no typeids.
00521     if (!LandingPad.LandingPadBlock ||
00522         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
00523       LandingPad.TypeIds.clear();
00524     ++i;
00525   }
00526 }
00527 
00528 /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call site
00529 /// indexes.
00530 void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
00531                                               ArrayRef<unsigned> Sites) {
00532   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
00533 }
00534 
00535 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
00536 /// function wide.
00537 unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
00538   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
00539     if (TypeInfos[i] == TI) return i + 1;
00540 
00541   TypeInfos.push_back(TI);
00542   return TypeInfos.size();
00543 }
00544 
00545 /// getFilterIDFor - Return the filter id for the specified typeinfos.  This is
00546 /// function wide.
00547 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
00548   // If the new filter coincides with the tail of an existing filter, then
00549   // re-use the existing filter.  Folding filters more than this requires
00550   // re-ordering filters and/or their elements - probably not worth it.
00551   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
00552        E = FilterEnds.end(); I != E; ++I) {
00553     unsigned i = *I, j = TyIds.size();
00554 
00555     while (i && j)
00556       if (FilterIds[--i] != TyIds[--j])
00557         goto try_next;
00558 
00559     if (!j)
00560       // The new filter coincides with range [i, end) of the existing filter.
00561       return -(1 + i);
00562 
00563 try_next:;
00564   }
00565 
00566   // Add the new filter.
00567   int FilterID = -(1 + FilterIds.size());
00568   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
00569   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
00570   FilterEnds.push_back(FilterIds.size());
00571   FilterIds.push_back(0); // terminator
00572   return FilterID;
00573 }
00574 
00575 /// getPersonality - Return the personality function for the current function.
00576 const Function *MachineModuleInfo::getPersonality() const {
00577   for (const LandingPadInfo &LPI : LandingPads)
00578     if (LPI.Personality)
00579       return LPI.Personality;
00580   return nullptr;
00581 }
00582 
00583 EHPersonality MachineModuleInfo::getPersonalityType() {
00584   if (PersonalityTypeCache == EHPersonality::Unknown) {
00585     if (const Function *F = getPersonality())
00586       PersonalityTypeCache = classifyEHPersonality(F);
00587   }
00588   return PersonalityTypeCache;
00589 }
00590 
00591 /// getPersonalityIndex - Return unique index for current personality
00592 /// function. NULL/first personality function should always get zero index.
00593 unsigned MachineModuleInfo::getPersonalityIndex() const {
00594   const Function* Personality = nullptr;
00595 
00596   // Scan landing pads. If there is at least one non-NULL personality - use it.
00597   for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
00598     if (LandingPads[i].Personality) {
00599       Personality = LandingPads[i].Personality;
00600       break;
00601     }
00602 
00603   for (unsigned i = 0, e = Personalities.size(); i < e; ++i) {
00604     if (Personalities[i] == Personality)
00605       return i;
00606   }
00607 
00608   // This will happen if the current personality function is
00609   // in the zero index.
00610   return 0;
00611 }
00612 
00613 const Function *MachineModuleInfo::getWinEHParent(const Function *F) const {
00614   StringRef WinEHParentName =
00615       F->getFnAttribute("wineh-parent").getValueAsString();
00616   if (WinEHParentName.empty() || WinEHParentName == F->getName())
00617     return F;
00618   return F->getParent()->getFunction(WinEHParentName);
00619 }
00620 
00621 WinEHFuncInfo &MachineModuleInfo::getWinEHFuncInfo(const Function *F) {
00622   auto &Ptr = FuncInfoMap[getWinEHParent(F)];
00623   if (!Ptr)
00624     Ptr.reset(new WinEHFuncInfo);
00625   return *Ptr;
00626 }