[llvm] [llvm-dwarfdump][LineCov 3/3] Add IR analysis for variable coverage (PR #195342)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 1 04:43:15 PDT 2026
================
@@ -195,6 +215,124 @@ static const SmallVector<DWARFDie> getParentSubroutines(DWARFDie DIE) {
return Parents;
}
+static bool isInScope(MDNode *Scope, const DebugLoc &Loc) {
+ MDNode *Parent = Loc.getScope();
+ while (Parent != Scope) {
+ auto *S = dyn_cast_if_present<DIScope>(Parent);
+ if (!S)
+ return false;
+ Parent = S->getScope();
+ }
+ return true;
+}
+
+static bool isLoop(BasicBlock *Origin, BasicBlock *BB,
+ SmallPtrSet<BasicBlock *, 8> &Visited) {
+ for (auto *P : predecessors(BB)) {
+ if (P == Origin)
+ return true;
+ if (!Visited.count(P)) {
+ Visited.insert(P);
+ if (isLoop(Origin, P, Visited))
+ return true;
+ }
+ }
+ return false;
+}
+
+struct VarState {
+ DbgVariableRecord &DVR;
+ DenseSet<std::pair<StringRef, uint32_t>> Lines;
+ SmallPtrSet<BasicBlock *, 8> LiveOut;
+};
+
+/// Given an instruction that stores to a variable and its basic block,
+/// recursively searches its successor instructions/basic blocks and adds lines
+/// where the variable has a defined value to the variable's line set.
+static void getSuccessorLines(VarState &Var, BasicBlock *BB, Instruction *I) {
+ // Process the basic block if it contains the store instruction or the
+ // variable is defined in all of its predecessors, excluding any that are part
+ // of a loop that contains the current block.
+ bool ShouldProcess = I;
+ if (!ShouldProcess) {
+ ShouldProcess = true;
+ for (auto *P : predecessors(BB)) {
+ SmallPtrSet<BasicBlock *, 8> Visited;
+ if (!Var.LiveOut.count(P) && !isLoop(BB, P, Visited)) {
----------------
unexpectedlydefined wrote:
The (new) approach I've taken here is to recursively visit all basic blocks that don't define `Var`, starting from the entry block; if we encounter a basic block that defines `Var` (marked ahead-of-time), we add all lines after the definition to the line set and continue without visiting its successors. Once we're done, any blocks we haven't visited must only be reachable from the entry block by passing through a block that defines `Var`, so we can include all instructions in those blocks in the line set.
https://github.com/llvm/llvm-project/pull/195342
More information about the llvm-commits
mailing list