@@ -4838,6 +4838,35 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
48384838 return NewAI;
48394839}
48404840
4841+ static void insertNewDbgInst (DIBuilder &DIB, DbgDeclareInst *Orig,
4842+ AllocaInst *NewAddr, DIExpression *NewFragmentExpr,
4843+ Instruction *BeforeInst) {
4844+ DIB.insertDeclare (NewAddr, Orig->getVariable (), NewFragmentExpr,
4845+ Orig->getDebugLoc (), BeforeInst);
4846+ }
4847+ static void insertNewDbgInst (DIBuilder &DIB, DbgAssignIntrinsic *Orig,
4848+ AllocaInst *NewAddr, DIExpression *NewFragmentExpr,
4849+ Instruction *BeforeInst) {
4850+ (void )BeforeInst;
4851+ if (!NewAddr->hasMetadata (LLVMContext::MD_DIAssignID)) {
4852+ NewAddr->setMetadata (LLVMContext::MD_DIAssignID,
4853+ DIAssignID::getDistinct (NewAddr->getContext ()));
4854+ }
4855+ auto *NewAssign = DIB.insertDbgAssign (
4856+ NewAddr, Orig->getValue (), Orig->getVariable (), NewFragmentExpr, NewAddr,
4857+ Orig->getAddressExpression (), Orig->getDebugLoc ());
4858+ LLVM_DEBUG (dbgs () << " Created new assign intrinsic: " << *NewAssign << " \n " );
4859+ }
4860+ static void insertNewDbgInst (DIBuilder &DIB, DPValue *Orig, AllocaInst *NewAddr,
4861+ DIExpression *NewFragmentExpr,
4862+ Instruction *BeforeInst) {
4863+ (void )DIB;
4864+ DPValue *New = new DPValue (ValueAsMetadata::get (NewAddr), Orig->getVariable (),
4865+ NewFragmentExpr, Orig->getDebugLoc (),
4866+ DPValue::LocationType::Declare);
4867+ BeforeInst->getParent ()->insertDPValueBefore (New, BeforeInst->getIterator ());
4868+ }
4869+
48414870// / Walks the slices of an alloca and form partitions based on them,
48424871// / rewriting each of their uses.
48434872bool SROA::splitAlloca (AllocaInst &AI, AllocaSlices &AS) {
@@ -4939,15 +4968,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
49394968
49404969 // Migrate debug information from the old alloca to the new alloca(s)
49414970 // and the individual partitions.
4942- TinyPtrVector<DbgVariableIntrinsic *> DbgVariables;
4943- SmallVector<DbgDeclareInst *, 1 > DbgDeclares;
4944- findDbgDeclares (DbgDeclares, &AI);
4945- for (auto *DbgDeclare : DbgDeclares)
4946- DbgVariables.push_back (DbgDeclare);
4947- for (auto *DbgAssign : at::getAssignmentMarkers (&AI))
4948- DbgVariables.push_back (DbgAssign);
4949-
4950- for (DbgVariableIntrinsic *DbgVariable : DbgVariables) {
4971+ auto MigrateOne = [&](auto *DbgVariable) {
49514972 auto *Expr = DbgVariable->getExpression ();
49524973 DIBuilder DIB (*AI.getModule (), /* AllowUnresolved*/ false );
49534974 uint64_t AllocaSize =
@@ -5001,37 +5022,33 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
50015022 // Remove any existing intrinsics on the new alloca describing
50025023 // the variable fragment.
50035024 SmallVector<DbgDeclareInst *, 1 > FragDbgDeclares;
5004- findDbgDeclares (FragDbgDeclares, Fragment. Alloca ) ;
5005- for (DbgDeclareInst *OldDII : FragDbgDeclares) {
5006- auto SameVariableFragment = []( const DbgVariableIntrinsic *LHS,
5007- const DbgVariableIntrinsic *RHS) {
5025+ SmallVector<DPValue *, 1 > FragDPVs ;
5026+ findDbgDeclares (FragDbgDeclares, Fragment. Alloca , &FragDPVs);
5027+ auto RemoveOne = [DbgVariable]( auto *OldDII) {
5028+ auto SameVariableFragment = []( const auto *LHS, const auto *RHS) {
50085029 return LHS->getVariable () == RHS->getVariable () &&
50095030 LHS->getDebugLoc ()->getInlinedAt () ==
50105031 RHS->getDebugLoc ()->getInlinedAt ();
50115032 };
50125033 if (SameVariableFragment (OldDII, DbgVariable))
50135034 OldDII->eraseFromParent ();
5014- }
5035+ };
5036+ for_each (FragDbgDeclares, RemoveOne);
5037+ for_each (FragDPVs, RemoveOne);
50155038
5016- if (auto *DbgAssign = dyn_cast<DbgAssignIntrinsic>(DbgVariable)) {
5017- if (!Fragment.Alloca ->hasMetadata (LLVMContext::MD_DIAssignID)) {
5018- Fragment.Alloca ->setMetadata (
5019- LLVMContext::MD_DIAssignID,
5020- DIAssignID::getDistinct (AI.getContext ()));
5021- }
5022- auto *NewAssign = DIB.insertDbgAssign (
5023- Fragment.Alloca , DbgAssign->getValue (), DbgAssign->getVariable (),
5024- FragmentExpr, Fragment.Alloca , DbgAssign->getAddressExpression (),
5025- DbgAssign->getDebugLoc ());
5026- NewAssign->setDebugLoc (DbgAssign->getDebugLoc ());
5027- LLVM_DEBUG (dbgs () << " Created new assign intrinsic: " << *NewAssign
5028- << " \n " );
5029- } else {
5030- DIB.insertDeclare (Fragment.Alloca , DbgVariable->getVariable (),
5031- FragmentExpr, DbgVariable->getDebugLoc (), &AI);
5032- }
5039+ insertNewDbgInst (DIB, DbgVariable, Fragment.Alloca , FragmentExpr, &AI);
50335040 }
5034- }
5041+ };
5042+
5043+ // Migrate debug information from the old alloca to the new alloca(s)
5044+ // and the individual partitions.
5045+ SmallVector<DbgDeclareInst *, 1 > DbgDeclares;
5046+ SmallVector<DPValue *, 1 > DPValues;
5047+ findDbgDeclares (DbgDeclares, &AI, &DPValues);
5048+ for_each (DbgDeclares, MigrateOne);
5049+ for_each (DPValues, MigrateOne);
5050+ for_each (at::getAssignmentMarkers (&AI), MigrateOne);
5051+
50355052 return Changed;
50365053}
50375054
@@ -5153,9 +5170,12 @@ bool SROA::deleteDeadInstructions(
51535170 if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
51545171 DeletedAllocas.insert (AI);
51555172 SmallVector<DbgDeclareInst *, 1 > DbgDeclares;
5156- findDbgDeclares (DbgDeclares, AI);
5173+ SmallVector<DPValue *, 1 > DPValues;
5174+ findDbgDeclares (DbgDeclares, AI, &DPValues);
51575175 for (DbgDeclareInst *OldDII : DbgDeclares)
51585176 OldDII->eraseFromParent ();
5177+ for (DPValue *OldDII : DPValues)
5178+ OldDII->eraseFromParent ();
51595179 }
51605180
51615181 at::deleteAssignmentMarkers (I);
0 commit comments