Skip to content

Commit b85b4e5

Browse files
committed
Add an instruction marker field to the ExtraInfo in MachineInstrs.
Summary: Add instruction marker to MachineInstr ExtraInfo. This does almost the same thing as Pre/PostInstrSymbols, except that it doesn't create a label until printing instructions. This allows for labels to be put around instructions that are deleted/duplicated somewhere. Also undo the workaround in r375137. Reviewers: rnk Subscribers: MatzeB, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D69136
1 parent abd89c2 commit b85b4e5

File tree

12 files changed

+347
-173
lines changed

12 files changed

+347
-173
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,6 @@ class MachineFunction {
322322
/// CodeView label annotations.
323323
std::vector<std::pair<MCSymbol *, MDNode *>> CodeViewAnnotations;
324324

325-
/// CodeView heapallocsites.
326-
std::vector<std::tuple<MCSymbol *, MCSymbol *, const DIType *>>
327-
CodeViewHeapAllocSites;
328-
329325
bool CallsEHReturn = false;
330326
bool CallsUnwindInit = false;
331327
bool HasEHScopes = false;
@@ -800,10 +796,9 @@ class MachineFunction {
800796
///
801797
/// This is allocated on the function's allocator and so lives the life of
802798
/// the function.
803-
MachineInstr::ExtraInfo *
804-
createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
805-
MCSymbol *PreInstrSymbol = nullptr,
806-
MCSymbol *PostInstrSymbol = nullptr);
799+
MachineInstr::ExtraInfo *createMIExtraInfo(
800+
ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol = nullptr,
801+
MCSymbol *PostInstrSymbol = nullptr, MDNode *HeapAllocMarker = nullptr);
807802

808803
/// Allocate a string and populate it with the given external symbol name.
809804
const char *createExternalSymbolName(StringRef Name);
@@ -947,14 +942,6 @@ class MachineFunction {
947942
return CodeViewAnnotations;
948943
}
949944

950-
/// Record heapallocsites
951-
void addCodeViewHeapAllocSite(MachineInstr *I, const MDNode *MD);
952-
953-
ArrayRef<std::tuple<MCSymbol *, MCSymbol *, const DIType *>>
954-
getCodeViewHeapAllocSites() const {
955-
return CodeViewHeapAllocSites;
956-
}
957-
958945
/// Return a reference to the C++ typeinfo for the current function.
959946
const std::vector<const GlobalValue *> &getTypeInfos() const {
960947
return TypeInfos;

llvm/include/llvm/CodeGen/MachineInstr.h

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,23 @@ class MachineInstr
136136
/// This has to be defined eagerly due to the implementation constraints of
137137
/// `PointerSumType` where it is used.
138138
class ExtraInfo final
139-
: TrailingObjects<ExtraInfo, MachineMemOperand *, MCSymbol *> {
139+
: TrailingObjects<ExtraInfo, MachineMemOperand *, MCSymbol *, MDNode *> {
140140
public:
141141
static ExtraInfo *create(BumpPtrAllocator &Allocator,
142142
ArrayRef<MachineMemOperand *> MMOs,
143143
MCSymbol *PreInstrSymbol = nullptr,
144-
MCSymbol *PostInstrSymbol = nullptr) {
144+
MCSymbol *PostInstrSymbol = nullptr,
145+
MDNode *HeapAllocMarker = nullptr) {
145146
bool HasPreInstrSymbol = PreInstrSymbol != nullptr;
146147
bool HasPostInstrSymbol = PostInstrSymbol != nullptr;
148+
bool HasHeapAllocMarker = HeapAllocMarker != nullptr;
147149
auto *Result = new (Allocator.Allocate(
148-
totalSizeToAlloc<MachineMemOperand *, MCSymbol *>(
149-
MMOs.size(), HasPreInstrSymbol + HasPostInstrSymbol),
150+
totalSizeToAlloc<MachineMemOperand *, MCSymbol *, MDNode *>(
151+
MMOs.size(), HasPreInstrSymbol + HasPostInstrSymbol,
152+
HasHeapAllocMarker),
150153
alignof(ExtraInfo)))
151-
ExtraInfo(MMOs.size(), HasPreInstrSymbol, HasPostInstrSymbol);
154+
ExtraInfo(MMOs.size(), HasPreInstrSymbol, HasPostInstrSymbol,
155+
HasHeapAllocMarker);
152156

153157
// Copy the actual data into the trailing objects.
154158
std::copy(MMOs.begin(), MMOs.end(),
@@ -159,6 +163,10 @@ class MachineInstr
159163
if (HasPostInstrSymbol)
160164
Result->getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol] =
161165
PostInstrSymbol;
166+
if (HasHeapAllocMarker)
167+
Result->getTrailingObjects<MDNode *>()[HasPreInstrSymbol +
168+
HasPostInstrSymbol] =
169+
HeapAllocMarker;
162170

163171
return Result;
164172
}
@@ -177,6 +185,13 @@ class MachineInstr
177185
: nullptr;
178186
}
179187

188+
MDNode *getHeapAllocMarker() const {
189+
return HasHeapAllocMarker
190+
? getTrailingObjects<MDNode *>()[HasPreInstrSymbol +
191+
HasPostInstrSymbol]
192+
: nullptr;
193+
}
194+
180195
private:
181196
friend TrailingObjects;
182197

@@ -188,6 +203,7 @@ class MachineInstr
188203
const int NumMMOs;
189204
const bool HasPreInstrSymbol;
190205
const bool HasPostInstrSymbol;
206+
const bool HasHeapAllocMarker;
191207

192208
// Implement the `TrailingObjects` internal API.
193209
size_t numTrailingObjects(OverloadToken<MachineMemOperand *>) const {
@@ -196,12 +212,17 @@ class MachineInstr
196212
size_t numTrailingObjects(OverloadToken<MCSymbol *>) const {
197213
return HasPreInstrSymbol + HasPostInstrSymbol;
198214
}
215+
size_t numTrailingObjects(OverloadToken<MDNode *>) const {
216+
return HasHeapAllocMarker;
217+
}
199218

200219
// Just a boring constructor to allow us to initialize the sizes. Always use
201220
// the `create` routine above.
202-
ExtraInfo(int NumMMOs, bool HasPreInstrSymbol, bool HasPostInstrSymbol)
221+
ExtraInfo(int NumMMOs, bool HasPreInstrSymbol, bool HasPostInstrSymbol,
222+
bool HasHeapAllocMarker)
203223
: NumMMOs(NumMMOs), HasPreInstrSymbol(HasPreInstrSymbol),
204-
HasPostInstrSymbol(HasPostInstrSymbol) {}
224+
HasPostInstrSymbol(HasPostInstrSymbol),
225+
HasHeapAllocMarker(HasHeapAllocMarker) {}
205226
};
206227

207228
/// Enumeration of the kinds of inline extra info available. It is important
@@ -211,7 +232,8 @@ class MachineInstr
211232
EIIK_MMO = 0,
212233
EIIK_PreInstrSymbol,
213234
EIIK_PostInstrSymbol,
214-
EIIK_OutOfLine
235+
EIIK_HeapAllocMarker,
236+
EIIK_OutOfLine,
215237
};
216238

217239
// We store extra information about the instruction here. The common case is
@@ -223,6 +245,7 @@ class MachineInstr
223245
PointerSumTypeMember<EIIK_MMO, MachineMemOperand *>,
224246
PointerSumTypeMember<EIIK_PreInstrSymbol, MCSymbol *>,
225247
PointerSumTypeMember<EIIK_PostInstrSymbol, MCSymbol *>,
248+
PointerSumTypeMember<EIIK_HeapAllocMarker, MDNode *>,
226249
PointerSumTypeMember<EIIK_OutOfLine, ExtraInfo *>>
227250
Info;
228251

@@ -592,6 +615,17 @@ class MachineInstr
592615
return nullptr;
593616
}
594617

618+
MDNode *getHeapAllocMarker() const {
619+
if (!Info)
620+
return nullptr;
621+
if (MDNode *S = Info.get<EIIK_HeapAllocMarker>())
622+
return S;
623+
if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>())
624+
return EI->getHeapAllocMarker();
625+
626+
return nullptr;
627+
}
628+
595629
/// API for querying MachineInstr properties. They are the same as MCInstrDesc
596630
/// queries but they are bundle aware.
597631

@@ -1597,6 +1631,12 @@ class MachineInstr
15971631
/// replace ours with it.
15981632
void cloneInstrSymbols(MachineFunction &MF, const MachineInstr &MI);
15991633

1634+
/// Set a marker on instructions that denotes where we should create and emit
1635+
/// heap alloc site labels. This waits until after instruction selection and
1636+
/// optimizations to create the label, so it should still work if the
1637+
/// instruction is removed or duplicated.
1638+
void setHeapAllocMarker(MachineFunction &MF, MDNode *DI);
1639+
16001640
/// Return the MIFlags which represent both MachineInstrs. This
16011641
/// should be used when merging two MachineInstrs into one. This routine does
16021642
/// not modify the MIFlags of this MachineInstr.
@@ -1657,6 +1697,12 @@ class MachineInstr
16571697
const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl(
16581698
unsigned OpIdx, Register Reg, const TargetRegisterClass *CurRC,
16591699
const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const;
1700+
1701+
/// Stores extra instruction information inline or allocates as ExtraInfo
1702+
/// based on the number of pointers.
1703+
void setExtraInfo(MachineFunction &MF, ArrayRef<MachineMemOperand *> MMOs,
1704+
MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol,
1705+
MDNode *HeapAllocMarker);
16601706
};
16611707

16621708
/// Special DenseMapInfo traits to compare MachineInstr* by *value* of the

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,13 +1065,9 @@ void AsmPrinter::EmitFunctionBody() {
10651065
++NumInstsInFunction;
10661066
}
10671067

1068-
// If there is a pre-instruction symbol, emit a label for it here. If the
1069-
// instruction was duplicated and the label has already been emitted,
1070-
// don't re-emit the same label.
1071-
// FIXME: Consider strengthening that to an assertion.
1068+
// If there is a pre-instruction symbol, emit a label for it here.
10721069
if (MCSymbol *S = MI.getPreInstrSymbol())
1073-
if (S->isUndefined())
1074-
OutStreamer->EmitLabel(S);
1070+
OutStreamer->EmitLabel(S);
10751071

10761072
if (ShouldPrintDebugScopes) {
10771073
for (const HandlerInfo &HI : Handlers) {
@@ -1124,13 +1120,9 @@ void AsmPrinter::EmitFunctionBody() {
11241120
break;
11251121
}
11261122

1127-
// If there is a post-instruction symbol, emit a label for it here. If
1128-
// the instruction was duplicated and the label has already been emitted,
1129-
// don't re-emit the same label.
1130-
// FIXME: Consider strengthening that to an assertion.
1123+
// If there is a post-instruction symbol, emit a label for it here.
11311124
if (MCSymbol *S = MI.getPostInstrSymbol())
1132-
if (S->isUndefined())
1133-
OutStreamer->EmitLabel(S);
1125+
OutStreamer->EmitLabel(S);
11341126

11351127
if (ShouldPrintDebugScopes) {
11361128
for (const HandlerInfo &HI : Handlers) {

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,14 +1100,8 @@ void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
11001100
}
11011101

11021102
for (auto HeapAllocSite : FI.HeapAllocSites) {
1103-
MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);
1104-
MCSymbol *EndLabel = std::get<1>(HeapAllocSite);
1105-
1106-
// The labels might not be defined if the instruction was replaced
1107-
// somewhere in the codegen pipeline.
1108-
if (!BeginLabel->isDefined() || !EndLabel->isDefined())
1109-
continue;
1110-
1103+
const MCSymbol *BeginLabel = std::get<0>(HeapAllocSite);
1104+
const MCSymbol *EndLabel = std::get<1>(HeapAllocSite);
11111105
const DIType *DITy = std::get<2>(HeapAllocSite);
11121106
MCSymbol *HeapAllocEnd = beginSymbolRecord(SymbolKind::S_HEAPALLOCSITE);
11131107
OS.AddComment("Call site offset");
@@ -1427,6 +1421,16 @@ void CodeViewDebug::beginFunctionImpl(const MachineFunction *MF) {
14271421
DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
14281422
maybeRecordLocation(FnStartDL, MF);
14291423
}
1424+
1425+
// Find heap alloc sites and emit labels around them.
1426+
for (const auto &MBB : *MF) {
1427+
for (const auto &MI : MBB) {
1428+
if (MI.getHeapAllocMarker()) {
1429+
requestLabelBeforeInsn(&MI);
1430+
requestLabelAfterInsn(&MI);
1431+
}
1432+
}
1433+
}
14301434
}
14311435

14321436
static bool shouldEmitUdt(const DIType *T) {
@@ -2850,8 +2854,18 @@ void CodeViewDebug::endFunctionImpl(const MachineFunction *MF) {
28502854
return;
28512855
}
28522856

2857+
// Find heap alloc sites and add to list.
2858+
for (const auto &MBB : *MF) {
2859+
for (const auto &MI : MBB) {
2860+
if (MDNode *MD = MI.getHeapAllocMarker()) {
2861+
CurFn->HeapAllocSites.push_back(std::make_tuple(getLabelBeforeInsn(&MI),
2862+
getLabelAfterInsn(&MI),
2863+
dyn_cast<DIType>(MD)));
2864+
}
2865+
}
2866+
}
2867+
28532868
CurFn->Annotations = MF->getCodeViewAnnotations();
2854-
CurFn->HeapAllocSites = MF->getCodeViewHeapAllocSites();
28552869

28562870
CurFn->End = Asm->getFunctionEnd();
28572871

llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class LLVM_LIBRARY_VISIBILITY CodeViewDebug : public DebugHandlerBase {
148148
SmallVector<LexicalBlock *, 1> ChildBlocks;
149149

150150
std::vector<std::pair<MCSymbol *, MDNode *>> Annotations;
151-
std::vector<std::tuple<MCSymbol *, MCSymbol *, const DIType *>>
151+
std::vector<std::tuple<const MCSymbol *, const MCSymbol *, const DIType *>>
152152
HeapAllocSites;
153153

154154
const MCSymbol *Begin = nullptr;

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,11 @@ MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
447447
MMO->getOrdering(), MMO->getFailureOrdering());
448448
}
449449

450-
MachineInstr::ExtraInfo *
451-
MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
452-
MCSymbol *PreInstrSymbol,
453-
MCSymbol *PostInstrSymbol) {
450+
MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo(
451+
ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol,
452+
MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) {
454453
return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
455-
PostInstrSymbol);
454+
PostInstrSymbol, HeapAllocMarker);
456455
}
457456

458457
const char *MachineFunction::createExternalSymbolName(StringRef Name) {
@@ -824,17 +823,6 @@ try_next:;
824823
return FilterID;
825824
}
826825

827-
void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I,
828-
const MDNode *MD) {
829-
MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true);
830-
MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true);
831-
I->setPreInstrSymbol(*this, BeginLabel);
832-
I->setPostInstrSymbol(*this, EndLabel);
833-
834-
const DIType *DI = dyn_cast<DIType>(MD);
835-
CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI));
836-
}
837-
838826
void MachineFunction::moveCallSiteInfo(const MachineInstr *Old,
839827
const MachineInstr *New) {
840828
assert(New->isCall() && "Call site info refers only to call instructions!");

0 commit comments

Comments
 (0)