Skip to content

Commit e85bbf5

Browse files
[DebugInfoMetadata] Refactor DIExpression::prepend constants (NFC)
Refactor DIExpression::With* into a flag enum in order to be less error-prone to use (as discussed on D60866). Patch by Djordje Todorovic. Differential Revision: https://reviews.llvm.org/D61943 llvm-svn: 361137
1 parent 96c5929 commit e85bbf5

18 files changed

+58
-52
lines changed

llvm/include/llvm/IR/DebugInfoMetadata.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -2477,14 +2477,18 @@ class DIExpression : public MDNode {
24772477
static const DIExpression *extractAddressClass(const DIExpression *Expr,
24782478
unsigned &AddrClass);
24792479

2480-
/// Constants for DIExpression::prepend.
2481-
enum { NoDeref = false, WithDeref = true, WithStackValue = true };
2480+
/// Used for DIExpression::prepend.
2481+
enum PrependOps : uint8_t {
2482+
ApplyOffset = 0,
2483+
DerefBefore = 1 << 0,
2484+
DerefAfter = 1 << 1,
2485+
StackValue = 1 << 2
2486+
};
24822487

24832488
/// Prepend \p DIExpr with a deref and offset operation and optionally turn it
24842489
/// into a stack value.
2485-
static DIExpression *prepend(const DIExpression *Expr, bool DerefBefore,
2486-
int64_t Offset = 0, bool DerefAfter = false,
2487-
bool StackValue = false);
2490+
static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
2491+
int64_t Offset = 0);
24882492

24892493
/// Prepend \p DIExpr with the given opcodes and optionally turn it into a
24902494
/// stack value.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V);
316316
/// (between the optional Deref operations). Offset can be negative.
317317
bool replaceDbgDeclare(Value *Address, Value *NewAddress,
318318
Instruction *InsertBefore, DIBuilder &Builder,
319-
bool DerefBefore, int Offset, bool DerefAfter);
319+
uint8_t DIExprFlags, int Offset);
320320

321321
/// Replaces llvm.dbg.declare instruction when the alloca it describes
322322
/// is replaced with a new value. If Deref is true, an additional
@@ -325,8 +325,8 @@ bool replaceDbgDeclare(Value *Address, Value *NewAddress,
325325
/// optional Deref operations). Offset can be negative. The new
326326
/// llvm.dbg.declare is inserted immediately after AI.
327327
bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
328-
DIBuilder &Builder, bool DerefBefore,
329-
int Offset, bool DerefAfter);
328+
DIBuilder &Builder, uint8_t DIExprFlags,
329+
int Offset);
330330

331331
/// Replaces multiple llvm.dbg.value instructions when the alloca it describes
332332
/// is replaced with a new value. If Offset is non-zero, a constant displacement

llvm/lib/CodeGen/LiveDebugValues.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ void LiveDebugValues::insertTransferDebugPair(
462462
// Create a DBG_VALUE instruction to describe the Var in its spilled
463463
// location.
464464
VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI);
465-
auto *SpillExpr =
466-
DIExpression::prepend(DMI->getDebugExpression(), DIExpression::NoDeref,
467-
SpillLocation.SpillOffset);
465+
auto *SpillExpr = DIExpression::prepend(DMI->getDebugExpression(),
466+
DIExpression::ApplyOffset,
467+
SpillLocation.SpillOffset);
468468
NewDMI =
469469
BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true,
470470
SpillLocation.SpillBase, DMI->getDebugVariable(), SpillExpr);

llvm/lib/CodeGen/LiveDebugVariables.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1316,11 +1316,13 @@ void UserValue::insertDebugValue(MachineBasicBlock *MBB, SlotIndex StartIdx,
13161316
// that the original virtual register was a pointer. Also, add the stack slot
13171317
// offset for the spilled register to the expression.
13181318
const DIExpression *Expr = Expression;
1319+
uint8_t DIExprFlags = DIExpression::ApplyOffset;
13191320
bool IsIndirect = Loc.wasIndirect();
13201321
if (Spilled) {
1321-
auto Deref = IsIndirect ? DIExpression::WithDeref : DIExpression::NoDeref;
1322+
if (IsIndirect)
1323+
DIExprFlags |= DIExpression::DerefAfter;
13221324
Expr =
1323-
DIExpression::prepend(Expr, DIExpression::NoDeref, SpillOffset, Deref);
1325+
DIExpression::prepend(Expr, DIExprFlags, SpillOffset);
13241326
IsIndirect = true;
13251327
}
13261328

llvm/lib/CodeGen/MachineInstr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2075,7 +2075,7 @@ static const DIExpression *computeExprForSpill(const MachineInstr &MI) {
20752075
const DIExpression *Expr = MI.getDebugExpression();
20762076
if (MI.isIndirectDebugValue()) {
20772077
assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
2078-
Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
2078+
Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
20792079
}
20802080
return Expr;
20812081
}

llvm/lib/CodeGen/PrologEpilogInserter.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -1187,11 +1187,13 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
11871187
// to be direct.
11881188
if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
11891189
SmallVector<uint64_t, 2> Ops = {dwarf::DW_OP_deref_size, Size};
1190-
DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, DIExpression::WithStackValue);
1190+
bool WithStackValue = true;
1191+
DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
11911192
// Make the DBG_VALUE direct.
11921193
MI.getOperand(1).ChangeToRegister(0, false);
11931194
}
1194-
DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset);
1195+
DIExpr =
1196+
DIExpression::prepend(DIExpr, DIExpression::ApplyOffset, Offset);
11951197
MI.getOperand(3).setMetadata(DIExpr);
11961198
continue;
11971199
}

llvm/lib/CodeGen/SafeStack.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
576576

577577
// Replace alloc with the new location.
578578
replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB,
579-
DIExpression::NoDeref, -Offset, DIExpression::NoDeref);
579+
DIExpression::ApplyOffset, -Offset);
580580
Arg->replaceAllUsesWith(NewArg);
581581
IRB.SetInsertPoint(cast<Instruction>(NewArg)->getNextNode());
582582
IRB.CreateMemCpy(Off, Align, Arg, Arg->getParamAlignment(), Size);
@@ -591,8 +591,8 @@ Value *SafeStack::moveStaticAllocasToUnsafeStack(
591591
if (Size == 0)
592592
Size = 1; // Don't create zero-sized stack objects.
593593

594-
replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::NoDeref,
595-
-Offset, DIExpression::NoDeref);
594+
replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::ApplyOffset,
595+
-Offset);
596596
replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
597597

598598
// Replace uses of the alloca with the new location.
@@ -683,8 +683,7 @@ void SafeStack::moveDynamicAllocasToUnsafeStack(
683683
if (AI->hasName() && isa<Instruction>(NewAI))
684684
NewAI->takeName(AI);
685685

686-
replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::NoDeref, 0,
687-
DIExpression::NoDeref);
686+
replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::ApplyOffset, 0);
688687
AI->replaceAllUsesWith(NewAI);
689688
AI->eraseFromParent();
690689
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -7946,9 +7946,8 @@ void SelectionDAG::salvageDebugInfo(SDNode &N) {
79467946
// DIExpression, we need to mark the expression with a
79477947
// DW_OP_stack_value.
79487948
auto *DIExpr = DV->getExpression();
7949-
DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset,
7950-
DIExpression::NoDeref,
7951-
DIExpression::WithStackValue);
7949+
DIExpr =
7950+
DIExpression::prepend(DIExpr, DIExpression::StackValue, Offset);
79527951
SDDbgValue *Clone =
79537952
getDbgValue(DV->getVariable(), DIExpr, N0.getNode(), N0.getResNo(),
79547953
DV->isIndirect(), DV->getDebugLoc(), DV->getOrder());

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,7 @@ static void processDbgDeclares(FunctionLoweringInfo *FuncInfo) {
13631363

13641364
DIExpression *Expr = DI->getExpression();
13651365
if (Offset.getBoolValue())
1366-
Expr = DIExpression::prepend(Expr, DIExpression::NoDeref,
1366+
Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset,
13671367
Offset.getZExtValue());
13681368
MF->setVariableDbgInfo(DI->getVariable(), Expr, FI, DI->getDebugLoc());
13691369
}

llvm/lib/IR/DebugInfoMetadata.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -979,17 +979,18 @@ const DIExpression *DIExpression::extractAddressClass(const DIExpression *Expr,
979979
return Expr;
980980
}
981981

982-
DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore,
983-
int64_t Offset, bool DerefAfter,
984-
bool StackValue) {
982+
DIExpression *DIExpression::prepend(const DIExpression *Expr, uint8_t Flags,
983+
int64_t Offset) {
985984
SmallVector<uint64_t, 8> Ops;
986-
if (DerefBefore)
985+
if (Flags & DIExpression::DerefBefore)
987986
Ops.push_back(dwarf::DW_OP_deref);
988987

989988
appendOffset(Ops, Offset);
990-
if (DerefAfter)
989+
if (Flags & DIExpression::DerefAfter)
991990
Ops.push_back(dwarf::DW_OP_deref);
992991

992+
bool StackValue = Flags & DIExpression::StackValue;
993+
993994
return prependOpcodes(Expr, Ops, StackValue);
994995
}
995996

llvm/lib/Target/NVPTX/NVPTXPrologEpilogPass.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ bool NVPTXPrologEpilogPass::runOnMachineFunction(MachineFunction &MF) {
7272
TFI.getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
7373
MI.getOperand(0).ChangeToRegister(Reg, /*isDef=*/false);
7474
MI.getOperand(0).setIsDebug();
75-
auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
76-
DIExpression::NoDeref, Offset);
75+
auto *DIExpr = DIExpression::prepend(
76+
MI.getDebugExpression(), DIExpression::ApplyOffset, Offset);
7777
MI.getOperand(3).setMetadata(DIExpr);
7878
continue;
7979
}

llvm/lib/Target/X86/X86OptimizeLEAs.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -568,11 +568,8 @@ MachineInstr *OptimizeLEAPass::replaceDebugValue(MachineInstr &MI,
568568
unsigned VReg,
569569
int64_t AddrDispShift) {
570570
DIExpression *Expr = const_cast<DIExpression *>(MI.getDebugExpression());
571-
572571
if (AddrDispShift != 0)
573-
Expr = DIExpression::prepend(Expr, DIExpression::NoDeref, AddrDispShift,
574-
DIExpression::NoDeref,
575-
DIExpression::WithStackValue);
572+
Expr = DIExpression::prepend(Expr, DIExpression::StackValue, AddrDispShift);
576573

577574
// Replace DBG_VALUE instruction with modified version.
578575
MachineBasicBlock *MBB = MI.getParent();

llvm/lib/Transforms/IPO/GlobalOpt.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,8 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal) {
16711671
dwarf::DW_OP_constu, ValMinus,
16721672
dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit,
16731673
dwarf::DW_OP_plus};
1674-
E = DIExpression::prependOpcodes(E, Ops, DIExpression::WithStackValue);
1674+
bool WithStackValue = true;
1675+
E = DIExpression::prependOpcodes(E, Ops, WithStackValue);
16751676
DIGlobalVariableExpression *DGVE =
16761677
DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E);
16771678
NewGV->addDebugInfo(DGVE);

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -3053,7 +3053,7 @@ void FunctionStackPoisoner::processStaticAllocas() {
30533053
Value *FakeStack;
30543054
Value *LocalStackBase;
30553055
Value *LocalStackBaseAlloca;
3056-
bool Deref;
3056+
uint8_t DIExprFlags = DIExpression::ApplyOffset;
30573057

30583058
if (DoStackMalloc) {
30593059
LocalStackBaseAlloca =
@@ -3094,22 +3094,21 @@ void FunctionStackPoisoner::processStaticAllocas() {
30943094
LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
30953095
IRB.SetCurrentDebugLocation(EntryDebugLocation);
30963096
IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca);
3097-
Deref = true;
3097+
DIExprFlags |= DIExpression::DerefBefore;
30983098
} else {
30993099
// void *FakeStack = nullptr;
31003100
// void *LocalStackBase = alloca(LocalStackSize);
31013101
FakeStack = ConstantInt::get(IntptrTy, 0);
31023102
LocalStackBase =
31033103
DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
31043104
LocalStackBaseAlloca = LocalStackBase;
3105-
Deref = false;
31063105
}
31073106

31083107
// Replace Alloca instructions with base+offset.
31093108
for (const auto &Desc : SVD) {
31103109
AllocaInst *AI = Desc.AI;
3111-
replaceDbgDeclareForAlloca(AI, LocalStackBaseAlloca, DIB, Deref,
3112-
Desc.Offset, DIExpression::NoDeref);
3110+
replaceDbgDeclareForAlloca(AI, LocalStackBaseAlloca, DIB, DIExprFlags,
3111+
Desc.Offset);
31133112
Value *NewAllocaPtr = IRB.CreateIntToPtr(
31143113
IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
31153114
AI->getType());

llvm/lib/Transforms/Utils/InlineFunction.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1862,8 +1862,7 @@ llvm::InlineResult llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI,
18621862
// Move any dbg.declares describing the allocas into the entry basic block.
18631863
DIBuilder DIB(*Caller->getParent());
18641864
for (auto &AI : IFI.StaticAllocas)
1865-
replaceDbgDeclareForAlloca(AI, AI, DIB, DIExpression::NoDeref, 0,
1866-
DIExpression::NoDeref);
1865+
replaceDbgDeclareForAlloca(AI, AI, DIB, DIExpression::ApplyOffset, 0);
18671866
}
18681867

18691868
SmallVector<Value*,4> VarArgsToForward;

llvm/lib/Transforms/Utils/Local.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1552,14 +1552,14 @@ void llvm::findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgUsers,
15521552

15531553
bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
15541554
Instruction *InsertBefore, DIBuilder &Builder,
1555-
bool DerefBefore, int Offset, bool DerefAfter) {
1555+
uint8_t DIExprFlags, int Offset) {
15561556
auto DbgAddrs = FindDbgAddrUses(Address);
15571557
for (DbgVariableIntrinsic *DII : DbgAddrs) {
15581558
DebugLoc Loc = DII->getDebugLoc();
15591559
auto *DIVar = DII->getVariable();
15601560
auto *DIExpr = DII->getExpression();
15611561
assert(DIVar && "Missing variable");
1562-
DIExpr = DIExpression::prepend(DIExpr, DerefBefore, Offset, DerefAfter);
1562+
DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
15631563
// Insert llvm.dbg.declare immediately before InsertBefore, and remove old
15641564
// llvm.dbg.declare.
15651565
Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore);
@@ -1571,10 +1571,10 @@ bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
15711571
}
15721572

15731573
bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
1574-
DIBuilder &Builder, bool DerefBefore,
1575-
int Offset, bool DerefAfter) {
1574+
DIBuilder &Builder, uint8_t DIExprFlags,
1575+
int Offset) {
15761576
return replaceDbgDeclare(AI, NewAllocaAddress, AI->getNextNode(), Builder,
1577-
DerefBefore, Offset, DerefAfter);
1577+
DIExprFlags, Offset);
15781578
}
15791579

15801580
static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress,

llvm/unittests/IR/MetadataTest.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -2335,7 +2335,11 @@ TEST_F(DIExpressionTest, get) {
23352335
// Test DIExpression::prepend().
23362336
uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
23372337
auto *N0 = DIExpression::get(Context, Elts0);
2338-
auto *N0WithPrependedOps = DIExpression::prepend(N0, true, 64, true, true);
2338+
uint8_t DIExprFlags = DIExpression::ApplyOffset;
2339+
DIExprFlags |= DIExpression::DerefBefore;
2340+
DIExprFlags |= DIExpression::DerefAfter;
2341+
DIExprFlags |= DIExpression::StackValue;
2342+
auto *N0WithPrependedOps = DIExpression::prepend(N0, DIExprFlags, 64);
23392343
uint64_t Elts1[] = {dwarf::DW_OP_deref,
23402344
dwarf::DW_OP_plus_uconst, 64,
23412345
dwarf::DW_OP_deref,

llvm/unittests/Transforms/Utils/LocalTest.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ TEST(Local, ReplaceDbgDeclare) {
153153
ASSERT_TRUE(DII);
154154
Value *NewBase = Constant::getNullValue(Type::getInt32PtrTy(C));
155155
DIBuilder DIB(*M);
156-
replaceDbgDeclare(AI, NewBase, DII, DIB, DIExpression::NoDeref, 0,
157-
DIExpression::NoDeref);
156+
replaceDbgDeclare(AI, NewBase, DII, DIB, DIExpression::ApplyOffset, 0);
158157

159158
// There should be exactly two dbg.declares.
160159
int Declares = 0;

0 commit comments

Comments
 (0)