Skip to content

Commit 3058548

Browse files
committed
Streamline the API of salvageDebugInfoImpl (NFC)
This patch refactors / simplifies salvageDebugInfoImpl(). The goal here is to simplify the implementation of coro::salvageDebugInfo() in a followup patch. 1. Change the return value to I.getOperand(0). Currently users of salvageDebugInfoImpl() assume that the first operand is I.getOperand(0). This patch makes this information explicit. A nice side-effect of this change is that it allows us to salvage expressions such as add i8 1, %a in the future. 2. Factor out the creation of a DIExpression and return an array of DIExpression operations instead. This change allows users that call salvageDebugInfoImpl() in a loop to avoid the costly creation of temporary DIExpressions and to defer the creation of a DIExpression until the end. This patch does not change any functionality. rdar://80227769 Differential Revision: https://reviews.llvm.org/D107383 (cherry picked from commit d6b6880)
1 parent 8a6f3ab commit 3058548

File tree

4 files changed

+80
-72
lines changed

4 files changed

+80
-72
lines changed

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,30 @@ void salvageDebugInfo(Instruction &I);
292292
void salvageDebugInfoForDbgValues(Instruction &I,
293293
ArrayRef<DbgVariableIntrinsic *> Insns);
294294

295-
/// Given an instruction \p I and DIExpression \p DIExpr operating on it, write
296-
/// the effects of \p I into the returned DIExpression, or return nullptr if
297-
/// it cannot be salvaged. \p StackVal: whether DW_OP_stack_value should be
298-
/// appended to the expression. \p LocNo: the index of the location operand to
299-
/// which \p I applies, should be 0 for debug info without a DIArgList.
300-
DIExpression *salvageDebugInfoImpl(Instruction &I, DIExpression *DIExpr,
301-
bool StackVal, unsigned LocNo,
302-
SmallVectorImpl<Value *> &AdditionalValues);
295+
/// Given an instruction \p I and DIExpression \p DIExpr operating on
296+
/// it, append the effects of \p I to the DIExpression operand list
297+
/// \p Ops, or return \p nullptr if it cannot be salvaged.
298+
/// \p CurrentLocOps is the number of SSA values referenced by the
299+
/// incoming \p Ops. \return the first non-constant operand
300+
/// implicitly referred to by Ops. If \p I references more than one
301+
/// non-constant operand, any additional operands are added to
302+
/// \p AdditionalValues.
303+
///
304+
/// \example
305+
////
306+
/// I = add %a, i32 1
307+
///
308+
/// Return = %a
309+
/// Ops = llvm::dwarf::DW_OP_lit1 llvm::dwarf::DW_OP_add
310+
///
311+
/// I = add %a, %b
312+
///
313+
/// Return = %a
314+
/// Ops = llvm::dwarf::DW_OP_LLVM_arg0 llvm::dwarf::DW_OP_add
315+
/// AdditionalValues = %b
316+
Value *salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
317+
SmallVectorImpl<uint64_t> &Ops,
318+
SmallVectorImpl<Value *> &AdditionalValues);
303319

304320
/// Point debug users of \p From to \p To or salvage them. Use this function
305321
/// only when replacing all uses of \p From with \p To, with a guarantee that

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,21 +1277,23 @@ void SelectionDAGBuilder::salvageUnresolvedDbgValue(DanglingDebugInfo &DDI) {
12771277
while (isa<Instruction>(V)) {
12781278
Instruction &VAsInst = *cast<Instruction>(V);
12791279
// Temporary "0", awaiting real implementation.
1280+
SmallVector<uint64_t, 16> Ops;
12801281
SmallVector<Value *, 4> AdditionalValues;
1281-
DIExpression *SalvagedExpr =
1282-
salvageDebugInfoImpl(VAsInst, Expr, StackValue, 0, AdditionalValues);
1283-
1282+
V = salvageDebugInfoImpl(VAsInst, Expr->getNumLocationOperands(), Ops,
1283+
AdditionalValues);
12841284
// If we cannot salvage any further, and haven't yet found a suitable debug
12851285
// expression, bail out.
1286+
if (!V)
1287+
break;
1288+
12861289
// TODO: If AdditionalValues isn't empty, then the salvage can only be
12871290
// represented with a DBG_VALUE_LIST, so we give up. When we have support
12881291
// here for variadic dbg_values, remove that condition.
1289-
if (!SalvagedExpr || !AdditionalValues.empty())
1292+
if (!AdditionalValues.empty())
12901293
break;
12911294

12921295
// New value and expr now represent this debuginfo.
1293-
V = VAsInst.getOperand(0);
1294-
Expr = SalvagedExpr;
1296+
Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, StackValue);
12951297

12961298
// Some kind of simplification occurred: check whether the operand of the
12971299
// salvaged debug expression can be encoded in this DAG.

llvm/lib/Transforms/Coroutines/CoroFrame.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,17 +2529,19 @@ void coro::salvageDebugInfo(
25292529
} else if (auto *StInst = dyn_cast<StoreInst>(Storage)) {
25302530
Storage = StInst->getOperand(0);
25312531
} else if (auto *GEPInst = dyn_cast<GetElementPtrInst>(Storage)) {
2532-
SmallVector<Value *> AdditionalValues;
2533-
DIExpression *SalvagedExpr = llvm::salvageDebugInfoImpl(
2534-
*GEPInst, Expr,
2535-
/*WithStackValue=*/false, 0, AdditionalValues);
2532+
SmallVector<uint64_t, 16> Ops;
2533+
SmallVector<Value *, 0> AdditionalValues;
2534+
Storage = llvm::salvageDebugInfoImpl(
2535+
*GEPInst, Expr ? Expr->getNumLocationOperands() : 0, Ops,
2536+
AdditionalValues);
2537+
if (!Storage)
2538+
break;
25362539
// Debug declares cannot currently handle additional location
25372540
// operands.
2538-
if (!SalvagedExpr || !AdditionalValues.empty())
2541+
if (!AdditionalValues.empty())
25392542
break;
2540-
Expr = SalvagedExpr;
2541-
Storage = GEPInst->getOperand(0);
2542-
} else if (auto *BCInst = dyn_cast<llvm::BitCastInst>(Storage)) {
2543+
Expr = DIExpression::appendOpsToArg(Expr, Ops, 0, /*StackValue*/ false);
2544+
} else if (auto *BCInst = dyn_cast<llvm::BitCastInst>(Storage))
25432545
Storage = BCInst->getOperand(0);
25442546
} else if (auto *I2PInst = dyn_cast<llvm::IntToPtrInst>(Storage)) {
25452547
Storage = I2PInst->getOperand(0);

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,20 +1752,26 @@ void llvm::salvageDebugInfoForDbgValues(
17521752
// must be updated in the DIExpression and potentially have additional
17531753
// values added; thus we call salvageDebugInfoImpl for each `I` instance in
17541754
// DIILocation.
1755+
Value *Op0 = nullptr;
17551756
DIExpression *SalvagedExpr = DII->getExpression();
17561757
auto LocItr = find(DIILocation, &I);
17571758
while (SalvagedExpr && LocItr != DIILocation.end()) {
1759+
SmallVector<uint64_t, 16> Ops;
17581760
unsigned LocNo = std::distance(DIILocation.begin(), LocItr);
1759-
SalvagedExpr = salvageDebugInfoImpl(I, SalvagedExpr, StackValue, LocNo,
1760-
AdditionalValues);
1761+
uint64_t CurrentLocOps = SalvagedExpr->getNumLocationOperands();
1762+
Op0 = salvageDebugInfoImpl(I, CurrentLocOps, Ops, AdditionalValues);
1763+
if (!Op0)
1764+
break;
1765+
SalvagedExpr =
1766+
DIExpression::appendOpsToArg(SalvagedExpr, Ops, LocNo, StackValue);
17611767
LocItr = std::find(++LocItr, DIILocation.end(), &I);
17621768
}
17631769
// salvageDebugInfoImpl should fail on examining the first element of
17641770
// DbgUsers, or none of them.
1765-
if (!SalvagedExpr)
1771+
if (!Op0)
17661772
break;
17671773

1768-
DII->replaceVariableLocationOp(&I, I.getOperand(0));
1774+
DII->replaceVariableLocationOp(&I, Op0);
17691775
if (AdditionalValues.empty()) {
17701776
DII->setExpression(SalvagedExpr);
17711777
} else if (isa<DbgValueInst>(DII) &&
@@ -1793,16 +1799,16 @@ void llvm::salvageDebugInfoForDbgValues(
17931799
}
17941800
}
17951801

1796-
bool getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
1797-
uint64_t CurrentLocOps,
1798-
SmallVectorImpl<uint64_t> &Opcodes,
1799-
SmallVectorImpl<Value *> &AdditionalValues) {
1802+
Value *getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
1803+
uint64_t CurrentLocOps,
1804+
SmallVectorImpl<uint64_t> &Opcodes,
1805+
SmallVectorImpl<Value *> &AdditionalValues) {
18001806
unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());
18011807
// Rewrite a GEP into a DIExpression.
18021808
MapVector<Value *, APInt> VariableOffsets;
18031809
APInt ConstantOffset(BitWidth, 0);
18041810
if (!GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))
1805-
return false;
1811+
return nullptr;
18061812
if (!VariableOffsets.empty() && !CurrentLocOps) {
18071813
Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0});
18081814
CurrentLocOps = 1;
@@ -1816,7 +1822,7 @@ bool getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL,
18161822
dwarf::DW_OP_plus});
18171823
}
18181824
DIExpression::appendOffset(Opcodes, ConstantOffset.getSExtValue());
1819-
return true;
1825+
return GEP->getOperand(0);
18201826
}
18211827

18221828
uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) {
@@ -1849,14 +1855,14 @@ uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode) {
18491855
}
18501856
}
18511857

1852-
bool getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps,
1853-
SmallVectorImpl<uint64_t> &Opcodes,
1854-
SmallVectorImpl<Value *> &AdditionalValues) {
1858+
Value *getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps,
1859+
SmallVectorImpl<uint64_t> &Opcodes,
1860+
SmallVectorImpl<Value *> &AdditionalValues) {
18551861
// Handle binary operations with constant integer operands as a special case.
18561862
auto *ConstInt = dyn_cast<ConstantInt>(BI->getOperand(1));
18571863
// Values wider than 64 bits cannot be represented within a DIExpression.
18581864
if (ConstInt && ConstInt->getBitWidth() > 64)
1859-
return false;
1865+
return nullptr;
18601866

18611867
Instruction::BinaryOps BinOpcode = BI->getOpcode();
18621868
// Push any Constant Int operand onto the expression stack.
@@ -1867,7 +1873,7 @@ bool getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps,
18671873
if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) {
18681874
uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val);
18691875
DIExpression::appendOffset(Opcodes, Offset);
1870-
return true;
1876+
return BI->getOperand(0);
18711877
}
18721878
Opcodes.append({dwarf::DW_OP_constu, Val});
18731879
} else {
@@ -1883,61 +1889,43 @@ bool getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps,
18831889
// representation in a DIExpression.
18841890
uint64_t DwarfBinOp = getDwarfOpForBinOp(BinOpcode);
18851891
if (!DwarfBinOp)
1886-
return false;
1892+
return nullptr;
18871893
Opcodes.push_back(DwarfBinOp);
1888-
1889-
return true;
1894+
return BI->getOperand(0);
18901895
}
18911896

1892-
DIExpression *
1893-
llvm::salvageDebugInfoImpl(Instruction &I, DIExpression *SrcDIExpr,
1894-
bool WithStackValue, unsigned LocNo,
1895-
SmallVectorImpl<Value *> &AdditionalValues) {
1896-
uint64_t CurrentLocOps = SrcDIExpr->getNumLocationOperands();
1897+
Value *llvm::salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps,
1898+
SmallVectorImpl<uint64_t> &Ops,
1899+
SmallVectorImpl<Value *> &AdditionalValues) {
18971900
auto &M = *I.getModule();
18981901
auto &DL = M.getDataLayout();
18991902

1900-
// Apply a vector of opcodes to the source DIExpression.
1901-
auto doSalvage = [&](SmallVectorImpl<uint64_t> &Ops) -> DIExpression * {
1902-
DIExpression *DIExpr = SrcDIExpr;
1903-
if (!Ops.empty()) {
1904-
DIExpr = DIExpression::appendOpsToArg(DIExpr, Ops, LocNo, WithStackValue);
1905-
}
1906-
return DIExpr;
1907-
};
1908-
1909-
// initializer-list helper for applying operators to the source DIExpression.
1910-
auto applyOps = [&](ArrayRef<uint64_t> Opcodes) {
1911-
SmallVector<uint64_t, 8> Ops(Opcodes.begin(), Opcodes.end());
1912-
return doSalvage(Ops);
1913-
};
1914-
19151903
if (auto *CI = dyn_cast<CastInst>(&I)) {
1904+
Value *FromValue = CI->getOperand(0);
19161905
// No-op casts are irrelevant for debug info.
1917-
if (CI->isNoopCast(DL))
1918-
return SrcDIExpr;
1906+
if (CI->isNoopCast(DL)) {
1907+
return FromValue;
1908+
}
19191909

19201910
Type *Type = CI->getType();
19211911
// Casts other than Trunc, SExt, or ZExt to scalar types cannot be salvaged.
19221912
if (Type->isVectorTy() ||
19231913
!(isa<TruncInst>(&I) || isa<SExtInst>(&I) || isa<ZExtInst>(&I)))
19241914
return nullptr;
19251915

1926-
Value *FromValue = CI->getOperand(0);
19271916
unsigned FromTypeBitSize = FromValue->getType()->getScalarSizeInBits();
19281917
unsigned ToTypeBitSize = Type->getScalarSizeInBits();
19291918

1930-
return applyOps(DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
1931-
isa<SExtInst>(&I)));
1919+
auto ExtOps = DIExpression::getExtOps(FromTypeBitSize, ToTypeBitSize,
1920+
isa<SExtInst>(&I));
1921+
Ops.append(ExtOps.begin(), ExtOps.end());
1922+
return FromValue;
19321923
}
19331924

1934-
SmallVector<uint64_t, 8> Ops;
1935-
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
1936-
if (getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues))
1937-
return doSalvage(Ops);
1938-
} else if (auto *BI = dyn_cast<BinaryOperator>(&I)) {
1939-
if (getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues))
1940-
return doSalvage(Ops);
1925+
if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
1926+
return getSalvageOpsForGEP(GEP, DL, CurrentLocOps, Ops, AdditionalValues);
1927+
else if (auto *BI = dyn_cast<BinaryOperator>(&I)) {
1928+
return getSalvageOpsForBinOp(BI, CurrentLocOps, Ops, AdditionalValues);
19411929
}
19421930
// *Not* to do: we should not attempt to salvage load instructions,
19431931
// because the validity and lifetime of a dbg.value containing

0 commit comments

Comments
 (0)