xref: /llvm-project/llvm/lib/CodeGen/MachineModuleInfo.cpp (revision 456dce8a84c97b56b10f0e7143c36bee4ac85fe4)
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 
10 #include "llvm/CodeGen/MachineModuleInfo.h"
11 #include "llvm/ADT/PointerUnion.h"
12 #include "llvm/ADT/PostOrderIterator.h"
13 #include "llvm/ADT/TinyPtrVector.h"
14 #include "llvm/Analysis/EHPersonalities.h"
15 #include "llvm/Analysis/ValueTracking.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineFunctionInitializer.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/GlobalVariable.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Target/TargetLoweringObjectFile.h"
30 #include "llvm/Target/TargetMachine.h"
31 using namespace llvm;
32 using namespace llvm::dwarf;
33 
34 // Handle the Pass registration stuff necessary to use DataLayout's.
35 INITIALIZE_TM_PASS(MachineModuleInfo, "machinemoduleinfo",
36                    "Machine Module Information", false, false)
37 char MachineModuleInfo::ID = 0;
38 
39 // Out of line virtual method.
40 MachineModuleInfoImpl::~MachineModuleInfoImpl() {}
41 
42 namespace llvm {
43 class MMIAddrLabelMapCallbackPtr final : CallbackVH {
44   MMIAddrLabelMap *Map;
45 public:
46   MMIAddrLabelMapCallbackPtr() : Map(nullptr) {}
47   MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(nullptr) {}
48 
49   void setPtr(BasicBlock *BB) {
50     ValueHandleBase::operator=(BB);
51   }
52 
53   void setMap(MMIAddrLabelMap *map) { Map = map; }
54 
55   void deleted() override;
56   void allUsesReplacedWith(Value *V2) override;
57 };
58 
59 class MMIAddrLabelMap {
60   MCContext &Context;
61   struct AddrLabelSymEntry {
62     /// The symbols for the label.
63     TinyPtrVector<MCSymbol *> Symbols;
64 
65     Function *Fn;   // The containing function of the BasicBlock.
66     unsigned Index; // The index in BBCallbacks for the BasicBlock.
67   };
68 
69   DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
70 
71   /// Callbacks for the BasicBlock's that we have entries for.  We use this so
72   /// we get notified if a block is deleted or RAUWd.
73   std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
74 
75   /// This is a per-function list of symbols whose corresponding BasicBlock got
76   /// deleted.  These symbols need to be emitted at some point in the file, so
77   /// AsmPrinter emits them after the function body.
78   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
79     DeletedAddrLabelsNeedingEmission;
80 public:
81 
82   MMIAddrLabelMap(MCContext &context) : Context(context) {}
83   ~MMIAddrLabelMap() {
84     assert(DeletedAddrLabelsNeedingEmission.empty() &&
85            "Some labels for deleted blocks never got emitted");
86   }
87 
88   ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
89 
90   void takeDeletedSymbolsForFunction(Function *F,
91                                      std::vector<MCSymbol*> &Result);
92 
93   void UpdateForDeletedBlock(BasicBlock *BB);
94   void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
95 };
96 }
97 
98 ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
99   assert(BB->hasAddressTaken() &&
100          "Shouldn't get label for block without address taken");
101   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
102 
103   // If we already had an entry for this block, just return it.
104   if (!Entry.Symbols.empty()) {
105     assert(BB->getParent() == Entry.Fn && "Parent changed");
106     return Entry.Symbols;
107   }
108 
109   // Otherwise, this is a new entry, create a new symbol for it and add an
110   // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
111   BBCallbacks.emplace_back(BB);
112   BBCallbacks.back().setMap(this);
113   Entry.Index = BBCallbacks.size() - 1;
114   Entry.Fn = BB->getParent();
115   Entry.Symbols.push_back(Context.createTempSymbol());
116   return Entry.Symbols;
117 }
118 
119 /// If we have any deleted symbols for F, return them.
120 void MMIAddrLabelMap::
121 takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
122   DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
123     DeletedAddrLabelsNeedingEmission.find(F);
124 
125   // If there are no entries for the function, just return.
126   if (I == DeletedAddrLabelsNeedingEmission.end()) return;
127 
128   // Otherwise, take the list.
129   std::swap(Result, I->second);
130   DeletedAddrLabelsNeedingEmission.erase(I);
131 }
132 
133 
134 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
135   // If the block got deleted, there is no need for the symbol.  If the symbol
136   // was already emitted, we can just forget about it, otherwise we need to
137   // queue it up for later emission when the function is output.
138   AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
139   AddrLabelSymbols.erase(BB);
140   assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
141   BBCallbacks[Entry.Index] = nullptr;  // Clear the callback.
142 
143   assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
144          "Block/parent mismatch");
145 
146   for (MCSymbol *Sym : Entry.Symbols) {
147     if (Sym->isDefined())
148       return;
149 
150     // If the block is not yet defined, we need to emit it at the end of the
151     // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
152     // for the containing Function.  Since the block is being deleted, its
153     // parent may already be removed, we have to get the function from 'Entry'.
154     DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
155   }
156 }
157 
158 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
159   // Get the entry for the RAUW'd block and remove it from our map.
160   AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
161   AddrLabelSymbols.erase(Old);
162   assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
163 
164   AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
165 
166   // If New is not address taken, just move our symbol over to it.
167   if (NewEntry.Symbols.empty()) {
168     BBCallbacks[OldEntry.Index].setPtr(New);    // Update the callback.
169     NewEntry = std::move(OldEntry);             // Set New's entry.
170     return;
171   }
172 
173   BBCallbacks[OldEntry.Index] = nullptr;    // Update the callback.
174 
175   // Otherwise, we need to add the old symbols to the new block's set.
176   NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(),
177                           OldEntry.Symbols.end());
178 }
179 
180 
181 void MMIAddrLabelMapCallbackPtr::deleted() {
182   Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
183 }
184 
185 void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
186   Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
187 }
188 
189 
190 //===----------------------------------------------------------------------===//
191 
192 MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM)
193   : ImmutablePass(ID), TM(*TM),
194     Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
195             TM->getObjFileLowering(), nullptr, false) {
196   initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
197 }
198 
199 MachineModuleInfo::~MachineModuleInfo() {
200 }
201 
202 bool MachineModuleInfo::doInitialization(Module &M) {
203 
204   ObjFileMMI = nullptr;
205   CurCallSite = 0;
206   CallsEHReturn = false;
207   CallsUnwindInit = false;
208   HasEHFunclets = false;
209   DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
210   PersonalityTypeCache = EHPersonality::Unknown;
211   AddrLabelSymbols = nullptr;
212   TheModule = &M;
213 
214   return false;
215 }
216 
217 bool MachineModuleInfo::doFinalization(Module &M) {
218 
219   Personalities.clear();
220 
221   delete AddrLabelSymbols;
222   AddrLabelSymbols = nullptr;
223 
224   Context.reset();
225 
226   delete ObjFileMMI;
227   ObjFileMMI = nullptr;
228 
229   return false;
230 }
231 
232 void MachineModuleInfo::EndFunction() {
233   // Clean up frame info.
234   FrameInstructions.clear();
235 
236   // Clean up exception info.
237   LandingPads.clear();
238   PersonalityTypeCache = EHPersonality::Unknown;
239   CallSiteMap.clear();
240   TypeInfos.clear();
241   FilterIds.clear();
242   FilterEnds.clear();
243   CallsEHReturn = false;
244   CallsUnwindInit = false;
245   HasEHFunclets = false;
246   VariableDbgInfos.clear();
247 }
248 
249 //===- Address of Block Management ----------------------------------------===//
250 
251 ArrayRef<MCSymbol *>
252 MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
253   // Lazily create AddrLabelSymbols.
254   if (!AddrLabelSymbols)
255     AddrLabelSymbols = new MMIAddrLabelMap(Context);
256  return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
257 }
258 
259 void MachineModuleInfo::
260 takeDeletedSymbolsForFunction(const Function *F,
261                               std::vector<MCSymbol*> &Result) {
262   // If no blocks have had their addresses taken, we're done.
263   if (!AddrLabelSymbols) return;
264   return AddrLabelSymbols->
265      takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
266 }
267 
268 //===- EH -----------------------------------------------------------------===//
269 
270 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
271     (MachineBasicBlock *LandingPad) {
272   unsigned N = LandingPads.size();
273   for (unsigned i = 0; i < N; ++i) {
274     LandingPadInfo &LP = LandingPads[i];
275     if (LP.LandingPadBlock == LandingPad)
276       return LP;
277   }
278 
279   LandingPads.push_back(LandingPadInfo(LandingPad));
280   return LandingPads[N];
281 }
282 
283 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
284                                   MCSymbol *BeginLabel, MCSymbol *EndLabel) {
285   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
286   LP.BeginLabels.push_back(BeginLabel);
287   LP.EndLabels.push_back(EndLabel);
288 }
289 
290 MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
291   MCSymbol *LandingPadLabel = Context.createTempSymbol();
292   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
293   LP.LandingPadLabel = LandingPadLabel;
294   return LandingPadLabel;
295 }
296 
297 void MachineModuleInfo::addPersonality(const Function *Personality) {
298   for (unsigned i = 0; i < Personalities.size(); ++i)
299     if (Personalities[i] == Personality)
300       return;
301   Personalities.push_back(Personality);
302 }
303 
304 void MachineModuleInfo::
305 addCatchTypeInfo(MachineBasicBlock *LandingPad,
306                  ArrayRef<const GlobalValue *> TyInfo) {
307   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
308   for (unsigned N = TyInfo.size(); N; --N)
309     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
310 }
311 
312 void MachineModuleInfo::
313 addFilterTypeInfo(MachineBasicBlock *LandingPad,
314                   ArrayRef<const GlobalValue *> TyInfo) {
315   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
316   std::vector<unsigned> IdsInFilter(TyInfo.size());
317   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
318     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
319   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
320 }
321 
322 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
323   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
324   LP.TypeIds.push_back(0);
325 }
326 
327 void MachineModuleInfo::addSEHCatchHandler(MachineBasicBlock *LandingPad,
328                                            const Function *Filter,
329                                            const BlockAddress *RecoverBA) {
330   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
331   SEHHandler Handler;
332   Handler.FilterOrFinally = Filter;
333   Handler.RecoverBA = RecoverBA;
334   LP.SEHHandlers.push_back(Handler);
335 }
336 
337 void MachineModuleInfo::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
338                                              const Function *Cleanup) {
339   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
340   SEHHandler Handler;
341   Handler.FilterOrFinally = Cleanup;
342   Handler.RecoverBA = nullptr;
343   LP.SEHHandlers.push_back(Handler);
344 }
345 
346 void MachineModuleInfo::TidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
347   for (unsigned i = 0; i != LandingPads.size(); ) {
348     LandingPadInfo &LandingPad = LandingPads[i];
349     if (LandingPad.LandingPadLabel &&
350         !LandingPad.LandingPadLabel->isDefined() &&
351         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
352       LandingPad.LandingPadLabel = nullptr;
353 
354     // Special case: we *should* emit LPs with null LP MBB. This indicates
355     // "nounwind" case.
356     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
357       LandingPads.erase(LandingPads.begin() + i);
358       continue;
359     }
360 
361     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
362       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
363       MCSymbol *EndLabel = LandingPad.EndLabels[j];
364       if ((BeginLabel->isDefined() ||
365            (LPMap && (*LPMap)[BeginLabel] != 0)) &&
366           (EndLabel->isDefined() ||
367            (LPMap && (*LPMap)[EndLabel] != 0))) continue;
368 
369       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
370       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
371       --j;
372       --e;
373     }
374 
375     // Remove landing pads with no try-ranges.
376     if (LandingPads[i].BeginLabels.empty()) {
377       LandingPads.erase(LandingPads.begin() + i);
378       continue;
379     }
380 
381     // If there is no landing pad, ensure that the list of typeids is empty.
382     // If the only typeid is a cleanup, this is the same as having no typeids.
383     if (!LandingPad.LandingPadBlock ||
384         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
385       LandingPad.TypeIds.clear();
386     ++i;
387   }
388 }
389 
390 void MachineModuleInfo::setCallSiteLandingPad(MCSymbol *Sym,
391                                               ArrayRef<unsigned> Sites) {
392   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
393 }
394 
395 unsigned MachineModuleInfo::getTypeIDFor(const GlobalValue *TI) {
396   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
397     if (TypeInfos[i] == TI) return i + 1;
398 
399   TypeInfos.push_back(TI);
400   return TypeInfos.size();
401 }
402 
403 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
404   // If the new filter coincides with the tail of an existing filter, then
405   // re-use the existing filter.  Folding filters more than this requires
406   // re-ordering filters and/or their elements - probably not worth it.
407   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
408        E = FilterEnds.end(); I != E; ++I) {
409     unsigned i = *I, j = TyIds.size();
410 
411     while (i && j)
412       if (FilterIds[--i] != TyIds[--j])
413         goto try_next;
414 
415     if (!j)
416       // The new filter coincides with range [i, end) of the existing filter.
417       return -(1 + i);
418 
419 try_next:;
420   }
421 
422   // Add the new filter.
423   int FilterID = -(1 + FilterIds.size());
424   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
425   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
426   FilterEnds.push_back(FilterIds.size());
427   FilterIds.push_back(0); // terminator
428   return FilterID;
429 }
430 
431 MachineFunction &MachineModuleInfo::getMachineFunction(const Function &F) {
432   // Shortcut for the common case where a sequence of MachineFunctionPasses
433   // all query for the same Function.
434   if (LastRequest == &F)
435     return *LastResult;
436 
437   auto I = MachineFunctions.insert(
438       std::make_pair(&F, std::unique_ptr<MachineFunction>()));
439   MachineFunction *MF;
440   if (I.second) {
441     // No pre-existing machine function, create a new one.
442     MF = new MachineFunction(&F, TM, NextFnNum++, *this);
443     // Update the set entry.
444     I.first->second.reset(MF);
445 
446     if (MFInitializer)
447       if (MFInitializer->initializeMachineFunction(*MF))
448         report_fatal_error("Unable to initialize machine function");
449   } else {
450     MF = I.first->second.get();
451   }
452 
453   LastRequest = &F;
454   LastResult = MF;
455   return *MF;
456 }
457 
458 void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
459   MachineFunctions.erase(&F);
460   LastRequest = nullptr;
461   LastResult = nullptr;
462 }
463 
464 namespace {
465 /// This pass frees the MachineFunction object associated with a Function.
466 class FreeMachineFunction : public FunctionPass {
467 public:
468   static char ID;
469   FreeMachineFunction() : FunctionPass(ID) {}
470 
471   void getAnalysisUsage(AnalysisUsage &AU) const override {
472     AU.addRequired<MachineModuleInfo>();
473     AU.addPreserved<MachineModuleInfo>();
474   }
475 
476   bool runOnFunction(Function &F) override {
477     MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
478     MMI.deleteMachineFunctionFor(F);
479     return true;
480   }
481 };
482 char FreeMachineFunction::ID;
483 } // end anonymous namespace
484 
485 namespace llvm {
486 FunctionPass *createFreeMachineFunctionPass() {
487   return new FreeMachineFunction();
488 }
489 } // end namespace llvm
490 
491 //===- MMI building helpers -----------------------------------------------===//
492 
493 void llvm::ComputeUsesVAFloatArgument(const CallInst &I,
494                                       MachineModuleInfo *MMI) {
495   FunctionType *FT =
496       cast<FunctionType>(I.getCalledValue()->getType()->getContainedType(0));
497   if (FT->isVarArg() && !MMI->usesVAFloatArgument()) {
498     for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) {
499       Type *T = I.getArgOperand(i)->getType();
500       for (auto i : post_order(T)) {
501         if (i->isFloatingPointTy()) {
502           MMI->setUsesVAFloatArgument(true);
503           return;
504         }
505       }
506     }
507   }
508 }
509 
510 void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI,
511                              MachineBasicBlock *MBB) {
512   if (const auto *PF = dyn_cast<Function>(
513           I.getParent()->getParent()->getPersonalityFn()->stripPointerCasts()))
514     MMI.addPersonality(PF);
515 
516   if (I.isCleanup())
517     MMI.addCleanup(MBB);
518 
519   // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
520   //        but we need to do it this way because of how the DWARF EH emitter
521   //        processes the clauses.
522   for (unsigned i = I.getNumClauses(); i != 0; --i) {
523     Value *Val = I.getClause(i - 1);
524     if (I.isCatch(i - 1)) {
525       MMI.addCatchTypeInfo(MBB,
526                            dyn_cast<GlobalValue>(Val->stripPointerCasts()));
527     } else {
528       // Add filters in a list.
529       Constant *CVal = cast<Constant>(Val);
530       SmallVector<const GlobalValue *, 4> FilterList;
531       for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
532            II != IE; ++II)
533         FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
534 
535       MMI.addFilterTypeInfo(MBB, FilterList);
536     }
537   }
538 }
539