Skip to content

Commit 1cd1444

Browse files
committed
Reland 196270 "Generalize debug info / EH emission in AsmPrinter"
Addressing the existense AMDGPUAsmPrinter and other subclasses of AsmPrinter llvm-svn: 196288
1 parent a6d47ae commit 1cd1444

File tree

11 files changed

+182
-84
lines changed

11 files changed

+182
-84
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/ErrorHandling.h"
2323

2424
namespace llvm {
25+
class AsmPrinterHandler;
2526
class BlockAddress;
2627
class GCStrategy;
2728
class Constant;
@@ -110,13 +111,21 @@ namespace llvm {
110111
/// function.
111112
MachineLoopInfo *LI;
112113

114+
struct HandlerInfo {
115+
AsmPrinterHandler *Handler;
116+
const char *TimerName, *TimerGroupName;
117+
HandlerInfo(AsmPrinterHandler *Handler, const char *TimerName,
118+
const char *TimerGroupName)
119+
: Handler(Handler), TimerName(TimerName),
120+
TimerGroupName(TimerGroupName) {}
121+
};
122+
/// Handlers - a vector of all debug/EH info emitters we should use.
123+
/// This vector maintains ownership of the emitters.
124+
SmallVector<HandlerInfo, 1> Handlers;
125+
113126
/// DD - If the target supports dwarf debug info, this pointer is non-null.
114127
DwarfDebug *DD;
115128

116-
/// DE - If the target supports dwarf exception info, this pointer is
117-
/// non-null.
118-
DwarfException *DE;
119-
120129
protected:
121130
explicit AsmPrinter(TargetMachine &TM, MCStreamer &Streamer);
122131

llvm/lib/CodeGen/AsmPrinter/ARMException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void ARMException::beginFunction(const MachineFunction *MF) {
6666

6767
/// endFunction - Gather and emit post-function exception information.
6868
///
69-
void ARMException::endFunction() {
69+
void ARMException::endFunction(const MachineFunction *) {
7070
ARMTargetStreamer &ATS = getTargetStreamer();
7171
if (!Asm->MF->getFunction()->needsUnwindTableEntry())
7272
ATS.emitCantUnwind();

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ AsmPrinter::AsmPrinter(TargetMachine &tm, MCStreamer &Streamer)
9999
OutContext(Streamer.getContext()),
100100
OutStreamer(Streamer),
101101
LastMI(0), LastFn(0), Counter(~0U), SetCounter(0) {
102-
DD = 0; DE = 0; MMI = 0; LI = 0; MF = 0;
102+
DD = 0; MMI = 0; LI = 0; MF = 0;
103103
CurrentFnSym = CurrentFnSymForSize = 0;
104104
GCMetadataPrinters = 0;
105105
VerboseAsm = Streamer.isVerboseAsm();
106106
}
107107

108108
AsmPrinter::~AsmPrinter() {
109-
assert(DD == 0 && DE == 0 && "Debug/EH info didn't get finalized");
109+
assert(DD == 0 && Handlers.empty() && "Debug/EH info didn't get finalized");
110110

111111
if (GCMetadataPrinters != 0) {
112112
gcp_map_type &GCMap = getGCMap(GCMetadataPrinters);
@@ -192,25 +192,29 @@ bool AsmPrinter::doInitialization(Module &M) {
192192
OutStreamer.AddBlankLine();
193193
}
194194

195-
if (MAI->doesSupportDebugInformation())
195+
if (MAI->doesSupportDebugInformation()) {
196196
DD = new DwarfDebug(this, &M);
197+
Handlers.push_back(HandlerInfo(DD, DbgTimerName, DWARFGroupName));
198+
}
197199

200+
DwarfException *DE = 0;
198201
switch (MAI->getExceptionHandlingType()) {
199202
case ExceptionHandling::None:
200-
return false;
203+
break;
201204
case ExceptionHandling::SjLj:
202205
case ExceptionHandling::DwarfCFI:
203206
DE = new DwarfCFIException(this);
204-
return false;
207+
break;
205208
case ExceptionHandling::ARM:
206209
DE = new ARMException(this);
207-
return false;
210+
break;
208211
case ExceptionHandling::Win64:
209212
DE = new Win64Exception(this);
210-
return false;
213+
break;
211214
}
212-
213-
llvm_unreachable("Unknown exception type.");
215+
if (DE)
216+
Handlers.push_back(HandlerInfo(DE, EHTimerName, DWARFGroupName));
217+
return false;
214218
}
215219

216220
void AsmPrinter::EmitLinkage(const GlobalValue *GV, MCSymbol *GVSym) const {
@@ -311,8 +315,11 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
311315
// sections and expected to be contiguous (e.g. ObjC metadata).
312316
unsigned AlignLog = getGVAlignmentLog2(GV, *DL);
313317

314-
if (DD)
315-
DD->setSymbolSize(GVSym, Size);
318+
for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
319+
const HandlerInfo &OI = Handlers[I];
320+
NamedRegionTimer T(OI.TimerName, OI.TimerGroupName, TimePassesIsEnabled);
321+
OI.Handler->setSymbolSize(GVSym, Size);
322+
}
316323

317324
// Handle common and BSS local symbols (.lcomm).
318325
if (GVKind.isCommon() || GVKind.isBSSLocal()) {
@@ -482,13 +489,10 @@ void AsmPrinter::EmitFunctionHeader() {
482489
}
483490

484491
// Emit pre-function debug and/or EH information.
485-
if (DE) {
486-
NamedRegionTimer T(EHTimerName, DWARFGroupName, TimePassesIsEnabled);
487-
DE->beginFunction(MF);
488-
}
489-
if (DD) {
490-
NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
491-
DD->beginFunction(MF);
492+
for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
493+
const HandlerInfo &OI = Handlers[I];
494+
NamedRegionTimer T(OI.TimerName, OI.TimerGroupName, TimePassesIsEnabled);
495+
OI.Handler->beginFunction(MF);
492496
}
493497

494498
// Emit the prefix data.
@@ -693,7 +697,7 @@ void AsmPrinter::EmitFunctionBody() {
693697
// Emit target-specific gunk before the function body.
694698
EmitFunctionBodyStart();
695699

696-
bool ShouldPrintDebugScopes = DD && MMI->hasDebugInfo();
700+
bool ShouldPrintDebugScopes = MMI->hasDebugInfo();
697701

698702
// Print out code for the function.
699703
bool HasAnyRealCode = false;
@@ -714,8 +718,12 @@ void AsmPrinter::EmitFunctionBody() {
714718
}
715719

716720
if (ShouldPrintDebugScopes) {
717-
NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
718-
DD->beginInstruction(II);
721+
for (unsigned III = 0, EEE = Handlers.size(); III != EEE; ++III) {
722+
const HandlerInfo &OI = Handlers[III];
723+
NamedRegionTimer T(OI.TimerName, OI.TimerGroupName,
724+
TimePassesIsEnabled);
725+
OI.Handler->beginInstruction(II);
726+
}
719727
}
720728

721729
if (isVerbose())
@@ -754,8 +762,12 @@ void AsmPrinter::EmitFunctionBody() {
754762
}
755763

756764
if (ShouldPrintDebugScopes) {
757-
NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
758-
DD->endInstruction(II);
765+
for (unsigned III = 0, EEE = Handlers.size(); III != EEE; ++III) {
766+
const HandlerInfo &OI = Handlers[III];
767+
NamedRegionTimer T(OI.TimerName, OI.TimerGroupName,
768+
TimePassesIsEnabled);
769+
OI.Handler->endInstruction();
770+
}
759771
}
760772
}
761773
}
@@ -811,14 +823,11 @@ void AsmPrinter::EmitFunctionBody() {
811823
OutStreamer.EmitELFSize(CurrentFnSym, SizeExp);
812824
}
813825

814-
// Emit post-function debug information.
815-
if (DD) {
816-
NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
817-
DD->endFunction(MF);
818-
}
819-
if (DE) {
820-
NamedRegionTimer T(EHTimerName, DWARFGroupName, TimePassesIsEnabled);
821-
DE->endFunction();
826+
// Emit post-function debug and/or EH information.
827+
for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
828+
const HandlerInfo &OI = Handlers[I];
829+
NamedRegionTimer T(OI.TimerName, OI.TimerGroupName, TimePassesIsEnabled);
830+
OI.Handler->endFunction(MF);
822831
}
823832
MMI->EndFunction();
824833

@@ -907,20 +916,15 @@ bool AsmPrinter::doFinalization(Module &M) {
907916
OutStreamer.Flush();
908917

909918
// Finalize debug and EH information.
910-
if (DE) {
911-
{
912-
NamedRegionTimer T(EHTimerName, DWARFGroupName, TimePassesIsEnabled);
913-
DE->endModule();
914-
}
915-
delete DE; DE = 0;
916-
}
917-
if (DD) {
918-
{
919-
NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
920-
DD->endModule();
921-
}
922-
delete DD; DD = 0;
923-
}
919+
for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
920+
const HandlerInfo &OI = Handlers[I];
921+
NamedRegionTimer T(OI.TimerName, OI.TimerGroupName,
922+
TimePassesIsEnabled);
923+
OI.Handler->endModule();
924+
delete OI.Handler;
925+
}
926+
Handlers.clear();
927+
DD = 0;
924928

925929
// If the target wants to know about weak references, print them all.
926930
if (MAI->getWeakRefDirective()) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//===-- lib/CodeGen/AsmPrinter/AsmPrinterHandler.h -------------*- 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+
// This file contains a generic interface for AsmPrinter handlers,
11+
// like debug and EH info emitters.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef CODEGEN_ASMPRINTER_ASMPRINTERHANDLER_H__
16+
#define CODEGEN_ASMPRINTER_ASMPRINTERHANDLER_H__
17+
18+
#include "llvm/Support/DataTypes.h"
19+
20+
namespace llvm {
21+
22+
class MachineFunction;
23+
class MachineInstr;
24+
class MCSymbol;
25+
26+
/// \brief Collects and handles AsmPrinter objects required to build debug
27+
/// or EH information.
28+
class AsmPrinterHandler {
29+
public:
30+
virtual ~AsmPrinterHandler() {}
31+
32+
/// \brief For symbols that have a size designated (e.g. common symbols),
33+
/// this tracks that size.
34+
virtual void setSymbolSize(const MCSymbol *Sym, uint64_t Size) = 0;
35+
36+
/// \brief Emit all sections that should come after the content.
37+
virtual void endModule() = 0;
38+
39+
/// \brief Gather pre-function debug information.
40+
/// Every beginFunction(MF) call should be followed by an endFunction(MF) call.
41+
virtual void beginFunction(const MachineFunction *MF) = 0;
42+
43+
/// \brief Gather post-function debug information.
44+
/// Please note that some AsmPrinter implementationss may not call
45+
/// beginFunction at all.
46+
virtual void endFunction(const MachineFunction *MF) = 0;
47+
48+
/// \brief Process beginning of an instruction.
49+
virtual void beginInstruction(const MachineInstr *MI) = 0;
50+
51+
/// \brief Process end of an instruction.
52+
virtual void endInstruction() = 0;
53+
};
54+
} // End of namespace llvm
55+
56+
#endif

llvm/lib/CodeGen/AsmPrinter/DIE.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ unsigned DIEEntry::getRefAddrSize(AsmPrinter *AP) {
390390
// specified to be four bytes in the DWARF 32-bit format and eight bytes
391391
// in the DWARF 64-bit format, while DWARF Version 2 specifies that such
392392
// references have the same size as an address on the target system.
393-
if (AP->getDwarfDebug()->getDwarfVersion() == 2)
393+
const DwarfDebug *DD = AP->getDwarfDebug();
394+
assert(DD && "Expected Dwarf Debug info to be available");
395+
if (DD->getDwarfVersion() == 2)
394396
return AP->getDataLayout().getPointerSize();
395397
return sizeof(int32_t);
396398
}

llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void DwarfCFIException::beginFunction(const MachineFunction *MF) {
137137

138138
/// endFunction - Gather and emit post-function exception information.
139139
///
140-
void DwarfCFIException::endFunction() {
140+
void DwarfCFIException::endFunction(const MachineFunction *) {
141141
if (!shouldEmitPersonality && !shouldEmitMoves)
142142
return;
143143

0 commit comments

Comments
 (0)