[PATCH] D109044: [DebugInfo][LoopStrengthReduction] Produce shorter expressions when using scev-based saslvaging

Chris Jackson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 31 23:33:41 PDT 2021


chrisjackson created this revision.
chrisjackson added reviewers: jmorse, TWeaver, aprantl, StephenTozer, Orlando, probinson, djtodoro, markus, dblaikie.
chrisjackson added a project: debug-info.
Herald added subscribers: javed.absar, hiraditya.
chrisjackson requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

The scev-based salvaging for LSR can sometimes produce unnecessarily verbose expressions. This patch adds logic to detect when the expression for the value to be recovered and the induction variable differ by only a constant offset, Then the expression to derive the current iteration count can be omitted from the dbg.value. This functionality is similar to the previous method for value recovery.


https://reviews.llvm.org/D109044

Files:
  llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
  llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-0.ll


Index: llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-0.ll
===================================================================
--- llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-0.ll
+++ llvm/test/Transforms/LoopStrengthReduce/dbg-preserve-0.ll
@@ -1,6 +1,8 @@
-; RUN: opt < %s -loop-reduce -S | FileCheck %s
+; RUN: opt -loop-reduce -S %s | FileCheck %s
 
-; Test that LSR preserves debug-info for induction variables.
+;; Test that LSR preserves debug-info for induction variables and scev-based
+;; salvaging produces short DIExpressions that use a constant offset from the
+;; induction variable.
 
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
 
@@ -21,11 +23,11 @@
   call void @llvm.dbg.value(metadata i8 %i.06, metadata !14, metadata !DIExpression()), !dbg !17
   call void @llvm.dbg.value(metadata i8* %p.addr.05, metadata !13, metadata !DIExpression()), !dbg !16
 ; CHECK-NOT: call void @llvm.dbg.value(metadata i8* undef
-; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i8* %lsr.iv, i8* %p), metadata ![[MID_p:[0-9]+]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_consts, 3, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_minus, DW_OP_consts, 3, DW_OP_div, DW_OP_consts, 3, DW_OP_mul, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_stack_value))
+; CHECK: all void @llvm.dbg.value(metadata i8* %lsr.iv, metadata ![[MID_p:[0-9]+]],  metadata !DIExpression(DW_OP_constu, 3, DW_OP_minus, DW_OP_stack_value))
   %add.ptr = getelementptr inbounds i8, i8* %p.addr.05, i64 3, !dbg !20
   call void @llvm.dbg.value(metadata i8* %add.ptr, metadata !13, metadata !DIExpression()), !dbg !16
 ; CHECK-NOT: call void @llvm.dbg.value(metadata i8* undef
-; CHECK: call void @llvm.dbg.value(metadata !DIArgList(i8* %lsr.iv, i8* %p), metadata ![[MID_p]], metadata !DIExpression(DW_OP_LLVM_arg, 0, DW_OP_consts, 3, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_minus, DW_OP_consts, 3, DW_OP_div, DW_OP_consts, 3, DW_OP_mul, DW_OP_consts, 3, DW_OP_LLVM_arg, 1, DW_OP_plus, DW_OP_plus, DW_OP_stack_value))
+; CHECK: call void @llvm.dbg.value(metadata i8* %lsr.iv, metadata ![[MID_p]], metadata !DIExpression())
   store i8 %i.06, i8* %add.ptr, align 1, !dbg !23, !tbaa !24
   %inc = add nuw nsw i8 %i.06, 1, !dbg !27
   call void @llvm.dbg.value(metadata i8 %inc, metadata !14, metadata !DIExpression()), !dbg !17
Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -6161,6 +6161,25 @@
   return true;
 }
 
+static bool RewriteDVIUsingOffset(DVIRecoveryRec &DVIRec, llvm::PHINode &IV,
+                                  int64_t Offset) {
+  assert(!DVIRec.DVI->hasArgList() && "Expected single location-op dbg.value.");
+  DbgValueInst *DVI = DVIRec.DVI;
+  SmallVector<uint64_t, 8> Ops;
+  DIExpression::appendOffset(Ops, Offset);
+  DIExpression *OldExpr =
+      DIExpression::prependOpcodes(DVI->getExpression(), Ops, true);
+  LLVM_DEBUG(dbgs() << "scev-salvage: Updating: " << *DVIRec.DVI << '\n');
+  DVI->setExpression(OldExpr);
+  llvm::Value *ValIV = dyn_cast<llvm::Value>(&IV);
+  DVI->replaceVariableLocationOp(
+      0u, llvm::MetadataAsValue::get(DVI->getContext(),
+                                     llvm::ValueAsMetadata::get(ValIV)));
+  LLVM_DEBUG(dbgs() << "scev-salvage: updated with offset to IV: "
+                    << *DVIRec.DVI << '\n');
+  return true;
+}
+
 static bool
 DbgRewriteSalvageableDVIs(llvm::Loop *L, ScalarEvolution &SE,
                           llvm::PHINode *LSRInductionVar,
@@ -6178,6 +6197,7 @@
     if (!IVAddRec->isAffine())
       return false;
 
+    // The iteratioun count is required to recover location values.
     SCEVDbgValueBuilder IterCountExpr;
     IterCountExpr.pushValue(LSRInductionVar);
     if (!IterCountExpr.SCEVToIterCountExpr(*IVAddRec, SE))
@@ -6201,13 +6221,28 @@
         llvm::Type *Ty = DVIRec.DVI->getVariableLocationOp(0)->getType();
         DVIRec.DVI->setRawLocation(
             llvm::ValueAsMetadata::get(UndefValue::get(Ty)));
-        DVIRec.DVI->setExpression(DVIRec.Expr);
       }
-
-      Changed |= RewriteDVIUsingIterCount(DVIRec, IterCountExpr, SE);
+      // LSR may have updated the expression, so restore to the expression
+      // that corresponds to te cached SCEV.
+      DVIRec.DVI->setExpression(DVIRec.Expr);
+
+      LLVM_DEBUG(dbgs() << "scev-salvage: value to recover SCEV: "
+                        << *DVIRec.SCEV);
+
+      // Create a simple expression if the IV and value to salvage SCEVs
+      // start values differ by only a constant value.
+      if (Optional<APInt> Offset =
+              SE.computeConstantDifference(DVIRec.SCEV, SCEVInductionVar)) {
+        if (Offset.getValue().getMinSignedBits() <= 64)
+          Changed |= RewriteDVIUsingOffset(DVIRec, *LSRInductionVar,
+                                           Offset.getValue().getSExtValue());
+      } else {
+        Changed |= RewriteDVIUsingIterCount(DVIRec, IterCountExpr, SE);
+      }
     }
+
+    return Changed;
   }
-  return Changed;
 }
 
 /// Identify and cache salvageable DVI locations and expressions along with the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109044.369860.patch
Type: text/x-patch
Size: 5238 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210901/822f948d/attachment.bin>


More information about the llvm-commits mailing list