@@ -6366,10 +6366,12 @@ struct SCEVDbgValueBuilder {
6366
6366
// / and DIExpression.
6367
6367
struct DVIRecoveryRec {
6368
6368
DVIRecoveryRec (DbgValueInst *DbgValue)
6369
- : DVI (DbgValue), Expr(DbgValue->getExpression ()),
6369
+ : DbgRef (DbgValue), Expr(DbgValue->getExpression ()),
6370
6370
HadLocationArgList(false ) {}
6371
+ DVIRecoveryRec (DPValue *DPV)
6372
+ : DbgRef(DPV), Expr(DPV->getExpression ()), HadLocationArgList(false ) {}
6371
6373
6372
- DbgValueInst *DVI ;
6374
+ PointerUnion< DbgValueInst *, DPValue *> DbgRef ;
6373
6375
DIExpression *Expr;
6374
6376
bool HadLocationArgList;
6375
6377
SmallVector<WeakVH, 2 > LocationOps;
@@ -6401,17 +6403,19 @@ static unsigned numLLVMArgOps(SmallVectorImpl<uint64_t> &Expr) {
6401
6403
// / Overwrites DVI with the location and Ops as the DIExpression. This will
6402
6404
// / create an invalid expression if Ops has any dwarf::DW_OP_llvm_arg operands,
6403
6405
// / because a DIArglist is not created for the first argument of the dbg.value.
6404
- static void updateDVIWithLocation (DbgValueInst &DVI, Value *Location,
6406
+ template <typename T>
6407
+ static void updateDVIWithLocation (T &DbgVal, Value *Location,
6405
6408
SmallVectorImpl<uint64_t > &Ops) {
6406
- assert (
6407
- numLLVMArgOps (Ops) == 0 &&
6408
- " Expected expression that does not contain any DW_OP_llvm_arg operands. " );
6409
- DVI. setRawLocation ( ValueAsMetadata ::get (Location ));
6410
- DVI .setExpression (DIExpression::get (DVI .getContext (), Ops));
6409
+ assert (numLLVMArgOps (Ops) == 0 && " Expected expression that does not "
6410
+ " contain any DW_OP_llvm_arg operands. " );
6411
+ DbgVal. setRawLocation ( ValueAsMetadata::get (Location) );
6412
+ DbgVal. setExpression ( DIExpression ::get (DbgVal. getContext (), Ops ));
6413
+ DbgVal .setExpression (DIExpression::get (DbgVal .getContext (), Ops));
6411
6414
}
6412
6415
6413
6416
// / Overwrite DVI with locations placed into a DIArglist.
6414
- static void updateDVIWithLocations (DbgValueInst &DVI,
6417
+ template <typename T>
6418
+ static void updateDVIWithLocations (T &DbgVal,
6415
6419
SmallVectorImpl<Value *> &Locations,
6416
6420
SmallVectorImpl<uint64_t > &Ops) {
6417
6421
assert (numLLVMArgOps (Ops) != 0 &&
@@ -6421,8 +6425,8 @@ static void updateDVIWithLocations(DbgValueInst &DVI,
6421
6425
for (Value *V : Locations)
6422
6426
MetadataLocs.push_back (ValueAsMetadata::get (V));
6423
6427
auto ValArrayRef = llvm::ArrayRef<llvm::ValueAsMetadata *>(MetadataLocs);
6424
- DVI .setRawLocation (llvm::DIArgList::get (DVI .getContext (), ValArrayRef));
6425
- DVI .setExpression (DIExpression::get (DVI .getContext (), Ops));
6428
+ DbgVal .setRawLocation (llvm::DIArgList::get (DbgVal .getContext (), ValArrayRef));
6429
+ DbgVal .setExpression (DIExpression::get (DbgVal .getContext (), Ops));
6426
6430
}
6427
6431
6428
6432
// / Write the new expression and new location ops for the dbg.value. If possible
@@ -6433,30 +6437,37 @@ static void updateDVIWithLocations(DbgValueInst &DVI,
6433
6437
static void UpdateDbgValueInst (DVIRecoveryRec &DVIRec,
6434
6438
SmallVectorImpl<Value *> &NewLocationOps,
6435
6439
SmallVectorImpl<uint64_t > &NewExpr) {
6436
- unsigned NumLLVMArgs = numLLVMArgOps (NewExpr);
6437
- if (NumLLVMArgs == 0 ) {
6438
- // Location assumed to be on the stack.
6439
- updateDVIWithLocation (*DVIRec.DVI , NewLocationOps[0 ], NewExpr);
6440
- } else if (NumLLVMArgs == 1 && NewExpr[0 ] == dwarf::DW_OP_LLVM_arg) {
6441
- // There is only a single DW_OP_llvm_arg at the start of the expression,
6442
- // so it can be omitted along with DIArglist.
6443
- assert (NewExpr[1 ] == 0 &&
6444
- " Lone LLVM_arg in a DIExpression should refer to location-op 0." );
6445
- llvm::SmallVector<uint64_t , 6 > ShortenedOps (llvm::drop_begin (NewExpr, 2 ));
6446
- updateDVIWithLocation (*DVIRec.DVI , NewLocationOps[0 ], ShortenedOps);
6447
- } else {
6448
- // Multiple DW_OP_llvm_arg, so DIArgList is strictly necessary.
6449
- updateDVIWithLocations (*DVIRec.DVI , NewLocationOps, NewExpr);
6450
- }
6440
+ auto UpdateDbgValueInstImpl = [&](auto *DbgVal) {
6441
+ unsigned NumLLVMArgs = numLLVMArgOps (NewExpr);
6442
+ if (NumLLVMArgs == 0 ) {
6443
+ // Location assumed to be on the stack.
6444
+ updateDVIWithLocation (*DbgVal, NewLocationOps[0 ], NewExpr);
6445
+ } else if (NumLLVMArgs == 1 && NewExpr[0 ] == dwarf::DW_OP_LLVM_arg) {
6446
+ // There is only a single DW_OP_llvm_arg at the start of the expression,
6447
+ // so it can be omitted along with DIArglist.
6448
+ assert (NewExpr[1 ] == 0 &&
6449
+ " Lone LLVM_arg in a DIExpression should refer to location-op 0." );
6450
+ llvm::SmallVector<uint64_t , 6 > ShortenedOps (llvm::drop_begin (NewExpr, 2 ));
6451
+ updateDVIWithLocation (*DbgVal, NewLocationOps[0 ], ShortenedOps);
6452
+ } else {
6453
+ // Multiple DW_OP_llvm_arg, so DIArgList is strictly necessary.
6454
+ updateDVIWithLocations (*DbgVal, NewLocationOps, NewExpr);
6455
+ }
6451
6456
6452
- // If the DIExpression was previously empty then add the stack terminator.
6453
- // Non-empty expressions have only had elements inserted into them and so the
6454
- // terminator should already be present e.g. stack_value or fragment.
6455
- DIExpression *SalvageExpr = DVIRec.DVI ->getExpression ();
6456
- if (!DVIRec.Expr ->isComplex () && SalvageExpr->isComplex ()) {
6457
- SalvageExpr = DIExpression::append (SalvageExpr, {dwarf::DW_OP_stack_value});
6458
- DVIRec.DVI ->setExpression (SalvageExpr);
6459
- }
6457
+ // If the DIExpression was previously empty then add the stack terminator.
6458
+ // Non-empty expressions have only had elements inserted into them and so
6459
+ // the terminator should already be present e.g. stack_value or fragment.
6460
+ DIExpression *SalvageExpr = DbgVal->getExpression ();
6461
+ if (!DVIRec.Expr ->isComplex () && SalvageExpr->isComplex ()) {
6462
+ SalvageExpr =
6463
+ DIExpression::append (SalvageExpr, {dwarf::DW_OP_stack_value});
6464
+ DbgVal->setExpression (SalvageExpr);
6465
+ }
6466
+ };
6467
+ if (isa<DbgValueInst *>(DVIRec.DbgRef ))
6468
+ UpdateDbgValueInstImpl (cast<DbgValueInst *>(DVIRec.DbgRef ));
6469
+ else
6470
+ UpdateDbgValueInstImpl (cast<DPValue *>(DVIRec.DbgRef ));
6460
6471
}
6461
6472
6462
6473
// / Cached location ops may be erased during LSR, in which case a poison is
@@ -6470,40 +6481,49 @@ static Value *getValueOrPoison(WeakVH &VH, LLVMContext &C) {
6470
6481
6471
6482
// / Restore the DVI's pre-LSR arguments. Substitute undef for any erased values.
6472
6483
static void restorePreTransformState (DVIRecoveryRec &DVIRec) {
6473
- LLVM_DEBUG (dbgs () << " scev-salvage: restore dbg.value to pre-LSR state\n "
6474
- << " scev-salvage: post-LSR: " << *DVIRec.DVI << ' \n ' );
6475
- assert (DVIRec.Expr && " Expected an expression" );
6476
- DVIRec.DVI ->setExpression (DVIRec.Expr );
6477
-
6478
- // Even a single location-op may be inside a DIArgList and referenced with
6479
- // DW_OP_LLVM_arg, which is valid only with a DIArgList.
6480
- if (!DVIRec.HadLocationArgList ) {
6481
- assert (DVIRec.LocationOps .size () == 1 &&
6482
- " Unexpected number of location ops." );
6483
- // LSR's unsuccessful salvage attempt may have added DIArgList, which in
6484
- // this case was not present before, so force the location back to a single
6485
- // uncontained Value.
6486
- Value *CachedValue =
6487
- getValueOrPoison (DVIRec.LocationOps [0 ], DVIRec.DVI ->getContext ());
6488
- DVIRec.DVI ->setRawLocation (ValueAsMetadata::get (CachedValue));
6489
- } else {
6490
- SmallVector<ValueAsMetadata *, 3 > MetadataLocs;
6491
- for (WeakVH VH : DVIRec.LocationOps ) {
6492
- Value *CachedValue = getValueOrPoison (VH, DVIRec.DVI ->getContext ());
6493
- MetadataLocs.push_back (ValueAsMetadata::get (CachedValue));
6484
+ auto RestorePreTransformStateImpl = [&](auto *DbgVal) {
6485
+ LLVM_DEBUG (dbgs () << " scev-salvage: restore dbg.value to pre-LSR state\n "
6486
+ << " scev-salvage: post-LSR: " << *DbgVal << ' \n ' );
6487
+ assert (DVIRec.Expr && " Expected an expression" );
6488
+ DbgVal->setExpression (DVIRec.Expr );
6489
+
6490
+ // Even a single location-op may be inside a DIArgList and referenced with
6491
+ // DW_OP_LLVM_arg, which is valid only with a DIArgList.
6492
+ if (!DVIRec.HadLocationArgList ) {
6493
+ assert (DVIRec.LocationOps .size () == 1 &&
6494
+ " Unexpected number of location ops." );
6495
+ // LSR's unsuccessful salvage attempt may have added DIArgList, which in
6496
+ // this case was not present before, so force the location back to a
6497
+ // single uncontained Value.
6498
+ Value *CachedValue =
6499
+ getValueOrPoison (DVIRec.LocationOps [0 ], DbgVal->getContext ());
6500
+ DbgVal->setRawLocation (ValueAsMetadata::get (CachedValue));
6501
+ } else {
6502
+ SmallVector<ValueAsMetadata *, 3 > MetadataLocs;
6503
+ for (WeakVH VH : DVIRec.LocationOps ) {
6504
+ Value *CachedValue = getValueOrPoison (VH, DbgVal->getContext ());
6505
+ MetadataLocs.push_back (ValueAsMetadata::get (CachedValue));
6506
+ }
6507
+ auto ValArrayRef = llvm::ArrayRef<llvm::ValueAsMetadata *>(MetadataLocs);
6508
+ DbgVal->setRawLocation (
6509
+ llvm::DIArgList::get (DbgVal->getContext (), ValArrayRef));
6494
6510
}
6495
- auto ValArrayRef = llvm::ArrayRef<llvm::ValueAsMetadata *>(MetadataLocs);
6496
- DVIRec.DVI ->setRawLocation (
6497
- llvm::DIArgList::get (DVIRec.DVI ->getContext (), ValArrayRef));
6498
- }
6499
- LLVM_DEBUG (dbgs () << " scev-salvage: pre-LSR: " << *DVIRec.DVI << ' \n ' );
6511
+ LLVM_DEBUG (dbgs () << " scev-salvage: pre-LSR: " << *DbgVal << ' \n ' );
6512
+ };
6513
+ if (isa<DbgValueInst *>(DVIRec.DbgRef ))
6514
+ RestorePreTransformStateImpl (cast<DbgValueInst *>(DVIRec.DbgRef ));
6515
+ else
6516
+ RestorePreTransformStateImpl (cast<DPValue *>(DVIRec.DbgRef ));
6500
6517
}
6501
6518
6502
6519
static bool SalvageDVI (llvm::Loop *L, ScalarEvolution &SE,
6503
6520
llvm::PHINode *LSRInductionVar, DVIRecoveryRec &DVIRec,
6504
6521
const SCEV *SCEVInductionVar,
6505
6522
SCEVDbgValueBuilder IterCountExpr) {
6506
- if (!DVIRec.DVI ->isKillLocation ())
6523
+
6524
+ if (isa<DbgValueInst *>(DVIRec.DbgRef )
6525
+ ? !cast<DbgValueInst *>(DVIRec.DbgRef )->isKillLocation ()
6526
+ : !cast<DPValue *>(DVIRec.DbgRef )->isKillLocation ())
6507
6527
return false ;
6508
6528
6509
6529
// LSR may have caused several changes to the dbg.value in the failed salvage
@@ -6596,16 +6616,20 @@ static bool SalvageDVI(llvm::Loop *L, ScalarEvolution &SE,
6596
6616
}
6597
6617
6598
6618
UpdateDbgValueInst (DVIRec, NewLocationOps, NewExpr);
6599
- LLVM_DEBUG (dbgs () << " scev-salvage: Updated DVI: " << *DVIRec.DVI << " \n " );
6619
+ if (isa<DbgValueInst *>(DVIRec.DbgRef ))
6620
+ LLVM_DEBUG (dbgs () << " scev-salvage: Updated DVI: "
6621
+ << *cast<DbgValueInst *>(DVIRec.DbgRef ) << " \n " );
6622
+ else
6623
+ LLVM_DEBUG (dbgs () << " scev-salvage: Updated DVI: "
6624
+ << *cast<DPValue *>(DVIRec.DbgRef ) << " \n " );
6600
6625
return true ;
6601
6626
}
6602
6627
6603
6628
// / Obtain an expression for the iteration count, then attempt to salvage the
6604
6629
// / dbg.value intrinsics.
6605
- static void
6606
- DbgRewriteSalvageableDVIs (llvm::Loop *L, ScalarEvolution &SE,
6607
- llvm::PHINode *LSRInductionVar,
6608
- SmallVector<std::unique_ptr<DVIRecoveryRec>, 2 > &DVIToUpdate) {
6630
+ static void DbgRewriteSalvageableDVIs (
6631
+ llvm::Loop *L, ScalarEvolution &SE, llvm::PHINode *LSRInductionVar,
6632
+ SmallVector<std::unique_ptr<DVIRecoveryRec>, 2 > &DVIToUpdate) {
6609
6633
if (DVIToUpdate.empty ())
6610
6634
return ;
6611
6635
@@ -6647,48 +6671,56 @@ static void DbgGatherSalvagableDVI(
6647
6671
SmallSet<AssertingVH<DbgValueInst>, 2 > &DVIHandles) {
6648
6672
for (const auto &B : L->getBlocks ()) {
6649
6673
for (auto &I : *B) {
6650
- auto DVI = dyn_cast<DbgValueInst>(&I);
6651
- if (!DVI)
6652
- continue ;
6653
- // Ensure that if any location op is undef that the dbg.vlue is not
6654
- // cached.
6655
- if (DVI->isKillLocation ())
6656
- continue ;
6657
-
6658
- // Check that the location op SCEVs are suitable for translation to
6659
- // DIExpression.
6660
- const auto &HasTranslatableLocationOps =
6661
- [&](const DbgValueInst *DVI) -> bool {
6662
- for (const auto LocOp : DVI->location_ops ()) {
6663
- if (!LocOp)
6664
- return false ;
6665
-
6666
- if (!SE.isSCEVable (LocOp->getType ()))
6667
- return false ;
6668
-
6669
- const SCEV *S = SE.getSCEV (LocOp);
6670
- if (SE.containsUndefs (S))
6671
- return false ;
6674
+ auto ProcessDbgValue = [&](auto *DbgVal) -> bool {
6675
+ // Ensure that if any location op is undef that the dbg.vlue is not
6676
+ // cached.
6677
+ if (DbgVal->isKillLocation ())
6678
+ return false ;
6679
+
6680
+ // Check that the location op SCEVs are suitable for translation to
6681
+ // DIExpression.
6682
+ const auto &HasTranslatableLocationOps =
6683
+ [&](const auto *DbgValToTranslate) -> bool {
6684
+ for (const auto LocOp : DbgValToTranslate->location_ops ()) {
6685
+ if (!LocOp)
6686
+ return false ;
6687
+
6688
+ if (!SE.isSCEVable (LocOp->getType ()))
6689
+ return false ;
6690
+
6691
+ const SCEV *S = SE.getSCEV (LocOp);
6692
+ if (SE.containsUndefs (S))
6693
+ return false ;
6694
+ }
6695
+ return true ;
6696
+ };
6697
+
6698
+ if (!HasTranslatableLocationOps (DbgVal))
6699
+ return false ;
6700
+
6701
+ std::unique_ptr<DVIRecoveryRec> NewRec =
6702
+ std::make_unique<DVIRecoveryRec>(DbgVal);
6703
+ // Each location Op may need a SCEVDbgValueBuilder in order to recover
6704
+ // it. Pre-allocating a vector will enable quick lookups of the builder
6705
+ // later during the salvage.
6706
+ NewRec->RecoveryExprs .resize (DbgVal->getNumVariableLocationOps ());
6707
+ for (const auto LocOp : DbgVal->location_ops ()) {
6708
+ NewRec->SCEVs .push_back (SE.getSCEV (LocOp));
6709
+ NewRec->LocationOps .push_back (LocOp);
6710
+ NewRec->HadLocationArgList = DbgVal->hasArgList ();
6672
6711
}
6712
+ SalvageableDVISCEVs.push_back (std::move (NewRec));
6673
6713
return true ;
6674
6714
};
6675
-
6676
- if (!HasTranslatableLocationOps (DVI))
6677
- continue ;
6678
-
6679
- std::unique_ptr<DVIRecoveryRec> NewRec =
6680
- std::make_unique<DVIRecoveryRec>(DVI);
6681
- // Each location Op may need a SCEVDbgValueBuilder in order to recover it.
6682
- // Pre-allocating a vector will enable quick lookups of the builder later
6683
- // during the salvage.
6684
- NewRec->RecoveryExprs .resize (DVI->getNumVariableLocationOps ());
6685
- for (const auto LocOp : DVI->location_ops ()) {
6686
- NewRec->SCEVs .push_back (SE.getSCEV (LocOp));
6687
- NewRec->LocationOps .push_back (LocOp);
6688
- NewRec->HadLocationArgList = DVI->hasArgList ();
6715
+ for (auto &DPV : I.getDbgValueRange ()) {
6716
+ if (DPV.isDbgValue () || DPV.isDbgAssign ())
6717
+ ProcessDbgValue (&DPV);
6689
6718
}
6690
- SalvageableDVISCEVs.push_back (std::move (NewRec));
6691
- DVIHandles.insert (DVI);
6719
+ auto DVI = dyn_cast<DbgValueInst>(&I);
6720
+ if (!DVI)
6721
+ continue ;
6722
+ if (ProcessDbgValue (DVI))
6723
+ DVIHandles.insert (DVI);
6692
6724
}
6693
6725
}
6694
6726
}
0 commit comments