Skip to content

Commit

Permalink
[NFC] Change findDbg<Ty> interface
Browse files Browse the repository at this point in the history
Use return values rather than out-parameters, and rename FindDbgDeclareUses to findDbgDeclares. See llvm#73498.
  • Loading branch information
OCHyams committed Dec 11, 2023
1 parent 07056c2 commit 991fd09
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 135 deletions.
10 changes: 5 additions & 5 deletions llvm/include/llvm/IR/DebugInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class Module;

/// Finds dbg.declare intrinsics declaring local variables as living in the
/// memory that 'V' points to.
TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V);
TinyPtrVector<DbgDeclareInst *> findDbgDeclares(Value *V);

/// Finds the llvm.dbg.value intrinsics describing a value.
void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues,
Value *V, SmallVectorImpl<DPValue *> *DPValues = nullptr);
SmallVector<DbgValueInst *> findDbgValues(Value *V);
SmallVector<DPValue *> findDPValues(Value *V);

/// Finds the debug info intrinsics describing a value.
void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts,
Value *V, SmallVectorImpl<DPValue *> *DPValues = nullptr);
SmallVector<DbgVariableIntrinsic *> findDbgUsers(Value *V);
SmallVector<DPValue *> findDPVUsers(Value *V);

/// Find subprogram that is enclosing this scope.
DISubprogram *getDISubprogram(const MDNode *Scope);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3098,7 +3098,8 @@ class TypePromotionTransaction {
}
// Record the debug uses separately. They are not in the instruction's
// use list, but they are replaced by RAUW.
findDbgValues(DbgValues, Inst, &DPValues);
DbgValues = findDbgValues(Inst);
DPValues = findDPValues(Inst);

// Now, we can replace the uses.
Inst->replaceAllUsesWith(New);
Expand Down
65 changes: 38 additions & 27 deletions llvm/lib/IR/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using namespace llvm;
using namespace llvm::at;
using namespace llvm::dwarf;

TinyPtrVector<DbgDeclareInst *> llvm::FindDbgDeclareUses(Value *V) {
TinyPtrVector<DbgDeclareInst *> llvm::findDbgDeclares(Value *V) {
// This function is hot. Check whether the value has any metadata to avoid a
// DenseMap lookup.
if (!V->isUsedByMetadata())
Expand All @@ -66,12 +66,12 @@ TinyPtrVector<DbgDeclareInst *> llvm::FindDbgDeclareUses(Value *V) {
}

template <typename IntrinsicT>
static void findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result,
Value *V, SmallVectorImpl<DPValue *> *DPValues) {
static SmallVector<IntrinsicT *> findDbgIntrinsics(Value *V) {
SmallVector<IntrinsicT *> Result;
// This function is hot. Check whether the value has any metadata to avoid a
// DenseMap lookup.
if (!V->isUsedByMetadata())
return;
return Result;

LLVMContext &Ctx = V->getContext();
// TODO: If this value appears multiple times in a DIArgList, we should still
Expand All @@ -80,51 +80,62 @@ static void findDbgIntrinsics(SmallVectorImpl<IntrinsicT *> &Result,
// V will also appear twice in a dbg.assign if its used in the both the value
// and address components.
SmallPtrSet<IntrinsicT *, 4> EncounteredIntrinsics;
SmallPtrSet<DPValue *, 4> EncounteredDPValues;

/// Append IntrinsicT users of MetadataAsValue(MD).
auto AppendUsers = [&Ctx, &EncounteredIntrinsics, &Result,
DPValues](Metadata *MD) {
auto AppendUsers = [&Ctx, &EncounteredIntrinsics, &Result](Metadata *MD) {
if (auto *MDV = MetadataAsValue::getIfExists(Ctx, MD)) {
for (User *U : MDV->users())
if (IntrinsicT *DVI = dyn_cast<IntrinsicT>(U))
if (EncounteredIntrinsics.insert(DVI).second)
Result.push_back(DVI);
}
if (!DPValues)
return;
// Get DPValues that use this as a single value.
if (LocalAsMetadata *L = dyn_cast<LocalAsMetadata>(MD)) {
for (DPValue *DPV : L->getAllDPValueUsers()) {
if (DPV->getType() == DPValue::LocationType::Value)
DPValues->push_back(DPV);
}
}
};

if (auto *L = LocalAsMetadata::getIfExists(V)) {
AppendUsers(L);
for (Metadata *AL : L->getAllArgListUsers()) {
AppendUsers(AL);
if (!DPValues)
continue;
DIArgList *DI = cast<DIArgList>(AL);
for (DPValue *DPV : DI->getAllDPValueUsers())
}
}
return Result;
}

static SmallVector<DPValue *> findDbgRecords(Value *V) {
SmallVector<DPValue *> Result;
// This function is hot. Check whether the value has any metadata to avoid a
// DenseMap lookup.
if (!V->isUsedByMetadata())
return Result;

// TODO: If this value appears multiple times in a DIArgList, we should still
// only add the owning DbgValueInst once; use this set to track ArgListUsers.
// This behaviour can be removed when we can automatically remove duplicates.
// V will also appear twice in a dbg.assign if its used in the both the value
// and address components.
SmallPtrSet<DPValue *, 4> EncounteredDPValues;

if (auto *L = LocalAsMetadata::getIfExists(V)) {
for (DPValue *DPV : L->getAllDPValueUsers()) {
if (DPV->getType() == DPValue::LocationType::Value)
Result.push_back(DPV);
}

for (Metadata *AL : L->getAllArgListUsers()) {
for (DPValue *DPV : cast<DIArgList>(AL)->getAllDPValueUsers())
if (DPV->getType() == DPValue::LocationType::Value)
if (EncounteredDPValues.insert(DPV).second)
DPValues->push_back(DPV);
Result.push_back(DPV);
}
}
return Result;
}

void llvm::findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues,
Value *V, SmallVectorImpl<DPValue *> *DPValues) {
findDbgIntrinsics<DbgValueInst>(DbgValues, V, DPValues);
SmallVector<DbgValueInst *> llvm::findDbgValues(Value *V) {
return findDbgIntrinsics<DbgValueInst>(V);
}

void llvm::findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers,
Value *V, SmallVectorImpl<DPValue *> *DPValues) {
findDbgIntrinsics<DbgVariableIntrinsic>(DbgUsers, V, DPValues);
SmallVector<DbgVariableIntrinsic *> llvm::findDbgUsers(Value *V) {
return findDbgIntrinsics<DbgVariableIntrinsic>(V);
}

DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,14 +573,11 @@ void Value::replaceUsesWithIf(Value *New,
/// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
/// with New.
static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {
SmallVector<DbgVariableIntrinsic *> DbgUsers;
SmallVector<DPValue *> DPUsers;
findDbgUsers(DbgUsers, V, &DPUsers);
for (auto *DVI : DbgUsers) {
for (auto *DVI : findDbgUsers(V)) {
if (DVI->getParent() != BB)
DVI->replaceVariableLocationOp(V, New);
}
for (auto *DPV : DPUsers) {
for (DPValue *DPV : findDPVUsers(V)) {
DPMarker *Marker = DPV->getMarker();
if (Marker->getParent() != BB)
DPV->replaceVariableLocationOp(V, New);
Expand Down
16 changes: 6 additions & 10 deletions llvm/lib/Transforms/Coroutines/CoroFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ static void cacheDIVar(FrameDataInfo &FrameData,
if (DIVarCache.contains(V))
continue;

auto DDIs = FindDbgDeclareUses(V);
auto DDIs = findDbgDeclares(V);
auto *I = llvm::find_if(DDIs, [](DbgDeclareInst *DDI) {
return DDI->getExpression()->getNumElements() == 0;
});
Expand Down Expand Up @@ -1119,7 +1119,7 @@ static void buildFrameDebugInfo(Function &F, coro::Shape &Shape,
assert(PromiseAlloca &&
"Coroutine with switch ABI should own Promise alloca");

TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(PromiseAlloca);
TinyPtrVector<DbgDeclareInst *> DIs = findDbgDeclares(PromiseAlloca);
if (DIs.empty())
return;

Expand Down Expand Up @@ -1840,7 +1840,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
FrameTy->getElementType(FrameData.getFieldIndex(E.first)), GEP,
SpillAlignment, E.first->getName() + Twine(".reload"));

TinyPtrVector<DbgDeclareInst *> DIs = FindDbgDeclareUses(Def);
TinyPtrVector<DbgDeclareInst *> DIs = findDbgDeclares(Def);
// Try best to find dbg.declare. If the spill is a temp, there may not
// be a direct dbg.declare. Walk up the load chain to find one from an
// alias.
Expand All @@ -1854,7 +1854,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
CurDef = LdInst->getPointerOperand();
if (!isa<AllocaInst, LoadInst>(CurDef))
break;
DIs = FindDbgDeclareUses(CurDef);
DIs = findDbgDeclares(CurDef);
}
}

Expand Down Expand Up @@ -1938,9 +1938,7 @@ static void insertSpills(const FrameDataInfo &FrameData, coro::Shape &Shape) {
auto *G = GetFramePointer(Alloca);
G->setName(Alloca->getName() + Twine(".reload.addr"));

SmallVector<DbgVariableIntrinsic *, 4> DIs;
findDbgUsers(DIs, Alloca);
for (auto *DVI : DIs)
for (auto *DVI : findDbgUsers(Alloca))
DVI->replaceUsesOfWith(Alloca, G);

for (Instruction *I : UsersToUpdate) {
Expand Down Expand Up @@ -3082,9 +3080,7 @@ void coro::buildCoroutineFrame(
// We would handle the dbg.values for allocas specially
for (auto &Iter : FrameData.Spills) {
auto *V = Iter.first;
SmallVector<DbgValueInst *, 16> DVIs;
findDbgValues(DVIs, V);
for (DbgValueInst *DVI : DVIs)
for (DbgValueInst *DVI : findDbgValues(V))
if (Checker.isDefinitionAcrossSuspend(*V, DVI))
FrameData.Spills[V].push_back(DVI);
}
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2679,7 +2679,8 @@ Instruction *InstCombinerImpl::visitAllocSite(Instruction &MI) {
SmallVector<DPValue *, 8> DPVs;
std::unique_ptr<DIBuilder> DIB;
if (isa<AllocaInst>(MI)) {
findDbgUsers(DVIs, &MI, &DPVs);
DVIs = findDbgUsers(&MI);
DPVs = findDPVUsers(&MI);
DIB.reset(new DIBuilder(*MI.getModule(), /*AllowUnresolved=*/false));
}

Expand Down Expand Up @@ -4076,8 +4077,7 @@ bool InstCombinerImpl::tryToSinkInstruction(Instruction *I,
// maximise the range variables have location for. If we cannot salvage, then
// mark the location undef: we know it was supposed to receive a new location
// here, but that computation has been sunk.
SmallVector<DbgVariableIntrinsic *, 2> DbgUsers;
findDbgUsers(DbgUsers, I);
SmallVector<DbgVariableIntrinsic *> DbgUsers = findDbgUsers(I);

// For all debug values in the destination block, the sunk instruction
// will still be available, so they do not need to be dropped.
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/JumpThreading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1972,10 +1972,11 @@ void JumpThreadingPass::updateSSA(
}

// Find debug values outside of the block
findDbgValues(DbgValues, &I, &DPValues);
DbgValues = findDbgValues(&I);
llvm::erase_if(DbgValues, [&](const DbgValueInst *DbgVal) {
return DbgVal->getParent() == BB;
});
DPValues = findDPValues(&I);
llvm::erase_if(DPValues, [&](const DPValue *DPVal) {
return DPVal->getParent() == BB;
});
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4940,7 +4940,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
// Migrate debug information from the old alloca to the new alloca(s)
// and the individual partitions.
TinyPtrVector<DbgVariableIntrinsic *> DbgVariables;
for (auto *DbgDeclare : FindDbgDeclareUses(&AI))
for (auto *DbgDeclare : findDbgDeclares(&AI))
DbgVariables.push_back(DbgDeclare);
for (auto *DbgAssign : at::getAssignmentMarkers(&AI))
DbgVariables.push_back(DbgAssign);
Expand Down Expand Up @@ -4997,7 +4997,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {

// Remove any existing intrinsics on the new alloca describing
// the variable fragment.
for (DbgDeclareInst *OldDII : FindDbgDeclareUses(Fragment.Alloca)) {
for (DbgDeclareInst *OldDII : findDbgDeclares(Fragment.Alloca)) {
auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS,
const DbgVariableIntrinsic *RHS) {
return LHS->getVariable() == RHS->getVariable() &&
Expand Down Expand Up @@ -5147,7 +5147,7 @@ bool SROA::deleteDeadInstructions(
// not be able to find it.
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
DeletedAllocas.insert(AI);
for (DbgDeclareInst *OldDII : FindDbgDeclareUses(AI))
for (DbgDeclareInst *OldDII : findDbgDeclares(AI))
OldDII->eraseFromParent();
}

Expand Down
7 changes: 2 additions & 5 deletions llvm/lib/Transforms/Utils/CodeExtractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1506,13 +1506,10 @@ void CodeExtractor::calculateNewCallTerminatorWeights(
/// \p F.
static void eraseDebugIntrinsicsWithNonLocalRefs(Function &F) {
for (Instruction &I : instructions(F)) {
SmallVector<DbgVariableIntrinsic *, 4> DbgUsers;
SmallVector<DPValue *, 4> DPValues;
findDbgUsers(DbgUsers, &I, &DPValues);
for (DbgVariableIntrinsic *DVI : DbgUsers)
for (DbgVariableIntrinsic *DVI : findDbgUsers(&I))
if (DVI->getFunction() != &F)
DVI->eraseFromParent();
for (DPValue *DPV : DPValues)
for (DPValue *DPV : findDPVUsers(&I))
if (DPV->getFunction() != &F)
DPV->eraseFromParent();
}
Expand Down
8 changes: 2 additions & 6 deletions llvm/lib/Transforms/Utils/LCSSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,8 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
SSAUpdate.RewriteUse(*UseToRewrite);
}

SmallVector<DbgValueInst *, 4> DbgValues;
SmallVector<DPValue *, 4> DPValues;
llvm::findDbgValues(DbgValues, I, &DPValues);

// Update pre-existing debug value uses that reside outside the loop.
for (auto *DVI : DbgValues) {
for (auto *DVI : llvm::findDbgValues(I)) {
BasicBlock *UserBB = DVI->getParent();
if (InstBB == UserBB || L->contains(UserBB))
continue;
Expand All @@ -261,7 +257,7 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,

// RemoveDIs: copy-paste of block above, using non-instruction debug-info
// records.
for (DPValue *DPV : DPValues) {
for (DPValue *DPV : llvm::findDPValues(I)) {
BasicBlock *UserBB = DPV->getMarker()->getParent();
if (InstBB == UserBB || L->contains(UserBB))
continue;
Expand Down
Loading

0 comments on commit 991fd09

Please sign in to comment.