LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - DwarfCFIException.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 71 72 98.6 %
Date: 2017-09-14 15:23:50 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
       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             : // This file contains support for writing DWARF exception info into asm files.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "DwarfException.h"
      15             : #include "llvm/ADT/StringExtras.h"
      16             : #include "llvm/ADT/Twine.h"
      17             : #include "llvm/BinaryFormat/Dwarf.h"
      18             : #include "llvm/CodeGen/AsmPrinter.h"
      19             : #include "llvm/CodeGen/MachineFrameInfo.h"
      20             : #include "llvm/CodeGen/MachineFunction.h"
      21             : #include "llvm/CodeGen/MachineModuleInfo.h"
      22             : #include "llvm/IR/DataLayout.h"
      23             : #include "llvm/IR/Mangler.h"
      24             : #include "llvm/IR/Module.h"
      25             : #include "llvm/MC/MCAsmInfo.h"
      26             : #include "llvm/MC/MCContext.h"
      27             : #include "llvm/MC/MCExpr.h"
      28             : #include "llvm/MC/MCSection.h"
      29             : #include "llvm/MC/MCStreamer.h"
      30             : #include "llvm/MC/MCSymbol.h"
      31             : #include "llvm/MC/MachineLocation.h"
      32             : #include "llvm/Support/ErrorHandling.h"
      33             : #include "llvm/Support/FormattedStream.h"
      34             : #include "llvm/Target/TargetFrameLowering.h"
      35             : #include "llvm/Target/TargetLoweringObjectFile.h"
      36             : #include "llvm/Target/TargetMachine.h"
      37             : #include "llvm/Target/TargetOptions.h"
      38             : #include "llvm/Target/TargetRegisterInfo.h"
      39             : using namespace llvm;
      40             : 
      41       14329 : DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
      42       14329 :     : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
      43             : 
      44      122104 : void DwarfCFIExceptionBase::markFunctionEnd() {
      45      122104 :   endFragment();
      46             : 
      47             :   // Map all labels and get rid of any dead landing pads.
      48      244206 :   if (!Asm->MF->getLandingPads().empty()) {
      49        2459 :     MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
      50        2459 :     NonConstMF->tidyLandingPads();
      51             :   }
      52      122103 : }
      53             : 
      54      122103 : void DwarfCFIExceptionBase::endFragment() {
      55      122103 :   if (shouldEmitCFI)
      56      158438 :     Asm->OutStreamer->EmitCFIEndProc();
      57      122103 : }
      58             : 
      59       12587 : DwarfCFIException::DwarfCFIException(AsmPrinter *A)
      60             :     : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
      61             :       forceEmitPersonality(false), shouldEmitLSDA(false),
      62       25174 :       shouldEmitMoves(false) {}
      63             : 
      64       25056 : DwarfCFIException::~DwarfCFIException() {}
      65             : 
      66             : /// endModule - Emit all exception information that should come after the
      67             : /// content.
      68       12527 : void DwarfCFIException::endModule() {
      69             :   // SjLj uses this pass and it doesn't need this info.
      70       12527 :   if (!Asm->MAI->usesCFIForEH())
      71             :     return;
      72             : 
      73       11828 :   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
      74             : 
      75       11829 :   unsigned PerEncoding = TLOF.getPersonalityEncoding();
      76             : 
      77       11829 :   if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
      78             :     return;
      79             : 
      80             :   // Emit references to all used personality functions
      81       19782 :   for (const Function *Personality : MMI->getPersonalities()) {
      82         110 :     if (!Personality)
      83           0 :       continue;
      84         110 :     MCSymbol *Sym = Asm->getSymbol(Personality);
      85         220 :     TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
      86             :   }
      87             : }
      88             : 
      89        2401 : static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
      90        2401 :   return Asm->getCurExceptionSym();
      91             : }
      92             : 
      93      112815 : void DwarfCFIException::beginFunction(const MachineFunction *MF) {
      94      112815 :   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
      95      112815 :   const Function *F = MF->getFunction();
      96             : 
      97             :   // If any landing pads survive, we need an EH table.
      98      225630 :   bool hasLandingPads = !MF->getLandingPads().empty();
      99             : 
     100             :   // See if we need frame move info.
     101      112815 :   AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
     102             : 
     103      112816 :   shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
     104             : 
     105      112816 :   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     106      112816 :   unsigned PerEncoding = TLOF.getPersonalityEncoding();
     107      112816 :   const Function *Per = nullptr;
     108      112816 :   if (F->hasPersonalityFn())
     109        5524 :     Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
     110             : 
     111             :   // Emit a personality function even when there are no landing pads
     112      112816 :   forceEmitPersonality =
     113             :       // ...if a personality function is explicitly specified
     114      112816 :       F->hasPersonalityFn() &&
     115             :       // ... and it's not known to be a noop in the absence of invokes
     116      112892 :       !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
     117             :       // ... and we're not explicitly asked not to emit it
     118          38 :       F->needsUnwindTableEntry();
     119             : 
     120      112816 :   shouldEmitPersonality =
     121      112779 :       (forceEmitPersonality ||
     122      115247 :        (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
     123             :       Per;
     124             : 
     125      112816 :   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
     126      112816 :   shouldEmitLSDA = shouldEmitPersonality &&
     127             :     LSDAEncoding != dwarf::DW_EH_PE_omit;
     128             : 
     129      336199 :   shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
     130      108166 :                   (shouldEmitPersonality || shouldEmitMoves);
     131      338448 :   beginFragment(&*MF->begin(), getExceptionSym);
     132      112816 : }
     133             : 
     134      112816 : void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
     135             :                                       ExceptionSymbolProvider ESP) {
     136      112816 :   if (!shouldEmitCFI)
     137             :     return;
     138             : 
     139       79160 :   if (!hasEmittedCFISections) {
     140        7494 :     if (Asm->needsOnlyDebugCFIMoves())
     141         308 :       Asm->OutStreamer->EmitCFISections(false, true);
     142        7494 :     hasEmittedCFISections = true;
     143             :   }
     144             : 
     145      158320 :   Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
     146             : 
     147             :   // Indicate personality routine, if any.
     148       79160 :   if (!shouldEmitPersonality)
     149             :     return;
     150             : 
     151        2401 :   auto *F = MBB->getParent()->getFunction();
     152        7203 :   auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
     153             :   assert(P && "Expected personality function");
     154             : 
     155             :   // If we are forced to emit this personality, make sure to record
     156             :   // it because it might not appear in any landingpad
     157        2401 :   if (forceEmitPersonality)
     158          30 :     MMI->addPersonality(P);
     159             : 
     160        2401 :   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     161        2401 :   unsigned PerEncoding = TLOF.getPersonalityEncoding();
     162        2401 :   const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
     163        4802 :   Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
     164             : 
     165             :   // Provide LSDA information.
     166        2401 :   if (shouldEmitLSDA)
     167        4802 :     Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
     168             : }
     169             : 
     170             : /// endFunction - Gather and emit post-function exception information.
     171             : ///
     172      112816 : void DwarfCFIException::endFunction(const MachineFunction *MF) {
     173      112816 :   if (!shouldEmitPersonality)
     174             :     return;
     175             : 
     176        2430 :   emitExceptionTable();
     177             : }

Generated by: LCOV version 1.13