MachineModuleInfo.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. #include "llvm/CodeGen/MachineModuleInfo.h"
  10. #include "llvm/ADT/PointerUnion.h"
  11. #include "llvm/Analysis/LibCallSemantics.h"
  12. #include "llvm/Analysis/ValueTracking.h"
  13. #include "llvm/CodeGen/MachineFunction.h"
  14. #include "llvm/CodeGen/MachineFunctionPass.h"
  15. #include "llvm/CodeGen/Passes.h"
  16. #include "llvm/CodeGen/WinEHFuncInfo.h"
  17. #include "llvm/IR/Constants.h"
  18. #include "llvm/IR/DerivedTypes.h"
  19. #include "llvm/IR/GlobalVariable.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/MC/MCObjectFileInfo.h"
  22. #include "llvm/MC/MCSymbol.h"
  23. #include "llvm/Support/Dwarf.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. using namespace llvm;
  26. using namespace llvm::dwarf;
  27. // Handle the Pass registration stuff necessary to use DataLayout's.
  28. INITIALIZE_PASS(MachineModuleInfo, "machinemoduleinfo",
  29. "Machine Module Information", false, false)
  30. char MachineModuleInfo::ID = 0;
  31. // Out of line virtual method.
  32. MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
  33. namespace llvm {
  34. class MMIAddrLabelMapCallbackPtr : CallbackVH {
  35. MMIAddrLabelMap *Map;
  36. public:
  37. MMIAddrLabelMapCallbackPtr() : Map(nullptr) {}
  38. MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(nullptr) {}
  39. void setPtr(BasicBlock *BB) {
  40. ValueHandleBase::operator=(BB);
  41. }
  42. void setMap(MMIAddrLabelMap *map) { Map = map; }
  43. void deleted() override;
  44. void allUsesReplacedWith(Value *V2) override;
  45. };
  46. class MMIAddrLabelMap {
  47. MCContext &Context;
  48. struct AddrLabelSymEntry {
  49. /// Symbols - The symbols for the label. This is a pointer union that is
  50. /// either one symbol (the common case) or a list of symbols.
  51. PointerUnion<MCSymbol *, std::vector<MCSymbol*>*> Symbols;
  52. Function *Fn; // The containing function of the BasicBlock.
  53. unsigned Index; // The index in BBCallbacks for the BasicBlock.
  54. };
  55. DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
  56. /// BBCallbacks - Callbacks for the BasicBlock's that we have entries for. We
  57. /// use this so we get notified if a block is deleted or RAUWd.
  58. std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
  59. /// DeletedAddrLabelsNeedingEmission - This is a per-function list of symbols
  60. /// whose corresponding BasicBlock got deleted. These symbols need to be
  61. /// emitted at some point in the file, so AsmPrinter emits them after the
  62. /// function body.
  63. DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
  64. DeletedAddrLabelsNeedingEmission;
  65. public:
  66. MMIAddrLabelMap(MCContext &context) : Context(context) {}
  67. ~MMIAddrLabelMap() {
  68. assert(DeletedAddrLabelsNeedingEmission.empty() &&
  69. "Some labels for deleted blocks never got emitted");
  70. // Deallocate any of the 'list of symbols' case.
  71. for (DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry>::iterator
  72. I = AddrLabelSymbols.begin(), E = AddrLabelSymbols.end(); I != E; ++I)
  73. if (I->second.Symbols.is<std::vector<MCSymbol*>*>())
  74. delete I->second.Symbols.get<std::vector<MCSymbol*>*>();
  75. }
  76. MCSymbol *getAddrLabelSymbol(BasicBlock *BB);
  77. std::vector<MCSymbol*> getAddrLabelSymbolToEmit(BasicBlock *BB);
  78. void takeDeletedSymbolsForFunction(Function *F,
  79. std::vector<MCSymbol*> &Result);
  80. void UpdateForDeletedBlock(BasicBlock *BB);
  81. void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
  82. };
  83. }
  84. MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
  85. assert(BB->hasAddressTaken() &&
  86. "Shouldn't get label for block without address taken");
  87. AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
  88. // If we already had an entry for this block, just return it.
  89. if (!Entry.Symbols.isNull()) {
  90. assert(BB->getParent() == Entry.Fn && "Parent changed");
  91. if (Entry.Symbols.is<MCSymbol*>())
  92. return Entry.Symbols.get<MCSymbol*>();
  93. return (*Entry.Symbols.get<std::vector<MCSymbol*>*>())[0];
  94. }
  95. // Otherwise, this is a new entry, create a new symbol for it and add an
  96. // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
  97. BBCallbacks.push_back(BB);
  98. BBCallbacks.back().setMap(this);
  99. Entry.Index = BBCallbacks.size()-1;
  100. Entry.Fn = BB->getParent();
  101. MCSymbol *Result = Context.CreateTempSymbol();
  102. Entry.Symbols = Result;
  103. return Result;
  104. }
  105. std::vector<MCSymbol*>
  106. MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
  107. assert(BB->hasAddressTaken() &&
  108. "Shouldn't get label for block without address taken");
  109. AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
  110. std::vector<MCSymbol*> Result;
  111. // If we already had an entry for this block, just return it.
  112. if (Entry.Symbols.isNull())
  113. Result.push_back(getAddrLabelSymbol(BB));
  114. else if (MCSymbol *Sym = Entry.Symbols.dyn_cast<MCSymbol*>())
  115. Result.push_back(Sym);
  116. else
  117. Result = *Entry.Symbols.get<std::vector<MCSymbol*>*>();
  118. return Result;
  119. }
  120. /// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
  121. /// them.
  122. void MMIAddrLabelMap::
  123. takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
  124. DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
  125. DeletedAddrLabelsNeedingEmission.find(F);
  126. // If there are no entries for the function, just return.
  127. if (I == DeletedAddrLabelsNeedingEmission.end()) return;
  128. // Otherwise, take the list.
  129. std::swap(Result, I->second);
  130. DeletedAddrLabelsNeedingEmission.erase(I);
  131. }
  132. void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
  133. // If the block got deleted, there is no need for the symbol. If the symbol
  134. // was already emitted, we can just forget about it, otherwise we need to
  135. // queue it up for later emission when the function is output.
  136. AddrLabelSymEntry Entry = AddrLabelSymbols[BB];
  137. AddrLabelSymbols.erase(BB);
  138. assert(!Entry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
  139. BBCallbacks[Entry.Index] = nullptr; // Clear the callback.
  140. assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
  141. "Block/parent mismatch");
  142. // Handle both the single and the multiple symbols cases.
  143. if (MCSymbol *Sym = Entry.Symbols.dyn_cast<MCSymbol*>()) {
  144. if (Sym->isDefined())
  145. return;
  146. // If the block is not yet defined, we need to emit it at the end of the
  147. // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
  148. // for the containing Function. Since the block is being deleted, its
  149. // parent may already be removed, we have to get the function from 'Entry'.
  150. DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
  151. } else {
  152. std::vector<MCSymbol*> *Syms = Entry.Symbols.get<std::vector<MCSymbol*>*>();
  153. for (unsigned i = 0, e = Syms->size(); i != e; ++i) {
  154. MCSymbol *Sym = (*Syms)[i];
  155. if (Sym->isDefined()) continue; // Ignore already emitted labels.
  156. // If the block is not yet defined, we need to emit it at the end of the
  157. // function. Add the symbol to the DeletedAddrLabelsNeedingEmission list
  158. // for the containing Function. Since the block is being deleted, its
  159. // parent may already be removed, we have to get the function from
  160. // 'Entry'.
  161. DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
  162. }
  163. // The entry is deleted, free the memory associated with the symbol list.
  164. delete Syms;
  165. }
  166. }
  167. void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
  168. // Get the entry for the RAUW'd block and remove it from our map.
  169. AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old];
  170. AddrLabelSymbols.erase(Old);
  171. assert(!OldEntry.Symbols.isNull() && "Didn't have a symbol, why a callback?");
  172. AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
  173. // If New is not address taken, just move our symbol over to it.
  174. if (NewEntry.Symbols.isNull()) {
  175. BBCallbacks[OldEntry.Index].setPtr(New); // Update the callback.
  176. NewEntry = OldEntry; // Set New's entry.
  177. return;
  178. }
  179. BBCallbacks[OldEntry.Index] = nullptr; // Update the callback.
  180. // Otherwise, we need to add the old symbol to the new block's set. If it is
  181. // just a single entry, upgrade it to a symbol list.
  182. if (MCSymbol *PrevSym = NewEntry.Symbols.dyn_cast<MCSymbol*>()) {
  183. std::vector<MCSymbol*> *SymList = new std::vector<MCSymbol*>();
  184. SymList->push_back(PrevSym);
  185. NewEntry.Symbols = SymList;
  186. }
  187. std::vector<MCSymbol*> *SymList =
  188. NewEntry.Symbols.get<std::vector<MCSymbol*>*>();
  189. // If the old entry was a single symbol, add it.
  190. if (MCSymbol *Sym = OldEntry.Symbols.dyn_cast<MCSymbol*>()) {
  191. SymList->push_back(Sym);
  192. return;
  193. }
  194. // Otherwise, concatenate the list.
  195. std::vector<MCSymbol*> *Syms =OldEntry.Symbols.get<std::vector<MCSymbol*>*>();
  196. SymList->insert(SymList->end(), Syms->begin(), Syms->end());
  197. delete Syms;
  198. }
  199. void MMIAddrLabelMapCallbackPtr::deleted() {
  200. Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
  201. }
  202. void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
  203. Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
  204. }
  205. //===----------------------------------------------------------------------===//
  206. MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI,
  207. const MCRegisterInfo &MRI,
  208. const MCObjectFileInfo *MOFI)
  209. : ImmutablePass(ID), Context(&MAI, &MRI, MOFI, nullptr, false) {
  210. initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
  211. }
  212. MachineModuleInfo::MachineModuleInfo()
  213. : ImmutablePass(ID), Context(nullptr, nullptr, nullptr) {
  214. llvm_unreachable("This MachineModuleInfo constructor should never be called, "
  215. "MMI should always be explicitly constructed by "
  216. "LLVMTargetMachine");
  217. }
  218. MachineModuleInfo::~MachineModuleInfo() {
  219. }
  220. bool MachineModuleInfo::doInitialization(Module &M) {
  221. ObjFileMMI = nullptr;
  222. CurCallSite = 0;
  223. CallsEHReturn = 0;
  224. CallsUnwindInit = 0;
  225. DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
  226. // Always emit some info, by default "no personality" info.
  227. Personalities.push_back(nullptr);
  228. PersonalityTypeCache = EHPersonality::Unknown;
  229. AddrLabelSymbols = nullptr;
  230. TheModule = nullptr;
  231. return false;
  232. }
  233. bool MachineModuleInfo::doFinalization(Module &M) {
  234. Personalities.clear();
  235. delete AddrLabelSymbols;
  236. AddrLabelSymbols = nullptr;
  237. Context.reset();
  238. delete ObjFileMMI;
  239. ObjFileMMI = nullptr;
  240. return false;
  241. }
  242. /// EndFunction - Discard function meta information.
  243. ///
  244. void MachineModuleInfo::EndFunction() {
  245. // Clean up frame info.
  246. FrameInstructions.clear();
  247. // Clean up exception info.
  248. LandingPads.clear();
  249. CallSiteMap.clear();
  250. TypeInfos.clear();
  251. FilterIds.clear();
  252. FilterEnds.clear();
  253. CallsEHReturn = 0;
  254. CallsUnwindInit = 0;
  255. VariableDbgInfos.clear();
  256. }
  257. /// AnalyzeModule - Scan the module for global debug information.
  258. ///
  259. void MachineModuleInfo::AnalyzeModule(const Module &M) {
  260. // Insert functions in the llvm.used array (but not llvm.compiler.used) into
  261. // UsedFunctions.
  262. const GlobalVariable *GV = M.getGlobalVariable("llvm.used");
  263. if (!GV || !GV->hasInitializer()) return;
  264. // Should be an array of 'i8*'.
  265. const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
  266. for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
  267. if (const Function *F =
  268. dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
  269. UsedFunctions.insert(F);
  270. }
  271. //===- Address of Block Management ----------------------------------------===//
  272. /// getAddrLabelSymbol - Return the symbol to be used for the specified basic
  273. /// block when its address is taken. This cannot be its normal LBB label
  274. /// because the block may be accessed outside its containing function.
  275. MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
  276. // Lazily create AddrLabelSymbols.
  277. if (!AddrLabelSymbols)
  278. AddrLabelSymbols = new MMIAddrLabelMap(Context);
  279. return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
  280. }
  281. /// getAddrLabelSymbolToEmit - Return the symbol to be used for the specified
  282. /// basic block when its address is taken. If other blocks were RAUW'd to
  283. /// this one, we may have to emit them as well, return the whole set.
  284. std::vector<MCSymbol*> MachineModuleInfo::
  285. getAddrLabelSymbolToEmit(const BasicBlock *BB) {
  286. // Lazily create AddrLabelSymbols.
  287. if (!AddrLabelSymbols)
  288. AddrLabelSymbols = new MMIAddrLabelMap(Context);
  289. return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
  290. }
  291. /// takeDeletedSymbolsForFunction - If the specified function has had any
  292. /// references to address-taken blocks generated, but the block got deleted,
  293. /// return the symbol now so we can emit it. This prevents emitting a
  294. /// reference to a symbol that has no definition.
  295. void MachineModuleInfo::
  296. takeDeletedSymbolsForFunction(const Function *F,
  297. std::vector<MCSymbol*> &Result) {
  298. // If no blocks have had their addresses taken, we're done.
  299. if (!AddrLabelSymbols) return;
  300. return AddrLabelSymbols->
  301. takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
  302. }
  303. //===- EH -----------------------------------------------------------------===//
  304. /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
  305. /// specified MachineBasicBlock.
  306. LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
  307. (MachineBasicBlock *LandingPad) {
  308. unsigned N = LandingPads.size();
  309. for (unsigned i = 0; i < N; ++i) {
  310. LandingPadInfo &LP = LandingPads[i];
  311. if (LP.LandingPadBlock == LandingPad)
  312. return LP;
  313. }
  314. LandingPads.push_back(LandingPadInfo(LandingPad));
  315. return LandingPads[N];
  316. }
  317. /// addInvoke - Provide the begin and end labels of an invoke style call and
  318. /// associate it with a try landing pad block.
  319. void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
  320. MCSymbol *BeginLabel, MCSymbol *EndLabel) {
  321. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  322. LP.BeginLabels.push_back(BeginLabel);
  323. LP.EndLabels.push_back(EndLabel);
  324. }
  325. /// addLandingPad - Provide the label of a try LandingPad block.
  326. ///
  327. MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
  328. MCSymbol *LandingPadLabel = Context.CreateTempSymbol();
  329. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  330. LP.LandingPadLabel = LandingPadLabel;
  331. return LandingPadLabel;
  332. }
  333. /// addPersonality - Provide the personality function for the exception
  334. /// information.
  335. void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
  336. const Function *Personality) {
  337. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  338. LP.Personality = Personality;
  339. for (unsigned i = 0; i < Personalities.size(); ++i)
  340. if (Personalities[i] == Personality)
  341. return;
  342. // If this is the first personality we're adding go
  343. // ahead and add it at the beginning.
  344. if (!Personalities[0])
  345. Personalities[0] = Personality;
  346. else
  347. Personalities.push_back(Personality);
  348. }
  349. void MachineModuleInfo::addWinEHState(MachineBasicBlock *LandingPad,
  350. int State) {
  351. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  352. LP.WinEHState = State;
  353. }
  354. /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
  355. ///
  356. void MachineModuleInfo::
  357. addCatchTypeInfo(MachineBasicBlock *LandingPad,
  358. ArrayRef<const GlobalValue *> TyInfo) {
  359. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  360. for (unsigned N = TyInfo.size(); N; --N)
  361. LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
  362. }
  363. /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
  364. ///
  365. void MachineModuleInfo::
  366. addFilterTypeInfo(MachineBasicBlock *LandingPad,
  367. ArrayRef<const GlobalValue *> TyInfo) {
  368. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  369. std::vector<unsigned> IdsInFilter(TyInfo.size());
  370. for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
  371. IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
  372. LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
  373. }
  374. /// addCleanup - Add a cleanup action for a landing pad.
  375. ///
  376. void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
  377. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  378. LP.TypeIds.push_back(0);
  379. }
  380. MCSymbol *
  381. MachineModuleInfo::addClauseForLandingPad(MachineBasicBlock *LandingPad) {
  382. MCSymbol *ClauseLabel = Context.CreateTempSymbol();
  383. LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
  384. LP.ClauseLabels.push_back(ClauseLabel);
  385. return ClauseLabel;
  386. }
  387. /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
  388. /// pads.
  389. void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
  390. for (unsigned i = 0; i != LandingPads.size(); ) {
  391. LandingPadInfo &LandingPad = LandingPads[i];
  392. if (LandingPad.LandingPadLabel &&
  393. !LandingPad.LandingPadLabel->isDefined() &&
  394. (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
  395. LandingPad.LandingPadLabel = nullptr;
  396. // Special case: we *should* emit LPs with null LP MBB. This indicates
  397. // "nounwind" case.
  398. if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
  399. LandingPads.erase(LandingPads.begin() + i);
  400. continue;
  401. }
  402. for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
  403. MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
  404. MCSymbol *EndLabel = LandingPad.EndLabels[j];
  405. if ((BeginLabel->isDefined() ||
  406. (LPMap && (*LPMap)[BeginLabel] != 0)) &&
  407. (EndLabel->isDefined() ||
  408. (LPMap && (*LPMap)[EndLabel] != 0))) continue;
  409. LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
  410. LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
  411. --j, --e;
  412. }
  413. // Remove landing pads with no try-ranges.
  414. if (LandingPads[i].BeginLabels.empty()) {
  415. LandingPads.erase(LandingPads.begin() + i);
  416. continue;
  417. }
  418. // If there is no landing pad, ensure that the list of typeids is empty.
  419. // If the only typeid is a cleanup, this is the same as having no typeids.
  420. if (!LandingPad.LandingPadBlock ||
  421. (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
  422. LandingPad.TypeIds.clear();
  423. ++i;
  424. }
  425. }
  426. /// setCallSiteLandingPad - Map the landing pad's EH symbol to the call site
  427. /// indexes.
  428. void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
  429. ArrayRef<unsigned> Sites) {
  430. LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
  431. }
  432. /// getTypeIDFor - Return the type id for the specified typeinfo. This is
  433. /// function wide.
  434. unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
  435. for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
  436. if (TypeInfos[i] == TI) return i + 1;
  437. TypeInfos.push_back(TI);
  438. return TypeInfos.size();
  439. }
  440. /// getFilterIDFor - Return the filter id for the specified typeinfos. This is
  441. /// function wide.
  442. int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
  443. // If the new filter coincides with the tail of an existing filter, then
  444. // re-use the existing filter. Folding filters more than this requires
  445. // re-ordering filters and/or their elements - probably not worth it.
  446. for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
  447. E = FilterEnds.end(); I != E; ++I) {
  448. unsigned i = *I, j = TyIds.size();
  449. while (i && j)
  450. if (FilterIds[--i] != TyIds[--j])
  451. goto try_next;
  452. if (!j)
  453. // The new filter coincides with range [i, end) of the existing filter.
  454. return -(1 + i);
  455. try_next:;
  456. }
  457. // Add the new filter.
  458. int FilterID = -(1 + FilterIds.size());
  459. FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
  460. FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
  461. FilterEnds.push_back(FilterIds.size());
  462. FilterIds.push_back(0); // terminator
  463. return FilterID;
  464. }
  465. /// getPersonality - Return the personality function for the current function.
  466. const Function *MachineModuleInfo::getPersonality() const {
  467. for (const LandingPadInfo &LPI : LandingPads)
  468. if (LPI.Personality)
  469. return LPI.Personality;
  470. return nullptr;
  471. }
  472. EHPersonality MachineModuleInfo::getPersonalityType() {
  473. if (PersonalityTypeCache == EHPersonality::Unknown)
  474. PersonalityTypeCache = classifyEHPersonality(getPersonality());
  475. return PersonalityTypeCache;
  476. }
  477. /// getPersonalityIndex - Return unique index for current personality
  478. /// function. NULL/first personality function should always get zero index.
  479. unsigned MachineModuleInfo::getPersonalityIndex() const {
  480. const Function* Personality = nullptr;
  481. // Scan landing pads. If there is at least one non-NULL personality - use it.
  482. for (unsigned i = 0, e = LandingPads.size(); i != e; ++i)
  483. if (LandingPads[i].Personality) {
  484. Personality = LandingPads[i].Personality;
  485. break;
  486. }
  487. for (unsigned i = 0, e = Personalities.size(); i < e; ++i) {
  488. if (Personalities[i] == Personality)
  489. return i;
  490. }
  491. // This will happen if the current personality function is
  492. // in the zero index.
  493. return 0;
  494. }
  495. const Function *MachineModuleInfo::getWinEHParent(const Function *F) const {
  496. StringRef WinEHParentName =
  497. F->getFnAttribute("wineh-parent").getValueAsString();
  498. if (WinEHParentName.empty() || WinEHParentName == F->getName())
  499. return F;
  500. return F->getParent()->getFunction(WinEHParentName);
  501. }
  502. WinEHFuncInfo &MachineModuleInfo::getWinEHFuncInfo(const Function *F) {
  503. auto &Ptr = FuncInfoMap[getWinEHParent(F)];
  504. if (!Ptr)
  505. Ptr.reset(new WinEHFuncInfo);
  506. return *Ptr;
  507. }