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.emplace_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   PersonalityTypeCache = EHPersonality::Unknown;
00312   CallSiteMap.clear();
00313   TypeInfos.clear();
00314   FilterIds.clear();
00315   FilterEnds.clear();
00316   CallsEHReturn = 0;
00317   CallsUnwindInit = 0;
00318   VariableDbgInfos.clear();
00319 }
00320 
00321 //===- Address of Block Management ----------------------------------------===//
00322 
00323 
00324 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
00325 /// block when its address is taken.  This cannot be its normal LBB label
00326 /// because the block may be accessed outside its containing function.
00327 MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
00328   // Lazily create AddrLabelSymbols.
00329   if (!AddrLabelSymbols)
00330     AddrLabelSymbols = new MMIAddrLabelMap(Context);
00331   return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
00332 }
00333 
00334 /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
00335 /// basic block when its address is taken.  If other blocks were RAUW'd to
00336 /// this one, we may have to emit them as well, return the whole set.
00337 std::vector<MCSymbol*> MachineModuleInfo::
00338 getAddrLabelSymbolToEmit(const BasicBlock *BB) {
00339   // Lazily create AddrLabelSymbols.
00340   if (!AddrLabelSymbols)
00341     AddrLabelSymbols = new MMIAddrLabelMap(Context);
00342  return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
00343 }
00344 
00345 
00346 /// takeDeletedSymbolsForFunction - If the specified function has had any
00347 /// references to address-taken blocks generated, but the block got deleted,
00348 /// return the symbol now so we can emit it.  This prevents emitting a
00349 /// reference to a symbol that has no definition.
00350 void MachineModuleInfo::
00351 takeDeletedSymbolsForFunction(const Function *F,
00352                               std::vector<MCSymbol*> &Result) {
00353   // If no blocks have had their addresses taken, we're done.
00354   if (!AddrLabelSymbols) return;
00355   return AddrLabelSymbols->
00356      takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
00357 }
00358 
00359 //===- EH -----------------------------------------------------------------===//
00360 
00361 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
00362 /// specified MachineBasicBlock.
00363 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
00364     (MachineBasicBlock *LandingPad) {
00365   unsigned N = LandingPads.size();
00366   for (unsigned i = 0; i < N; ++i) {
00367     LandingPadInfo &LP = LandingPads[i];
00368     if (LP.LandingPadBlock == LandingPad)
00369       return LP;
00370   }
00371 
00372   LandingPads.push_back(LandingPadInfo(LandingPad));
00373   return LandingPads[N];
00374 }
00375 
00376 /// addInvoke - Provide the begin and end labels of an invoke style call and
00377 /// associate it with a try landing pad block.
00378 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
00379                                   MCSymbol *BeginLabel, MCSymbol *EndLabel) {
00380   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00381   LP.BeginLabels.push_back(BeginLabel);
00382   LP.EndLabels.push_back(EndLabel);
00383 }
00384 
00385 /// addLandingPad - Provide the label of a try LandingPad block.
00386 ///
00387 MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
00388   MCSymbol *LandingPadLabel = Context.createTempSymbol();
00389   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00390   LP.LandingPadLabel = LandingPadLabel;
00391   return LandingPadLabel;
00392 }
00393 
00394 /// addPersonality - Provide the personality function for the exception
00395 /// information.
00396 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
00397                                        const Function *Personality) {
00398   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00399   LP.Personality = Personality;
00400 
00401   for (unsigned i = 0; i < Personalities.size(); ++i)
00402     if (Personalities[i] == Personality)
00403       return;
00404 
00405   // If this is the first personality we're adding go
00406   // ahead and add it at the beginning.
00407   if (!Personalities[0])
00408     Personalities[0] = Personality;
00409   else
00410     Personalities.push_back(Personality);
00411 }
00412 
00413 void MachineModuleInfo::addWinEHState(MachineBasicBlock *LandingPad,
00414                                       int State) {
00415   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00416   LP.WinEHState = State;
00417 }
00418 
00419 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
00420 ///
00421 void MachineModuleInfo::
00422 addCatchTypeInfo(MachineBasicBlock *LandingPad,
00423                  ArrayRef<const GlobalValue *> TyInfo) {
00424   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00425   for (unsigned N = TyInfo.size(); N; --N)
00426     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
00427 }
00428 
00429 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
00430 ///
00431 void MachineModuleInfo::
00432 addFilterTypeInfo(MachineBasicBlock *LandingPad,
00433                   ArrayRef<const GlobalValue *> TyInfo) {
00434   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00435   std::vector<unsigned> IdsInFilter(TyInfo.size());
00436   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
00437     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
00438   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
00439 }
00440 
00441 /// addCleanup - Add a cleanup action for a landing pad.
00442 ///
00443 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
00444   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00445   LP.TypeIds.push_back(0);
00446 }
00447 
00448 void MachineModuleInfo::addSEHCatchHandler(MachineBasicBlock *LandingPad,
00449                                            const Function *Filter,
00450                                            const BlockAddress *RecoverBA) {
00451   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00452   SEHHandler Handler;
00453   Handler.FilterOrFinally = Filter;
00454   Handler.RecoverBA = RecoverBA;
00455   LP.SEHHandlers.push_back(Handler);
00456 }
00457 
00458 void MachineModuleInfo::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
00459                                              const Function *Cleanup) {
00460   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00461   SEHHandler Handler;
00462   Handler.FilterOrFinally = Cleanup;
00463   Handler.RecoverBA = nullptr;
00464   LP.SEHHandlers.push_back(Handler);
00465 }
00466 
00467 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
00468 /// pads.
00469 void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
00470   for (unsigned i = 0; i != LandingPads.size(); ) {
00471     LandingPadInfo &LandingPad = LandingPads[i];
00472     if (LandingPad.LandingPadLabel &&
00473         !LandingPad.LandingPadLabel->isDefined() &&
00474         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
00475       LandingPad.LandingPadLabel = nullptr;
00476 
00477     // Special case: we *should* emit LPs with null LP MBB. This indicates
00478     // "nounwind" case.
00479     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
00480       LandingPads.erase(LandingPads.begin() + i);
00481       continue;
00482     }
00483 
00484     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
00485       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
00486       MCSymbol *EndLabel = LandingPad.EndLabels[j];
00487       if ((BeginLabel->isDefined() ||
00488            (LPMap && (*LPMap)[BeginLabel] != 0)) &&
00489           (EndLabel->isDefined() ||
00490            (LPMap && (*LPMap)[EndLabel] != 0))) continue;
00491 
00492       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
00493       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
00494       --j, --e;
00495     }
00496 
00497     // Remove landing pads with no try-ranges.
00498     if (LandingPads[i].BeginLabels.empty()) {
00499       LandingPads.erase(LandingPads.begin() + i);
00500       continue;
00501     }
00502 
00503     // If there is no landing pad, ensure that the list of typeids is empty.
00504     // If the only typeid is a cleanup, this is the same as having no typeids.
00505     if (!LandingPad.LandingPadBlock ||
00506         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
00507       LandingPad.TypeIds.clear();
00508     ++i;
00509   }
00510 }
00511 
00512 /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call site
00513 /// indexes.
00514 void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
00515                                               ArrayRef<unsigned> Sites) {
00516   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
00517 }
00518 
00519 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is
00520 /// function wide.
00521 unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
00522   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
00523     if (TypeInfos[i] == TI) return i + 1;
00524 
00525   TypeInfos.push_back(TI);
00526   return TypeInfos.size();
00527 }
00528 
00529 /// getFilterIDFor - Return the filter id for the specified typeinfos.  This is
00530 /// function wide.
00531 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
00532   // If the new filter coincides with the tail of an existing filter, then
00533   // re-use the existing filter.  Folding filters more than this requires
00534   // re-ordering filters and/or their elements - probably not worth it.
00535   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
00536        E = FilterEnds.end(); I != E; ++I) {
00537     unsigned i = *I, j = TyIds.size();
00538 
00539     while (i && j)
00540       if (FilterIds[--i] != TyIds[--j])
00541         goto try_next;
00542 
00543     if (!j)
00544       // The new filter coincides with range [i, end) of the existing filter.
00545       return -(1 + i);
00546 
00547 try_next:;
00548   }
00549 
00550   // Add the new filter.
00551   int FilterID = -(1 + FilterIds.size());
00552   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
00553   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
00554   FilterEnds.push_back(FilterIds.size());
00555   FilterIds.push_back(0); // terminator
00556   return FilterID;
00557 }
00558 
00559 /// getPersonality - Return the personality function for the current function.
00560 const Function *MachineModuleInfo::getPersonality() const {
00561   for (const LandingPadInfo &LPI : LandingPads)
00562     if (LPI.Personality)
00563       return LPI.Personality;
00564   return nullptr;
00565 }
00566 
00567 EHPersonality MachineModuleInfo::getPersonalityType() {
00568   if (PersonalityTypeCache == EHPersonality::Unknown) {
00569     if (const Function *F = getPersonality())
00570       PersonalityTypeCache = classifyEHPersonality(F);
00571   }
00572   return PersonalityTypeCache;
00573 }
00574 
00575 /// getPersonalityIndex - Return unique index for current personality
00576 /// function. NULL/first personality function should always get zero index.
00577 unsigned MachineModuleInfo::getPersonalityIndex() const {
00578   const Function* Personality = nullptr;
00579 
00580   // Scan landing pads. If there is at least one non-NULL personality - use it.
00581   for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
00582     if (LandingPads[i].Personality) {
00583       Personality = LandingPads[i].Personality;
00584       break;
00585     }
00586 
00587   for (unsigned i = 0, e = Personalities.size(); i < e; ++i) {
00588     if (Personalities[i] == Personality)
00589       return i;
00590   }
00591 
00592   // This will happen if the current personality function is
00593   // in the zero index.
00594   return 0;
00595 }
00596 
00597 const Function *MachineModuleInfo::getWinEHParent(const Function *F) const {
00598   StringRef WinEHParentName =
00599       F->getFnAttribute("wineh-parent").getValueAsString();
00600   if (WinEHParentName.empty() || WinEHParentName == F->getName())
00601     return F;
00602   return F->getParent()->getFunction(WinEHParentName);
00603 }
00604 
00605 WinEHFuncInfo &MachineModuleInfo::getWinEHFuncInfo(const Function *F) {
00606   auto &Ptr = FuncInfoMap[getWinEHParent(F)];
00607   if (!Ptr)
00608     Ptr.reset(new WinEHFuncInfo);
00609   return *Ptr;
00610 }