llvm.org GIT mirror llvm / 0375b23
Generalize llvm::replaceDbgDeclare and actually support the use-case that is mentioned in the documentation (inserting a deref before the plus_uconst). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320203 91177308-0d34-0410-b5e6-96231b3b80d8 Adrian Prantl 1 year, 11 months ago
11 changed file(s) with 55 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
22962296
22972297 /// Prepend \p DIExpr with a deref and offset operation and optionally turn it
22982298 /// into a stack value.
2299 static DIExpression *prepend(const DIExpression *DIExpr, bool Deref,
2300 int64_t Offset = 0, bool StackValue = false);
2299 static DIExpression *prepend(const DIExpression *DIExpr, bool DerefBefore,
2300 int64_t Offset = 0, bool DerefAfter = false,
2301 bool StackValue = false);
23012302
23022303 /// Create a DIExpression to describe one part of an aggregate variable that
23032304 /// is fragmented across multiple Values. The DW_OP_LLVM_fragment operation
334334 /// Finds the llvm.dbg.value intrinsics describing a value.
335335 void findDbgValues(SmallVectorImpl &DbgValues, Value *V);
336336
337 /// Replaces llvm.dbg.declare instruction when the address it describes
338 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
339 /// prepended to the expression. If Offset is non-zero, a constant displacement
340 /// is added to the expression (after the optional Deref). Offset can be
341 /// negative.
337 /// Replaces llvm.dbg.declare instruction when the address it
338 /// describes is replaced with a new value. If Deref is true, an
339 /// additional DW_OP_deref is prepended to the expression. If Offset
340 /// is non-zero, a constant displacement is added to the expression
341 /// (between the optional Deref operations). Offset can be negative.
342342 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
343343 Instruction *InsertBefore, DIBuilder &Builder,
344 bool Deref, int Offset);
344 bool DerefBefore, int Offset, bool DerefAfter);
345345
346346 /// Replaces llvm.dbg.declare instruction when the alloca it describes
347 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
348 /// prepended to the expression. If Offset is non-zero, a constant displacement
349 /// is added to the expression (after the optional Deref). Offset can be
350 /// negative. New llvm.dbg.declare is inserted immediately before AI.
347 /// is replaced with a new value. If Deref is true, an additional
348 /// DW_OP_deref is prepended to the expression. If Offset is non-zero,
349 /// a constant displacement is added to the expression (between the
350 /// optional Deref operations). Offset can be negative. The new
351 /// llvm.dbg.declare is inserted immediately before AI.
351352 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
352 DIBuilder &Builder, bool Deref, int Offset = 0);
353 DIBuilder &Builder, bool DerefBefore,
354 int Offset, bool DerefAfter);
353355
354356 /// Replaces multiple llvm.dbg.value instructions when the alloca it describes
355357 /// is replaced with a new value. If Offset is non-zero, a constant displacement
557557
558558 // Replace alloc with the new location.
559559 replaceDbgDeclare(Arg, BasePointer, BasePointer->getNextNode(), DIB,
560 /*Deref=*/false, -Offset);
560 DIExpression::NoDeref, -Offset, DIExpression::NoDeref);
561561 Arg->replaceAllUsesWith(NewArg);
562562 IRB.SetInsertPoint(cast(NewArg)->getNextNode());
563563 IRB.CreateMemCpy(Off, Arg, Size, Arg->getParamAlignment());
572572 if (Size == 0)
573573 Size = 1; // Don't create zero-sized stack objects.
574574
575 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, /*Deref=*/false, -Offset);
575 replaceDbgDeclareForAlloca(AI, BasePointer, DIB, DIExpression::NoDeref,
576 -Offset, DIExpression::NoDeref);
576577 replaceDbgValueForAlloca(AI, BasePointer, DIB, -Offset);
577578
578579 // Replace uses of the alloca with the new location.
662663 if (AI->hasName() && isa(NewAI))
663664 NewAI->takeName(AI);
664665
665 replaceDbgDeclareForAlloca(AI, NewAI, DIB, /*Deref=*/false);
666 replaceDbgDeclareForAlloca(AI, NewAI, DIB, DIExpression::NoDeref, 0,
667 DIExpression::NoDeref);
666668 AI->replaceAllUsesWith(NewAI);
667669 AI->eraseFromParent();
668670 }
71297129 // DW_OP_stack_value.
71307130 auto *DIExpr = DV->getExpression();
71317131 DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset,
7132 DIExpression::NoDeref,
71327133 DIExpression::WithStackValue);
71337134 SDDbgValue *Clone =
71347135 getDbgValue(DV->getVariable(), DIExpr, N0.getNode(), N0.getResNo(),
749749 return false;
750750 }
751751
752 DIExpression *DIExpression::prepend(const DIExpression *Expr, bool Deref,
753 int64_t Offset, bool StackValue) {
752 DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore,
753 int64_t Offset, bool DerefAfter,
754 bool StackValue) {
754755 SmallVector Ops;
756 if (DerefBefore)
757 Ops.push_back(dwarf::DW_OP_deref);
758
755759 appendOffset(Ops, Offset);
756 if (Deref)
760 if (DerefAfter)
757761 Ops.push_back(dwarf::DW_OP_deref);
762
758763 if (Expr)
759764 for (auto Op : Expr->expr_ops()) {
760765 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
567567
568568 if (AddrDispShift != 0)
569569 Expr = DIExpression::prepend(Expr, DIExpression::NoDeref, AddrDispShift,
570 DIExpression::NoDeref,
570571 DIExpression::WithStackValue);
571572
572573 // Replace DBG_VALUE instruction with modified version.
29172917 Value *NewAllocaPtr = IRB.CreateIntToPtr(
29182918 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
29192919 AI->getType());
2920 replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB, DIExpression::NoDeref);
2920 replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB, DIExpression::NoDeref,
2921 0, DIExpression::NoDeref);
29212922 AI->replaceAllUsesWith(NewAllocaPtr);
29222923 }
29232924
18091809 // Move any dbg.declares describing the allocas into the entry basic block.
18101810 DIBuilder DIB(*Caller->getParent());
18111811 for (auto &AI : IFI.StaticAllocas)
1812 replaceDbgDeclareForAlloca(AI, AI, DIB, /*Deref=*/false);
1812 replaceDbgDeclareForAlloca(AI, AI, DIB, DIExpression::NoDeref, 0,
1813 DIExpression::NoDeref);
18131814 }
18141815
18151816 SmallVector VarArgsToForward;
13031303
13041304 bool llvm::replaceDbgDeclare(Value *Address, Value *NewAddress,
13051305 Instruction *InsertBefore, DIBuilder &Builder,
1306 bool Deref, int Offset) {
1306 bool DerefBefore, int Offset, bool DerefAfter) {
13071307 auto DbgAddrs = FindDbgAddrUses(Address);
13081308 for (DbgInfoIntrinsic *DII : DbgAddrs) {
13091309 DebugLoc Loc = DII->getDebugLoc();
13101310 auto *DIVar = DII->getVariable();
13111311 auto *DIExpr = DII->getExpression();
13121312 assert(DIVar && "Missing variable");
1313 DIExpr = DIExpression::prepend(DIExpr, Deref, Offset);
1313 DIExpr = DIExpression::prepend(DIExpr, DerefBefore, Offset, DerefAfter);
13141314 // Insert llvm.dbg.declare immediately after InsertBefore, and remove old
13151315 // llvm.dbg.declare.
13161316 Builder.insertDeclare(NewAddress, DIVar, DIExpr, Loc, InsertBefore);
13221322 }
13231323
13241324 bool llvm::replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
1325 DIBuilder &Builder, bool Deref, int Offset) {
1325 DIBuilder &Builder, bool DerefBefore,
1326 int Offset, bool DerefAfter) {
13261327 return replaceDbgDeclare(AI, NewAllocaAddress, AI->getNextNode(), Builder,
1327 Deref, Offset);
1328 DerefBefore, Offset, DerefAfter);
13281329 }
13291330
13301331 static void replaceOneDbgValueForAlloca(DbgValueInst *DVI, Value *NewAddress,
13771378 auto applyOffset = [&](DbgValueInst *DVI, uint64_t Offset) {
13781379 auto *DIExpr = DVI->getExpression();
13791380 DIExpr = DIExpression::prepend(DIExpr, DIExpression::NoDeref, Offset,
1381 DIExpression::NoDeref,
13801382 DIExpression::WithStackValue);
13811383 DVI->setOperand(0, wrapMD(I.getOperand(0)));
13821384 DVI->setOperand(2, MetadataAsValue::get(I.getContext(), DIExpr));
20302030
20312031 TempDIExpression Temp = N->clone();
20322032 EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
2033
2034 // Test DIExpression::prepend().
2035 uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
2036 auto *N0 = DIExpression::get(Context, Elts0);
2037 N0 = DIExpression::prepend(N0, true, 64, true, true);
2038 uint64_t Elts1[] = {dwarf::DW_OP_deref,
2039 dwarf::DW_OP_plus_uconst, 64,
2040 dwarf::DW_OP_deref,
2041 dwarf::DW_OP_stack_value,
2042 dwarf::DW_OP_LLVM_fragment, 0, 32};
2043 auto *N1 = DIExpression::get(Context, Elts1);
2044 EXPECT_EQ(N0, N1);
20332045 }
20342046
20352047 TEST_F(DIExpressionTest, isValid) {
156156 ASSERT_TRUE(DII);
157157 Value *NewBase = Constant::getNullValue(Type::getInt32PtrTy(C));
158158 DIBuilder DIB(*M);
159 replaceDbgDeclare(AI, NewBase, DII, DIB, /*Deref=*/false, /*Offset=*/0);
159 replaceDbgDeclare(AI, NewBase, DII, DIB, DIExpression::NoDeref, 0,
160 DIExpression::NoDeref);
160161
161162 // There should be exactly two dbg.declares.
162163 int Declares = 0;