[PATCH] D48340: [Local] Add a utility to insert replacement dbg.values, NFC

Vedant Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 19 16:33:33 PDT 2018


vsk created this revision.
vsk added reviewers: aprantl, gramanas, lebedev.ri.
Herald added a subscriber: JDevlieghere.

The purpose of this utility is to make it easier for optimizations to
insert replacement dbg.values for instructions they are deleting. This
is useful in situations where salvageDebugInfo is inapplicable, say,
because the new dbg.value cannot refer to a use of the dying value.

The utility is called insertReplacementDbgValues.

It assumes that the instruction 'From' is going to be deleted, and
inserts replacement dbg.values for each debug user of 'From'. The
newly-inserted dbg.values refer to 'To' instead of 'From'. Each
replacement dbg.value has the same location and variable as the debug
user it replaces, has a DIExpression determined by the result of
'RewriteExpr' applied to an old debug user of 'From', and is placed
before 'InsertBefore'.

This should simplify future patches, like https://reviews.llvm.org/D48331.


https://reviews.llvm.org/D48340

Files:
  include/llvm/Transforms/Utils/Local.h
  lib/Transforms/InstCombine/InstCombineCasts.cpp
  lib/Transforms/Utils/Local.cpp


Index: lib/Transforms/Utils/Local.cpp
===================================================================
--- lib/Transforms/Utils/Local.cpp
+++ lib/Transforms/Utils/Local.cpp
@@ -1671,6 +1671,24 @@
   }
 }
 
+void llvm::insertReplacementDbgValues(
+    Instruction &From, Instruction &To, Instruction &InsertBefore,
+    function_ref<DIExpression *(DbgInfoIntrinsic &OldDII)> RewriteExpr) {
+  // Collect all debug users of From.
+  SmallVector<DbgInfoIntrinsic *, 1> Users;
+  findDbgUsers(Users, &From);
+  if (Users.empty())
+    return;
+
+  // Insert a replacement debug value before each old debug user. It's assumed
+  // that the old debug users will be erased later.
+  DIBuilder DIB(*From.getModule());
+  for (auto *OldDII : Users)
+    DIB.insertDbgValueIntrinsic(&To, OldDII->getVariable(),
+                                RewriteExpr(*OldDII),
+                                OldDII->getDebugLoc().get(), &InsertBefore);
+}
+
 unsigned llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
   unsigned NumDeadInst = 0;
   // Delete the instructions backwards, as it has a reduced likelihood of
Index: lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -268,17 +268,12 @@
       // the second cast (CI). CSrc will then have a good chance of being dead.
       auto *Res = CastInst::Create(NewOpc, CSrc->getOperand(0), CI.getType());
 
-      // If the eliminable cast has debug users, insert a debug value after the
-      // cast pointing to the new Value.
-      SmallVector<DbgInfoIntrinsic *, 1> CSrcDbgInsts;
-      findDbgUsers(CSrcDbgInsts, CSrc);
-      if (CSrcDbgInsts.size()) {
-        DIBuilder DIB(*CI.getModule());
-        for (auto *DII : CSrcDbgInsts)
-          DIB.insertDbgValueIntrinsic(
-              Res, DII->getVariable(), DII->getExpression(),
-              DII->getDebugLoc().get(), &*std::next(CI.getIterator()));
-      }
+      // Replace debug users of the eliminable cast by emitting debug values
+      // which refer to the new cast.
+      insertReplacementDbgValues(
+          *CSrc, *Res, *std::next(CI.getIterator()),
+          [](DbgInfoIntrinsic &OldDII) { return OldDII.getExpression(); });
+
       return Res;
     }
   }
Index: include/llvm/Transforms/Utils/Local.h
===================================================================
--- include/llvm/Transforms/Utils/Local.h
+++ include/llvm/Transforms/Utils/Local.h
@@ -16,6 +16,7 @@
 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/TinyPtrVector.h"
@@ -333,6 +334,16 @@
 /// DIExpression.
 void salvageDebugInfo(Instruction &I);
 
+/// Assuming the instruction \p From is going to be deleted, insert replacement
+/// dbg.value intrinsics for each debug user of \p From. The newly-inserted
+/// dbg.values refer to \p To instead of \p From. Each replacement dbg.value
+/// has the same location and variable as the debug user it replaces, has a
+/// DIExpression determined by the result of \p RewriteExpr applied to an old
+/// debug user of \p From, and is placed before \p InsertBefore.
+void insertReplacementDbgValues(
+    Instruction &From, Instruction &To, Instruction &InsertBefore,
+    function_ref<DIExpression *(DbgInfoIntrinsic &OldDII)> RewriteExpr);
+
 /// Remove all instructions from a basic block other than it's terminator
 /// and any present EH pad instructions.
 unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48340.151993.patch
Type: text/x-patch
Size: 3704 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180619/74e79736/attachment.bin>


More information about the llvm-commits mailing list