llvm.org GIT mirror llvm / 0ddfb52
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361137 91177308-0d34-0410-b5e6-96231b3b80d8 Petar Jovanovic 1 year, 6 months ago
18 changed file(s) with 58 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
24762476 static const DIExpression *extractAddressClass(const DIExpression *Expr,
24772477 unsigned &AddrClass);
24782478
2479 /// Constants for DIExpression::prepend.
2480 enum { NoDeref = false, WithDeref = true, WithStackValue = true };
2479 /// Used for DIExpression::prepend.
2480 enum PrependOps : uint8_t {
2481 ApplyOffset = 0,
2482 DerefBefore = 1 << 0,
2483 DerefAfter = 1 << 1,
2484 StackValue = 1 << 2
2485 };
24812486
24822487 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
24832488 /// into a stack value.
2484 static DIExpression *prepend(const DIExpression *Expr, bool DerefBefore,
2485 int64_t Offset = 0, bool DerefAfter = false,
2486 bool StackValue = false);
2489 static DIExpression *prepend(const DIExpression *Expr, uint8_t Flags,
2490 int64_t Offset = 0);
24872491
24882492 /// Prepend \p DIExpr with the given opcodes and optionally turn it into a
24892493 /// stack value.
315315 /// (between the optional Deref operations). Offset can be negative.
316316 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
317317 Instruction *InsertBefore, DIBuilder &Builder,
318 bool DerefBefore, int Offset, bool DerefAfter);
318 uint8_t DIExprFlags, int Offset);
319319
320320 /// Replaces llvm.dbg.declare instruction when the alloca it describes
321321 /// is replaced with a new value. If Deref is true, an additional
324324 /// optional Deref operations). Offset can be negative. The new
325325 /// llvm.dbg.declare is inserted immediately after AI.
326326 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
327 DIBuilder &Builder, bool DerefBefore,
328 int Offset, bool DerefAfter);
327 DIBuilder &Builder, uint8_t DIExprFlags,
328 int Offset);
329329
330330 /// Replaces multiple llvm.dbg.value instructions when the alloca it describes
331331 /// is replaced with a new value. If Offset is non-zero, a constant displacement
461461 // Create a DBG_VALUE instruction to describe the Var in its spilled
462462 // location.
463463 VarLoc::SpillLoc SpillLocation = extractSpillBaseRegAndOffset(MI);
464 auto *SpillExpr =
465 DIExpression::prepend(DMI->getDebugExpression(), DIExpression::NoDeref,
466 SpillLocation.SpillOffset);
464 auto *SpillExpr = DIExpression::prepend(DMI->getDebugExpression(),
465 DIExpression::ApplyOffset,
466 SpillLocation.SpillOffset);
467467 NewDMI =
468468 BuildMI(*MF, DMI->getDebugLoc(), DMI->getDesc(), true,
469469 SpillLocation.SpillBase, DMI->getDebugVariable(), SpillExpr);
13151315 // that the original virtual register was a pointer. Also, add the stack slot
13161316 // offset for the spilled register to the expression.
13171317 const DIExpression *Expr = Expression;
1318 uint8_t DIExprFlags = DIExpression::ApplyOffset;
13181319 bool IsIndirect = Loc.wasIndirect();
13191320 if (Spilled) {
1320 auto Deref = IsIndirect ? DIExpression::WithDeref : DIExpression::NoDeref;
1321 if (IsIndirect)
1322 DIExprFlags |= DIExpression::DerefAfter;
13211323 Expr =
1322 DIExpression::prepend(Expr, DIExpression::NoDeref, SpillOffset, Deref);
1324 DIExpression::prepend(Expr, DIExprFlags, SpillOffset);
13231325 IsIndirect = true;
13241326 }
13251327
20742074 const DIExpression *Expr = MI.getDebugExpression();
20752075 if (MI.isIndirectDebugValue()) {
20762076 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
2077 Expr = DIExpression::prepend(Expr, DIExpression::WithDeref);
2077 Expr = DIExpression::prepend(Expr, DIExpression::DerefBefore);
20782078 }
20792079 return Expr;
20802080 }
11861186 // to be direct.
11871187 if (MI.isIndirectDebugValue() && DIExpr->isImplicit()) {
11881188 SmallVector Ops = {dwarf::DW_OP_deref_size, Size};
1189 DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, DIExpression::WithStackValue);
1189 bool WithStackValue = true;
1190 DIExpr = DIExpression::prependOpcodes(DIExpr, Ops, WithStackValue);
11901191 // Make the DBG_VALUE direct.
11911192 MI.getOperand(1).ChangeToRegister(0, false);
11921193 }
1193 DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset);
1194 DIExpr =
1195 DIExpression::prepend(DIExpr, DIExpression::ApplyOffset, Offset);
11941196 MI.getOperand(3).setMetadata(DIExpr);
11951197 continue;
11961198 }
575575
576576 // Replace alloc with the new location.
577577 replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB,
578 DIExpression::NoDeref, -Offset, DIExpression::NoDeref);
578 DIExpression::ApplyOffset, -Offset);
579579 Arg->replaceAllUsesWith(NewArg);
580580 IRB.SetInsertPoint(cast(NewArg)->getNextNode());
581581 IRB.CreateMemCpy(Off, Align, Arg, Arg->getParamAlignment(), Size);
590590 if (Size == 0)
591591 Size = 1; // Don't create zero-sized stack objects.
592592
593 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::NoDeref,
594 -Offset, DIExpression::NoDeref);
593 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::ApplyOffset,
594 -Offset);
595595 replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
596596
597597 // Replace uses of the alloca with the new location.
682682 if (AI->hasName() && isa(NewAI))
683683 NewAI->takeName(AI);
684684
685 replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::NoDeref, 0,
686 DIExpression::NoDeref);
685 replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::ApplyOffset, 0);
687686 AI->replaceAllUsesWith(NewAI);
688687 AI->eraseFromParent();
689688 }
79457945 // DIExpression, we need to mark the expression with a
79467946 // DW_OP_stack_value.
79477947 auto *DIExpr = DV->getExpression();
7948 DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset,
7949 DIExpression::NoDeref,
7950 DIExpression::WithStackValue);
7948 DIExpr =
7949 DIExpression::prepend(DIExpr, DIExpression::StackValue, Offset);
79517950 SDDbgValue *Clone =
79527951 getDbgValue(DV->getVariable(), DIExpr, N0.getNode(), N0.getResNo(),
79537952 DV->isIndirect(), DV->getDebugLoc(), DV->getOrder());
13621362
13631363 DIExpression *Expr = DI->getExpression();
13641364 if (Offset.getBoolValue())
1365 Expr = DIExpression::prepend(Expr, DIExpression::NoDeref,
1365 Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset,
13661366 Offset.getZExtValue());
13671367 MF->setVariableDbgInfo(DI->getVariable(), Expr, FI, DI->getDebugLoc());
13681368 }
978978 return Expr;
979979 }
980980
981 DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore,
982 int64_t Offset, bool DerefAfter,
983 bool StackValue) {
981 DIExpression *DIExpression::prepend(const DIExpression *Expr, uint8_t Flags,
982 int64_t Offset) {
984983 SmallVector Ops;
985 if (DerefBefore)
984 if (Flags & DIExpression::DerefBefore)
986985 Ops.push_back(dwarf::DW_OP_deref);
987986
988987 appendOffset(Ops, Offset);
989 if (DerefAfter)
988 if (Flags & DIExpression::DerefAfter)
990989 Ops.push_back(dwarf::DW_OP_deref);
990
991 bool StackValue = Flags & DIExpression::StackValue;
991992
992993 return prependOpcodes(Expr, Ops, StackValue);
993994 }
7171 TFI.getFrameIndexReference(MF, MI.getOperand(0).getIndex(), Reg);
7272 MI.getOperand(0).ChangeToRegister(Reg, /*isDef=*/false);
7373 MI.getOperand(0).setIsDebug();
74 auto *DIExpr = DIExpression::prepend(MI.getDebugExpression(),
75 DIExpression::NoDeref, Offset);
74 auto *DIExpr = DIExpression::prepend(
75 MI.getDebugExpression(), DIExpression::ApplyOffset, Offset);
7676 MI.getOperand(3).setMetadata(DIExpr);
7777 continue;
7878 }
567567 unsigned VReg,
568568 int64_t AddrDispShift) {
569569 DIExpression *Expr = const_cast(MI.getDebugExpression());
570
571570 if (AddrDispShift != 0)
572 Expr = DIExpression::prepend(Expr, DIExpression::NoDeref, AddrDispShift,
573 DIExpression::NoDeref,
574 DIExpression::WithStackValue);
571 Expr = DIExpression::prepend(Expr, DIExpression::StackValue, AddrDispShift);
575572
576573 // Replace DBG_VALUE instruction with modified version.
577574 MachineBasicBlock *MBB = MI.getParent();
16701670 dwarf::DW_OP_constu, ValMinus,
16711671 dwarf::DW_OP_mul, dwarf::DW_OP_constu, ValInit,
16721672 dwarf::DW_OP_plus};
1673 E = DIExpression::prependOpcodes(E, Ops, DIExpression::WithStackValue);
1673 bool WithStackValue = true;
1674 E = DIExpression::prependOpcodes(E, Ops, WithStackValue);
16741675 DIGlobalVariableExpression *DGVE =
16751676 DIGlobalVariableExpression::get(NewGV->getContext(), DGV, E);
16761677 NewGV->addDebugInfo(DGVE);
30523052 Value *FakeStack;
30533053 Value *LocalStackBase;
30543054 Value *LocalStackBaseAlloca;
3055 bool Deref;
3055 uint8_t DIExprFlags = DIExpression::ApplyOffset;
30563056
30573057 if (DoStackMalloc) {
30583058 LocalStackBaseAlloca =
30933093 LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
30943094 IRB.SetCurrentDebugLocation(EntryDebugLocation);
30953095 IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca);
3096 Deref = true;
3096 DIExprFlags |= DIExpression::DerefBefore;
30973097 } else {
30983098 // void *FakeStack = nullptr;
30993099 // void *LocalStackBase = alloca(LocalStackSize);
31013101 LocalStackBase =
31023102 DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
31033103 LocalStackBaseAlloca = LocalStackBase;
3104 Deref = false;
31053104 }
31063105
31073106 // Replace Alloca instructions with base+offset.
31083107 for (const auto &Desc : SVD) {
31093108 AllocaInst *AI = Desc.AI;
3110 replaceDbgDeclareForAlloca(AI, LocalStackBaseAlloca, DIB, Deref,
3111 Desc.Offset, DIExpression::NoDeref);
3109 replaceDbgDeclareForAlloca(AI, LocalStackBaseAlloca, DIB, DIExprFlags,
3110 Desc.Offset);
31123111 Value *NewAllocaPtr = IRB.CreateIntToPtr(
31133112 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
31143113 AI->getType());
18611861 // Move any dbg.declares describing the allocas into the entry basic block.
18621862 DIBuilder DIB(*Caller->getParent());
18631863 for (auto &AI : IFI.StaticAllocas)
1864 replaceDbgDeclareForAlloca(AI, AI, DIB, DIExpression::NoDeref, 0,
1865 DIExpression::NoDeref);
1864 replaceDbgDeclareForAlloca(AI, AI, DIB, DIExpression::ApplyOffset, 0);
18661865 }
18671866
18681867 SmallVector VarArgsToForward;
15511551
15521552 bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
15531553 Instruction *InsertBefore, DIBuilder &Builder,
1554 bool DerefBefore, int Offset, bool DerefAfter) {
1554 uint8_t DIExprFlags, int Offset) {
15551555 auto DbgAddrs = FindDbgAddrUses(Address);
15561556 for (DbgVariableIntrinsic *DII : DbgAddrs) {
15571557 DebugLoc Loc = DII->getDebugLoc();
15581558 auto *DIVar = DII->getVariable();
15591559 auto *DIExpr = DII->getExpression();
15601560 assert(DIVar && "Missing variable");
1561 DIExpr = DIExpression::prepend(DIExpr, DerefBefore, Offset, DerefAfter);
1561 DIExpr = DIExpression::prepend(DIExpr, DIExprFlags, Offset);
15621562 // Insert llvm.dbg.declare immediately before InsertBefore, and remove old
15631563 // llvm.dbg.declare.
15641564 Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore);
15701570 }
15711571
15721572 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
1573 DIBuilder &Builder, bool DerefBefore,
1574 int Offset, bool DerefAfter) {
1573 DIBuilder &Builder, uint8_t DIExprFlags,
1574 int Offset) {
15751575 return replaceDbgDeclare(AI, NewAllocaAddress, AI->getNextNode(), Builder,
1576 DerefBefore, Offset, DerefAfter);
1576 DIExprFlags, Offset);
15771577 }
15781578
15791579 static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress,
23342334 // Test DIExpression::prepend().
23352335 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
23362336 auto *N0 = DIExpression::get(Context, Elts0);
2337 auto *N0WithPrependedOps = DIExpression::prepend(N0, true, 64, true, true);
2337 uint8_t DIExprFlags = DIExpression::ApplyOffset;
2338 DIExprFlags |= DIExpression::DerefBefore;
2339 DIExprFlags |= DIExpression::DerefAfter;
2340 DIExprFlags |= DIExpression::StackValue;
2341 auto *N0WithPrependedOps = DIExpression::prepend(N0, DIExprFlags, 64);
23382342 uint64_t Elts1[] = {dwarf::DW_OP_deref,
23392343 dwarf::DW_OP_plus_uconst, 64,
23402344 dwarf::DW_OP_deref,
152152 ASSERT_TRUE(DII);
153153 Value *NewBase = Constant::getNullValue(Type::getInt32PtrTy(C));
154154 DIBuilder DIB(*M);
155 replaceDbgDeclare(AI, NewBase, DII, DIB, DIExpression::NoDeref, 0,
156 DIExpression::NoDeref);
155 replaceDbgDeclare(AI, NewBase, DII, DIB, DIExpression::ApplyOffset, 0);
157156
158157 // There should be exactly two dbg.declares.
159158 int Declares = 0;