Skip to content

Commit ebb7e40

Browse files
committed
DebugInfo: Factor out logic to update locations in MD_loop metadata, NFC
Factor out the logic needed to update debug locations contained within MD_loop metadata. This refactor is preparation for a future change that also needs to rewrite MD_loop metadata. rdar://45507940 (cherry picked from commit a2cc80b)
1 parent 511928a commit ebb7e40

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

llvm/include/llvm/IR/DebugInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef LLVM_IR_DEBUGINFO_H
1717
#define LLVM_IR_DEBUGINFO_H
1818

19+
#include "llvm/ADT/STLExtras.h"
1920
#include "llvm/ADT/SmallPtrSet.h"
2021
#include "llvm/ADT/SmallVector.h"
2122
#include "llvm/ADT/iterator_range.h"
@@ -25,6 +26,7 @@ namespace llvm {
2526

2627
class DbgDeclareInst;
2728
class DbgValueInst;
29+
class Instruction;
2830
class Module;
2931

3032
/// Find subprogram that is enclosing this scope.
@@ -50,6 +52,13 @@ bool stripDebugInfo(Function &F);
5052
/// All debug type metadata nodes are unreachable and garbage collected.
5153
bool stripNonLineTableDebugInfo(Module &M);
5254

55+
/// Update the debug locations contained within the MD_loop metadata attached
56+
/// to the instruction \p I, if one exists. \p Updater is applied to each debug
57+
/// location in the MD_loop metadata: the returned value is included in the
58+
/// updated loop metadata node if it is non-null.
59+
void updateLoopMetadataDebugLocations(
60+
Instruction &I, function_ref<DILocation *(const DILocation &)> Updater);
61+
5362
/// Return Debug Info Metadata Version by checking module flags.
5463
unsigned getDebugMetadataVersionFromModule(const Module &M);
5564

llvm/lib/IR/DebugInfo.cpp

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,41 @@ bool DebugInfoFinder::addScope(DIScope *Scope) {
278278
return true;
279279
}
280280

281+
static MDNode *updateLoopMetadataDebugLocationsImpl(
282+
MDNode *OrigLoopID,
283+
function_ref<DILocation *(const DILocation &)> Updater) {
284+
assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 &&
285+
"Loop ID needs at least one operand");
286+
assert(OrigLoopID && OrigLoopID->getOperand(0).get() == OrigLoopID &&
287+
"Loop ID should refer to itself");
288+
289+
// Save space for the self-referential LoopID.
290+
SmallVector<Metadata *, 4> MDs = {nullptr};
291+
292+
for (unsigned i = 1; i < OrigLoopID->getNumOperands(); ++i) {
293+
Metadata *MD = OrigLoopID->getOperand(i);
294+
if (DILocation *DL = dyn_cast<DILocation>(MD)) {
295+
if (DILocation *NewDL = Updater(*DL))
296+
MDs.push_back(NewDL);
297+
} else
298+
MDs.push_back(MD);
299+
}
300+
301+
MDNode *NewLoopID = MDNode::getDistinct(OrigLoopID->getContext(), MDs);
302+
// Insert the self-referential LoopID.
303+
NewLoopID->replaceOperandWith(0, NewLoopID);
304+
return NewLoopID;
305+
}
306+
307+
void llvm::updateLoopMetadataDebugLocations(
308+
Instruction &I, function_ref<DILocation *(const DILocation &)> Updater) {
309+
MDNode *OrigLoopID = I.getMetadata(LLVMContext::MD_loop);
310+
if (!OrigLoopID)
311+
return;
312+
MDNode *NewLoopID = updateLoopMetadataDebugLocationsImpl(OrigLoopID, Updater);
313+
I.setMetadata(LLVMContext::MD_loop, NewLoopID);
314+
}
315+
281316
static MDNode *stripDebugLocFromLoopID(MDNode *N) {
282317
assert(!N->operands().empty() && "Missing self reference?");
283318

@@ -294,20 +329,10 @@ static MDNode *stripDebugLocFromLoopID(MDNode *N) {
294329
}))
295330
return nullptr;
296331

297-
SmallVector<Metadata *, 4> Args;
298-
// Reserve operand 0 for loop id self reference.
299-
auto TempNode = MDNode::getTemporary(N->getContext(), None);
300-
Args.push_back(TempNode.get());
301-
// Add all non-debug location operands back.
302-
for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
303-
if (!isa<DILocation>(*Op))
304-
Args.push_back(*Op);
305-
}
306-
307-
// Set the first operand to itself.
308-
MDNode *LoopID = MDNode::get(N->getContext(), Args);
309-
LoopID->replaceOperandWith(0, LoopID);
310-
return LoopID;
332+
auto dropDebugLoc = [](const DILocation &) -> DILocation * {
333+
return nullptr;
334+
};
335+
return updateLoopMetadataDebugLocationsImpl(N, dropDebugLoc);
311336
}
312337

313338
bool llvm::stripDebugInfo(Function &F) {

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,34 +1356,6 @@ static DebugLoc inlineDebugLoc(DebugLoc OrigDL, DILocation *InlinedAt,
13561356
IA);
13571357
}
13581358

1359-
/// Returns the LoopID for a loop which has has been cloned from another
1360-
/// function for inlining with the new inlined-at start and end locs.
1361-
static MDNode *inlineLoopID(const MDNode *OrigLoopId, DILocation *InlinedAt,
1362-
LLVMContext &Ctx,
1363-
DenseMap<const MDNode *, MDNode *> &IANodes) {
1364-
assert(OrigLoopId && OrigLoopId->getNumOperands() > 0 &&
1365-
"Loop ID needs at least one operand");
1366-
assert(OrigLoopId && OrigLoopId->getOperand(0).get() == OrigLoopId &&
1367-
"Loop ID should refer to itself");
1368-
1369-
// Save space for the self-referential LoopID.
1370-
SmallVector<Metadata *, 4> MDs = {nullptr};
1371-
1372-
for (unsigned i = 1; i < OrigLoopId->getNumOperands(); ++i) {
1373-
Metadata *MD = OrigLoopId->getOperand(i);
1374-
// Update the DILocations to encode the inlined-at metadata.
1375-
if (DILocation *DL = dyn_cast<DILocation>(MD))
1376-
MDs.push_back(inlineDebugLoc(DL, InlinedAt, Ctx, IANodes));
1377-
else
1378-
MDs.push_back(MD);
1379-
}
1380-
1381-
MDNode *NewLoopID = MDNode::getDistinct(Ctx, MDs);
1382-
// Insert the self-referential LoopID.
1383-
NewLoopID->replaceOperandWith(0, NewLoopID);
1384-
return NewLoopID;
1385-
}
1386-
13871359
/// Update inlined instructions' line numbers to
13881360
/// to encode location where these instructions are inlined.
13891361
static void fixupLineNumbers(Function *Fn, Function::iterator FI,
@@ -1415,11 +1387,11 @@ static void fixupLineNumbers(Function *Fn, Function::iterator FI,
14151387
BI != BE; ++BI) {
14161388
// Loop metadata needs to be updated so that the start and end locs
14171389
// reference inlined-at locations.
1418-
if (MDNode *LoopID = BI->getMetadata(LLVMContext::MD_loop)) {
1419-
MDNode *NewLoopID =
1420-
inlineLoopID(LoopID, InlinedAtNode, BI->getContext(), IANodes);
1421-
BI->setMetadata(LLVMContext::MD_loop, NewLoopID);
1422-
}
1390+
auto updateLoopInfoLoc = [&Ctx, &InlinedAtNode, &IANodes](
1391+
const DILocation &Loc) -> DILocation * {
1392+
return inlineDebugLoc(&Loc, InlinedAtNode, Ctx, IANodes).get();
1393+
};
1394+
updateLoopMetadataDebugLocations(*BI, updateLoopInfoLoc);
14231395

14241396
if (!NoInlineLineTables)
14251397
if (DebugLoc DL = BI->getDebugLoc()) {

0 commit comments

Comments
 (0)