[PATCH] D83896: [DebugInfo] Add replaceArg function for handling DBG_VALUE_LIST expressions

Stephen Tozer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 15 11:31:16 PDT 2020


StephenTozer created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

This is a very simple patch that is not needed for the basic addition of the DBG_VALUE_LIST instruction, but is needed for both the LiveDebugValues and LiveDebugVariables patches that follow it.

The purpose of the `replaceArg` function is to modify an expression containing `DW_OP_LLVM_arg` when one of the args is a duplicate of another, and can be replaced. For example, if we have


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D83896

Files:
  llvm/include/llvm/IR/DebugInfoMetadata.h
  llvm/lib/IR/DebugInfoMetadata.cpp


Index: llvm/lib/IR/DebugInfoMetadata.cpp
===================================================================
--- llvm/lib/IR/DebugInfoMetadata.cpp
+++ llvm/lib/IR/DebugInfoMetadata.cpp
@@ -1188,6 +1188,28 @@
   return DIExpression::get(Expr->getContext(), NewOps);
 }
 
+DIExpression *DIExpression::replaceArg(const DIExpression *Expr,
+                                       uint64_t OldArg, uint64_t NewArg) {
+  assert(Expr && "Can't replace args in this expression");
+
+  SmallVector<uint64_t, 8> NewOps;
+
+  for (auto Op : Expr->expr_ops()) {
+    if (Op.getOp() != dwarf::DW_OP_LLVM_arg || Op.getArg(0) < OldArg) {
+      Op.appendToVector(NewOps);
+      continue;
+    }
+    NewOps.push_back(dwarf::DW_OP_LLVM_arg);
+    uint64_t Arg = Op.getArg(0) == OldArg ? NewArg : Op.getArg(0);
+    // OldArg has been deleted from the Op list, so decrement all indices
+    // greater than it.
+    if (Arg > OldArg)
+      --Arg;
+    NewOps.push_back(Arg);
+  }
+  return DIExpression::get(Expr->getContext(), NewOps);
+}
+
 DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr,
                                            SmallVectorImpl<uint64_t> &Ops,
                                            bool StackValue,
Index: llvm/include/llvm/IR/DebugInfoMetadata.h
===================================================================
--- llvm/include/llvm/IR/DebugInfoMetadata.h
+++ llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -2597,6 +2597,14 @@
   static DIExpression *appendOpsToArg(const DIExpression *Expr,
                                       ArrayRef<uint64_t> Ops, uint64_t ArgNo);
 
+  /// Modify \p Expr by replacing each instance of `DW_OP_LLVM_arg, \p OldArg`
+  /// with `DW_OP_LLVM_arg, \p NewArg`, and then each `DW_OP_LLVM_arg, Arg`
+  /// with `DW_OP_LLVM_arg, Arg - 1` for all Arg > \p OldArg.
+  /// This is used when replacing one of the operands of a debug value list
+  /// with another operand in the same list and deleting the old operand.
+  static DIExpression *replaceArg(const DIExpression *Expr, uint64_t OldArg,
+	  uint64_t NewArg);
+
   /// Create a DIExpression to describe one part of an aggregate variable that
   /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
   /// will be appended to the elements of \c Expr. If \c Expr already contains


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83896.278258.patch
Type: text/x-patch
Size: 2365 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200715/baac0fc4/attachment.bin>


More information about the llvm-commits mailing list