MachineModuleInfo.cpp 11 KB

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