LLVM API Documentation

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