@@ -1816,8 +1816,12 @@ void at::RAUW(DIAssignID *Old, DIAssignID *New) {
18161816
18171817void at::deleteAll (Function *F) {
18181818 SmallVector<DbgAssignIntrinsic *, 12 > ToDelete;
1819+ SmallVector<DPValue *, 12 > DPToDelete;
18191820 for (BasicBlock &BB : *F) {
18201821 for (Instruction &I : BB) {
1822+ for (auto &DPV : I.getDbgValueRange ())
1823+ if (DPV.isDbgAssign ())
1824+ DPToDelete.push_back (&DPV);
18211825 if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(&I))
18221826 ToDelete.push_back (DAI);
18231827 else
@@ -1826,6 +1830,8 @@ void at::deleteAll(Function *F) {
18261830 }
18271831 for (auto *DAI : ToDelete)
18281832 DAI->eraseFromParent ();
1833+ for (auto *DPV : DPToDelete)
1834+ DPV->eraseFromParent ();
18291835}
18301836
18311837// / Get the FragmentInfo for the variable if it exists, otherwise return a
@@ -2056,9 +2062,9 @@ std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL,
20562062}
20572063
20582064// / Returns nullptr if the assignment shouldn't be attributed to this variable.
2059- static CallInst * emitDbgAssign (AssignmentInfo Info, Value *Val, Value *Dest,
2060- Instruction &StoreLikeInst ,
2061- const VarRecord &VarRec, DIBuilder &DIB) {
2065+ static void emitDbgAssign (AssignmentInfo Info, Value *Val, Value *Dest,
2066+ Instruction &StoreLikeInst, const VarRecord &VarRec ,
2067+ DIBuilder &DIB) {
20622068 auto *ID = StoreLikeInst.getMetadata (LLVMContext::MD_DIAssignID);
20632069 assert (ID && " Store instruction must have DIAssignID metadata" );
20642070 (void )ID;
@@ -2082,7 +2088,7 @@ static CallInst *emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest,
20822088
20832089 // Discard stores to bits outside this variable.
20842090 if (FragStartBit >= FragEndBit)
2085- return nullptr ;
2091+ return ;
20862092
20872093 StoreToWholeVariable = FragStartBit <= VarStartBit && FragEndBit >= *Size;
20882094 }
@@ -2097,8 +2103,17 @@ static CallInst *emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest,
20972103 }
20982104 DIExpression *AddrExpr =
20992105 DIExpression::get (StoreLikeInst.getContext (), std::nullopt );
2100- return DIB.insertDbgAssign (&StoreLikeInst, Val, VarRec.Var , Expr, Dest,
2101- AddrExpr, VarRec.DL );
2106+ if (StoreLikeInst.getParent ()->IsNewDbgInfoFormat ) {
2107+ auto *Assign = DPValue::createLinkedDPVAssign (
2108+ &StoreLikeInst, Val, VarRec.Var , Expr, Dest, AddrExpr, VarRec.DL );
2109+ (void )Assign;
2110+ LLVM_DEBUG (if (Assign) errs () << " > INSERT: " << *Assign << " \n " );
2111+ return ;
2112+ }
2113+ auto *Assign = DIB.insertDbgAssign (&StoreLikeInst, Val, VarRec.Var , Expr,
2114+ Dest, AddrExpr, VarRec.DL );
2115+ (void )Assign;
2116+ LLVM_DEBUG (if (Assign) errs () << " > INSERT: " << *Assign << " \n " );
21022117}
21032118
21042119#undef DEBUG_TYPE // Silence redefinition warning (from ConstantsContext.h).
@@ -2185,12 +2200,8 @@ void at::trackAssignments(Function::iterator Start, Function::iterator End,
21852200 I.setMetadata (LLVMContext::MD_DIAssignID, ID);
21862201 }
21872202
2188- for (const VarRecord &R : LocalIt->second ) {
2189- auto *Assign =
2190- emitDbgAssign (*Info, ValueComponent, DestComponent, I, R, DIB);
2191- (void )Assign;
2192- LLVM_DEBUG (if (Assign) errs () << " > INSERT: " << *Assign << " \n " );
2193- }
2203+ for (const VarRecord &R : LocalIt->second )
2204+ emitDbgAssign (*Info, ValueComponent, DestComponent, I, R, DIB);
21942205 }
21952206 }
21962207}
@@ -2206,32 +2217,38 @@ bool AssignmentTrackingPass::runOnFunction(Function &F) {
22062217 // storage" is limited to Allocas). We'll use this to find dbg.declares to
22072218 // delete after running `trackAssignments`.
22082219 DenseMap<const AllocaInst *, SmallPtrSet<DbgDeclareInst *, 2 >> DbgDeclares;
2220+ DenseMap<const AllocaInst *, SmallPtrSet<DPValue *, 2 >> DPVDeclares;
22092221 // Create another similar map of {storage : variables} that we'll pass to
22102222 // trackAssignments.
22112223 StorageToVarsMap Vars;
2224+ auto ProcessDeclare = [&](auto *Declare, auto &DeclareList) {
2225+ // FIXME: trackAssignments doesn't let you specify any modifiers to the
2226+ // variable (e.g. fragment) or location (e.g. offset), so we have to
2227+ // leave dbg.declares with non-empty expressions in place.
2228+ if (Declare->getExpression ()->getNumElements () != 0 )
2229+ return ;
2230+ if (!Declare->getAddress ())
2231+ return ;
2232+ if (AllocaInst *Alloca =
2233+ dyn_cast<AllocaInst>(Declare->getAddress ()->stripPointerCasts ())) {
2234+ // FIXME: Skip VLAs for now (let these variables use dbg.declares).
2235+ if (!Alloca->isStaticAlloca ())
2236+ return ;
2237+ // Similarly, skip scalable vectors (use dbg.declares instead).
2238+ if (auto Sz = Alloca->getAllocationSize (*DL); Sz && Sz->isScalable ())
2239+ return ;
2240+ DeclareList[Alloca].insert (Declare);
2241+ Vars[Alloca].insert (VarRecord (Declare));
2242+ }
2243+ };
22122244 for (auto &BB : F) {
22132245 for (auto &I : BB) {
2214- DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(&I);
2215- if (!DDI)
2216- continue ;
2217- // FIXME: trackAssignments doesn't let you specify any modifiers to the
2218- // variable (e.g. fragment) or location (e.g. offset), so we have to
2219- // leave dbg.declares with non-empty expressions in place.
2220- if (DDI->getExpression ()->getNumElements () != 0 )
2221- continue ;
2222- if (!DDI->getAddress ())
2223- continue ;
2224- if (AllocaInst *Alloca =
2225- dyn_cast<AllocaInst>(DDI->getAddress ()->stripPointerCasts ())) {
2226- // FIXME: Skip VLAs for now (let these variables use dbg.declares).
2227- if (!Alloca->isStaticAlloca ())
2228- continue ;
2229- // Similarly, skip scalable vectors (use dbg.declares instead).
2230- if (auto Sz = Alloca->getAllocationSize (*DL); Sz && Sz->isScalable ())
2231- continue ;
2232- DbgDeclares[Alloca].insert (DDI);
2233- Vars[Alloca].insert (VarRecord (DDI));
2246+ for (auto &DPV : I.getDbgValueRange ()) {
2247+ if (DPV.isDbgDeclare ())
2248+ ProcessDeclare (&DPV, DPVDeclares);
22342249 }
2250+ if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(&I))
2251+ ProcessDeclare (DDI, DbgDeclares);
22352252 }
22362253 }
22372254
@@ -2247,35 +2264,30 @@ bool AssignmentTrackingPass::runOnFunction(Function &F) {
22472264 trackAssignments (F.begin (), F.end (), Vars, *DL);
22482265
22492266 // Delete dbg.declares for variables now tracked with assignment tracking.
2250- for (auto &P : DbgDeclares) {
2251- const AllocaInst *Alloca = P.first ;
2252- auto Markers = at::getAssignmentMarkers (Alloca);
2253- SmallVector<DPValue *> DPMarkers = at::getDPVAssignmentMarkers (Alloca);
2267+ auto DeleteSubsumedDeclare = [&](const auto &Markers, auto &Declares) {
22542268 (void )Markers;
2255- (void )DPMarkers;
2256- for (DbgDeclareInst *DDI : P.second ) {
2257- // Assert that the alloca that DDI uses is now linked to a dbg.assign
2269+ for (auto *Declare : Declares) {
2270+ // Assert that the alloca that Declare uses is now linked to a dbg.assign
22582271 // describing the same variable (i.e. check that this dbg.declare has
22592272 // been replaced by a dbg.assign). Use DebugVariableAggregate to Discard
22602273 // the fragment part because trackAssignments may alter the
22612274 // fragment. e.g. if the alloca is smaller than the variable, then
22622275 // trackAssignments will create an alloca-sized fragment for the
22632276 // dbg.assign.
2264- assert (llvm::any_of (Markers,
2265- [DDI](DbgAssignIntrinsic *DAI) {
2266- return DebugVariableAggregate (DAI) ==
2267- DebugVariableAggregate (DDI);
2268- }) ||
2269- llvm::any_of (DPMarkers, [DDI](DPValue *DPV) {
2270- return DebugVariableAggregate (DPV) ==
2271- DebugVariableAggregate (DDI);
2272- }));
2273- // Delete DDI because the variable location is now tracked using
2277+ assert (llvm::any_of (Markers, [Declare](auto *Assign) {
2278+ return DebugVariableAggregate (Assign) ==
2279+ DebugVariableAggregate (Declare);
2280+ }));
2281+ // Delete Declare because the variable location is now tracked using
22742282 // assignment tracking.
2275- DDI ->eraseFromParent ();
2283+ Declare ->eraseFromParent ();
22762284 Changed = true ;
22772285 }
2278- }
2286+ };
2287+ for (auto &P : DbgDeclares)
2288+ DeleteSubsumedDeclare (at::getAssignmentMarkers (P.first ), P.second );
2289+ for (auto &P : DPVDeclares)
2290+ DeleteSubsumedDeclare (at::getDPVAssignmentMarkers (P.first ), P.second );
22792291 return Changed;
22802292}
22812293
0 commit comments